ItemService.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  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. ->where('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. if(empty($data['is_use'])) return [false, '是否启用不能为空'];
  204. if(! isset(Item::Use[$data['is_use']])) return [false, '是否启用错误'];
  205. if(empty($data['man_list'])) return [false, '研发人员不能为空'];
  206. foreach ($data['man_list'] as $value){
  207. if(empty($value['type'])) return [false, '类型不能为空'];
  208. if(empty($value['data_id'])) return [false, '研发人员不能为空'];
  209. }
  210. list($status, $msg) = $this->checkArrayRepeat($data['man_list'],'data_id','研发人员');
  211. if(! $status) return [false, $msg];
  212. if(! empty($data['device_list'])){
  213. foreach ($data['device_list'] as $value){
  214. if(empty($value['type'])) return [false, '类型不能为空'];
  215. if(empty($value['data_id'])) return [false, '数据ID不能为空'];
  216. }
  217. }
  218. if($is_add){
  219. $bool = Item::where('code',$data['code'])
  220. ->where('crt_id', $user['id'])
  221. ->where('del_time',0)
  222. ->exists();
  223. }else{
  224. if(empty($data['id'])) return [false,'ID不能为空'];
  225. $bool = Item::where('code',$data['code'])
  226. ->where('crt_id', $user['id'])
  227. ->where('id','<>',$data['id'])
  228. ->where('del_time',0)
  229. ->exists();
  230. }
  231. if($bool) return [false, '编码已存在'];
  232. return [true, $data];
  233. }
  234. public function fillData($data){
  235. if(empty($data['data'])) return $data;
  236. $emp = (new EmployeeService())->getEmployeeMap(array_unique(array_column($data['data'],'crt_id')));
  237. foreach ($data['data'] as $key => $value){
  238. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  239. $data['data'][$key]['start_time'] = $value['start_time'] ? date('Y-m-d',$value['start_time']) : '';
  240. $data['data'][$key]['end_time'] = $value['end_time'] ? date('Y-m-d',$value['end_time']) : '';
  241. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  242. $data['data'][$key]['is_use_title'] = Item::Use[$value['is_use']] ?? "";
  243. }
  244. return $data;
  245. }
  246. //--------------------------------------------------------
  247. public function calendarEdit($data,$user){
  248. list($status,$msg) = $this->calendarRule($data, $user, false);
  249. if(!$status) return [$status,$msg];
  250. try {
  251. DB::beginTransaction();
  252. $model = Calendar::where('id',$data['id'])->first();
  253. $model->time = $data['time'] ?? 0;
  254. $model->work_days = $data['work_days'] ?? 0;
  255. $model->each_day_hours = $data['each_day_hours'] ?? 0;
  256. $model->total_hours = $data['total_hours'] ?? 0;
  257. $model->save();
  258. $time = time();
  259. CalendarDetails::where('del_time',0)
  260. ->where('calendar_id', $model->id)
  261. ->update(['del_time' => $time]);
  262. $this->saveDetail1($model->id, $time, $data);
  263. DB::commit();
  264. }catch (\Exception $exception){
  265. DB::rollBack();
  266. return [false,$exception->getMessage()];
  267. }
  268. return [true, ''];
  269. }
  270. public function calendarAdd($data,$user){
  271. list($status,$msg) = $this->calendarRule($data, $user);
  272. if(!$status) return [$status,$msg];
  273. try {
  274. DB::beginTransaction();
  275. $model = new Calendar();
  276. $model->time = $data['time'] ?? 0;
  277. $model->work_days = $data['work_days'] ?? 0;
  278. $model->each_day_hours = $data['each_day_hours'] ?? 0;
  279. $model->total_hours = $data['total_hours'] ?? 0;
  280. $model->crt_id = $user['id'];
  281. $model->save();
  282. $this->saveDetail1($model->id, time(), $data);
  283. DB::commit();
  284. }catch (\Exception $exception){
  285. DB::rollBack();
  286. return [false,$exception->getMessage()];
  287. }
  288. return [true, ''];
  289. }
  290. private function saveDetail1($id, $time, $data){
  291. if(! empty($data['details'])){
  292. $unit = [];
  293. foreach ($data['details'] as $value){
  294. $unit[] = [
  295. 'calendar_id' => $id,
  296. 'time' => $value['time'],
  297. 'is_work' => $value['is_work'],
  298. 'crt_time' => $time,
  299. ];
  300. }
  301. if(! empty($unit)) CalendarDetails::insert($unit);
  302. }
  303. //更新人员满勤工时 设备满勤工时
  304. EmployeeDetails::where('del_time',0)
  305. ->where('time',$data['time'])
  306. ->update(['del_time' => $time]);
  307. $employee = Employee::where('del_time',0)
  308. ->select('id')
  309. ->get()->toArray();
  310. $employee_details = [];
  311. foreach ($employee as $value){
  312. $employee_details[] = [
  313. 'employee_id' => $value['id'],
  314. 'time' => $data['time'],
  315. // 'work_days' => $data['work_days'],
  316. // 'each_day_hours' => $data['each_day_hours'],
  317. 'total_hours' => $data['total_hours'],
  318. 'total_hours_2' => $data['total_hours'],
  319. 'crt_time' => $time,
  320. ];
  321. }
  322. if(! empty($employee_details)) EmployeeDetails::insert($employee_details);
  323. DeviceDetails::where('del_time',0)
  324. ->where('time',$data['time'])
  325. ->update(['del_time' => $time]);
  326. $device = Device::where('del_time',0)
  327. ->where('is_use', 1)
  328. ->select('id')
  329. ->get()->toArray();
  330. $device_details = [];
  331. foreach ($device as $value){
  332. $device_details[] = [
  333. 'device_id' => $value['id'],
  334. 'time' => $data['time'],
  335. // 'work_days' => $data['work_days'],
  336. // 'each_day_hours' => $data['each_day_hours'],
  337. 'total_hours' => $data['total_hours'],
  338. 'total_hours_2' => $data['total_hours'],
  339. 'crt_time' => $time,
  340. ];
  341. }
  342. if(! empty($device_details)) DeviceDetails::insert($device_details);
  343. }
  344. private function getDetail1($id){
  345. $data = CalendarDetails::where('del_time',0)
  346. ->where('calendar_id', $id)
  347. ->get()->toArray();
  348. $unit = [];
  349. foreach ($data as $value){
  350. $unit[] = [
  351. 'time' => date("Y-m-d",$value['time']),
  352. 'is_work' => $value['is_work'],
  353. ];
  354. }
  355. $detail = [
  356. 'details' => $unit,
  357. ];
  358. foreach ($detail as $key => $value) {
  359. if (empty($value)) {
  360. $detail[$key] = (object)[]; // 转成 stdClass 对象
  361. }
  362. }
  363. return $detail;
  364. }
  365. public function calendarDel($data){
  366. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  367. try {
  368. DB::beginTransaction();
  369. $time = time();
  370. Calendar::where('del_time',0)
  371. ->whereIn('id',$data['id'])
  372. ->update(['del_time' => $time]);
  373. CalendarDetails::where('del_time',0)
  374. ->where('calendar_id', $data['id'])
  375. ->update(['del_time' => $time]);
  376. DB::commit();
  377. }catch (\Exception $exception){
  378. DB::rollBack();
  379. return [false,$exception->getMessage()];
  380. }
  381. return [true, ''];
  382. }
  383. public function calendarDetail($data, $user){
  384. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  385. $customer = Calendar::where('del_time',0)
  386. ->where('id',$data['id'])
  387. ->first();
  388. if(empty($customer)) return [false,'日历设置不存在或已被删除'];
  389. $customer = $customer->toArray();
  390. $customer['time'] = ! empty($customer['time']) ? date("Y-m", $customer['time']) : "";
  391. $customer['crt_name'] = Employee::where('id',$customer['crt_id'])->value('emp_name');
  392. $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
  393. $details = $this->getDetail1($data['id']);
  394. $customer = array_merge($customer, $details);
  395. return [true, $customer];
  396. }
  397. public function calendarCommon($data,$user, $field = []){
  398. if(empty($field)) $field = Calendar::$field;
  399. $model = Calendar::where('del_time',0)
  400. ->select($field)
  401. ->orderby('id', 'desc');
  402. if(! empty($data['id'])) $model->whereIn('id', $data['id']);
  403. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  404. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  405. $model->where('crt_time','>=',$return[0]);
  406. $model->where('crt_time','<=',$return[1]);
  407. }
  408. if(! empty($data['time'][0]) && ! empty($data['time'][1])) {
  409. $return = $this->changeDateToTimeStampAboutRange($data['time']);
  410. $model->where('time','>=',$return[0]);
  411. $model->where('time','<=',$return[1]);
  412. }
  413. return $model;
  414. }
  415. public function calendarList($data,$user){
  416. $model = $this->calendarCommon($data, $user);
  417. $list = $this->limit($model,'',$data);
  418. $list = $this->fillData1($list,$user,$data);
  419. return [true, $list];
  420. }
  421. public function calendarRule(&$data, $user, $is_add = true){
  422. if(empty($data['time'])) return [false, '年月不能为空'];
  423. $data['time'] = $this->changeDateToMonth($data['time']);
  424. $res = $this->checkNumber($data['work_days'],0,'positive');
  425. if(! $res['valid']) return [false,'工作日:' . $res['error']];
  426. $res = $this->checkNumber($data['each_day_hours'],0,'positive');
  427. if(! $res['valid']) return [false,'每日工时:' . $res['error']];
  428. $res = $this->checkNumber($data['total_hours'],0,'positive');
  429. if(! $res['valid']) return [false,'总工时:' . $res['error']];
  430. if(! empty($data['details'])){
  431. foreach ($data['details'] as $key => $value){
  432. if(empty($value['time'])) return [false, '日期不能为空'];
  433. $data['details'][$key]['time'] = $this->changeDateToDate($value['time']);
  434. if(! isset($value['is_work'])) return [false, '是否工作日不能为空'];
  435. }
  436. }
  437. if($is_add){
  438. $bool = Calendar::where('time', $data['time'])
  439. ->where('del_time',0)
  440. ->exists();
  441. }else{
  442. if(empty($data['id'])) return [false,'ID不能为空'];
  443. $bool = Calendar::where('time',$data['time'])
  444. ->where('id','<>',$data['id'])
  445. ->where('del_time',0)
  446. ->exists();
  447. }
  448. if($bool) return [false, '该年月下的设置已存在'];
  449. return [true, $data];
  450. }
  451. public function fillData1($data, $user, $search){
  452. if(empty($data['data'])) return $data;
  453. $emp = (new EmployeeService())->getEmployeeMap(array_unique(array_column($data['data'],'crt_id')));
  454. foreach ($data['data'] as $key => $value){
  455. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  456. $data['data'][$key]['time'] = $value['time'] ? date('Y-m',$value['time']) : '';
  457. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  458. }
  459. return $data;
  460. }
  461. }