CustomRedpacket.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <?php namespace App\Http\Controllers\Admin;
  2. use App\Models\Custom;
  3. use App\Models\Custom\Redpacket as Model;
  4. use App\Models\Redpacket\Redpacket;
  5. use App\Http\Requests\Admin\Custom\Redpacket as Request;
  6. use App\Models\AdminUser;
  7. /**
  8. * 红包管理
  9. *
  10. * @author 刘相欣
  11. *
  12. */
  13. class CustomRedpacket 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,Custom $Custom,AdminUser $AdminUser){
  24. // 接受参数
  25. $name = request('name','');
  26. $customCode = request('custom_code','');
  27. $startTime = request('start_time','');
  28. $endTime = request('end_time','');
  29. $status = request('status');
  30. // 编码转ID
  31. $customUid = $customCode ? $Custom->codeToId($customCode) : 0;
  32. // 查询条件
  33. $map = [];
  34. // 编码ID
  35. if( $name ) $map[] = ['redpacket.name','=',$name];
  36. if( $customUid ) $map[] = ['custom_redpacket.custom_uid','=',$customUid];
  37. if( $startTime ) $map[] = ['custom_redpacket.insert_time','>=',strtotime($startTime)];
  38. if( $endTime ) $map[] = ['custom_redpacket.insert_time','<=',strtotime($endTime)];
  39. if( !is_null($status) ) $map[] = ['custom_redpacket.status','=',$status];
  40. // 查询数据
  41. $list = $Model->query()->join('redpacket','custom_redpacket.redpacket_id','=','redpacket.id')->where($map)
  42. ->orderBy('custom_redpacket.status')
  43. ->orderByDesc('custom_redpacket.id')
  44. ->select(['custom_redpacket.*','redpacket.name as redpacket_name','redpacket.start_time','redpacket.end_time'])
  45. ->paginate(request('limit',config('page_num',10)))
  46. ->appends(request()->all());
  47. // 循环处理数据
  48. foreach ($list as $key => $value) {
  49. // id转编号
  50. $value['custom_code'] = $Custom->idToCode($value['custom_uid']);
  51. // id转编号
  52. $value['custom_name'] = $Custom->getValue($value['custom_uid'],'username');
  53. // id转编号
  54. $value['admin_name'] = $AdminUser->getOne($value['admin_uid'],'username');
  55. // 重组
  56. $list[$key] = $value;
  57. }
  58. // 分配数据
  59. $this->assign('empty', '<tr><td colspan="20">~~暂无数据</td></tr>');
  60. $this->assign('list', $list);
  61. // 加载模板
  62. return $this->fetch();
  63. }
  64. /**
  65. * 发放红包
  66. *
  67. * */
  68. public function add(Request $request,Model $Model,Redpacket $Redpacket,Custom $Custom){
  69. // 红包ID
  70. $redpacketId = request('redpacket_id',0);
  71. // 添加
  72. if( request()->isMethod('post') ){
  73. // 验证参数
  74. $request->scene('add')->validate();
  75. // 接收数据
  76. $redpacketId = request('redpacket_id',0);
  77. $customCodes = request('custom_codes','');
  78. // 如果操作失败
  79. if( !$redpacketId ) return json_send(['code'=>'error','msg'=>'请选择正确的活动ID']);
  80. // 查询红包信息
  81. $redpacketInfo = $Redpacket->query()->find($redpacketId);
  82. // 提示
  83. if( !$redpacketInfo ) return json_send(['code'=>'error','msg'=>'未找到对应的活动']);
  84. // 兼容逗号问题
  85. $customCodes = str_ireplace(',',',',$customCodes);
  86. $customCodes = str_ireplace("\r\n",',',$customCodes);
  87. $customCodes = str_ireplace("\n",',',$customCodes);
  88. // 转数组处理
  89. $customCodes = explode(',',$customCodes);
  90. // 循环处理
  91. foreach ($customCodes as $key=>$value) {
  92. // 转ID
  93. $customUid = $Custom->codeToId($value);
  94. // id有问题
  95. if( !$customUid ) return json_send(['code'=>'error','msg'=>'客户编码'.$value.'不存在']);
  96. // 重组
  97. $customCodes[$key] = $customUid;
  98. }
  99. // 去重
  100. $customCodes = array_unique($customCodes);
  101. // 数量
  102. if( count($customCodes) > 1000 ) return json_send(['code'=>'error','msg'=>'客户编码请勿超过1000']);
  103. // 排除数据
  104. $customCodes = is_array($customCodes) ? $customCodes : [];
  105. // 插入列表
  106. $insertList = [];
  107. // 时间
  108. $time = time();
  109. // 循环客户
  110. foreach ($customCodes as $key => $value) {
  111. // 是否存在已发放的客户
  112. $oldId = $Model->query()->where([['redpacket_id','=',$redpacketId],['custom_uid','=',$value]])->value('id');
  113. // 如果存在ID
  114. if( $oldId ) continue;
  115. // 批量写入列表
  116. $insertList[] = ['redpacket_id'=>$redpacketId,'custom_uid'=>$value,'money'=>$redpacketInfo['money'],'admin_uid'=>admin('uid'),'insert_time'=>$time,'update_time'=>$time];
  117. }
  118. // 如果有客户范围
  119. if( $insertList ) {
  120. // 分块,每1000一批写入
  121. foreach (array_chunk($insertList,1000) as $chunk) {
  122. // 写入数据
  123. $result = $Model->insert($chunk);
  124. // 提示新增失败
  125. if( !$result ) {
  126. // 提示失败
  127. return json_send(['code'=>'error','msg'=>'指定客户写入失败']);
  128. }
  129. }
  130. }
  131. // 告知结果
  132. return json_send(['code'=>'success','msg'=>'新增成功','action'=>'add','path'=>url('admin/custom_redpacket/index?'.http_build_query(['redpacket_id'=>$redpacketId]))]);
  133. }
  134. // 提示
  135. if( !$redpacketId ) return $this->error('请选择正确的活动ID');
  136. // 查询红包信息
  137. $redpacketInfo = $Redpacket->query()->find($redpacketId);
  138. // 提示
  139. if( !$redpacketId ) return $this->error('未找到对应的活动');
  140. // 分配数据
  141. $this->assign('crumbs','发放红包');
  142. $this->assign('redpacketInfo',$redpacketInfo);
  143. // 加载模板
  144. return $this->fetch();
  145. }
  146. /**
  147. * 状态
  148. *
  149. * */
  150. public function set_status( Request $request, Model $Model){
  151. // 验证参数
  152. $request->scene('set_status')->validate();
  153. // 接收参数
  154. $id = request('id',0);
  155. $status = request('status',0);
  156. // 查询数据
  157. $result = $Model->edit($id,['status'=>$status]);
  158. // 提示新增失败
  159. if( !$result ) return json_send(['code'=>'error','msg'=>'设置失败']);
  160. // 记录行为
  161. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,[],['status'=>$status]);
  162. // 告知结果
  163. return json_send(['code'=>'success','msg'=>'设置成功','path'=>'']);
  164. }
  165. /**
  166. * 导出表格
  167. *
  168. * */
  169. public function down_excel(Model $Model,Custom $Custom,AdminUser $AdminUser){
  170. // 接受参数
  171. $name = request('name','');
  172. $customCode = request('custom_code','');
  173. $startTime = request('start_time','');
  174. $endTime = request('end_time','');
  175. $status = request('status');
  176. // 编码转ID
  177. $customUid = $customCode ? $Custom->codeToId($customCode) : 0;
  178. // 查询条件
  179. $map = [];
  180. // 编码ID
  181. if( $name ) $map[] = ['redpacket.name','=',$name];
  182. if( $customUid ) $map[] = ['custom_redpacket.custom_uid','=',$customUid];
  183. if( $startTime ) $map[] = ['custom_redpacket.insert_time','>=',strtotime($startTime)];
  184. if( $endTime ) $map[] = ['custom_redpacket.insert_time','<=',strtotime($endTime)];
  185. if( !is_null($status) ) $map[] = ['custom_redpacket.status','=',$status];
  186. // 查询数据
  187. $list = $Model->query()->join('redpacket','custom_redpacket.redpacket_id','=','redpacket.id')->where($map)
  188. ->orderBy('custom_redpacket.status')
  189. ->orderByDesc('custom_redpacket.id')
  190. ->select([
  191. 'custom_redpacket.id',
  192. 'custom_redpacket.redpacket_id',
  193. 'custom_redpacket.money',
  194. 'custom_redpacket.status',
  195. 'custom_redpacket.get_time',
  196. 'redpacket.name as redpacket_name',
  197. 'redpacket.start_time',
  198. 'redpacket.end_time',
  199. 'custom_redpacket.admin_uid',
  200. 'custom_redpacket.custom_uid',
  201. ])
  202. ->limit(10000)->get()->toArray();
  203. // 循环处理数据
  204. foreach ($list as $key => $value) {
  205. // id转编号
  206. $value['custom_code'] = (string)$Custom->idToCode($value['custom_uid']);
  207. // id转编号
  208. $value['custom_uid'] = (string)$Custom->getValue($value['custom_uid'],'username');
  209. // 操作人员
  210. $value['admin_uid'] = (string)$AdminUser->getOne($value['admin_uid'],'username');
  211. // 时间
  212. $value['get_time'] = $value['get_time'] ? date('Y-m-d H:i:s',$value['get_time']) : '';
  213. // id转编号
  214. $value['start_time'] = $value['start_time'] ? date('Y-m-d H:i:s',$value['start_time']) : '';
  215. // id转编号
  216. $value['end_time'] = $value['end_time'] ? date('Y-m-d H:i:s',$value['end_time']) : '';
  217. // 重组
  218. $list[$key] = $value;
  219. }
  220. // 去下载
  221. $this->toDown($list);
  222. }
  223. /**
  224. * 去下载
  225. */
  226. private function toDown($data){
  227. // xlsx文件保存路径
  228. $excel = new \Vtiful\Kernel\Excel(['path' =>public_path().'/uploads/']);
  229. $filePath = $excel->fileName('tutorial01.xlsx', 'sheet1')->header(['记录ID','活动ID','红包金额','领取状态','领取时间','活动名称','开始时间','结束时间','操作人员','客户昵称','客户编码'])->data($data)->output();
  230. header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
  231. header('Content-Disposition: attachment;filename="客户红包记录.xlsx"');
  232. header('Content-Length: ' . filesize($filePath));
  233. header('Content-Transfer-Encoding: binary');
  234. header('Cache-Control: must-revalidate');
  235. header('Cache-Control: max-age=0');
  236. header('Pragma: public');
  237. ob_clean();
  238. flush();
  239. // 输出文件,成功删除文件路径
  240. if ( copy($filePath, 'php://output') ) @unlink($filePath);
  241. }
  242. }