PurchaseCate.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <?php
  2. namespace addons\Purchase\common\models;
  3. use common\enums\StatusEnum;
  4. use Yii;
  5. use yii\db\Exception;
  6. /**
  7. * This is the model class for table "{{%addons_purchase_cate}}".
  8. *
  9. * @property int $id
  10. * @property int $mall_id
  11. * @property int $parent_id 父级ID
  12. * @property string $name 分类名称
  13. * @property string $pic
  14. * @property string $big_pic
  15. * @property string $advert_pic 广告图片
  16. * @property string|null $advert_url 广告链接
  17. * @property string $advert_open_type 打开方式
  18. * @property string|null $advert_params 导航参数
  19. * @property int $is_show 是否显示
  20. * @property int $status 状态[0禁用 1启用 -1删除]
  21. * @property int $sort 排序
  22. * @property int $created_at 创建时间
  23. * @property int $updated_at 修改时间
  24. */
  25. class PurchaseCate extends \common\models\base\BaseActiveRecord
  26. {
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public static function tableName()
  31. {
  32. return '{{%addons_purchase_cate}}';
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function rules()
  38. {
  39. return [
  40. [['mall_id'], 'required'],
  41. [['mall_id', 'parent_id', 'is_show', 'status', 'sort', 'created_at', 'updated_at'], 'integer'],
  42. [['advert_params'], 'string'],
  43. [['name'], 'string', 'max' => 45],
  44. [['pic', 'big_pic', 'advert_pic', 'advert_url'], 'string', 'max' => 255],
  45. [['advert_open_type'], 'string', 'max' => 65],
  46. ];
  47. }
  48. /**
  49. * {@inheritdoc}
  50. */
  51. public function attributeLabels()
  52. {
  53. return [
  54. 'id' => 'ID',
  55. 'mall_id' => 'Mall ID',
  56. 'parent_id' => '父级ID',
  57. 'name' => '分类名称',
  58. 'pic' => 'Pic',
  59. 'big_pic' => 'Big Pic',
  60. 'advert_pic' => '广告图片',
  61. 'advert_url' => '广告链接',
  62. 'advert_open_type' => '打开方式',
  63. 'advert_params' => '导航参数',
  64. 'is_show' => '是否显示',
  65. 'status' => '状态[0禁用 1启用 -1删除]',
  66. 'sort' => '排序',
  67. 'created_at' => '创建时间',
  68. 'updated_at' => '修改时间',
  69. ];
  70. }
  71. /**
  72. * @notes:关联父级分类
  73. * @return \yii\db\ActiveQuery
  74. * @author: lun
  75. * @Time: 2022/10/22 11:15
  76. */
  77. public function getParent()
  78. {
  79. return $this->hasOne(self::class, ['id' => 'parent_id'])->andWhere(['<>', 'status', StatusEnum::DELETE]);
  80. }
  81. /**
  82. * @notes:关联子分类
  83. * @return \yii\db\ActiveQuery
  84. * @author: lun
  85. * @Time: 2022/10/22 11:15
  86. */
  87. public function getChild()
  88. {
  89. return $this->hasMany(self::class, ['parent_id' => 'id'])->andWhere(['<>', 'status', StatusEnum::DELETE])->orderBy(['sort' => SORT_ASC, 'created_at' => SORT_DESC]);
  90. }
  91. /**
  92. * @notes:关联商品
  93. * @return \yii\db\ActiveQuery
  94. * @author: lun
  95. * @Time: 2022/10/22 11:17
  96. */
  97. public function getPurchaseGoodsCateRelate()
  98. {
  99. return $this->hasMany(PurchaseGoodsCateRelate::class, ['cate_id' => 'id']);
  100. }
  101. /**
  102. * 保存数据
  103. * @Author bing
  104. * @DateTime 2021-08-26 14:16:24
  105. * @copyright: Copyright (c) 2020 广东七件事集团
  106. * @param array $params
  107. * @return object|boolean
  108. */
  109. public static function setData(array $params)
  110. {
  111. try {
  112. if (!empty($params['id'])) {
  113. $model = self::findOne(['and', ['id' => $params['id'], 'mall_id' => $params['mall_id']], ['<>', 'status', StatusEnum::DELETE]]);
  114. if (empty($model)) throw new \Exception('分类不存在或已删除');
  115. } else {
  116. $model = new self();
  117. $model->loadDefaultValues();
  118. $model->mall_id = $params['mall_id'];
  119. $model->parent_id = $params['parent_id'] ?? 0;
  120. }
  121. isset($params['advert_params']) && $params['advert_params'] = json_encode($params['advert_params']);
  122. if (!$model->load($params, '') || !$model->save()) {
  123. throw new Exception($model->getErrorMessage());
  124. }
  125. return $model;
  126. } catch (\Exception $e) {
  127. self::$static_error = $e->getMessage();
  128. return false;
  129. }
  130. }
  131. /**
  132. * @notes:获取分类列表
  133. * @param array $cond
  134. * @param array $with
  135. * @param bool $is_array
  136. * @param string $select
  137. * @return array|\yii\db\ActiveRecord[]
  138. * @author: lun
  139. * @Time: 2022/10/22 11:18
  140. */
  141. public static function getCates(array $cond = [], array $with = [], bool $is_array = true, string $select = '*')
  142. {
  143. $default_cond = [['<>', 'status', StatusEnum::DELETE]];
  144. $cond = array_merge($default_cond, $cond);
  145. $query = self::find()->select($select);
  146. self::paramPack(['where' => $cond, 'with' => $with], $query);
  147. $cates = $query->asArray($is_array)->all();
  148. return $cates;
  149. }
  150. /**
  151. * @notes:获取树状分类
  152. * @param array $cates
  153. * @param int $parent_id
  154. * @return array
  155. * @author: lun
  156. * @Time: 2022/10/22 11:19
  157. */
  158. public static function getTreeCate(array $cates, int $parent_id = 0)
  159. {
  160. $tree = [];
  161. foreach ($cates as $k => $v) {
  162. $v['value'] = $v['id'];
  163. $v['label'] = $v['name'];
  164. if ($v['parent_id'] == $parent_id) {
  165. $tmp = $v;
  166. // 当前路由的菜单被选中
  167. $children = self::getTreeCate($cates, $v['id']);
  168. $children && $tmp['children'] = $children;
  169. $tree[] = $tmp;
  170. }
  171. }
  172. return $tree;
  173. }
  174. /**
  175. * 排序
  176. * @Author bing
  177. * @DateTime 2021-11-12 10:16:01
  178. * @copyright: Copyright (c) 2020 广东七件事集团
  179. * @param array $post
  180. * @return bool
  181. */
  182. public static function sort(array $post)
  183. {
  184. try {
  185. $first_list = json_decode($post['first_list'], true);
  186. $second_list = json_decode($post['second_list'], true);
  187. $third_list = json_decode($post['third_list'], true);
  188. $t = \Yii::$app->db->beginTransaction();
  189. self::_saveSort($first_list);
  190. self::_saveSort($second_list);
  191. self::_saveSort($third_list);
  192. $t->commit();
  193. return true;
  194. } catch (Exception $e) {
  195. self::setStaticError($e->getMessage());
  196. return false;
  197. }
  198. }
  199. /**
  200. * @notes:修改分类排序
  201. * @param $sort_cates
  202. * @throws \Exception
  203. * @author: lun
  204. * @Time: 2022/10/22 11:19
  205. */
  206. private static function _saveSort($sort_cates)
  207. {
  208. foreach ($sort_cates as $index => $cate) {
  209. $model = self::findOne($cate['id']);
  210. if (!$model) throw new \Exception('分类不存在');
  211. $model->sort = $index;
  212. $res = $model->save();
  213. if (!$res) throw new \Exception($model->getErrorMessage());
  214. }
  215. }
  216. }