AssetService.php 6.6 KB

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