AttendanceRecordResult.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  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 AttendanceRecordResult extends Model
  8. {
  9. protected $connection = 'db_config_wy';
  10. public function situationByMonth($month,$staffId,$orgId){
  11. $date = $month.'-01';
  12. $timestamp = strtotime( $date );
  13. $start_time = date( 'Y-m-d', $timestamp );
  14. $mdays = date( 't', $timestamp );
  15. $end_time = date( 'Y-m-' . $mdays, $timestamp );
  16. $lists = Db::name('attendance_user_class')
  17. ->where('user_id',$staffId)
  18. ->where('org_id',$orgId)
  19. ->where('day','>=',$start_time)
  20. ->where('day','<=',$end_time)
  21. ->select();
  22. $lists = $lists?$lists:[];
  23. foreach ($lists as $k=>$v){
  24. $record = Db::name('attendance_record')
  25. ->where('user_class_id',$v['id'])
  26. ->order('create_time asc')
  27. ->select();
  28. $record = $record?$record:[];
  29. $lack = 0;
  30. $early = 0;
  31. $late = 0;
  32. foreach ($record as $kk=>$vv){
  33. if($vv['effective'] == 1&&$vv['status'] == 1){
  34. $late++;
  35. }else if($vv['effective'] == 1&&$vv['status'] == 2){
  36. $early++;
  37. }
  38. }
  39. $dates = json_decode($v['content'],true);
  40. foreach ($dates as $kk=>$vv){
  41. $snr = $enr = [];
  42. if($record){
  43. $f1 = 0;
  44. $f2 = 0;
  45. foreach ($record as $kkk=>$vvv){
  46. if($vvv['kq_time'] == $vv['stime']){
  47. $snr[] = $vvv;
  48. $f1 = 1;
  49. }
  50. if($vvv['kq_time'] == $vv['etime']){
  51. $enr[] = $vvv;
  52. $f2 = 1;
  53. }
  54. }
  55. if(!$f1){
  56. $lack++;
  57. }
  58. if(!$f2){
  59. $lack++;
  60. }
  61. }else{
  62. $lack += 2;
  63. }
  64. $dates[$kk]['slist'] = $snr;
  65. $dates[$kk]['elist'] = $enr;
  66. }
  67. $lists[$k]['content'] = $dates;
  68. $lists[$k]['lack'] = $lack;
  69. $lists[$k]['early'] = $early;
  70. $lists[$k]['late'] = $late;
  71. //获取今日请假记录
  72. $leave = Db::name('attendance_leave')
  73. ->where('status',1)
  74. ->where('user_id',$v['user_id'])
  75. ->where('start_time','<=',$v['day'].' 23:59:59')
  76. ->where('end_time','>=',$v['day'].' 00:00:00')
  77. ->select();
  78. $leave = $leave?$leave:[];
  79. foreach ($leave as $key=>$val){
  80. $leave['leave_type'] = Db::name('attendance_leave_type')->where('id',$val['id'])->value('name');
  81. }
  82. $lists[$k]['leave'] = $leave?$leave:[];
  83. }
  84. return $lists;
  85. }
  86. public function situationByMonth_old($month,$staffId,$orgId){
  87. $map[] = ['ar.user_id','=',$staffId];
  88. $map[] = ['ar.org_id','=',$orgId];
  89. $date = $month.'-01';
  90. $timestamp = strtotime( $date );
  91. $start_time = date( 'Y-m-1', $timestamp );
  92. $mdays = date( 't', $timestamp );
  93. $end_time = date( 'Y-m-' . $mdays, $timestamp );
  94. $map[] = ['day','>=',$start_time];
  95. $map[] = ['day','<=',$end_time];
  96. $lists = Db::name('attendance_record_result')
  97. ->alias('ar')
  98. ->join('user_info ui','ui.user_id = ar.user_id')
  99. ->join('attendance_class ac','ac.id = ar.class_id')
  100. ->where($map)
  101. ->field('ar.*,ui.name as user_name,ac.stime as start_time,ac.etime as end_time,ar.work as isWork')
  102. ->order('ar.id desc')
  103. ->select();
  104. $lists = $lists?$lists:[];
  105. foreach ($lists as $k=>$v){
  106. $record = Db::name('attendance_record')
  107. ->where('user_id',$staffId)
  108. ->where('create_time','>=',$v['day'].' 00:00:00')
  109. ->where('create_time','<=',$v['day'].' 23:59:59')
  110. ->field('type as record_type,create_time as record_time')
  111. ->select();
  112. $lists[$k]['recordDetail'] = $record?$record:[];
  113. $leave = Db::name('attendance_leave')
  114. ->where('user_id',$staffId)
  115. ->where('start_time','<=',$v['day'].' 23:59:59')
  116. ->where('end_time','>=',$v['day'].' 00:00:00')
  117. ->field('days as leave_days,leave_type,start_time as leave_start_time,end_time as leave_end_time')
  118. ->select();
  119. $lists[$k]['leaveDetail'] = $leave?$leave:[];
  120. }
  121. return $lists;
  122. }
  123. public function attendanceStatistics($month,$page,$size,$orgId,$userName){
  124. $date = $month.'-01';
  125. $timestamp = strtotime( $date );
  126. $start_time = date( 'Y-m-d', $timestamp );
  127. $mdays = date( 't', $timestamp );
  128. $end_time = date( 'Y-m-' . $mdays, $timestamp );
  129. if($userName !=''){
  130. $userIds = Db::name('user_info')->where('name','like','%'.$userName.'%')->column('user_id');
  131. $map[] = ['u.id','in',$userIds];
  132. }
  133. $map[] = ['u.del','=',0];
  134. $lists = Db::name('attendance_user_class')
  135. ->alias('a')
  136. ->join('user u','u.id = a.user_id')
  137. ->where($map)
  138. ->where('a.org_id',$orgId)
  139. ->where('a.day','>=',$start_time)
  140. ->where('a.day','<=',$end_time)
  141. ->field('a.user_id,count(*) as days')
  142. ->group('a.user_id')
  143. ->distinct(true)
  144. ->order('a.user_id')
  145. ->page($page,$size)
  146. ->select();
  147. $lists = $lists?$lists:[];
  148. foreach ($lists as $k=>$v){
  149. $duration = 0;
  150. $uclist = Db::name('attendance_user_class')
  151. ->where('org_id',$orgId)
  152. ->where('day','>=',$start_time)
  153. ->where('day','<=',$end_time)
  154. ->where('user_id',$v['user_id'])
  155. ->field('content')
  156. ->select();
  157. foreach ($uclist as $key=>$val){
  158. $dates = json_decode($val['content'],true);
  159. foreach ($dates as $kk=>$vv){
  160. if(!empty($vv['ssgin']) && !empty($vv['esign'])){
  161. $duration += strtotime($vv['esign']) - strtotime($vv['ssgin']);
  162. }
  163. }
  164. }
  165. $lists[$k]['duration'] = round($duration/60);
  166. $lists[$k]['user_name'] = Db::name('user_info')->where('user_id',$v['user_id'])->value('name');
  167. $work = Db::name('attendance_user_class')
  168. ->alias('auc')
  169. ->join('attendance_record ar','ar.user_class_id = auc.id','left')
  170. ->where('auc.org_id',$orgId)
  171. ->where('auc.day','>=',$start_time)
  172. ->where('auc.day','<=',$end_time)
  173. ->where('auc.user_id',$v['user_id'])
  174. ->where('ar.user_id',$v['user_id'])
  175. ->group('auc.day')
  176. ->count();
  177. // $work = Db::name('attendance_record')
  178. // ->alias('ar')
  179. // ->join('attendance_user_class auc','ar.user_class_id = auc.id')
  180. // ->where('auc.org_id',$orgId)
  181. // ->where('auc.day','>=',$start_time)
  182. // ->where('auc.day','<=',$end_time)
  183. // ->where('ar.user_id',$v['user_id'])
  184. // ->group('auc.day')
  185. // ->distinct(true)
  186. // ->count();
  187. $lists[$k]['work'] = $work; // 出勤天数
  188. $bk = Db::name('attendance_record')
  189. ->alias('ar')
  190. ->join('attendance_user_class auc','ar.user_class_id = auc.id')
  191. ->where('auc.org_id',$orgId)
  192. ->where('auc.day','>=',$start_time)
  193. ->where('auc.day','<=',$end_time)
  194. ->where('ar.type',2)
  195. ->where('ar.user_id',$v['user_id'])
  196. ->count();
  197. $lists[$k]['bk'] = $bk; // 补卡次数
  198. $late = Db::name('attendance_record')
  199. ->alias('ar')
  200. ->join('attendance_user_class auc','ar.user_class_id = auc.id')
  201. ->where('auc.org_id',$orgId)
  202. ->where('auc.day','>=',$start_time)
  203. ->where('auc.day','<=',$end_time)
  204. ->where('ar.status',1)
  205. ->where('ar.effective',1)
  206. ->where('ar.user_id',$v['user_id'])
  207. ->count();
  208. $lists[$k]['late'] = $late; // 迟到次数
  209. $lated = Db::name('attendance_record')
  210. ->alias('ar')
  211. ->join('attendance_user_class auc','ar.user_class_id = auc.id')
  212. ->where('auc.org_id',$orgId)
  213. ->where('auc.day','>=',$start_time)
  214. ->where('auc.day','<=',$end_time)
  215. ->where('ar.status',1)
  216. ->where('ar.effective',1)
  217. ->where('ar.user_id',$v['user_id'])
  218. ->sum('ar.duration');
  219. $lists[$k]['late_duration'] = $lated?round($lated/60):0; // 迟到时长
  220. $early = Db::name('attendance_record')
  221. ->alias('ar')
  222. ->join('attendance_user_class auc','ar.user_class_id = auc.id')
  223. ->where('auc.org_id',$orgId)
  224. ->where('auc.day','>=',$start_time)
  225. ->where('auc.day','<=',$end_time)
  226. ->where('ar.status',2)
  227. ->where('ar.effective',1)
  228. ->where('ar.user_id',$v['user_id'])
  229. ->count();
  230. $lists[$k]['early'] = $early; // 早退次数
  231. $earlyd = Db::name('attendance_record')
  232. ->alias('ar')
  233. ->join('attendance_user_class auc','ar.user_class_id = auc.id')
  234. ->where('auc.org_id',$orgId)
  235. ->where('auc.day','>=',$start_time)
  236. ->where('auc.day','<=',$end_time)
  237. ->where('ar.status',2)
  238. ->where('ar.effective',1)
  239. ->where('ar.user_id',$v['user_id'])
  240. ->sum('ar.duration');
  241. $lists[$k]['early_duration'] = $earlyd?round($earlyd/60):0; // 早退时长
  242. // 请假天数
  243. $leave = Db::name('attendance_leave')
  244. ->where('user_id',$v['user_id'])
  245. ->where('status',1)
  246. ->where('start_time','<=',$end_time.' 23:59:59')
  247. ->where('end_time','>=',$start_time.' 00:00:00')
  248. ->sum('cur_days');
  249. $lists[$k]['leave'] = $leave; // 请假天数
  250. // 获取用户岗位
  251. $jlist = Db::name('user_job')
  252. ->alias('uj')
  253. ->join('job j','j.id = uj.job_id')
  254. ->join('dep d','d.id = j.dep_id')
  255. ->where('uj.user_id',$v['user_id'])
  256. ->field("d.name as dep,j.name as job,d.parent_id")
  257. ->select();
  258. $jlist = $jlist?$jlist:[];
  259. $deps = $jobs = [];
  260. $post = [];
  261. foreach ($jlist as $kk=>$vv){
  262. $subName = model('Dep')->getDepParent($vv['parent_id']);
  263. // $depname = $vv['dep'].'/'.$vv['job'];
  264. $depname = $vv['dep'];
  265. $jobs[] = $subName?$subName.'/'.$depname:$depname;
  266. $deps[] = $vv['dep'];
  267. // $jobs[] = $vv['job'];
  268. $post[] = $vv['job'];
  269. }
  270. $lists[$k]['dep'] = $deps?implode(',',$deps):'';
  271. $lists[$k]['job_name'] = $jobs?implode(',',$jobs):'';
  272. // $lists[$k]['post_name'] = $jobs?implode(',',$post):'';
  273. }
  274. // $total = Db::name('attendance_user_class')
  275. // ->where('org_id',$orgId)
  276. // ->where('day','>=',$start_time)
  277. // ->where('day','<=',$end_time)
  278. // ->group('user_id')
  279. // ->distinct(true)
  280. // ->order('user_id')
  281. // ->count();
  282. $total = Db::name('attendance_user_class')
  283. ->alias('a')
  284. ->join('user u','u.id = a.user_id')
  285. ->where('u.del',0)
  286. ->where('a.org_id',$orgId)
  287. ->where('a.day','>=',$start_time)
  288. ->where('a.day','<=',$end_time)
  289. ->field('a.user_id,count(*) as days')
  290. ->group('a.user_id')
  291. ->distinct(true)
  292. ->count();
  293. $data = [
  294. 'total' => $total,
  295. 'list' => $lists
  296. ];
  297. return $data;
  298. }
  299. public function wyattendanceStatistics($month,$page,$size,$userId = [],$userName){
  300. $date = $month.'-01';
  301. $timestamp = strtotime( $date );
  302. $start_time = date( 'Y-m-d', $timestamp );
  303. $mdays = date( 't', $timestamp );
  304. $end_time = date( 'Y-m-' . $mdays, $timestamp );
  305. if($userName !=''){
  306. $map[] = ['u.real_name','like','%'.$userName.'%'];
  307. }
  308. $map[] = ['u.del','=',0];
  309. $lists = Db::connect($this->connection)
  310. ->name('attendance_user_class')
  311. ->alias('a')
  312. ->join('user u','u.id = a.user_id')
  313. ->where($map)
  314. ->where('a.user_id','in',$userId)
  315. ->where('a.day','>=',$start_time)
  316. ->where('a.day','<=',$end_time)
  317. ->field('a.user_id,count(*) as days')
  318. ->group('a.user_id')
  319. ->distinct(true)
  320. ->order('a.user_id')
  321. ->page($page,$size)
  322. ->select();
  323. $lists = $lists?$lists:[];
  324. foreach ($lists as $k=>$v){
  325. $duration = 0;
  326. $uclist = Db::connect($this->connection)
  327. ->name('attendance_user_class')
  328. ->where('day','>=',$start_time)
  329. ->where('day','<=',$end_time)
  330. ->where('user_id',$v['user_id'])
  331. ->field('content')
  332. ->select();
  333. foreach ($uclist as $key=>$val){
  334. $dates = json_decode($val['content'],true);
  335. foreach ($dates as $kk=>$vv){
  336. if(!empty($vv['ssgin']) && !empty($vv['esign'])){
  337. $duration += strtotime($vv['esign']) - strtotime($vv['ssgin']);
  338. }
  339. }
  340. }
  341. $lists[$k]['duration'] = round($duration/60);
  342. $uid = Db::name('user_wyorg')
  343. ->where('wyuser_id',$v['user_id'])
  344. ->find();
  345. $lists[$k]['user_name'] = Db::name('user_info')
  346. ->where('user_id',$uid['user_id'])->value('name');
  347. $work = Db::connect($this->connection)
  348. ->name('attendance_user_class')
  349. ->alias('auc')
  350. ->join('attendance_record ar','ar.user_class_id = auc.id','left')
  351. ->where('auc.day','>=',$start_time)
  352. ->where('auc.day','<=',$end_time)
  353. ->where('auc.user_id',$v['user_id'])
  354. ->where('ar.user_id',$v['user_id'])
  355. ->group('auc.day')
  356. ->count();
  357. // $work = Db::name('attendance_record')
  358. // ->alias('ar')
  359. // ->join('attendance_user_class auc','ar.user_class_id = auc.id')
  360. // ->where('auc.org_id',$orgId)
  361. // ->where('auc.day','>=',$start_time)
  362. // ->where('auc.day','<=',$end_time)
  363. // ->where('ar.user_id',$v['user_id'])
  364. // ->group('auc.day')
  365. // ->distinct(true)
  366. // ->count();
  367. $lists[$k]['work'] = $work; // 出勤天数
  368. $bk = Db::connect($this->connection)
  369. ->name('attendance_record')
  370. ->alias('ar')
  371. ->join('attendance_user_class auc','ar.user_class_id = auc.id')
  372. ->where('auc.day','>=',$start_time)
  373. ->where('auc.day','<=',$end_time)
  374. ->where('ar.type',2)
  375. ->where('ar.user_id',$v['user_id'])
  376. ->count();
  377. $lists[$k]['bk'] = $bk; // 补卡次数
  378. $late = Db::connect($this->connection)
  379. ->name('attendance_record')
  380. ->alias('ar')
  381. ->join('attendance_user_class auc','ar.user_class_id = auc.id')
  382. ->where('auc.day','>=',$start_time)
  383. ->where('auc.day','<=',$end_time)
  384. ->where('ar.status',1)
  385. ->where('ar.effective',1)
  386. ->where('ar.user_id',$v['user_id'])
  387. ->count();
  388. $lists[$k]['late'] = $late; // 迟到次数
  389. $lated = Db::connect($this->connection)
  390. ->name('attendance_record')
  391. ->alias('ar')
  392. ->join('attendance_user_class auc','ar.user_class_id = auc.id')
  393. ->where('auc.day','>=',$start_time)
  394. ->where('auc.day','<=',$end_time)
  395. ->where('ar.status',1)
  396. ->where('ar.effective',1)
  397. ->where('ar.user_id',$v['user_id'])
  398. ->sum('ar.duration');
  399. $lists[$k]['late_duration'] = $lated?round($lated/60):0; // 迟到时长
  400. $early = Db::connect($this->connection)
  401. ->name('attendance_record')
  402. ->alias('ar')
  403. ->join('attendance_user_class auc','ar.user_class_id = auc.id')
  404. ->where('auc.day','>=',$start_time)
  405. ->where('auc.day','<=',$end_time)
  406. ->where('ar.status',2)
  407. ->where('ar.effective',1)
  408. ->where('ar.user_id',$v['user_id'])
  409. ->count();
  410. $lists[$k]['early'] = $early; // 早退次数
  411. $earlyd = Db::connect($this->connection)
  412. ->name('attendance_record')
  413. ->alias('ar')
  414. ->join('attendance_user_class auc','ar.user_class_id = auc.id')
  415. ->where('auc.day','>=',$start_time)
  416. ->where('auc.day','<=',$end_time)
  417. ->where('ar.status',2)
  418. ->where('ar.effective',1)
  419. ->where('ar.user_id',$v['user_id'])
  420. ->sum('ar.duration');
  421. $lists[$k]['early_duration'] = $earlyd?round($earlyd/60):0; // 早退时长
  422. // 请假天数
  423. $leave = Db::connect($this->connection)
  424. ->name('attendance_leave')
  425. ->where('user_id',$v['user_id'])
  426. ->where('status',1)
  427. ->where('start_time','<=',$end_time.' 23:59:59')
  428. ->where('end_time','>=',$start_time.' 00:00:00')
  429. ->sum('cur_days');
  430. $lists[$k]['leave'] = $leave; // 请假天数
  431. $dep = Db::connect($this->connection)
  432. ->name('dep')
  433. ->alias('o')
  434. ->field('o.title')
  435. ->join('user_dep uo','uo.dep_id=o.id')
  436. ->where('uo.user_id',$v['user_id'])
  437. // ->where('uo.ORG_TYPE',1)
  438. ->find();
  439. $lists[$k]['dep'] = $dep?$dep['title']:'';
  440. }
  441. // $total = Db::connect($this->connection)
  442. // ->name('attendance_user_class')
  443. // ->where('day','>=',$start_time)
  444. // ->where('day','<=',$end_time)
  445. // ->where('user_id','in',$userId)
  446. // ->group('user_id')
  447. // ->distinct(true)
  448. // ->order('user_id')
  449. // ->count();
  450. $total = Db::connect($this->connection)
  451. ->name('attendance_user_class')
  452. ->alias('a')
  453. ->join('user u','u.id = a.user_id')
  454. ->where('u.del',0)
  455. ->where('a.user_id','in',$userId)
  456. ->where('a.day','>=',$start_time)
  457. ->where('a.day','<=',$end_time)
  458. ->field('a.user_id,count(*) as days')
  459. ->group('a.user_id')
  460. ->distinct(true)
  461. ->count();
  462. $data = [
  463. 'total' => $total,
  464. 'list' => $lists
  465. ];
  466. return $data;
  467. }
  468. public function wyattendanceExport($month,$userId = [],$userName){
  469. $date = $month.'-01';
  470. $timestamp = strtotime( $date );
  471. $start_time = date( 'Y-m-d', $timestamp );
  472. $mdays = date( 't', $timestamp );
  473. $end_time = date( 'Y-m-' . $mdays, $timestamp );
  474. if($userName !=''){
  475. $map[] = ['u.real_name','like','%'.$userName.'%'];
  476. }
  477. $map[] = ['u.del','=',0];
  478. $lists =Db::connect($this->connection)
  479. ->name('attendance_user_class')
  480. ->alias('a')
  481. ->join('user u','u.id = a.user_id')
  482. ->where($map)
  483. ->where('a.user_id','in',$userId)
  484. ->where('a.day','>=',$start_time)
  485. ->where('a.day','<=',$end_time)
  486. ->field('a.user_id,count(*) as days')
  487. ->group('a.user_id')
  488. ->distinct(true)
  489. ->order('a.user_id')
  490. ->select();
  491. $lists = $lists?$lists:[];
  492. foreach ($lists as $k=>$v){
  493. $duration = 0;
  494. $uclist = Db::connect($this->connection)
  495. ->name('attendance_user_class')
  496. ->where('day','>=',$start_time)
  497. ->where('day','<=',$end_time)
  498. ->where('user_id',$v['user_id'])
  499. ->field('content')
  500. ->select();
  501. foreach ($uclist as $key=>$val){
  502. $dates = json_decode($val['content'],true);
  503. foreach ($dates as $kk=>$vv){
  504. if(!empty($vv['ssgin']) && !empty($vv['esign'])){
  505. $duration += strtotime($vv['esign']) - strtotime($vv['ssgin']);
  506. }
  507. }
  508. }
  509. $lists[$k]['duration'] = round($duration/60);
  510. $lists[$k]['user_name'] = Db::connect($this->connection)
  511. ->name('user')->where('id',$v['user_id'])->value('real_name');
  512. $work = Db::connect($this->connection)
  513. ->name('attendance_user_class')
  514. ->alias('auc')
  515. ->join('attendance_record ar','ar.user_class_id = auc.id','left')
  516. ->where('auc.day','>=',$start_time)
  517. ->where('auc.day','<=',$end_time)
  518. ->where('auc.user_id',$v['user_id'])
  519. ->where('ar.user_id',$v['user_id'])
  520. ->group('auc.day')
  521. ->count();
  522. // $work = Db::name('attendance_record')
  523. // ->alias('ar')
  524. // ->join('attendance_user_class auc','ar.user_class_id = auc.id')
  525. // ->where('auc.org_id',$orgId)
  526. // ->where('auc.day','>=',$start_time)
  527. // ->where('auc.day','<=',$end_time)
  528. // ->where('ar.user_id',$v['user_id'])
  529. // ->group('auc.day')
  530. // ->distinct(true)
  531. // ->count();
  532. $lists[$k]['work'] = $work; // 出勤天数
  533. $bk = Db::connect($this->connection)
  534. ->name('attendance_record')
  535. ->alias('ar')
  536. ->join('attendance_user_class auc','ar.user_class_id = auc.id')
  537. ->where('auc.day','>=',$start_time)
  538. ->where('auc.day','<=',$end_time)
  539. ->where('ar.type',2)
  540. ->where('ar.user_id',$v['user_id'])
  541. ->count();
  542. $lists[$k]['bk'] = $bk; // 补卡次数
  543. $late = Db::connect($this->connection)
  544. ->name('attendance_record')
  545. ->alias('ar')
  546. ->join('attendance_user_class auc','ar.user_class_id = auc.id')
  547. ->where('auc.day','>=',$start_time)
  548. ->where('auc.day','<=',$end_time)
  549. ->where('ar.status',1)
  550. ->where('ar.effective',1)
  551. ->where('ar.user_id',$v['user_id'])
  552. ->count();
  553. $lists[$k]['late'] = $late; // 迟到次数
  554. $lated = Db::connect($this->connection)
  555. ->name('attendance_record')
  556. ->alias('ar')
  557. ->join('attendance_user_class auc','ar.user_class_id = auc.id')
  558. ->where('auc.day','>=',$start_time)
  559. ->where('auc.day','<=',$end_time)
  560. ->where('ar.status',1)
  561. ->where('ar.effective',1)
  562. ->where('ar.user_id',$v['user_id'])
  563. ->sum('ar.duration');
  564. $lists[$k]['late_duration'] = $lated?round($lated/60):0; // 迟到时长
  565. $early = Db::connect($this->connection)
  566. ->name('attendance_record')
  567. ->alias('ar')
  568. ->join('attendance_user_class auc','ar.user_class_id = auc.id')
  569. ->where('auc.day','>=',$start_time)
  570. ->where('auc.day','<=',$end_time)
  571. ->where('ar.status',2)
  572. ->where('ar.effective',1)
  573. ->where('ar.user_id',$v['user_id'])
  574. ->count();
  575. $lists[$k]['early'] = $early; // 早退次数
  576. $earlyd = Db::connect($this->connection)
  577. ->name('attendance_record')
  578. ->alias('ar')
  579. ->join('attendance_user_class auc','ar.user_class_id = auc.id')
  580. ->where('auc.day','>=',$start_time)
  581. ->where('auc.day','<=',$end_time)
  582. ->where('ar.status',2)
  583. ->where('ar.effective',1)
  584. ->where('ar.user_id',$v['user_id'])
  585. ->sum('ar.duration');
  586. $lists[$k]['early_duration'] = $earlyd?round($earlyd/60):0; // 早退时长
  587. // 请假天数
  588. $leave = Db::connect($this->connection)
  589. ->name('attendance_leave')
  590. ->where('user_id',$v['user_id'])
  591. ->where('status',1)
  592. ->where('start_time','<=',$end_time.' 23:59:59')
  593. ->where('end_time','>=',$start_time.' 00:00:00')
  594. ->sum('cur_days');
  595. $lists[$k]['leave'] = $leave; // 请假天数
  596. $dep = Db::connect($this->connection)
  597. ->name('dep')
  598. ->alias('o')
  599. ->field('o.title')
  600. ->join('user_dep uo','uo.dep_id=o.id')
  601. ->where('uo.user_id',$v['user_id'])
  602. // ->where('uo.ORG_TYPE',1)
  603. ->find();
  604. $lists[$k]['dep'] = $dep?$dep['title']:'';
  605. }
  606. $columns = [
  607. ["title" => "姓名","key" => "userName"],
  608. ["title" => "部门","key" => "dep"],
  609. ["title" => "应出勤天数","key" => "days"],
  610. ["title" => "出勤天数","key" => "work"],
  611. ["title" => "工作时长","key" => "duration"],
  612. ["title" => "迟到次数","key" => "late"],
  613. ["title" => "迟到时长","key" => "lateDuration"],
  614. ["title" => "早退次数","key" => "early"],
  615. ["title" => "早退时长","key" => "earlyDuration"],
  616. ["title" => "补卡次数","key" => "bk"],
  617. ["title" => "请假","key" => "leave"],
  618. ];
  619. $data = [
  620. 'columns' => $columns,
  621. 'list' => $lists
  622. ];
  623. return $data;
  624. }
  625. public function attendanceExport($month,$orgId,$userName){
  626. $date = $month.'-01';
  627. $timestamp = strtotime( $date );
  628. $start_time = date( 'Y-m-d', $timestamp );
  629. $mdays = date( 't', $timestamp );
  630. $end_time = date( 'Y-m-' . $mdays, $timestamp );
  631. if($userName !=''){
  632. $userIds = Db::name('user_info')->where('name','like','%'.$userName.'%')->column('user_id');
  633. $map[] = ['u.id','in',$userIds];
  634. }
  635. $map[] = ['u.del','=',0];
  636. $lists = Db::name('attendance_user_class')
  637. ->alias('a')
  638. ->join('user u','u.id = a.user_id')
  639. ->where($map)
  640. ->where('a.org_id',$orgId)
  641. ->where('a.day','>=',$start_time)
  642. ->where('a.day','<=',$end_time)
  643. ->field('a.user_id,count(*) as days')
  644. ->group('a.user_id')
  645. ->distinct(true)
  646. ->order('a.user_id')
  647. ->select();
  648. $lists = $lists?$lists:[];
  649. foreach ($lists as $k=>$v){
  650. $duration = 0;
  651. $uclist = Db::name('attendance_user_class')
  652. ->where('org_id',$orgId)
  653. ->where('day','>=',$start_time)
  654. ->where('day','<=',$end_time)
  655. ->where('user_id',$v['user_id'])
  656. ->field('content')
  657. ->select();
  658. foreach ($uclist as $key=>$val){
  659. $dates = json_decode($val['content'],true);
  660. foreach ($dates as $kk=>$vv){
  661. if(!empty($vv['ssgin']) && !empty($vv['esign'])){
  662. $duration += strtotime($vv['esign']) - strtotime($vv['ssgin']);
  663. }
  664. }
  665. }
  666. $lists[$k]['duration'] = round($duration/60);
  667. $lists[$k]['user_name'] = Db::name('user_info')->where('user_id',$v['user_id'])->value('name');
  668. $work = Db::name('attendance_user_class')
  669. ->alias('auc')
  670. ->join('attendance_record ar','ar.user_class_id = auc.id','left')
  671. ->where('auc.org_id',$orgId)
  672. ->where('auc.day','>=',$start_time)
  673. ->where('auc.day','<=',$end_time)
  674. ->where('auc.user_id',$v['user_id'])
  675. ->where('ar.user_id',$v['user_id'])
  676. ->group('auc.day')
  677. ->count();
  678. // $work = Db::name('attendance_record')
  679. // ->alias('ar')
  680. // ->join('attendance_user_class auc','ar.user_class_id = auc.id')
  681. // ->where('auc.org_id',$orgId)
  682. // ->where('auc.day','>=',$start_time)
  683. // ->where('auc.day','<=',$end_time)
  684. // ->where('ar.user_id',$v['user_id'])
  685. // ->group('auc.day')
  686. // ->distinct(true)
  687. // ->count();
  688. $lists[$k]['work'] = $work; // 出勤天数
  689. $bk = Db::name('attendance_record')
  690. ->alias('ar')
  691. ->join('attendance_user_class auc','ar.user_class_id = auc.id')
  692. ->where('auc.org_id',$orgId)
  693. ->where('auc.day','>=',$start_time)
  694. ->where('auc.day','<=',$end_time)
  695. ->where('ar.type',2)
  696. ->where('ar.user_id',$v['user_id'])
  697. ->count();
  698. $lists[$k]['bk'] = $bk; // 补卡次数
  699. $late = Db::name('attendance_record')
  700. ->alias('ar')
  701. ->join('attendance_user_class auc','ar.user_class_id = auc.id')
  702. ->where('auc.org_id',$orgId)
  703. ->where('auc.day','>=',$start_time)
  704. ->where('auc.day','<=',$end_time)
  705. ->where('ar.status',1)
  706. ->where('ar.effective',1)
  707. ->where('ar.user_id',$v['user_id'])
  708. ->count();
  709. $lists[$k]['late'] = $late; // 迟到次数
  710. $lated = Db::name('attendance_record')
  711. ->alias('ar')
  712. ->join('attendance_user_class auc','ar.user_class_id = auc.id')
  713. ->where('auc.org_id',$orgId)
  714. ->where('auc.day','>=',$start_time)
  715. ->where('auc.day','<=',$end_time)
  716. ->where('ar.status',1)
  717. ->where('ar.effective',1)
  718. ->where('ar.user_id',$v['user_id'])
  719. ->sum('ar.duration');
  720. $lists[$k]['late_duration'] = $lated?round($lated/60):0; // 迟到时长
  721. $early = Db::name('attendance_record')
  722. ->alias('ar')
  723. ->join('attendance_user_class auc','ar.user_class_id = auc.id')
  724. ->where('auc.org_id',$orgId)
  725. ->where('auc.day','>=',$start_time)
  726. ->where('auc.day','<=',$end_time)
  727. ->where('ar.status',2)
  728. ->where('ar.effective',1)
  729. ->where('ar.user_id',$v['user_id'])
  730. ->count();
  731. $lists[$k]['early'] = $early; // 早退次数
  732. $earlyd = Db::name('attendance_record')
  733. ->alias('ar')
  734. ->join('attendance_user_class auc','ar.user_class_id = auc.id')
  735. ->where('auc.org_id',$orgId)
  736. ->where('auc.day','>=',$start_time)
  737. ->where('auc.day','<=',$end_time)
  738. ->where('ar.status',2)
  739. ->where('ar.effective',1)
  740. ->where('ar.user_id',$v['user_id'])
  741. ->sum('ar.duration');
  742. $lists[$k]['early_duration'] = $earlyd?round($earlyd/60):0; // 早退时长
  743. // 请假天数
  744. $leave = Db::name('attendance_leave')
  745. ->where('user_id',$v['user_id'])
  746. ->where('status',1)
  747. ->where('start_time','<=',$end_time.' 23:59:59')
  748. ->where('end_time','>=',$start_time.' 00:00:00')
  749. ->sum('cur_days');
  750. $lists[$k]['leave'] = $leave; // 请假天数
  751. // 获取用户岗位
  752. $jlist = Db::name('user_job')
  753. ->alias('uj')
  754. ->join('job j','j.id = uj.job_id')
  755. ->join('dep d','d.id = j.dep_id')
  756. ->where('uj.user_id',$v['user_id'])
  757. ->field("d.name as dep,j.name as job,d.parent_id")
  758. ->select();
  759. $jlist = $jlist?$jlist:[];
  760. $deps = $jobs = [];
  761. $post = [];
  762. foreach ($jlist as $kk=>$vv){
  763. $subName = model('Dep')->getDepParent($vv['parent_id']);
  764. // $depname = $vv['dep'].'/'.$vv['job'];
  765. $depname = $vv['dep'];
  766. $jobs[] = $subName?$subName.'/'.$depname:$depname;
  767. $deps[] = $vv['dep'];
  768. // $jobs[] = $vv['job'];
  769. $post[] = $vv['job'];
  770. }
  771. $lists[$k]['dep'] = $deps?implode(',',$deps):'';
  772. $lists[$k]['job_name'] = $jobs?implode(',',$jobs):'';
  773. // $lists[$k]['post_name'] = $jobs?implode(',',$post):'';
  774. }
  775. $columns = [
  776. ["title" => "姓名","key" => "userName"],
  777. ["title" => "岗位","key" => "jobName"],
  778. ["title" => "部门","key" => "dep"],
  779. ["title" => "应出勤天数","key" => "days"],
  780. ["title" => "出勤天数","key" => "work"],
  781. ["title" => "工作时长","key" => "duration"],
  782. ["title" => "迟到次数","key" => "late"],
  783. ["title" => "迟到时长","key" => "lateDuration"],
  784. ["title" => "早退次数","key" => "early"],
  785. ["title" => "早退时长","key" => "earlyDuration"],
  786. ["title" => "补卡次数","key" => "bk"],
  787. ["title" => "请假","key" => "leave"],
  788. ];
  789. $data = [
  790. 'columns' => $columns,
  791. 'list' => $lists
  792. ];
  793. return $data;
  794. }
  795. }