CalendarService.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. 'month' => $value['month'],
  60. ];
  61. }
  62. if(! empty($unit)) CalendarDetails::insert($unit);
  63. }
  64. }
  65. private function getDetail($id){
  66. $data = CalendarDetails::where('del_time',0)
  67. ->where('calendar_id', $id)
  68. ->get()->toArray();
  69. $unit = [];
  70. foreach ($data as $value){
  71. $unit[] = [
  72. 'time' => date("Y-m-d",$value['time']),
  73. 'is_work' => $value['is_work'],
  74. ];
  75. }
  76. $detail = [
  77. 'details' => $unit,
  78. ];
  79. foreach ($detail as $key => $value) {
  80. if (empty($value)) {
  81. $detail[$key] = (object)[]; // 转成 stdClass 对象
  82. }
  83. }
  84. return $detail;
  85. }
  86. public function calendarDel($data){
  87. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  88. try {
  89. DB::beginTransaction();
  90. $time = time();
  91. Calendar::where('del_time',0)
  92. ->whereIn('id',$data['id'])
  93. ->update(['del_time' => $time]);
  94. CalendarDetails::where('del_time',0)
  95. ->whereIn('calendar_id', $data['id'])
  96. ->update(['del_time' => $time]);
  97. DB::commit();
  98. }catch (\Exception $exception){
  99. DB::rollBack();
  100. return [false,$exception->getMessage()];
  101. }
  102. return [true, ''];
  103. }
  104. public function calendarDetail($data, $user){
  105. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  106. $customer = Calendar::where('del_time',0)
  107. ->where('id',$data['id'])
  108. ->first();
  109. if(empty($customer)) return [false,'日历设置不存在或已被删除'];
  110. $customer = $customer->toArray();
  111. $customer['time'] = ! empty($customer['time']) ? date("Y-m", $customer['time']) : "";
  112. $customer['crt_name'] = Employee::where('id',$customer['crt_id'])->value('emp_name');
  113. $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
  114. $details = $this->getDetail($data['id']);
  115. $customer = array_merge($customer, $details);
  116. return [true, $customer];
  117. }
  118. public function calendarCommon($data,$user, $field = []){
  119. if(empty($field)) $field = Calendar::$field;
  120. $model = Calendar::Clear($user,$data);
  121. $model = $model->where('del_time',0)
  122. ->select($field)
  123. ->orderby('id', 'desc');
  124. if(! empty($data['id'])) $model->whereIn('id', $data['id']);
  125. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  126. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  127. $model->where('crt_time','>=',$return[0]);
  128. $model->where('crt_time','<=',$return[1]);
  129. }
  130. if(! empty($data['time'][0]) && ! empty($data['time'][1])) {
  131. $return = $this->changeDateToTimeStampAboutRange($data['time']);
  132. $model->where('time','>=',$return[0]);
  133. $model->where('time','<=',$return[1]);
  134. }
  135. return $model;
  136. }
  137. public function calendarList($data,$user){
  138. $model = $this->calendarCommon($data, $user);
  139. $list = $this->limit($model,'',$data);
  140. $list = $this->fillData($list,$user,$data);
  141. return [true, $list];
  142. }
  143. public function calendarRule(&$data, $user, $is_add = true){
  144. $data['top_depart_id'] = $user['top_depart_id'];
  145. if(empty($data['time'])) return [false, '年月不能为空'];
  146. $data['time'] = $this->changeDateToMonth($data['time']);
  147. $res = $this->checkNumber($data['work_days'],0,'positive');
  148. if(! $res['valid']) return [false,'工作日:' . $res['error']];
  149. if(! empty($data['details'])){
  150. foreach ($data['details'] as $key => $value){
  151. if(empty($value['time'])) return [false, '日期不能为空'];
  152. $data['details'][$key]['time'] = $this->changeDateToDate($value['time']);
  153. if(! isset($value['is_work'])) return [false, '是否工作日不能为空'];
  154. $data['details'][$key]['top_depart_id'] = $data['top_depart_id'];
  155. $data['details'][$key]['month'] = $data['time'];
  156. }
  157. }
  158. if($is_add){
  159. $bool = Calendar::where('time', $data['time'])
  160. ->where('top_depart_id', $data['top_depart_id'])
  161. ->where('del_time',0)
  162. ->exists();
  163. }else{
  164. if(empty($data['id'])) return [false,'ID不能为空'];
  165. $bool = Calendar::where('time',$data['time'])
  166. ->where('top_depart_id', $data['top_depart_id'])
  167. ->where('id','<>',$data['id'])
  168. ->where('del_time',0)
  169. ->exists();
  170. }
  171. if($bool) return [false, '该年月下的日历设置已存在'];
  172. return [true, ''];
  173. }
  174. public function fillData($data, $user, $search){
  175. if(empty($data['data'])) return $data;
  176. $emp = (new EmployeeService())->getEmployeeMap(array_unique(array_column($data['data'],'crt_id')));
  177. foreach ($data['data'] as $key => $value){
  178. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  179. $data['data'][$key]['time'] = $value['time'] ? date('Y-m',$value['time']) : '';
  180. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  181. }
  182. return $data;
  183. }
  184. }