AssetService.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. namespace App\Service;
  3. use App\Jobs\AssetDeviceJob;
  4. use App\Model\Asset;
  5. use App\Model\InventoryOrder;
  6. use App\Model\InventoryOrderAsset;
  7. use App\Model\Settings;
  8. use Illuminate\Support\Facades\Redis;
  9. class AssetService extends Service
  10. {
  11. public function edit($data){
  12. list($status,$msg) = $this->AssetRule($data,false);
  13. if(!$status) return [$status,$msg];
  14. $model = new Asset();
  15. $model = $model->where('id',$data['id'])->first();
  16. $model->name = $data['name'];
  17. $model->singleCode = $data['singleCode'];
  18. $model->assetNo = $data['assetNo'] ?? '';
  19. $model->version = $data['version'] ?? '';
  20. $model->located = $data['located'] ?? '';
  21. $model->startUseDate = $data['startUseDate'] ?? '';
  22. $model->useDept = $data['useDept'] ?? '';
  23. $model->userName = $data['userName'] ?? '';
  24. $model->assetCode = $data['assetCode'] ?? '';
  25. $model->assetType = $data['assetType'] ?? '';
  26. $model->originalValue = $data['originalValue'] ?? '';
  27. $model->purchaseTime = $data['purchaseTime'] ?? '';
  28. $model->expectedLife = $data['expectedLife'] ?? '';
  29. $model->isKey = $data['isKey'] ?? '';
  30. $model->brand = $data['brand'] ?? '';
  31. $model->type = $data['type'] ?? '';
  32. $model->remark = $data['remark'] ?? '';
  33. $model->kind = $data['kind'] ?? '';
  34. $model->gs1 = $data['gs1'] ?? '';
  35. $model->nextCalibrationTime = $data['nextCalibrationTime'] ?? '';
  36. $model->save();
  37. return [true,''];
  38. }
  39. public function add($data){
  40. list($status,$msg) = $this->AssetRule($data);
  41. if(!$status) return [$status,$msg];
  42. $model = new Asset();
  43. $model->name = $data['name'];
  44. $model->singleCode = $data['singleCode'];
  45. $model->assetNo = $data['assetNo'] ?? '';
  46. $model->version = $data['version'] ?? '';
  47. $model->located = $data['located'] ?? '';
  48. $model->startUseDate = $data['startUseDate'] ?? '';
  49. $model->useDept = $data['useDept'] ?? '';
  50. $model->userName = $data['userName'] ?? '';
  51. $model->assetCode = $data['assetCode'] ?? '';
  52. $model->assetType = $data['assetType'] ?? '';
  53. $model->originalValue = $data['originalValue'] ?? '';
  54. $model->purchaseTime = $data['purchaseTime'] ?? '';
  55. $model->expectedLife = $data['expectedLife'] ?? '';
  56. $model->isKey = $data['isKey'] ?? '';
  57. $model->brand = $data['brand'] ?? '';
  58. $model->type = $data['type'] ?? '';
  59. $model->remark = $data['remark'] ?? '';
  60. $model->kind = $data['kind'] ?? '';
  61. $model->gs1 = $data['gs1'] ?? '';
  62. $model->nextCalibrationTime = $data['nextCalibrationTime'] ?? '';
  63. $model->save();
  64. return [true,''];
  65. }
  66. public function del($data){
  67. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  68. $bool = InventoryOrderAsset::where('del_time',0)
  69. ->where('asset_id',$data['id'])
  70. ->exists();
  71. if($bool) return [false,'资产已录入盘点单,删除失败!'];
  72. Asset::where('id',$data['id'])->update([
  73. 'del_time' => time()
  74. ]);
  75. return [true,''];
  76. }
  77. public function assetList($data){
  78. $model = Asset::where('del_time',0)
  79. ->select('*')
  80. ->orderby('id', 'desc');
  81. if(! empty($data['name'])) $model->where('name', 'LIKE', '%'.$data['name'].'%');
  82. if(! empty($data['singleCode'])) $model->where('singleCode', 'LIKE', '%'.$data['singleCode'].'%');
  83. if(! empty($data['located'])) $model->where('located', 'LIKE', '%'.$data['located'].'%');
  84. if(! empty($data['useDept'])) $model->where('useDept', 'LIKE', '%'.$data['useDept'].'%');
  85. $list = $this->limit($model,'',$data);
  86. return [true,$list];
  87. }
  88. public function AssetRule($data, $is_check = true){
  89. if($this->isEmpty($data,'name')) return [false,'资产名称不能为空!'];
  90. if($this->isEmpty($data,'singleCode')) return [false,'资产唯一码不能为空!'];
  91. if($is_check){
  92. $bool = Asset::where('del_time',0)
  93. ->where('singleCode',$data['singleCode'])
  94. ->exists();
  95. }else{
  96. if($this->isEmpty($data,'id')) return [false,'数据ID不能为空!'];
  97. $bool = Asset::where('del_time',0)
  98. ->where('id','<>',$data['id'])
  99. ->where('singleCode',$data['singleCode'])
  100. ->exists();
  101. }
  102. if($bool) return [false,'资产唯一码不能重复'];
  103. return [true, ''];
  104. }
  105. public function getDepAndArea($data){
  106. $list = Asset::where('del_time',0)
  107. ->select('located','useDept')
  108. ->get()->toArray();
  109. $located = $dep = [];
  110. foreach ($list as $value){
  111. if(! in_array($value['located'], $located)) $located[] = $value['located'];
  112. if(! in_array($value['useDept'], $dep)) $dep[] = $value['useDept'];
  113. }
  114. return [true,['located' => $located,'dep' => $dep]];
  115. }
  116. public function updateData($data){
  117. list($status,$msg) = $this->rule($data);
  118. if(! $status) return [false, 'IP未入白名单'];
  119. dispatch(new AssetDeviceJob($data))->onQueue(Asset::Key_Queue);
  120. return [true,''];
  121. }
  122. public function rule($data){
  123. // 获取用户的IP地址
  124. $userIP = $_SERVER['REMOTE_ADDR'];
  125. // 获取设置的IP地址
  126. $allowedIPs = $this->allowedIPs();
  127. if(empty($allowedIPs)) return [false, $userIP];
  128. // 校验用户IP是否在允许的范围内
  129. $isValidIP = false;
  130. foreach ($allowedIPs as $allowedIP) {
  131. if (strpos($allowedIP, '/') !== false) {
  132. // IP段表示法校验
  133. list($subnet, $mask) = explode('/', $allowedIP);
  134. if ((ip2long($userIP) & ~((1 << (32 - $mask)) - 1)) == ip2long($subnet)) {
  135. $isValidIP = true;
  136. break;
  137. }
  138. } else {
  139. // 单个IP地址校验
  140. if ($allowedIP === $userIP) {
  141. $isValidIP = true;
  142. break;
  143. }
  144. }
  145. }
  146. return [$isValidIP, $userIP];
  147. }
  148. public function allowedIPs(){
  149. $allowedIPs = Settings::where('name','allowedIPs')->first();
  150. if(empty($allowedIPs) || empty($allowedIPs->value)) return [];
  151. return explode(',',$allowedIPs->value);
  152. }
  153. }