AttendanceGroup.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 AttendanceGroup extends Model
  8. {
  9. public function add(){
  10. $data = [
  11. 'id' => input('id/d',0),
  12. 'name' => input('name','','trim'),
  13. 'type' => input('type/d',0),
  14. 'cate' => input('cate','','trim'),
  15. 'addr' => input('addr','','trim'),
  16. 'content' => input('content','','trim'),
  17. 'class_id' => input('classId','','trim'),
  18. 'org_id' => input('orgId/d',0),
  19. 'users' => input('users','','trim')
  20. ];
  21. $result = validate('AttendanceGroup')->check($data,[],'');
  22. if(true !== $result){
  23. HelpHander::error(validate('AttendanceGroup')->getError());
  24. }
  25. $id = $data['id'];
  26. unset($data['id']);
  27. $users = $data['users'];
  28. Db::startTrans();
  29. try{
  30. $groupId = 0;
  31. if($id > 0){
  32. $groupId = $id;
  33. $data['update_time'] = date('Y-m-d H:i:s');
  34. $ret = $this->allowField(true)->save($data,['id'=>$id]);
  35. }else{
  36. $data['create_time'] = date('Y-m-d H:i:s');
  37. $ret = $this->allowField(true)->save($data);
  38. $groupId = $this->id;
  39. }
  40. if(!$ret){
  41. \exception('操作失败');
  42. }
  43. // 更新考勤组用户
  44. $users = json_decode($users,true);
  45. $userids = [];
  46. $jobs = [];
  47. foreach ($users as $k=>$v){
  48. if($v['type'] == 2){
  49. $userids[] = $v['id'];
  50. }else{
  51. $jres = model('Dep')->getJobByDep($v['id']);
  52. $jobs = array_merge($jobs,$jres);
  53. }
  54. }
  55. if($jobs){
  56. $userids2 = Db::name('user_job')->where('job_id','in',$jobs)->column('user_id');
  57. if($userids2){
  58. $userids = array_unique(array_merge($userids,$userids2));
  59. }
  60. }
  61. if($userids){
  62. // 删除已在其他组的人
  63. Db::name('attendance_group_user')->where('user_id','in',$userids)->delete();
  64. // 删除本组的其他人
  65. Db::name('attendance_group_user')->where('group_id',$groupId)->where('user_id','not in',$userids)->delete();
  66. // TODO::删除人员分组
  67. $arr = [];
  68. foreach ($userids as $k=>$v){
  69. $arr[] = [
  70. 'org_id' => $data['org_id'],
  71. 'group_id' => $groupId,
  72. 'user_id' => $v
  73. ];
  74. if(count($arr) == 50){
  75. $count = Db::name('attendance_group_user')->insertAll($arr);
  76. if($count != count($arr)){
  77. \exception('保存失败');
  78. }else{
  79. $arr = [];
  80. }
  81. }
  82. }
  83. if(count($arr) > 0){
  84. $count = Db::name('attendance_group_user')->insertAll($arr);
  85. if($count != count($arr)){
  86. \exception('保存失败');
  87. }
  88. }
  89. }else{ // 为空删除组的所有人
  90. // 删除本组的人
  91. Db::name('attendance_group_user')->where('group_id',$groupId)->delete();
  92. }
  93. Db::commit();
  94. }catch (Exception $e){
  95. Db::rollback();
  96. HelpHander::error($e->getMessage());
  97. }
  98. return true;
  99. }
  100. public function info($id){
  101. $info = $this->where('id',$id)->find();
  102. if(!$info){
  103. HelpHander::error('数据不存在');
  104. }
  105. $data = $info->toArray();
  106. $data['content'] = $data?json_decode($data['content'],true):'';
  107. if($data['type'] == 1 && $data['content']){
  108. foreach ($data['content']['week'] as $k=>$v){
  109. if(!empty($v['class_id'])){
  110. $data['content']['week'][$k]['class_name'] = model('AttendanceClass')->getNameById($v['class_id']);
  111. }
  112. }
  113. foreach ($data['content']['sign'] as $k=>$v){
  114. if(!empty($v['class_id'])){
  115. $data['content']['sign'][$k]['class_name'] = model('AttendanceClass')->getNameById($v['class_id']);
  116. }
  117. }
  118. }else if($data['type'] == 2 && $data['content'] && isset($data['content']['circle'])){
  119. foreach ($data['content']['circle']['class'] as $k=>$v){
  120. if(!empty($v['class_id'])){
  121. $data['content']['circle']['class'][$k]['name'] = Db::name('AttendanceClass')->where('id',$v['class_id'])->value('name');
  122. $data['content']['circle']['class'][$k]['tname'] = model('AttendanceClass')->getNameById($v['class_id']);
  123. }
  124. }
  125. }
  126. if($data['content']){
  127. $data['content'] = json_encode($data['content']);
  128. }
  129. $userids = Db::name('attendance_group_user')->where('group_id',$data['id'])->column('user_id');
  130. if($data['class_id']){
  131. $classList = Db::name('attendance_class')->where('id','in',$data['class_id'])->select();
  132. foreach ($classList as $k=>$v){
  133. $classList[$k]['tname'] = model('AttendanceClass')->formatClassName($v);
  134. }
  135. $data['class_list'] = $classList?$classList:[];
  136. }else{
  137. $data['class_list'] = [];
  138. }
  139. $data['userIds'] = $userids?$userids:[];
  140. return $data;
  141. }
  142. public function lists($page,$size,$name,$orgId){
  143. $map[] = ['del','=',0];
  144. $map[] = ['org_id','=',$orgId];
  145. if($name != ''){
  146. $map[] = ['name','like','%'.$name.'%'];
  147. }
  148. $lists = $this
  149. ->where($map)
  150. ->page($page,$size)
  151. ->order('id desc')
  152. ->select();
  153. $total = $this->where($map)->count();
  154. $data = [
  155. 'total' => $total,
  156. 'list' => $lists?$lists->toArray():[]
  157. ];
  158. return $data;
  159. }
  160. public function del($id){
  161. $ret = $this->where('id',$id)->setField('del',1);
  162. if(!$ret){
  163. HelpHander::error('删除失败');
  164. }
  165. return true;
  166. }
  167. }