CustomClockinRecord.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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\CustomClockinRecord as Model;
  8. /**
  9. * 客户签到记录
  10. *
  11. * @author jun
  12. *
  13. */
  14. class CustomClockinRecord extends Auth{
  15. protected function _initialize(){
  16. parent::_initialize();
  17. $this->assign('breadcrumb1','积分管理');
  18. $this->assign('breadcrumb2','打卡记录');
  19. }
  20. /**
  21. * 列表页
  22. *
  23. * */
  24. public function index(Model $Model,Custom $Custom,WeiBanFollow $WeiBanFollow,City $City){
  25. // 接受参数
  26. $code = request('custom_code','');
  27. $phone = request('phone','');
  28. $username = request('username','');
  29. $activeName = request('active_name','');
  30. $cityId = request('city_id',0);
  31. $status = request('status');
  32. $startTime = request('start_time','');
  33. $endTime = request('end_time','');
  34. // 编码转ID
  35. $uid = $Custom->codeToId($code);
  36. // 查询条件
  37. $map = [];
  38. // 编码ID
  39. if( $uid ) $map[] = ['custom.uid','=',$uid];
  40. if( $phone ) $map[] = ['custom.phone','=',$phone];
  41. if( $username ) $map[] = ['custom.username','=',$username];
  42. if( $cityId ) $map[] = ['custom.city_id','=',$cityId];
  43. if( $activeName ) $map[] = ['score_clockin_active.name','=',$activeName];
  44. if( $startTime ) $map[] = ['custom_clockin_record.insert_time','>=',Carbon::createFromFormat('Y-m-d',$startTime)->startOfDay()->getTimestamp()];
  45. if( $endTime ) $map[] = ['custom_clockin_record.insert_time','<=',Carbon::createFromFormat('Y-m-d',$endTime)->endOfDay()->getTimestamp()];
  46. if( !is_null($status) ) $map[] = ['custom_clockin_record.status','=',$status];
  47. // 查询数据
  48. $list = $Model->query()
  49. ->leftJoin('custom','custom.uid','=','custom_clockin_record.custom_uid')
  50. ->leftJoin('score_clockin_active','score_clockin_active.id','=','custom_clockin_record.active_id')
  51. ->where($map)
  52. ->select(['custom.*','score_clockin_active.name as active_name','custom_clockin_record.*'])
  53. ->orderByDesc('custom_clockin_record.id')
  54. ->paginate(config('page_num',10))
  55. ->appends(request()->all());
  56. // 循环处理数据
  57. foreach ($list as $key => $value) {
  58. // 城市名
  59. $value['city_name'] = $value['city_id'] ? $City->getOne($value['city_id'],'name') : '';
  60. // id转编号
  61. $value['custom_code'] = $Custom->idToCode($value['uid']);
  62. // 如果不存在微伴ID
  63. if( !$value['weiban_extid'] ) {
  64. // 通过手机号查询注册的账号
  65. $value['weiban_extid'] = (string) $WeiBanFollow->query()->where([['phone_number','=',$value['phone']]])->value('weiban_extid');
  66. // 如果存在的话,修正
  67. if( $value['weiban_extid'] ) $Model->edit($value['uid'],['weiban_extid'=>$value['weiban_extid']]);
  68. }
  69. // 重组
  70. $list[$key] = $value;
  71. }
  72. // 获取列表
  73. $cityList = $City->getCityList();
  74. // 分配数据
  75. $this->assign('cityList',$cityList);
  76. // 分配数据
  77. $this->assign('empty', '<tr><td colspan="20">~~暂无数据</td></tr>');
  78. $this->assign('list',$list);
  79. // 加载模板
  80. return $this->fetch();
  81. }
  82. /**
  83. * 添加
  84. *
  85. * */
  86. public function add(Request $request,Model $Model){
  87. if( request()->isMethod('post') ){
  88. // 验证参数
  89. $request->scene('add')->validate();
  90. // 接收数据
  91. // 接收数据
  92. $data['username'] = request('username','');
  93. $data['phone'] = request('phone','');
  94. // 写入数据表
  95. $uid = $Model->add($data);
  96. // 如果操作失败
  97. if( !$uid ) return json_send(['code'=>'error','msg'=>'新增失败']);
  98. // 记录行为
  99. $this->addAdminHistory(admin('uid'),$Model->getTable(),$uid,1,[],$data);
  100. // 告知结果
  101. return json_send(['code'=>'success','msg'=>'新增成功','action'=>'add']);
  102. }
  103. // 分配数据
  104. $this->assign('crumbs','新增');
  105. // 加载模板
  106. return $this->fetch();
  107. }
  108. /**
  109. * 修改
  110. *
  111. * */
  112. public function edit(Request $request,Model $Model,City $City){
  113. // 接收参数
  114. $uid = request('uid',0);
  115. // 查询用户
  116. $oldData = $Model->where(['uid'=>$uid])->first();
  117. // 修改
  118. if(request()->isMethod('post')){
  119. // 验证参数
  120. $request->scene('edit')->validate();
  121. // 如果用户不存在
  122. if( !$oldData ) return json_send(['code'=>'error','msg'=>'用户不存在']);
  123. // 转数组
  124. $oldData = $oldData->toArray();
  125. // 接收数据
  126. $data['username'] = request('username','');
  127. $data['phone'] = request('phone','');
  128. $data['city_id'] = request('city_id',0);
  129. $data['weiban_extid'] = request('weiban_extid','');
  130. // 写入数据表
  131. $result = $Model->edit($uid,$data);
  132. // 如果操作失败
  133. if( !$result ) return json_send(['code'=>'error','msg'=>'新增失败']);
  134. // 记录行为
  135. $this->addAdminHistory(admin('uid'),$Model->getTable(),$uid,2,$oldData,$data);
  136. // 告知结果
  137. return json_send(['code'=>'success','msg'=>'修改成功','action'=>'edit']);
  138. }
  139. // 错误告知
  140. if( !$oldData ) return $this->error('查无数据');
  141. // 获取列表
  142. $cityList = $City->getCityList();
  143. // 分配数据
  144. $this->assign('cityList',$cityList);
  145. $this->assign('oldData',$oldData);
  146. $this->assign('crumbs','修改');
  147. // 加载模板
  148. return $this->fetch();
  149. }
  150. /**
  151. * 修改状态
  152. *
  153. * */
  154. public function set_status(Request $request,Model $Model){
  155. // 验证参数
  156. $request->scene('set_status')->validate();
  157. // 设置状态
  158. $uid = request('uid',0);
  159. $status = request('status',0);
  160. // 查询用户
  161. $oldData = $Model->where(['uid'=>$uid])->first();
  162. // 如果用户不存在
  163. if( !$oldData ) return json_send(['code'=>'error','msg'=>'用户不存在']);
  164. // 执行修改
  165. $result = $Model->edit($uid,['status'=>$status]);
  166. // 提示新增失败
  167. if( !$result ) return json_send(['code'=>'error','msg'=>'设置失败']);
  168. // 记录行为
  169. $this->addAdminHistory(admin('uid'),$Model->getTable(),$uid,2,$oldData,['status'=>$status]);
  170. // 告知结果
  171. return json_send(['code'=>'success','msg'=>'设置成功','path'=>'']);
  172. }
  173. /**
  174. * 导出表格
  175. *
  176. * */
  177. public function down_excel(Model $Model,Custom $Custom,WeiBanFollow $WeiBanFollow,City $City){
  178. // 接受参数
  179. $code = request('custom_code','');
  180. $phone = request('phone','');
  181. $username = request('username','');
  182. $activeName = request('active_name','');
  183. $cityId = request('city_id',0);
  184. $status = request('status');
  185. $startTime = request('start_time','');
  186. $endTime = request('end_time','');
  187. // 编码转ID
  188. $uid = $Custom->codeToId($code);
  189. // 查询条件
  190. $map = [];
  191. // 编码ID
  192. if( $uid ) $map[] = ['custom.uid','=',$uid];
  193. if( $phone ) $map[] = ['custom.phone','=',$phone];
  194. if( $username ) $map[] = ['custom.username','=',$username];
  195. if( $cityId ) $map[] = ['custom.city_id','=',$cityId];
  196. if( $activeName ) $map[] = ['score_clockin_active.name','=',$activeName];
  197. if( $startTime ) $map[] = ['custom_clockin_record.insert_time','>=',Carbon::createFromFormat('Y-m-d',$startTime)->startOfDay()->getTimestamp()];
  198. if( $endTime ) $map[] = ['custom_clockin_record.insert_time','<=',Carbon::createFromFormat('Y-m-d',$endTime)->endOfDay()->getTimestamp()];
  199. if( !is_null($status) ) $map[] = ['custom_clockin_record.status','=',$status];
  200. // 查询数据
  201. $list = $Model->query()
  202. ->leftJoin('custom','custom.uid','=','custom_clockin_record.custom_uid')
  203. ->leftJoin('score_clockin_active','score_clockin_active.id','=','custom_clockin_record.active_id')
  204. ->where($map)
  205. ->select(['custom.*','score_clockin_active.name as active_name','custom_clockin_record.*'])
  206. ->orderByDesc('custom_clockin_record.id')
  207. ->get()
  208. ->toArray();
  209. // 循环处理数据
  210. foreach ($list as $key => $value) {
  211. // 城市名
  212. $value['city_name'] = $value['city_id'] ? $City->getOne($value['city_id'],'name') : '';
  213. // id转编号
  214. $value['custom_code'] = $Custom->idToCode($value['uid']);
  215. // 如果不存在微伴ID
  216. if( !$value['weiban_extid'] ) {
  217. // 通过手机号查询注册的账号
  218. $value['weiban_extid'] = (string) $WeiBanFollow->query()->where([['phone_number','=',$value['phone']]])->value('weiban_extid');
  219. // 如果存在的话,修正
  220. if( $value['weiban_extid'] ) $Model->edit($value['uid'],['weiban_extid'=>$value['weiban_extid']]);
  221. }
  222. // 重构数据
  223. $value = [
  224. 'id'=>$value['id'],
  225. 'active_id'=>$value['active_id'],
  226. 'active_name'=>$value['active_name'],
  227. 'custom_code'=>$value['custom_code'],
  228. 'username'=>$value['username'],
  229. 'phone'=>(string)$value['phone'],
  230. 'clockin_day'=> $value['clockin_day'],
  231. 'clockin_time'=>date('Y-m-d H:i:s',$value['clockin_time']),
  232. 'city_name'=>$value['city_name'],
  233. 'weiban_extid'=>$value['weiban_extid'],
  234. ];
  235. // 重组
  236. $list[$key] = $value;
  237. }
  238. try {
  239. // 去下载
  240. $this->toDown($list);
  241. } catch (\Throwable $th) {
  242. echo $th->getMessage();
  243. }
  244. }
  245. /**
  246. * 去下载
  247. */
  248. private function toDown($data){
  249. // xlsx文件保存路径
  250. $excel = new \Vtiful\Kernel\Excel(['path' =>public_path().'/uploads/']);
  251. $filePath = $excel->fileName(uniqid().'.xlsx', 'sheet1')->header(['记录ID','活动ID','活动名称','客户编码','客户昵称','联系方式','打卡天数','打卡时间','城市名称','微伴ID'])->data($data)->output();
  252. header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
  253. header('Content-Disposition: attachment;filename="打卡记录'.date('Y-m-d His').'.xlsx"');
  254. header('Content-Length: ' . filesize($filePath));
  255. header('Content-Transfer-Encoding: binary');
  256. header('Cache-Control: must-revalidate');
  257. header('Cache-Control: max-age=0');
  258. header('Pragma: public');
  259. ob_clean();
  260. flush();
  261. // 输出文件,成功删除文件路径
  262. if ( copy($filePath, 'php://output') ) @unlink($filePath);
  263. }
  264. }