RegimentRecord.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <?php namespace App\Http\Controllers\Admin;
  2. use App\Models\City;
  3. use App\Models\Custom as Custom;
  4. use App\Models\Product as Product;
  5. use App\Models\Orders as Orders;
  6. use App\Models\RegimentActive as RegimentActive;
  7. use App\Models\RegimentRecord as Model;
  8. use App\Models\WeiBan\Follow as WeiBanFollow;
  9. use Illuminate\Support\Carbon;
  10. use PhpOffice\PhpSpreadsheet\IOFactory;
  11. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  12. use PhpOffice\PhpSpreadsheet\Style\Alignment;
  13. use PhpOffice\PhpSpreadsheet\Style\Fill;
  14. /**
  15. * 客户签到记录
  16. *
  17. * @author jun
  18. *
  19. */
  20. class RegimentRecord extends Auth{
  21. protected function _initialize(){
  22. parent::_initialize();
  23. $this->assign('breadcrumb1','拼团活动');
  24. $this->assign('breadcrumb2','拼团记录');
  25. }
  26. /**
  27. * 列表页
  28. *
  29. * */
  30. public function index(Model $Model, Product $Product, RegimentActive $RegimentActive, Custom $Custom,Orders $Orders){
  31. // 接收参数
  32. $code = request('code','');
  33. $name = request('name','');
  34. $productName = request('product_name','');
  35. $status = request('status','');
  36. $start_time = request('start_time','');
  37. $end_time = request('end_time','');
  38. // 查询条件
  39. $map = [];
  40. // 组合条件
  41. if( $name ) $map[] = ['regiment_active.name','=',$name];
  42. if( $code ){
  43. $id = $RegimentActive->codeToId($code);
  44. $map[] = ['regiment_active.id','=',$id];
  45. }
  46. if( $productName ) $map[] = ['product.name','=',$productName];
  47. if( $status ) $map[] = ['regiment_record.status','=',$status];
  48. if( $start_time ) {
  49. $start_time = strtotime($start_time);
  50. $map[] = ['regiment_record.insert_time','>=',$start_time];
  51. }
  52. if( $end_time ) {
  53. $end_time = strtotime($end_time);
  54. $map[] = ['regiment_record.insert_time','<=',$end_time];
  55. }
  56. $select = [
  57. 'regiment_record.*',
  58. 'regiment.status as regiment_status',
  59. 'regiment.custom_uid as regiment_uid',
  60. 'regiment_active.name as active_name',
  61. 'product.name as product_name',
  62. 'custom.username as username',
  63. ];
  64. // 查询数据
  65. $list = $Model->query()
  66. ->join('regiment','regiment.id','=','regiment_record.regiment_id')
  67. ->join('regiment_active','regiment_active.id','=','regiment_record.active_id')
  68. ->join('product','product.id','=','regiment_record.product_id')
  69. ->join('custom','custom.uid','=','regiment_record.custom_uid')
  70. ->where($map)
  71. ->select($select)
  72. ->orderByDesc('id')
  73. ->paginate(config('page_num',10));
  74. // 循环处理数据
  75. foreach($list as &$v){
  76. $v['product_code'] = $Product->idToCode($v['product_id']);
  77. $v['active_code'] = $RegimentActive->idToCode($v['active_id']);
  78. $v['custom_code'] = $Custom->idToCode($v['custom_uid']);
  79. $v['order_code'] = $Orders->idToCode($v['order_id']);
  80. $v['regiment_uid_code'] = $Custom->idToCode($v['regiment_uid']);
  81. $userInfo = $Custom->getOne($v['regiment_uid']);
  82. $v['regiment_username'] = $userInfo['username'];
  83. }
  84. // 分配数据
  85. $this->assign('empty', '<tr><td colspan="20">~~暂无数据</td></tr>');
  86. $this->assign('list',$list);
  87. // 加载模板
  88. return $this->fetch();
  89. }
  90. /**
  91. * 导出表格
  92. *
  93. * */
  94. public function down_excel(Model $Model, Product $Product, RegimentActive $RegimentActive, Custom $Custom,Orders $Orders){
  95. // 接受参数
  96. // 接收参数
  97. $code = request('code','');
  98. $name = request('name','');
  99. $productName = request('product_name','');
  100. $status = request('status','');
  101. $start_time = request('start_time','');
  102. $end_time = request('end_time','');
  103. // 查询条件
  104. $map = [];
  105. // 组合条件
  106. if( $name ) $map[] = ['regiment_active.name','=',$name];
  107. if( $code ){
  108. $id = $RegimentActive->codeToId($code);
  109. $map[] = ['regiment_active.id','=',$id];
  110. }
  111. if( $productName ) $map[] = ['product.name','=',$productName];
  112. if( $status ) $map[] = ['regiment_record.status','=',$status];
  113. if( $start_time ) {
  114. $start_time = strtotime($start_time);
  115. $map[] = ['regiment_record.insert_time','>=',$start_time];
  116. }
  117. if( $end_time ) {
  118. $end_time = strtotime($end_time);
  119. $map[] = ['regiment_record.insert_time','<=',$end_time];
  120. }
  121. $select = [
  122. 'regiment_record.*',
  123. 'regiment.status as regiment_status',
  124. 'regiment.custom_uid as regiment_uid',
  125. 'regiment_active.name as active_name',
  126. 'product.name as product_name',
  127. 'custom.username as username',
  128. ];
  129. // 查询数据
  130. $list = $Model->query()
  131. ->join('regiment','regiment.id','=','regiment_record.regiment_id')
  132. ->join('regiment_active','regiment_active.id','=','regiment_record.active_id')
  133. ->join('product','product.id','=','regiment_record.product_id')
  134. ->join('custom','custom.uid','=','regiment_record.custom_uid')
  135. ->where($map)
  136. ->select($select)
  137. ->orderByDesc('id')
  138. ->paginate(config('page_num',10));
  139. // 循环处理数据
  140. foreach($list as &$v){
  141. $v['product_code'] = $Product->idToCode($v['product_id']);
  142. $v['active_code'] = $RegimentActive->idToCode($v['active_id']);
  143. $v['custom_code'] = $Custom->idToCode($v['custom_uid']);
  144. $v['order_code'] = $Orders->idToCode($v['order_id']);
  145. $userInfo = $Custom->getOne($v['regiment_uid']);
  146. $v['regiment_username'] = $userInfo['username'];
  147. }
  148. try {
  149. // 去下载
  150. $this->toDown($list);
  151. } catch (\Throwable $th) {
  152. echo $th->getMessage();
  153. }
  154. }
  155. /**
  156. * 去下载
  157. */
  158. private function toDown($data){
  159. // 创建新的电子表格对象
  160. $spreadsheet = new Spreadsheet();
  161. // 设置合并单元格的行和列,例如合并A1到B2的单元格
  162. $sheet = $this->setStyle($spreadsheet);
  163. // 从第二行写入
  164. $row = 2;
  165. // 循环写入
  166. foreach ($data as $key => $value) {
  167. $statusName = '';
  168. // 单元格内容写入
  169. $sheet->setCellValue('A'.$row, $value['id']);
  170. $sheet->setCellValue('B'.$row, $value['regiment_id']);
  171. $sheet->setCellValue('C'.$row, $value['order_code']);
  172. $sheet->setCellValue('D'.$row, $value['username']);
  173. $sheet->setCellValue('E'.$row, $value['active_name']);
  174. $sheet->setCellValue('F'.$row, $value['product_name']);
  175. $sheet->setCellValue('G'.$row, $value['regiment_username']);
  176. switch ($value['status']) {
  177. case '0':
  178. $statusName = '拼团中';
  179. break;
  180. case '1':
  181. $statusName = '取消拼团';
  182. break;
  183. case '2':
  184. $statusName = '拼团成功';
  185. break;
  186. case '3':
  187. $statusName = '拼团失败';
  188. break;
  189. }
  190. $sheet->setCellValue('H'.$row, $statusName);
  191. $sheet->setCellValue('I'.$row, date('Y-m-d H:i:s',$value['insert_time']));
  192. $row++;
  193. }
  194. //
  195. // 创建内容
  196. $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
  197. header('Pragma: public');
  198. header('Content-type:application/vnd.ms-excel');
  199. header('Content-Disposition: inline;filename=下载拼团记录.xlsx');
  200. // 输出数据流
  201. return $writer->save('php://output');
  202. }
  203. /**
  204. * 设置表格样式
  205. *
  206. */
  207. private function setStyle(Spreadsheet $spreadsheet){
  208. // 选择当前活动的工作表
  209. $sheet = $spreadsheet->getActiveSheet();
  210. // 宽
  211. $sheet->getColumnDimension('A')->setWidth(15);
  212. $sheet->getColumnDimension('B')->setWidth(15);
  213. $sheet->getColumnDimension('C')->setWidth(15);
  214. $sheet->getColumnDimension('D')->setWidth(15);
  215. $sheet->getColumnDimension('E')->setWidth(15);
  216. $sheet->getColumnDimension('F')->setWidth(15);
  217. $sheet->getColumnDimension('G')->setWidth(15);
  218. $sheet->getColumnDimension('H')->setWidth(15);
  219. $sheet->getColumnDimension('I')->setWidth(15);
  220. // 默认高度
  221. $sheet->getDefaultRowDimension()->setRowHeight(18);
  222. // 加粗第一行
  223. $sheet->getStyle('A:S')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER)->setVertical(Alignment::VERTICAL_CENTER);
  224. $sheet->getStyle('A1:S1')->getFont()->setBold(true);
  225. $sheet->getStyle('A1:S1')->getFill()->setFillType(Fill::FILL_SOLID)->getStartColor()->setARGB('FF00FF00'); // ARGB颜色代码,例如绿色
  226. // 设置表格标题
  227. $sheet
  228. ->setCellValue('A1', '拼团记录ID')
  229. ->setCellValue('B1', '团单ID')
  230. ->setCellValue('C1', '订单编码')
  231. ->setCellValue('D1', '客户昵称')
  232. ->setCellValue('E1', '参与活动')
  233. ->setCellValue('F1', '产品名称')
  234. ->setCellValue('G1', '发起客户昵称')
  235. ->setCellValue('H1', '拼团状态')
  236. ->setCellValue('I1', '订单生成时间');
  237. // 返回结果
  238. return $sheet;
  239. }
  240. }