| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- <?php
- namespace addons\ScoreExpansion\backend\services;
- use addons\ScoreExpansion\common\models\ScoreExpansionRecommendRewardGoods;
- use common\enums\StatusEnum;
- use common\models\goods\GoodsCateRelate;
- use Yii;
- use yii\db\Exception;
- class ScoreExpansionRecommendRewardGoodsService extends \addons\ScoreExpansion\common\services\RecommendRewardGoodsService
- {
- /**
- * 列表
- *
- * @param array $params
- *
- * @return array
- */
- public function list(array $params): array
- {
- $baseWhere = [
- 'mall_id' => $this->mall_id,
- 'status' => StatusEnum::ENABLED,
- ];
- $where = [$baseWhere];
- if (!empty($params['goods_id'])) {
- $where[] = ['=', 'goods_id', $params['goods_id']];
- }
- if(!empty($params['cats'])){
- $goods_cate_relate_list = GoodsCateRelate::lists(['where'=>[['cate_id'=>$params['cats']]]]);
- $goods_ids = array_column($goods_cate_relate_list,'goods_id');
- $where[] = ['in', 'goods_id', $goods_ids];
- }
- $list = ScoreExpansionRecommendRewardGoods::listPage([
- 'where' => $where,
- 'with' => [
- 'goods' => function ($query) {
- $query->select('id, goods_name, price, cover_pic')->with(['cats']);
- }
- ],
- ]);
- foreach ($list['list'] as &$item) {
- $item['created_at_text'] = date('Y-m-d H:i:s', $item['created_at']);
- $item['setting'] = json_decode($item['setting'], true) ?: [];
- }
- // 返回所有已参与的商品
- $list['selected_goods_ids'] = $this->getSelectedGoodsIds();
- return $list;
- }
- /**
- * @param array $post
- *
- * @return bool
- */
- public function edit(array $post): bool
- {
- $post['mall_id'] = $this->mall_id;
- if (ScoreExpansionRecommendRewardGoods::setData($post) === false) {
- $this->setError(ScoreExpansionRecommendRewardGoods::getStaticError());
- return false;
- }
- return true;
- }
- /**
- * @param int $id
- *
- * @return bool
- */
- public function del(int $id): bool
- {
- return (bool)ScoreExpansionRecommendRewardGoods::updateAll(
- ['status' => StatusEnum::DELETE, 'updated_at' => time()],
- ['id' => $id, 'status' => StatusEnum::ENABLED, 'mall_id' => $this->mall_id]
- );
- }
- /**
- * @param array $goodsIds
- *
- * @return bool
- * @throws Exception
- */
- public function save(array $goodsIds): bool
- {
- $selectedGoodsIds = $this->getSelectedGoodsIds();
- // 要插入的商品id
- $insertGoodsIds = array_diff($goodsIds, $selectedGoodsIds);
- if (empty($insertGoodsIds)) {
- $this->setError('不存在要添加的商品');
- return false;
- }
- $batchInsert = [];
- $now = time();
- foreach ($insertGoodsIds as $goodsId) {
- $batchInsert[] = [
- 'mall_id' => $this->mall_id,
- 'goods_id' => $goodsId,
- 'is_alone' => 0,
- 'created_at' => $now,
- 'updated_at' => $now
- ];
- }
- return (bool)Yii::$app
- ->db
- ->createCommand()
- ->batchInsert(
- ScoreExpansionRecommendRewardGoods::tableName(),
- array_keys($batchInsert[0]),
- $batchInsert
- )
- ->execute();
- }
- /**
- * @return array
- */
- public function getSelectedGoodsIds()
- {
- return ScoreExpansionRecommendRewardGoods::find()
- ->where([
- 'mall_id' => $this->mall_id,
- 'status' => StatusEnum::ENABLED
- ])
- ->select('goods_id')
- ->column();
- }
- }
|