DiyMallTemplate.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. namespace common\models\diy;
  3. use common\enums\DiyEnum;
  4. use common\enums\StatusEnum;
  5. use Yii;
  6. use yii\db\Exception;
  7. /**
  8. * This is the model class for table "{{%diy_mall_template}}".
  9. *
  10. * @property int $id
  11. * @property int $mall_id 商城id
  12. * @property int $template_id qimall_diy_template.id
  13. * @property string $cate_id 分类id
  14. * @property string $title 名称
  15. * @property string $template 页面模板
  16. * @property string $content 内容
  17. * @property string|null $cover 预览图片
  18. * @property int $expire_time 过期时间[-1:永久有效]
  19. * @property int|null $status 状态[-1:删除;0:禁用;1:启用,2:已过期]
  20. * @property int|null $created_at 添加时间
  21. * @property int $updated_at
  22. */
  23. class DiyMallTemplate extends \common\models\base\BaseActiveRecord
  24. {
  25. /**
  26. * 一个月按多少天算
  27. * @var int
  28. */
  29. const MONTH_DAYS = 30;
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public static function tableName()
  34. {
  35. return '{{%diy_mall_template}}';
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function rules()
  41. {
  42. return [
  43. [['mall_id', 'template_id', 'expire_time', 'status', 'created_at', 'updated_at'], 'integer'],
  44. [['content'], 'required'],
  45. [['content'], 'string'],
  46. [['cate_id', 'title'], 'string', 'max' => 15],
  47. [['template'], 'string', 'max' => 20],
  48. [['cover'], 'string', 'max' => 255],
  49. ];
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. public function attributeLabels()
  55. {
  56. return [
  57. 'id' => 'ID',
  58. 'mall_id' => '商城id',
  59. 'template_id' => 'qimall_diy_template.id',
  60. 'cate_id' => '分类id',
  61. 'title' => '名称',
  62. 'template' => '页面模板',
  63. 'content' => '内容',
  64. 'cover' => '预览图片',
  65. 'expire_time' => '过期时间[-1:永久有效]',
  66. 'status' => '状态[-1:删除;0:禁用;1:启用,2:已过期]',
  67. 'created_at' => '添加时间',
  68. 'updated_at' => 'Updated At',
  69. ];
  70. }
  71. /**
  72. * 保存数据
  73. * @param array $params
  74. * @return bool|DiyMallTemplate
  75. */
  76. public static function setData(array $params){
  77. try{
  78. $model = self::findOne(['mall_id' => $params['mall_id'], 'template_id' => $params['template_id'], 'status'=>[StatusEnum::ENABLED]]);
  79. if (empty($model)) {
  80. $model = new self();
  81. $model->loadDefaultValues();
  82. } else {
  83. if ($model->expire_time == -1) {
  84. $params['expire_time'] = -1;
  85. } else {
  86. $time = time();
  87. if ($params['expire_time'] != -1 && $model->expire_time > $time) {
  88. $params['expire_time'] = $model->expire_time - $time + $params['expire_time'];
  89. }
  90. }
  91. }
  92. if(!$model->load($params,'') || !$model->save()){
  93. throw new Exception($model->getErrorMessage());
  94. }
  95. return $model;
  96. }catch(Exception $e){
  97. self::setStaticError($e->getMessage());
  98. return false;
  99. }
  100. }
  101. /**
  102. * 获取商城已有的模板
  103. * @Author: lun
  104. * @DateTime: 2021/12/31 0031 10:33
  105. * @Copyright: copyright (c) 2021 广东七件事集团
  106. * @param $mall_id
  107. */
  108. public static function getMallTemplate($mall_id, $select = "*"){
  109. return self::find()->select($select)->where(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED])->asArray()->all();
  110. }
  111. /**
  112. * 当前模板是否永久
  113. *
  114. * @return boolean
  115. */
  116. public function isPermanent()
  117. {
  118. return $this->expire_time == -1;
  119. }
  120. /**
  121. * 获取商城的某个模板
  122. *
  123. * @param integer $mall_id
  124. * @param integer $template_id
  125. * @return static|null
  126. */
  127. public static function getMallTemplateById(int $mall_id, int $template_id)
  128. {
  129. return static::find()
  130. ->where(['mall_id' => $mall_id, 'template_id' => $template_id, 'status' => StatusEnum::ENABLED])
  131. ->one();
  132. }
  133. /**
  134. * 添加模板
  135. *
  136. * @param DiyTemplate $template
  137. * @param integer $mall_id
  138. * @param integer $duration
  139. * @return boolean
  140. */
  141. public static function addTemplate(DiyTemplate $template, int $mall_id, int $duration)
  142. {
  143. $data['mall_id'] = $mall_id;
  144. $data['title'] = $template->title;
  145. $data['cate_id'] = $template->cate_id;
  146. $data['template_id'] = $template->id;
  147. $data['template'] = $template->template;
  148. $data['cover'] = $template->cover;
  149. $data['content'] = $template->content;
  150. $data['expire_time'] = static::calculateExpiration($duration, time());
  151. return static::setData($data);
  152. }
  153. /**
  154. * 续费模板
  155. *
  156. * @param integer $duration -1等于永久
  157. * @return boolean
  158. */
  159. public function renewTemplate(int $duration)
  160. {
  161. $expire_time = static::calculateExpiration($duration, $this->expire_time);
  162. $this->expire_time = $expire_time;
  163. return $this->save();
  164. }
  165. /**
  166. * 计算过期时间
  167. *
  168. * @param integer $duration
  169. * @param integer $time
  170. * @return int
  171. */
  172. public static function calculateExpiration(int $duration, int $time)
  173. {
  174. return $duration == -1 ? -1 : bcadd(bcmul(bcmul($duration, static::MONTH_DAYS), 86400), $time);
  175. }
  176. }