| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- <?php
- namespace common\models\diy;
- use common\enums\DiyEnum;
- use common\enums\StatusEnum;
- use Yii;
- use yii\db\Exception;
- /**
- * This is the model class for table "{{%diy_template}}".
- *
- * @property int $id
- * @property string $cate_id 分类id
- * @property string $title 名称
- * @property string $template 页面模板
- * @property string $content 内容
- * @property string|null $cover 预览图片
- * @property string $setting 价格设置
- * @property int|null $sort 排序,越小排越前
- * @property int|null $is_on_sale 上架状态【1是 0否】
- * @property int|null $status 状态[-1:删除;0:禁用;1启用]
- * @property int|null $created_at 添加时间
- * @property int $updated_at
- */
- class DiyTemplate extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%diy_template}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['sort', 'is_on_sale', 'status', 'created_at', 'updated_at'], 'integer'],
- [['content'], 'required'],
- [['content','setting'], 'string'],
- [['cate_id', 'title'], 'string', 'max' => 15],
- [['template'], 'string', 'max' => 20],
- [['cover'], 'string', 'max' => 255],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'cate_id' => '分类id',
- 'title' => '名称',
- 'template' => '页面模板',
- 'content' => '内容',
- 'cover' => '预览图片',
- 'setting' => '价格设置',
- 'sort' => '排序,越小排越前',
- 'is_on_sale' => '上架状态【1是 0否】',
- 'status' => '状态[-1:删除;0:禁用;1启用]',
- 'created_at' => '添加时间',
- 'updated_at' => 'Updated At',
- ];
- }
- /**
- * 保存数据
- * @Author: lun
- * @DateTime: 2021/12/14 0014 16:59
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param array $params
- * @return bool|DiyTemplate
- */
- public static function setData(array $params){
- try{
- if(!empty($params['id'])){
- $model = self::findOne(['id' => $params['id']]);
- if(empty($model)) throw new Exception('页面不存在或已删除');
- }else{
- $model = new self();
- $model->loadDefaultValues();
- $model->template = DiyEnum::CUSTOM_PAGE;
- }
- if(!$model->load($params,'') || !$model->save()){
- throw new Exception($model->getErrorMessage());
- }
- // 模板禁用,会将所有商城使用该模板的数据禁用。已经购买,能正常使用,不禁用了
- /*if ($model->status == StatusEnum::DISABLED) {
- DiyMallTemplate::updateAll(['status' => StatusEnum::DELETE], ['template_id' => $model->id]);
- DiyPage::updateAll(['status' => StatusEnum::DELETE], ['template_id' => $model->id]);
- }*/
- return $model;
- }catch(Exception $e){
- self::setStaticError($e->getMessage());
- return false;
- }
- }
- /**
- * 获取所选的购买规则设置
- *
- * @param integer $index
- * @return array|null
- */
- public function getCurrentItem(int $index)
- {
- $setting = json_decode($this->setting, true);
- return $setting[$index] ?? null;
- }
- /**
- * 通过ID获取模板
- *
- * @param integer $id
- * @return static|null
- */
- public static function getTemplateById(int $id)
- {
- return static::find()
- ->where(['id' => $id, 'status' => StatusEnum::ENABLED])
- ->one();
- }
- }
|