BrandServiceImpl.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. package com.om.service.impl;
  2. import cn.hutool.core.bean.BeanUtil;
  3. import cn.hutool.core.collection.CollectionUtil;
  4. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  5. import com.om.entity.dto.AppBaseDTO;
  6. import com.om.entity.dto.BrandDTO;
  7. import com.om.entity.dto.BrandQueryPageDTO;
  8. import com.om.entity.dto.GuidDTO;
  9. import com.om.entity.po.Brand;
  10. import com.om.entity.po.BrandClient;
  11. import com.om.entity.po.Client;
  12. import com.om.entity.vo.APPBrandVO;
  13. import com.om.entity.vo.BrandQueryPageVO;
  14. import com.om.entity.vo.BrandVO;
  15. import com.om.exception.BadReqException;
  16. import com.om.mapper.BrandMapper;
  17. import com.om.service.IBrandClientService;
  18. import com.om.service.IBrandService;
  19. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  20. import com.om.service.IClientService;
  21. import com.om.utils.*;
  22. import org.springframework.stereotype.Service;
  23. import org.springframework.transaction.annotation.Transactional;
  24. import javax.annotation.Resource;
  25. import java.time.LocalDateTime;
  26. import java.time.ZoneOffset;
  27. import java.util.ArrayList;
  28. import java.util.Collections;
  29. import java.util.List;
  30. import java.util.stream.Collectors;
  31. /**
  32. * <p>
  33. * 服务实现类
  34. * </p>
  35. *
  36. * @author bmmx
  37. * @since 2024-02-07
  38. */
  39. @Service
  40. public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements IBrandService {
  41. @Resource
  42. private IBrandClientService brandClientService;
  43. @Resource
  44. private JwtUtils jwtUtils;
  45. @Resource
  46. private IClientService clientService;
  47. @Override
  48. @Transactional
  49. public Result createBrand(BrandDTO dto) {
  50. if (BeanUtil.isEmpty(dto)) {
  51. throw new BadReqException("数据为空");
  52. }
  53. //保存品牌
  54. Brand brand = BeanUtil.copyProperties(dto, Brand.class);
  55. LocalDateTime now = LocalDateTime.now();
  56. brand.setCreateTime(now);
  57. brand.setUpdateTime(now);
  58. this.save(brand);
  59. //保存客户端和品牌的关联
  60. List<Integer> clientIds = dto.getClientIds();
  61. if (!CollectionUtil.isEmpty(clientIds)) {
  62. //重新查询一遍
  63. Brand one = this.getById(brand);
  64. List<BrandClient> brandClients = new ArrayList<>();
  65. for (Integer clientId : clientIds) {
  66. BrandClient brandClient = new BrandClient();
  67. brandClient.setBrandId(one.getId());
  68. brandClient.setClientId(clientId);
  69. brandClients.add(brandClient);
  70. }
  71. // 批量保存
  72. brandClientService.saveBatch(brandClients);
  73. }
  74. return Result.ok();
  75. }
  76. @Override
  77. @Transactional
  78. public Result deleteBrand(Integer id) {
  79. // 根据id查询数据库
  80. Brand brand = this.getById(id);
  81. // 判断是否存在
  82. if (BeanUtil.isEmpty(brand)) {
  83. throw new BadReqException("该品牌不存在");
  84. }
  85. //先 删除 品牌和客户端的关联表
  86. List<BrandClient> list = brandClientService.lambdaQuery()
  87. .eq(BrandClient::getBrandId, brand.getId())
  88. .list();
  89. if (!CollectionUtil.isEmpty(list)) {
  90. List<Integer> collect = list.stream().map(c -> c.getId()).collect(Collectors.toList());
  91. brandClientService.removeByIds(collect);
  92. }
  93. //删除品牌表
  94. this.removeById(brand);
  95. return Result.ok();
  96. }
  97. @Override
  98. public Result<BrandVO> queryById(Integer id) {
  99. //根据id查询
  100. Brand brand = this.getById(id);
  101. //判断是否存在
  102. if (BeanUtil.isEmpty(brand)) {
  103. throw new BadReqException("该品牌不存在");
  104. }
  105. //封装vo
  106. BrandVO brandVO = BeanUtil.copyProperties(brand, BrandVO.class);
  107. //查询 品牌和客户端的关联表
  108. List<BrandClient> list = brandClientService.lambdaQuery()
  109. .eq(BrandClient::getBrandId, id)
  110. .list();
  111. if (!CollectionUtil.isEmpty(list)) {
  112. List<Integer> clientIds = list.stream().map(c -> c.getClientId()).collect(Collectors.toList());
  113. brandVO.setClientIds(clientIds);
  114. }
  115. return Result.ok(brandVO);
  116. }
  117. @Override
  118. @Transactional
  119. public Result updateBrand(BrandDTO dto) {
  120. if (BeanUtil.isEmpty(dto)) {
  121. throw new BadReqException("数据为空");
  122. }
  123. //修改品牌
  124. //根据dto查询数据库
  125. Brand brand = this.getById(dto.getId());
  126. if (BeanUtil.isEmpty(brand)) {
  127. throw new BadReqException("该品牌不存在");
  128. }
  129. BeanUtil.copyProperties(dto, brand);
  130. LocalDateTime now = LocalDateTime.now();
  131. brand.setUpdateTime(now);
  132. this.updateById(brand);
  133. //修改 客户端和品牌的关联
  134. //先删除 后新增
  135. //批量查询
  136. List<BrandClient> brandClients = brandClientService.lambdaQuery()
  137. .eq(BrandClient::getBrandId, brand.getId())
  138. .list();
  139. if (!CollectionUtil.isEmpty(brandClients)) {
  140. //删除
  141. List<Integer> ids = brandClients.stream().map(c -> c.getId()).collect(Collectors.toList());
  142. brandClientService.removeByIds(ids);
  143. }
  144. //新增
  145. List<Integer> clientIds = dto.getClientIds();
  146. if (!CollectionUtil.isEmpty(clientIds)) {
  147. List<BrandClient> brandClientList = new ArrayList<>();
  148. for (Integer clientId : clientIds) {
  149. BrandClient brandClient = new BrandClient();
  150. brandClient.setClientId(clientId);
  151. brandClient.setBrandId(brand.getId());
  152. brandClientList.add(brandClient);
  153. }
  154. // 批量保存
  155. brandClientService.saveBatch(brandClientList);
  156. }
  157. return Result.ok();
  158. }
  159. @Override
  160. public Result<List<BrandVO>> queryList() {
  161. List<Brand> list = this.list();
  162. List<BrandVO> brandVOS = BeanUtil.copyToList(list, BrandVO.class);
  163. if (CollectionUtil.isEmpty(brandVOS)) {
  164. return Result.ok(Collections.emptyList());
  165. }
  166. //根据品牌ids 批量查询
  167. for (BrandVO brandVO : brandVOS) {
  168. List<BrandClient> brandClients = brandClientService.lambdaQuery()
  169. .eq(BrandClient::getBrandId, brandVO.getId())
  170. .list();
  171. List<Integer> collect = brandClients.stream().map(c -> c.getClientId()).collect(Collectors.toList());
  172. brandVO.setClientIds(collect);
  173. }
  174. return Result.ok(brandVOS);
  175. }
  176. @Override
  177. public Result<BrandQueryPageVO> queryPageList(BrandQueryPageDTO dto) {
  178. //从dto中取数据
  179. Integer pageIndex = dto.getPageIndex();
  180. Integer pageSize = dto.getPageSize();
  181. String searchCn = dto.getSearchCn();
  182. String searchEn = dto.getSearchEn();
  183. //分页查询
  184. Page<Brand> page = this.lambdaQuery()
  185. .like(searchCn != null, Brand::getBrandCn, searchCn)
  186. .like(searchEn != null, Brand::getBrandEn, searchEn)
  187. .orderByDesc(Brand::getCreateTime)
  188. .page(new Page<>(pageIndex, pageSize));
  189. //封装vo
  190. BrandQueryPageVO brandQueryPageVO = new BrandQueryPageVO();
  191. brandQueryPageVO.setCurrent((int) page.getCurrent());
  192. brandQueryPageVO.setSize((int) page.getSize());
  193. brandQueryPageVO.setPages((int) page.getPages());
  194. brandQueryPageVO.setTotal((int) page.getTotal());
  195. if (searchCn !=null || searchEn !=null){
  196. brandQueryPageVO.setSearchCount(true);
  197. }
  198. List<Brand> records = page.getRecords();
  199. List<BrandVO> list = new ArrayList<>();
  200. for (Brand record : records) {
  201. BrandVO brandVO = BeanUtil.copyProperties(record, BrandVO.class);
  202. //根据id查询 关联表
  203. List<BrandClient> clients = brandClientService.lambdaQuery()
  204. .eq(BrandClient::getBrandId, record.getId())
  205. .list();
  206. if (!CollectionUtil.isEmpty(clients)){
  207. List<Integer> collect = clients.stream().map(c -> c.getClientId()).collect(Collectors.toList());
  208. brandVO.setClientIds(collect);
  209. }
  210. list.add(brandVO);
  211. }
  212. brandQueryPageVO.setRecords(list);
  213. return Result.ok(brandQueryPageVO);
  214. }
  215. @Override
  216. public Result getAPPList(AppBaseDTO dto) {
  217. if (BeanUtil.isEmpty(dto)){
  218. return Result.fail("未获取到数据").result(ResultCode.NO_DATA);
  219. }
  220. String guid = dto.getGuid();
  221. GuidDTO guidDTO = AesUtil.getGuidDTOFromGuid(guid);
  222. String clientNum = guidDTO.getClientNum();
  223. //根据客户端编号查询客户端id
  224. Client client = clientService.lambdaQuery()
  225. .eq(Client::getNumber, clientNum)
  226. .one();
  227. if (BeanUtil.isEmpty(client)){
  228. throw new BadReqException("该客户端不存在!!");
  229. }
  230. //根据客户端id查询品牌客户端关联表
  231. List<BrandClient> brandClients = brandClientService.lambdaQuery()
  232. .eq(BrandClient::getClientId, client.getId())
  233. .list();
  234. List<Integer> brandids = brandClients.stream().map(c -> c.getBrandId()).collect(Collectors.toList());
  235. if (brandids.isEmpty()){
  236. return Result.ok();
  237. }
  238. List<Brand> brands = this.listByIds(brandids);
  239. List<APPBrandVO> brandVOS = new ArrayList<>();
  240. for (Brand brand : brands) {
  241. APPBrandVO brandVO = BeanUtil.copyProperties(brand, APPBrandVO.class);
  242. //查询brand_client
  243. List<Integer> clientIds = brandClientService.lambdaQuery()
  244. .eq(BrandClient::getBrandId, brand.getId())
  245. .list().stream().map(c -> c.getClientId()).collect(Collectors.toList());
  246. if (clientIds.isEmpty()){
  247. throw new BadReqException("该品牌下没有查询到客户端");
  248. }
  249. brandVO.setClientIds(clientIds);
  250. brandVO.setCreateTime(brand.getCreateTime().toInstant(ZoneOffset.of("+8")).toEpochMilli());
  251. brandVOS.add(brandVO);
  252. }
  253. return Result.ok(brandVOS);
  254. }
  255. }