ScoreExpansionRecommendRewardGoodsService.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace addons\ScoreExpansion\backend\services;
  3. use addons\ScoreExpansion\common\models\ScoreExpansionRecommendRewardGoods;
  4. use common\enums\StatusEnum;
  5. use common\models\goods\GoodsCateRelate;
  6. use Yii;
  7. use yii\db\Exception;
  8. class ScoreExpansionRecommendRewardGoodsService extends \addons\ScoreExpansion\common\services\RecommendRewardGoodsService
  9. {
  10. /**
  11. * 列表
  12. *
  13. * @param array $params
  14. *
  15. * @return array
  16. */
  17. public function list(array $params): array
  18. {
  19. $baseWhere = [
  20. 'mall_id' => $this->mall_id,
  21. 'status' => StatusEnum::ENABLED,
  22. ];
  23. $where = [$baseWhere];
  24. if (!empty($params['goods_id'])) {
  25. $where[] = ['=', 'goods_id', $params['goods_id']];
  26. }
  27. if(!empty($params['cats'])){
  28. $goods_cate_relate_list = GoodsCateRelate::lists(['where'=>[['cate_id'=>$params['cats']]]]);
  29. $goods_ids = array_column($goods_cate_relate_list,'goods_id');
  30. $where[] = ['in', 'goods_id', $goods_ids];
  31. }
  32. $list = ScoreExpansionRecommendRewardGoods::listPage([
  33. 'where' => $where,
  34. 'with' => [
  35. 'goods' => function ($query) {
  36. $query->select('id, goods_name, price, cover_pic')->with(['cats']);
  37. }
  38. ],
  39. ]);
  40. foreach ($list['list'] as &$item) {
  41. $item['created_at_text'] = date('Y-m-d H:i:s', $item['created_at']);
  42. $item['setting'] = json_decode($item['setting'], true) ?: [];
  43. }
  44. // 返回所有已参与的商品
  45. $list['selected_goods_ids'] = $this->getSelectedGoodsIds();
  46. return $list;
  47. }
  48. /**
  49. * @param array $post
  50. *
  51. * @return bool
  52. */
  53. public function edit(array $post): bool
  54. {
  55. $post['mall_id'] = $this->mall_id;
  56. if (ScoreExpansionRecommendRewardGoods::setData($post) === false) {
  57. $this->setError(ScoreExpansionRecommendRewardGoods::getStaticError());
  58. return false;
  59. }
  60. return true;
  61. }
  62. /**
  63. * @param int $id
  64. *
  65. * @return bool
  66. */
  67. public function del(int $id): bool
  68. {
  69. return (bool)ScoreExpansionRecommendRewardGoods::updateAll(
  70. ['status' => StatusEnum::DELETE, 'updated_at' => time()],
  71. ['id' => $id, 'status' => StatusEnum::ENABLED, 'mall_id' => $this->mall_id]
  72. );
  73. }
  74. /**
  75. * @param array $goodsIds
  76. *
  77. * @return bool
  78. * @throws Exception
  79. */
  80. public function save(array $goodsIds): bool
  81. {
  82. $selectedGoodsIds = $this->getSelectedGoodsIds();
  83. // 要插入的商品id
  84. $insertGoodsIds = array_diff($goodsIds, $selectedGoodsIds);
  85. if (empty($insertGoodsIds)) {
  86. $this->setError('不存在要添加的商品');
  87. return false;
  88. }
  89. $batchInsert = [];
  90. $now = time();
  91. foreach ($insertGoodsIds as $goodsId) {
  92. $batchInsert[] = [
  93. 'mall_id' => $this->mall_id,
  94. 'goods_id' => $goodsId,
  95. 'is_alone' => 0,
  96. 'created_at' => $now,
  97. 'updated_at' => $now
  98. ];
  99. }
  100. return (bool)Yii::$app
  101. ->db
  102. ->createCommand()
  103. ->batchInsert(
  104. ScoreExpansionRecommendRewardGoods::tableName(),
  105. array_keys($batchInsert[0]),
  106. $batchInsert
  107. )
  108. ->execute();
  109. }
  110. /**
  111. * @return array
  112. */
  113. public function getSelectedGoodsIds()
  114. {
  115. return ScoreExpansionRecommendRewardGoods::find()
  116. ->where([
  117. 'mall_id' => $this->mall_id,
  118. 'status' => StatusEnum::ENABLED
  119. ])
  120. ->select('goods_id')
  121. ->column();
  122. }
  123. }