CustomCoupon.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 Illuminate\Support\Facades\DB;
  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){
  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.id','coupon.id as coupon_id','coupon.name as coupon_name','coupon.type_id','coupon.rebate_type','coupon.std_pay','coupon.rebate','custom_coupon.status','custom_coupon.custom_uid','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. // 如果过期时间
  59. if( $value['status'] == 0 && ( $value['exp_time']>0 && $value['exp_time']< time() ) ) {
  60. // 状态设置
  61. $Model->edit($value['id'],['status'=>3]);
  62. // 状态设置
  63. $value['status'] = 3;
  64. }
  65. // 重组
  66. $list[$key] = $value;
  67. }
  68. // 分配数据
  69. $this->assign('empty', '<tr><td colspan="20">~~暂无数据</td></tr>');
  70. $this->assign('list', $list);
  71. // 加载模板
  72. return $this->fetch();
  73. }
  74. /**
  75. * 发放优惠券
  76. *
  77. * */
  78. public function add(Request $request,Model $Model,Coupon $Coupon,Custom $Custom){
  79. if( request()->isMethod('post') ){
  80. // 验证参数
  81. $request->scene('add')->validate();
  82. // 接收数据
  83. $couponCode = request('coupon_code','');
  84. $customCodes = request('custom_codes','');
  85. $couponId = $Coupon->codeToId($couponCode);
  86. // 如果操作失败
  87. if( !$couponId ) return json_send(['code'=>'error','msg'=>'请填写正确的优惠券编码']);
  88. // 查询优惠券信息
  89. $couponInfo = $Coupon->query()->find($couponId);
  90. // 提示
  91. if( !$couponId ) return json_send(['code'=>'error','msg'=>'请填写正确的优惠券编码']);
  92. // 兼容逗号问题
  93. $customCodes = str_ireplace(',',',',$customCodes);
  94. $customCodes = str_ireplace("\r\n",',',$customCodes);
  95. $customCodes = str_ireplace("\n",',',$customCodes);
  96. // 转数组处理
  97. $customCodes = explode(',',$customCodes);
  98. // 循环处理
  99. foreach ($customCodes as $key=>$value) {
  100. // 转ID
  101. $customUid = $Custom->codeToId($value);
  102. // id有问题
  103. if( !$customUid ) return json_send(['code'=>'error','msg'=>'客户编码'.$value.'不存在']);
  104. // 重组
  105. $customCodes[$key] = $customUid;
  106. }
  107. // 去重
  108. $customCodes = array_unique($customCodes);
  109. // 数量
  110. if( count($customCodes) > 10000 ) return json_send(['code'=>'error','msg'=>'客户编码请勿超过10000']);
  111. // 排除数据
  112. $customCodes = is_array($customCodes) ? $customCodes : [];
  113. // 插入列表
  114. $insertList = [];
  115. // 时间
  116. $time = time();
  117. // 循环客户
  118. foreach ($customCodes as $key => $value) {
  119. // 是否存在已发放的客户
  120. $oldId = $Model->query()->where([['coupon_id','=',$couponId],['custom_uid','=',$value]])->value('id');
  121. // 如果存在ID
  122. if( $oldId ) continue;
  123. // 批量写入列表
  124. $insertList[] = ['coupon_id'=>$couponId,'custom_uid'=>$value,'exp_time'=>$Coupon->getExpTime($couponInfo['exp_time']),'insert_time'=>$time,'update_time'=>$time];
  125. }
  126. // 查询该优惠券已经发放的数量
  127. $countTotal = $Model->query()->where([['coupon_id','=',$couponId]])->count();
  128. // 如果超过数量,不允许发放
  129. if( $couponInfo['issue_total'] < (count($insertList) + $countTotal) ) return json_send(['code'=>'error','msg'=>'发放超过发行数量 => '.((count($insertList) + $countTotal) - $couponInfo['issue_total'])]);
  130. // 如果有客户范围
  131. if( $insertList ) {
  132. // 分块,每1000一批写入
  133. foreach (array_chunk($insertList,1000) as $chunk) {
  134. // 写入数据
  135. $result = $Model->insert($chunk);
  136. // 提示新增失败
  137. if( !$result ) {
  138. // 提示失败
  139. return json_send(['code'=>'error','msg'=>'指定客户写入失败']);
  140. }
  141. }
  142. }
  143. // 告知结果
  144. return json_send(['code'=>'success','msg'=>'新增成功','action'=>'add','path'=>url('admin/custom_coupon/index?'.http_build_query(['coupon_code'=>$couponCode]))]);
  145. }
  146. // 优惠券ID
  147. $couponCode = request('coupon_code','');
  148. $couponId = $Coupon->codeToId($couponCode);
  149. // 提示
  150. if( !$couponId ) return $this->error('请填写正确的优惠券编码');
  151. // 查询优惠券信息
  152. $couponInfo = $Coupon->query()->find($couponId);
  153. // 提示
  154. if( !$couponId ) return $this->error('请填写正确的优惠券编码');
  155. // 分配数据
  156. $this->assign('crumbs','发放优惠券');
  157. $this->assign('couponInfo',$couponInfo);
  158. // 加载模板
  159. return $this->fetch();
  160. }
  161. /**
  162. * 状态
  163. *
  164. * */
  165. public function set_status( Request $request, Model $Model){
  166. // 验证参数
  167. $request->scene('set_status')->validate();
  168. // 接收参数
  169. $id = request('id',0);
  170. $status = request('status',0);
  171. // 查询数据
  172. $result = $Model->edit($id,['status'=>$status]);
  173. // 提示新增失败
  174. if( !$result ) return json_send(['code'=>'error','msg'=>'设置失败']);
  175. // 记录行为
  176. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,[],['status'=>$status]);
  177. // 告知结果
  178. return json_send(['code'=>'success','msg'=>'设置成功','path'=>'']);
  179. }
  180. }