MchCate.php 7.6 KB

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