ItemService.php 20 KB

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