CustomClockinRecord.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php namespace App\Http\Controllers\Admin;
  2. use App\Models\City;
  3. use App\Models\Custom as Custom;
  4. use Illuminate\Support\Carbon;
  5. use App\Models\WeiBan\Follow as WeiBanFollow;
  6. use App\Models\CustomClockinRecord as Model;
  7. use PhpOffice\PhpSpreadsheet\IOFactory;
  8. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  9. use PhpOffice\PhpSpreadsheet\Style\Alignment;
  10. use PhpOffice\PhpSpreadsheet\Style\Fill;
  11. /**
  12. * 客户签到记录
  13. *
  14. * @author jun
  15. *
  16. */
  17. class CustomClockinRecord extends Auth{
  18. protected function _initialize(){
  19. parent::_initialize();
  20. $this->assign('breadcrumb1','积分管理');
  21. $this->assign('breadcrumb2','打卡记录');
  22. }
  23. /**
  24. * 列表页
  25. *
  26. * */
  27. public function index(Model $Model,Custom $Custom,City $City){
  28. // 接受参数
  29. $code = request('custom_code','');
  30. $phone = request('phone','');
  31. $username = request('username','');
  32. $activeName = request('active_name','');
  33. $cityId = request('city_id',0);
  34. $status = request('status');
  35. $startTime = request('start_time','');
  36. $endTime = request('end_time','');
  37. // 编码转ID
  38. $uid = $Custom->codeToId($code);
  39. // 查询条件
  40. $map = [];
  41. // 编码ID
  42. if( $uid ) $map[] = ['custom.uid','=',$uid];
  43. if( $phone ) $map[] = ['custom.phone','=',$phone];
  44. if( $username ) $map[] = ['custom.username','=',$username];
  45. if( $cityId ) $map[] = ['custom.city_id','=',$cityId];
  46. if( $activeName ) $map[] = ['score_clockin_active.name','=',$activeName];
  47. if( $startTime ) $map[] = ['custom_clockin_record.insert_time','>=',Carbon::createFromFormat('Y-m-d',$startTime)->startOfDay()->getTimestamp()];
  48. if( $endTime ) $map[] = ['custom_clockin_record.insert_time','<=',Carbon::createFromFormat('Y-m-d',$endTime)->endOfDay()->getTimestamp()];
  49. if( !is_null($status) ) $map[] = ['custom_clockin_record.status','=',$status];
  50. // 查询数据
  51. $list = $Model->query()
  52. ->leftJoin('custom','custom.uid','=','custom_clockin_record.custom_uid')
  53. ->leftJoin('score_clockin_active','score_clockin_active.id','=','custom_clockin_record.active_id')
  54. ->where($map)
  55. ->select(['custom.*','score_clockin_active.name as active_name','custom_clockin_record.*'])
  56. ->orderByDesc('custom_clockin_record.id')
  57. ->paginate(config('page_num',10))
  58. ->appends(request()->all());
  59. // 循环处理数据
  60. foreach ($list as $key => $value) {
  61. // 城市名
  62. $value['city_name'] = $value['city_id'] ? $City->getOne($value['city_id'],'name') : '';
  63. // id转编号
  64. $value['custom_code'] = $Custom->idToCode($value['uid']);
  65. // 重组
  66. $list[$key] = $value;
  67. }
  68. // 获取列表
  69. $cityList = $City->getCityList();
  70. // 分配数据
  71. $this->assign('cityList',$cityList);
  72. // 分配数据
  73. $this->assign('empty', '<tr><td colspan="20">~~暂无数据</td></tr>');
  74. $this->assign('list',$list);
  75. // 加载模板
  76. return $this->fetch();
  77. }
  78. /**
  79. * 导出表格
  80. *
  81. * */
  82. public function down_excel(Model $Model,Custom $Custom,City $City){
  83. // 接受参数
  84. $code = request('custom_code','');
  85. $phone = request('phone','');
  86. $username = request('username','');
  87. $activeName = request('active_name','');
  88. $cityId = request('city_id',0);
  89. $status = request('status');
  90. $startTime = request('start_time','');
  91. // 编码转ID
  92. $uid = $Custom->codeToId($code);
  93. // 查询条件
  94. $map = [];
  95. // 编码ID
  96. if( $uid ) $map[] = ['custom.uid','=',$uid];
  97. if( $phone ) $map[] = ['custom.phone','=',$phone];
  98. if( $username ) $map[] = ['custom.username','=',$username];
  99. if( $cityId ) $map[] = ['custom.city_id','=',$cityId];
  100. if( $activeName ) $map[] = ['score_clockin_active.name','=',$activeName];
  101. if( $startTime ) $map[] = ['custom_clockin_record.insert_time','>=',Carbon::createFromFormat('Y-m-d',$startTime)->startOfDay()->getTimestamp()];
  102. if( $startTime ) $map[] = ['custom_clockin_record.nsert_time','<=',Carbon::createFromFormat('Y-m-d',$startTime)->endOfDay()->getTimestamp()];
  103. if( !is_null($status) ) $map[] = ['custom_clockin_record.status','=',$status];
  104. // 查询数据
  105. $list = $Model->query()
  106. ->leftJoin('custom','custom.uid','=','custom_clockin_record.custom_uid')
  107. ->leftJoin('score_clockin_active','score_clockin_active.id','=','custom_clockin_record.active_id')
  108. ->where($map)
  109. ->select(['custom.*','score_clockin_active.name as active_name','custom_clockin_record.*'])
  110. ->orderByDesc('custom_clockin_record.id')
  111. ->get()
  112. ->toArray();
  113. // 循环处理数据
  114. foreach ($list as $key => $value) {
  115. // 城市名
  116. $value['city_name'] = $value['city_id'] ? $City->getOne($value['city_id'],'name') : '';
  117. // id转编号
  118. $value['custom_code'] = $Custom->idToCode($value['uid']);
  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. // 创建新的电子表格对象
  134. $spreadsheet = new Spreadsheet();
  135. // 设置合并单元格的行和列,例如合并A1到B2的单元格
  136. $sheet = $this->setStyle($spreadsheet);
  137. // 从第二行写入
  138. $row = 2;
  139. // 循环写入
  140. foreach ($data as $key => $value) {
  141. // 单元格内容写入
  142. $sheet->setCellValue('A'.$row, $value['id']);
  143. $sheet->setCellValue('B'.$row, $value['custom_code']);
  144. $sheet->setCellValue('C'.$row, $value['username']);
  145. $sheet->setCellValue('D'.$row, $value['active_name']);
  146. $sheet->setCellValue('E'.$row, $value['phone']);
  147. $sheet->setCellValue('F'.$row, $value['external_userid']);
  148. $sheet->setCellValue('G'.$row, $value['city_name']);
  149. $sheet->setCellValue('H'.$row, date('Y-m-d H:i:s',$value['clockin_time']));
  150. $sheet->setCellValue('I'.$row, $value['clockin_day']);
  151. $row++;
  152. }
  153. //
  154. // 创建内容
  155. $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
  156. header('Pragma: public');
  157. header('Content-type:application/vnd.ms-excel');
  158. header('Content-Disposition: inline;filename=下载打卡记录.xlsx');
  159. // 输出数据流
  160. return $writer->save('php://output');
  161. }
  162. /**
  163. * 设置表格样式
  164. *
  165. */
  166. private function setStyle(Spreadsheet $spreadsheet){
  167. // 选择当前活动的工作表
  168. $sheet = $spreadsheet->getActiveSheet();
  169. // 宽
  170. $sheet->getColumnDimension('A')->setWidth(15);
  171. $sheet->getColumnDimension('B')->setWidth(15);
  172. $sheet->getColumnDimension('C')->setWidth(15);
  173. $sheet->getColumnDimension('D')->setWidth(15);
  174. $sheet->getColumnDimension('E')->setWidth(15);
  175. $sheet->getColumnDimension('F')->setWidth(15);
  176. $sheet->getColumnDimension('G')->setWidth(15);
  177. $sheet->getColumnDimension('H')->setWidth(15);
  178. $sheet->getColumnDimension('I')->setWidth(15);
  179. $sheet->getColumnDimension('J')->setWidth(15);
  180. // 默认高度
  181. $sheet->getDefaultRowDimension()->setRowHeight(18);
  182. // 加粗第一行
  183. $sheet->getStyle('A:S')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER)->setVertical(Alignment::VERTICAL_CENTER);
  184. $sheet->getStyle('A1:S1')->getFont()->setBold(true);
  185. $sheet->getStyle('A1:S1')->getFill()->setFillType(Fill::FILL_SOLID)->getStartColor()->setARGB('FF00FF00'); // ARGB颜色代码,例如绿色
  186. // 设置表格标题
  187. $sheet
  188. ->setCellValue('A1', '记录ID')
  189. ->setCellValue('B1', '客户编码')
  190. ->setCellValue('C1', '客户昵称')
  191. ->setCellValue('D1', '活动名称')
  192. ->setCellValue('E1', '联系方式')
  193. ->setCellValue('F1', '企微ID')
  194. ->setCellValue('G1', '客户城市')
  195. ->setCellValue('H1', '打卡时间')
  196. ->setCellValue('I1', '连续打卡天数');
  197. // 返回结果
  198. return $sheet;
  199. }
  200. }