123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- <?php namespace App\Http\Controllers\Admin;
- use App\Models\Business;
- use App\Models\Custom;
- use App\Models\CustomCoupon as Model;
- use App\Models\Coupon as Coupon;
- use App\Http\Requests\Admin\CustomCoupon as Request;
- use App\Models\AdminUser;
- /**
- * 优惠券管理
- *
- * @author 刘相欣
- *
- */
- class CustomCoupon extends Auth{
-
- protected function _initialize(){
- parent::_initialize();
- $this->assign('breadcrumb1','营销管理');
- $this->assign('breadcrumb2','客户优惠券');
- }
- /**
- * 首页列表
- *
- * */
- public function index(Model $Model,Coupon $Coupon,Custom $Custom,AdminUser $AdminUser){
- // 接受参数
- $code = request('coupon_code','');
- $customCode = request('custom_code','');
- $startTime = request('start_time','');
- $endTime = request('end_time','');
- $status = request('status');
- // 编码转ID
- $couponId = $code ? $Coupon->codeToId($code) : 0;
- $customUid = $customCode ? $Custom->codeToId($customCode) : 0;
- // 查询条件
- $map = [];
- // 编码ID
- if( $couponId ) $map[] = ['coupon.id','=',$couponId];
- if( $customUid ) $map[] = ['custom_coupon.custom_uid','=',$customUid];
- if( $startTime ) $map[] = ['custom_coupon.insert_time','>=',strtotime($startTime)];
- if( $endTime ) $map[] = ['custom_coupon.insert_time','<=',strtotime($endTime)];
- if( !is_null($status) ) $map[] = ['custom_coupon.status','=',$status];
- // 查询数据
- $list = $Model
- ->query()->join('coupon','custom_coupon.coupon_id','=','coupon.id');
- $list = $list->where($map)
- ->orderBy('custom_coupon.status')
- ->orderByDesc('custom_coupon.id')
- ->select(['custom_coupon.*','coupon.name as coupon_name','coupon.type_id','coupon.rebate_type','coupon.std_pay','coupon.rebate','custom_coupon.exp_time'])
- ->paginate(request('limit',config('page_num',10)))
- ->appends(request()->all());
- // 循环处理数据
- foreach ($list as $key => $value) {
- // id转编号
- $value['coupon_code']= $Coupon->idToCode($value['coupon_id']);
- // id转编号
- $value['custom_code'] = $Custom->idToCode($value['custom_uid']);
- // id转编号
- $value['custom_name'] = $Custom->getValue($value['custom_uid'],'username');
- // id转编号
- $value['admin_name'] = $AdminUser->getOne($value['admin_uid'],'username');
- // 如果过期时间
- if( $value['status'] == 0 && $value['exp_time'] <= time() ) {
- // 状态设置
- $Model->edit($value['id'],['status'=>3]);
- // 状态设置
- $value['status'] = 3;
- }
- // 重组
- $list[$key] = $value;
- }
- // 分配数据
- $this->assign('empty', '<tr><td colspan="20">~~暂无数据</td></tr>');
- $this->assign('list', $list);
- // 加载模板
- return $this->fetch();
- }
- /**
- * 发放优惠券
- *
- * */
- public function add(Request $request,Model $Model,Coupon $Coupon,Custom $Custom){
- if( request()->isMethod('post') ){
- // 验证参数
- $request->scene('add')->validate();
- // 接收数据
- $couponCode = request('coupon_code','');
- $customCodes = request('custom_codes','');
- $couponId = $Coupon->codeToId($couponCode);
- // 如果操作失败
- if( !$couponId ) return json_send(['code'=>'error','msg'=>'请填写正确的优惠券编码']);
- // 查询优惠券信息
- $couponInfo = $Coupon->query()->find($couponId);
- // 提示
- if( !$couponId ) return json_send(['code'=>'error','msg'=>'请填写正确的优惠券编码']);
- // 兼容逗号问题
- $customCodes = str_ireplace(',',',',$customCodes);
- $customCodes = str_ireplace("\r\n",',',$customCodes);
- $customCodes = str_ireplace("\n",',',$customCodes);
- // 转数组处理
- $customCodes = explode(',',$customCodes);
- // 循环处理
- foreach ($customCodes as $key=>$value) {
- // 转ID
- $customUid = $Custom->codeToId($value);
- // id有问题
- if( !$customUid ) return json_send(['code'=>'error','msg'=>'客户编码'.$value.'不存在']);
- // 重组
- $customCodes[$key] = $customUid;
- }
- // 去重
- $customCodes = array_unique($customCodes);
- // 数量
- if( count($customCodes) > 10000 ) return json_send(['code'=>'error','msg'=>'客户编码请勿超过10000']);
- // 排除数据
- $customCodes = is_array($customCodes) ? $customCodes : [];
- // 插入列表
- $insertList = [];
- // 时间
- $time = time();
- // 循环客户
- foreach ($customCodes as $key => $value) {
- // 是否存在已发放的客户
- $oldId = $Model->query()->where([['coupon_id','=',$couponId],['custom_uid','=',$value]])->value('id');
- // 如果存在ID
- if( $oldId ) continue;
- // 批量写入列表
- $insertList[] = ['coupon_id'=>$couponId,'custom_uid'=>$value,'exp_time'=>$Coupon->getExpTime($couponInfo['exp_time']),'admin_uid'=>admin('uid'),'insert_time'=>$time,'update_time'=>$time];
- }
- // 查询该优惠券已经发放的数量
- $countTotal = $Model->query()->where([['coupon_id','=',$couponId]])->count();
- // 如果超过数量,不允许发放
- if( $couponInfo['issue_total'] < (count($insertList) + $countTotal) ) return json_send(['code'=>'error','msg'=>'发放超过发行数量 => '.((count($insertList) + $countTotal) - $couponInfo['issue_total'])]);
- // 如果有客户范围
- if( $insertList ) {
- // 分块,每1000一批写入
- foreach (array_chunk($insertList,1000) as $chunk) {
- // 写入数据
- $result = $Model->insert($chunk);
- // 提示新增失败
- if( !$result ) {
- // 提示失败
- return json_send(['code'=>'error','msg'=>'指定客户写入失败']);
- }
- }
- }
- // 告知结果
- return json_send(['code'=>'success','msg'=>'新增成功','action'=>'add','path'=>url('admin/custom_coupon/index?'.http_build_query(['coupon_code'=>$couponCode]))]);
- }
- // 优惠券ID
- $couponCode = request('coupon_code','');
- $couponId = $Coupon->codeToId($couponCode);
- // 提示
- if( !$couponId ) return $this->error('请填写正确的优惠券编码');
- // 查询优惠券信息
- $couponInfo = $Coupon->query()->find($couponId);
- // 提示
- if( !$couponId ) return $this->error('请填写正确的优惠券编码');
- // 分配数据
- $this->assign('crumbs','发放优惠券');
- $this->assign('couponInfo',$couponInfo);
- // 加载模板
- return $this->fetch();
- }
- /**
- * 状态
- *
- * */
- public function set_status( Request $request, Model $Model){
- // 验证参数
- $request->scene('set_status')->validate();
- // 接收参数
- $id = request('id',0);
- $status = request('status',0);
- // 查询数据
- $result = $Model->edit($id,['status'=>$status]);
- // 提示新增失败
- if( !$result ) return json_send(['code'=>'error','msg'=>'设置失败']);
- // 记录行为
- $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,[],['status'=>$status]);
- // 告知结果
- return json_send(['code'=>'success','msg'=>'设置成功','path'=>'']);
- }
-
- }
|