ScheduleService.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <?php
  2. namespace App\Service;
  3. use App\Model\BasicType;
  4. use App\Model\Employee;
  5. use App\Model\Product;
  6. use App\Model\ProductActivity;
  7. use App\Model\ProductActivityPrice;
  8. use App\Model\Schedule;
  9. use App\Model\ScheduleInfo;
  10. use Illuminate\Support\Facades\DB;
  11. class ScheduleService extends Service
  12. {
  13. public function edit($data,$user){
  14. list($status,$msg) = $this->scheduleRule($data, $user, false);
  15. if(!$status) return [$status,$msg];
  16. try {
  17. DB::beginTransaction();
  18. $model = Schedule::where('id',$data['id'])->first();
  19. $model->start_time = $data['start_time'] ?? 0;
  20. $model->end_time = $data['end_time'] ?? 0;
  21. $model->mark = $data['mark'] ?? '';
  22. $model->save();
  23. $time = time();
  24. ScheduleInfo::where('schedule_id',$data['id'])
  25. ->where('del_time',0)
  26. ->update(['del_time' => $time]);
  27. if(! empty($data['day_time'])){
  28. $insert = [];
  29. foreach ($data['day_time'] as $value){
  30. for ($i=0;$i<$value['num'];$i++){
  31. foreach ($data['time_range'] as $range){
  32. $insert[] = [
  33. 'schedule_id' => $model->id,
  34. 'crt_time' => $time,
  35. 'day' => $range ?? 0,
  36. 'start_time' => $value['start_time'] ?? "",
  37. 'end_time' => $value['end_time'] ?? "",
  38. ];
  39. }
  40. }
  41. }
  42. ScheduleInfo::insert($insert);
  43. }
  44. DB::commit();
  45. }catch (\Exception $exception){
  46. DB::rollBack();
  47. return [false,$exception->getMessage()];
  48. }
  49. return [true,''];
  50. }
  51. public function add($data,$user){
  52. list($status,$msg) = $this->scheduleRule($data, $user);
  53. if(!$status) return [$status,$msg];
  54. try {
  55. DB::beginTransaction();
  56. $model = new Schedule();
  57. $model->start_time = $data['start_time'] ?? 0;
  58. $model->end_time = $data['end_time'] ?? 0;
  59. $model->mark = $data['mark'] ?? '';
  60. $model->crt_id = $user['id'];
  61. $model->depart_id = $data['depart_id'] ?? 0;
  62. $model->top_depart_id = $data['top_depart_id'] ?? 0;
  63. $model->save();
  64. $time = time();
  65. if(! empty($data['day_time'])){
  66. $insert = [];
  67. foreach ($data['day_time'] as $value){
  68. for ($i=0;$i<$value['num'];$i++){
  69. foreach ($data['time_range'] as $range){
  70. $insert[] = [
  71. 'schedule_id' => $model->id,
  72. 'crt_time' => $time,
  73. 'day' => $range ?? 0,
  74. 'start_time' => $value['start_time'] ?? "",
  75. 'end_time' => $value['end_time'] ?? "",
  76. ];
  77. }
  78. }
  79. }
  80. ScheduleInfo::insert($insert);
  81. }
  82. DB::commit();
  83. }catch (\Exception $exception){
  84. DB::rollBack();
  85. return [false,$exception->getMessage()];
  86. }
  87. return [true,''];
  88. }
  89. public function del($data){
  90. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  91. try {
  92. DB::beginTransaction();
  93. $time = time();
  94. Schedule::where('id',$data['id'])->update(['del_time' => $time]);
  95. ScheduleInfo::where('schedule_id',$data['id'])
  96. ->where('del_time',0)
  97. ->update(['del_time' => $time]);
  98. DB::commit();
  99. }catch (\Exception $exception){
  100. DB::rollBack();
  101. return [false,$exception->getMessage()];
  102. }
  103. return [true,''];
  104. }
  105. public function detail($data,$user){
  106. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  107. $customer = ProductActivity::where('del_time',0)
  108. ->where('id',$data['id'])
  109. ->first();
  110. if(empty($customer)) return [false,'活动不存在或已被删除'];
  111. $customer = $customer->toArray();
  112. $detail = ProductActivityPrice::where('del_time',0)
  113. ->where('product_activity_id',$data['id'])
  114. ->get()->toArray();
  115. $title_map = BasicType::whereIn('id',array_column($detail,'basic_type_id'))
  116. ->pluck('title','id')
  117. ->toArray();
  118. $customer['product'] = [];
  119. //产品字典
  120. $map = (new ProductService())->getProductDetail(array_unique(array_column($detail,'product_id')));
  121. foreach ($detail as $value){
  122. $basic_tmp = [
  123. 'basic_type_id' => $value['basic_type_id'],
  124. 'basic_type_title' => $title_map[$value['basic_type_id']] ?? '',
  125. 'price' => $value['price'],
  126. ];
  127. if(! isset($customer['product'][$value['product_id']])){
  128. $tmp = $map[$value['product_id']] ?? [];
  129. $tmp['activity'][] = $basic_tmp;
  130. $customer['product'][$value['product_id']] = $tmp;
  131. }else{
  132. $customer['product'][$value['product_id']]['activity'][] = $basic_tmp;
  133. }
  134. }
  135. $customer['product'] = array_values($customer['product']);
  136. $customer['crt_name'] = Employee::where('id',$customer['crt_id'])->value('emp_name');
  137. $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
  138. return [true, $customer];
  139. }
  140. public function getList($data,$user){
  141. $model = ProductActivity::TopClear($user,$data);
  142. $model = $model->where('del_time',0)
  143. ->select('id','title','crt_id','mark','start_time','end_time','crt_time','type')
  144. ->orderby('id', 'desc');
  145. if(! empty($data['type'])) $model->where('type', $data['type']);
  146. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  147. if(! empty($data['mark'])) $model->where('mark', 'LIKE', '%'.$data['mark'].'%');
  148. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  149. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  150. $model->where('crt_time','>=',$return[0]);
  151. $model->where('crt_time','<=',$return[1]);
  152. }
  153. if(! empty($data['activity_time'][0]) && ! empty($data['activity_time'][1])) {
  154. $start_time = $this->changeDateToDateMin($data['activity_time'][0]);
  155. $end_time = $this->changeDateToDateMin($data['activity_time'][1]);
  156. $model->where('start_time','>=',$start_time);
  157. $model->where('end_time','<=',$end_time);
  158. }
  159. $list = $this->limit($model,'',$data);
  160. $list = $this->fillData($list,$user);
  161. return [true, $list];
  162. }
  163. public function scheduleRule(&$data, $user, $is_add = true){
  164. if(empty($data['schedule'][0]) || empty($data['schedule'][1])) return [false,'排班日期不能为空'];
  165. $data['start_time'] = $this->changeDateToDate($data['schedule'][0]);
  166. $data['end_time'] = $this->changeDateToDate($data['schedule'][1]);
  167. $data['time_range'] = $this->getTimestampRange($data['start_time'],$data['end_time']);
  168. if(empty($data['day_time'])) return [false,'排班时间段不能为空'];
  169. foreach ($data['day_time'] as $key => $value){
  170. if(empty($value['start_time'])) return [false,'排班时间段开始时间不能为空'];
  171. if(empty($value['end_time'])) return [false,'排班时间段结束时间不能为空'];
  172. if(empty($value['num'])) return [false,'排班车间数量不能为空'];
  173. }
  174. if($is_add){
  175. $bool = Schedule::where('del_time',0)
  176. ->where('start_time', '<=', $data['end_time'])
  177. ->where('end_time', '>=', $data['start_time'])
  178. ->exists();
  179. }else{
  180. if(empty($data['id'])) return [false,'ID不能为空'];
  181. $bool = Schedule::where('del_time',0)
  182. ->where('id','<>',$data['id'])
  183. ->where('start_time', '<=', $data['end_time'])
  184. ->where('end_time', '>=', $data['start_time'])
  185. ->exists();
  186. }
  187. if($bool) return [false,'排班日期已经设置,请不要重复设置!'];
  188. //所属部门 以及 顶级部门
  189. if(empty($data['depart_id'])) $data['depart_id'] = $this->getDepart($user);
  190. $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0;
  191. return [true, $data];
  192. }
  193. public function fillData($data, $user){
  194. if(empty($data['data'])) return $data;
  195. $emp = Employee::whereIn('id',array_unique(array_column($data['data'],'crt_id')))
  196. ->pluck('emp_name','id')
  197. ->toArray();
  198. foreach ($data['data'] as $key => $value){
  199. $start_time = $value['start_time'] ? date("Y-m-d H:i",$value['start_time']) : '';
  200. $end_time = $value['end_time'] ? date("Y-m-d H:i",$value['end_time']) : '';
  201. $data['data'][$key]['activity_time'] = $start_time . '——' . $end_time;
  202. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  203. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  204. $data['data'][$key]['type_title'] = ProductActivity::$type_name[$value['type']] ?? '';
  205. }
  206. return $data;
  207. }
  208. public function getTimestampRange($startTimestamp, $endTimestamp){
  209. // 将时间戳转换为日期格式
  210. $startDate = date('Y-m-d', $startTimestamp);
  211. $endDate = date('Y-m-d', $endTimestamp);
  212. // 创建日期范围
  213. $dateRange = [];
  214. $currentDate = strtotime($startDate);
  215. while ($currentDate <= strtotime($endDate)) {
  216. $dateRange[] = $currentDate;
  217. $currentDate = strtotime('+1 day', $currentDate);
  218. }
  219. // 将每个日期设置为当天的 0 点 0 分 0 秒并转换为时间戳
  220. $zeroTimestamps = [];
  221. foreach ($dateRange as $date) {
  222. $zeroTimestamps[] = strtotime(date('Y-m-d 00:00:00', $date));
  223. }
  224. return $zeroTimestamps;
  225. }
  226. }