| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- <?php
- namespace addons\CloudStock\api\services;
- use addons\CloudStock\common\models\CloudStockGoods;
- use addons\CloudStock\common\services\BaseService;
- use common\enums\StatusEnum;
- use common\models\goods\Goods;
- use RuntimeException;
- class CloudStockGoodsService extends BaseService
- {
- /**
- * 获取有效的云库存商品
- *
- * @param array $goodsIds
- *
- * @return array
- */
- public function getEnableGoodsList(array $goodsIds): array
- {
- if (empty($goodsIds)) {
- return [];
- }
- $goodsIds = Goods::find()
- ->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')
- );
- }
- }
|