AttendanceRecord.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. <?php
  2. namespace app\common\model;
  3. use app\hander\HelpHander;
  4. use think\Db;
  5. use think\Exception;
  6. class AttendanceRecord extends Base{
  7. public function sjsign($data){
  8. //检查是否绑定设备
  9. $device = Db::name('user')->where('id',$data['user_id'])->value('device_sn');
  10. if(!$device){
  11. HelpHander::error('未绑定设备号');
  12. }
  13. if($device != $data['device_sn']){
  14. HelpHander::error('该设备与绑定设备不一致');
  15. }
  16. // TODO::检查地点距离
  17. $ret = $this->sign($data['device_sn'],$data['user_id'],$data['signtime'],1,$data['lat'],$data['lng'],$data['address'],$data['remark']);
  18. if(!$ret){
  19. HelpHander::error($this->error);
  20. }
  21. return true;
  22. }
  23. // 为跨天的需要加一个定时任务,使考勤组状态变更
  24. public function sign($sn,$pin,$signdate,$type=3,$lat='',$lng='',$address='',$remark=''){
  25. //先检查这个时间点是否已记录在系统内
  26. $res = Db::name('attendance_record')->where('user_id',$pin)->where('create_time',$signdate)->find();
  27. if($res){ // 已存在返回已处理
  28. return true;
  29. }
  30. $curTime = date('Y-m-d H:i:s');
  31. $signtime = strtotime($signdate);
  32. // 检查当前打开时间是否有效 checkValid
  33. $ret = Db::name('attendance_record')
  34. ->alias('ar')
  35. ->join('attendance_user_class auc','auc.id = ar.user_class_id')
  36. ->where('ar.user_id',$pin)
  37. ->where('auc.status',1)
  38. ->where('ar.create_time','>',$signdate)
  39. ->field('ar.*')
  40. ->find();
  41. if($ret){ // 时间无效,则标记为已处理
  42. return true;
  43. }
  44. Db::startTrans();
  45. try{
  46. //先检查今天是否已生成打卡记录,未生成打卡记录需要查询昨天的考勤是否有跨天情况,取出今天的排班情况
  47. $info = model('AttendanceUserClass')->getCurGroup($pin);
  48. if($info) { // 获取当前的班次,没有班次,当做外勤打卡
  49. // 检查支持的考勤类型
  50. $cate = Db::name('attendance_group')->where('id',$info['group_id'])->value('cate');
  51. if($type == 1){ // 手机打开
  52. if(!$cate){
  53. trace('不支持手机打卡');
  54. Db::commit();
  55. return true;
  56. }else{
  57. $cates = explode(',',$cate);
  58. if(!in_array(1,$cates)){
  59. trace('不支持手机打卡');
  60. Db::commit();
  61. return true;
  62. }
  63. }
  64. }else if($type == 3){ //考勤机打卡
  65. if(!$cate){
  66. trace('不支持考勤机打卡');
  67. Db::commit();
  68. return true;
  69. }else{
  70. $cates = explode(',',$cate);
  71. if(!in_array(2,$cates)){
  72. trace('不支持考勤机打卡');
  73. Db::commit();
  74. return true;
  75. }
  76. }
  77. }
  78. if($info['day'] == date('Y-m-d') && $info['day'] != date('Y-m-d',$signtime)){ // 昨天的时间,今天的班次,不能使用
  79. Db::commit();
  80. return true;
  81. }
  82. $dates = json_decode($info['content'], true);
  83. $records = Db::name('attendance_record')->where('user_class_id', $info['id'])->order('create_time asc')->select();
  84. $records = $records ? $records : [];
  85. $nrecords = [];
  86. $flag = false;
  87. $ndata = [
  88. 'kq_time' => '',
  89. 'user_id' => $pin,
  90. 'org_id' => $info['org_id'],
  91. 'type' => $type,
  92. 'lat' => $lat,
  93. 'lng' => $lng,
  94. 'address' => $address,
  95. 'from' => $sn,
  96. 'result' => '',
  97. 'create_time' => date('Y-m-d H:i:s',$signtime),
  98. 'add_time' => $curTime,
  99. 'user_class_id' => $info['id'],
  100. 'effective' => 0,
  101. 'status' => 0,
  102. 'cate' => 0,
  103. 'duration' => 0,
  104. 'remark'=>$remark,
  105. ];
  106. foreach ($records as $k => $v) {
  107. if (!$flag && strtotime($v['create_time']) > $signtime) {
  108. $nrecords[] = $ndata;
  109. $flag = true;
  110. }
  111. $nrecords[] = $v;
  112. }
  113. if(!$flag){ // 为最大值时
  114. $nrecords[] = $ndata;
  115. }
  116. if (!$nrecords) {
  117. $nrecords[] = $ndata;
  118. }
  119. $datecount = count($dates);
  120. if ($datecount == 1) { // 一天一个班次
  121. foreach ($nrecords as $k => $v) {
  122. if ($k == 0) { // 上班
  123. $nrecords[$k]['kq_time'] = $dates[0]['stime'];
  124. $nrecords[$k]['effective'] = 1;
  125. $nrecords[$k]['cate'] = 1;
  126. $nrecords[$k]['status'] = 0;
  127. $nrecords[$k]['duration'] = 0;
  128. $nrecords[$k]['result'] = '正常';
  129. $dates[0]['sstatus'] = 1;
  130. $dates[0]['ssign'] = $v['create_time'];
  131. if (strtotime($v['create_time']) > strtotime($dates[0]['stime'])) { // 迟到
  132. $nrecords[$k]['status'] = 1;
  133. $nrecords[$k]['duration'] = strtotime($v['create_time']) - strtotime($dates[0]['stime']);
  134. $nrecords[$k]['result'] = '迟到';
  135. }
  136. } else { // 下班
  137. $nrecords[$k]['kq_time'] = $dates[0]['etime'];
  138. $nrecords[$k]['effective'] = 1;
  139. $nrecords[$k]['cate'] = 2;
  140. $nrecords[$k]['status'] = 0;
  141. $nrecords[$k]['duration'] = 0;
  142. $nrecords[$k]['result'] = '正常';
  143. $dates[0]['estatus'] = 1;
  144. $dates[0]['esign'] = $v['create_time'];
  145. //判断是否是最后一条
  146. if ($k + 1 == count($nrecords)) { // 是
  147. if (strtotime($v['create_time']) < strtotime($dates[0]['etime'])) { // 早退
  148. $nrecords[$k]['result'] = '早退';
  149. $nrecords[$k]['status'] = 2;
  150. $nrecords[$k]['duration'] = strtotime($dates[0]['etime']) - strtotime($v['create_time']);
  151. }
  152. } else { // 否,无效记录
  153. $nrecords[$k]['effective'] = 0;
  154. $nrecords[$k]['status'] = 0;
  155. $nrecords[$k]['duration'] = 0;
  156. $nrecords[$k]['result'] = '打卡无效:此记录已被更新';
  157. }
  158. }
  159. }
  160. }else if($datecount == 2){ // 一天二个班次
  161. foreach ($nrecords as $k => $v) {
  162. if ($k == 0) { // 1次上班
  163. $nrecords[$k]['kq_time'] = $dates[0]['stime'];
  164. $nrecords[$k]['effective'] = 1;
  165. $nrecords[$k]['cate'] = 1;
  166. $nrecords[$k]['status'] = 0;
  167. $nrecords[$k]['duration'] = 0;
  168. $nrecords[$k]['result'] = '正常';
  169. $dates[0]['sstatus'] = 1;
  170. $dates[0]['ssign'] = $v['create_time'];
  171. if (strtotime($v['create_time']) > strtotime($dates[0]['stime'])) { // 迟到
  172. $nrecords[$k]['status'] = 1;
  173. $nrecords[$k]['duration'] = strtotime($v['create_time']) - strtotime($dates[0]['stime']);
  174. $nrecords[$k]['result'] = '迟到';
  175. }
  176. } else if ($k == 1){ // 1次下班
  177. $nrecords[$k]['kq_time'] = $dates[0]['etime'];
  178. $nrecords[$k]['effective'] = 1;
  179. $nrecords[$k]['cate'] = 2;
  180. $nrecords[$k]['status'] = 0;
  181. $nrecords[$k]['duration'] = 0;
  182. $nrecords[$k]['result'] = '正常';
  183. $dates[0]['estatus'] = 1;
  184. $dates[0]['esign'] = $v['create_time'];
  185. if (strtotime($v['create_time']) < strtotime($dates[0]['etime'])) { // 早退
  186. $nrecords[$k]['result'] = '早退';
  187. $nrecords[$k]['status'] = 2;
  188. $nrecords[$k]['duration'] = strtotime($dates[0]['etime']) - strtotime($v['create_time']);
  189. }
  190. } else if ($k == 2){ // 2次上班
  191. $nrecords[$k]['kq_time'] = $dates[1]['stime'];
  192. $nrecords[$k]['effective'] = 1;
  193. $nrecords[$k]['cate'] = 1;
  194. $nrecords[$k]['status'] = 0;
  195. $nrecords[$k]['duration'] = 0;
  196. $nrecords[$k]['result'] = '正常';
  197. $dates[1]['sstatus'] = 1;
  198. $dates[1]['ssign'] = $v['create_time'];
  199. if (strtotime($v['create_time']) > strtotime($dates[1]['stime'])) { // 迟到
  200. $nrecords[$k]['status'] = 1;
  201. $nrecords[$k]['duration'] = strtotime($v['create_time']) - strtotime($dates[1]['stime']);
  202. $nrecords[$k]['result'] = '迟到';
  203. }
  204. } else { // 2次下班
  205. $nrecords[$k]['kq_time'] = $dates[1]['etime'];
  206. $nrecords[$k]['effective'] = 1;
  207. $nrecords[$k]['cate'] = 2;
  208. $nrecords[$k]['status'] = 0;
  209. $nrecords[$k]['duration'] = 0;
  210. $nrecords[$k]['result'] = '正常';
  211. $dates[1]['estatus'] = 1;
  212. $dates[1]['esign'] = $v['create_time'];
  213. //判断是否是最后一条
  214. if ($k + 1 == count($nrecords)) { // 是
  215. if (strtotime($v['create_time']) < strtotime($dates[1]['etime'])) { // 早退
  216. $nrecords[$k]['result'] = '早退';
  217. $nrecords[$k]['status'] = 2;
  218. $nrecords[$k]['duration'] = strtotime($dates[1]['etime']) - strtotime($v['create_time']);
  219. }
  220. } else { // 否,无效记录
  221. $nrecords[$k]['effective'] = 0;
  222. $nrecords[$k]['status'] = 0;
  223. $nrecords[$k]['duration'] = 0;
  224. $nrecords[$k]['result'] = '打卡无效:此记录已被更新';
  225. }
  226. }
  227. }
  228. }else if($datecount == 3){ // 一天三个班次
  229. foreach ($nrecords as $k => $v) {
  230. if ($k == 0) { // 1次上班
  231. $nrecords[$k]['kq_time'] = $dates[0]['stime'];
  232. $nrecords[$k]['effective'] = 1;
  233. $nrecords[$k]['cate'] = 1;
  234. $nrecords[$k]['status'] = 0;
  235. $nrecords[$k]['duration'] = 0;
  236. $nrecords[$k]['result'] = '正常';
  237. $dates[0]['sstatus'] = 1;
  238. $dates[0]['ssign'] = $v['create_time'];
  239. if (strtotime($v['create_time']) > strtotime($dates[0]['stime'])) { // 迟到
  240. $nrecords[$k]['status'] = 1;
  241. $nrecords[$k]['duration'] = strtotime($v['create_time']) - strtotime($dates[0]['stime']);
  242. $nrecords[$k]['result'] = '迟到';
  243. }
  244. } else if ($k == 1){ // 1次下班
  245. $nrecords[$k]['kq_time'] = $dates[0]['etime'];
  246. $nrecords[$k]['effective'] = 1;
  247. $nrecords[$k]['cate'] = 2;
  248. $nrecords[$k]['status'] = 0;
  249. $nrecords[$k]['duration'] = 0;
  250. $nrecords[$k]['result'] = '正常';
  251. $dates[0]['estatus'] = 1;
  252. $dates[0]['esign'] = $v['create_time'];
  253. if (strtotime($v['create_time']) < strtotime($dates[0]['etime'])) { // 早退
  254. $nrecords[$k]['result'] = '早退';
  255. $nrecords[$k]['status'] = 2;
  256. $nrecords[$k]['duration'] = strtotime($dates[0]['etime']) - strtotime($v['create_time']);
  257. }
  258. } else if ($k == 2){ // 2次上班
  259. $nrecords[$k]['kq_time'] = $dates[1]['stime'];
  260. $nrecords[$k]['effective'] = 1;
  261. $nrecords[$k]['cate'] = 1;
  262. $nrecords[$k]['status'] = 0;
  263. $nrecords[$k]['duration'] = 0;
  264. $nrecords[$k]['result'] = '正常';
  265. $dates[1]['sstatus'] = 1;
  266. $dates[1]['ssign'] = $v['create_time'];
  267. if (strtotime($v['create_time']) > strtotime($dates[1]['stime'])) { // 迟到
  268. $nrecords[$k]['status'] = 1;
  269. $nrecords[$k]['duration'] = strtotime($v['create_time']) - strtotime($dates[1]['stime']);
  270. $nrecords[$k]['result'] = '迟到';
  271. }
  272. } else if ($k == 3){ // 2次下班
  273. $nrecords[$k]['kq_time'] = $dates[1]['etime'];
  274. $nrecords[$k]['effective'] = 1;
  275. $nrecords[$k]['cate'] = 2;
  276. $nrecords[$k]['status'] = 0;
  277. $nrecords[$k]['duration'] = 0;
  278. $nrecords[$k]['result'] = '正常';
  279. $dates[1]['estatus'] = 1;
  280. $dates[1]['esign'] = $v['create_time'];
  281. if (strtotime($v['create_time']) < strtotime($dates[1]['etime'])) { // 早退
  282. $nrecords[$k]['result'] = '早退';
  283. $nrecords[$k]['status'] = 2;
  284. $nrecords[$k]['duration'] = strtotime($dates[1]['etime']) - strtotime($v['create_time']);
  285. }
  286. } else if ($k == 4){ // 3次上班
  287. $nrecords[$k]['kq_time'] = $dates[2]['stime'];
  288. $nrecords[$k]['effective'] = 1;
  289. $nrecords[$k]['cate'] = 1;
  290. $nrecords[$k]['status'] = 0;
  291. $nrecords[$k]['duration'] = 0;
  292. $nrecords[$k]['result'] = '正常';
  293. $dates[2]['sstatus'] = 1;
  294. $dates[2]['ssign'] = $v['create_time'];
  295. if (strtotime($v['create_time']) > strtotime($dates[2]['stime'])) { // 迟到
  296. $nrecords[$k]['status'] = 1;
  297. $nrecords[$k]['duration'] = strtotime($v['create_time']) - strtotime($dates[2]['stime']);
  298. $nrecords[$k]['result'] = '迟到';
  299. }
  300. } else { // 3次下班
  301. $nrecords[$k]['kq_time'] = $dates[2]['etime'];
  302. $nrecords[$k]['effective'] = 1;
  303. $nrecords[$k]['cate'] = 2;
  304. $nrecords[$k]['status'] = 0;
  305. $nrecords[$k]['duration'] = 0;
  306. $nrecords[$k]['result'] = '正常';
  307. $dates[2]['estatus'] = 1;
  308. $dates[2]['esign'] = $v['create_time'];
  309. //判断是否是最后一条
  310. if ($k + 1 == count($nrecords)) { // 是
  311. if (strtotime($v['create_time']) < strtotime($dates[2]['etime'])) { // 早退
  312. $nrecords[$k]['result'] = '早退';
  313. $nrecords[$k]['status'] = 2;
  314. $nrecords[$k]['duration'] = strtotime($dates[2]['etime']) - strtotime($v['create_time']);
  315. }
  316. } else { // 否,无效记录
  317. $nrecords[$k]['effective'] = 0;
  318. $nrecords[$k]['status'] = 0;
  319. $nrecords[$k]['duration'] = 0;
  320. $nrecords[$k]['result'] = '打卡无效:此记录已被更新';
  321. }
  322. }
  323. }
  324. }
  325. foreach ($nrecords as $k=>$v){
  326. $v['update_time'] = date('Y-m-d H:i:s');
  327. if(isset($v['id']) && $v['id'] > 0){ //修改记录
  328. $ret = Db::name('attendance_record')->where('id',$v['id'])->update($v);
  329. }else{ // 新增记录
  330. $ret = Db::name('attendance_record')->insert($v);
  331. }
  332. if(!$ret){
  333. \exception('打卡失败');
  334. }
  335. }
  336. $res = Db::name('attendance_user_class')->where('id',$info['id'])->update([
  337. 'content' => json_encode($dates),
  338. 'update_time' => date('Y-m-d H:i:s')
  339. ]);
  340. if(!$res){
  341. \exception('打卡失败');
  342. }
  343. } else { // 外勤打卡
  344. $org_id = Db::name('user')
  345. ->alias('u')
  346. ->join('user_roles ur','ur.user_id = u.id')
  347. ->join('roles r','r.id = ur.roles_id')
  348. ->where('u.id',$pin)
  349. ->where('r.type',3)
  350. ->value('r.org_id');
  351. $rdata = [
  352. 'user_id' => $pin,
  353. 'org_id' => $org_id?$org_id:0,
  354. 'type' => $type,
  355. 'lat' => $lat,
  356. 'lng' => $lng,
  357. 'address' => $address,
  358. 'from' => $sn,
  359. 'result' => '外勤',
  360. 'create_time' => date('Y-m-d H:i:s',$signtime),
  361. 'add_time' => date('Y-m-d H:i:s'),
  362. 'user_class_id' => 0,
  363. 'effective' => 1,
  364. 'status' => 0,
  365. 'cate' => 0,
  366. 'duration' => 0
  367. ];
  368. $ret = Db::name('attendance_record')->insert($rdata);
  369. if(!$ret){
  370. \exception('打卡失败');
  371. }
  372. }
  373. Db::commit();
  374. }catch (Exception $e){
  375. trace($e->getMessage());
  376. $this->error = $e->getMessage();
  377. Db::rollback();
  378. return false;
  379. }
  380. return true;
  381. }
  382. function bundleDevice($userId,$deviceSn){
  383. if(!$deviceSn){
  384. HelpHander::error('参数错误');
  385. }
  386. $user = Db::name('user')->where('id',$userId)->find();
  387. if(!$user){
  388. HelpHander::error('用户不存在');
  389. }
  390. if($user['device_sn']){
  391. HelpHander::error('该用户已绑定设备');
  392. }
  393. $uinfo = Db::name('user')->where('device_sn',$deviceSn)->find();
  394. if($uinfo){
  395. HelpHander::error('该设备已绑定用户');
  396. }
  397. $ret = Db::name('user')->where('id',$userId)->setField('device_sn',$deviceSn);
  398. if(!$ret){
  399. HelpHander::error('操作失败');
  400. }
  401. return true;
  402. }
  403. }