CustomCoupon.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php namespace App\Http\Controllers\Admin;
  2. use App\Models\Business;
  3. use App\Models\Custom;
  4. use App\Models\CustomCoupon as Model;
  5. use App\Models\Coupon as Coupon;
  6. use App\Http\Requests\Admin\CustomCoupon as Request;
  7. use App\Models\AdminUser;
  8. /**
  9. * 优惠券管理
  10. *
  11. * @author 刘相欣
  12. *
  13. */
  14. class CustomCoupon extends Auth{
  15. protected function _initialize(){
  16. parent::_initialize();
  17. $this->assign('breadcrumb1','营销管理');
  18. $this->assign('breadcrumb2','客户优惠券');
  19. }
  20. /**
  21. * 首页列表
  22. *
  23. * */
  24. public function index(Model $Model,Coupon $Coupon,Custom $Custom,AdminUser $AdminUser){
  25. // 接受参数
  26. $code = request('coupon_code','');
  27. $customCode = request('custom_code','');
  28. $startTime = request('start_time','');
  29. $endTime = request('end_time','');
  30. $status = request('status');
  31. // 编码转ID
  32. $couponId = $code ? $Coupon->codeToId($code) : 0;
  33. $customUid = $customCode ? $Custom->codeToId($customCode) : 0;
  34. // 查询条件
  35. $map = [];
  36. // 编码ID
  37. if( $couponId ) $map[] = ['coupon.id','=',$couponId];
  38. if( $customUid ) $map[] = ['custom_coupon.custom_uid','=',$customUid];
  39. if( $startTime ) $map[] = ['custom_coupon.insert_time','>=',strtotime($startTime)];
  40. if( $endTime ) $map[] = ['custom_coupon.insert_time','<=',strtotime($endTime)];
  41. if( !is_null($status) ) $map[] = ['custom_coupon.status','=',$status];
  42. // 查询数据
  43. $list = $Model
  44. ->query()->join('coupon','custom_coupon.coupon_id','=','coupon.id');
  45. $list = $list->where($map)
  46. ->orderBy('custom_coupon.status')
  47. ->orderByDesc('custom_coupon.id')
  48. ->select(['custom_coupon.*','coupon.name as coupon_name','coupon.type_id','coupon.rebate_type','coupon.std_pay','coupon.rebate','custom_coupon.exp_time'])
  49. ->paginate(request('limit',config('page_num',10)))
  50. ->appends(request()->all());
  51. // 循环处理数据
  52. foreach ($list as $key => $value) {
  53. // id转编号
  54. $value['coupon_code']= $Coupon->idToCode($value['coupon_id']);
  55. // id转编号
  56. $value['custom_code'] = $Custom->idToCode($value['custom_uid']);
  57. // id转编号
  58. $value['custom_name'] = $Custom->getValue($value['custom_uid'],'username');
  59. // id转编号
  60. $value['admin_name'] = $AdminUser->getOne($value['admin_uid'],'username');
  61. // 如果过期时间
  62. if( $value['status'] == 0 && $value['exp_time'] <= time() ) {
  63. // 状态设置
  64. $Model->edit($value['id'],['status'=>3]);
  65. // 状态设置
  66. $value['status'] = 3;
  67. }
  68. // 重组
  69. $list[$key] = $value;
  70. }
  71. // 分配数据
  72. $this->assign('empty', '<tr><td colspan="20">~~暂无数据</td></tr>');
  73. $this->assign('list', $list);
  74. // 加载模板
  75. return $this->fetch();
  76. }
  77. /**
  78. * 发放优惠券
  79. *
  80. * */
  81. public function add(Request $request,Model $Model,Coupon $Coupon,Custom $Custom){
  82. if( request()->isMethod('post') ){
  83. // 验证参数
  84. $request->scene('add')->validate();
  85. // 接收数据
  86. $couponCode = request('coupon_code','');
  87. $customCodes = request('custom_codes','');
  88. $couponId = $Coupon->codeToId($couponCode);
  89. // 如果操作失败
  90. if( !$couponId ) return json_send(['code'=>'error','msg'=>'请填写正确的优惠券编码']);
  91. // 查询优惠券信息
  92. $couponInfo = $Coupon->query()->find($couponId);
  93. // 提示
  94. if( !$couponId ) return json_send(['code'=>'error','msg'=>'请填写正确的优惠券编码']);
  95. // 兼容逗号问题
  96. $customCodes = str_ireplace(',',',',$customCodes);
  97. $customCodes = str_ireplace("\r\n",',',$customCodes);
  98. $customCodes = str_ireplace("\n",',',$customCodes);
  99. // 转数组处理
  100. $customCodes = explode(',',$customCodes);
  101. // 循环处理
  102. foreach ($customCodes as $key=>$value) {
  103. // 转ID
  104. $customUid = $Custom->codeToId($value);
  105. // id有问题
  106. if( !$customUid ) return json_send(['code'=>'error','msg'=>'客户编码'.$value.'不存在']);
  107. // 重组
  108. $customCodes[$key] = $customUid;
  109. }
  110. // 去重
  111. $customCodes = array_unique($customCodes);
  112. // 数量
  113. if( count($customCodes) > 10000 ) return json_send(['code'=>'error','msg'=>'客户编码请勿超过10000']);
  114. // 排除数据
  115. $customCodes = is_array($customCodes) ? $customCodes : [];
  116. // 插入列表
  117. $insertList = [];
  118. // 时间
  119. $time = time();
  120. // 循环客户
  121. foreach ($customCodes as $key => $value) {
  122. // 是否存在已发放的客户
  123. $oldId = $Model->query()->where([['coupon_id','=',$couponId],['custom_uid','=',$value]])->value('id');
  124. // 如果存在ID
  125. if( $oldId ) continue;
  126. // 批量写入列表
  127. $insertList[] = ['coupon_id'=>$couponId,'custom_uid'=>$value,'exp_time'=>$Coupon->getExpTime($couponInfo['exp_time']),'admin_uid'=>admin('uid'),'insert_time'=>$time,'update_time'=>$time];
  128. }
  129. // 查询该优惠券已经发放的数量
  130. $countTotal = $Model->query()->where([['coupon_id','=',$couponId]])->count();
  131. // 如果超过数量,不允许发放
  132. if( $couponInfo['issue_total'] < (count($insertList) + $countTotal) ) return json_send(['code'=>'error','msg'=>'发放超过发行数量 => '.((count($insertList) + $countTotal) - $couponInfo['issue_total'])]);
  133. // 如果有客户范围
  134. if( $insertList ) {
  135. // 分块,每1000一批写入
  136. foreach (array_chunk($insertList,1000) as $chunk) {
  137. // 写入数据
  138. $result = $Model->insert($chunk);
  139. // 提示新增失败
  140. if( !$result ) {
  141. // 提示失败
  142. return json_send(['code'=>'error','msg'=>'指定客户写入失败']);
  143. }
  144. }
  145. }
  146. // 告知结果
  147. return json_send(['code'=>'success','msg'=>'新增成功','action'=>'add','path'=>url('admin/custom_coupon/index?'.http_build_query(['coupon_code'=>$couponCode]))]);
  148. }
  149. // 优惠券ID
  150. $couponCode = request('coupon_code','');
  151. $couponId = $Coupon->codeToId($couponCode);
  152. // 提示
  153. if( !$couponId ) return $this->error('请填写正确的优惠券编码');
  154. // 查询优惠券信息
  155. $couponInfo = $Coupon->query()->find($couponId);
  156. // 提示
  157. if( !$couponId ) return $this->error('请填写正确的优惠券编码');
  158. // 分配数据
  159. $this->assign('crumbs','发放优惠券');
  160. $this->assign('couponInfo',$couponInfo);
  161. // 加载模板
  162. return $this->fetch();
  163. }
  164. /**
  165. * 状态
  166. *
  167. * */
  168. public function set_status( Request $request, Model $Model){
  169. // 验证参数
  170. $request->scene('set_status')->validate();
  171. // 接收参数
  172. $id = request('id',0);
  173. $status = request('status',0);
  174. // 查询数据
  175. $result = $Model->edit($id,['status'=>$status]);
  176. // 提示新增失败
  177. if( !$result ) return json_send(['code'=>'error','msg'=>'设置失败']);
  178. // 记录行为
  179. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,[],['status'=>$status]);
  180. // 告知结果
  181. return json_send(['code'=>'success','msg'=>'设置成功','path'=>'']);
  182. }
  183. }