where([ 'mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $goodsIds, 'is_on_sale' => 1, 'is_attr' => 0 ]) ->select('id') ->column(); return CloudStockGoods::find() ->where([ 'mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'goods_id' => $goodsIds ]) ->select('goods_id') ->column(); } /** * 计算汇总价格及利润 * * @param array $stockGoodsList * @param array $goodsList * @param array $goodsIdNumberMap * * @return array */ public function getTotalOrderFields(array &$stockGoodsList, array $goodsList, array $goodsIdNumberMap): array { $res = [ 'replenish_price' => 0, 'profit' => 0 ]; foreach ($stockGoodsList as &$item) { $item['agent_price'] = json_decode($item['agent_price'], true) ?: []; $computePrice = $this->computePrice($item['agent_price'], $goodsList[$item['goods_id']]); // 得到单价及利润 // 商品价格 $res['replenish_price'] = bcadd( $res['replenish_price'], bcmul($computePrice['replenish_price'], $goodsIdNumberMap[$item['goods_id']], 2), 2 ); // 利润 $res['profit'] = bcadd( $res['profit'], bcmul($computePrice['profit'], $goodsIdNumberMap[$item['goods_id']], 2), 2 ); // 修改商品价格及利润 $item['goods_info']['price'] = $computePrice['replenish_price'] ?? $item['goods_info']['price']; $item['goods_info']['profit'] = $computePrice['profit'] ?? 0; unset($item['agent_price']); } return $res; } /** * @param array $agentPrice * @param array $goods * * @return array */ public function computePrice(array $agentPrice, array $goods) { $agentLevel = $this->stock_agent['level']; $agent_price = array_column($agentPrice, null, 'level'); $replenish_price = $goods['price'] ?? 0; if (!empty($agent_price[$agentLevel])) { $replenish_price = $agent_price[$agentLevel]['price']; } return [ 'replenish_price' => $replenish_price, // 单价 'profit' => bcsub($goods['price'], $replenish_price, 2), // 利润 ]; } public function getGoodsPrice(array $cartList) { $goodsList = Goods::lists([ 'select' => 'id, price', 'where' => [ [ 'mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => array_column($cartList, 'goods_id'), 'is_on_sale' => 1, 'is_attr' => 0 ] ], 'index' => 'id' ]); $cloudStockGoodsList = CloudStockGoods::lists([ 'where' => [ [ 'mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'goods_id' => array_column($goodsList, 'id') ] ], 'select' => 'id, goods_id, agent_price' ]); return $this->getTotalOrderFields( $cloudStockGoodsList, $goodsList, array_column($cartList, 'num', 'goods_id') ); } }