sysGood.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\command;
  4. use app\admin\model\GoodBasic;
  5. use app\admin\model\GoodCombind;use app\admin\model\GoodZixun;
  6. use app\user\model\TaxRelation;
  7. use think\console\Command;
  8. use think\console\Input;
  9. use think\console\input\Argument;
  10. use think\console\input\Option;
  11. use think\console\Output;
  12. use think\facade\Cache;
  13. class sysGood extends Command
  14. {
  15. protected $date;
  16. protected function configure()
  17. {
  18. // 指令配置
  19. $this->setName('sysgood')
  20. ->setDescription('the sysgood command');
  21. }
  22. protected function execute(Input $input, Output $output)
  23. {
  24. $this->date = date('Y-m-d H:i:s', time() - 3600);
  25. // $this->date = "2024-08-05 00:00:00";
  26. $goodset =Cache::store('redis')->get('goodSet');
  27. if($goodset ==1) return;
  28. Cache::store('redis')->set('goodSet',1,1800);
  29. try {
  30. $this->processGoods('goodBasic');
  31. $this->processGoods('goodZx');
  32. $this->goodCombind();
  33. } catch (\Exception $e) {
  34. $output->writeln('【' . date('Y-m-d H:i:s') . '】' . $e->getMessage());
  35. }
  36. }
  37. protected function processGoods($method)
  38. {
  39. $goods = call_user_func([$this, $method]);
  40. if (empty($goods)) {
  41. return;
  42. }
  43. $this->addGood($goods);
  44. }
  45. protected function goodBasic()
  46. {
  47. return $this->processGood(GoodBasic::class, 0);
  48. }
  49. protected function goodZx()
  50. {
  51. return $this->processGood(GoodZixun::class, 1);
  52. }
  53. protected function processGood($modelClass, $isZx)
  54. {
  55. $with = ["unit", "cat", "goodTax"=>["outCategory", "inCategory"]];
  56. $fields = "spuCode,good_name,craft_desc,supplierNo,companyNo,supplierName,companyName,good_img,
  57. good_info_img,good_thumb_img,creater,createrid,{$isZx} as isZx,is_combind,cgd_supplier_code cgd_supplierNo,
  58. cgd_supplier_name cgd_supplierName,isChild,addtime,updatetime,'' exam_info";
  59. if ($isZx == 0) {
  60. $fields .=",after_sales,is_stock";
  61. }else{
  62. $fields .=",'' after_sales, 0 is_stock";
  63. }
  64. return $modelClass::with($with)
  65. ->field($fields)
  66. ->where('updatetime', '>=', $this->date)
  67. ->where('status', '=', 1)
  68. ->where('is_del', 0)
  69. ->select()
  70. ->each(function (&$item){
  71. $this->populateItem($item);
  72. });
  73. }
  74. protected function populateItem(&$item)
  75. {
  76. $item['cgd_inv_good_name'] = $item['cgd_inv_good_name'] ?? '';
  77. $item['inv_cat_name'] = $item['inv_cat_name'] ?? '';
  78. $item['inv_cat_code'] =$item['inv_cat_code'] ?? '';
  79. $item['inv_good_name'] = $item['inv_good_name'] ?? '';
  80. $item['cgd_inv_cat_code'] =$item['cgd_inv_cat_code'] ?? '';
  81. $item['cgd_inv_cat_name'] = $item['cgd_inv_cat_name'] ?? '';
  82. $item['inv_tag']=isset($item['sumitem'])?($item['sumitem']=='是'?1:0):0;
  83. $item['inv_tax'] = isset($item['inv_tax'])&& $item['inv_tax']!='' ? (str_replace('%', '', $item['inv_tax']) / 100) : '';
  84. $item['cgd_inv_tax'] = isset($item['cgd_inv_tax'])&& $item['cgd_inv_tax']!=''?(str_replace('%','',$item['cgd_inv_tax'])/100):"";
  85. $item['cat_diff'] = ($item['inv_cat_code'] != '' || $item['cgd_inv_cat_code'] != '') ? 0 : (($item['inv_cat_code'] != $item['cgd_inv_cat_code']) ?2 : 1);
  86. $item['tax_diff'] = ($item['inv_cat_code'] != '' || $item['cgd_inv_cat_code'] != '') ? 0: (($item['inv_cat_tax'] !=$item['cgd_inv_cat_tax'] ) ? 2 : 1);
  87. $item['status'] = ($item['inv_cat_code'] != '' && $item['cgd_inv_cat_code'] != '') ? 2 : (($item['inv_cat_code'] != '' || $item['cgd_inv_cat_code'] != '') ? 1 : 0);
  88. }
  89. protected function addGood($goods)
  90. {
  91. $spuCode = array_column($goods->toArray(), 'spuCode');
  92. $idsArr = (new \app\cxinv\model\Good())
  93. ->whereIn('spuCode', $spuCode)
  94. ->column('id,status,updatetime', 'spuCode');
  95. $saveAll = [];
  96. foreach ($goods as $item) {
  97. if (!isset($idsArr[$item['spuCode']]) ||($idsArr[$item['spuCode']]['updatetime'] != $item['updatetime']) || $idsArr[$item['spuCode']]['status'] != 2) {
  98. $item['id'] = $idsArr[$item['spuCode']]['id'] ?? null;
  99. $saveAll[] = $item->toArray();
  100. }
  101. }
  102. if (!empty($saveAll)) {
  103. try {
  104. (new \app\cxinv\model\Good())->saveAll($saveAll);
  105. } catch (\Exception $e) {
  106. throw new \Exception($e->getMessage());
  107. }
  108. }
  109. }
  110. protected function goodCombind()
  111. {
  112. $list =GoodCombind::where('createtime','>=', $this->date)
  113. ->select();
  114. if(empty($list)==false){
  115. $add=[];
  116. foreach($list as $item){
  117. unset($item['id']);
  118. $ist=GoodCombind::where(['spuCode'=>$item['spuCode'],'childCode'=>$item['childCode']])->findOrEmpty();
  119. if($ist->isEmpty())$add[]=$item;
  120. }
  121. (new \app\cxinv\model\GoodCombind)->saveAll($add);
  122. }
  123. }
  124. }