good.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\command;
  4. 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. // Cache::store("redis")->set("goodSet",0);
  30. }catch (\Exception $e){
  31. // Cache::store("redis")->set("goodSet",0);
  32. $output->writeln("【".date("Y-m-d H:i:s")."】".$e->getMessage());
  33. }
  34. }
  35. public function addGood($data){
  36. if(empty($data)) return;
  37. foreach ($data as $v){
  38. $is=Db::name("good")->where("spuCode",$v['spuCode'])->findOrEmpty();
  39. if(empty($is)){
  40. $array=[
  41. "spuCode"=>$v['spuCode'],
  42. "good_name"=>$v['good_name'],
  43. "companyNo"=>$v['companyNo'],
  44. "supplierNo"=>$v['supplierNo'],
  45. "is_stock"=>$v['is_stock'],
  46. "craft_desc"=>$v['craft_desc'],
  47. "after_sales"=>$v['after_sales'],
  48. "good_img"=>$v['good_img'],
  49. "good_thumb_img"=>$v['good_thumb_img'],
  50. "good_info_img"=>$v['good_info_img'],
  51. "creater"=>$v['creater'],
  52. "createrid"=>$v['createrid'],
  53. "cat_name"=>$v['cat_id'],
  54. "unit_name"=>$v['good_unit'],
  55. "isZx"=>$v['isZx'],
  56. "status"=>0,
  57. "addtime"=>date("Y-m-d H:i:s"),
  58. "updatetime"=>date("Y-m-d H:i:s")
  59. ];
  60. Db::name("good")->insert($array);
  61. echo "【".date("Y-m-d H:i:s")."】 添加商品{$v['spuCode']}进入类目表\r\n";
  62. }else{
  63. echo "【".date("Y-m-d H:i:s")."】 更新商品{$v['spuCode']}进入类目表\r\n";
  64. }
  65. }
  66. }
  67. /**
  68. * @throws \think\db\exception\DataNotFoundException
  69. * @throws \think\db\exception\DbException
  70. * @throws \think\db\exception\ModelNotFoundException
  71. */
  72. public function goodBasic(){
  73. $list =Db::connect('mysql_wsm')
  74. ->name('good_basic')
  75. ->withAttr("cat_id",function ($v){
  76. return Db::connect('mysql_wsm')->name('cat')->where("id",$v)->value("cat_name","");
  77. })
  78. ->withAttr("good_unit",function ($m){
  79. return Db::connect('mysql_wsm')->name('unit')->where("id",$m)->value("unit","");
  80. })->field("spuCode,good_name,cat_id,good_unit,craft_desc,after_sales,supplierNo,companyNo,good_img,
  81. good_info_img,good_thumb_img,creater,createrid,is_stock,0 isZx")
  82. ->where("updatetime",">=", $this->date)
  83. ->where("status","=", 1)
  84. ->select()->toArray();
  85. return $this->addGood($list);
  86. }
  87. public function goodZx(){
  88. $list =Db::connect('mysql_wsm')
  89. ->name('good_zixun')
  90. ->withAttr("cat_id",function ($v){
  91. return Db::connect('mysql_wsm')->name('cat')->where("id",$v)->value("cat_name","");
  92. })
  93. ->withAttr("good_unit",function ($v){
  94. return Db::connect('mysql_wsm')->name('unit')->where("id",$v)->value("unit","");
  95. })
  96. ->field("spuCode,good_name,craft_desc,'' after_sales,cat_id,good_unit,supplierNo,companyNo,good_img,good_info_img,good_thumb_img,creater,createrid,0 is_stock,1 isZx")
  97. ->where("addtime",">=", $this->date)
  98. ->select()->toArray();
  99. return $this->addGood($list);
  100. }
  101. }