redis = new \Redis(); $this->redis->connect('127.0.0.1', 6379); $this->redis->auth('qwe2019@'); $modelRoot = "\\app\\common\\model\\nwms"; $this->nStoreModel = new \app\common\model\nwms\Store; $this->oStoreModel = new \app\common\model\wms\Store; $this->nStoreWarehouse = new \app\common\model\nwms\StoreWarehouse; $this->oStockModel = new \app\common\model\wms\Stock; $this->oStockImgs = new \app\common\model\wms\StockImgs; $this->nStockModel = new \app\common\model\nwms\Stock; $this->nFileReferenceModel = new \app\common\model\nwms\FileReference; $this->nFileModel = new \app\common\model\nwms\File; $this->nStockBarcodesModel = new \app\common\model\nwms\StockBarcode; } public function async_store(){ $list = $this->nStoreModel->select(); $storeNames = []; foreach ($list as $item){ array_push($storeNames,$item['company_name']); } $list = $this->oStoreModel->alias("store")->field("store.TITLE as TITLE,warestore.ID as WARESTOREID")->join("yj_warestore warestore","warestore.STOREID=store.STOREID") ->where("warestore.WAREHOUSEID=5400")->where("store.TITLE","not in",$storeNames)->limit(200)->select(); if(!$list){ return "暂无未同步货主"; } $this->nStoreModel->startTrans(); try { foreach ($list as $store){ // 1. 检查 business_license 是否已存在(未软删除) $oldStore = $this->nStoreModel->where(["business_license"=>$store["WARESTOREID"], "deleted_at"=>null])->lock(true)->find(); if($oldStore && ($oldStore['company_name'] ?? null)!= $store['TITLE']){ // 归档旧记录 $this->nStoreModel->where(["id"=>$oldStore['id']])->update(["deleted_at"=>date('Y-m-d H:i:s')]); // 关联store_warehouse全部use_status=0 $this->nStoreWarehouse->where(["store_id"=>$oldStore['id']])->update(["use_status"=>0]); } // 2. 检查 company_name 是否已存在(未软删除) $exit = $this->nStoreModel->where(["company_name"=>$store["TITLE"], "deleted_at"=>null])->lock(true)->find(); if(!$exit){ $insert_data = [ "company_name"=>$store["TITLE"], "business_license"=>$store["WARESTOREID"], "legal_person"=>"", "contact_phone"=>"", "contact_email"=>"", "contact_person"=>"", "address"=>"", "industry"=>"", "account_id"=>"1", "status"=>2, "created_by"=>1, "logo_url"=>"", "domain"=>"", "deleted_at"=>null, "quota_bytes"=>"", "project"=>"store" ]; $return = $this->nStoreModel->create($insert_data); if($return['id']){ $insert_data = [ "store_id"=>$return["id"], "warehouse_id"=>$this->warehouseId, "auth_status"=>2, "apply_reason"=>"自动采集", "applied_by"=>1, "store_remask"=>$return["company_name"], "warehouse_remask"=>"天豹云仓", "print_quantity"=>0, "use_status"=>1 ]; $return = $this->nStoreWarehouse->create($insert_data); } } } $this->nStoreModel->commit(); return "同步成功"; } catch (Exception $e) { $this->nStoreModel->rollback(); return "同步失败"; } } public function asyncStock($full=1){ $this->async_store(); // 是否全量同步,full=1为全量,否则为增量 echo "[asyncStock] 开始同步,模式:" . ($full==1 ? '全量' : '增量') . "\n"; $last_stock = false; $stocks = false; if($full==1){ // 全量:查找当前仓库最新一条商品编码 $last_stock = $this->nStockModel->field("goods_code")->where(["warehouse_id"=>$this->warehouseId])->order("goods_code","desc")->find(); echo "[asyncStock] 全量模式,last_stock=" . json_encode($last_stock, JSON_UNESCAPED_UNICODE) . "\n"; }else{ // 增量:查找所有已同步商品编码 $stocks = $this->nStockModel->field("goods_code")->where(["warehouse_id"=>$this->warehouseId])->select(); echo "[asyncStock] 增量模式,已同步商品数=" . count($stocks) . "\n"; } // 查询源表商品及关联商品名等信息 $list = $this->oStockModel->alias('stock') ->field("goods.NAME as GOODSNAME,stock.STOCKID as STOCKID,stock.STYLE as STYLE,stock.COLOR as COLOR,stock.WARESTOREID as WARESTOREID,stock.IMG as IMG,stock.WEIGHT as WEIGHT,stock.BARCODE as BARCODE,stock.SKU as SKU") ->join("yj_goods goods","goods.GOODSID = stock.GOODSID","left") ->where(["stock.WAREHOUSEID"=>5400]); // 全量同步时,只同步大于最新goods_code的商品 if($last_stock){ $list->where('stock.STOCKID',">",$last_stock["goods_code"]); echo "[asyncStock] last_stock过滤后\n"; } // 增量同步时,排除已同步商品 if($stocks){ $stocks_arr = []; foreach ($stocks as $stock){ array_push($stocks_arr,$stock["goods_code"]); } $list->where('stock.STOCKID',"not in",$stocks_arr); echo "[asyncStock] stocks过滤后\n"; } // 限制每次最多同步1000条,按商品编码升序 $list = $list->order("stock.STOCKID","asc")->limit(1000) ->select(); echo "[asyncStock] 待同步商品数:" . count($list) . "\n"; if(!$list){ echo "[asyncStock] 暂无未同步商品\n"; return "暂无未同步商品"; } // 收集所有商品ID,批量查找图片 $ostockids = []; foreach ($list as $stock){ array_push($ostockids,$stock['STOCKID']); } $imgs = $this->oStockImgs->where("stock_id","in",$ostockids)->select(); echo "[asyncStock] 批量查找图片完成\n"; // 整理每个商品的图片数组 foreach ($list as $k=> $stock){ $imgs_arr = []; if($stock["IMG"]){ $str = $stock["IMG"]; if (substr($stock["IMG"], 0, 1) === 'n') { $str = substr($stock["IMG"], 1); } array_push($imgs_arr,$this->oWmsImgUrl.$str); } foreach ($imgs as $img){ if($stock["STOCKID"]==$img['stock_id']){ array_push($imgs_arr,$img['url']); } } $uniqueArray = array_values(array_unique($imgs_arr)); $list[$k]['imgs'] = $uniqueArray; } echo "[asyncStock] 商品图片整理完成\n"; // 获取所有商家ID与营业执照号的映射 $stores_arr = $this->nStoreModel->field("id,business_license")->select(); $stores = []; foreach($stores_arr as $r){ $stores[$r['business_license']] = $r['id']; } echo "[asyncStock] 商家映射整理完成\n"; // 开启事务,逐条同步商品 $this->nStockModel->startTrans(); try { foreach ($list as $stock){ // 判断商品是否已存在,避免重复 $exit = $this->nStockModel->where(["goods_code"=>$stock["STOCKID"]])->lock(true)->find(); if(!$exit){ echo "[asyncStock] 新增商品:" . $stock["STOCKID"] . "\n"; // 整理sku属性 $sku_property = []; if($stock["STYLE"]){ array_push($sku_property,$stock["STYLE"]); } if($stock["COLOR"]){ array_push($sku_property,$stock["COLOR"]); } // 构造插入数据 $insert_data = [ "item_code"=>$stock["SKU"], "goods_code"=>$stock["STOCKID"], "store_id"=>$stores[$stock["WARESTOREID"]], "item_name"=>$stock["GOODSNAME"], "sku_property"=>implode(";",$sku_property), "stock_unit"=>"个", "gross_weight"=>$stock["WEIGHT"], "item_type"=>"ZC", "is_sn_mgmt"=>"N", "is_shelf_life_mgmt"=>"N", "is_batch_mgmt"=>"N", "is_images"=>"N", "is_valid"=>"Y", "warehouse_id"=>$this->warehouseId, "quantity"=>0, "locked_quantity"=>0, "level"=>"C" ]; $warehouse_stock = $this->nStockModel->create($insert_data); // 处理条码 $barcodes = explode(";",$stock["BARCODE"]); $barcodes_arr = []; foreach ($barcodes as $barcode){ $tmp_barcode = trim($barcode); if($tmp_barcode){ array_push($barcodes_arr,$tmp_barcode); } } foreach ($barcodes_arr as $barcode){ $insert_data = ["stock_id"=>$warehouse_stock['id'], "barcode"=>$barcode,"warehouse_id"=>$this->warehouseId]; $this->nStockBarcodesModel->create($insert_data); } // 有条码则标记有图片 if($barcodes_arr>0){ $warehouse_stock->save(["is_images"=>"Y"]); } // 处理图片及文件引用 foreach ($stock['imgs'] as $img){ $img_tmp = $this->nFileModel->where(["file_url"=>$img])->lock(true)->find(); if(!$img_tmp){ $insert_data = [ "warehouse_id"=>$this->warehouseId, "file_key"=>$img, "file_url"=>$img, "file_name"=>"采集图片", "file_size"=>0, "md5_hash"=>md5($img), "sha1_hash"=>"", "storage_mode"=>"local", "storage_class"=>"standard", "uploader_id"=>30, "uploader_name"=>"代理采集" ]; $img_tmp = $this->nFileModel->create($insert_data); } $exit = $this->nFileReferenceModel->where([ "file_id"=>$img_tmp['id'], "business_type"=>"stock", "business_id"=>$warehouse_stock['id'], "warehouse_id"=>$this->warehouseId ])->lock(true)->find(); if(!$exit){ $insert_data = [ "file_id"=>$img_tmp['id'], "warehouse_id"=>$this->warehouseId, "business_type"=>"stock", "business_id"=>$warehouse_stock['id'], "display_order"=>0, "created_by"=>"代理采集" ]; $return = $this->nFileReferenceModel->create($insert_data); } } }else{ echo "[asyncStock] 已存在商品:" . $stock["STOCKID"] . "\n"; } } $this->nStockModel->commit(); echo "[asyncStock] 同步成功\n"; return "同步成功"; } catch (Exception $e) { echo "[asyncStock] 同步失败:" . $e->getMessage() . "\n"; return "同步失败"; } } }