MaterialCate.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. namespace addons\Material\common\models;
  3. use common\enums\StatusEnum;
  4. use common\models\common\CapitalLog;
  5. use Yii;
  6. use yii\db\Exception;
  7. /**
  8. * This is the model class for table "qimall_addons_material_cate".
  9. *
  10. * @property int $id
  11. * @property int $mall_id 商城模块id
  12. * @property string|null $name 分类名称
  13. * @property int|null $status 状态[-1:删除;0:禁用;1启用]
  14. * @property int $created_at 创建时间
  15. * @property int $sort 排序
  16. * @property int $parent_id 父级ID
  17. * @property int|null $updated_at 更新时间
  18. */
  19. class MaterialCate extends \common\models\base\BaseActiveRecord
  20. {
  21. /**
  22. * {@inheritdoc}
  23. */
  24. public static function tableName()
  25. {
  26. return '{{%addons_material_cate}}';
  27. }
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public function rules()
  32. {
  33. return [
  34. [['mall_id'], 'required'],
  35. [['mall_id', 'status','created_at', 'parent_id','updated_at','sort'], 'integer'],
  36. [['name'], 'string', 'max' => 80],
  37. ];
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function attributeLabels()
  43. {
  44. return [
  45. 'id' => 'ID',
  46. 'mall_id' => '商城模块id',
  47. 'name' => '分类名称',
  48. 'status' => '状态[-1:删除;0:禁用;1启用]',
  49. 'sort' => '排序,越小越前面',
  50. 'parent_id' => '父级ID',
  51. 'created_at' => '创建时间',
  52. 'updated_at' => '更新时间',
  53. ];
  54. }
  55. public function getChildren()
  56. {
  57. return $this->hasMany(self::className(), ['parent_id' => 'id'])
  58. ->where(['!=', 'status', -1])
  59. ->orderBy('sort ASC');
  60. }
  61. public function getParent()
  62. {
  63. return $this->hasOne(Cate::className(), ['id' => 'parent_id']);
  64. }
  65. /**
  66. * 保存数据
  67. * @Author: ltm
  68. * @DateTime: 2021/12/13 0022 17:13
  69. * @Copyright: copyright (c) 2021 广东七件事集团
  70. * @param array $params
  71. * @return AddonsMaterial|bool
  72. */
  73. public static function setData(array $params){
  74. $t = \Yii::$app->db->beginTransaction();
  75. try{
  76. $params = self::exceptNullVal($params);
  77. $where[] = ['mall_id' => $params['mall_id'],'status'=>[StatusEnum::DISABLED,StatusEnum::ENABLED]];
  78. if($params['name']) $where[] = ['name' => $params['name']];
  79. $find_data = self::getOne($where);
  80. if ($find_data && (!$params['id'] || $find_data->id != $params['id']) && !empty($params['name'])) {
  81. self::$static_error = "分类名称已经存在";
  82. return false;
  83. }
  84. if($params['id']) {
  85. $cont = ['id' => $params['id']];
  86. $model = self::findOne($cont);
  87. }
  88. //var_dump($params);die;
  89. //判断分类是否已经被使用
  90. if($params['status']=='-1'){
  91. $findMaterial = Material::getOne([['cate_id' => $params['id'], 'mall_id' => $params['mall_id'], 'status' => StatusEnum::ENABLED, 'status' => StatusEnum::DISABLED]],1);
  92. if($findMaterial){
  93. self::$static_error = "该分类已经关联素材,请先移除该分类的下素材";
  94. return false;
  95. }
  96. if ($model && $model->parent_id > 0) {
  97. //二级分类删除,关联素材归到一级分类
  98. Material::updateAll([
  99. 'second_cate_id' => 0,
  100. 'updated_at' => $params['updated_at'] ?? time()
  101. ],
  102. [
  103. 'mall_id' => $params['mall_id'],
  104. 'second_cate_id' => $model->id,
  105. 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED],
  106. ]);
  107. }
  108. }
  109. if(empty($model)) {
  110. $model = new self();
  111. $model->loadDefaultValues();
  112. $model->mall_id = $params['mall_id'];
  113. $model->parent_id = $params['parent_id'] ?? 0;
  114. }
  115. if(!$model->load($params,'') || !$model->save()){
  116. throw new Exception($model->getErrorMessage());
  117. }
  118. $t->commit();
  119. return $model;
  120. }catch(\Exception $e){
  121. $t->rollBack();
  122. self::$static_error = $e->getMessage();
  123. return false;
  124. }
  125. }
  126. /**
  127. * 获取分类列表
  128. * @Author: lun
  129. * @DateTime: 2023/7/3 0003 14:13
  130. * @Copyright: copyright (c) 2021 广东七件事集团
  131. * @param $mall_id
  132. * @return array|\yii\db\ActiveRecord[]
  133. */
  134. public static function getList($mall_id)
  135. {
  136. $list = self::find()
  137. ->select('id, id as value, name as label')
  138. ->where([
  139. 'mall_id' => $mall_id,
  140. 'status' => StatusEnum::ENABLED,
  141. 'parent_id' => 0
  142. ])
  143. ->with([
  144. 'children' => function ($query) {
  145. $query->select('id, id as value, name as label,parent_id');
  146. }
  147. ])
  148. ->orderBy('sort asc')
  149. ->asArray()->all();
  150. return $list;
  151. }
  152. }