sysGood.php 4.6 KB

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