123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 |
- <?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){
- // 接受参数
- $name = request('name','');
- $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( $name ) $map[] = ['redpacket.name','=',$name];
- 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'=>'']);
- }
-
- /**
- * 导出表格
- *
- * */
- public function down_excel(Model $Model,Custom $Custom,AdminUser $AdminUser){
- // 接受参数
- $name = request('name','');
- $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( $name ) $map[] = ['redpacket.name','=',$name];
- 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.id',
- 'custom_redpacket.redpacket_id',
- 'custom_redpacket.money',
- 'custom_redpacket.status',
- 'custom_redpacket.get_time',
- 'redpacket.name as redpacket_name',
- 'redpacket.start_time',
- 'redpacket.end_time',
- 'custom_redpacket.admin_uid',
- 'custom_redpacket.custom_uid',
- ])
- ->limit(10000)->get()->toArray();
- // 循环处理数据
- foreach ($list as $key => $value) {
- // id转编号
- $value['custom_code'] = (string)$Custom->idToCode($value['custom_uid']);
- // id转编号
- $value['custom_uid'] = (string)$Custom->getValue($value['custom_uid'],'username');
- // 操作人员
- $value['admin_uid'] = (string)$AdminUser->getOne($value['admin_uid'],'username');
- // 时间
- $value['get_time'] = $value['get_time'] ? date('Y-m-d H:i:s',$value['get_time']) : '';
- // id转编号
- $value['start_time'] = $value['start_time'] ? date('Y-m-d H:i:s',$value['start_time']) : '';
- // id转编号
- $value['end_time'] = $value['end_time'] ? date('Y-m-d H:i:s',$value['end_time']) : '';
- // 重组
- $list[$key] = $value;
- }
- // 去下载
- $this->toDown($list);
- }
- /**
- * 去下载
- */
- private function toDown($data){
- // xlsx文件保存路径
- $excel = new \Vtiful\Kernel\Excel(['path' =>public_path().'/uploads/']);
- $filePath = $excel->fileName('tutorial01.xlsx', 'sheet1')->header(['记录ID','活动ID','红包金额','领取状态','领取时间','活动名称','开始时间','结束时间','操作人员','客户昵称','客户编码'])->data($data)->output();
- header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
- header('Content-Disposition: attachment;filename="客户红包记录.xlsx"');
- header('Content-Length: ' . filesize($filePath));
- header('Content-Transfer-Encoding: binary');
- header('Cache-Control: must-revalidate');
- header('Cache-Control: max-age=0');
- header('Pragma: public');
- ob_clean();
- flush();
- // 输出文件,成功删除文件路径
- if ( copy($filePath, 'php://output') ) @unlink($filePath);
- }
- }
|