AssetService.php 7.9 KB

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