findOrEmpty()->toArray(); if($stock_type == 1){ $this->ordinary($product_arr,$store_id,$admin_id,$find,$warehouse_id); }elseif($stock_type == 2){ $this->exchange($product_arr,$store_id,$admin_id,$find,$warehouse_id); } } /**普通 */ public function ordinary($product_arr,$store_id,$admin_id,$find,$warehouse_id){ $store_find = StoreBranchProduct::where(['product_id' => $product_arr['id'], 'store_id' => $store_id])->findOrEmpty()->toArray(); if ($find && !$store_find) { $attr_value = StoreProductAttrValue::where('product_id', $product_arr['id'])->findOrEmpty(); Db::startTrans(); try { $dealCate = self::dealChangeCate($find['cate_id']); $product = [ 'product_id' => $find['id'], 'image' => $find['image'], 'store_name' => $find['store_name'], 'store_info' => $find['store_info'], 'keyword' => $find['keyword'], 'bar_code' => $find['bar_code'], 'cate_id' => $find['cate_id'], 'top_cate_id' => $dealCate['top_cate_id'], 'two_cate_id' => $dealCate['two_cate_id'], 'price' => $find['price'], // 'cost' => $find['cost'], //v1.0 'cost' => $find['cost'], 'purchase' => $find['purchase'], 'vip_price' => $find['vip_price'], 'manufacturer_information' => $find['manufacturer_information']??'', 'unit' => $find['unit'], 'batch' => $find['batch'], 'store_id' => $store_id, 'sales' => 0, 'product_type' => $find['product_type'], 'stock' => 0, 'rose' => $find['rose'], ]; $branch = StoreBranchProduct::create($product); $arr = [ 'product_id' => $product_arr['id'], 'store_id' => $store_id, 'unique' => setUnique($branch['id'], '', 0), 'sales' => 0, 'type' => 0, 'bar_code' => $attr_value['bar_code'] ]; StoreBranchProductAttrValue::create($arr); if ($product_arr['stock'] > 0) { $this->storage($find, $store_id, $admin_id, $product_arr,$warehouse_id); } StoreProductLogic::updateGoodsclass($find['cate_id'],$store_id); Db::commit(); return true; } catch (\Exception $e) { Db::rollback(); Log::error('store-storage队列消费失败: ' . $e->getMessage() . ',line:' . $e->getLine() . ',file:' . $e->getFile()); return false; } } else { Db::startTrans(); try { if ($product_arr['stock'] > 0) { $this->storage($find, $store_id, $admin_id, $product_arr,1,$warehouse_id); } Db::commit(); return true; } catch (\Exception $e) { Db::rollback(); Log::error('store-storage队列消费失败: ' . $e->getMessage() . ',line:' . $e->getLine() . ',file:' . $e->getFile()); return false; } } } /**兑换 */ public function exchange($product_arr,$store_id,$admin_id,$find,$warehouse_id){ $store_find = StoreBranchProductExchange::where(['product_id' => $product_arr['id'], 'store_id' => $store_id])->findOrEmpty()->toArray(); if ($find && !$store_find) { Db::startTrans(); try { $product = [ 'product_id' => $find['id'], 'image' => $find['image'], 'store_name' => $find['store_name'], 'store_info' => $find['store_info'], 'keyword' => $find['keyword'], 'bar_code' => $find['bar_code'], 'cate_id' => $find['cate_id'], 'price' => $find['price'], 'cost' => $find['cost'], 'purchase' => $find['purchase'], 'vip_price' => $find['vip_price'], 'unit' => $find['unit'], 'store_id' => $store_id, 'sales' => 0, 'stock' => 0, ]; StoreBranchProductExchange::create($product); if ($product_arr['stock'] > 0) { $this->storage($find, $store_id, $admin_id, $product_arr,$warehouse_id); } // StoreProductLogic::updateGoodsclass($find['cate_id'],$store_id); Db::commit(); return true; } catch (\Exception $e) { Db::rollback(); Log::error('store-storage队列消费失败: ' . $e->getMessage() . ',line:' . $e->getLine() . ',file:' . $e->getFile()); return false; } } else { Db::startTrans(); try { if ($product_arr['stock'] > 0) { $this->storage($find, $store_id, $admin_id, $product_arr,2,$warehouse_id); } Db::commit(); return true; } catch (\Exception $e) { Db::rollback(); Log::error('store-storage队列消费失败: ' . $e->getMessage() . ',line:' . $e->getLine() . ',file:' . $e->getFile()); return false; } } } public function storage($find, $store_id, $admin_id, $product_arr,$stock_type=1,$warehouse_id=0) { $storage = [ 'product_id' => $product_arr['id'], 'store_id' => $store_id, 'nums' => $product_arr['stock'], 'admin_id' => $admin_id, 'type' => $stock_type, ]; $data=[ 'warehouse_id'=>$warehouse_id, 'product_id' => $product_arr['id'], 'financial_pm' => 0, 'batch' => $product_arr['batch']??1, 'nums' => $product_arr['stock'], 'status' =>1, 'admin_id' =>$admin_id, ]; $warehouse=WarehouseProductStorege::where('warehouse_id',$warehouse_id)->where('product_id',$product_arr['id'])->find(); if ($warehouse) { if($warehouse['nums']< $product_arr['stock']){ $storage['status'] = -1; $data['status'] = -1; $storage['mark'] = '库存不足,分库存为:' .$warehouse['nums']; $data['mark'] = '库存不足,分库存为:' .$warehouse['nums'].' 总仓库存为:'.$find['stock']; } SystemStoreStorage::create($storage); WarehouseProductLogic::add($data); } else { SystemStoreStorage::create($storage); WarehouseProductLogic::add($data); } } public function onConsumeFailure(\Throwable $e, $package) { $package['max_attempts'] = 0; Log::error('store-storage队列消费失败: ' . $e->getMessage() . ',line:' . $e->getLine() . ',file:' . $e->getFile()); return $package; } /* * 传入三级或者二级分类返还上级分类 一级那全是相同 */ public static function dealChangeCate($cate_id) { $value =[]; $last_cate = Db::name('store_category')->where('id',$cate_id)->value('pid'); if(!empty($last_cate)){ //2 $value['two_cate_id'] = $last_cate; //1 $first_cate = Db::name('store_category')->where('id',$value['two_cate_id'])->value('pid'); if(empty($first_cate)){//顶级了 $value['two_cate_id'] = $cate_id; $value['top_cate_id'] = $last_cate; }else{ $value['top_cate_id'] = $first_cate; } }else{ //1-2 选的1级目录 $value['two_cate_id'] = $cate_id; $value['top_cate_id'] = $cate_id; } return $value; } }