AssetDeviceJob.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. namespace App\Jobs;
  3. use App\Model\Asset;
  4. use App\Model\AssetTemp;
  5. use App\Service\InOutOptionService;
  6. use Illuminate\Bus\Queueable;
  7. use Illuminate\Contracts\Queue\ShouldQueue;
  8. use Illuminate\Foundation\Bus\Dispatchable;
  9. use Illuminate\Queue\InteractsWithQueue;
  10. use Illuminate\Queue\SerializesModels;
  11. use Illuminate\Support\Facades\DB;
  12. use Illuminate\Support\Facades\Redis;
  13. use Symfony\Component\Console\Output\ConsoleOutput;
  14. use Symfony\Component\Console\Output\OutputInterface;
  15. class AssetDeviceJob implements ShouldQueue
  16. {
  17. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  18. protected $data;
  19. public $timeout = 1800;
  20. /**
  21. * Create a new job instance
  22. *
  23. * @return void
  24. */
  25. public function __construct($data)
  26. {
  27. $this->data = $data;
  28. }
  29. /**
  30. * Execute the job.
  31. *
  32. * @return void
  33. */
  34. public function handle()
  35. {
  36. try{
  37. DB::beginTransaction();
  38. $this->settle();
  39. DB::commit();
  40. }catch (\Exception $exception){
  41. DB::rollBack();
  42. file_put_contents('record_error.txt',date("Y-m-d H:i:s",time()).json_encode($this->data) . PHP_EOL.$exception->getMessage()."line" . $exception->getLine().$exception->getFile().PHP_EOL,8);
  43. }
  44. }
  45. public function settle(){
  46. $data = $this->data;
  47. if(empty($data)){
  48. file_put_contents('record_data.txt',date("Y-m-d H:i:s",time())."原数据:".json_encode($data).PHP_EOL,8);
  49. return;
  50. }
  51. if(empty($data['assetOas'])) {
  52. file_put_contents('record_data.txt',date("Y-m-d H:i:s",time())."数据结构错误".PHP_EOL,8);
  53. return;
  54. }
  55. //内存设置
  56. ini_set('memory_limit', -1);
  57. //软删除
  58. $time = time();
  59. Asset::where('del_time',0)
  60. ->where('crt_time','<', strtotime(date('Y-m-d 00:00:00')))
  61. ->update(['del_time' => $time]);
  62. $this->assetTempInsert();
  63. echo 'TEMP OVER-------------------'. PHP_EOL;;
  64. $this->assetInsert();
  65. echo 'OVER-------------------';
  66. AssetTemp::truncate();
  67. }
  68. //临时表数据插入
  69. public function assetTempInsert(){
  70. $data = $this->data;
  71. $batchSize = 100;
  72. $totalAssets = count($data['assetOas']);
  73. for ($i = 0; $i < $totalAssets; $i += $batchSize) {
  74. $batch = array_slice($data['assetOas'], $i, $batchSize);
  75. $insert = [];
  76. // 对每个批次的数据进行处理
  77. foreach ($batch as $value) {
  78. if(empty($value['singleCode'])) continue;
  79. $insert[] = [
  80. "assetCode" => $value['assetCode'] ?? "",
  81. "assetNo" => $value['assetNo'] ?? "",
  82. "assetType" => $value['assetType'] ?? "",
  83. "brand" => $value['brand'] ?? "",
  84. "expectedLife" => $value['expectedLife'] ?? "",
  85. "gs1" => $value['gs1'] ?? "",
  86. "isKey" => $value['isKey'] ?? "",
  87. "kind" => $value['kind'] ?? "",
  88. "located" => $value['located'] ?? "",
  89. "name" => $value['name'] ?? "",
  90. "nextCalibrationTime" => $value['nextCalibrationTime'] ?? "",
  91. "originalValue" => $value['originalValue'] ?? "",
  92. "purchaseTime" => $value['purchaseTime'] ?? "",
  93. "remark" => $value['remark'] ?? "",
  94. "singleCode" => $value['singleCode'] ?? "",
  95. "startUseDate" => $value['startUseDate'] ?? "",
  96. "type" => $value['type'] ?? "",
  97. "useDept" => $value['useDept'] ?? "",
  98. "userName" => $value['userName'] ?? "",
  99. "version" => $value['version'] ?? "",
  100. ];
  101. }
  102. // 在处理完每个批次后,进行一些内存清理操作
  103. unset($batch);
  104. if(! empty($insert)) {
  105. echo "insert Temp " . PHP_EOL;
  106. AssetTemp::insert($insert);
  107. }
  108. }
  109. }
  110. //正式数据插入或更新
  111. public function assetInsert(){
  112. DB::table('asset_temp')
  113. ->select('*')
  114. ->orderBy('id','desc')
  115. ->chunk(500,function ($data){
  116. $data_array = $data->toArray();
  117. $time = time();
  118. $update = $insert = [];
  119. foreach ($data_array as $value){
  120. $bool = Asset::where('singleCode',$value->singleCode)->exists();
  121. if($bool){
  122. $update[] = [
  123. "assetCode" => $value->assetCode ?? "",
  124. "assetNo" => $value->assetNo ?? "",
  125. "assetType" => $value->assetType ?? "",
  126. "brand" => $value->brand ?? "",
  127. "expectedLife" => $value->expectedLife ?? "",
  128. "gs1" => $value->gs1 ?? "",
  129. "isKey" => $value->isKey ?? "",
  130. "kind" => $value->kind ?? "",
  131. "located" => $value->located ?? "",
  132. "name" => $value->name ?? "",
  133. "nextCalibrationTime" => $value->nextCalibrationTime ?? "",
  134. "originalValue" => $value->originalValue ?? "",
  135. "purchaseTime" => $value->purchaseTime ?? "",
  136. "remark" => $value->remark ?? "",
  137. "singleCode" => $value->singleCode ?? "",
  138. "startUseDate" => $value->startUseDate ?? "",
  139. "type" => $value->type ?? "",
  140. "useDept" => $value->useDept ?? "",
  141. "userName" => $value->userName ?? "",
  142. "version" => $value->version ?? "",
  143. // "crt_time" => $time,
  144. "del_time" => 0,
  145. ];
  146. }else{
  147. $insert[] = [
  148. "assetCode" => $value->assetCode ?? "",
  149. "assetNo" => $value->assetNo ?? "",
  150. "assetType" => $value->assetType ?? "",
  151. "brand" => $value->brand ?? "",
  152. "expectedLife" => $value->expectedLife ?? "",
  153. "gs1" => $value->gs1 ?? "",
  154. "isKey" => $value->isKey ?? "",
  155. "kind" => $value->kind ?? "",
  156. "located" => $value->located ?? "",
  157. "name" => $value->name ?? "",
  158. "nextCalibrationTime" => $value->nextCalibrationTime ?? "",
  159. "originalValue" => $value->originalValue ?? "",
  160. "purchaseTime" => $value->purchaseTime ?? "",
  161. "remark" => $value->remark ?? "",
  162. "singleCode" => $value->singleCode ?? "",
  163. "startUseDate" => $value->startUseDate ?? "",
  164. "type" => $value->type ?? "",
  165. "useDept" => $value->useDept ?? "",
  166. "userName" => $value->userName ?? "",
  167. "version" => $value->version ?? "",
  168. "crt_time" => $time,
  169. "upd_time" => $time,
  170. ];
  171. }
  172. }
  173. if(! empty($update)){
  174. foreach ($update as $value){
  175. Asset::where('singleCode',$value['singleCode'])->update($value);
  176. }
  177. }
  178. if(! empty($insert)) Asset::insert($insert);
  179. echo "insert OR update ing" . PHP_EOL;
  180. });
  181. }
  182. protected function echoMessage(OutputInterface $output)
  183. {
  184. $output->writeln($this->data);
  185. }
  186. }