CustomClockinRecord.php 12 KB

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