CateService.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <?php
  2. namespace addons\TinyShop\services\product;
  3. use Yii;
  4. use common\enums\StatusEnum;
  5. use common\helpers\ArrayHelper;
  6. use common\components\Service;
  7. use common\helpers\TreeHelper;
  8. use addons\TinyShop\common\models\product\Cate;
  9. /**
  10. * Class Cate
  11. * @package addons\TinyShop\common\components\product
  12. * @author jianyan74 <751393839@qq.com>
  13. */
  14. class CateService extends Service
  15. {
  16. /**
  17. * 获取下拉
  18. *
  19. * @param string $id
  20. * @return array
  21. */
  22. public function getDropDownForEdit($id = '')
  23. {
  24. $list = Cate::find()
  25. ->where(['>=', 'status', StatusEnum::DISABLED])
  26. ->andWhere(['merchant_id' => Yii::$app->services->merchant->getNotNullId()])
  27. ->andFilterWhere(['<>', 'id', $id])
  28. ->select(['id', 'title', 'pid', 'level'])
  29. ->orderBy('sort asc')
  30. ->asArray()
  31. ->all();
  32. $models = ArrayHelper::itemsMerge($list);
  33. $data = ArrayHelper::map(ArrayHelper::itemsMergeDropDown($models), 'id', 'title');
  34. return ArrayHelper::merge([0 => '顶级分类'], $data);
  35. }
  36. /**
  37. * @return array
  38. */
  39. public function getMapList($merchant_id = '')
  40. {
  41. $models = ArrayHelper::itemsMerge($this->getList($merchant_id));
  42. return ArrayHelper::map(ArrayHelper::itemsMergeDropDown($models), 'id', 'title');
  43. }
  44. /**
  45. * @param string $pid
  46. * @return array|\yii\db\ActiveRecord[]
  47. */
  48. public function getList($merchant_id = '')
  49. {
  50. $merchant_id === '' && $merchant_id = Yii::$app->services->merchant->getNotNullId();
  51. return Cate::find()
  52. ->select(['id', 'title', 'pid', 'cover', 'level'])
  53. ->where(['status' => StatusEnum::ENABLED])
  54. ->andWhere(['merchant_id' => $merchant_id])
  55. ->orderBy('sort asc, id desc')
  56. ->asArray()
  57. ->all();
  58. }
  59. /**
  60. * 获取首页推荐
  61. *
  62. * @return array|\yii\db\ActiveRecord[]
  63. */
  64. public function findByRecommend()
  65. {
  66. return Cate::find()
  67. ->select(['id', 'title', 'subhead', 'cover'])
  68. ->where(['status' => StatusEnum::ENABLED])
  69. ->andWhere(['is_recommend' => StatusEnum::ENABLED])
  70. ->andWhere(['merchant_id' => Yii::$app->services->merchant->getNotNullId()])
  71. ->orderBy('sort asc, id desc')
  72. ->cache(30)
  73. ->asArray()
  74. ->all();
  75. }
  76. /**
  77. * 自定义分类
  78. *
  79. * @param $limit
  80. * @param array $ids
  81. * @return array|\yii\db\ActiveRecord[]
  82. */
  83. public function findByCustom($limit, $ids = [])
  84. {
  85. if (empty($ids)) {
  86. $condition = ['is_recommend' => StatusEnum::ENABLED];
  87. } else {
  88. $condition = ['in', 'id', $ids];
  89. $limit = count($ids);
  90. }
  91. $data = Cate::find()
  92. ->select(['id', 'title', 'subhead', 'cover'])
  93. ->where(['status' => StatusEnum::ENABLED])
  94. ->andWhere($condition)
  95. ->andWhere(['merchant_id' => Yii::$app->services->merchant->getNotNullId()])
  96. ->orderBy('sort asc, id desc')
  97. ->limit($limit)
  98. ->asArray()
  99. ->all();
  100. if (empty($ids)) {
  101. return $data;
  102. }
  103. // 排序后返回
  104. $newData = [];
  105. foreach ($ids as $id) {
  106. foreach ($data as $datum) {
  107. if ($id == $datum['id']) {
  108. $newData[] = $datum;
  109. }
  110. }
  111. }
  112. return $newData;
  113. }
  114. /**
  115. * 获取所有下级id
  116. *
  117. * @param $id
  118. * @return array
  119. */
  120. public function findChildIdsById($id, $merchant_id = '')
  121. {
  122. if ($model = $this->findById($id, $merchant_id)) {
  123. $tree = $model['tree'] . TreeHelper::prefixTreeKey($id);
  124. $list = $this->getChilds($tree);
  125. return ArrayHelper::merge([$id], array_column($list, 'id'));
  126. }
  127. return [];
  128. }
  129. /**
  130. * 获取所有下级
  131. *
  132. * @param $tree
  133. * @return array|\yii\db\ActiveRecord[]
  134. */
  135. public function getChilds($tree)
  136. {
  137. return Cate::find()
  138. ->where(['like', 'tree', $tree . '%', false])
  139. ->andWhere(['>=', 'status', StatusEnum::DISABLED])
  140. ->asArray()
  141. ->all();
  142. }
  143. /**
  144. * 获取所有下级
  145. *
  146. * @param $tree
  147. * @return array|\yii\db\ActiveRecord[]
  148. */
  149. public function getParentIds($ids)
  150. {
  151. $allTree = Cate::find()
  152. ->select(['id', 'tree'])
  153. ->where(['in', 'id', $ids])
  154. ->andWhere(['status' => StatusEnum::ENABLED])
  155. ->asArray()
  156. ->all();
  157. $parentIds = [];
  158. foreach ($allTree as $item) {
  159. $parentIds[$item['id']] = [];
  160. $parentIds[$item['id']][] = $item['id'];
  161. if (!empty(trim($item['tree']))) {
  162. $parentIds[$item['id']] = array_merge($parentIds[$item['id']], explode('-', trim($item['tree'])));
  163. }
  164. }
  165. return $parentIds;
  166. }
  167. /**
  168. * @param $id
  169. * @return array|\yii\db\ActiveRecord|null|Cate
  170. */
  171. public function findById($id, $merchant_id = '')
  172. {
  173. $merchant_id === '' && $merchant_id = Yii::$app->services->merchant->getNotNullId();
  174. return Cate::find()
  175. ->where(['id' => $id])
  176. ->andWhere(['>=', 'status', StatusEnum::DISABLED])
  177. ->andWhere(['merchant_id' => $merchant_id])
  178. ->asArray()
  179. ->one();
  180. }
  181. /**
  182. * @param $id
  183. * @return array|\yii\db\ActiveRecord|null|Cate
  184. */
  185. public function findByIds($ids)
  186. {
  187. return Cate::find()
  188. ->select(['id'])
  189. ->where(['in', 'id', $ids])
  190. ->andWhere(['>=', 'status', StatusEnum::DISABLED])
  191. ->andWhere(['merchant_id' => Yii::$app->services->merchant->getNotNullId()])
  192. ->asArray()
  193. ->column();
  194. }
  195. /**
  196. * 根据ID获取所有分类
  197. *
  198. * @param $ids
  199. * @return array|\yii\db\ActiveRecord[]
  200. */
  201. public function findAllByIds($ids)
  202. {
  203. return Cate::find()
  204. ->where(['in', 'id', $ids])
  205. ->andWhere(['>=', 'status', StatusEnum::DISABLED])
  206. ->asArray()
  207. ->all();
  208. }
  209. /**
  210. * @param $level
  211. * @return array|\yii\db\ActiveRecord[]
  212. */
  213. public function findByPId($id)
  214. {
  215. return Cate::find()
  216. ->where(['status' => StatusEnum::ENABLED, 'pid' => $id])
  217. ->asArray()
  218. ->all();
  219. }
  220. /**
  221. * @param $id
  222. * @return array|\yii\db\ActiveRecord|null|Cate
  223. */
  224. public function findAll($merchant_id = '')
  225. {
  226. $merchant_id === '' && $merchant_id = Yii::$app->services->merchant->getNotNullId();
  227. return Cate::find()
  228. ->where(['status' => StatusEnum::ENABLED])
  229. ->andWhere(['merchant_id' => $merchant_id])
  230. ->asArray()
  231. ->cache(30)
  232. ->all();
  233. }
  234. }
粤ICP备19079148号