RegimentRecord.php 11 KB

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