| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- <?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_mall_template}}".
- *
- * @property int $id
- * @property int $mall_id 商城id
- * @property int $template_id qimall_diy_template.id
- * @property string $cate_id 分类id
- * @property string $title 名称
- * @property string $template 页面模板
- * @property string $content 内容
- * @property string|null $cover 预览图片
- * @property int $expire_time 过期时间[-1:永久有效]
- * @property int|null $status 状态[-1:删除;0:禁用;1:启用,2:已过期]
- * @property int|null $created_at 添加时间
- * @property int $updated_at
- */
- class DiyMallTemplate extends \common\models\base\BaseActiveRecord
- {
- /**
- * 一个月按多少天算
- * @var int
- */
- const MONTH_DAYS = 30;
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%diy_mall_template}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'template_id', 'expire_time', 'status', 'created_at', 'updated_at'], 'integer'],
- [['content'], 'required'],
- [['content'], 'string'],
- [['cate_id', 'title'], 'string', 'max' => 15],
- [['template'], 'string', 'max' => 20],
- [['cover'], 'string', 'max' => 255],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => '商城id',
- 'template_id' => 'qimall_diy_template.id',
- 'cate_id' => '分类id',
- 'title' => '名称',
- 'template' => '页面模板',
- 'content' => '内容',
- 'cover' => '预览图片',
- 'expire_time' => '过期时间[-1:永久有效]',
- 'status' => '状态[-1:删除;0:禁用;1:启用,2:已过期]',
- 'created_at' => '添加时间',
- 'updated_at' => 'Updated At',
- ];
- }
- /**
- * 保存数据
- * @param array $params
- * @return bool|DiyMallTemplate
- */
- public static function setData(array $params){
- try{
- $model = self::findOne(['mall_id' => $params['mall_id'], 'template_id' => $params['template_id'], 'status'=>[StatusEnum::ENABLED]]);
- if (empty($model)) {
- $model = new self();
- $model->loadDefaultValues();
- } else {
- if ($model->expire_time == -1) {
- $params['expire_time'] = -1;
- } else {
- $time = time();
- if ($params['expire_time'] != -1 && $model->expire_time > $time) {
- $params['expire_time'] = $model->expire_time - $time + $params['expire_time'];
- }
- }
- }
- if(!$model->load($params,'') || !$model->save()){
- throw new Exception($model->getErrorMessage());
- }
- return $model;
- }catch(Exception $e){
- self::setStaticError($e->getMessage());
- return false;
- }
- }
- /**
- * 获取商城已有的模板
- * @Author: lun
- * @DateTime: 2021/12/31 0031 10:33
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $mall_id
- */
- public static function getMallTemplate($mall_id, $select = "*"){
- return self::find()->select($select)->where(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED])->asArray()->all();
- }
- /**
- * 当前模板是否永久
- *
- * @return boolean
- */
- public function isPermanent()
- {
- return $this->expire_time == -1;
- }
- /**
- * 获取商城的某个模板
- *
- * @param integer $mall_id
- * @param integer $template_id
- * @return static|null
- */
- public static function getMallTemplateById(int $mall_id, int $template_id)
- {
- return static::find()
- ->where(['mall_id' => $mall_id, 'template_id' => $template_id, 'status' => StatusEnum::ENABLED])
- ->one();
- }
- /**
- * 添加模板
- *
- * @param DiyTemplate $template
- * @param integer $mall_id
- * @param integer $duration
- * @return boolean
- */
- public static function addTemplate(DiyTemplate $template, int $mall_id, int $duration)
- {
- $data['mall_id'] = $mall_id;
- $data['title'] = $template->title;
- $data['cate_id'] = $template->cate_id;
- $data['template_id'] = $template->id;
- $data['template'] = $template->template;
- $data['cover'] = $template->cover;
- $data['content'] = $template->content;
- $data['expire_time'] = static::calculateExpiration($duration, time());
- return static::setData($data);
- }
- /**
- * 续费模板
- *
- * @param integer $duration -1等于永久
- * @return boolean
- */
- public function renewTemplate(int $duration)
- {
- $expire_time = static::calculateExpiration($duration, $this->expire_time);
- $this->expire_time = $expire_time;
- return $this->save();
- }
- /**
- * 计算过期时间
- *
- * @param integer $duration
- * @param integer $time
- * @return int
- */
- public static function calculateExpiration(int $duration, int $time)
- {
- return $duration == -1 ? -1 : bcadd(bcmul(bcmul($duration, static::MONTH_DAYS), 86400), $time);
- }
- }
|