ItemService.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. <?php
  2. namespace App\Service;
  3. use App\Model\Calendar;
  4. use App\Model\CalendarDetails;
  5. use App\Model\Device;
  6. use App\Model\DeviceDetails;
  7. use App\Model\Employee;
  8. use App\Model\EmployeeDetails;
  9. use App\Model\Item;
  10. use App\Model\ItemDetails;
  11. use Illuminate\Support\Facades\DB;
  12. class ItemService extends Service
  13. {
  14. public function itemEdit($data,$user){
  15. list($status,$msg) = $this->itemRule($data, $user, false);
  16. if(!$status) return [$status,$msg];
  17. try {
  18. DB::beginTransaction();
  19. $model = Item::where('id',$data['id'])->first();
  20. $model->code = $data['code'] ?? '';
  21. $model->title = $data['title'] ?? '';
  22. $model->mark = $data['mark'] ?? "";
  23. $model->start_time = $data['start_time'] ?? 0;
  24. $model->end_time = $data['end_time'] ?? 0;
  25. $model->is_use = $data['is_use'] ?? 0;
  26. $model->save();
  27. $time = time();
  28. ItemDetails::where('del_time',0)
  29. ->where('item_id', $model->id)
  30. ->update(['del_time' => $time]);
  31. $this->saveDetail($model->id, $time, $data);
  32. DB::commit();
  33. }catch (\Exception $exception){
  34. DB::rollBack();
  35. return [false,$exception->getMessage()];
  36. }
  37. return [true, ''];
  38. }
  39. public function itemAdd($data,$user){
  40. list($status,$msg) = $this->itemRule($data, $user);
  41. if(!$status) return [$status,$msg];
  42. try {
  43. DB::beginTransaction();
  44. $model = new Item();
  45. $model->code = $data['code'] ?? '';
  46. $model->title = $data['title'] ?? '';
  47. $model->mark = $data['mark'] ?? "";
  48. $model->start_time = $data['start_time'] ?? 0;
  49. $model->end_time = $data['end_time'] ?? 0;
  50. $model->is_use = $data['is_use'] ?? 0;
  51. $model->crt_id = $user['id'];
  52. $model->save();
  53. $this->saveDetail($model->id, time(), $data);
  54. DB::commit();
  55. }catch (\Exception $exception){
  56. DB::rollBack();
  57. return [false,$exception->getMessage()];
  58. }
  59. return [true, ''];
  60. }
  61. private function saveDetail($id, $time, $data){
  62. if(! empty($data['man_list'])){
  63. $unit = [];
  64. foreach ($data['man_list'] as $value){
  65. $unit[] = [
  66. 'item_id' => $id,
  67. 'type' => $value['type'],
  68. 'data_id' => $value['data_id'],
  69. 'crt_time' => $time,
  70. ];
  71. }
  72. if(! empty($unit)) ItemDetails::insert($unit);
  73. }
  74. if(! empty($data['device_list'])){
  75. $receipt = [];
  76. foreach ($data['device_list'] as $value){
  77. $receipt[] = [
  78. 'item_id' => $id,
  79. 'type' => $value['type'],
  80. 'data_id' => $value['data_id'],
  81. 'crt_time' => $time,
  82. ];
  83. }
  84. if(! empty($receipt)) ItemDetails::insert($receipt);
  85. }
  86. }
  87. private function getDetail($id){
  88. $data = ItemDetails::where('del_time',0)
  89. ->where('item_id', $id)
  90. ->get()->toArray();
  91. $id = $id2 = [];
  92. foreach ($data as $value){
  93. if($value['type'] == ItemDetails::type_one) {
  94. $id[] = $value['data_id'];
  95. }else{
  96. $id2[] = $value['data_id'];
  97. }
  98. }
  99. $map = Employee::whereIn('id', $id)->select('emp_name','id','number')->get()->toArray();
  100. $map = array_column($map,null,'id');
  101. $map2 = Device::whereIn('id', $id2)->select('code','id','title')->get()->toArray();
  102. $map2 = array_column($map2,null,'id');
  103. $unit = $receipt = [];
  104. foreach ($data as $value){
  105. if($value['type'] == ItemDetails::type_one) {
  106. $tmp = $map[$value['data_id']] ?? [];
  107. $unit[] = [
  108. 'type' => $value['type'],
  109. 'data_id' => $value['data_id'],
  110. 'data_title' => $tmp['emp_name'],
  111. 'data_code' => $tmp['number'],
  112. ];
  113. }else{
  114. $tmp = $map2[$value['data_id']] ?? [];
  115. $receipt[] = [
  116. 'type' => $value['type'],
  117. 'data_id' => $value['data_id'],
  118. 'data_title' => $tmp['title'] ?? "",
  119. 'data_code' => $tmp['code'] ?? "",
  120. ];
  121. }
  122. }
  123. $detail = [
  124. 'man_list' => $unit,
  125. 'device_list' => $receipt,
  126. ];
  127. foreach ($detail as $key => $value) {
  128. if (empty($value)) {
  129. $detail[$key] = (object)[]; // 转成 stdClass 对象
  130. }
  131. }
  132. return $detail;
  133. }
  134. public function getItemMap($ids){
  135. if(empty($ids)) return [];
  136. if(! is_array($ids)) $ids = [$ids];
  137. return Item::whereIn('id', $ids)
  138. ->pluck('title', 'id')
  139. ->toArray();
  140. }
  141. public function itemDel($data){
  142. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  143. try {
  144. DB::beginTransaction();
  145. $time = time();
  146. Item::where('del_time',0)
  147. ->whereIn('id',$data['id'])
  148. ->update(['del_time' => $time]);
  149. ItemDetails::where('del_time',0)
  150. ->whereIn('item_id', $data['id'])
  151. ->update(['del_time' => $time]);
  152. DB::commit();
  153. }catch (\Exception $exception){
  154. DB::rollBack();
  155. return [false,$exception->getMessage()];
  156. }
  157. return [true, ''];
  158. }
  159. public function itemDetail($data, $user){
  160. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  161. $customer = Item::where('del_time',0)
  162. ->where('id',$data['id'])
  163. ->first();
  164. if(empty($customer)) return [false,'项目不存在或已被删除'];
  165. $customer = $customer->toArray();
  166. $customer['start_time'] = ! empty($customer['start_time']) ? date("Y-m-d", $customer['start_time']) : "";
  167. $customer['end_time'] = ! empty($customer['end_time']) ? date("Y-m-d", $customer['end_time']) : "";
  168. $customer['crt_name'] = Employee::where('id',$customer['crt_id'])->value('emp_name');
  169. $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
  170. $details = $this->getDetail($data['id']);
  171. $customer = array_merge($customer, $details);
  172. return [true, $customer];
  173. }
  174. public function itemCommon($data,$user, $field = []){
  175. if(empty($field)) $field = Item::$field;
  176. $model = Item::Clear($user,$data);
  177. $model = $model->where('del_time',0)
  178. ->select($field)
  179. ->orderby('id', 'desc');
  180. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  181. if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
  182. if(! empty($data['type'])) $model->where('type', $data['type']);
  183. if(! empty($data['id'])) $model->whereIn('id', $data['id']);
  184. if(! empty($data['is_use'])) $model->where('is_use', $data['is_use']);
  185. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  186. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  187. $model->where('crt_time','>=',$return[0]);
  188. $model->where('crt_time','<=',$return[1]);
  189. }
  190. return $model;
  191. }
  192. public function itemList($data,$user){
  193. $model = $this->itemCommon($data, $user);
  194. $list = $this->limit($model,'',$data);
  195. $list = $this->fillData($list);
  196. return [true, $list];
  197. }
  198. public function itemRule(&$data, $user, $is_add = true){
  199. if(empty($data['code'])) return [false, '编码不能为空'];
  200. if(empty($data['title'])) return [false, '名称不能为空'];
  201. if(! empty($data['start_time'])) $data['start_time'] = $this->changeDateToDate($data['start_time']);
  202. if(! empty($data['end_time'])) $data['end_time'] = $this->changeDateToDate($data['end_time'],true);
  203. $isMonthEnd = date('j', $data['end_time']) == date('t', $data['end_time']);
  204. if(! $isMonthEnd) return [false, '项目结束日期必须是当月最后一天'];
  205. if(empty($data['is_use'])) return [false, '是否启用不能为空'];
  206. if(! isset(Item::Use[$data['is_use']])) return [false, '是否启用错误'];
  207. if(empty($data['man_list'])) return [false, '研发人员不能为空'];
  208. foreach ($data['man_list'] as $value){
  209. if(empty($value['type'])) return [false, '类型不能为空'];
  210. if(empty($value['data_id'])) return [false, '研发人员不能为空'];
  211. }
  212. list($status, $msg) = $this->checkArrayRepeat($data['man_list'],'data_id','研发人员');
  213. if(! $status) return [false, $msg];
  214. if(! empty($data['device_list'])){
  215. foreach ($data['device_list'] as $value){
  216. if(empty($value['type'])) return [false, '类型不能为空'];
  217. if(empty($value['data_id'])) return [false, '数据ID不能为空'];
  218. }
  219. }
  220. if($is_add){
  221. $bool = Item::where('code',$data['code'])
  222. ->where('crt_id', $user['id'])
  223. ->where('del_time',0)
  224. ->exists();
  225. }else{
  226. if(empty($data['id'])) return [false,'ID不能为空'];
  227. $bool = Item::where('code',$data['code'])
  228. ->where('crt_id', $user['id'])
  229. ->where('id','<>',$data['id'])
  230. ->where('del_time',0)
  231. ->exists();
  232. }
  233. if($bool) return [false, '编码已存在'];
  234. return [true, $data];
  235. }
  236. public function fillData($data){
  237. if(empty($data['data'])) return $data;
  238. $emp = (new EmployeeService())->getEmployeeMap(array_unique(array_column($data['data'],'crt_id')));
  239. foreach ($data['data'] as $key => $value){
  240. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  241. $data['data'][$key]['start_time'] = $value['start_time'] ? date('Y-m-d',$value['start_time']) : '';
  242. $data['data'][$key]['end_time'] = $value['end_time'] ? date('Y-m-d',$value['end_time']) : '';
  243. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  244. $data['data'][$key]['is_use_title'] = Item::Use[$value['is_use']] ?? "";
  245. }
  246. return $data;
  247. }
  248. //--------------------------------------------------------
  249. public function calendarEdit($data,$user){
  250. list($status,$msg) = $this->calendarRule($data, $user, false);
  251. if(!$status) return [$status,$msg];
  252. try {
  253. DB::beginTransaction();
  254. $model = Calendar::where('id',$data['id'])->first();
  255. $model->time = $data['time'] ?? 0;
  256. $model->work_days = $data['work_days'] ?? 0;
  257. $model->each_day_hours = $data['each_day_hours'] ?? 0;
  258. $model->total_hours = $data['total_hours'] ?? 0;
  259. $model->save();
  260. $time = time();
  261. CalendarDetails::where('del_time',0)
  262. ->where('calendar_id', $model->id)
  263. ->update(['del_time' => $time]);
  264. $this->saveDetail1($model->id, $time, $data);
  265. DB::commit();
  266. }catch (\Exception $exception){
  267. DB::rollBack();
  268. return [false,$exception->getMessage()];
  269. }
  270. return [true, ''];
  271. }
  272. public function calendarAdd($data,$user){
  273. list($status,$msg) = $this->calendarRule($data, $user);
  274. if(!$status) return [$status,$msg];
  275. try {
  276. DB::beginTransaction();
  277. $model = new Calendar();
  278. $model->time = $data['time'] ?? 0;
  279. $model->work_days = $data['work_days'] ?? 0;
  280. $model->each_day_hours = $data['each_day_hours'] ?? 0;
  281. $model->total_hours = $data['total_hours'] ?? 0;
  282. $model->crt_id = $user['id'];
  283. $model->save();
  284. $this->saveDetail1($model->id, time(), $data);
  285. DB::commit();
  286. }catch (\Exception $exception){
  287. DB::rollBack();
  288. return [false,$exception->getMessage()];
  289. }
  290. return [true, ''];
  291. }
  292. private function saveDetail1($id, $time, $data){
  293. if(! empty($data['details'])){
  294. $unit = [];
  295. foreach ($data['details'] as $value){
  296. $unit[] = [
  297. 'calendar_id' => $id,
  298. 'time' => $value['time'],
  299. 'is_work' => $value['is_work'],
  300. 'crt_time' => $time,
  301. ];
  302. }
  303. if(! empty($unit)) CalendarDetails::insert($unit);
  304. }
  305. //更新人员满勤工时 设备满勤工时
  306. EmployeeDetails::where('del_time',0)
  307. ->where('time',$data['time'])
  308. ->update(['del_time' => $time]);
  309. $employee = Employee::where('del_time',0)
  310. ->select('id')
  311. ->get()->toArray();
  312. $employee_details = [];
  313. foreach ($employee as $value){
  314. $employee_details[] = [
  315. 'employee_id' => $value['id'],
  316. 'time' => $data['time'],
  317. // 'work_days' => $data['work_days'],
  318. // 'each_day_hours' => $data['each_day_hours'],
  319. 'total_hours' => $data['total_hours'],
  320. 'total_hours_2' => $data['total_hours'],
  321. 'crt_time' => $time,
  322. ];
  323. }
  324. if(! empty($employee_details)) EmployeeDetails::insert($employee_details);
  325. DeviceDetails::where('del_time',0)
  326. ->where('time',$data['time'])
  327. ->update(['del_time' => $time]);
  328. $device = Device::where('del_time',0)
  329. ->where('is_use', 1)
  330. ->select('id')
  331. ->get()->toArray();
  332. $device_details = [];
  333. foreach ($device as $value){
  334. $device_details[] = [
  335. 'device_id' => $value['id'],
  336. 'time' => $data['time'],
  337. // 'work_days' => $data['work_days'],
  338. // 'each_day_hours' => $data['each_day_hours'],
  339. 'total_hours' => $data['total_hours'],
  340. 'total_hours_2' => $data['total_hours'],
  341. 'crt_time' => $time,
  342. ];
  343. }
  344. if(! empty($device_details)) DeviceDetails::insert($device_details);
  345. }
  346. private function getDetail1($id){
  347. $data = CalendarDetails::where('del_time',0)
  348. ->where('calendar_id', $id)
  349. ->get()->toArray();
  350. $unit = [];
  351. foreach ($data as $value){
  352. $unit[] = [
  353. 'time' => date("Y-m-d",$value['time']),
  354. 'is_work' => $value['is_work'],
  355. ];
  356. }
  357. $detail = [
  358. 'details' => $unit,
  359. ];
  360. foreach ($detail as $key => $value) {
  361. if (empty($value)) {
  362. $detail[$key] = (object)[]; // 转成 stdClass 对象
  363. }
  364. }
  365. return $detail;
  366. }
  367. public function calendarDel($data){
  368. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  369. try {
  370. DB::beginTransaction();
  371. $time = time();
  372. Calendar::where('del_time',0)
  373. ->whereIn('id',$data['id'])
  374. ->update(['del_time' => $time]);
  375. CalendarDetails::where('del_time',0)
  376. ->whereIn('calendar_id', $data['id'])
  377. ->update(['del_time' => $time]);
  378. DB::commit();
  379. }catch (\Exception $exception){
  380. DB::rollBack();
  381. return [false,$exception->getMessage()];
  382. }
  383. return [true, ''];
  384. }
  385. public function calendarDetail($data, $user){
  386. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  387. $customer = Calendar::where('del_time',0)
  388. ->where('id',$data['id'])
  389. ->first();
  390. if(empty($customer)) return [false,'日历设置不存在或已被删除'];
  391. $customer = $customer->toArray();
  392. $customer['time'] = ! empty($customer['time']) ? date("Y-m", $customer['time']) : "";
  393. $customer['crt_name'] = Employee::where('id',$customer['crt_id'])->value('emp_name');
  394. $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
  395. $details = $this->getDetail1($data['id']);
  396. $customer = array_merge($customer, $details);
  397. return [true, $customer];
  398. }
  399. public function calendarCommon($data,$user, $field = []){
  400. if(empty($field)) $field = Calendar::$field;
  401. $model = Calendar::where('del_time',0)
  402. ->select($field)
  403. ->orderby('id', 'desc');
  404. if(! empty($data['id'])) $model->whereIn('id', $data['id']);
  405. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  406. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  407. $model->where('crt_time','>=',$return[0]);
  408. $model->where('crt_time','<=',$return[1]);
  409. }
  410. if(! empty($data['time'][0]) && ! empty($data['time'][1])) {
  411. $return = $this->changeDateToTimeStampAboutRange($data['time']);
  412. $model->where('time','>=',$return[0]);
  413. $model->where('time','<=',$return[1]);
  414. }
  415. return $model;
  416. }
  417. public function calendarList($data,$user){
  418. $model = $this->calendarCommon($data, $user);
  419. $list = $this->limit($model,'',$data);
  420. $list = $this->fillData1($list,$user,$data);
  421. return [true, $list];
  422. }
  423. public function calendarRule(&$data, $user, $is_add = true){
  424. if(empty($data['time'])) return [false, '年月不能为空'];
  425. $data['time'] = $this->changeDateToMonth($data['time']);
  426. $res = $this->checkNumber($data['work_days'],0,'positive');
  427. if(! $res['valid']) return [false,'工作日:' . $res['error']];
  428. $res = $this->checkNumber($data['each_day_hours'],0,'positive');
  429. if(! $res['valid']) return [false,'每日工时:' . $res['error']];
  430. if(floatval($data['each_day_hours']) > 8.0) return [false, '每日工时最多8小时'];
  431. $res = $this->checkNumber($data['total_hours'],0,'positive');
  432. if(! $res['valid']) return [false,'总工时:' . $res['error']];
  433. if(! empty($data['details'])){
  434. foreach ($data['details'] as $key => $value){
  435. if(empty($value['time'])) return [false, '日期不能为空'];
  436. $data['details'][$key]['time'] = $this->changeDateToDate($value['time']);
  437. if(! isset($value['is_work'])) return [false, '是否工作日不能为空'];
  438. }
  439. }
  440. if($is_add){
  441. $bool = Calendar::where('time', $data['time'])
  442. ->where('del_time',0)
  443. ->exists();
  444. }else{
  445. if(empty($data['id'])) return [false,'ID不能为空'];
  446. $bool = Calendar::where('time',$data['time'])
  447. ->where('id','<>',$data['id'])
  448. ->where('del_time',0)
  449. ->exists();
  450. }
  451. if($bool) return [false, '该年月下的设置已存在'];
  452. return [true, $data];
  453. }
  454. public function fillData1($data, $user, $search){
  455. if(empty($data['data'])) return $data;
  456. $emp = (new EmployeeService())->getEmployeeMap(array_unique(array_column($data['data'],'crt_id')));
  457. foreach ($data['data'] as $key => $value){
  458. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  459. $data['data'][$key]['time'] = $value['time'] ? date('Y-m',$value['time']) : '';
  460. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  461. }
  462. return $data;
  463. }
  464. }