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); } }