EquipmentService.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. namespace App\Service;
  3. use App\Model\Depart;
  4. use App\Model\DeviceOrderInfo;
  5. use App\Model\Equipment;
  6. use App\Model\EquipmentXj;
  7. use Illuminate\Support\Facades\DB;
  8. /**
  9. * 设备档案
  10. * @package App\Models
  11. */
  12. class EquipmentService extends Service
  13. {
  14. /**
  15. * 设备编辑
  16. * @param $data
  17. * @return array
  18. */
  19. public function equipmentEdit($data){
  20. list($status,$msg) = $this->equipmentRule($data,false);
  21. if(!$status) return [$status,$msg];
  22. $time = time();
  23. try {
  24. DB::beginTransaction();
  25. $update = $msg['data'][0];
  26. Equipment::where('id',$data['id'])->update($update);
  27. EquipmentXj::where('equipment_id',$data['id'])
  28. ->where('del_time',0)
  29. ->update(['del_time' => $time]);
  30. foreach ($msg['time_arr'] as $key => $value){
  31. $msg['time_arr'][$key]['equipment_id'] = $data['id'];
  32. $msg['time_arr'][$key]['crt_time'] = $time;
  33. $msg['time_arr'][$key]['upd_time'] = $time;
  34. }
  35. EquipmentXj::insert($msg['time_arr']);
  36. DB::commit();
  37. }catch (\Throwable $exception){
  38. DB::rollBack();
  39. return [false, $exception->getMessage()];
  40. }
  41. return [true,'保存成功!'];
  42. }
  43. /**
  44. * 设备新增
  45. * @param $data
  46. * @return array
  47. */
  48. public function equipmentAdd($data){
  49. list($status,$msg) = $this->equipmentRule($data);
  50. if(!$status) return [$status,$msg];
  51. $time = time();
  52. try {
  53. DB::beginTransaction();
  54. foreach ($msg['data'] as $key => $value){
  55. $msg['data'][$key]['crt_time'] = $time;
  56. $msg['data'][$key]['upd_time'] = $time;
  57. }
  58. Equipment::insert($msg['data']);
  59. //获取上一次插入订单的所有id
  60. $last_insert_id = Equipment::where('crt_time',$time)->select('id')->get()->toArray();
  61. $last_insert_id = array_column($last_insert_id,'id');
  62. foreach ($msg['time_arr'] as $key => $value){
  63. $msg['time_arr'][$key]['equipment_id'] = $last_insert_id[$key];
  64. $msg['time_arr'][$key]['crt_time'] = $time;
  65. $msg['time_arr'][$key]['upd_time'] = $time;
  66. }
  67. EquipmentXj::insert($msg['time_arr']);
  68. DB::commit();
  69. }catch (\Throwable $exception){
  70. DB::rollBack();
  71. return [false, $exception->getMessage()];
  72. }
  73. return [true,'保存成功!'];
  74. }
  75. /**
  76. * 设备删除
  77. * @param $data
  78. * @return array
  79. */
  80. public function equipmentDel($data){
  81. if($this->isEmpty($data,'id')) return [false,'ID必须!'];
  82. $device_bool = DeviceOrderInfo::where('del_time',0)
  83. ->whereIn('equipment_id',$data['id'])
  84. ->exists();
  85. if($device_bool) return [false, '设备已生成巡检、点检或维修单,删除失败'];
  86. Equipment::whereIn('id',$data['id'])->update([
  87. 'del_time' => time()
  88. ]);
  89. EquipmentXj::whereIn('equipment_id',$data['id'])->update([
  90. 'del_time' => time()
  91. ]);
  92. return [true,'删除成功'];
  93. }
  94. /**
  95. * 设备列表
  96. * @param $data
  97. * @return array
  98. */
  99. public function equipmentList($data){
  100. $model = Equipment::where('del_time',0)
  101. ->select('*');
  102. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  103. if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
  104. $list = $this->limit($model,'',$data);
  105. //组织数据
  106. $list = $this->fillData($list);
  107. return [true, $list];
  108. }
  109. public function fillData($data){
  110. if (empty($data['data'])) return $data;
  111. $depart_map = Depart::whereIn('id',array_unique(array_column($data['data'],'depart_id')))
  112. ->pluck('title','id')
  113. ->toArray();
  114. // 设备最大一次的巡检的时间 设置的首次巡检
  115. list($equipment_map,$equipment_first_map) = (new InspectService())->getXJLastData(array_column($data['data'],'id'));
  116. $day = date('Y-m-d');
  117. foreach ($data['data'] as $key => $value){
  118. $data['data'][$key]['depart_title'] = $depart_map[$value['depart_id']] ?? '';
  119. $first_time = $equipment_first_map[$value['id']] ?? 0;
  120. $data['data'][$key]['first_time'] = $first_time ? date('Y-m-d', $first_time) : '';
  121. $data['data'][$key]['maintenance_expire_time'] = $value['maintenance_expire_time'] ? date('Y-m-d', $value['maintenance_expire_time']) : '';
  122. $next_time = 0;
  123. $detail_time = $equipment_map[$value['id']] ?? 0;
  124. if($detail_time > 0 && $value['check_cycle'] > 0) $next_time = strtotime("+{$value['check_cycle']} days", $detail_time);
  125. if($day > date('Y-m-d', $next_time)){
  126. $data['data'][$key]['next_time'] = "暂无巡检计划";
  127. }else{
  128. $data['data'][$key]['next_time'] = $next_time ? date('Y-m-d', $next_time) : "暂无巡检计划";
  129. }
  130. }
  131. return $data;
  132. }
  133. /**
  134. * 设备规则
  135. * @param $data
  136. * @param bool $is_add
  137. * @return array
  138. */
  139. public function equipmentRule($data,$is_add = true){
  140. if($this->isEmpty($data,'data')) return [false,'数据不能为空!'];
  141. $code = array_column($data['data'],'code');
  142. $code = array_map(function($val) {
  143. return $val !== null ? $val : 0;
  144. }, $code);
  145. $code_count = array_count_values($code);
  146. foreach ($code as $value){
  147. if(empty($value)) return [false,'编码不能为空!'];
  148. if($code_count[$value] > 1) return [false,'编码不能重复'];
  149. }
  150. $title = array_column($data['data'],'title');
  151. $title = array_map(function($val) {
  152. return $val !== null ? $val : 0;
  153. }, $title);
  154. $title_count = array_count_values($title);
  155. foreach ($title as $value){
  156. if(empty($value)) return [false,'名称不能为空!'];
  157. if($title_count[$value] > 1) return [false,'名称不能重复'];
  158. }
  159. $timestamp = strtotime(date('Y-m-d 00:00:00'));
  160. $return = [];
  161. foreach ($data['data'] as $key => $value){
  162. if(isset($value['check_cycle']) && isset($value['first_time'])){
  163. if(! empty($value['check_cycle']) && ! is_numeric($value['check_cycle'])) return [false, '设备巡检周期请输入数字'];
  164. if(! empty($value['first_time']) && ! is_numeric($value['first_time'])) return [false, '首次巡检时间格式错误'];
  165. if($value['check_cycle'] >= 0 && $value['first_time'] < $timestamp){
  166. return [false, '首次巡检时间请选择大于等于今天的日期'];
  167. }
  168. }
  169. $return[$key]['first_time'] = ! empty($value['first_time']) ? $value['first_time'] : 0;
  170. if(array_key_exists('first_time', $value)) unset($data['data'][$key]['first_time']);
  171. $search = "(title = '{$value['title']}' OR code = '{$value['code']}')";
  172. if($is_add){
  173. $bool = Equipment::whereRaw($search)
  174. ->where('del_time',0)
  175. ->exists();
  176. }else{
  177. if($this->isEmpty($data,'id')) return [false,'id不能为空!'];
  178. $bool = Equipment::whereRaw($search)
  179. ->where('id','<>',$data['id'])
  180. ->where('del_time',0)
  181. ->exists();
  182. }
  183. if($bool) return [false,'名称和编码不能重复'];
  184. }
  185. $data['time_arr'] = $return;
  186. return [true, $data];
  187. }
  188. public function getDeviceList($index = "title", $value = "code"){
  189. $deviceList = Equipment::where('del_time',0)
  190. ->where('model','<>',1)
  191. ->select('id','title','code')
  192. ->get()->toArray();
  193. return array_column($deviceList,$value,$index);
  194. }
  195. }