AttendanceGroup.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. namespace app\common\model;
  3. use think\Db;
  4. use think\Exception;
  5. class AttendanceGroup extends Base{
  6. public function updates(){
  7. $data = request()->post();
  8. $data['org_id'] = cur_org_id();
  9. $result = validate('AttendanceClass')->check($data,[],'');
  10. if(true !== $result){
  11. $this->error = validate('AttendanceClass')->getError();
  12. return false;
  13. }
  14. $id = $data['id'];
  15. $users = $data['users'];
  16. $data['class_id'] = $data['classId'];
  17. unset($data['users'],$data['id'],$data['classId']);
  18. Db::startTrans();
  19. try {
  20. if($id > 0){
  21. $data['update_time'] = date('Y-m-d H:i:s');
  22. $this->allowField(true)->save($data,['id'=>$id]);
  23. $groupId = $id;
  24. }else{
  25. $data['create_time'] = date('Y-m-d H:i:s');
  26. $groupId = db('attendance_group')->insertGetId($data);
  27. }
  28. // 更新考勤组用户
  29. $userids = $users?explode(',',$users):[];
  30. if($userids){
  31. // 删除已在其他组的人
  32. Db::name('attendance_group_user')->whereIn('user_id',$userids)->delete();
  33. Db::name('attendance_group_user')->whereNotIn('user_id',$userids)->where('group_id',$groupId)->delete();
  34. // TODO::删除人员分组
  35. $arr = [];
  36. foreach ($userids as $k=>$v){
  37. $arr[] = [
  38. 'org_id' => $data['org_id'],
  39. 'group_id' => $groupId,
  40. 'user_id' => $v
  41. ];
  42. if(count($arr) == 50){
  43. $count = Db::name('attendance_group_user')->insertAll($arr);
  44. if($count != count($arr)){
  45. exception('操作失败');
  46. }else{
  47. $arr = [];
  48. }
  49. }
  50. }
  51. if(count($arr) > 0){
  52. $count = Db::name('attendance_group_user')->insertAll($arr);
  53. if($count != count($arr)){
  54. exception('操作失败');
  55. }
  56. }
  57. }else{
  58. $del = Db::name('attendance_group_user')->where('group_id',$groupId)->delete();
  59. if(!$del){
  60. exception('操作失败');
  61. }
  62. }
  63. Db::commit();
  64. return true;
  65. } catch (Exception $e) {
  66. $this->error = $e->getMessage();
  67. Db::rollback();
  68. return false;
  69. }
  70. }
  71. public function info($id){
  72. $info = Db::name('attendance_group')->where('id',$id)->find();
  73. if(!$info){
  74. return false;
  75. }
  76. $data = $info;
  77. $data['content'] = $data?json_decode($data['content'],true):'';
  78. if($data['type'] == 1 && $data['content']){
  79. foreach ($data['content']['week'] as $k=>$v){
  80. if(!empty($v['class_id'])){
  81. $data['content']['week'][$k]['class_name'] = $this->getNameById($v['class_id']);
  82. }
  83. }
  84. foreach ($data['content']['sign'] as $k=>$v){
  85. if(!empty($v['class_id'])){
  86. $data['content']['sign'][$k]['class_name'] = $this->getNameById($v['class_id']);
  87. }
  88. }
  89. }else if($data['type'] == 2 && $data['content'] && isset($data['content']['circle'])){
  90. foreach ($data['content']['circle']['class'] as $k=>$v){
  91. if(!empty($v['class_id'])){
  92. $data['content']['circle']['class'][$k]['name'] = $this->getNameById($v['class_id'],2);
  93. $data['content']['circle']['class'][$k]['tname'] = $this->getNameById($v['class_id']);
  94. }
  95. }
  96. }
  97. if($data['content']){
  98. $data['content'] = json_encode($data['content']);
  99. }
  100. $userids = [];
  101. $users = Db::name('attendance_group_user')->field('user_id')->where('group_id',$data['id'])->select();
  102. $users = $users?$users:[];
  103. foreach ($users as $k=>$v){
  104. $userids[] = $v['user_id'];
  105. }
  106. if($data['class_id']){
  107. $classList = Db('attendance_class') ->whereIn('id',$data['class_id'])->select();
  108. foreach ($classList as $k=>$v){
  109. $classList[$k]['tname'] = $this->formatClassName($v);
  110. }
  111. $data['class_list'] = $classList?$classList:[];
  112. }else{
  113. $data['class_list'] = [];
  114. }
  115. $data['userIds'] = $userids?$userids:[];
  116. return $data;
  117. }
  118. public function getNameById($id,$type=1){
  119. $info = Db::name('attendance_class')->where('id',$id)->find();
  120. if(!$info){
  121. return false;
  122. }
  123. return $type==1?$this->formatClassName($info):$info['name'];
  124. }
  125. public function formatClassName($info){
  126. $info['content'] = json_decode($info['content'],true);
  127. $names = [];
  128. foreach ($info['content']['dates'] as $k=>$v){
  129. $str = '';
  130. if ($v['snext'] == 1) {
  131. $str .= '次日'.$v['stime'];
  132. } else {
  133. $str .= $v['stime'];
  134. }
  135. $str .= '-';
  136. if ($v['enext'] === 1) {
  137. $str .= '次日'.$v['etime'];
  138. } else {
  139. $str .= $v['etime'];
  140. }
  141. $names[] = $str;
  142. }
  143. return $info['name'].' '.implode(',',$names);
  144. }
  145. public function showOne($id){
  146. $info = Db::name('attendance_group')->where('id',$id)->find();
  147. return $info;
  148. }
  149. public function groupUpdates(){
  150. $data = request()->post();
  151. $gid = $data['gid'];
  152. $info = $this->showOne($gid);
  153. if(!$info){
  154. $this->error = '记录不存在';
  155. return false;
  156. }
  157. $content = json_decode($info['content'],true);
  158. $circle = $content['circle'];
  159. if($data['type'] == 1){
  160. if($data['class_id'] == 0){
  161. Db::name('attendance_group_class')->where('group_id',$gid)->where('day',$data['day'])->delete();
  162. }else{
  163. $info = Db::name('attendance_group_class')->where('group_id',$gid)->where('day',$data['day'])->find();
  164. if($info){
  165. if($info['day'] == $data['day']){
  166. Db::name('attendance_group_class')->where('id',$info['id'])->update(['class_id'=>$data['class_id']]);
  167. }
  168. }else{
  169. Db::name('attendance_group_class')->where('id',$info['id'])->insert([
  170. 'org_id' => cur_org_id(),
  171. 'group_id' => $gid,
  172. 'class_id' => $data['class_id'],
  173. 'day' => $data['day']
  174. ]);
  175. }
  176. }
  177. }else{
  178. if(!$data['week']){
  179. Db::name('attendance_group_class')->where('group_id',$gid)->where('day','>=',$data['day'])->delete();
  180. }else{
  181. $class = $circle['class'];
  182. $curmonth = date('Y-m',strtotime($data['day']));
  183. $days = [$data['day']];
  184. $i = 1;
  185. while (true){
  186. $tt = strtotime($data['day']) + $i*24*60*60;
  187. $month = date('Y-m',$tt);
  188. if($month == $curmonth){
  189. $days[] = date('Y-m-d',$tt);
  190. $i++;
  191. }else{
  192. break;
  193. }
  194. }
  195. $i = 0;
  196. $max = $circle['days'] - 1;
  197. foreach ($days as $k=>$v){
  198. if($class[$i]['class_id'] == 0) {
  199. Db::name('attendance_group_class')->where('group_id',$gid)->where('day',$v)->delete();
  200. }else {
  201. $info = Db::name('attendance_group_class')->where('group_id',$gid)->where('day',$v)->find();
  202. if($info){
  203. if($info['day'] != $v){
  204. Db::name('attendance_group_class')->where('id',$info['id'])->update(['class_id'=>$data[$i]['class_id']]);
  205. }
  206. }else{
  207. Db::name('attendance_group_class')->where('id',$info['id'])->insert([
  208. 'org_id' => cur_org_id(),
  209. 'group_id' => $gid,
  210. 'class_id' => $class[$i]['class_id'],
  211. 'day' => $v
  212. ]);
  213. }
  214. }
  215. if($i == $max) {
  216. $i=0;
  217. }else{
  218. $i++;
  219. }
  220. }
  221. }
  222. }
  223. return true;
  224. }
  225. }