sysGood.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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,'' exam_info";
  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['cgd_inv_good_name'] = $item['cgd_inv_good_name'] ?? '';
  72. $item['inv_cat_name'] = $item['inv_cat_name'] ?? '';
  73. $item['inv_cat_code'] =$item['inv_cat_code'] ?? '';
  74. $item['inv_good_name'] = $item['inv_good_name'] ?? '';
  75. $item['cgd_inv_cat_code'] =$item['cgd_inv_cat_code'] ?? '';
  76. $item['cgd_inv_cat_name'] = $item['cgd_inv_cat_name'] ?? '';
  77. $item['inv_tag']=isset($item['sumitem'])?($item['sumitem']=='是'?1:0):0;
  78. $item['inv_tax'] = isset($item['inv_tax'])&& $item['inv_tax']!='' ? (str_replace('%', '', $item['inv_tax']) / 100) : '';
  79. $item['cgd_inv_tax'] = isset($item['cgd_inv_tax'])&& $item['cgd_inv_tax']!=''?(str_replace('%','',$item['cgd_inv_tax'])/100):"";
  80. $item['cat_diff'] = ($item['inv_cat_code'] != '' || $item['cgd_inv_cat_code'] != '') ? 0 : (($item['inv_cat_code'] != $item['cgd_inv_cat_code']) ?2 : 1);
  81. $item['tax_diff'] = ($item['inv_cat_code'] != '' || $item['cgd_inv_cat_code'] != '') ? 0: (($item['inv_cat_tax'] !=$item['cgd_inv_cat_tax'] ) ? 2 : 1);
  82. $item['status'] = ($item['inv_cat_code'] != '' && $item['cgd_inv_cat_code'] != '') ? 2 : (($item['inv_cat_code'] != '' || $item['cgd_inv_cat_code'] != '') ? 1 : 0);
  83. }
  84. protected function addGood($goods)
  85. {
  86. $spuCode = array_column($goods->toArray(), 'spuCode');
  87. $idsArr = (new \app\cxinv\model\Good())
  88. ->whereIn('spuCode', $spuCode)
  89. ->column('id,status,updatetime', 'spuCode');
  90. $saveAll = [];
  91. foreach ($goods as $item) {
  92. if (!isset($idsArr[$item['spuCode']]) ||($idsArr[$item['spuCode']]['updatetime'] != $item['updatetime']) || $idsArr[$item['spuCode']]['status'] != 2) {
  93. $item['id'] = $idsArr[$item['spuCode']]['id'] ?? null;
  94. $saveAll[] = $item->toArray();
  95. }
  96. }
  97. if (!empty($saveAll)) {
  98. try {
  99. (new \app\cxinv\model\Good())->saveAll($saveAll);
  100. } catch (\Exception $e) {
  101. throw new \Exception($e->getMessage());
  102. }
  103. }
  104. }
  105. }