DiyTemplate.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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_template}}".
  9. *
  10. * @property int $id
  11. * @property string $cate_id 分类id
  12. * @property string $title 名称
  13. * @property string $template 页面模板
  14. * @property string $content 内容
  15. * @property string|null $cover 预览图片
  16. * @property string $setting 价格设置
  17. * @property int|null $sort 排序,越小排越前
  18. * @property int|null $is_on_sale 上架状态【1是 0否】
  19. * @property int|null $status 状态[-1:删除;0:禁用;1启用]
  20. * @property int|null $created_at 添加时间
  21. * @property int $updated_at
  22. */
  23. class DiyTemplate extends \common\models\base\BaseActiveRecord
  24. {
  25. /**
  26. * {@inheritdoc}
  27. */
  28. public static function tableName()
  29. {
  30. return '{{%diy_template}}';
  31. }
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function rules()
  36. {
  37. return [
  38. [['sort', 'is_on_sale', 'status', 'created_at', 'updated_at'], 'integer'],
  39. [['content'], 'required'],
  40. [['content','setting'], 'string'],
  41. [['cate_id', 'title'], 'string', 'max' => 15],
  42. [['template'], 'string', 'max' => 20],
  43. [['cover'], 'string', 'max' => 255],
  44. ];
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. public function attributeLabels()
  50. {
  51. return [
  52. 'id' => 'ID',
  53. 'cate_id' => '分类id',
  54. 'title' => '名称',
  55. 'template' => '页面模板',
  56. 'content' => '内容',
  57. 'cover' => '预览图片',
  58. 'setting' => '价格设置',
  59. 'sort' => '排序,越小排越前',
  60. 'is_on_sale' => '上架状态【1是 0否】',
  61. 'status' => '状态[-1:删除;0:禁用;1启用]',
  62. 'created_at' => '添加时间',
  63. 'updated_at' => 'Updated At',
  64. ];
  65. }
  66. /**
  67. * 保存数据
  68. * @Author: lun
  69. * @DateTime: 2021/12/14 0014 16:59
  70. * @Copyright: copyright (c) 2021 广东七件事集团
  71. * @param array $params
  72. * @return bool|DiyTemplate
  73. */
  74. public static function setData(array $params){
  75. try{
  76. if(!empty($params['id'])){
  77. $model = self::findOne(['id' => $params['id']]);
  78. if(empty($model)) throw new Exception('页面不存在或已删除');
  79. }else{
  80. $model = new self();
  81. $model->loadDefaultValues();
  82. $model->template = DiyEnum::CUSTOM_PAGE;
  83. }
  84. if(!$model->load($params,'') || !$model->save()){
  85. throw new Exception($model->getErrorMessage());
  86. }
  87. // 模板禁用,会将所有商城使用该模板的数据禁用。已经购买,能正常使用,不禁用了
  88. /*if ($model->status == StatusEnum::DISABLED) {
  89. DiyMallTemplate::updateAll(['status' => StatusEnum::DELETE], ['template_id' => $model->id]);
  90. DiyPage::updateAll(['status' => StatusEnum::DELETE], ['template_id' => $model->id]);
  91. }*/
  92. return $model;
  93. }catch(Exception $e){
  94. self::setStaticError($e->getMessage());
  95. return false;
  96. }
  97. }
  98. /**
  99. * 获取所选的购买规则设置
  100. *
  101. * @param integer $index
  102. * @return array|null
  103. */
  104. public function getCurrentItem(int $index)
  105. {
  106. $setting = json_decode($this->setting, true);
  107. return $setting[$index] ?? null;
  108. }
  109. /**
  110. * 通过ID获取模板
  111. *
  112. * @param integer $id
  113. * @return static|null
  114. */
  115. public static function getTemplateById(int $id)
  116. {
  117. return static::find()
  118. ->where(['id' => $id, 'status' => StatusEnum::ENABLED])
  119. ->one();
  120. }
  121. }