DeviceService.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. namespace App\Service;
  3. use App\Model\Employee;
  4. use App\Model\Device;
  5. use App\Model\DeviceDetails;
  6. use Illuminate\Support\Facades\DB;
  7. class DeviceService extends Service
  8. {
  9. public function deviceEdit($data,$user){
  10. list($status,$msg) = $this->deviceRule($data, $user, false);
  11. if(!$status) return [$status,$msg];
  12. try {
  13. DB::beginTransaction();
  14. $model = Device::where('id',$data['id'])->first();
  15. $model->code = $data['code'] ?? '';
  16. $model->title = $data['title'] ?? '';
  17. $model->size = $data['size'] ?? '';
  18. $model->mark = $data['mark'] ?? '';
  19. $model->is_use = $data['is_use'] ?? 0;
  20. $model->save();
  21. $time = time();
  22. DeviceDetails::where('del_time',0)
  23. ->where('device_id', $model->id)
  24. ->update(['del_time' => $time]);
  25. $this->saveDetail($model->id, $time, $data);
  26. DB::commit();
  27. }catch (\Exception $exception){
  28. DB::rollBack();
  29. return [false,$exception->getMessage()];
  30. }
  31. return [true, ''];
  32. }
  33. public function deviceAdd($data,$user){
  34. list($status,$msg) = $this->deviceRule($data, $user);
  35. if(!$status) return [$status,$msg];
  36. try {
  37. DB::beginTransaction();
  38. $model = new Device();
  39. $model->code = $data['code'] ?? '';
  40. $model->title = $data['title'] ?? '';
  41. $model->size = $data['size'] ?? '';
  42. $model->mark = $data['mark'] ?? '';
  43. $model->is_use = $data['is_use'] ?? 0;
  44. $model->crt_id = $user['id'];
  45. $model->save();
  46. $this->saveDetail($model->id, time(), $data);
  47. DB::commit();
  48. }catch (\Exception $exception){
  49. DB::rollBack();
  50. return [false,$exception->getMessage()];
  51. }
  52. return [true, ''];
  53. }
  54. private function saveDetail($id, $time, $data){
  55. if(! empty($data['details'])){
  56. $unit = [];
  57. foreach ($data['details'] as $value){
  58. $unit[] = [
  59. 'device_id' => $id,
  60. 'time' => $value['time'],
  61. 'total_hours' => $value['total_hours'],
  62. 'crt_time' => $time,
  63. ];
  64. }
  65. if(! empty($unit)) DeviceDetails::insert($unit);
  66. }
  67. }
  68. private function getDetail($id){
  69. $data = DeviceDetails::where('del_time',0)
  70. ->where('device_id', $id)
  71. ->get()->toArray();
  72. $unit = [];
  73. foreach ($data as $value){
  74. $unit[] = [
  75. 'time' => date("Y-m",$value['time']),
  76. 'total_hours' => $value['total_hours'],
  77. ];
  78. }
  79. $detail = [
  80. 'details' => $unit,
  81. ];
  82. foreach ($detail as $key => $value) {
  83. if (empty($value)) {
  84. $detail[$key] = (object)[]; // 转成 stdClass 对象
  85. }
  86. }
  87. return $detail;
  88. }
  89. public function deviceDel($data){
  90. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  91. try {
  92. DB::beginTransaction();
  93. $time = time();
  94. Device::where('del_time',0)
  95. ->whereIn('id',$data['id'])
  96. ->update(['del_time' => $time]);
  97. DeviceDetails::where('del_time',0)
  98. ->where('device_id', $data['id'])
  99. ->update(['del_time' => $time]);
  100. DB::commit();
  101. }catch (\Exception $exception){
  102. DB::rollBack();
  103. return [false,$exception->getMessage()];
  104. }
  105. return [true, ''];
  106. }
  107. public function deviceDetail($data, $user){
  108. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  109. $customer = Device::where('del_time',0)
  110. ->where('id',$data['id'])
  111. ->first();
  112. if(empty($customer)) return [false,'设备不存在或已被删除'];
  113. $customer = $customer->toArray();
  114. $customer['crt_name'] = Employee::where('id',$customer['crt_id'])->value('emp_name');
  115. $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
  116. $details = $this->getDetail($data['id']);
  117. $customer = array_merge($customer, $details);
  118. return [true, $customer];
  119. }
  120. public function deviceCommon($data,$user, $field = []){
  121. if(empty($field)) $field = Device::$field;
  122. $model = Device::where('del_time',0)
  123. ->select($field)
  124. ->orderby('id', 'desc');
  125. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  126. if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
  127. if(! empty($data['id'])) $model->whereIn('id', $data['id']);
  128. if(! empty($data['is_use'])) $model->where('is_use', $data['is_use']);
  129. if(! empty($data['crt_id'])) $model->whereIn('crt_id', $data['crt_id']);
  130. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  131. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  132. $model->where('crt_time','>=',$return[0]);
  133. $model->where('crt_time','<=',$return[1]);
  134. }
  135. return $model;
  136. }
  137. public function deviceList($data,$user){
  138. $model = $this->deviceCommon($data, $user);
  139. $list = $this->limit($model,'',$data);
  140. $list = $this->fillData($list);
  141. return [true, $list];
  142. }
  143. public function deviceRule(&$data, $user, $is_add = true){
  144. if(empty($data['code'])) return [false, '编码不能为空'];
  145. if(empty($data['title'])) return [false, '名称不能为空'];
  146. if(empty($data['is_use'])) return [false, '是否启用不能为空'];
  147. if(! isset(Device::Use[$data['is_use']])) return [false, '是否启用错误'];
  148. if(! empty($data['details'])){
  149. list($status, $msg) = $this->checkArrayRepeat($data['details'],'time','设备年月');
  150. if(! $status) return [false, $msg];
  151. foreach ($data['details'] as $key => $value){
  152. if(empty($value['time'])) return [false, '设备年月不能为空'];
  153. $data['details'][$key]['time'] = $this->changeDateToMonth($value['time']);
  154. $res = $this->checkNumber($value['total_hours'],0,'positive');
  155. if(! $res['valid']) return [false,'总工时:' . $res['error']];
  156. }
  157. }
  158. if($is_add){
  159. $bool = Device::where('code',$data['code'])
  160. ->where('crt_id', $user['id'])
  161. ->where('del_time',0)
  162. ->exists();
  163. }else{
  164. if(empty($data['id'])) return [false,'ID不能为空'];
  165. $bool = Device::where('code',$data['code'])
  166. ->where('crt_id', $user['id'])
  167. ->where('id','<>',$data['id'])
  168. ->where('del_time',0)
  169. ->exists();
  170. }
  171. if($bool) return [false, '编码已存在'];
  172. return [true, $data];
  173. }
  174. public function fillData($data){
  175. if(empty($data['data'])) return $data;
  176. $emp = (new EmployeeService())->getEmployeeMap(array_unique(array_column($data['data'],'crt_id')));
  177. foreach ($data['data'] as $key => $value){
  178. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  179. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  180. $data['data'][$key]['is_use_title'] = Device::Use[$value['is_use']] ?? '';
  181. }
  182. return $data;
  183. }
  184. }