common.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. // 接口公共方法
  3. use think\Db;
  4. // 更新商品库存
  5. function update_goods_stock($goods_id,$goods_no,$num){
  6. $goods_info = Db::table('store_goods')
  7. ->field('id,spec,stock')
  8. ->where(['id'=>$goods_id])
  9. ->find();
  10. $spec = json_decode($goods_info['spec'],true);
  11. foreach ($spec as $gk=>&$gv) {
  12. if($gv['goods_no'] == $goods_no) {
  13. $spec[$gk]['store_num'] = bcsub($gv['store_num'],$num) > 0 ? bcsub($gv['store_num'],$num) : 0;
  14. }
  15. }
  16. $stock = $goods_info['stock']-$num >0 ? $goods_info['stock']-$num:0;
  17. return Db::table('store_goods')->where(['id'=>$goods_id])->update(['spec'=>json_encode($spec),'stock'=>$stock]);
  18. }
  19. // 获取退款理由
  20. function get_refund_cause()
  21. {
  22. $list = Db::table('order_refund_cause')
  23. ->field('id,title')
  24. ->where(['status'=>1])
  25. ->order('sort desc , id asc')
  26. ->select();
  27. return $list;
  28. }
  29. //获取当月日历
  30. function get_month_days(){
  31. $date = date('Y-m-d');
  32. $start_time = strtotime($date);
  33. $total_month_day = date('t', $start_time);// 当月天数
  34. $month_data = [];
  35. $base_data = date("Y-m");
  36. for($i=1;$i<=$total_month_day;$i++) {
  37. $c = $i;
  38. if($c < 10) $c ='0'.$i;
  39. $month_data[] = [
  40. 'date_str'=>$base_data.'-'.$c,
  41. 'date' => $i,
  42. 'prefix_date' => $c,
  43. 'week' => date('w',strtotime($base_data.'-'.$c))
  44. ];
  45. }
  46. return $month_data;
  47. }
  48. function get_order_sn(){
  49. $order_id_main = date('YmdHis') . rand(10000000,99999999);
  50. $order_id_len = strlen($order_id_main);
  51. $order_id_sum = 0;
  52. for($i=0; $i<$order_id_len; $i++){
  53. $order_id_sum += (int)(substr($order_id_main,$i,1));
  54. }
  55. $osn = $order_id_main . str_pad((100 - $order_id_sum % 100) % 100,2,'0',STR_PAD_LEFT);
  56. return $osn;
  57. }
  58. /**
  59. *随机抽奖
  60. * @param array $data
  61. * @param string $field
  62. * @param int $num
  63. * @return array ['抽中的键'=>'抽中次数']
  64. */
  65. function draw_lottery ($data = [],$field='',$num = 1,$is_clip=0)
  66. {
  67. $draw_data = [];
  68. foreach ($data as $k=>$v)
  69. {
  70. if($is_clip && $k== 0) continue;
  71. if($v[$field] > 0)
  72. {
  73. for($i=1;$i<= $v[$field];$i++) {
  74. $draw_data[] = $k.'_key_'.$i;
  75. }
  76. }
  77. }
  78. $rand_key = array_rand($draw_data,$num);
  79. $draw_key = [];
  80. if($num == 1) {
  81. $sel_key = explode( '_key_',$draw_data[$rand_key])[0];
  82. $draw_key[$sel_key] = 1;
  83. }else{
  84. for ($a=0;$a<$num;$a++) {
  85. $sel_key = explode( '_key_',$draw_data[$rand_key[$a]])[0];
  86. if(isset($draw_key[$sel_key])) {
  87. $draw_key[$sel_key]++;
  88. }else{
  89. $draw_key[$sel_key] = 1;
  90. }
  91. }
  92. }
  93. return $draw_key;
  94. }
  95. /**
  96. *指定抽奖
  97. * @param array $data
  98. * @param string $field
  99. * @param int $num
  100. * @param int $stock 剩余库存
  101. * @return array ['抽中的键'=>'抽中次数']
  102. */
  103. function point_lottery ($data = [],$field='',$num = 1,$check_num = 0)
  104. {
  105. $draw_data = [];
  106. $draw_key = [];
  107. foreach ($data as $k=>$v) {
  108. // 指定前三个中奖
  109. if(in_array($k,[0,1,2]) && isset($v['point_num'])) {
  110. // 剩余库存 > 0 && 售卖数量>= 指定数量 && 可以抽奖
  111. if( $v['store_num'] > 0 && $check_num >= $v['point_num'] && $num > 0){
  112. $draw_key[$k] = 1;
  113. $num = $num - 1;
  114. }
  115. }elseif($v[$field] > 0 ) {
  116. for($i=1;$i<= $v[$field];$i++) {
  117. $draw_data[] = $k.'_key_'.$i;
  118. }
  119. }
  120. }
  121. if($num == 0) return $draw_key;// 不可以抽奖
  122. $rand_key = array_rand($draw_data,$num);
  123. if($num == 1) {
  124. $sel_key = explode( '_key_',$draw_data[$rand_key])[0];
  125. $draw_key[$sel_key] = 1;
  126. }else{
  127. for ($a=0;$a<$num;$a++) {
  128. $sel_key = explode( '_key_',$draw_data[$rand_key[$a]])[0];
  129. if(isset($draw_key[$sel_key])) {
  130. $draw_key[$sel_key]++;
  131. }else{
  132. $draw_key[$sel_key] = 1;
  133. }
  134. }
  135. }
  136. return $draw_key;
  137. }
  138. function cdraw_lottery($data,$draw_key,$user_id=0,$sel_key)
  139. {
  140. $keys = max(array_keys($draw_key));
  141. if($data[$sel_key]['store_num'] > 0 && $keys != $sel_key) {
  142. $draw_key[$keys] = $draw_key[$keys] - 1;
  143. if($draw_key[$keys] == 0) unset($draw_key[$keys]);
  144. if(isset($draw_key[$sel_key])) {
  145. $draw_key[$sel_key] = $draw_key[$sel_key]+1;
  146. }else{
  147. $draw_key[$sel_key] = 1;
  148. }
  149. }
  150. return $draw_key;
  151. }
  152. //获取运费
  153. function get_freight($num)
  154. {
  155. $first_freight = floatval(sysconf('first_freight_price')) > 0 ? floatval(sysconf('first_freight_price')):0;
  156. $later_freight = floatval(sysconf('later_freight_price')) > 0 ? floatval(sysconf('later_freight_price')):0;
  157. $all_freight = bcadd($first_freight,bcmul($num-1,$later_freight,2),2);
  158. return ['all_freight'=>$all_freight,'first_freight'=>$first_freight,'later_freight'=>$later_freight];
  159. }
  160. // 生成消息记录
  161. function send_user_message($user_id,$type_id,$content){
  162. $msg_data =[
  163. 'user_id' => $user_id,
  164. 'type_id' => $type_id,
  165. 'content' => $content,
  166. 'create_at' => date('Y-m-d H:i:s'),
  167. ];
  168. Db::table('user_message')->insert($msg_data);
  169. return Db::getLastInsID();
  170. }
  171. // 自动分解魔玩柜
  172. function resolve_magic($user_id = 0)
  173. {
  174. $time = time();
  175. $where = [];
  176. $where[] = ['num','>',0];
  177. $where[] = ['past_int','<',$time];
  178. if($user_id) $where[] = ['user_id','=',$user_id];
  179. $magic_case = Db::table('magic_case')
  180. ->field('id,user_id,num,crystal')
  181. ->where($where)
  182. ->select();
  183. foreach ($magic_case as $value) {
  184. // 扣减魔玩柜变更记录
  185. $crystal = bcmul($value['num'],$value['crystal'],2);
  186. $log_data= [
  187. 'user_id' => $value['user_id'],
  188. 'create_at' => date('Y-m-d H:i:s'),
  189. 'num' => $value['num'],
  190. 'case_id' => $value['id'],
  191. 'crystal' =>$crystal ,
  192. ];
  193. //更新明信片余额
  194. $member_info = Db::table('store_member')->field('id,crystal_cash')->find($value['user_id']);
  195. // 扣减魔玩柜的数量
  196. Db::startTrans();
  197. $res1= Db::table('magic_case')->where('id',$value['id'])->update(['num'=>0]);
  198. $res2 = Db::table('store_member')->where('id',$value['user_id'])->update(['crystal_cash'=>bcadd($member_info['crystal_cash'],$crystal,2)]);
  199. Db::table('magic_resolve')->insert($log_data);// 分解日志
  200. // 余额日志
  201. crystal_log($value['user_id'],$crystal,'魔玩柜分解',3);
  202. if($res1 && $res2) {
  203. Db::commit();
  204. }else{
  205. Db::rollback();
  206. }
  207. }
  208. }
  209. // 运费券过期
  210. function user_coupon_over($user_id = 0)
  211. {
  212. $where = [];
  213. $where[] = ['status','=',1];
  214. $where[] = ['past_at','< time',date('Y-m-d H:i:s')];
  215. if($user_id) $where[] = ['user_id','=',$user_id];
  216. Db::table('user_coupon_list')
  217. ->where($where)
  218. ->update(['status'=>3]);
  219. }