AddonsXhsCate.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace addons\Xhs\common\models;
  3. use Yii;
  4. use yii\db\Exception;
  5. use common\enums\StatusEnum;
  6. /**
  7. * This is the model class for table "qimall_addons_xhs_cate".
  8. *
  9. * @property int $id ID
  10. * @property int $mall_id 商城ID
  11. * @property int $sort 序号
  12. * @property string $name 分类名称
  13. * @property int $status -1是删除,0是禁用,1是启用
  14. * @property int $created_at 创建时间
  15. * @property int $updated_at 更新时间
  16. */
  17. class AddonsXhsCate extends \common\models\base\BaseActiveRecord
  18. {
  19. /**
  20. * {@inheritdoc}
  21. */
  22. public static function tableName()
  23. {
  24. return '{{%addons_xhs_cate}}';
  25. }
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public function rules()
  30. {
  31. return [
  32. [['mall_id'], 'required'],
  33. [['mall_id', 'sort', 'status', 'created_at', 'updated_at'], 'integer'],
  34. [['name'], 'string', 'max' => 150],
  35. ];
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function attributeLabels()
  41. {
  42. return [
  43. 'id' => 'ID',
  44. 'mall_id' => '商城ID',
  45. 'sort' => '序号',
  46. 'name' => '分类名称',
  47. 'status' => '-1是删除,0是禁用,1是启用',
  48. 'created_at' => '创建时间',
  49. 'updated_at' => '更新时间',
  50. ];
  51. }
  52. /**
  53. * 保存数据
  54. * @Author: ltm
  55. * @DateTime: 2021/01/04 0022 17:13
  56. * @Copyright: copyright (c) 2021 广东七件事集团
  57. * @param array $params
  58. * @return AddonsMaterial|bool
  59. */
  60. public static function setData(array $params){
  61. try{
  62. $where[] = ['mall_id' => $params['mall_id']];
  63. if($params['name']) $where[] = ['name' => $params['name']];
  64. $find_data = self::getOne($where);
  65. if($find_data&&!$params['id']){
  66. self::$static_error = "分类名称已经存在";
  67. return false;
  68. }
  69. if($params['id']) {
  70. $cont = ['id' => $params['id']];
  71. $model = self::findOne($cont);
  72. }
  73. //判断分类是否已经被使用
  74. if($params['status']=='-1'){
  75. $findMaterial = AddonsXhsContent::getOne([['cate_id' => $params['id']],['status'=>[StatusEnum::ENABLED,StatusEnum::DISABLED]]]);
  76. if($findMaterial){
  77. self::$static_error = "分类名称已经使用,请先移除该分类的下素材";
  78. return false;
  79. }
  80. }
  81. if(empty($model)) {
  82. $model = new self();
  83. $model->loadDefaultValues();
  84. $model->mall_id = $params['mall_id'];
  85. }
  86. if(!$model->load($params,'') || !$model->save()){
  87. throw new Exception($model->getErrorMessage());
  88. }
  89. return $model;
  90. }catch(\Exception $e){
  91. self::$static_error = $e->getMessage();
  92. return false;
  93. }
  94. }
  95. }