AmountRecord.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php namespace App\Http\Controllers\Admin;
  2. use App\Models\Custom as Custom;
  3. use Illuminate\Support\Carbon;
  4. use App\Models\Amount\Record as Model;
  5. /**
  6. * 余额记录
  7. *
  8. * @author jun
  9. *
  10. */
  11. class AmountRecord extends Auth{
  12. protected function _initialize(){
  13. parent::_initialize();
  14. $this->assign('breadcrumb1','余额管理');
  15. $this->assign('breadcrumb2','余额记录');
  16. }
  17. /**
  18. * 列表页
  19. *
  20. * */
  21. public function index(Model $Model,Custom $Custom){
  22. // 接受参数
  23. $code = request('custom_code','');
  24. $phone = request('phone','');
  25. $username = request('username','');
  26. $status = request('status');
  27. $startTime = request('start_time','');
  28. $endTime = request('end_time','');
  29. // 编码转ID
  30. $uid = $Custom->codeToId($code);
  31. // 查询条件
  32. $map = [];
  33. // 编码ID
  34. if( $uid ) $map[] = ['custom.uid','=',$uid];
  35. if( $phone ) $map[] = ['custom.phone','=',$phone];
  36. if( $username ) $map[] = ['custom.username','=',$username];
  37. if( $startTime ) $map[] = ['amount_record.insert_time','>=',Carbon::createFromFormat('Y-m-d',$startTime)->startOfDay()->getTimestamp()];
  38. if( $endTime ) $map[] = ['amount_record.insert_time','<=',Carbon::createFromFormat('Y-m-d',$endTime)->endOfDay()->getTimestamp()];
  39. if( !is_null($status) ) $map[] = ['amount_record.status','=',$status];
  40. // 查询数据
  41. $list = $Model->query()
  42. ->leftJoin('custom','custom.uid','=','amount_record.custom_uid')
  43. ->where($map)
  44. ->select(['amount_record.*','custom.username'])
  45. ->orderByDesc('amount_record.id')
  46. ->paginate(config('page_num',10))
  47. ->appends(request()->all());
  48. // 循环处理数据
  49. foreach ($list as $key => $value) {
  50. // id转编号
  51. $value['custom_code'] = (string) $Custom->idToCode($value['custom_uid']);
  52. $value['custom_name'] = (string) $Custom->getValue($value['custom_uid'],'username');
  53. $value['buy_type'] = (string) $Model->getBuyType($value['buy_type'],'name');
  54. // 重组
  55. $list[$key] = $value;
  56. }
  57. // 分配数据
  58. $this->assign('empty', '<tr><td colspan="20">~~暂无数据</td></tr>');
  59. $this->assign('list',$list);
  60. // 加载模板
  61. return $this->fetch();
  62. }
  63. /**
  64. * 导出表格
  65. *
  66. * */
  67. public function down_excel(Model $Model,Custom $Custom){
  68. // 接受参数
  69. $code = request('custom_code','');
  70. $phone = request('phone','');
  71. $username = request('username','');
  72. $status = request('status');
  73. $startTime = request('start_time','');
  74. $endTime = request('end_time','');
  75. // 编码转ID
  76. $uid = $Custom->codeToId($code);
  77. // 查询条件
  78. $map = [];
  79. // 编码ID
  80. if( $uid ) $map[] = ['custom.uid','=',$uid];
  81. if( $phone ) $map[] = ['custom.phone','=',$phone];
  82. if( $username ) $map[] = ['custom.username','=',$username];
  83. if( $startTime ) $map[] = ['amount_record.insert_time','>=',Carbon::createFromFormat('Y-m-d',$startTime)->startOfDay()->getTimestamp()];
  84. if( $endTime ) $map[] = ['amount_record.insert_time','<=',Carbon::createFromFormat('Y-m-d',$endTime)->endOfDay()->getTimestamp()];
  85. if( !is_null($status) ) $map[] = ['amount_record.status','=',$status];
  86. // 查询数据
  87. $list = $Model->query()
  88. ->leftJoin('custom','custom.uid','=','amount_record.custom_uid')
  89. ->where($map)
  90. ->select([
  91. 'amount_record.id',
  92. 'amount_record.out_bill_no',
  93. 'amount_record.transfer_bill_no',
  94. 'amount_record.prefix',
  95. 'amount_record.amount',
  96. 'amount_record.balance',
  97. 'amount_record.description',
  98. 'amount_record.buy_type',
  99. 'amount_record.status',
  100. 'amount_record.payment_time',
  101. 'amount_record.insert_time',
  102. 'amount_record.custom_uid',
  103. 'custom.username as custom_name',
  104. ])
  105. ->orderByDesc('amount_record.id')
  106. ->limit(10000)->get()->toArray();
  107. // 循环处理数据
  108. foreach ($list as $key => $value) {
  109. // 加减
  110. $value['amount'] = "'".($value['prefix'] == 1 ? '+' : '-').$value['amount'];
  111. // id转编号
  112. $value['custom_uid'] = (string) $Custom->idToCode($value['custom_uid']);
  113. // id转编号
  114. $value['buy_type'] = (string) $Model->getBuyType($value['buy_type'],'name');
  115. // 时间
  116. $value['payment_time'] = $value['payment_time'] ? date('Y-m-d H:i:s',$value['payment_time']) : '';
  117. // id转编号
  118. $value['insert_time'] = $value['insert_time'] ? date('Y-m-d H:i:s',$value['insert_time']) : '';
  119. // 删除符号字段
  120. unset($value['prefix']);
  121. // 重组
  122. $list[$key] = $value;
  123. }
  124. // 去下载
  125. $this->toDown($list);
  126. }
  127. /**
  128. * 去下载
  129. */
  130. private function toDown($data){
  131. // xlsx文件保存路径
  132. $excel = new \Vtiful\Kernel\Excel(['path' =>public_path().'/uploads/']);
  133. $filePath = $excel->fileName('tutorial01.xlsx', 'sheet1')->header(['记录ID','系统编号','第三方编号','变动金额','剩余金额','交易描述','交易类型','交易状态','支付时间','交易时间','客户编码','客户昵称'])->data($data)->output();
  134. header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
  135. header('Content-Disposition: attachment;filename="客户余额记录.xlsx"');
  136. header('Content-Length: ' . filesize($filePath));
  137. header('Content-Transfer-Encoding: binary');
  138. header('Cache-Control: must-revalidate');
  139. header('Cache-Control: max-age=0');
  140. header('Pragma: public');
  141. ob_clean();
  142. flush();
  143. // 输出文件,成功删除文件路径
  144. if ( copy($filePath, 'php://output') ) @unlink($filePath);
  145. }
  146. }