CalendarService.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. namespace App\Service;
  3. use App\Model\Calendar;
  4. use App\Model\CalendarDetails;
  5. use App\Model\Employee;
  6. use Illuminate\Support\Facades\DB;
  7. class CalendarService extends Service
  8. {
  9. public function calendarEdit($data,$user){
  10. list($status,$msg) = $this->calendarRule($data, $user, false);
  11. if(!$status) return [$status,$msg];
  12. try {
  13. DB::beginTransaction();
  14. $model = Calendar::where('id',$data['id'])->first();
  15. $model->time = $data['time'] ?? 0;
  16. $model->work_days = $data['work_days'] ?? 0;
  17. $model->save();
  18. $time = time();
  19. CalendarDetails::where('del_time',0)
  20. ->where('calendar_id', $model->id)
  21. ->update(['del_time' => $time]);
  22. $this->saveDetail($model->id, $time, $data);
  23. DB::commit();
  24. }catch (\Exception $exception){
  25. DB::rollBack();
  26. return [false,$exception->getMessage()];
  27. }
  28. return [true, ''];
  29. }
  30. public function calendarAdd($data,$user){
  31. list($status,$msg) = $this->calendarRule($data, $user);
  32. if(!$status) return [$status,$msg];
  33. try {
  34. DB::beginTransaction();
  35. $model = new Calendar();
  36. $model->time = $data['time'] ?? 0;
  37. $model->work_days = $data['work_days'] ?? 0;
  38. $model->crt_id = $user['id'];
  39. $model->top_depart_id = $data['top_depart_id'];
  40. $model->save();
  41. $this->saveDetail($model->id, time(), $data);
  42. DB::commit();
  43. }catch (\Exception $exception){
  44. DB::rollBack();
  45. return [false,$exception->getMessage()];
  46. }
  47. return [true, ''];
  48. }
  49. private function saveDetail($id, $time, $data){
  50. if(! empty($data['details'])){
  51. $unit = [];
  52. foreach ($data['details'] as $value){
  53. $unit[] = [
  54. 'calendar_id' => $id,
  55. 'time' => $value['time'],
  56. 'is_work' => $value['is_work'],
  57. 'crt_time' => $time,
  58. 'top_depart_id' => $value['top_depart_id'],
  59. ];
  60. }
  61. if(! empty($unit)) CalendarDetails::insert($unit);
  62. }
  63. }
  64. private function getDetail($id){
  65. $data = CalendarDetails::where('del_time',0)
  66. ->where('calendar_id', $id)
  67. ->get()->toArray();
  68. $unit = [];
  69. foreach ($data as $value){
  70. $unit[] = [
  71. 'time' => date("Y-m-d",$value['time']),
  72. 'is_work' => $value['is_work'],
  73. ];
  74. }
  75. $detail = [
  76. 'details' => $unit,
  77. ];
  78. foreach ($detail as $key => $value) {
  79. if (empty($value)) {
  80. $detail[$key] = (object)[]; // 转成 stdClass 对象
  81. }
  82. }
  83. return $detail;
  84. }
  85. public function calendarDel($data){
  86. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  87. try {
  88. DB::beginTransaction();
  89. $time = time();
  90. Calendar::where('del_time',0)
  91. ->whereIn('id',$data['id'])
  92. ->update(['del_time' => $time]);
  93. CalendarDetails::where('del_time',0)
  94. ->whereIn('calendar_id', $data['id'])
  95. ->update(['del_time' => $time]);
  96. DB::commit();
  97. }catch (\Exception $exception){
  98. DB::rollBack();
  99. return [false,$exception->getMessage()];
  100. }
  101. return [true, ''];
  102. }
  103. public function calendarDetail($data, $user){
  104. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  105. $customer = Calendar::where('del_time',0)
  106. ->where('id',$data['id'])
  107. ->first();
  108. if(empty($customer)) return [false,'日历设置不存在或已被删除'];
  109. $customer = $customer->toArray();
  110. $customer['time'] = ! empty($customer['time']) ? date("Y-m", $customer['time']) : "";
  111. $customer['crt_name'] = Employee::where('id',$customer['crt_id'])->value('emp_name');
  112. $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
  113. $details = $this->getDetail($data['id']);
  114. $customer = array_merge($customer, $details);
  115. return [true, $customer];
  116. }
  117. public function calendarCommon($data,$user, $field = []){
  118. if(empty($field)) $field = Calendar::$field;
  119. $model = Calendar::Clear($user,$data);
  120. $model = $model->where('del_time',0)
  121. ->select($field)
  122. ->orderby('id', 'desc');
  123. if(! empty($data['id'])) $model->whereIn('id', $data['id']);
  124. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  125. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  126. $model->where('crt_time','>=',$return[0]);
  127. $model->where('crt_time','<=',$return[1]);
  128. }
  129. if(! empty($data['time'][0]) && ! empty($data['time'][1])) {
  130. $return = $this->changeDateToTimeStampAboutRange($data['time']);
  131. $model->where('time','>=',$return[0]);
  132. $model->where('time','<=',$return[1]);
  133. }
  134. return $model;
  135. }
  136. public function calendarList($data,$user){
  137. $model = $this->calendarCommon($data, $user);
  138. $list = $this->limit($model,'',$data);
  139. $list = $this->fillData($list,$user,$data);
  140. return [true, $list];
  141. }
  142. public function calendarRule(&$data, $user, $is_add = true){
  143. $data['top_depart_id'] = $user['top_depart_id'];
  144. if(empty($data['time'])) return [false, '年月不能为空'];
  145. $data['time'] = $this->changeDateToMonth($data['time']);
  146. $res = $this->checkNumber($data['work_days'],0,'positive');
  147. if(! $res['valid']) return [false,'工作日:' . $res['error']];
  148. if(! empty($data['details'])){
  149. foreach ($data['details'] as $key => $value){
  150. if(empty($value['time'])) return [false, '日期不能为空'];
  151. $data['details'][$key]['time'] = $this->changeDateToDate($value['time']);
  152. if(! isset($value['is_work'])) return [false, '是否工作日不能为空'];
  153. $data['details'][$key]['top_depart_id'] = $data['top_depart_id'];
  154. }
  155. }
  156. if($is_add){
  157. $bool = Calendar::where('time', $data['time'])
  158. ->where('top_depart_id', $data['top_depart_id'])
  159. ->where('del_time',0)
  160. ->exists();
  161. }else{
  162. if(empty($data['id'])) return [false,'ID不能为空'];
  163. $bool = Calendar::where('time',$data['time'])
  164. ->where('top_depart_id', $data['top_depart_id'])
  165. ->where('id','<>',$data['id'])
  166. ->where('del_time',0)
  167. ->exists();
  168. }
  169. if($bool) return [false, '该年月下的日历设置已存在'];
  170. return [true, ''];
  171. }
  172. public function fillData($data, $user, $search){
  173. if(empty($data['data'])) return $data;
  174. $emp = (new EmployeeService())->getEmployeeMap(array_unique(array_column($data['data'],'crt_id')));
  175. foreach ($data['data'] as $key => $value){
  176. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  177. $data['data'][$key]['time'] = $value['time'] ? date('Y-m',$value['time']) : '';
  178. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  179. }
  180. return $data;
  181. }
  182. }