CustomClockinRecord.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. <?php namespace App\Http\Controllers\Admin;
  2. use App\Http\Requests\Admin\Custom as Request;
  3. use App\Models\City;
  4. use App\Models\Custom as Custom;
  5. use Illuminate\Support\Carbon;
  6. use App\Models\WeiBan\Follow as WeiBanFollow;
  7. use App\Models\FilesManager;
  8. use App\Models\WeiBan\External as WeiBanExternal;
  9. use App\Models\CustomClockinRecord as Model;
  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 CustomClockinRecord 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,Custom $Custom,CustomScore $CustomScore,WeiBanFollow $WeiBanFollow,City $City){
  31. // 接受参数
  32. $code = request('custom_code','');
  33. $phone = request('phone','');
  34. $username = request('username','');
  35. $activeName = request('active_name','');
  36. $cityId = request('city_id',0);
  37. $status = request('status');
  38. $startTime = request('start_time','');
  39. $endTime = request('end_time','');
  40. // 编码转ID
  41. $uid = $Custom->codeToId($code);
  42. // 查询条件
  43. $map = [];
  44. // 编码ID
  45. if( $uid ) $map[] = ['custom.uid','=',$uid];
  46. if( $phone ) $map[] = ['custom.phone','=',$phone];
  47. if( $username ) $map[] = ['custom.username','=',$username];
  48. if( $cityId ) $map[] = ['custom.city_id','=',$cityId];
  49. if( $activeName ) $map[] = ['score_clockin_active.name','=',$activeName];
  50. if( $startTime ) $map[] = ['custom_clockin_record.insert_time','>=',Carbon::createFromFormat('Y-m-d',$startTime)->startOfDay()->getTimestamp()];
  51. if( $endTime ) $map[] = ['custom_clockin_record.insert_time','<=',Carbon::createFromFormat('Y-m-d',$endTime)->endOfDay()->getTimestamp()];
  52. if( !is_null($status) ) $map[] = ['custom_clockin_record.status','=',$status];
  53. // 查询数据
  54. $list = $Model->query()
  55. ->leftJoin('custom','custom.uid','=','custom_clockin_record.custom_uid')
  56. ->leftJoin('score_clockin_active','score_clockin_active.id','=','custom_clockin_record.active_id')
  57. ->where($map)
  58. ->select(['custom.*','score_clockin_active.name as active_name','custom_clockin_record.*'])
  59. ->orderByDesc('custom_clockin_record.id')
  60. ->paginate(config('page_num',10))
  61. ->appends(request()->all());
  62. // 循环处理数据
  63. foreach ($list as $key => $value) {
  64. // 城市名
  65. $value['city_name'] = $value['city_id'] ? $City->getOne($value['city_id'],'name') : '';
  66. // id转编号
  67. $value['custom_code'] = $Custom->idToCode($value['uid']);
  68. // 如果不存在微伴ID
  69. if( !$value['weiban_extid'] ) {
  70. // 通过手机号查询注册的账号
  71. $value['weiban_extid'] = (string) $WeiBanFollow->query()->where([['phone_number','=',$value['phone']]])->value('weiban_extid');
  72. // 如果存在的话,修正
  73. if( $value['weiban_extid'] ) $Model->edit($value['uid'],['weiban_extid'=>$value['weiban_extid']]);
  74. }
  75. // 重组
  76. $list[$key] = $value;
  77. }
  78. // 获取列表
  79. $cityList = $City->getCityList();
  80. // 分配数据
  81. $this->assign('cityList',$cityList);
  82. // 分配数据
  83. $this->assign('empty', '<tr><td colspan="20">~~暂无数据</td></tr>');
  84. $this->assign('list',$list);
  85. // 加载模板
  86. return $this->fetch();
  87. }
  88. /**
  89. * 添加
  90. *
  91. * */
  92. public function add(Request $request,Model $Model){
  93. if( request()->isMethod('post') ){
  94. // 验证参数
  95. $request->scene('add')->validate();
  96. // 接收数据
  97. // 接收数据
  98. $data['username'] = request('username','');
  99. $data['phone'] = request('phone','');
  100. // 写入数据表
  101. $uid = $Model->add($data);
  102. // 如果操作失败
  103. if( !$uid ) return json_send(['code'=>'error','msg'=>'新增失败']);
  104. // 记录行为
  105. $this->addAdminHistory(admin('uid'),$Model->getTable(),$uid,1,[],$data);
  106. // 告知结果
  107. return json_send(['code'=>'success','msg'=>'新增成功','action'=>'add']);
  108. }
  109. // 分配数据
  110. $this->assign('crumbs','新增');
  111. // 加载模板
  112. return $this->fetch();
  113. }
  114. /**
  115. * 修改
  116. *
  117. * */
  118. public function edit(Request $request,Model $Model,City $City){
  119. // 接收参数
  120. $uid = request('uid',0);
  121. // 查询用户
  122. $oldData = $Model->where(['uid'=>$uid])->first();
  123. // 修改
  124. if(request()->isMethod('post')){
  125. // 验证参数
  126. $request->scene('edit')->validate();
  127. // 如果用户不存在
  128. if( !$oldData ) return json_send(['code'=>'error','msg'=>'用户不存在']);
  129. // 转数组
  130. $oldData = $oldData->toArray();
  131. // 接收数据
  132. $data['username'] = request('username','');
  133. $data['phone'] = request('phone','');
  134. $data['city_id'] = request('city_id',0);
  135. $data['weiban_extid'] = request('weiban_extid','');
  136. // 写入数据表
  137. $result = $Model->edit($uid,$data);
  138. // 如果操作失败
  139. if( !$result ) return json_send(['code'=>'error','msg'=>'新增失败']);
  140. // 记录行为
  141. $this->addAdminHistory(admin('uid'),$Model->getTable(),$uid,2,$oldData,$data);
  142. // 告知结果
  143. return json_send(['code'=>'success','msg'=>'修改成功','action'=>'edit']);
  144. }
  145. // 错误告知
  146. if( !$oldData ) return $this->error('查无数据');
  147. // 获取列表
  148. $cityList = $City->getCityList();
  149. // 分配数据
  150. $this->assign('cityList',$cityList);
  151. $this->assign('oldData',$oldData);
  152. $this->assign('crumbs','修改');
  153. // 加载模板
  154. return $this->fetch();
  155. }
  156. /**
  157. * 修改状态
  158. *
  159. * */
  160. public function set_status(Request $request,Model $Model){
  161. // 验证参数
  162. $request->scene('set_status')->validate();
  163. // 设置状态
  164. $uid = request('uid',0);
  165. $status = request('status',0);
  166. // 查询用户
  167. $oldData = $Model->where(['uid'=>$uid])->first();
  168. // 如果用户不存在
  169. if( !$oldData ) return json_send(['code'=>'error','msg'=>'用户不存在']);
  170. // 执行修改
  171. $result = $Model->edit($uid,['status'=>$status]);
  172. // 提示新增失败
  173. if( !$result ) return json_send(['code'=>'error','msg'=>'设置失败']);
  174. // 记录行为
  175. $this->addAdminHistory(admin('uid'),$Model->getTable(),$uid,2,$oldData,['status'=>$status]);
  176. // 告知结果
  177. return json_send(['code'=>'success','msg'=>'设置成功','path'=>'']);
  178. }
  179. /**
  180. * 导出表格
  181. *
  182. * */
  183. public function down_excel(Model $Model,Custom $Custom,CustomScore $CustomScore,WeiBanFollow $WeiBanFollow,City $City){
  184. // 接受参数
  185. $code = request('custom_code','');
  186. $phone = request('phone','');
  187. $username = request('username','');
  188. $activeName = request('active_name','');
  189. $cityId = request('city_id',0);
  190. $status = request('status');
  191. $startTime = request('start_time','');
  192. // 编码转ID
  193. $uid = $Custom->codeToId($code);
  194. // 查询条件
  195. $map = [];
  196. // 编码ID
  197. if( $uid ) $map[] = ['custom.uid','=',$uid];
  198. if( $phone ) $map[] = ['custom.phone','=',$phone];
  199. if( $username ) $map[] = ['custom.username','=',$username];
  200. if( $cityId ) $map[] = ['custom.city_id','=',$cityId];
  201. if( $activeName ) $map[] = ['score_clockin_active.name','=',$activeName];
  202. if( $startTime ) $map[] = ['custom_clockin_record.insert_time','>=',Carbon::createFromFormat('Y-m-d',$startTime)->startOfDay()->getTimestamp()];
  203. if( $startTime ) $map[] = ['custom_clockin_record.nsert_time','<=',Carbon::createFromFormat('Y-m-d',$startTime)->endOfDay()->getTimestamp()];
  204. if( !is_null($status) ) $map[] = ['custom_clockin_record.status','=',$status];
  205. // 查询数据
  206. $list = $Model->query()
  207. ->leftJoin('custom','custom.uid','=','custom_clockin_record.custom_uid')
  208. ->leftJoin('score_clockin_active','score_clockin_active.id','=','custom_clockin_record.active_id')
  209. ->where($map)
  210. ->select(['custom.*','score_clockin_active.name as active_name','custom_clockin_record.*'])
  211. ->orderByDesc('custom_clockin_record.id')
  212. ->get()
  213. ->toArray();
  214. // 循环处理数据
  215. foreach ($list as $key => $value) {
  216. // 城市名
  217. $value['city_name'] = $value['city_id'] ? $City->getOne($value['city_id'],'name') : '';
  218. // id转编号
  219. $value['custom_code'] = $Custom->idToCode($value['uid']);
  220. // 如果不存在微伴ID
  221. if( !$value['weiban_extid'] ) {
  222. // 通过手机号查询注册的账号
  223. $value['weiban_extid'] = (string) $WeiBanFollow->query()->where([['phone_number','=',$value['phone']]])->value('weiban_extid');
  224. // 如果存在的话,修正
  225. if( $value['weiban_extid'] ) $Model->edit($value['uid'],['weiban_extid'=>$value['weiban_extid']]);
  226. }
  227. // 重组
  228. $list[$key] = $value;
  229. }
  230. try {
  231. // 去下载
  232. $this->toDown($list);
  233. } catch (\Throwable $th) {
  234. echo $th->getMessage();
  235. }
  236. }
  237. /**
  238. * 去下载
  239. */
  240. private function toDown($data){
  241. // 创建新的电子表格对象
  242. $spreadsheet = new Spreadsheet();
  243. // 设置合并单元格的行和列,例如合并A1到B2的单元格
  244. $sheet = $this->setStyle($spreadsheet);
  245. // 从第二行写入
  246. $row = 2;
  247. // 循环写入
  248. foreach ($data as $key => $value) {
  249. // 单元格内容写入
  250. $sheet->setCellValue('A'.$row, $value['id']);
  251. $sheet->setCellValue('B'.$row, $value['custom_code']);
  252. $sheet->setCellValue('C'.$row, $value['username']);
  253. $sheet->setCellValue('D'.$row, $value['active_name']);
  254. $sheet->setCellValue('E'.$row, $value['phone']);
  255. $sheet->setCellValue('F'.$row, $value['weiban_extid']);
  256. $sheet->setCellValue('G'.$row, $value['city_name']);
  257. $sheet->setCellValue('H'.$row, date('Y-m-d H:i:s',$value['clockin_time']));
  258. $sheet->setCellValue('I'.$row, $value['clockin_day']);
  259. $row++;
  260. }
  261. //
  262. // 创建内容
  263. $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
  264. header('Pragma: public');
  265. header('Content-type:application/vnd.ms-excel');
  266. header('Content-Disposition: inline;filename=下载打卡记录.xlsx');
  267. // 输出数据流
  268. return $writer->save('php://output');
  269. }
  270. /**
  271. * 设置表格样式
  272. *
  273. */
  274. private function setStyle(Spreadsheet $spreadsheet){
  275. // 选择当前活动的工作表
  276. $sheet = $spreadsheet->getActiveSheet();
  277. // 宽
  278. $sheet->getColumnDimension('A')->setWidth(15);
  279. $sheet->getColumnDimension('B')->setWidth(15);
  280. $sheet->getColumnDimension('C')->setWidth(15);
  281. $sheet->getColumnDimension('D')->setWidth(15);
  282. $sheet->getColumnDimension('E')->setWidth(15);
  283. $sheet->getColumnDimension('F')->setWidth(15);
  284. $sheet->getColumnDimension('G')->setWidth(15);
  285. $sheet->getColumnDimension('H')->setWidth(15);
  286. $sheet->getColumnDimension('I')->setWidth(15);
  287. $sheet->getColumnDimension('J')->setWidth(15);
  288. // 默认高度
  289. $sheet->getDefaultRowDimension()->setRowHeight(18);
  290. // 加粗第一行
  291. $sheet->getStyle('A:S')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER)->setVertical(Alignment::VERTICAL_CENTER);
  292. $sheet->getStyle('A1:S1')->getFont()->setBold(true);
  293. $sheet->getStyle('A1:S1')->getFill()->setFillType(Fill::FILL_SOLID)->getStartColor()->setARGB('FF00FF00'); // ARGB颜色代码,例如绿色
  294. // 设置表格标题
  295. $sheet
  296. ->setCellValue('A1', '记录ID')
  297. ->setCellValue('B1', '客户编码')
  298. ->setCellValue('C1', '客户昵称')
  299. ->setCellValue('D1', '活动名称')
  300. ->setCellValue('E1', '联系方式')
  301. ->setCellValue('F1', '微伴ID')
  302. ->setCellValue('G1', '客户城市')
  303. ->setCellValue('H1', '打卡时间')
  304. ->setCellValue('I1', '连续打卡天数');
  305. // 返回结果
  306. return $sheet;
  307. }
  308. }