123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- <?php
- namespace App\Service;
- use App\Jobs\AssetDeviceJob;
- use App\Model\Asset;
- use App\Model\AssetOther;
- use App\Model\InventoryOrderAsset;
- use App\Model\Settings;
- class AssetService extends Service
- {
- public function edit($data){
- list($status,$msg) = $this->AssetRule($data,false);
- if(!$status) return [$status,$msg];
- $model = new Asset();
- $model = $model->where('id',$data['id'])->first();
- $model->name = $data['name'];
- $model->singleCode = $data['singleCode'];
- $model->assetNo = $data['assetNo'] ?? '';
- $model->version = $data['version'] ?? '';
- $model->located = $data['located'] ?? '';
- $model->startUseDate = $data['startUseDate'] ?? '';
- $model->useDept = $data['useDept'] ?? '';
- $model->userName = $data['userName'] ?? '';
- $model->assetCode = $data['assetCode'] ?? '';
- $model->assetType = $data['assetType'] ?? '';
- $model->originalValue = $data['originalValue'] ?? '';
- $model->purchaseTime = $data['purchaseTime'] ?? '';
- $model->expectedLife = $data['expectedLife'] ?? '';
- $model->isKey = $data['isKey'] ?? '';
- $model->brand = $data['brand'] ?? '';
- $model->type = $data['type'] ?? '';
- $model->remark = $data['remark'] ?? '';
- $model->kind = $data['kind'] ?? '';
- $model->gs1 = $data['gs1'] ?? '';
- $model->nextCalibrationTime = $data['nextCalibrationTime'] ?? '';
- $model->save();
- return [true,''];
- }
- public function add($data){
- list($status,$msg) = $this->AssetRule($data);
- if(!$status) return [$status,$msg];
- $model = new Asset();
- $model->name = $data['name'];
- $model->singleCode = $data['singleCode'];
- $model->assetNo = $data['assetNo'] ?? '';
- $model->version = $data['version'] ?? '';
- $model->located = $data['located'] ?? '';
- $model->startUseDate = $data['startUseDate'] ?? '';
- $model->useDept = $data['useDept'] ?? '';
- $model->userName = $data['userName'] ?? '';
- $model->assetCode = $data['assetCode'] ?? '';
- $model->assetType = $data['assetType'] ?? '';
- $model->originalValue = $data['originalValue'] ?? '';
- $model->purchaseTime = $data['purchaseTime'] ?? '';
- $model->expectedLife = $data['expectedLife'] ?? '';
- $model->isKey = $data['isKey'] ?? '';
- $model->brand = $data['brand'] ?? '';
- $model->type = $data['type'] ?? '';
- $model->remark = $data['remark'] ?? '';
- $model->kind = $data['kind'] ?? '';
- $model->gs1 = $data['gs1'] ?? '';
- $model->nextCalibrationTime = $data['nextCalibrationTime'] ?? '';
- $model->save();
- return [true,''];
- }
- public function del($data){
- if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
- AssetOther::where('id',$data['id'])->update([
- 'del_time' => time()
- ]);
- return [true,''];
- }
- public function assetList($data){
- $model = Asset::where('del_time',0)
- ->select('*')
- ->orderby('id', 'desc');
- if(! empty($data['name'])) $model->where('name', 'LIKE', '%'.$data['name'].'%');
- if(! empty($data['singleCode'])) $model->where('singleCode', 'LIKE', '%'.$data['singleCode'].'%');
- if(! empty($data['located'])) $model->where('located', 'LIKE', '%'.$data['located'].'%');
- if(! empty($data['useDept'])) $model->where('useDept', 'LIKE', '%'.$data['useDept'].'%');
- $list = $this->limit($model,'',$data);
- $list = $this->fillData($list);
- return [true,$list];
- }
- public function fillData($data){
- if(empty($data['data'])) return $data;
- foreach ($data['data'] as $key => $value){
- $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
- $data['data'][$key]['upd_time'] = $value['upd_time'] ? date('Y-m-d H:i:s',$value['upd_time']) : '';
- }
- return $data;
- }
- public function AssetRule($data, $is_check = true){
- if($this->isEmpty($data,'name')) return [false,'资产名称不能为空!'];
- if($this->isEmpty($data,'singleCode')) return [false,'资产唯一码不能为空!'];
- if($is_check){
- $bool = AssetOther::where('del_time',0)
- ->where('singleCode',$data['singleCode'])
- ->exists();
- }else{
- if($this->isEmpty($data,'id')) return [false,'数据ID不能为空!'];
- $bool = AssetOther::where('del_time',0)
- ->where('id','<>',$data['id'])
- ->where('singleCode',$data['singleCode'])
- ->exists();
- }
- if($bool) return [false,'资产唯一码不能重复'];
- return [true, ''];
- }
- public function getDepAndArea($data){
- $list = Asset::where('del_time',0)
- ->select('located','useDept')
- ->get()->toArray();
- $located = $dep = [];
- foreach ($list as $value){
- if(! empty($value['located']) && ! in_array($value['located'], $located)) $located[] = $value['located'];
- if(! empty($value['useDept']) && ! in_array($value['useDept'], $dep)) $dep[] = $value['useDept'];
- }
- return [true,['located' => $located,'dep' => $dep]];
- }
- public function updateData($data){
- list($status,$msg) = $this->rule($data);
- if(! $status) {
- file_put_contents('record_ip.txt',date("Y-m-d H:i:s",time()).json_encode($data) . PHP_EOL."来源IP".$msg.PHP_EOL,8);
- return [false, 'IP未入白名单'];
- }
- dispatch(new AssetDeviceJob($data))->onQueue(Asset::Key_Queue);
- return [true,''];
- }
- public function rule($data){
- // 获取用户的IP地址
- $userIP = $_SERVER['REMOTE_ADDR'];
- // 获取设置的IP地址
- $allowedIPs = $this->allowedIPs();
- if(empty($allowedIPs)) return [false, $userIP];
- // 校验用户IP是否在允许的范围内
- $isValidIP = false;
- foreach ($allowedIPs as $allowedIP) {
- if (strpos($allowedIP, '/') !== false) {
- // IP段表示法校验
- list($subnet, $mask) = explode('/', $allowedIP);
- if ((ip2long($userIP) & ~((1 << (32 - $mask)) - 1)) == ip2long($subnet)) {
- $isValidIP = true;
- break;
- }
- } else {
- // 单个IP地址校验
- if ($allowedIP === $userIP) {
- $isValidIP = true;
- break;
- }
- }
- }
- return [$isValidIP, $userIP];
- }
- public function allowedIPs(){
- $allowedIPs = Settings::where('name','allowedIPs')->first();
- if(empty($allowedIPs) || empty($allowedIPs->value)) return [];
- return explode(',',$allowedIPs->value);
- }
- public function assetOtherList($data){
- $model = AssetOther::where('del_time',0)
- ->select('*')
- ->orderby('id', 'desc');
- if(! empty($data['name'])) $model->where('name', 'LIKE', '%'.$data['name'].'%');
- if(! empty($data['singleCode'])) $model->where('singleCode', 'LIKE', '%'.$data['singleCode'].'%');
- if(! empty($data['located'])) $model->where('located', 'LIKE', '%'.$data['located'].'%');
- if(! empty($data['useDept'])) $model->where('useDept', 'LIKE', '%'.$data['useDept'].'%');
- $list = $this->limit($model,'',$data);
- $list = $this->fillOtherData($list);
- return [true,$list];
- }
- public function fillOtherData($data){
- if(empty($data['data'])) return $data;
- foreach ($data['data'] as $key => $value){
- $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
- $data['data'][$key]['upd_time'] = $value['upd_time'] ? date('Y-m-d H:i:s',$value['upd_time']) : '';
- }
- return $data;
- }
- }
|