| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- <?php
- namespace common\models\goods\template;
- use common\enums\DiyEnum;
- use common\enums\StatusEnum;
- use common\models\diy\DiyComponentsData;
- use common\models\diy\DiyPage;
- use common\models\goods\Goods;
- use Yii;
- /**
- * This is the model class for table "{{%goods_decoration_template}}".
- *
- * @property int $id
- * @property int $mall_id 商城ID
- * @property string $name 名称
- * @property int $is_default 是否为默认; [0非默认, 1默认]
- * @property int $template_id 模板ID[qimall_goods_decoration_template_content主键]
- * @property int $status 状态【1启用 0禁用 -1删除】
- * @property int $created_at 创建时间
- * @property int $updated_at 更新时间
- */
- class GoodsDecorationTemplate extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%goods_decoration_template}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'is_default', 'template_id', 'status', 'created_at', 'updated_at'], 'integer'],
- [['name'], 'string'],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => '商城ID',
- 'name' => '名称',
- 'is_default' => '是否为默认; [0非默认, 1默认]',
- 'template_id' => '模板ID[qimall_goods_decoration_template_content主键] ',
- 'status' => '状态【1启用 0禁用 -1删除】',
- 'created_at' => '创建时间',
- 'updated_at' => '更新时间',
- ];
- }
- /**
- * 保存数据
- * @Author bing
- * @DateTime 2021-08-26 14:16:24
- * @copyright: Copyright (c) 2020 广东七件事集团
- * @param array $params
- * @return object|boolean
- */
- public static function setData(array $params)
- {
- try {
- if (!empty($params['id'])) {
- $model = self::findOne(['id' => $params['id'], 'mall_id' => $params['mall_id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
- if (empty($model)) throw new \Exception('模板已存在或已删除');
- } else {
- $model = new self();
- $model->loadDefaultValues();
- }
- if (isset($params['is_default']) && $params['is_default'] == StatusEnum::ENABLED) {
- $old_data = self::findAll(['mall_id' => $params['mall_id'], 'is_default' => StatusEnum::ENABLED]);
- foreach ($old_data as $item) {
- $item->is_default = StatusEnum::DISABLED;
- $item->save();
- }
- }
- if (!$model->load($params, '') || !$model->save()) throw new \Exception($model->getErrorMessage());
- return $model;
- } catch (\Exception $e) {
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- public static function getGoodsListByStrategy($mall_id, $list, $user_id = 0)
- {
- // 获取数据
- $strategy = $list['strategy'];
- $displayNumber = $list['displayNumber'];
- $goods_params = [
- 'where' => [
- ['=', 'mall_id', $mall_id],
- ['=', 'status', StatusEnum::ENABLED],
- ['=', 'is_on_sale', StatusEnum::ENABLED]
- ],
- 'select' => 'id,goods_name,subtitle,price,original_price,cover_pic,(sales_num+virtual_sales) as sales_num,is_attr',
- 'limit' => $displayNumber,
- 'index' => 'id'
- ];
- if (in_array($strategy, [1, 2, 3, 4])) {
- $order_str = '';
- switch ($strategy) {
- case 1:
- $order_str = 'sales_num desc';
- break;
- case 2:
- $order_str = 'price desc';
- break;
- case 3:
- $order_str = 'price asc';
- break;
- case 4:
- $order_str = 'created_at desc';
- break;
- }
- $goods_params['order'] = $order_str;
- } else {
- $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';
- $result = Yii::$app->db->createCommand($sql)->queryAll();
- if ($result) {
- $goods_params['where'][] = ['in', 'id', array_column($result, 'goods_id')];
- } else {
- $goods_params['order'] = 'RAND()';
- }
- }
- $data = Goods::lists($goods_params, true);
- $data = DiyComponentsData::getMemberPrice($mall_id, $data, 1, $user_id);
- return $data;
- }
- /**
- * 获取广告模板列表
- * @Author bing
- * @DateTime 2021-08-26 13:00:11
- * @copyright: Copyright (c) 2020 广东七件事集团
- * @param array $cond
- * @param array $with
- * @param integer $is_array
- * @return array|null
- */
- public static function getTemplates(array $cond = [], array $with = [], bool $is_array = true, string $select = '*')
- {
- $default_cond = [['status' =>StatusEnum::ENABLED]];
- $cond = array_merge($default_cond, $cond);
- $query = self::find()->select($select);
- self::paramPack(['where' => $cond, 'with' => $with], $query);
- $data = $query->asArray($is_array)->all();
- return $data;
- }
- public static function getGoodsDecoration($mall_id, $goods_id)
- {
- $decoration_ralation = GoodsTemplateRelation::findOne(['mall_id' => $mall_id, 'goods_id' => $goods_id, 'status' => StatusEnum::ENABLED]);
- if (!$decoration_ralation || !$decoration_ralation->template) {
- $data = [];
- $is_default = self::find()->where(['and', ['mall_id' => $mall_id], ['status' => StatusEnum::ENABLED], ['is_default' => StatusEnum::ENABLED]])->select('template_id')->asArray()->limit(1)->one();
- if($is_default) $data = GoodsDecorationTemplateContent::getOne([['mall_id' => $mall_id],['id' => $is_default['template_id']], ['status' => StatusEnum::ENABLED]], true, [], '', 'id,title,content');
- if(!$data) $data = DiyPage::getOne([['mall_id' => $mall_id], ['template' => DiyEnum::GOODS_DETAIL], ['status' => StatusEnum::ENABLED]], true, [], '', 'id,title,content');
- } else {
- $data = GoodsDecorationTemplateContent::getOne([['mall_id' => $mall_id], ['id' => $decoration_ralation->template->template_id], ['status' => StatusEnum::ENABLED]], true, [], '', 'id,title,content');
- }
- return $data;
- }
- }
|