StoreBrandServices.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2021 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\product\brand;
  12. use app\dao\product\brand\StoreBrandDao;
  13. use app\jobs\product\ProductCategoryBrandJob;
  14. use app\services\BaseServices;
  15. use crmeb\exceptions\AdminException;
  16. /**
  17. * 商品品牌
  18. * Class StoreBrandServices
  19. * @package app\services\product\brand
  20. * @mixin StoreBrandDao
  21. */
  22. class StoreBrandServices extends BaseServices
  23. {
  24. public function __construct(StoreBrandDao $dao)
  25. {
  26. $this->dao = $dao;
  27. }
  28. /**
  29. * 获取缓存
  30. * @param int $id
  31. * @return array|false|mixed|string|null
  32. * @throws \think\db\exception\DataNotFoundException
  33. * @throws \think\db\exception\DbException
  34. * @throws \think\db\exception\ModelNotFoundException
  35. * @author 等风来
  36. * @email 136327134@qq.com
  37. * @date 2022/11/3
  38. */
  39. public function getCacheBrandInfo(int $id)
  40. {
  41. $storeBrandInfo = [];
  42. if ($id) {
  43. $storeBrandInfo = $this->dao->cacheRemember($id, function () use ($id) {
  44. $storeBrandInfo = $this->dao->get(['id' => $id, 'is_show' => 1, 'is_del' => 0]);
  45. if ($storeBrandInfo) {
  46. $storeBrandInfo = $storeBrandInfo->toArray();
  47. }
  48. return $storeBrandInfo ?: [];
  49. });
  50. }
  51. return $storeBrandInfo;
  52. }
  53. /**
  54. * 获取品牌列表
  55. * @param $where
  56. * @return array
  57. * @throws \think\db\exception\DataNotFoundException
  58. * @throws \think\db\exception\DbException
  59. * @throws \think\db\exception\ModelNotFoundException
  60. */
  61. public function getTreeList($where)
  62. {
  63. $list = $this->dao->getList($where, ['product']);
  64. if (!empty($list) && $where['brand_name'] !== '') {
  65. $fid = [];
  66. foreach ($list as $item) {
  67. $fid = array_merge($fid, explode(',', $item['fid']));
  68. }
  69. $pids = array_unique(array_filter($fid));
  70. $parentList = $this->dao->getList(['id' => $pids], ['product']);
  71. $list = array_merge($list, $parentList);
  72. foreach ($list as $key => $item) {
  73. $arr = $list[$key];
  74. unset($list[$key]);
  75. if (!in_array($arr, $list)) {
  76. $list[] = $arr;
  77. }
  78. }
  79. }
  80. foreach ($list as &$item) {
  81. $item['brand_num'] = $item['product'][0]['brand_num'] ?? 0;
  82. $item['fid'] = $item['fid'] ? array_map('intval', explode(',', $item['fid'])) : [];
  83. $item['type'] = count($item['fid']) < 2 ? 1 : 0;
  84. //添加子品牌fid
  85. if ($item['type'] == 1) {
  86. $item['fid_son'] = $item['fid'];
  87. array_push($item['fid_son'], $item['id']);
  88. }
  89. unset($item['product']);
  90. }
  91. $list = get_tree_children($list);
  92. $count = $this->dao->count($where);
  93. return compact('list', 'count');
  94. }
  95. /**
  96. * 获取品牌列表
  97. * @param $where
  98. * @return array
  99. * @throws \think\db\exception\DataNotFoundException
  100. * @throws \think\db\exception\DbException
  101. * @throws \think\db\exception\ModelNotFoundException
  102. */
  103. public function getList($where)
  104. {
  105. $list = $this->dao->getList($where, ['product', 'children']);
  106. $count = $this->dao->count($where);
  107. if ($list) {
  108. foreach ($list as &$item) {
  109. $item['brand_num'] = $item['product'][0]['brand_num'] ?? 0;
  110. $item['fid'] = $item['fid'] ? array_map('intval', explode(',', $item['fid'])) : [];
  111. $item['type'] = count($item['fid']) < 2 ? 1 : 0;
  112. //添加子品牌fid
  113. if ($item['type'] == 1) {
  114. $item['fid_son'] = $item['fid'];
  115. array_push($item['fid_son'], $item['id']);
  116. }
  117. if (isset($item['children']) && $item['children']) {
  118. $item['children'] = [];
  119. $item['loading'] = false;
  120. $item['_loading'] = false;
  121. } else {
  122. unset($item['children']);
  123. }
  124. unset($item['product']);
  125. }
  126. }
  127. return compact('list', 'count');
  128. }
  129. /**
  130. * 获取品牌cascader
  131. * @param string $show
  132. * @param int $type
  133. * @return array
  134. * @throws \think\db\exception\DataNotFoundException
  135. * @throws \think\db\exception\DbException
  136. * @throws \think\db\exception\ModelNotFoundException
  137. */
  138. public function cascaderList($type = 1)
  139. {
  140. $where = [];
  141. if ($type == 1) {
  142. $top = true;
  143. } else {
  144. $top = false;
  145. }
  146. $menus = [];
  147. $where['is_del'] = 0;
  148. $where['is_show'] = 1;
  149. $list = get_tree_children($this->dao->getList($where, [], ['id as value', 'brand_name as label', 'pid']), 'children', 'value');
  150. if ($top) {
  151. $menus = [['value' => 0, 'label' => '顶级品牌']];
  152. foreach ($list as &$item) {
  153. if (isset($item['children']) && $item['children']) {
  154. foreach ($item['children'] as &$val) {
  155. if (isset($val['children']) && $val['children']) {
  156. unset($val['children']);
  157. }
  158. }
  159. }
  160. }
  161. }
  162. $menus = array_merge($menus, $list);
  163. return $menus;
  164. }
  165. /**
  166. * 设置品牌状态
  167. * @param $id
  168. * @param $is_show
  169. */
  170. public function setShow(int $id, int $is_show)
  171. {
  172. $res = $this->dao->update($id, ['is_show' => $is_show]);
  173. // $res = $res && $this->dao->update($id, ['is_show' => $is_show], 'pid');
  174. if (!$res) {
  175. throw new AdminException('设置失败');
  176. }
  177. //设置缓存
  178. if (!$is_show) {
  179. $this->cacheDelById($id);
  180. return;
  181. }
  182. $branInfo = $this->dao->cacheInfoById($id);
  183. if ($branInfo) {
  184. $branInfo['is_show'] = 1;
  185. } else {
  186. $branInfo = $this->dao->get($id);
  187. if (!$branInfo) {
  188. return;
  189. }
  190. $branInfo = $branInfo->toArray();
  191. }
  192. $this->dao->cacheUpdate($branInfo);
  193. //修改关联
  194. ProductCategoryBrandJob::dispatchDo('setShow', [$id, 'brand_id', 'status', $is_show]);
  195. return true;
  196. }
  197. /**
  198. * 保存新增数据
  199. * @param $data
  200. */
  201. public function createData($data)
  202. {
  203. $data['pid'] = end($data['fid']);
  204. if ($this->dao->getOne(['brand_name' => $data['brand_name'], 'pid' => $data['pid']])) {
  205. throw new AdminException('该品牌已经存在');
  206. }
  207. $data['fid'] = implode(',', $data['fid']);
  208. $data['add_time'] = time();
  209. $res = $this->dao->save($data);
  210. if (!$res) throw new AdminException('添加失败');
  211. //更新缓存
  212. if ($data['is_show']) {
  213. $data['id'] = $res->id;
  214. $this->cacheUpdate($data);
  215. }
  216. }
  217. /**
  218. * 保存修改数据
  219. * @param $id
  220. * @param $data
  221. */
  222. public function editData($id, $data)
  223. {
  224. $cate = $this->dao->getOne(['id' => $id]);
  225. if (!$cate) {
  226. throw new AdminException('该品牌不存在');
  227. }
  228. $data['pid'] = end($data['fid']) ?? 0;
  229. if ($data['pid']) {
  230. $pcate = $this->dao->getOne(['id' => $data['pid']]);
  231. if (!$pcate) {
  232. throw new AdminException('上级品牌不存在');
  233. }
  234. if ($pcate['pid'] == $id) {
  235. throw new AdminException('上级品牌不能是当前品牌的下级');
  236. }
  237. }
  238. $data['fid'] = implode(',', $data['fid']);
  239. $cate = $this->dao->getOne(['pid' => $data['pid'], 'brand_name' => $data['brand_name']]);
  240. if ($cate && $cate['id'] != $id) {
  241. throw new AdminException('该品牌已经存在');
  242. }
  243. $res = $this->dao->update($id, $data);
  244. if (!$res) throw new AdminException('修改失败');
  245. //更新缓存
  246. if ($data['is_show']) {
  247. $data['id'] = $res->id;
  248. $this->cacheUpdate($data);
  249. }
  250. }
  251. /**
  252. * 删除数据
  253. * @param int $id
  254. */
  255. public function del(int $id)
  256. {
  257. if ($this->dao->count(['pid' => $id])) {
  258. throw new AdminException('请先删除子品牌!');
  259. }
  260. $res = $this->dao->delete($id);
  261. if (!$res) throw new AdminException('删除失败');
  262. //更新缓存
  263. $this->cacheDelById($id);
  264. //修改关联
  265. ProductCategoryBrandJob::dispatchDo('setShow', [$id, 'brand_id', 'is_del', 1]);
  266. return true;
  267. }
  268. }