GoodsLabel.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace common\models\goods;
  3. use common\enums\StatusEnum;
  4. use Exception;
  5. use Yii;
  6. /**
  7. * This is the model class for table "{{%goods_label}}".
  8. *
  9. *
  10. * @property int $id
  11. * @property int $mall_id 商城ID
  12. * @property string $title 标签名称
  13. * @property string $sub_title 标签小标题
  14. * @property int $is_default 是否默认[0=否,1=是]
  15. * @property int $status 状态【1启用 0禁用 -1删除】
  16. * @property int $sort 排序
  17. * @property int $created_at 创建时间
  18. * @property int $updated_at 更新时间
  19. */
  20. class GoodsLabel extends \common\models\base\BaseActiveRecord
  21. {
  22. /**
  23. * {@inheritdoc}
  24. */
  25. public static function tableName()
  26. {
  27. return '{{%goods_label}}';
  28. }
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function rules()
  33. {
  34. return [
  35. [['mall_id', 'mch_id', 'status', 'sort', 'is_default', 'created_at', 'updated_at'], 'integer'],
  36. [['title'], 'required'],
  37. [['title', 'sub_title'], 'string', 'max' => 50],
  38. ];
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function attributeLabels()
  44. {
  45. return [
  46. 'id' => 'ID',
  47. 'mall_id' => '商城ID',
  48. 'mch_id' => '商户ID',
  49. 'title' => '标签名称',
  50. 'sub_title' => '标签小标题',
  51. 'is_default' => '是否默认[0=否,1=是]',
  52. 'status' => '状态【1启用 0禁用 -1删除】',
  53. 'sort' => '排序',
  54. 'created_at' => '创建时间',
  55. 'updated_at' => '更新时间',
  56. ];
  57. }
  58. /**
  59. * 保存数据
  60. * @Author bing
  61. * @DateTime 2021-08-24 15:34:48
  62. * @copyright: Copyright (c) 2020 广东七件事集团
  63. * @param array $params
  64. * @return void
  65. */
  66. public static function setData(array $params){
  67. try{
  68. if(!empty($params['id'])){
  69. $model = self::findOne(['mall_id' => $params['mall_id'], 'id' => $params['id'],'status'=>[StatusEnum::ENABLED,StatusEnum::DISABLED]]);
  70. if(empty($model)) throw new Exception('标签不存在或已删除');
  71. }else{
  72. $model = new self();
  73. $model->loadDefaultValues();
  74. $model->mall_id = $params['mall_id'];
  75. }
  76. if(!$model->load($params,'') || !$model->save()){
  77. throw new Exception($model->getErrorMessage());
  78. }
  79. return $model;
  80. }catch(Exception $e){
  81. self::$static_error = $e->getMessage();
  82. return false;
  83. }
  84. }
  85. /**
  86. * 获取标签列表
  87. * @Author bing
  88. * @DateTime 2021-08-26 13:00:11
  89. * @copyright: Copyright (c) 2020 广东七件事集团
  90. * @param array $cond
  91. * @param array $with
  92. * @param integer $is_array
  93. * @return array|null
  94. */
  95. public static function getLabels(array $cond=[],array $with=[],bool $is_array=true,string $select='*'){
  96. $default_cond = [['<>','status',StatusEnum::DELETE]];
  97. $cond = array_merge($default_cond,$cond);
  98. $query = self::find()->select($select);
  99. self::paramPack(['where'=>$cond,'with'=>$with],$query);
  100. $labels = $query->asArray($is_array)->all();
  101. return $labels;
  102. }
  103. }