GoodsDecorationTemplate.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. namespace common\models\goods\template;
  3. use common\enums\DiyEnum;
  4. use common\enums\StatusEnum;
  5. use common\models\diy\DiyComponentsData;
  6. use common\models\diy\DiyPage;
  7. use common\models\goods\Goods;
  8. use Yii;
  9. /**
  10. * This is the model class for table "{{%goods_decoration_template}}".
  11. *
  12. * @property int $id
  13. * @property int $mall_id 商城ID
  14. * @property string $name 名称
  15. * @property int $is_default 是否为默认; [0非默认, 1默认]
  16. * @property int $template_id 模板ID[qimall_goods_decoration_template_content主键]
  17. * @property int $status 状态【1启用 0禁用 -1删除】
  18. * @property int $created_at 创建时间
  19. * @property int $updated_at 更新时间
  20. */
  21. class GoodsDecorationTemplate extends \common\models\base\BaseActiveRecord
  22. {
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public static function tableName()
  27. {
  28. return '{{%goods_decoration_template}}';
  29. }
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public function rules()
  34. {
  35. return [
  36. [['mall_id', 'is_default', 'template_id', 'status', 'created_at', 'updated_at'], 'integer'],
  37. [['name'], 'string'],
  38. ];
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function attributeLabels()
  44. {
  45. return [
  46. 'id' => 'ID',
  47. 'mall_id' => '商城ID',
  48. 'name' => '名称',
  49. 'is_default' => '是否为默认; [0非默认, 1默认]',
  50. 'template_id' => '模板ID[qimall_goods_decoration_template_content主键] ',
  51. 'status' => '状态【1启用 0禁用 -1删除】',
  52. 'created_at' => '创建时间',
  53. 'updated_at' => '更新时间',
  54. ];
  55. }
  56. /**
  57. * 保存数据
  58. * @Author bing
  59. * @DateTime 2021-08-26 14:16:24
  60. * @copyright: Copyright (c) 2020 广东七件事集团
  61. * @param array $params
  62. * @return object|boolean
  63. */
  64. public static function setData(array $params)
  65. {
  66. try {
  67. if (!empty($params['id'])) {
  68. $model = self::findOne(['id' => $params['id'], 'mall_id' => $params['mall_id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
  69. if (empty($model)) throw new \Exception('模板已存在或已删除');
  70. } else {
  71. $model = new self();
  72. $model->loadDefaultValues();
  73. }
  74. if (isset($params['is_default']) && $params['is_default'] == StatusEnum::ENABLED) {
  75. $old_data = self::findAll(['mall_id' => $params['mall_id'], 'is_default' => StatusEnum::ENABLED]);
  76. foreach ($old_data as $item) {
  77. $item->is_default = StatusEnum::DISABLED;
  78. $item->save();
  79. }
  80. }
  81. if (!$model->load($params, '') || !$model->save()) throw new \Exception($model->getErrorMessage());
  82. return $model;
  83. } catch (\Exception $e) {
  84. self::$static_error = $e->getMessage();
  85. return false;
  86. }
  87. }
  88. public static function getGoodsListByStrategy($mall_id, $list, $user_id = 0)
  89. {
  90. // 获取数据
  91. $strategy = $list['strategy'];
  92. $displayNumber = $list['displayNumber'];
  93. $goods_params = [
  94. 'where' => [
  95. ['=', 'mall_id', $mall_id],
  96. ['=', 'status', StatusEnum::ENABLED],
  97. ['=', 'is_on_sale', StatusEnum::ENABLED]
  98. ],
  99. 'select' => 'id,goods_name,subtitle,price,original_price,cover_pic,(sales_num+virtual_sales) as sales_num,is_attr',
  100. 'limit' => $displayNumber,
  101. 'index' => 'id'
  102. ];
  103. if (in_array($strategy, [1, 2, 3, 4])) {
  104. $order_str = '';
  105. switch ($strategy) {
  106. case 1:
  107. $order_str = 'sales_num desc';
  108. break;
  109. case 2:
  110. $order_str = 'price desc';
  111. break;
  112. case 3:
  113. $order_str = 'price asc';
  114. break;
  115. case 4:
  116. $order_str = 'created_at desc';
  117. break;
  118. }
  119. $goods_params['order'] = $order_str;
  120. } else {
  121. $sql = 'SELECT COUNT(goods_id) AS comment_counts, goods_id FROM `qimall_order_comments` WHERE mall_id = ' . $mall_id . ' AND `status` = 1 GROUP BY goods_id ORDER BY COUNT(goods_id) DESC';
  122. $result = Yii::$app->db->createCommand($sql)->queryAll();
  123. if ($result) {
  124. $goods_params['where'][] = ['in', 'id', array_column($result, 'goods_id')];
  125. } else {
  126. $goods_params['order'] = 'RAND()';
  127. }
  128. }
  129. $data = Goods::lists($goods_params, true);
  130. $data = DiyComponentsData::getMemberPrice($mall_id, $data, 1, $user_id);
  131. return $data;
  132. }
  133. /**
  134. * 获取广告模板列表
  135. * @Author bing
  136. * @DateTime 2021-08-26 13:00:11
  137. * @copyright: Copyright (c) 2020 广东七件事集团
  138. * @param array $cond
  139. * @param array $with
  140. * @param integer $is_array
  141. * @return array|null
  142. */
  143. public static function getTemplates(array $cond = [], array $with = [], bool $is_array = true, string $select = '*')
  144. {
  145. $default_cond = [['status' =>StatusEnum::ENABLED]];
  146. $cond = array_merge($default_cond, $cond);
  147. $query = self::find()->select($select);
  148. self::paramPack(['where' => $cond, 'with' => $with], $query);
  149. $data = $query->asArray($is_array)->all();
  150. return $data;
  151. }
  152. public static function getGoodsDecoration($mall_id, $goods_id)
  153. {
  154. $decoration_ralation = GoodsTemplateRelation::findOne(['mall_id' => $mall_id, 'goods_id' => $goods_id, 'status' => StatusEnum::ENABLED]);
  155. if (!$decoration_ralation || !$decoration_ralation->template) {
  156. $data = [];
  157. $is_default = self::find()->where(['and', ['mall_id' => $mall_id], ['status' => StatusEnum::ENABLED], ['is_default' => StatusEnum::ENABLED]])->select('template_id')->asArray()->limit(1)->one();
  158. if($is_default) $data = GoodsDecorationTemplateContent::getOne([['mall_id' => $mall_id],['id' => $is_default['template_id']], ['status' => StatusEnum::ENABLED]], true, [], '', 'id,title,content');
  159. if(!$data) $data = DiyPage::getOne([['mall_id' => $mall_id], ['template' => DiyEnum::GOODS_DETAIL], ['status' => StatusEnum::ENABLED]], true, [], '', 'id,title,content');
  160. } else {
  161. $data = GoodsDecorationTemplateContent::getOne([['mall_id' => $mall_id], ['id' => $decoration_ralation->template->template_id], ['status' => StatusEnum::ENABLED]], true, [], '', 'id,title,content');
  162. }
  163. return $data;
  164. }
  165. }