mall_id; $where[] = ['mall_id' => $mall_id]; $where[] = ['status' => [StatusEnum::DISABLED, StatusEnum::ENABLED]]; if (!empty($params['name'])) { $where[] = ['like', 'name', trim($params['name'])]; } if (!empty($params['level'])) { $where[] = ['level' => $params['level']]; } $params['where'] = $where; $params['order'] = 'id desc'; $page_data = CloudStockPriceDifferenceGiftPackage::listPage($params, false); if (!empty($page_data['list'])) { $goods_ids = []; $level_arr = []; foreach ($page_data['list'] as &$item) { $item['goods_id'] = explode(',', $item['goods_id']); foreach ($item['goods_id'] as $g_id) { $goods_ids[] = $g_id; } $level_arr[] = $item['level']; } $level_list = CloudStockLevel::lists([ 'where' => [ ['mall_id' => $params['mall_id']], ['level' => $level_arr], ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]] ], 'index' => 'level', 'select' => 'level,name' ]); $goods_list = Goods::lists(['where' => [['id' => $goods_ids]], 'index' => 'id', 'select' => 'id,goods_name,cover_pic']); foreach ($page_data['list'] as &$item) { $temp_goods_list = []; foreach ($item['goods_id'] as $g_id) { if (isset($goods_list[$g_id])) { $temp_goods_list[] = $goods_list[$g_id]; } } $item['goods_list'] = $temp_goods_list; $item['level_name'] = $level_list[$item['level']]['name'] ?? ''; } } return $page_data; } public function edit($params) { $params['goods_id'] = implode(',', $params['goods_id']); if (empty($params['recommend_reward'])) { $params['recommend_reward'] = []; } $params['recommend_reward'] = json_encode($params['recommend_reward']); if (empty($params['pic_type'])) { $params['pic'] = \Yii::getAlias('@attachurl') . '/static/addons/CloudStock/price_difference_gift_package.png'; } $res = CloudStockPriceDifferenceGiftPackage::setData($params); if (empty($res)) { throw new \Exception(CloudStockPriceDifferenceGiftPackage::$static_error); } return $res; } public function switchStatus($params) { if ($params['status'] == StatusEnum::ENABLED) { $model = CloudStockPriceDifferenceGiftPackage::findOne(['id' => $params['id'], 'mall_id' => $this->mall_id]); if (empty($model['level'])) { throw new \Exception('请先设置升级等级'); } } $res = CloudStockPriceDifferenceGiftPackage::updateAll(['status' => $params['status']], ['id' => $params['id'], 'mall_id' => $this->mall_id]); if ($res === false) { throw new \Exception(CloudStockPriceDifferenceGiftPackage::$static_error); } return true; } public function del($params) { $mall_id = $this->mall_id; $model = CloudStockPriceDifferenceGiftPackage::findOne(['id' => $params['id'], 'mall_id' => $mall_id]); if (!empty($model)) { $model->status = StatusEnum::DELETE; $res = $model->save(); if ($res == false) { throw new \Exception($model->getErrorMessage()); } } return true; } public function detail($params) { if (!empty($params['id'])) { $id = $params['id']; $data = CloudStockPriceDifferenceGiftPackage::getOne([['id' => $id]], true); $data['goods_id'] = explode(',', $data['goods_id']); $goods_ids = []; foreach ($data['goods_id'] as $g_id) { $goods_ids[] = $g_id; } $main_goods_list = Goods::lists(['where' => [['id' => $goods_ids]], 'index' => 'id', 'select' => 'id,goods_name,cover_pic']); $goods_list = []; foreach ($data['goods_id'] as $id) { if (isset($main_goods_list[$id])) { $goods_list[] = $main_goods_list[$id]; } } $data['goods_list'] = $goods_list; $recommend_reward = json_decode($data['recommend_reward'], true); $recommend_reward = array_column($recommend_reward, null, 'level'); } $level_list = CloudStockLevel::lists(['where' => [['mall_id' => $this->mall_id], ['status' => StatusEnum::ENABLED]], 'order' => 'level asc', 'select' => 'level,name']); foreach ($level_list as &$item) { if (isset($recommend_reward[$item['level']])) { $item['reward_fee'] = !empty($recommend_reward[$item['level']]['reward_fee']) ? $recommend_reward[$item['level']]['reward_fee'] : 0; } else { $item['reward_fee'] = 0; } } $data['recommend_reward'] = $level_list; return $data; } public function getLevelList($params) { $mall_id = $this->mall_id; $where[] = ['mall_id' => $mall_id]; $where[] = ['status' => [StatusEnum::DISABLED, StatusEnum::ENABLED]]; $params['where'] = $where; $params['select'] = 'level as value,name as label'; $params['order'] = 'value asc'; $list = CloudStockLevel::lists($params); return $list; } public function list($params) { $cloud_stock_agent = CloudStockAgent::findOne(['mall_id' => $this->mall_id, 'user_id' => $this->user_id, 'status' => StatusEnum::ENABLED]); $mall_id = $this->mall_id; $where[] = ['mall_id' => $mall_id]; $where[] = ['status' => StatusEnum::ENABLED]; $where[] = ['>', 'level', $cloud_stock_agent['level'] ?? 0]; $params['where'] = $where; $params['order'] = 'id desc'; $params['select'] = 'id,name,price,pic_type,pic,status'; $page_data = CloudStockPriceDifferenceGiftPackage::listPage($params, false); return $page_data; } public function info($params) { $id = $params['id']; $package = CloudStockPriceDifferenceGiftPackage::getOne([['id' => $id]], true); $goods_ids = explode(',', $package['goods_id']); $main_goods_list = Goods::lists(['where' => [['id' => $goods_ids]], 'index' => 'id', 'select' => 'id,goods_name,cover_pic,price']); $list = []; $obj = new CloudStockPriceDifferenceGiftPackageOrderService(['mall_id' => $this->mall_id, 'user_id' => $this->user_id]); foreach ($main_goods_list as $item) { $price = $item['price']; $self_pickup_num = $cloud_warehouse_num = 0; $this->getPrice($item['id'], $goods_ids, $price, $package['level']); $key = $obj->getCacheKey($id); $field = $obj->getCacheField(CloudStockPriceDifferenceGiftPackageOrderService::$self_pickup, $item['id']); $cache_data = \Yii::$app->redis->hget($key, $field); if (!empty($cache_data)) { $cache_data = json_decode($cache_data, true); $self_pickup_num = $cache_data['num']; } $field = $obj->getCacheField(CloudStockPriceDifferenceGiftPackageOrderService::$cloud_warehouse, $item['id']); $cache_data = \Yii::$app->redis->hget($key, $field); if (!empty($cache_data)) { $cache_data = json_decode($cache_data, true); $cloud_warehouse_num = $cache_data['num']; } $list[] = [ 'goods_id' => $item['id'], 'goods_name' => $item['goods_name'], 'price' => $price, 'cover_pic' => $item['cover_pic'], 'self_pickup_num' => $self_pickup_num, 'cloud_warehouse_num' => $cloud_warehouse_num, ]; } $cloud_stock_agent_level = 0; $cloud_stock_agent = CloudStockAgent::findOne(['mall_id' => $this->mall_id, 'user_id' => $this->user_id, 'status' => StatusEnum::ENABLED]); if (!empty($cloud_stock_agent)) { $cloud_stock_agent_level = $cloud_stock_agent['level']; } $price_diff = $obj->priceDiff($cloud_stock_agent_level, $package['price']); return ['list' => $list, 'price' => $price_diff]; } public function getCloudStockGoodsList($goods_ids) { $cloud_stock_goods_list = CloudStockGoods::lists([ 'where' => [ ['goods_id' => $goods_ids], ['status' => StatusEnum::ENABLED] ], 'index' => 'goods_id', 'select' => 'goods_id,price_difference' ]); foreach ($cloud_stock_goods_list as &$val) { $agent_price = json_decode($val['price_difference'], true); $val['price_difference'] = array_column($agent_price ?? [], null, 'level'); } return $cloud_stock_goods_list; } public function getPrice($goods_id, $goods_ids, &$price, $level) { $cloud_stock_goods_list = $this->getCloudStockGoodsList($goods_ids); if (isset($cloud_stock_goods_list[$goods_id])) { $cloud_stock_goods = $cloud_stock_goods_list[$goods_id]; if (isset($cloud_stock_goods['price_difference'][$level]) && $cloud_stock_goods['price_difference'][$level]['price'] !== '') { $price = $cloud_stock_goods['price_difference'][$level]['price']; } } } }