ScoreRecord.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php namespace App\Http\Controllers\Admin;
  2. use App\Models\Score\Record as Model;
  3. use App\Models\Custom;
  4. /**
  5. * 客户管理
  6. *
  7. * @author 刘相欣
  8. *
  9. */
  10. class ScoreRecord extends Auth{
  11. protected function _initialize(){
  12. parent::_initialize();
  13. $this->assign('breadcrumb1','积分管理');
  14. $this->assign('breadcrumb2','变动记录');
  15. }
  16. /**
  17. * 列表页
  18. *
  19. * */
  20. public function index(Model $Model,Custom $Custom){
  21. // 接受参数
  22. $code = request('custom_code','');
  23. $phone = request('phone','');
  24. $username = request('username','');
  25. $startTime = request('start_time','');
  26. $endTime = request('end_time','');
  27. // 时间戳
  28. $startTime = $startTime ? Carbon::createFromFormat('Y-m-d',$startTime)->startOfDay()->getTimestamp() : now()->addDays(-7)->startOfDay()->getTimestamp();
  29. $endTime = $endTime ? Carbon::createFromFormat('Y-m-d',$endTime)->endOfDay()->getTimestamp() : now()->endOfDay()->getTimestamp();
  30. // 编码转ID
  31. $uid = $Custom->codeToId($code);
  32. // 查询条件
  33. $map = [];
  34. // 编码ID
  35. if( $uid ) $map[] = ['custom.uid','=',$uid];
  36. if( $phone ) $map[] = ['custom.phone','=',$phone];
  37. if( $username ) $map[] = ['custom.username','=',$username];
  38. if( $startTime ) $map[] = ['score_record.pay_time','>=',$startTime];
  39. if( $endTime ) $map[] = ['score_record.pay_time','<=',$endTime];
  40. // 查询数据
  41. $list = $Model->query()
  42. ->join('custom','score_record.custom_uid','=','custom.uid')
  43. ->where($map)
  44. ->orderByDesc('score_record.id')
  45. ->select([
  46. 'score_record.id',
  47. 'score_record.score', // 变动积分
  48. 'score_record.balance', // 变动后剩余
  49. 'score_record.buy_type', // 购买类型
  50. 'score_record.pay_type', // 支付类型
  51. 'score_record.status', //
  52. 'custom.uid as custom_code',
  53. 'custom.username',
  54. 'custom.phone',
  55. 'score_record.pay_time',
  56. ])
  57. ->paginate(config('page_num',10))->appends(request()->all());
  58. // 循环处理数据
  59. foreach ($list as $key => $value) {
  60. // id转编号
  61. $value['custom_code'] = $Custom->idToCode($value['custom_code']);
  62. $value['pay_type'] = $Model->getPayType($value['buy_type'],$value['pay_type'],'name');
  63. // 重组
  64. $list[$key] = $value;
  65. }
  66. // 分配数据
  67. $this->assign('empty', '<tr><td colspan="20">~~暂无数据</td></tr>');
  68. $this->assign('list',$list);
  69. // 加载模板
  70. return $this->fetch();
  71. }
  72. /**
  73. * 导出表格
  74. *
  75. * */
  76. public function down_excel(Model $Model,Custom $Custom){
  77. // 接受参数
  78. $code = request('custom_code','');
  79. $phone = request('phone','');
  80. $username = request('username','');
  81. $startTime = request('start_time','');
  82. $endTime = request('end_time','');
  83. // 时间戳
  84. $startTime = $startTime ? Carbon::createFromFormat('Y-m-d',$startTime)->startOfDay()->getTimestamp() : now()->addDays(-7)->startOfDay()->getTimestamp();
  85. $endTime = $endTime ? Carbon::createFromFormat('Y-m-d',$endTime)->endOfDay()->getTimestamp() : now()->endOfDay()->getTimestamp();
  86. // 编码转ID
  87. $uid = $Custom->codeToId($code);
  88. // 查询条件
  89. $map = [];
  90. // 编码ID
  91. if( $uid ) $map[] = ['custom.uid','=',$uid];
  92. if( $phone ) $map[] = ['custom.phone','=',$phone];
  93. if( $username ) $map[] = ['custom.username','=',$username];
  94. if( $startTime ) $map[] = ['score_record.pay_time','>=',$startTime];
  95. if( $endTime ) $map[] = ['score_record.pay_time','<=',$endTime];
  96. // 查询数据
  97. $list = $Model->query()
  98. ->join('custom','score_record.custom_uid','=','custom.uid')
  99. ->where($map)
  100. ->orderByDesc('score_record.id')
  101. ->get([
  102. 'score_record.id',
  103. 'custom.uid as custom_code',
  104. 'custom.username',
  105. 'custom.phone',
  106. 'score_record.buy_type', // 购买类型
  107. 'score_record.pay_type', // 支付类型
  108. 'score_record.score', // 变动积分
  109. 'score_record.balance', // 变动后剩余
  110. 'score_record.pay_time',
  111. ])->toArray();
  112. // 循环处理数据
  113. foreach ($list as $key => $value) {
  114. // id转编号
  115. $value['custom_code'] = $Custom->idToCode($value['custom_code']);
  116. $value['pay_type'] = $Model->getPayType($value['buy_type'],$value['pay_type'],'name');
  117. $value['pay_time'] = date('Y-m-d H:i:s',$value['pay_time']);
  118. unset($value['buy_type']);
  119. // 重组
  120. $list[$key] = $value;
  121. }
  122. try {
  123. // 去下载
  124. $this->toDown($list);
  125. } catch (\Throwable $th) {
  126. echo $th->getMessage();
  127. }
  128. }
  129. /**
  130. * 去下载
  131. */
  132. private function toDown($data){
  133. try {
  134. $config = [
  135. 'path' =>public_path().'/uploads/' // xlsx文件保存路径
  136. ];
  137. $excel = new \Vtiful\Kernel\Excel($config);
  138. $header = [
  139. '记录ID',
  140. '客户编码',
  141. '客户昵称',
  142. '联系方式',
  143. '积分来源',
  144. '变动积分',
  145. '剩余积分',
  146. '支付时间',
  147. ];
  148. $filePath = $excel->fileName(uniqid().'.xlsx', 'sheet1')
  149. ->header($header)
  150. ->data($data)
  151. ->output();
  152. header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
  153. header('Content-Disposition: attachment;filename="积分记录.xlsx"');
  154. header('Content-Length: '.filesize($filePath));
  155. header('Content-Transfer-Encoding: binary');
  156. header('Cache-Control: must-revalidate');
  157. header('Cache-Control: max-age=0');
  158. header('Pragma: public');
  159. ob_clean();
  160. flush();
  161. if (copy($filePath, 'php://output') === false) {
  162. return json_send(['code'=>'error','msg'=>'下载失败']);
  163. }
  164. @unlink($filePath);
  165. return json_send(['code'=>'success','msg'=>'下载成功','path'=>'']);
  166. }catch (\Exception $exception) {
  167. return json_send(['code'=>'error','msg'=>'下载失败']);
  168. }
  169. }
  170. }