AttendanceGroupUser.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. <?php
  2. namespace app\common\model;
  3. use app\hander\HelpHander;
  4. use think\Db;
  5. use think\Exception;
  6. class AttendanceGroupUser extends Base{
  7. public function userGroup($userId,$orgId,$day){
  8. $reissue_count = config('reissue_count');
  9. $reissue_day = config('reissue_day');
  10. $rule = '每月最多补卡'.$reissue_count.'次,可补过去'.$reissue_day.'天内的缺卡';
  11. if($day == date('Y-m-d')){ // 今日
  12. $data = $this->userCurGroup($userId,$orgId,$rule);
  13. }else{ // 历史
  14. $device_sn = Db::name('user')->where('id',$userId)->value('device_sn');
  15. $device_sn = $device_sn?$device_sn:"";
  16. $info = Db::name('attendance_user_class')
  17. ->where('day',$day)
  18. ->where('user_id',$userId)
  19. ->find();
  20. if($info){
  21. $record = Db::name('attendance_record')
  22. ->where('user_class_id',$info['id'])
  23. ->where('user_id',$userId)
  24. ->where('effective',1)
  25. ->field('id,lat,lng,address,type,from,create_time,kq_time,result,status,cate,remark')
  26. ->order('create_time asc')
  27. ->select();
  28. $group = Db::name('attendance_group')->where('id',$info['group_id'])->find();
  29. $addr = [];
  30. if($group['addr']){
  31. $addr = Db::name('attendance_addr')
  32. ->where('id','in',$group['addr'])
  33. ->where('enable',1)
  34. ->where('del',0)
  35. ->field('id,title,distance,lat,lng')
  36. ->select();
  37. }
  38. $dates = json_decode($info['content'],true);
  39. $newdate = [];
  40. foreach ($dates as $k=>$v){
  41. $newdate[] = [
  42. 'stime' => date('H:i',strtotime($v['stime'])),
  43. 'etime' => date('H:i',strtotime($v['etime']))
  44. ];
  45. }
  46. $data = [
  47. 'group' => [
  48. 'name' => $group['name'],
  49. 'dates' => $newdate?$newdate:null,
  50. 'rule' => $rule,
  51. 'day' => $day,
  52. 'cate' => $group['cate'],
  53. 'addr' => $addr?$addr:[]
  54. ],
  55. 'record' => $record?$record:[],
  56. 'device_sn' => $device_sn
  57. ];
  58. }else{ // 无考勤
  59. $record = Db::name('attendance_record')
  60. ->where('create_time','>=',date('Y-m-d').' 00:00:00')
  61. ->where('create_time','<=',date('Y-m-d').' 00:00:00')
  62. ->where('user_id',$userId)
  63. ->where('effective',1)
  64. ->field('id,lat,lng,address,type,from,create_time,kq_time,result,status,cate,remark')
  65. ->order('create_time asc')
  66. ->select();
  67. $data = [
  68. 'group' => [
  69. 'name' => '未设置考勤组',
  70. 'dates' => null,
  71. 'rule' => $rule,
  72. 'day' => $day,
  73. 'cate' => '',
  74. 'addr' => []
  75. ],
  76. 'record' => $record?$record:[],
  77. 'device_sn' => $device_sn
  78. ];
  79. }
  80. }
  81. return $data;
  82. }
  83. private function userCurGroup($userId,$orgId,$rule){
  84. $device_sn = Db::name('user')->where('id',$userId)->value('device_sn');
  85. $curTime = time();
  86. $day = date('Y-m-d');
  87. $yday = date('Y-m-d',time() - 86400);
  88. $nday = date('Y-m-d',time() + 86400);
  89. // 获取今天的考勤组
  90. $info = Db::name('attendance_user_class')
  91. ->where('day',$day)
  92. ->where('user_id',$userId)
  93. ->where('status',0)
  94. ->find();
  95. if($info){ // 今日班次
  96. $record = Db::name('attendance_record')
  97. ->where('user_class_id',$info['id'])
  98. ->where('user_id',$userId)
  99. ->where('effective',1)
  100. ->field('id,lat,lng,address,type,from,create_time,kq_time,result,status,cate,remark')
  101. ->order('create_time asc')
  102. ->select();
  103. $group = Db::name('attendance_group')->where('id',$info['group_id'])->find();
  104. $addr = [];
  105. if($group['addr']){
  106. $addr = Db::name('attendance_addr')
  107. ->where('id','in',$group['addr'])
  108. ->where('enable',1)
  109. ->where('del',0)
  110. ->field('id,title,distance,lat,lng')
  111. ->select();
  112. }
  113. $dates = json_decode($info['content'],true);
  114. $newdate = [];
  115. foreach ($dates as $k=>$v){
  116. $newdate[] = [
  117. 'stime' => date('H:i',strtotime($v['stime'])),
  118. 'etime' => date('H:i',strtotime($v['etime']))
  119. ];
  120. }
  121. $data = [
  122. 'group' => [
  123. 'name' => $group['name'],
  124. 'dates' => $newdate,
  125. 'rule' => $rule,
  126. 'day' => $day,
  127. 'cate' => $group['cate'],
  128. 'addr' => $addr?$addr:[]
  129. ],
  130. 'record' => $record?$record:[],
  131. 'device_sn' => $device_sn
  132. ];
  133. }else{ // 今日班次不存在
  134. // 昨日班次
  135. $yinfo = Db::name('attendance_user_class')
  136. ->where('day',$yday)
  137. ->where('user_id',$userId)
  138. ->where('next',1)
  139. ->where('status',0)
  140. ->find();
  141. // 今日班次
  142. $class = model('AttendanceGroupClass')->getClassByDay($userId);
  143. if(!$class && !$yinfo){
  144. $record = Db::name('attendance_record')
  145. ->where('create_time','>=',date('Y-m-d').' 00:00:00')
  146. ->where('user_id',$userId)
  147. ->where('effective',1)
  148. ->field('id,lat,lng,address,type,from,create_time,kq_time,result,status,cate,remark')
  149. ->order('create_time asc')
  150. ->select();
  151. $data = [
  152. 'group' => [
  153. 'name' => '未设置考勤组',
  154. 'dates' => [],
  155. 'rule' => $rule,
  156. 'day' => $day,
  157. 'cate' => '',
  158. 'addr' => []
  159. ],
  160. 'record' => $record?$record:[],
  161. 'device_sn' => $device_sn
  162. ];
  163. }else if($yinfo && !$class){ // 昨天班次
  164. $group = Db::name('attendance_group')->where('id',$yinfo['group_id'])->find();
  165. $record = Db::name('attendance_record')
  166. ->where('user_class_id',$yinfo['id'])
  167. ->where('user_id',$userId)
  168. ->where('effective',1)
  169. ->field('id,lat,lng,address,type,from,create_time,kq_time,result,status,cate,remark')
  170. ->order('create_time asc')
  171. ->select();
  172. $addr = [];
  173. if($group['addr']){
  174. $addr = Db::name('attendance_addr')
  175. ->where('id','in',$group['addr'])
  176. ->where('enable',1)
  177. ->where('del',0)
  178. ->field('id,title,distance,lat,lng')
  179. ->select();
  180. }
  181. $dates = json_decode($yinfo['content'],true);
  182. $newdate = [];
  183. foreach ($dates as $k=>$v){
  184. $newdate[] = [
  185. 'stime' => date('H:i',strtotime($v['stime'])),
  186. 'etime' => date('H:i',strtotime($v['etime']))
  187. ];
  188. }
  189. $data = [
  190. 'group' => [
  191. 'name' => $group['name'],
  192. 'dates' => $newdate,
  193. 'rule' => $rule,
  194. 'day' => $day,
  195. 'cate' => $group['cate'],
  196. 'addr' => $addr?$addr:[]
  197. ],
  198. 'record' => $record?$record:[],
  199. 'device_sn' => $device_sn
  200. ];
  201. }else if(!$info && $class){ // 今日班次不为空
  202. $nday = date('Y-m-d',time() + 86400);
  203. $content = json_decode($class['content'],true);
  204. $dates = $content['dates'];
  205. $newdates = [];
  206. $newdates2 = [];
  207. foreach ($dates as $k=>$v) {
  208. $newdates[] = [
  209. "stime" => $v['snext'] > 0 ? $nday . ' ' . $v['stime'] . ':00' : $day . ' ' . $v['stime'] . ':00',
  210. "etime" => $v['enext'] > 0 ? $nday . ' ' . $v['etime'] . ':00' : $day . ' ' . $v['etime'] . ':00',
  211. "sstatus" => 0,
  212. "estatus" => 0,
  213. ];
  214. $newdates2[] = [
  215. "stime" => $v['stime'],
  216. "etime" => $v['etime'],
  217. ];
  218. }
  219. $addr = [];
  220. if($class['addr']){
  221. $addr = Db::name('attendance_addr')
  222. ->where('id','in',$class['addr'])
  223. ->where('enable',1)
  224. ->where('del',0)
  225. ->field('id,title,distance,lat,lng')
  226. ->select();
  227. }
  228. $group = Db::name('attendance_group')
  229. ->where('id',$yinfo['group_id'])->find();
  230. $data = [
  231. 'group' => [
  232. 'name' => $class['group_name'],
  233. 'dates' => $newdates2,
  234. 'rule' => $rule,
  235. 'day' => $day,
  236. 'cate' => $group['cate'],
  237. 'addr' => $addr?$addr:[]
  238. ],
  239. 'record' => [],
  240. 'device_sn' => $device_sn
  241. ];
  242. }else{
  243. $nday = date('Y-m-d',time() + 86400);
  244. $content = json_decode($class['content'],true);
  245. $dates = $content['dates'];
  246. $newdates = [];
  247. $newdates2 = [];
  248. foreach ($dates as $k=>$v) {
  249. $newdates[] = [
  250. "stime" => $v['snext'] > 0 ? $nday . ' ' . $v['stime'] . ':00' : $day . ' ' . $v['stime'] . ':00',
  251. "etime" => $v['enext'] > 0 ? $nday . ' ' . $v['etime'] . ':00' : $day . ' ' . $v['etime'] . ':00',
  252. "sstatus" => 0,
  253. "estatus" => 0,
  254. ];
  255. $newdates2[] = [
  256. "stime" => $v['stime'],
  257. "etime" => $v['etime'],
  258. ];
  259. }
  260. if($curTime >= strtotime($newdates[0]['stime']) - 2*60){ // 显示今日
  261. $nday = date('Y-m-d',time() + 86400);
  262. $content = json_decode($class['content'],true);
  263. $dates = $content['dates'];
  264. $newdates = [];
  265. $newdates2 = [];
  266. foreach ($dates as $k=>$v) {
  267. $newdates[] = [
  268. "stime" => $v['snext'] > 0 ? $nday . ' ' . $v['stime'] . ':00' : $day . ' ' . $v['stime'] . ':00',
  269. "etime" => $v['enext'] > 0 ? $nday . ' ' . $v['etime'] . ':00' : $day . ' ' . $v['etime'] . ':00',
  270. "sstatus" => 0,
  271. "estatus" => 0,
  272. ];
  273. $newdates2[] = [
  274. "stime" => $v['stime'],
  275. "etime" => $v['etime'],
  276. ];
  277. }
  278. $addr = [];
  279. if($class['addr']){
  280. $addr = Db::name('attendance_addr')
  281. ->where('id','in',$class['addr'])
  282. ->where('enable',1)
  283. ->where('del',0)
  284. ->field('id,title,distance,lat,lng')
  285. ->select();
  286. }
  287. $data = [
  288. 'group' => [
  289. 'name' => $class['group_name'],
  290. 'dates' => $newdates2,
  291. 'rule' => $rule,
  292. 'day' => $day,
  293. 'cate' => $yinfo['cate'],
  294. 'addr' => $addr?$addr:[]
  295. ],
  296. 'record' => [],
  297. 'device_sn' => $device_sn
  298. ];
  299. }else{ // 显示昨日
  300. $group_name = Db::name('attendance_group')->where('id',$yinfo['group_id'])->value('name');
  301. $record = Db::name('attendance_record')
  302. ->where('user_class_id',$yinfo['id'])
  303. ->where('user_id',$userId)
  304. ->where('effective',1)
  305. ->field('id,lat,lng,address,type,from,create_time,kq_time,result,status,cate,remark')
  306. ->order('create_time asc')
  307. ->select();
  308. $dates = json_decode($yinfo['content'],true);
  309. $newdate = [];
  310. foreach ($dates as $k=>$v){
  311. $newdate[] = [
  312. 'stime' => date('H:i',strtotime($v['stime'])),
  313. 'etime' => date('H:i',strtotime($v['etime']))
  314. ];
  315. }
  316. $addr = [];
  317. if($yinfo['addr']){
  318. $addr = Db::name('attendance_addr')
  319. ->where('id','in',$yinfo['addr'])
  320. ->where('enable',1)
  321. ->where('del',0)
  322. ->field('id,title,distance,lat,lng')
  323. ->select();
  324. }
  325. $data = [
  326. 'group' => [
  327. 'name' => $group_name,
  328. 'dates' => $newdate,
  329. 'rule' => $rule,
  330. 'day' => $day,
  331. 'cate' => $yinfo['cate'],
  332. 'addr' => $addr?$addr:[]
  333. ],
  334. 'record' => $record?$record:[],
  335. 'device_sn' => $device_sn
  336. ];
  337. }
  338. }
  339. }
  340. return $data;
  341. }
  342. }