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