StoreGoodsCate.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. <?php
  2. namespace addons\Store\common\models;
  3. use common\enums\StatusEnum;
  4. use common\models\base\BaseActiveRecord;
  5. use addons\Store\common\enums\StoreEnum;
  6. use Exception;
  7. use Yii;
  8. /**
  9. * This is the model class for table "qimall_addons_store_good_cate".
  10. *
  11. * @property int $id
  12. * @property int $mall_id
  13. * @property int $store_id
  14. * @property int $cate_id 主商城分类id
  15. * @property int $parent_id 父级ID
  16. * @property string $name 分类名称
  17. * @property string $pic
  18. * @property string $big_pic
  19. * @property string $advert_pic 广告图片
  20. * @property string|null $advert_url 广告链接
  21. * @property string $advert_open_type 打开方式
  22. * @property string|null $advert_params 导航参数
  23. * @property int $mall_goods_cate_id 关联主商城商品分类ID
  24. * @property int $is_show 是否显示
  25. * @property int $status 状态[0禁用 1启用 -1删除]
  26. * @property int $sort 排序
  27. * @property int $created_at 创建时间
  28. * @property int $updated_at 修改时间
  29. */
  30. class StoreGoodsCate extends BaseActiveRecord
  31. {
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public static function tableName()
  36. {
  37. return '{{%addons_store_goods_cate}}';
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function rules()
  43. {
  44. return [
  45. [['mall_id', 'store_id', 'cate_id', 'parent_id', 'is_show', 'status', 'sort', 'mall_goods_cate_id', 'created_at', 'updated_at', 'level'], 'integer'],
  46. [['advert_params'], 'string'],
  47. [['name'], 'string', 'max' => 45],
  48. [['pic', 'big_pic', 'advert_pic', 'advert_url'], 'string', 'max' => 255],
  49. [['advert_open_type'], 'string', 'max' => 65],
  50. ];
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function attributeLabels()
  56. {
  57. return [
  58. 'id' => 'ID',
  59. 'mall_id' => 'Mall ID',
  60. 'store_id' => 'Store ID',
  61. 'cate_id' => '主商城分类id',
  62. 'parent_id' => '父级ID',
  63. 'name' => '分类名称',
  64. 'pic' => 'Pic',
  65. 'big_pic' => 'Big Pic',
  66. 'advert_pic' => '广告图片',
  67. 'advert_url' => '广告链接',
  68. 'advert_open_type' => '打开方式',
  69. 'advert_params' => '导航参数',
  70. 'is_show' => '是否显示',
  71. 'status' => '状态[0禁用 1启用 -1删除]',
  72. 'sort' => '排序',
  73. 'mall_goods_cate_id' => '关联主商城商品分类ID',
  74. 'created_at' => '创建时间',
  75. 'updated_at' => '修改时间',
  76. ];
  77. }
  78. // 关联模型
  79. public function getParent()
  80. {
  81. return $this->hasOne(self::class, ['id' => 'parent_id'])->andWhere(['<>', 'status', StatusEnum::DELETE]);
  82. }
  83. public function getChild()
  84. {
  85. return $this->hasMany(self::class, ['parent_id' => 'id'])->andWhere(['<>', 'status', StatusEnum::DELETE])->orderBy(['sort' => SORT_ASC, 'created_at' => SORT_DESC]);
  86. }
  87. // 分类关系
  88. public function getStoreGoodsCateRelate()
  89. {
  90. return $this->hasMany(StoreGoodsCateRelate::class, ['cate_id' => 'id']);
  91. }
  92. // 分类
  93. public function getGoods()
  94. {
  95. return $this->hasMany(StoreGoods::class, ['id' => 'goods_id'])
  96. ->via('storeGoodsCateRelate');
  97. }
  98. // public function getGoodsRecommend()
  99. // {
  100. // return $this->hasMany(StoreGoodsRecommend::class, ['id' => 'goods_id'])
  101. // ->via('storeGoodsCateRelate');
  102. // }
  103. public static function setData(array $params)
  104. {
  105. try {
  106. if (!empty($params['id'])) {
  107. $model = self::findOne(['and', ['id' => $params['id']], ['mall_id' => $params['mall_id']], ['<>', 'status', StatusEnum::DELETE]]);
  108. if (empty($model)) throw new \Exception('分类不存在或已删除');
  109. } else {
  110. $model = new self();
  111. $model->loadDefaultValues();
  112. $model->mall_id = $params['mall_id'];
  113. $model->store_id = $params['store_id'];
  114. $model->parent_id = $params['parent_id'] ?? 0;
  115. }
  116. isset($params['advert_params']) && $params['advert_params'] = json_encode($params['advert_params']);
  117. if ($model->parent_id == 0) {
  118. //上级分类不存在则为层级1
  119. $model->level = 1;
  120. } else {
  121. //查找上级分类数据
  122. $goods_cate_data = self::findOne(['and', ['id' => $model->parent_id, 'mall_id' => $params['mall_id']], ['<>', 'status', StatusEnum::DELETE]]);
  123. if (!empty($goods_cate_data)) $model->level = $goods_cate_data['level'] + 1;
  124. }
  125. if (!$model->load($params, '') || !$model->save()) {
  126. throw new \Exception($model->getErrorMessage());
  127. }
  128. return $model;
  129. } catch (\Exception $e) {
  130. self::$static_error = $e->getMessage();
  131. return false;
  132. }
  133. }
  134. public static function insertDataForName(array $data)
  135. {
  136. $model = self::findOne(['mall_id' => $data['mall_id'], 'store_id' => $data['store_id'], 'parent_id' => $data['parent_id'], 'name' => $data['name'], 'status' => StatusEnum::ENABLED]);
  137. if (!empty($model)) return $model;
  138. $model = new self();
  139. $model->loadDefaultValues();
  140. if (!$model->load($data, '') || !$model->save()) {
  141. throw new \Exception($model->getErrorMessage());
  142. }
  143. return $model;
  144. }
  145. public static function insertDataForMallCateID(array $data)
  146. {
  147. $model = self::findOne(['mall_id' => $data['mall_id'], 'store_id' => $data['store_id'], 'parent_id' => $data['parent_id'], 'mall_goods_cate_id' => $data['mall_goods_cate_id'], 'status' => StatusEnum::ENABLED]);
  148. if (!empty($model)) return $model;
  149. $model = new self();
  150. $model->loadDefaultValues();
  151. if (!$model->load($data, '') || !$model->save()) {
  152. throw new \Exception($model->getErrorMessage());
  153. }
  154. return $model;
  155. }
  156. /**
  157. * 获取分类列表
  158. * @Author bing
  159. * @DateTime 2021-08-26 13:00:11
  160. * @copyright: Copyright (c) 2020 广东七件事集团
  161. * @param array $cond
  162. * @param array $with
  163. * @param integer $is_array
  164. * @return array|null
  165. */
  166. public static function getCates(array $cond = [], array $with = [], bool $is_array = true, string $select = '*')
  167. {
  168. $default_cond = [['<>', 'status', StatusEnum::DELETE]];
  169. $cond = array_merge($default_cond, $cond);
  170. $query = self::find()->select($select);
  171. self::paramPack(['where' => $cond, 'with' => $with], $query);
  172. $cates = $query->asArray($is_array)->all();
  173. return $cates;
  174. }
  175. /**
  176. * 获取树状分类
  177. * @Author bing
  178. * @DateTime 2021-08-26 13:00:40
  179. * @copyright: Copyright (c) 2020 广东七件事集团
  180. * @param array $cates
  181. * @param integer $parent_id
  182. * @return void
  183. */
  184. public static function getTreeCate(array $cates, int $parent_id = 0)
  185. {
  186. $tree = [];
  187. foreach ($cates as $k => $v) {
  188. $v['value'] = $v['id'];
  189. $v['label'] = $v['name'];
  190. if ($v['parent_id'] == $parent_id) {
  191. $tmp = $v;
  192. // 当前路由的菜单被选中
  193. $children = self::getTreeCate($cates, $v['id']);
  194. $children && $tmp['children'] = $children;
  195. $tree[] = $tmp;
  196. }
  197. }
  198. return $tree;
  199. }
  200. /**
  201. * 排序
  202. * @Author bing
  203. * @DateTime 2021-11-12 10:16:01
  204. * @copyright: Copyright (c) 2020 广东七件事集团
  205. * @param array $post
  206. * @return bool
  207. */
  208. public static function sort(array $post)
  209. {
  210. try {
  211. $first_list = json_decode($post['first_list'], true);
  212. $second_list = json_decode($post['second_list'], true);
  213. $third_list = json_decode($post['third_list'], true);
  214. $t = \Yii::$app->db->beginTransaction();
  215. self::_saveSort($first_list);
  216. self::_saveSort($second_list);
  217. self::_saveSort($third_list);
  218. $t->commit();
  219. return true;
  220. } catch (\Exception $e) {
  221. self::setStaticError($e->getMessage());
  222. return false;
  223. }
  224. }
  225. /**
  226. * 保存分类
  227. * @Author bing
  228. * @DateTime 2021-11-12 10:18:43
  229. * @copyright: Copyright (c) 2020 广东七件事集团
  230. * @param [type] $sort_cates
  231. * @return void
  232. */
  233. private static function _saveSort($sort_cates)
  234. {
  235. foreach ($sort_cates as $index => $cate) {
  236. $model = self::findOne($cate['id']);
  237. if (!$model) throw new \Exception('分类不存在');
  238. $model->sort = $index;
  239. $res = $model->save();
  240. if (!$res) throw new \Exception($model->getErrorMessage());
  241. }
  242. }
  243. public static function getRecommendGoodCateListInDiy(int $mall_id, array $params)
  244. {
  245. $wheres[] = [
  246. 'sc.mall_id' => $mall_id,
  247. // 'sc.parent_id' => 0,
  248. 'sc.status' => StatusEnum::ENABLED,
  249. 'sc.is_show' => StatusEnum::ENABLED,
  250. 's.status' => StatusEnum::ENABLED,
  251. 's.check_status' => StoreEnum::CHECK_ADOPT
  252. ];
  253. $wheres[] = ['>', 'sc.store_id', 0];
  254. if (!empty($params['keyword'])) {
  255. $wheres[] = ['like', 'sc.name', $params['keyword']];
  256. }
  257. if (!empty($params['store_id'])) {
  258. $wheres[] = ['=', 'sc.store_id', $params['store_id']];
  259. }
  260. //商品分类层级
  261. if (!empty($params['level'])) $wheres[] = ['sc.level' => $params['level']];
  262. $params = [
  263. 'alias' => 'sc',
  264. 'select' => 'sc.*,s.store_name ',
  265. 'join' => [
  266. ['INNER JOIN', Store::tableName() . ' as s', 'sc.store_id = s.id'],
  267. ],
  268. 'where' => $wheres,
  269. 'order' => 'sc.sort asc,sc.created_at desc',
  270. 'page' => $params['page'] ?? 1,
  271. 'limit' => $params['limit'] ?? 15,
  272. ];
  273. $list = StoreGoodsCate::listPage($params);
  274. if (false === $list) {
  275. return self::$static_error;
  276. }
  277. foreach ($list['list'] as &$lv) {
  278. $lv['title'] = $lv['name'];
  279. switch ($lv['level']) {
  280. case 1:
  281. $lv['level_name'] = '一级分类';
  282. break;
  283. case 2:
  284. $lv['level_name'] = '二级分类';
  285. break;
  286. case 3:
  287. $lv['level_name'] = '三级分类';
  288. break;
  289. }
  290. }
  291. return $list;
  292. }
  293. public static function getAllCate($mallId, $store_id = null, $cateIds = [], &$cate_ids = [])
  294. {
  295. // 获取分类
  296. $cate_ids[] = implode(',', $cateIds);
  297. $where = ['mall_id' => $mallId, 'parent_id' => $cateIds, 'is_show' => StatusEnum::ENABLED, 'status' => StatusEnum::ENABLED];
  298. if ($store_id) {
  299. $where['store_id'] = $store_id;
  300. }
  301. $child_cates = self::find()->select('id,')->where($where)->column();
  302. if ($child_cates) {
  303. self::getAllCate($mallId, $store_id, $child_cates, $cate_ids);
  304. }
  305. return explode(',', implode(',', $cate_ids));
  306. }
  307. /**
  308. * 根据门店分类获取次分类门店下商品分类
  309. * @param $mall_id
  310. * @param $cate_id
  311. * @return array
  312. */
  313. public static function getStoreGoodsAllCate($mall_id, $cate_id): array
  314. {
  315. if (empty($mall_id) || empty($cate_id)) return [];
  316. $cate_id = explode(',', $cate_id);
  317. $store_id = Store::find()->where(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'check_status' => StatusEnum::ENABLED])
  318. ->andWhere(['c_id' => $cate_id])
  319. ->select('id')
  320. ->column();
  321. return self::find()->select('id')
  322. ->where(['mall_id' => $mall_id, 'is_show' => StatusEnum::ENABLED, 'status' => StatusEnum::ENABLED, 'store_id' => $store_id])
  323. ->andWhere(['parent_id' => StatusEnum::DISABLED])
  324. ->column();
  325. }
  326. public static function getChildCateIDByTree($cate = [], $key = 'children')
  327. {
  328. $children_cate_id_arr = [];
  329. if (!empty($cate[$key])){
  330. $children_cate_id_arr = array_column($cate[$key], 'id');
  331. foreach ($cate[$key] as $child_row) {
  332. $children_cate_id_arr = array_merge($children_cate_id_arr, self::getChildCateIDByTree($child_row, $key));
  333. }
  334. }
  335. return $children_cate_id_arr;
  336. }
  337. /**
  338. * 获取子分类,不递归
  339. * @param array $cates
  340. * @param int $parent_id
  341. * @return array
  342. */
  343. public static function getChildrenCate(array $cates, int $parent_id = 0)
  344. {
  345. $children = [];
  346. foreach ($cates as $v) {
  347. if ($v['parent_id'] == $parent_id) {
  348. $children[] = $v;
  349. }
  350. }
  351. return $children;
  352. }
  353. //数据迁移-新增分类层级,旧数据维护
  354. public static function migrateCateLevel()
  355. {
  356. try {
  357. //获取一级分类数据
  358. $first_cate_data = self::lists([
  359. 'where' => [
  360. ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]],
  361. ['parent_id' => 0]
  362. ],
  363. ]);
  364. $first_cate_ids = array_column($first_cate_data, 'id');
  365. //修改分类层级为1
  366. self::updateAll(['level' => 1], ['id' => $first_cate_ids]);
  367. //修改上级id为一级分类的数据层级为2
  368. self::updateAll(['level' => 2], ['parent_id' => $first_cate_ids]);
  369. //修改剩余上级id不为0,且不存在层级的分类数据层级为3
  370. self::updateAll(['level' => 3], ['and', ['>', 'parent_id', 0], ['level' => 0]]);
  371. } catch (\Exception $e) {
  372. throw new Exception($e->getMessage());
  373. }
  374. }
  375. }