123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- <?php
- declare (strict_types = 1);
- namespace app\command;
- use app\admin\model\GoodBasic;
- use app\admin\model\GoodCombind;
- use app\admin\model\GoodZixun;
- use think\console\Command;
- use think\console\Input;
- use think\console\Output;
- use think\facade\Cache;
- class sysGood extends Command
- {
- protected $date;
- protected function configure()
- {
- // 指令配置
- $this->setName('sysgood')
- ->setDescription('the sysgood command');
- }
- protected function execute(Input $input, Output $output)
- {
- $this->date = date('Y-m-d H:i:s', time() - 3600);
- $goodset =Cache::store('redis')->get('goodSet');
- if($goodset ==1) return;
- Cache::store('redis')->set('goodSet',1,1800);
- try {
- $this->processGoods('goodBasic');
- $this->processGoods('goodZx');
- $this->goodCombind();
- } catch (\Exception $e) {
- $output->writeln('【' . date('Y-m-d H:i:s') . '】' . $e->getMessage());
- }
- }
- protected function processGoods($method)
- {
- $goods = call_user_func([$this, $method]);
- if (empty($goods)) {
- return;
- }
- $this->addGood($goods);
- }
- protected function goodBasic()
- {
- return $this->processGood(GoodBasic::class, 0);
- }
- protected function goodZx()
- {
- return $this->processGood(GoodZixun::class, 1);
- }
- protected function processGood($modelClass, $isZx)
- {
- $with = ["unit", "cat", "goodTax"=>["outCategory", "inCategory"]];
- $fields = "spuCode,good_name,craft_desc,supplierNo,companyNo,supplierName,companyName,good_img,
- good_info_img,good_thumb_img,creater,createrid,{$isZx} as isZx,is_combind,cgd_supplier_code cgd_supplierNo,
- cgd_supplier_name cgd_supplierName,isChild,addtime,updatetime,'' exam_info";
- if ($isZx == 0) {
- $fields .=",after_sales,is_stock";
- }else{
- $fields .=",'' after_sales, 0 is_stock";
- }
- return $modelClass::with($with)
- ->field($fields)
- ->where('updatetime', '>=', $this->date)
- ->where('status', '=', 1)
- ->where('is_del', 0)
- ->select()
- ->each(function (&$item){
- $this->populateItem($item);
- });
- }
- protected function populateItem(&$item)
- {
- $item['cgd_inv_good_name'] = $item['inv_good_name'] ?? '';
- $item['inv_cat_name'] = $item['out_tax_short_name'] ?? '';
- $item['inv_cat_code'] =$item['out_tax_merge_code'] ?? '';
- $item['inv_good_name'] = $item['inv_good_name'] ?? '';
- $item['cgd_inv_cat_code'] =$item['in_tax_merge_code'] ?? '';
- $item['cgd_inv_cat_name'] = $item['in_tax_short_name'] ?? '';
- $item['inv_tag']=isset($item['sumitem'])?($item['sumitem']=='是'?1:0):0;
- $item['addTax']=$item['addTax']??"";
- $item['cgd_inv_tax'] = isset($item['in_tax'])&& $item['in_tax']!='' ? (str_replace('%', '', $item['in_tax']) / 100) : '';
- $item['inv_tax'] = isset($item['out_tax'])&& $item['out_tax']!=''?(str_replace('%','',$item['out_tax'])/100):"";
- $item['cat_diff'] = ($item['inv_cat_code'] != '' || $item['cgd_inv_cat_code'] != '') ? 0 : (($item['inv_cat_code'] != $item['cgd_inv_cat_code']) ?2 : 1);
- $item['tax_diff'] = ($item['inv_cat_code'] != '' || $item['cgd_inv_cat_code'] != '') ? 0: (($item['inv_cat_tax'] !=$item['cgd_inv_cat_tax'] ) ? 2 : 1);
- $item['status'] = ($item['inv_cat_code'] != '' && $item['cgd_inv_cat_code'] != '') ? 2 : (($item['inv_cat_code'] != '' || $item['cgd_inv_cat_code'] != '') ? 1 : 0);
- }
- protected function addGood($goods)
- {
- $spuCode = array_column($goods->toArray(), 'spuCode');
- $idsArr = (new \app\cxinv\model\Good())
- ->whereIn('spuCode', $spuCode)
- ->column('id,status,updatetime', 'spuCode');
- $saveAll = [];
- foreach ($goods as $item) {
- if (!isset($idsArr[$item['spuCode']]) ||($idsArr[$item['spuCode']]['updatetime'] != $item['updatetime']) || $idsArr[$item['spuCode']]['status'] != 2) {
- $item['id'] = $idsArr[$item['spuCode']]['id'] ?? null;
- echo "【".date('Y-m-d H:i:s')."】 添加商品{$item['spuCode']}进入类目表\r\n";
- $saveAll[] = $item->toArray();
- }
- }
- if (!empty($saveAll)) {
- try {
- (new \app\cxinv\model\Good())->saveAll($saveAll);
- } catch (\Exception $e) {
- throw new \Exception($e->getMessage());
- }
- }
- }
- protected function goodCombind()
- {
- $list =GoodCombind::where('createtime','>=', $this->date)
- ->select();
- if(empty($list)==false){
- $add=[];
- foreach($list as $item){
- unset($item['id']);
- echo '【'.date('Y-m-d H:i:s')."】 添加商品{$item['spuCode']}进入组合商品目表\r\n";
- $ist=GoodCombind::where(['spuCode'=>$item['spuCode'],'childCode'=>$item['childCode']])->findOrEmpty();
- if($ist->isEmpty())$add[]=$item;
- }
- (new \app\cxinv\model\GoodCombind)->saveAll($add);
- }
- }
- }
|