sysGood.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. try {
  26. $this->processGoods('goodBasic');
  27. $this->processGoods('goodZx');
  28. $this->goodCombind();
  29. } catch (\Exception $e) {
  30. $output->writeln('【' . date('Y-m-d H:i:s') . '】' . $e->getMessage());
  31. }
  32. }
  33. protected function processGoods($method)
  34. {
  35. $goods = call_user_func([$this, $method]);
  36. if (empty($goods)) {
  37. return;
  38. }
  39. $this->addGood($goods);
  40. }
  41. protected function goodBasic()
  42. {
  43. return $this->processGood(GoodBasic::class, 0);
  44. }
  45. protected function goodZx()
  46. {
  47. return $this->processGood(GoodZixun::class, 1);
  48. }
  49. protected function processGood($modelClass, $isZx)
  50. {
  51. $with = ["unit", "cat", "goodTax"=>["outCategory", "inCategory"]];
  52. $fields = "spuCode,good_name,craft_desc,supplierNo,companyNo,supplierName,companyName,good_img,
  53. good_info_img,good_thumb_img,creater,createrid,{$isZx} as isZx,is_combind,cgd_supplier_code cgd_supplierNo,
  54. cgd_supplier_name cgd_supplierName,isChild,addtime,updatetime,'' exam_info";
  55. if ($isZx == 0) {
  56. $fields .=",after_sales,is_stock";
  57. }else{
  58. $fields .=",'' after_sales, 0 is_stock";
  59. }
  60. return $modelClass::with($with)
  61. ->field($fields)
  62. ->where('updatetime', '>=', $this->date)
  63. ->where('status', '=', 1)
  64. ->where('is_del', 0)
  65. ->select()
  66. ->each(function (&$item){
  67. $this->populateItem($item);
  68. });
  69. }
  70. protected function populateItem(&$item)
  71. {
  72. $item['cgd_inv_good_name'] = $item['cgd_inv_good_name'] ?? '';
  73. $item['inv_cat_name'] = $item['inv_cat_name'] ?? '';
  74. $item['inv_cat_code'] =$item['inv_cat_code'] ?? '';
  75. $item['inv_good_name'] = $item['inv_good_name'] ?? '';
  76. $item['cgd_inv_cat_code'] =$item['cgd_inv_cat_code'] ?? '';
  77. $item['cgd_inv_cat_name'] = $item['cgd_inv_cat_name'] ?? '';
  78. $item['inv_tag']=isset($item['sumitem'])?($item['sumitem']=='是'?1:0):0;
  79. $item['inv_tax'] = isset($item['inv_tax'])&& $item['inv_tax']!='' ? (str_replace('%', '', $item['inv_tax']) / 100) : '';
  80. $item['cgd_inv_tax'] = isset($item['cgd_inv_tax'])&& $item['cgd_inv_tax']!=''?(str_replace('%','',$item['cgd_inv_tax'])/100):"";
  81. $item['cat_diff'] = ($item['inv_cat_code'] != '' || $item['cgd_inv_cat_code'] != '') ? 0 : (($item['inv_cat_code'] != $item['cgd_inv_cat_code']) ?2 : 1);
  82. $item['tax_diff'] = ($item['inv_cat_code'] != '' || $item['cgd_inv_cat_code'] != '') ? 0: (($item['inv_cat_tax'] !=$item['cgd_inv_cat_tax'] ) ? 2 : 1);
  83. $item['status'] = ($item['inv_cat_code'] != '' && $item['cgd_inv_cat_code'] != '') ? 2 : (($item['inv_cat_code'] != '' || $item['cgd_inv_cat_code'] != '') ? 1 : 0);
  84. }
  85. protected function addGood($goods)
  86. {
  87. $spuCode = array_column($goods->toArray(), 'spuCode');
  88. $idsArr = (new \app\cxinv\model\Good())
  89. ->whereIn('spuCode', $spuCode)
  90. ->column('id,status,updatetime', 'spuCode');
  91. $saveAll = [];
  92. foreach ($goods as $item) {
  93. if (!isset($idsArr[$item['spuCode']]) ||($idsArr[$item['spuCode']]['updatetime'] != $item['updatetime']) || $idsArr[$item['spuCode']]['status'] != 2) {
  94. $item['id'] = $idsArr[$item['spuCode']]['id'] ?? null;
  95. $saveAll[] = $item->toArray();
  96. }
  97. }
  98. if (!empty($saveAll)) {
  99. try {
  100. (new \app\cxinv\model\Good())->saveAll($saveAll);
  101. } catch (\Exception $e) {
  102. throw new \Exception($e->getMessage());
  103. }
  104. }
  105. }
  106. protected function goodCombind()
  107. {
  108. $list =GoodCombind::where('createtime','>=', $this->date)
  109. ->select();
  110. if(empty($list)==false){
  111. $add=[];
  112. foreach($list as $item){
  113. unset($item['id']);
  114. $ist=GoodCombind::where(['spuCode'=>$item['spuCode'],'childCode'=>$item['childCode']])->findOrEmpty();
  115. if($ist->isEmpty())$add[]=$item;
  116. }
  117. (new \app\cxinv\model\GoodCombind)->saveAll($add);
  118. }
  119. }
  120. }