ItemService.php 19 KB

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