RedpacketActiveRecord.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php namespace App\Http\Controllers\Admin;
  2. use App\Http\Requests\Admin\Redpacket\ActiveReward as Request;
  3. use App\Models\Custom;
  4. use App\Models\CustomAmount;
  5. use App\Models\Redpacket\ActiveRecord as Model;
  6. use App\Models\Redpacket\Active as RedpacketActive;
  7. use Illuminate\Support\Facades\DB;
  8. /**
  9. * 抽奖记录
  10. *
  11. * @author 刘相欣
  12. *
  13. */
  14. class RedpacketActiveRecord extends Auth{
  15. protected function _initialize(){
  16. parent::_initialize();
  17. $this->assign('breadcrumb1','红包活动');
  18. $this->assign('breadcrumb2','参与记录');
  19. }
  20. /**
  21. * 列表页
  22. *
  23. * */
  24. public function index(Model $Model,RedpacketActive $RedpacketActive,Custom $Custom){
  25. // 接受参数
  26. $activeId = request('active_id',0);
  27. $customCode = request('custom_code','');
  28. $startTime = request('start_time','');
  29. $endTime = request('end_time','');
  30. $status = request('status');
  31. // 查询条件
  32. $map = [['redpacket_active_record.reward_id','>',0]];
  33. // 编码转ID
  34. $customUid = $customCode ? $Custom->codeToId($customCode) : 0;
  35. if( $customUid ) $map[] = ['redpacket_active_record.custom_uid','=',$customUid];
  36. if( $activeId ) $map[] = ['redpacket_active_record.active_id','=',$activeId];
  37. if( $startTime ) $map[] = ['redpacket_active_record.insert_time','>=',strtotime($startTime)];
  38. if( $endTime ) $map[] = ['redpacket_active_record.insert_time','<=',strtotime($endTime)];
  39. if( !is_null($status) ) $map[] = ['redpacket_active_record.status','=',$status];
  40. // 查询数据
  41. $list = $Model->query()
  42. ->join('redpacket_active','redpacket_active.id','=','redpacket_active_record.active_id')
  43. ->join('custom','custom.uid','=','redpacket_active_record.custom_uid')
  44. ->where($map)
  45. ->select(['redpacket_active_record.*','custom.weiban_extid','custom.username','redpacket_active.name as active_name'])
  46. ->orderByDesc('id')
  47. ->paginate(config('page_num',10))->appends(request()->all());
  48. // 循环处理数据
  49. foreach ($list as $key => $value) {
  50. // 组合数据
  51. $value['state'] = $Model->getRecordState($value['status'],'name');
  52. // id转编号
  53. $value['custom_code'] = $Custom->idToCode($value['custom_uid']);
  54. // 重组
  55. $list[$key] = $value;
  56. }
  57. // 获取列表
  58. $statusList = $Model->getRecordStateList();
  59. // 获取列表
  60. $activeList = $RedpacketActive->query()->get(['id','name'])->toArray();
  61. // 分配数据
  62. $this->assign('empty', '<tr><td colspan="20">~~暂无数据</td></tr>');
  63. $this->assign('list',$list);
  64. $this->assign('statusList',$statusList);
  65. $this->assign('activeList',$activeList);
  66. // 加载模板
  67. return $this->fetch();
  68. }
  69. /**
  70. * 修改状态
  71. *
  72. * */
  73. public function set_status(Request $request,Model $Model,CustomAmount $CustomAmount ){
  74. // 验证参数
  75. $request->scene('set_status')->validate();
  76. // 设置状态
  77. $id = request('id',0);
  78. $status = request('status',0);
  79. // 查询用户
  80. $oldData = $Model->where(['id'=>$id])->first();
  81. // 如果用户不存在
  82. if( !$oldData ) return json_send(['code'=>'error','msg'=>'数据不存在']);
  83. // 开启事务
  84. DB::beginTransaction();
  85. try {
  86. // 如果设置成已完成
  87. if( $status == 8 ){
  88. // 发放红包
  89. $CustomAmount->trade($oldData['custom_uid'],$oldData['active_id'],$oldData['money'],8,1,'红包活动');
  90. // 发放失败,改为未中奖
  91. if( isset($result['error']) ) {
  92. // 事务回滚
  93. DB::rollBack();
  94. // 提示失败
  95. return json_send(['code'=>'error','msg'=>'发放失败,请重试']);
  96. }
  97. }
  98. // 执行修改
  99. $result = $Model->edit($id,['status'=>$status]);
  100. // 提示新增失败
  101. if( !$result ) {
  102. // 事务回滚
  103. DB::rollBack();
  104. // 提示失败
  105. return json_send(['code'=>'error','msg'=>'状态设置失败,请重试']);
  106. }
  107. // 提交事务
  108. DB::commit();
  109. // 记录行为
  110. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,$oldData,['status'=>$status]);
  111. // 告知结果
  112. return json_send(['code'=>'success','msg'=>'发放成功','path'=>'']);
  113. } catch (\Throwable $th) {
  114. // 事务回滚
  115. DB::rollBack();
  116. // 提示失败
  117. return json_send(['code'=>'error','msg'=>'发放失败,请重试']);
  118. }
  119. }
  120. /**
  121. * 导出表格
  122. *
  123. * */
  124. public function down_excel(Model $Model,Custom $Custom){
  125. // 接受参数
  126. $activeId = request('active_id',0);
  127. $customCode = request('custom_code','');
  128. $startTime = request('start_time','');
  129. $endTime = request('end_time','');
  130. $status = request('status');
  131. // 查询条件
  132. $map = [['redpacket_active_record.reward_id','>',0]];
  133. // 编码转ID
  134. $customUid = $customCode ? $Custom->codeToId($customCode) : 0;
  135. if( $customUid ) $map[] = ['redpacket_active_record.custom_uid','=',$customUid];
  136. if( $activeId ) $map[] = ['redpacket_active_record.active_id','=',$activeId];
  137. if( $startTime ) $map[] = ['redpacket_active_record.insert_time','>=',strtotime($startTime)];
  138. if( $endTime ) $map[] = ['redpacket_active_record.insert_time','<=',strtotime($endTime)];
  139. if( !is_null($status) ) $map[] = ['redpacket_active_record.status','=',$status];
  140. // 查询数据
  141. $list = $Model->query()
  142. ->join('redpacket_active','redpacket_active.id','=','redpacket_active_record.active_id')
  143. ->join('custom','custom.uid','=','redpacket_active_record.custom_uid')
  144. ->where($map)
  145. ->select([
  146. 'redpacket_active_record.id',
  147. 'redpacket_active.id as active_id',
  148. 'redpacket_active.name as active_name',
  149. 'custom.uid as custom_uid',
  150. 'custom.username',
  151. 'redpacket_active_record.money',
  152. 'redpacket_active_record.status',
  153. 'redpacket_active_record.insert_time',
  154. 'custom.weiban_extid',
  155. ])
  156. ->orderByDesc('id')->get()->toArray();
  157. // 循环处理数据
  158. foreach ($list as $key => $value) {
  159. // 组合数据
  160. $value['status'] = $Model->getRecordState($value['status'],'name');
  161. $value['custom_uid'] = $Custom->idToCode($value['custom_uid']);
  162. $value['username'] = hide_phone($value['username']);
  163. $value['insert_time'] = date('Y-m-d H:i:s',$value['insert_time']);
  164. // 重组
  165. $list[$key] = $value;
  166. }
  167. // 去下载
  168. $this->toDown($list);
  169. }
  170. /**
  171. * 去下载
  172. */
  173. private function toDown($data){
  174. // xlsx文件保存路径
  175. $excel = new \Vtiful\Kernel\Excel(['path' =>public_path().'/uploads/']);
  176. $filePath = $excel->fileName(uniqid().'.xlsx', 'sheet1')->header(['记录ID','活动ID','活动名称','客户编码','客户昵称','红包金额','发放状态','中奖时间','微伴ID'])->data($data)->output();
  177. header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
  178. header('Content-Disposition: attachment;filename="红包活动中奖记录'.date('Y-m-d His').'.xlsx"');
  179. header('Content-Length: ' . filesize($filePath));
  180. header('Content-Transfer-Encoding: binary');
  181. header('Cache-Control: must-revalidate');
  182. header('Cache-Control: max-age=0');
  183. header('Pragma: public');
  184. ob_clean();
  185. flush();
  186. // 输出文件,成功删除文件路径
  187. if ( copy($filePath, 'php://output') ) @unlink($filePath);
  188. }
  189. }