GoodsCate.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. <?php
  2. namespace common\models\goods;
  3. use common\enums\StatusEnum;
  4. use Exception;
  5. use Yii;
  6. /**
  7. * This is the model class for table "{{%goods_cate}}".
  8. *
  9. * @property int $id
  10. * @property int $mall_id
  11. * @property int $mch_id
  12. * @property int $parent_id 父级ID
  13. * @property string $name 分类名称
  14. * @property string $pic
  15. * @property string $big_pic
  16. * @property string $advert_pic 广告图片
  17. * @property string $advert_url 广告链接
  18. * @property string $advert_open_type 打开方式
  19. * @property string $advert_params 导航参数
  20. * @property int|null $is_show 是否显示
  21. * @property int $status 状态[0禁用 1启用 -1删除]
  22. * @property int $sort 排序
  23. * @property int $created_at 创建时间
  24. * @property int $updated_at 修改时间
  25. */
  26. class GoodsCate extends \common\models\base\BaseActiveRecord
  27. {
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public static function tableName()
  32. {
  33. return '{{%goods_cate}}';
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function rules()
  39. {
  40. return [
  41. [['mall_id'], 'required'],
  42. [['mall_id', 'mch_id', 'parent_id', 'is_show', 'status', 'sort', 'created_at', 'updated_at', 'level'], 'integer'],
  43. [['advert_params'], 'string'],
  44. [['name'], 'string', 'max' => 45],
  45. [['pic', 'big_pic', 'advert_pic', 'advert_url'], 'string', 'max' => 255],
  46. [['advert_open_type'], 'string', 'max' => 65],
  47. ];
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function attributeLabels()
  53. {
  54. return [
  55. 'id' => 'ID',
  56. 'mall_id' => 'Mall ID',
  57. 'mch_id' => 'Mch ID',
  58. 'parent_id' => '父级ID',
  59. 'name' => '分类名称',
  60. 'pic' => '分类图片',
  61. '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. ];
  72. }
  73. // 关联模型
  74. public function getParent()
  75. {
  76. return $this->hasOne(self::class, ['id' => 'parent_id'])->andWhere(['<>', 'status', StatusEnum::DELETE]);
  77. }
  78. public function getChild()
  79. {
  80. return $this->hasMany(self::class, ['parent_id' => 'id'])->andWhere(['<>', 'status', StatusEnum::DELETE])->orderBy(['sort' => SORT_ASC, 'created_at' => SORT_DESC]);
  81. }
  82. // 分类关系
  83. public function getGoodsCateRelate()
  84. {
  85. return $this->hasMany(GoodsCateRelate::class, ['cate_id' => 'id']);
  86. }
  87. // 分类
  88. public function getGoods()
  89. {
  90. return $this->hasMany(Goods::class, ['id' => 'goods_id'])
  91. ->select('id,goods_name,price,original_price,cover_pic,virtual_sales,sales_num')
  92. ->where(['mch_id' => 0, 'status' => StatusEnum::ENABLED])
  93. ->via('goodsCateRelate');
  94. }
  95. /**
  96. * 保存数据
  97. * @Author bing
  98. * @DateTime 2021-08-26 14:16:24
  99. * @copyright: Copyright (c) 2020 广东七件事集团
  100. * @param array $params
  101. * @return object|boolean
  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->mch_id = $params['mch_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. /**
  135. * 获取分类列表
  136. * @Author bing
  137. * @DateTime 2021-08-26 13:00:11
  138. * @copyright: Copyright (c) 2020 广东七件事集团
  139. * @param array $cond
  140. * @param array $with
  141. * @param integer $is_array
  142. * @return array|null
  143. */
  144. public static function getCates(array $cond = [], array $with = [], bool $is_array = true, string $select = '*',$order = 'sort asc,created_at desc')
  145. {
  146. $default_cond = [['<>', 'status', StatusEnum::DELETE]];
  147. $cond = array_merge($default_cond, $cond);
  148. $query = self::find()->orderBy($order)->select($select);
  149. self::paramPack(['where' => $cond, 'with' => $with], $query);
  150. $cates = $query->asArray($is_array)->limit(10000)->all();
  151. return $cates;
  152. }
  153. /**
  154. * 获取树状分类
  155. * @Author bing
  156. * @DateTime 2021-08-26 13:00:40
  157. * @copyright: Copyright (c) 2020 广东七件事集团
  158. * @param array $cates
  159. * @param integer $parent_id
  160. * @return void
  161. */
  162. public static function getTreeCate(array $cates, int $parent_id = 0)
  163. {
  164. $tree = [];
  165. foreach ($cates as $k => $v) {
  166. $v['value'] = $v['id'];
  167. $v['label'] = $v['name'];
  168. if ($v['parent_id'] == $parent_id) {
  169. $tmp = $v;
  170. // 当前路由的菜单被选中
  171. $children = self::getTreeCate($cates, $v['id']);
  172. $children && $tmp['children'] = $children;
  173. $tree[] = $tmp;
  174. }
  175. }
  176. return $tree;
  177. }
  178. /**
  179. * 排序
  180. * @Author bing
  181. * @DateTime 2021-11-12 10:16:01
  182. * @copyright: Copyright (c) 2020 广东七件事集团
  183. * @param array $post
  184. * @return void
  185. */
  186. public static function sort(array $post)
  187. {
  188. try {
  189. $first_list = json_decode($post['first_list'], true);
  190. $second_list = json_decode($post['second_list'], true);
  191. $third_list = json_decode($post['third_list'], true);
  192. $t = \Yii::$app->db->beginTransaction();
  193. self::_saveSort($first_list);
  194. self::_saveSort($second_list);
  195. self::_saveSort($third_list);
  196. $t->commit();
  197. return true;
  198. } catch (Exception $e) {
  199. self::setStaticError($e->getMessage());
  200. return false;
  201. }
  202. }
  203. /**
  204. * 保存分类
  205. * @Author bing
  206. * @DateTime 2021-11-12 10:18:43
  207. * @copyright: Copyright (c) 2020 广东七件事集团
  208. * @param [type] $sort_cates
  209. * @return void
  210. */
  211. private static function _saveSort($sort_cates)
  212. {
  213. foreach ($sort_cates as $index => $cate) {
  214. $model = self::findOne($cate['id']);
  215. if (!$model) throw new \Exception('分类不存在');
  216. $model->sort = $index;
  217. $res = $model->save();
  218. if (!$res) throw new \Exception($model->getErrorMessage());
  219. }
  220. }
  221. /**
  222. * 删除分类
  223. *
  224. * @param array $params
  225. * @return boolean
  226. */
  227. public static function del($params)
  228. {
  229. try {
  230. $del_ids = [$params['id']];
  231. $condition = [
  232. 'mall_id' => $params['mall_id'],
  233. 'parent_id' => $params['id'],
  234. 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED],
  235. ];
  236. while ($ids = static::find()->where($condition)->select('id')->column()) {
  237. $del_ids = array_merge($del_ids, $ids);
  238. $condition['parent_id'] = $ids;
  239. }
  240. $is_check = GoodsCateRelate::checkCateHasGoods($del_ids);
  241. if ($is_check) {
  242. self::$static_error = '该分类或其子级分类有商品,请先删除商品';
  243. return false;
  244. }
  245. GoodsCate::updateAll(
  246. ['status' => StatusEnum::DELETE],
  247. ['id' => $del_ids, 'mall_id' => $params['mall_id'],'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]
  248. );
  249. return true;
  250. } catch (\yii\db\Exception $e) {
  251. self::setStaticError($e->getMessage());
  252. return false;
  253. }
  254. }
  255. /**
  256. * 根据一级分类获取所属的所有下级分类(包括自己)
  257. * @Author: lun
  258. * @DateTime: 2023/5/17 0017 11:08
  259. * @Copyright: copyright (c) 2021 广东七件事集团
  260. * @param $mallId
  261. * @param array $cateIds
  262. * @param array $cate_ids
  263. * @return false|string[]
  264. */
  265. public static function getAllCate($mallId, $cateIds = [], &$cate_ids = [])
  266. {
  267. // 获取分类
  268. $cate_ids[] = implode(',', $cateIds);
  269. $child_cates = self::find()->select('id,')->where(['mall_id' => $mallId, 'parent_id' => $cateIds, 'is_show' => StatusEnum::ENABLED, 'status' => StatusEnum::ENABLED])->column();
  270. if ($child_cates) {
  271. self::getAllCate($mallId, $child_cates, $cate_ids);
  272. }
  273. return explode(',', implode(',', $cate_ids));
  274. }
  275. /**
  276. * 获取该站点隐藏的分类IDs
  277. * @param integer $mall_id
  278. * @return array
  279. */
  280. public static function getHiddenIds(int $mall_id)
  281. {
  282. return static::find()
  283. ->where(['mall_id' => $mall_id, 'is_show' => false, 'status' => StatusEnum::ENABLED])
  284. ->select(['id'])
  285. ->column();
  286. }
  287. /**
  288. * 获取指定分类及其所有子分类的ID(仅包含启用状态)
  289. * @param array $ids
  290. * @return array
  291. */
  292. public static function getAllEnabledSubCateIds(array $ids)
  293. {
  294. $res_ids = $ids;
  295. while (count($ids)) {
  296. $ids = static::find()
  297. ->where(['parent_id' => $ids, 'status' => StatusEnum::ENABLED])
  298. ->select(['id'])
  299. ->column();
  300. // $res_ids = [...$res_ids, ...$ids];
  301. $res_ids = array_merge($res_ids, $ids);
  302. }
  303. return $res_ids;
  304. }
  305. //数据迁移-新增分类层级,旧数据维护
  306. public static function migrateCateLevel()
  307. {
  308. try {
  309. //获取一级分类数据
  310. $first_cate_data = self::lists([
  311. 'where' => [
  312. ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]],
  313. ['parent_id' => 0]
  314. ],
  315. ]);
  316. $first_cate_ids = array_column($first_cate_data, 'id');
  317. //修改分类层级为1
  318. self::updateAll(['level' => 1], ['id' => $first_cate_ids]);
  319. //修改上级id为一级分类的数据层级为2
  320. self::updateAll(['level' => 2], ['parent_id' => $first_cate_ids]);
  321. //修改剩余上级id不为0,且不存在层级的分类数据层级为3
  322. self::updateAll(['level' => 3], ['and', ['>', 'parent_id', 0], ['level' => 0]]);
  323. } catch (\Exception $e) {
  324. throw new Exception($e->getMessage());
  325. }
  326. }
  327. }