CustomCoupon.php 6.8 KB

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