123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233 |
- <?php
- namespace App\Service;
- use App\Model\Depart;
- use App\Model\DeviceOrderInfo;
- use App\Model\Equipment;
- use App\Model\EquipmentXj;
- use Illuminate\Support\Facades\DB;
- /**
- * 设备档案
- * @package App\Models
- */
- class EquipmentService extends Service
- {
- /**
- * 设备编辑
- * @param $data
- * @return array
- */
- public function equipmentEdit($data){
- list($status,$msg) = $this->equipmentRule($data,false);
- if(!$status) return [$status,$msg];
- $time = time();
- try {
- DB::beginTransaction();
- $update = $msg['data'][0];
- Equipment::where('id',$data['id'])->update($update);
- EquipmentXj::where('equipment_id',$data['id'])
- ->where('del_time',0)
- ->update(['del_time' => $time]);
- foreach ($msg['time_arr'] as $key => $value){
- $msg['time_arr'][$key]['equipment_id'] = $data['id'];
- $msg['time_arr'][$key]['crt_time'] = $time;
- $msg['time_arr'][$key]['upd_time'] = $time;
- }
- EquipmentXj::insert($msg['time_arr']);
- DB::commit();
- }catch (\Throwable $exception){
- DB::rollBack();
- return [false, $exception->getMessage()];
- }
- return [true,'保存成功!'];
- }
- /**
- * 设备新增
- * @param $data
- * @return array
- */
- public function equipmentAdd($data){
- list($status,$msg) = $this->equipmentRule($data);
- if(!$status) return [$status,$msg];
- $time = time();
- try {
- DB::beginTransaction();
- foreach ($msg['data'] as $key => $value){
- $msg['data'][$key]['crt_time'] = $time;
- $msg['data'][$key]['upd_time'] = $time;
- }
- Equipment::insert($msg['data']);
- //获取上一次插入订单的所有id
- $last_insert_id = Equipment::where('crt_time',$time)->select('id')->get()->toArray();
- $last_insert_id = array_column($last_insert_id,'id');
- foreach ($msg['time_arr'] as $key => $value){
- $msg['time_arr'][$key]['equipment_id'] = $last_insert_id[$key];
- $msg['time_arr'][$key]['crt_time'] = $time;
- $msg['time_arr'][$key]['upd_time'] = $time;
- }
- EquipmentXj::insert($msg['time_arr']);
- DB::commit();
- }catch (\Throwable $exception){
- DB::rollBack();
- return [false, $exception->getMessage()];
- }
- return [true,'保存成功!'];
- }
- /**
- * 设备删除
- * @param $data
- * @return array
- */
- public function equipmentDel($data){
- if($this->isEmpty($data,'id')) return [false,'ID必须!'];
- $device_bool = DeviceOrderInfo::where('del_time',0)
- ->whereIn('equipment_id',$data['id'])
- ->exists();
- if($device_bool) return [false, '设备已生成巡检、点检或维修单,删除失败'];
- Equipment::whereIn('id',$data['id'])->update([
- 'del_time' => time()
- ]);
- EquipmentXj::whereIn('equipment_id',$data['id'])->update([
- 'del_time' => time()
- ]);
- return [true,'删除成功'];
- }
- /**
- * 设备列表
- * @param $data
- * @return array
- */
- public function equipmentList($data){
- $model = Equipment::where('del_time',0)
- ->select('*');
- if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
- if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
- $list = $this->limit($model,'',$data);
- //组织数据
- $list = $this->fillData($list);
- return [true, $list];
- }
- public function fillData($data){
- if (empty($data['data'])) return $data;
- $depart_map = Depart::whereIn('id',array_unique(array_column($data['data'],'depart_id')))
- ->pluck('title','id')
- ->toArray();
- // 设备最大一次的巡检的时间 设置的首次巡检
- list($equipment_map,$equipment_first_map) = (new InspectService())->getXJLastData(array_column($data['data'],'id'));
- $day = date('Y-m-d');
- foreach ($data['data'] as $key => $value){
- $data['data'][$key]['depart_title'] = $depart_map[$value['depart_id']] ?? '';
- $first_time = $equipment_first_map[$value['id']] ?? 0;
- $data['data'][$key]['first_time'] = $first_time ? date('Y-m-d', $first_time) : '';
- $data['data'][$key]['maintenance_expire_time'] = $value['maintenance_expire_time'] ? date('Y-m-d', $value['maintenance_expire_time']) : '';
- $next_time = 0;
- $detail_time = $equipment_map[$value['id']] ?? 0;
- if($detail_time > 0 && $value['check_cycle'] > 0) $next_time = strtotime("+{$value['check_cycle']} days", $detail_time);
- if($day > date('Y-m-d', $next_time)){
- $data['data'][$key]['next_time'] = "暂无巡检计划";
- }else{
- $data['data'][$key]['next_time'] = $next_time ? date('Y-m-d', $next_time) : "暂无巡检计划";
- }
- }
- return $data;
- }
- /**
- * 设备规则
- * @param $data
- * @param bool $is_add
- * @return array
- */
- public function equipmentRule($data,$is_add = true){
- if($this->isEmpty($data,'data')) return [false,'数据不能为空!'];
- $code = array_column($data['data'],'code');
- $code = array_map(function($val) {
- return $val !== null ? $val : 0;
- }, $code);
- $code_count = array_count_values($code);
- foreach ($code as $value){
- if(empty($value)) return [false,'编码不能为空!'];
- if($code_count[$value] > 1) return [false,'编码不能重复'];
- }
- $title = array_column($data['data'],'title');
- $title = array_map(function($val) {
- return $val !== null ? $val : 0;
- }, $title);
- $title_count = array_count_values($title);
- foreach ($title as $value){
- if(empty($value)) return [false,'名称不能为空!'];
- if($title_count[$value] > 1) return [false,'名称不能重复'];
- }
- $timestamp = strtotime(date('Y-m-d 00:00:00'));
- $return = [];
- foreach ($data['data'] as $key => $value){
- if(isset($value['check_cycle']) && isset($value['first_time'])){
- if(! empty($value['check_cycle']) && ! is_numeric($value['check_cycle'])) return [false, '设备巡检周期请输入数字'];
- if(! empty($value['first_time']) && ! is_numeric($value['first_time'])) return [false, '首次巡检时间格式错误'];
- if($value['check_cycle'] >= 0 && $value['first_time'] < $timestamp){
- return [false, '首次巡检时间请选择大于等于今天的日期'];
- }
- }
- $return[$key]['first_time'] = ! empty($value['first_time']) ? $value['first_time'] : 0;
- if(array_key_exists('first_time', $value)) unset($data['data'][$key]['first_time']);
- $search = "(title = '{$value['title']}' OR code = '{$value['code']}')";
- if($is_add){
- $bool = Equipment::whereRaw($search)
- ->where('del_time',0)
- ->exists();
- }else{
- if($this->isEmpty($data,'id')) return [false,'id不能为空!'];
- $bool = Equipment::whereRaw($search)
- ->where('id','<>',$data['id'])
- ->where('del_time',0)
- ->exists();
- }
- if($bool) return [false,'名称和编码不能重复'];
- }
- $data['time_arr'] = $return;
- return [true, $data];
- }
- public function getDeviceList($index = "title", $value = "code"){
- $deviceList = Equipment::where('del_time',0)
- ->where('model','<>',1)
- ->select('id','title','code')
- ->get()->toArray();
- return array_column($deviceList,$value,$index);
- }
- }
|