Material.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace addons\Material\common\models;
  3. use addons\Material\common\enums\MaterialEnum;
  4. use common\enums\StatusEnum;
  5. use common\models\goods\Goods;
  6. use common\models\user\User;
  7. use Yii;
  8. use yii\db\Exception;
  9. /**
  10. * This is the model class for table "qimall_addons_material".
  11. *
  12. * @property int $id ID
  13. * @property int $mall_id 商城ID
  14. * @property int $user_id 用户ID
  15. * @property string $user_avatar_url 发布者头像
  16. * @property string $user_name 发布者
  17. * @property int $create_type 创建类型:1-用户;2-平台;
  18. * @property int|null $virtual_download_num 虚拟下载次数
  19. * @property int|null $download_num 实际下载次数
  20. * @property int $download_type 上传类型:1-图文;2-短视频
  21. * @property int $cate_id 素材分类id
  22. * @property int $second_cate_id 素材二级分类id
  23. * @property string $describe 素材 文案
  24. * @property int $goods_id 关联商品
  25. * @property string|null $pic_url 素材集合
  26. * @property int|null $sort 排序(越大排越前)
  27. * @property string|null $remark 备注
  28. * @property String|null $thumb_url 封面
  29. * @property String|null $video_url 视频集合
  30. * @property int|null $check_status 审核状态:0:未审核 1:审核通过 2:审核不通过
  31. * @property int|null $status 状态[-1:删除;0:禁用;1启用]
  32. * @property int|null $created_at 创建时间
  33. * @property int|null $updated_at 更新时间
  34. * @property int|null $check_at 审核时间
  35. */
  36. class Material extends \common\models\base\BaseActiveRecord
  37. {
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public static function tableName()
  42. {
  43. return '{{%addons_material}}';
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function rules()
  49. {
  50. return [
  51. [['mall_id', 'download_type', 'cate_id', 'second_cate_id', 'describe'], 'required'],
  52. [['mall_id','goods_id', 'user_id', 'create_type', 'virtual_download_num', 'download_num', 'download_type', 'cate_id', 'sort', 'check_status', 'status', 'created_at', 'updated_at', 'check_at'], 'integer'],
  53. [['describe', 'pic_url','thumb_url','video_url'], 'string'],
  54. [['user_avatar_url', 'remark'], 'string', 'max' => 255],
  55. [['user_name'], 'string', 'max' => 20],
  56. ];
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. public function attributeLabels()
  62. {
  63. return [
  64. 'id' => 'ID',
  65. 'mall_id' => '商城ID',
  66. 'user_id' => '用户ID',
  67. 'user_avatar_url' => '发布者头像',
  68. 'user_name' => '发布者',
  69. 'create_type' => '创建类型:1-用户;2-平台;',
  70. 'virtual_download_num' => '虚拟下载次数',
  71. 'download_num' => '实际下载次数',
  72. 'download_type' => '上传类型:1-图文;2-短视频',
  73. 'cate_id' => '素材分类id',
  74. 'second_cate_id' => '素材二级分类id',
  75. 'describe' => ' 素材 文案',
  76. 'goods_id' => '关联商品',
  77. 'status' => '状态[-1:删除;0:禁用;1启用]',
  78. 'pic_url' => '图片集合',
  79. 'thumb_url' => '视频封面',
  80. 'video_url' => '视频集合',
  81. 'sort' => '排序(越大排越前)',
  82. 'remark' => '备注',
  83. 'check_status' => '审核状态:0:未审核 1:审核通过 2:审核不通过',
  84. 'created_at' => '创建时间',
  85. 'updated_at' => '更新时间',
  86. 'check_at' => '审核时间',
  87. ];
  88. }
  89. public function getUser()
  90. {
  91. return $this->hasOne(User::class, ['id' => 'user_id'])->select('id,mobile,nickname,avatar_url,parent_id,username');
  92. }
  93. //管理商品
  94. public function getGoods()
  95. {
  96. return $this->hasOne(Goods::class, ['id' => 'goods_id'])->select('id,goods_name,price,cover_pic,sales_num,virtual_sales');
  97. }
  98. /**
  99. * 获取分类
  100. * @Author: ltm
  101. * @DateTime: 2021/12/13 0022 17:13
  102. * @Copyright: copyright (c) 2021 广东七件事集团
  103. * @param array $params
  104. * @return AddonsMaterial|bool
  105. */
  106. public function getCate(){
  107. return $this->hasOne(MaterialCate::class, ['id' => 'cate_id'])->andWhere(['<>','status',StatusEnum::DELETE]);
  108. }
  109. public function getSecondCate(){
  110. return $this->hasOne(MaterialCate::class, ['id' => 'second_cate_id'])->andWhere(['<>','status',StatusEnum::DELETE]);
  111. }
  112. /**
  113. * 保存数据
  114. * @Author: ltm
  115. * @DateTime: 2021/12/13 0022 17:13
  116. * @Copyright: copyright (c) 2021 广东七件事集团
  117. * @param array $params
  118. * @return AddonsMaterial|bool
  119. */
  120. public static function setData(array $params){
  121. try{
  122. if (!empty($params['id'])) {
  123. $model = self::findOne(['and',['id'=>$params['id'],'mall_id'=>$params['mall_id']],['<>','status',StatusEnum::DELETE]]);
  124. if (empty($model)) throw new Exception('素材不存在或已删除');
  125. }else{
  126. $model = new self();
  127. $model->loadDefaultValues();
  128. $model->mall_id = $params['mall_id'];
  129. }
  130. if(!$model->load($params,'') || !$model->save()){
  131. throw new Exception($model->getErrorMessage());
  132. }
  133. return $model;
  134. }catch(\Exception $e){
  135. self::$static_error = $e->getMessage();
  136. return false;
  137. }
  138. }
  139. }