123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- <?php namespace App\Http\Controllers\Admin;
- use App\Models\Custom;
- use App\Models\Custom\Redpacket as Model;
- use App\Models\Redpacket\Redpacket;
- use App\Http\Requests\Admin\Custom\Redpacket as Request;
- use App\Models\AdminUser;
- /**
- * 红包管理
- *
- * @author 刘相欣
- *
- */
- class CustomRedpacket extends Auth{
-
- protected function _initialize(){
- parent::_initialize();
- $this->assign('breadcrumb1','营销管理');
- $this->assign('breadcrumb2','客户红包');
- }
- /**
- * 首页列表
- *
- * */
- public function index(Model $Model,Custom $Custom,AdminUser $AdminUser){
- // 接受参数
- $redpacketId = request('redpacket_id','');
- $customCode = request('custom_code','');
- $startTime = request('start_time','');
- $endTime = request('end_time','');
- $status = request('status');
- // 编码转ID
- $customUid = $customCode ? $Custom->codeToId($customCode) : 0;
- // 查询条件
- $map = [];
- // 编码ID
- if( $redpacketId ) $map[] = ['redpacket.id','=',$redpacketId];
- if( $customUid ) $map[] = ['custom_redpacket.custom_uid','=',$customUid];
- if( $startTime ) $map[] = ['custom_redpacket.insert_time','>=',strtotime($startTime)];
- if( $endTime ) $map[] = ['custom_redpacket.insert_time','<=',strtotime($endTime)];
- if( !is_null($status) ) $map[] = ['custom_redpacket.status','=',$status];
- // 查询数据
- $list = $Model->query()->join('redpacket','custom_redpacket.redpacket_id','=','redpacket.id')->where($map)
- ->orderBy('custom_redpacket.status')
- ->orderByDesc('custom_redpacket.id')
- ->select(['custom_redpacket.*','redpacket.name as redpacket_name','redpacket.start_time','redpacket.end_time'])
- ->paginate(request('limit',config('page_num',10)))
- ->appends(request()->all());
- // 循环处理数据
- foreach ($list as $key => $value) {
- // 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');
- // 重组
- $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,Redpacket $Redpacket,Custom $Custom){
- // 红包ID
- $redpacketId = request('redpacket_id',0);
- // 添加
- if( request()->isMethod('post') ){
- // 验证参数
- $request->scene('add')->validate();
- // 接收数据
- $redpacketId = request('redpacket_id',0);
- $customCodes = request('custom_codes','');
- // 如果操作失败
- if( !$redpacketId ) return json_send(['code'=>'error','msg'=>'请选择正确的活动ID']);
- // 查询红包信息
- $redpacketInfo = $Redpacket->query()->find($redpacketId);
- // 提示
- if( !$redpacketInfo ) 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) > 1000 ) return json_send(['code'=>'error','msg'=>'客户编码请勿超过1000']);
- // 排除数据
- $customCodes = is_array($customCodes) ? $customCodes : [];
- // 插入列表
- $insertList = [];
- // 时间
- $time = time();
- // 循环客户
- foreach ($customCodes as $key => $value) {
- // 是否存在已发放的客户
- $oldId = $Model->query()->where([['redpacket_id','=',$redpacketId],['custom_uid','=',$value]])->value('id');
- // 如果存在ID
- if( $oldId ) continue;
- // 批量写入列表
- $insertList[] = ['redpacket_id'=>$redpacketId,'custom_uid'=>$value,'money'=>$redpacketInfo['money'],'admin_uid'=>admin('uid'),'insert_time'=>$time,'update_time'=>$time];
- }
- // 如果有客户范围
- 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_redpacket/index?'.http_build_query(['redpacket_id'=>$redpacketId]))]);
- }
- // 提示
- if( !$redpacketId ) return $this->error('请选择正确的活动ID');
- // 查询红包信息
- $redpacketInfo = $Redpacket->query()->find($redpacketId);
- // 提示
- if( !$redpacketId ) return $this->error('未找到对应的活动');
- // 分配数据
- $this->assign('crumbs','发放红包');
- $this->assign('redpacketInfo',$redpacketInfo);
- // 加载模板
- 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'=>'']);
- }
-
-
- }
|