good.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\command;
  4. use app\admin\model\GoodCombind;use think\console\Command;
  5. use think\console\Input;
  6. use think\console\input\Argument;
  7. use think\console\input\Option;
  8. use think\console\Output;
  9. use think\facade\Cache;
  10. use think\facade\Db;
  11. class good extends Command
  12. {
  13. protected function configure()
  14. {
  15. // 指令配置
  16. $this->setName('good')
  17. ->setDescription('the good command');
  18. }
  19. protected function execute(Input $input, Output $output)
  20. {
  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->goodBasic();
  28. $this->goodZx();
  29. $this->GoodBind();
  30. }catch (\Exception $e){
  31. $output->writeln("【".date("Y-m-d H:i:s")."】".$e->getMessage());
  32. }
  33. }
  34. public function addGood($data){
  35. if(empty($data)) return;
  36. $saleAll=[];
  37. foreach ($data as $v){
  38. $is=\app\admin\model\Good::where("spuCode",$v['spuCode'])->findOrEmpty();
  39. if($is->isEmpty() || ($is->isZx==1 && $is->updatetime!=$v['updatetime'])){
  40. $array=[
  41. "id"=>$is->isEmpty()?null:$is->id,
  42. "spuCode"=>$v['spuCode'],
  43. "good_name"=>$v['good_name'],
  44. "companyNo"=>$v['companyNo'],
  45. "companyName"=>$v['companyName'],
  46. "supplierNo"=>$v['supplierNo'],
  47. "supplierName"=>$v['supplierName'],
  48. "is_stock"=>$v['is_stock'],
  49. "craft_desc"=>$v['craft_desc'],
  50. "after_sales"=>$v['after_sales'],
  51. "good_img"=>$v['good_img'],
  52. "good_thumb_img"=>$v['good_thumb_img'],
  53. "good_info_img"=>$v['good_info_img'],
  54. "creater"=>$v['creater'],
  55. "createrid"=>$v['createrid'],
  56. "cat_name"=>$v['cat_id'],
  57. "unit_name"=>$v['good_unit'],
  58. "isZx"=>$v['isZx'],
  59. "isCombind"=>$v['is_combind'],
  60. "isChild"=>$v['isChild'],
  61. "cgd_supplierNo"=>$v['cgd_supplier_code'],
  62. "cgd_supplierName"=>$v['cgd_supplier_name'],
  63. "exam_info"=>"",
  64. "status"=>0,
  65. "addtime"=>$v['addtime'],
  66. "updatetime"=>$v['updatetime'],
  67. ];
  68. $saleAll[]=$array;
  69. echo "【".date("Y-m-d H:i:s")."】 添加商品{$v['spuCode']}进入类目表\r\n";
  70. }
  71. }
  72. if(empty($saleAll)==false)(new \app\admin\model\Good())->saveAll($saleAll);
  73. }
  74. /**
  75. * @throws \think\db\exception\DataNotFoundException
  76. * @throws \think\db\exception\DbException
  77. * @throws \think\db\exception\ModelNotFoundException
  78. */
  79. public function goodBasic(){
  80. $list =Db::connect('mysql_wsm')
  81. ->name('good_basic')->alias("a")
  82. ->leftJoin("good_tax b","a.spuCode=b.spuCode")
  83. ->withAttr("cat_id",function ($v){
  84. return Db::connect('mysql_wsm')->name('cat')->where("id",$v)->value("cat_name","");
  85. })
  86. ->withAttr("good_unit",function ($m){
  87. return Db::connect('mysql_wsm')->name('unit')->where("id",$m)->value("unit","");
  88. })->field("a.spuCode,good_name,cat_id,good_unit,craft_desc,after_sales,supplierNo,companyNo,supplierName,companyName,good_img,
  89. good_info_img,good_thumb_img,creater,createrid,is_stock,0 isZx,is_combind,cgd_supplier_code,cgd_supplier_name,isChild,addtime,updatetime
  90. ,b.tax_id,b.tax,b.inv_good_name")
  91. ->where("updatetime",">=", $this->date)
  92. ->where("status","=", 1)
  93. ->select()->toArray();
  94. return $this->addGood($list);
  95. }
  96. public function goodZx(){
  97. $list =Db::connect('mysql_wsm')
  98. ->name('good_zixun')
  99. ->alias('a')
  100. ->leftJoin('good_tax b','a.spuCode=b.spuCode')
  101. ->withAttr("cat_id",function ($v){
  102. return Db::connect('mysql_wsm')->name('cat')->where("id",$v)->value("cat_name","");
  103. })
  104. ->withAttr("good_unit",function ($v){
  105. return Db::connect('mysql_wsm')->name('unit')->where("id",$v)->value("unit","");
  106. })
  107. ->field("a.spuCode,good_name,craft_desc,'' after_sales,cat_id,good_unit,supplierNo,a.companyNo,supplierName,companyName,good_img,
  108. good_info_img,good_thumb_img,creater,createrid,0 is_stock,1 isZx,is_combind,cgd_supplier_code,cgd_supplier_name,isChild,
  109. addtime,updatetime,b.tax_id,b.tax,b.inv_good_name")
  110. ->where("addtime",">=", $this->date)
  111. ->select()->toArray();
  112. return $this->addGood($list);
  113. }
  114. public function GoodBind(){
  115. $list =Db::connect('mysql_wsm')->name("good_combind")
  116. ->where('createtime','>=', $this->date)
  117. ->select();
  118. if(empty($list)==false){
  119. $add=[];
  120. foreach($list as $item){
  121. unset($item['id']);
  122. $ist=GoodCombind::where(["spuCode"=>$item['spuCode'],"childCode"=>$item['childCode']])->findOrEmpty();
  123. if($ist->isEmpty())$add[]=$item;
  124. }
  125. (new \app\admin\model\GoodCombind())->saveAll($add);
  126. }
  127. }
  128. }