RegimentRecord.php 11 KB

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