StoreCate.php 9.2 KB

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