CustomClockinRecord.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. /**
  11. * 客户签到记录
  12. *
  13. * @author jun
  14. *
  15. */
  16. class CustomClockinRecord extends Auth{
  17. protected function _initialize(){
  18. parent::_initialize();
  19. $this->assign('breadcrumb1','积分管理');
  20. $this->assign('breadcrumb2','打卡记录');
  21. }
  22. /**
  23. * 列表页
  24. *
  25. * */
  26. public function index(Model $Model,Custom $Custom,CustomScore $CustomScore,WeiBanFollow $WeiBanFollow,City $City){
  27. // 接受参数
  28. $code = request('custom_code','');
  29. $phone = request('phone','');
  30. $username = request('username','');
  31. $weibanId = request('weiban_extid','');
  32. $cityId = request('city_id',0);
  33. $status = request('status');
  34. $startTime = request('start_time','');
  35. // 编码转ID
  36. $uid = $Custom->codeToId($code);
  37. // 查询条件
  38. $map = [];
  39. // 编码ID
  40. if( $uid ) $map[] = ['custom.uid','=',$uid];
  41. if( $phone ) $map[] = ['custom.phone','=',$phone];
  42. if( $username ) $map[] = ['custom.username','=',$username];
  43. if( $cityId ) $map[] = ['custom.city_id','=',$cityId];
  44. if( $weibanId ) $map[] = ['custom.weiban_extid','=',$weibanId];
  45. if( $startTime ) $map[] = ['custom_clockin_record.insert_time','>=',Carbon::createFromFormat('Y-m-d',$startTime)->startOfDay()->getTimestamp()];
  46. if( $startTime ) $map[] = ['custom_clockin_record.nsert_time','<=',Carbon::createFromFormat('Y-m-d',$startTime)->endOfDay()->getTimestamp()];
  47. if( !is_null($status) ) $map[] = ['custom_clockin_record.status','=',$status];
  48. // 查询数据
  49. $list = $Model->query()
  50. ->leftJoin('custom','custom.uid','=','custom_clockin_record.custom_uid')
  51. ->leftJoin('score_clockin_active','score_clockin_active.id','=','custom_clockin_record.active_id')
  52. ->where($map)
  53. ->select(['custom.*','score_clockin_active.name as active_name','custom_clockin_record.*'])
  54. ->orderByDesc('custom_clockin_record.id')
  55. ->paginate(config('page_num',10))
  56. ->appends(request()->all());
  57. // 循环处理数据
  58. foreach ($list as $key => $value) {
  59. // 城市名
  60. $value['city_name'] = $value['city_id'] ? $City->getOne($value['city_id'],'name') : '';
  61. // id转编号
  62. $value['custom_code'] = $Custom->idToCode($value['uid']);
  63. // 如果不存在微伴ID
  64. if( !$value['weiban_extid'] ) {
  65. // 通过手机号查询注册的账号
  66. $value['weiban_extid'] = (string) $WeiBanFollow->query()->where([['phone_number','=',$value['phone']]])->value('weiban_extid');
  67. // 如果存在的话,修正
  68. if( $value['weiban_extid'] ) $Model->edit($value['uid'],['weiban_extid'=>$value['weiban_extid']]);
  69. }
  70. // 重组
  71. $list[$key] = $value;
  72. }
  73. // 获取列表
  74. $cityList = $City->getCityList();
  75. // 分配数据
  76. $this->assign('cityList',$cityList);
  77. // 分配数据
  78. $this->assign('empty', '<tr><td colspan="20">~~暂无数据</td></tr>');
  79. $this->assign('list',$list);
  80. // 加载模板
  81. return $this->fetch();
  82. }
  83. /**
  84. * 添加
  85. *
  86. * */
  87. public function add(Request $request,Model $Model){
  88. if( request()->isMethod('post') ){
  89. // 验证参数
  90. $request->scene('add')->validate();
  91. // 接收数据
  92. // 接收数据
  93. $data['username'] = request('username','');
  94. $data['phone'] = request('phone','');
  95. // 写入数据表
  96. $uid = $Model->add($data);
  97. // 如果操作失败
  98. if( !$uid ) return json_send(['code'=>'error','msg'=>'新增失败']);
  99. // 记录行为
  100. $this->addAdminHistory(admin('uid'),$Model->getTable(),$uid,1,[],$data);
  101. // 告知结果
  102. return json_send(['code'=>'success','msg'=>'新增成功','action'=>'add']);
  103. }
  104. // 分配数据
  105. $this->assign('crumbs','新增');
  106. // 加载模板
  107. return $this->fetch();
  108. }
  109. /**
  110. * 修改
  111. *
  112. * */
  113. public function edit(Request $request,Model $Model,City $City){
  114. // 接收参数
  115. $uid = request('uid',0);
  116. // 查询用户
  117. $oldData = $Model->where(['uid'=>$uid])->first();
  118. // 修改
  119. if(request()->isMethod('post')){
  120. // 验证参数
  121. $request->scene('edit')->validate();
  122. // 如果用户不存在
  123. if( !$oldData ) return json_send(['code'=>'error','msg'=>'用户不存在']);
  124. // 转数组
  125. $oldData = $oldData->toArray();
  126. // 接收数据
  127. $data['username'] = request('username','');
  128. $data['phone'] = request('phone','');
  129. $data['city_id'] = request('city_id',0);
  130. $data['weiban_extid'] = request('weiban_extid','');
  131. // 写入数据表
  132. $result = $Model->edit($uid,$data);
  133. // 如果操作失败
  134. if( !$result ) return json_send(['code'=>'error','msg'=>'新增失败']);
  135. // 记录行为
  136. $this->addAdminHistory(admin('uid'),$Model->getTable(),$uid,2,$oldData,$data);
  137. // 告知结果
  138. return json_send(['code'=>'success','msg'=>'修改成功','action'=>'edit']);
  139. }
  140. // 错误告知
  141. if( !$oldData ) return $this->error('查无数据');
  142. // 获取列表
  143. $cityList = $City->getCityList();
  144. // 分配数据
  145. $this->assign('cityList',$cityList);
  146. $this->assign('oldData',$oldData);
  147. $this->assign('crumbs','修改');
  148. // 加载模板
  149. return $this->fetch();
  150. }
  151. /**
  152. * 修改状态
  153. *
  154. * */
  155. public function set_status(Request $request,Model $Model){
  156. // 验证参数
  157. $request->scene('set_status')->validate();
  158. // 设置状态
  159. $uid = request('uid',0);
  160. $status = request('status',0);
  161. // 查询用户
  162. $oldData = $Model->where(['uid'=>$uid])->first();
  163. // 如果用户不存在
  164. if( !$oldData ) return json_send(['code'=>'error','msg'=>'用户不存在']);
  165. // 执行修改
  166. $result = $Model->edit($uid,['status'=>$status]);
  167. // 提示新增失败
  168. if( !$result ) return json_send(['code'=>'error','msg'=>'设置失败']);
  169. // 记录行为
  170. $this->addAdminHistory(admin('uid'),$Model->getTable(),$uid,2,$oldData,['status'=>$status]);
  171. // 告知结果
  172. return json_send(['code'=>'success','msg'=>'设置成功','path'=>'']);
  173. }
  174. /**
  175. * 表格导入
  176. *
  177. * */
  178. public function import_execl( Request $request,Model $Model,FilesManager $FilesManager,City $City,CustomAddr $CustomAddr,WeiBanExternal $WeiBanExternal){
  179. // 验证参数
  180. $request->scene('import_execl')->validate();
  181. // 获取表格信息
  182. $file = request()->file('custom_file');
  183. // 返回结果
  184. $sheetList = $FilesManager->excelToCustom($file);
  185. // 如果不存在结果
  186. if( isset($sheetList['error']) ) return json_send(['code'=>'error','msg'=>$sheetList['error']]);
  187. // 循环表格数据
  188. foreach ($sheetList as $value) {
  189. // 获取城市ID
  190. $value['city_id'] = 0;
  191. // 存在城市名称,查询城市ID
  192. if( $value['contact_city'] ) {
  193. // 获取城市ID
  194. $value['city_id'] = (int) $City->getIdByName($value['contact_city']);
  195. // 如果城市不存在的话
  196. if ( !$value['city_id'] ) return json_send(['code'=>'error','msg'=>$value['contact_city'].' => 未找到匹配的城市,请核对城市全称']);
  197. }
  198. // 获取手机号,查询是否用客户
  199. $custom = $Model->getOneByPhone($value['phone']);
  200. // 组装数据
  201. $data = ['weiban_extid'=>$value['weiban_extid'],'username'=>$value['username']];
  202. // 如果客户不存在
  203. if( !$custom ) $data['phone'] = $value['phone'];
  204. // 如果存在城市ID,才修改城市
  205. if( $value['city_id'] ) $data['city_id'] = $value['city_id'];
  206. // 如果存在手机号
  207. $uid = $custom ? $Model->edit($custom['uid'],$data) : $Model->add($data);
  208. // 如果客户存在
  209. if( !$uid ) return json_send(['code'=>'error','msg'=>$value['username'].'【'.$value['phone'].'】'.'用户创建或者更新失败']);
  210. // 存在详细地址,才创建地址库
  211. if( $value['contact_name'] && $value['contact_phone'] && $value['contact_province'] && $value['contact_city'] && $value['contact_area'] && $value['contact_addr'] ) {
  212. // 收件地址是否存在
  213. $oldAddr = $CustomAddr->query()->where([['custom_uid','=',$uid]])->first();
  214. // 如果不存在地址
  215. if( !$oldAddr ) $CustomAddr->add(['custom_uid'=>$uid,'contact_name'=>$value['contact_name'],'contact_shop'=>$value['contact_shop'],'contact_phone'=>$value['contact_phone'],'contact_province'=>$value['contact_province'],'contact_city'=>$value['contact_city'],'contact_area'=>$value['contact_area'],'contact_addr'=>$value['contact_addr']]);
  216. }
  217. }
  218. // 提示成功
  219. return json_send(['code'=>'success','msg'=>'批量导入成功','path'=>'']);
  220. }
  221. }