AttendanceGroupUser.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. <?php
  2. namespace app\common\model;
  3. use app\hander\HelpHander;
  4. use think\Db;
  5. use think\Exception;
  6. use think\Model;
  7. class AttendanceGroupUser extends Model
  8. {
  9. public function add(){
  10. $data = [
  11. 'user_id' => input('uId/d',0),
  12. 'group_id' => input('groupId/d',0),
  13. 'org_id' => input('orgId/d',0),
  14. ];
  15. if($data['group_id'] <= 0){
  16. HelpHander::error('未选择考勤组');
  17. }
  18. $info = Db::name('attendance_group_user')
  19. ->where('user_id',$data['user_id'])
  20. ->where('org_id',$data['org_id'])
  21. ->find();
  22. if($info){
  23. if($info['group_id'] != $data['group_id']){
  24. $ret = Db::name('attendance_group_user')->where('id',$info['id'])->setField('group_id',$data['group_id']);
  25. if(!$ret){
  26. HelpHander::error('操作失败');
  27. }
  28. }
  29. }else{
  30. $ret = Db::name('attendance_group_user')->insertGetId($data);
  31. if(!$ret){
  32. HelpHander::error('操作失败');
  33. }
  34. }
  35. return true;
  36. }
  37. public function lists($page,$size,$name,$phone,$code,$jobId,$depId,$orgId){
  38. $map[] = ['u.del','=',0];
  39. $map[] = ['u.enable','=',1];
  40. $map[] = ['j.org_id','=',$orgId];
  41. if($name){
  42. $map[] = ['ui.name','like','%'.$name.'%'];
  43. }
  44. if($phone){
  45. $map[] = ['u.phone','like','%'.$phone.'%'];
  46. }
  47. if($code){
  48. $map[] = ['u.code','like','%'.$code.'%'];
  49. }
  50. if($jobId > 0){
  51. $map[] = ['uj.job_id','=',$jobId];
  52. }else{
  53. if($depId > 0){
  54. $jobs = Db::name('job')
  55. ->where('dep_id',$depId)
  56. ->where('enable',1)
  57. ->where('del',0)
  58. ->column('id');
  59. if($jobs){
  60. $map[] = ['uj.job_id','in',$jobs];
  61. }
  62. }
  63. }
  64. $lists = Db::name('user')
  65. ->alias('u')
  66. ->join('user_info ui','ui.user_id = u.id')
  67. ->join('user_job uj','uj.user_id = u.id')
  68. ->join('job j','j.id = uj.job_id')
  69. ->where($map)
  70. ->field('ui.user_id,ui.name as user_name,u.phone,u.code')
  71. ->page($page,$size)
  72. ->order('u.sorts asc,u.id desc')
  73. ->select();
  74. $lists = $lists?$lists:[];
  75. foreach ($lists as $k=>$v){
  76. $group = Db::name('attendance_group_user')
  77. ->alias('agu')
  78. ->join('attendance_group ug','ug.id = agu.group_id')
  79. ->field('ug.id,ug.name')
  80. ->where('agu.user_id',$v['user_id'])
  81. ->where('agu.org_id',$orgId)
  82. ->find();
  83. $lists[$k]['group_id'] = null;
  84. $lists[$k]['group_name'] = '';
  85. if($group){
  86. $lists[$k]['group_id'] = $group['id'];
  87. $lists[$k]['group_name'] = $group['name'];
  88. }
  89. }
  90. $total = Db::name('user')
  91. ->alias('u')
  92. ->join('user_info ui','ui.user_id = u.id')
  93. ->join('user_job uj','uj.user_id = u.id')
  94. ->join('job j','j.id = uj.job_id')
  95. ->where($map)->count();
  96. $data = [
  97. 'total' => $total,
  98. 'list' => $lists?$lists:[]
  99. ];
  100. return $data;
  101. }
  102. public function userGroup($userId,$orgId,$day){
  103. $reissue_count = config('reissue_count');
  104. $reissue_day = config('reissue_day');
  105. $rule = '每月最多补卡'.$reissue_count.'次,可补过去'.$reissue_day.'天内的缺卡';
  106. if($day == date('Y-m-d')){ // 今日
  107. $data = $this->userCurGroup($userId,$orgId,$rule);
  108. }else{ // 历史
  109. $device_sn = Db::name('user_info')->where('user_id',$userId)->value('device_sn');
  110. $info = Db::name('attendance_user_class')
  111. ->where('day',$day)
  112. ->where('user_id',$userId)
  113. ->find();
  114. if($info){
  115. $record = Db::name('attendance_record')
  116. ->where('user_class_id',$info['id'])
  117. ->where('user_id',$userId)
  118. ->where('effective',1)
  119. ->field('id,lat,lng,address,type,from,create_time,kq_time,result,status,cate,remark')
  120. ->order('create_time asc')
  121. ->select();
  122. $group = Db::name('attendance_group')->where('id',$info['group_id'])->find();
  123. $addr = [];
  124. if($group['addr']){
  125. $addr = Db::name('attendance_addr')
  126. ->where('id','in',$group['addr'])
  127. ->where('enable',1)
  128. ->where('del',0)
  129. ->field('id,title,distance,lat,lng')
  130. ->select();
  131. }
  132. $dates = json_decode($info['content'],true);
  133. $newdate = [];
  134. foreach ($dates as $k=>$v){
  135. $newdate[] = [
  136. 'stime' => date('H:i',strtotime($v['stime'])),
  137. 'etime' => date('H:i',strtotime($v['etime']))
  138. ];
  139. }
  140. $data = [
  141. 'group' => [
  142. 'name' => $group['name'],
  143. 'dates' => $newdate?$newdate:null,
  144. 'rule' => $rule,
  145. 'day' => $day,
  146. 'cate' => $group['cate'],
  147. 'addr' => $addr?$addr:[]
  148. ],
  149. 'record' => $record?$record:[],
  150. 'device_sn' => $device_sn
  151. ];
  152. }else{ // 无考勤
  153. $record = Db::name('attendance_record')
  154. ->where('create_time','>=',date('Y-m-d').' 00:00:00')
  155. ->where('create_time','<=',date('Y-m-d').' 00:00:00')
  156. ->where('user_id',$userId)
  157. ->where('effective',1)
  158. ->field('id,lat,lng,address,type,from,create_time,kq_time,result,status,cate,remark')
  159. ->order('create_time asc')
  160. ->select();
  161. $data = [
  162. 'group' => [
  163. 'name' => '未设置考勤组',
  164. 'dates' => null,
  165. 'rule' => $rule,
  166. 'day' => $day,
  167. 'cate' => '',
  168. 'addr' => []
  169. ],
  170. 'record' => $record?$record:[],
  171. 'device_sn' => $device_sn
  172. ];
  173. }
  174. }
  175. return $data;
  176. }
  177. private function userCurGroup($userId,$orgId,$rule){
  178. $device_sn = Db::name('user_info')->where('user_id',$userId)->value('device_sn');
  179. $curTime = time();
  180. $day = date('Y-m-d');
  181. $yday = date('Y-m-d',time() - 86400);
  182. $nday = date('Y-m-d',time() + 86400);
  183. // 获取今天的考勤组
  184. $info = Db::name('attendance_user_class')
  185. ->where('day',$day)
  186. ->where('user_id',$userId)
  187. ->where('status',0)
  188. ->find();
  189. if($info){ // 今日班次
  190. $record = Db::name('attendance_record')
  191. ->where('user_class_id',$info['id'])
  192. ->where('user_id',$userId)
  193. ->where('effective',1)
  194. ->field('id,lat,lng,address,type,from,create_time,kq_time,result,status,cate,remark')
  195. ->order('create_time asc')
  196. ->select();
  197. $group = Db::name('attendance_group')->where('id',$info['group_id'])->find();
  198. $addr = [];
  199. if($group['addr']){
  200. $addr = Db::name('attendance_addr')
  201. ->where('id','in',$group['addr'])
  202. ->where('enable',1)
  203. ->where('del',0)
  204. ->field('id,title,distance,lat,lng')
  205. ->select();
  206. }
  207. $dates = json_decode($info['content'],true);
  208. $newdate = [];
  209. foreach ($dates as $k=>$v){
  210. $newdate[] = [
  211. 'stime' => date('H:i',strtotime($v['stime'])),
  212. 'etime' => date('H:i',strtotime($v['etime']))
  213. ];
  214. }
  215. $data = [
  216. 'group' => [
  217. 'name' => $group['name'],
  218. 'dates' => $newdate,
  219. 'rule' => $rule,
  220. 'day' => $day,
  221. 'cate' => $group['cate'],
  222. 'addr' => $addr?$addr:[]
  223. ],
  224. 'record' => $record?$record:[],
  225. 'device_sn' => $device_sn
  226. ];
  227. }else{ // 今日班次不存在
  228. // 昨日班次
  229. $yinfo = Db::name('attendance_user_class')
  230. ->where('day',$yday)
  231. ->where('user_id',$userId)
  232. ->where('next',1)
  233. ->where('status',0)
  234. ->find();
  235. // 今日班次
  236. $class = model('AttendanceGroupClass')->getClassByDay($userId);
  237. if(!$class && !$yinfo){
  238. $record = Db::name('attendance_record')
  239. ->where('create_time','>=',date('Y-m-d').' 00:00:00')
  240. ->where('user_id',$userId)
  241. ->where('effective',1)
  242. ->field('id,lat,lng,address,type,from,create_time,kq_time,result,status,cate,remark')
  243. ->order('create_time asc')
  244. ->select();
  245. $data = [
  246. 'group' => [
  247. 'name' => '未设置考勤组',
  248. 'dates' => [],
  249. 'rule' => $rule,
  250. 'day' => $day,
  251. 'cate' => '',
  252. 'addr' => []
  253. ],
  254. 'record' => $record?$record:[],
  255. 'device_sn' => $device_sn
  256. ];
  257. }else if($yinfo && !$class){ // 昨天班次
  258. $group = Db::name('attendance_group')->where('id',$yinfo['group_id'])->find();
  259. $record = Db::name('attendance_record')
  260. ->where('user_class_id',$yinfo['id'])
  261. ->where('user_id',$userId)
  262. ->where('effective',1)
  263. ->field('id,lat,lng,address,type,from,create_time,kq_time,result,status,cate,remark')
  264. ->order('create_time asc')
  265. ->select();
  266. $addr = [];
  267. if($group['addr']){
  268. $addr = Db::name('attendance_addr')
  269. ->where('id','in',$group['addr'])
  270. ->where('enable',1)
  271. ->where('del',0)
  272. ->field('id,title,distance,lat,lng')
  273. ->select();
  274. }
  275. $dates = json_decode($yinfo['content'],true);
  276. $newdate = [];
  277. foreach ($dates as $k=>$v){
  278. $newdate[] = [
  279. 'stime' => date('H:i',strtotime($v['stime'])),
  280. 'etime' => date('H:i',strtotime($v['etime']))
  281. ];
  282. }
  283. $data = [
  284. 'group' => [
  285. 'name' => $group['name'],
  286. 'dates' => $newdate,
  287. 'rule' => $rule,
  288. 'day' => $day,
  289. 'cate' => $group['cate'],
  290. 'addr' => $addr?$addr:[]
  291. ],
  292. 'record' => $record?$record:[],
  293. 'device_sn' => $device_sn
  294. ];
  295. }else if(!$info && $class){ // 今日班次不为空
  296. $nday = date('Y-m-d',time() + 86400);
  297. $content = json_decode($class['content'],true);
  298. $dates = $content['dates'];
  299. $newdates = [];
  300. $newdates2 = [];
  301. foreach ($dates as $k=>$v) {
  302. $newdates[] = [
  303. "stime" => $v['snext'] > 0 ? $nday . ' ' . $v['stime'] . ':00' : $day . ' ' . $v['stime'] . ':00',
  304. "etime" => $v['enext'] > 0 ? $nday . ' ' . $v['etime'] . ':00' : $day . ' ' . $v['etime'] . ':00',
  305. "sstatus" => 0,
  306. "estatus" => 0,
  307. ];
  308. $newdates2[] = [
  309. "stime" => $v['stime'],
  310. "etime" => $v['etime'],
  311. ];
  312. }
  313. $addr = [];
  314. if($class['addr']){
  315. $addr = Db::name('attendance_addr')
  316. ->where('id','in',$class['addr'])
  317. ->where('enable',1)
  318. ->where('del',0)
  319. ->field('id,title,distance,lat,lng')
  320. ->select();
  321. }
  322. $data = [
  323. 'group' => [
  324. 'name' => $class['group_name'],
  325. 'dates' => $newdates2,
  326. 'rule' => $rule,
  327. 'day' => $day,
  328. 'cate' => $yinfo['cate'],
  329. 'addr' => $addr?$addr:[]
  330. ],
  331. 'record' => [],
  332. 'device_sn' => $device_sn
  333. ];
  334. }else{
  335. $nday = date('Y-m-d',time() + 86400);
  336. $content = json_decode($class['content'],true);
  337. $dates = $content['dates'];
  338. $newdates = [];
  339. $newdates2 = [];
  340. foreach ($dates as $k=>$v) {
  341. $newdates[] = [
  342. "stime" => $v['snext'] > 0 ? $nday . ' ' . $v['stime'] . ':00' : $day . ' ' . $v['stime'] . ':00',
  343. "etime" => $v['enext'] > 0 ? $nday . ' ' . $v['etime'] . ':00' : $day . ' ' . $v['etime'] . ':00',
  344. "sstatus" => 0,
  345. "estatus" => 0,
  346. ];
  347. $newdates2[] = [
  348. "stime" => $v['stime'],
  349. "etime" => $v['etime'],
  350. ];
  351. }
  352. if($curTime >= strtotime($newdates[0]['stime']) - 2*60){ // 显示今日
  353. $nday = date('Y-m-d',time() + 86400);
  354. $content = json_decode($class['content'],true);
  355. $dates = $content['dates'];
  356. $newdates = [];
  357. $newdates2 = [];
  358. foreach ($dates as $k=>$v) {
  359. $newdates[] = [
  360. "stime" => $v['snext'] > 0 ? $nday . ' ' . $v['stime'] . ':00' : $day . ' ' . $v['stime'] . ':00',
  361. "etime" => $v['enext'] > 0 ? $nday . ' ' . $v['etime'] . ':00' : $day . ' ' . $v['etime'] . ':00',
  362. "sstatus" => 0,
  363. "estatus" => 0,
  364. ];
  365. $newdates2[] = [
  366. "stime" => $v['stime'],
  367. "etime" => $v['etime'],
  368. ];
  369. }
  370. $addr = [];
  371. if($class['addr']){
  372. $addr = Db::name('attendance_addr')
  373. ->where('id','in',$class['addr'])
  374. ->where('enable',1)
  375. ->where('del',0)
  376. ->field('id,title,distance,lat,lng')
  377. ->select();
  378. }
  379. $data = [
  380. 'group' => [
  381. 'name' => $class['group_name'],
  382. 'dates' => $newdates2,
  383. 'rule' => $rule,
  384. 'day' => $day,
  385. 'cate' => $yinfo['cate'],
  386. 'addr' => $addr?$addr:[]
  387. ],
  388. 'record' => [],
  389. 'device_sn' => $device_sn
  390. ];
  391. }else{ // 显示昨日
  392. $group_name = Db::name('attendance_group')->where('id',$yinfo['group_id'])->value('name');
  393. $record = Db::name('attendance_record')
  394. ->where('user_class_id',$yinfo['id'])
  395. ->where('user_id',$userId)
  396. ->where('effective',1)
  397. ->field('id,lat,lng,address,type,from,create_time,kq_time,result,status,cate,remark')
  398. ->order('create_time asc')
  399. ->select();
  400. $dates = json_decode($yinfo['content'],true);
  401. $newdate = [];
  402. foreach ($dates as $k=>$v){
  403. $newdate[] = [
  404. 'stime' => date('H:i',strtotime($v['stime'])),
  405. 'etime' => date('H:i',strtotime($v['etime']))
  406. ];
  407. }
  408. $addr = [];
  409. if($yinfo['addr']){
  410. $addr = Db::name('attendance_addr')
  411. ->where('id','in',$yinfo['addr'])
  412. ->where('enable',1)
  413. ->where('del',0)
  414. ->field('id,title,distance,lat,lng')
  415. ->select();
  416. }
  417. $data = [
  418. 'group' => [
  419. 'name' => $group_name,
  420. 'dates' => $newdate,
  421. 'rule' => $rule,
  422. 'day' => $day,
  423. 'cate' => $yinfo['cate'],
  424. 'addr' => $addr?$addr:[]
  425. ],
  426. 'record' => $record?$record:[],
  427. 'device_sn' => $device_sn
  428. ];
  429. }
  430. }
  431. }
  432. return $data;
  433. }
  434. }