PoliceServiceImpl.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. package com.metro.service.impl;
  2. import cn.hutool.core.bean.BeanUtil;
  3. import cn.hutool.core.util.ObjectUtil;
  4. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  5. import com.baomidou.mybatisplus.core.metadata.IPage;
  6. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  7. import com.metro.constant.UserConstant;
  8. import com.metro.entity.po.Account;
  9. import com.metro.entity.po.Area;
  10. import com.metro.entity.po.Company;
  11. import com.metro.entity.po.Police;
  12. import com.metro.entity.ro.Police.*;
  13. import com.metro.entity.ro.account.*;
  14. import com.metro.exception.BusinessException;
  15. import com.metro.mapper.AccountsMapper;
  16. import com.metro.mapper.CompanysMapper;
  17. import com.metro.mapper.PoliceMapper;
  18. import com.metro.service.PoliceService;
  19. import com.metro.service.base.BaseService;
  20. import com.metro.utils.MD5UtilNew;
  21. import com.metro.utils.MapperManager;
  22. import com.metro.utils.StringUtil;
  23. import io.github.classgraph.utils.Join;
  24. import org.apache.commons.lang3.ObjectUtils;
  25. import org.apache.commons.lang3.StringUtils;
  26. import org.assertj.core.util.Lists;
  27. import org.springframework.beans.factory.annotation.Autowired;
  28. import org.springframework.stereotype.Service;
  29. import org.springframework.transaction.annotation.Transactional;
  30. import org.springframework.util.CollectionUtils;
  31. import java.math.BigDecimal;
  32. import java.time.LocalDateTime;
  33. import java.util.*;
  34. import java.util.stream.Collectors;
  35. import static com.metro.service.impl.AccountServiceImpl.getUpdateAccountResp;
  36. /**
  37. * @ClassName: PoliceServiceImpl
  38. * @Description: TODO
  39. * @Author: ZS
  40. * @CreateName: lws
  41. * @Date 2022/3/18 1:25
  42. * ...
  43. */
  44. @Service
  45. public class PoliceServiceImpl extends BaseService implements PoliceService {
  46. @Autowired
  47. private PoliceMapper mapper;
  48. @Autowired
  49. private CompanysMapper companysMapper;
  50. @Autowired
  51. private AccountsMapper accountsMapper;
  52. @Override
  53. public IPage<SelectPolicePageResp> page(SelectPolicePageReq req) {
  54. Page<Company> companyPage = companysMapper.selectPage(new Page<>(req.getPageNum(), req.getPageSize()), pageWrapper(req));
  55. if (CollectionUtils.isEmpty(companyPage.getRecords())) return new Page<>();
  56. List<Account> accounts = accountsMapper.selectList(new LambdaQueryWrapper<Account>()
  57. .like(StringUtils.isNotEmpty(req.getAddress()), Account::getAddress, req.getAddress())
  58. .like(StringUtils.isNotEmpty(req.getUserName()), Account::getUserName, req.getUserName())
  59. .like(StringUtils.isNotEmpty(req.getPhone()), Account::getPhone, req.getPhone())
  60. .in(Account::getCompanyId,
  61. companysMapper.selectList(new LambdaQueryWrapper<Company>()
  62. .in(Company::getCompanyId, companyPage.getRecords().stream().map(Company::getCompanyId).collect(Collectors.toSet())))
  63. .stream().map(Company::getId).collect(Collectors.toSet())
  64. ));
  65. if (CollectionUtils.isEmpty(accounts)) return new Page<>();
  66. Map<String, Account> collect = accounts.stream().collect(Collectors.toMap(Account::getCompanyId, p -> p, (t1, t2) -> t1));
  67. return companyPage.convert(p -> MapperManager.mapper(p, SelectPolicePageResp.class, q -> {
  68. q.setCompanyName(p.getBranchName()).setId(p.getId());
  69. if (!CollectionUtils.isEmpty(collect)) {
  70. Account account = collect.get(p.getId() + "");
  71. if (ObjectUtil.isNotEmpty(account)) {
  72. q.setCreateTime(account.getCreateTime()).setAccount(account.getAccount()).setAddress(account.getAddress()).setPhone(account.getPhone()).setUserName(account.getUserName());
  73. }
  74. }
  75. }));
  76. }
  77. /*
  78. * 查询条件
  79. * */
  80. private LambdaQueryWrapper<Company> pageWrapper(SelectPolicePageReq req) {
  81. LambdaQueryWrapper<Company> wrapper = new LambdaQueryWrapper<>();
  82. wrapper.eq(Company::getCompanyId, getAccountInfo().getCompanyId());
  83. wrapper.like(StringUtils.isNotEmpty(req.getCompanyName()), Company::getBranchName, req.getCompanyName());
  84. return wrapper;
  85. }
  86. /*
  87. * 新增
  88. * */
  89. @Override
  90. @Transactional
  91. public InsertPoliceResp add(InsertPoliceReq req) {
  92. if (getAccountInfo().getType() != 2) throw new BusinessException("非分局管理员不能注册");
  93. Police account = mapper.selectOne(new LambdaQueryWrapper<Police>().eq(Police::getAccount, req.getAccount()).eq(Police::getIsDelete, UserConstant.NOT_DELETE));
  94. if (ObjectUtil.isNotEmpty(account)) throw new BusinessException(account.getAccount() + "已经存在!不能注册");
  95. List<Company> companies = companysMapper.selectList(new LambdaQueryWrapper<Company>().eq(Company::getCompanyId, getAccountInfo().getCompanyId()).eq(Company::getBranchName, req.getBranchName()));
  96. if (!CollectionUtils.isEmpty(companies)) throw new BusinessException("派出所已经存在,请重新填写派所名");
  97. Company company = companysMapper.selectList(new LambdaQueryWrapper<Company>().eq(Company::getCompanyId, getAccountInfo().getCompanyId()).orderByDesc(Company::getBranchId)).stream().findFirst().orElse(null);
  98. int insert1 = companysMapper.insert(company.setBranchId(String.valueOf(Integer.valueOf(company.getBranchId()) + 1)).setCompanyId(getAccountInfo().getCompanyId()).setBranchName(req.getBranchName()));
  99. if (insert1 == BigDecimal.ZERO.intValue()) throw new BusinessException("创建派出所失败!");
  100. int insert = mapper.insert(MapperManager.mapper(req, Police.class, p -> p.setPassword(MD5UtilNew.string2MD5(req.getPassword()))
  101. .setCreateTime(LocalDateTime.now()).setCompanyId(company.getId() + "").setType(getAccountInfo().setAreaId(getAccountInfo().getAreaId()).getType() + 1)));
  102. if (insert == BigDecimal.ZERO.intValue()) throw new BusinessException("创建派出所管理员账号异常!");
  103. return new InsertPoliceResp().setCount(insert + insert1);
  104. }
  105. /*
  106. * 查看详情
  107. * */
  108. @Override
  109. public DetailPoliceResp detail(DetailPoliceReq req) {
  110. if (getAccountInfo().getType() != 2) throw new BusinessException("非分局管理员不能修改!");
  111. Company selectOne = companysMapper.selectOne(new LambdaQueryWrapper<Company>().eq(Company::getId, req.getId()));
  112. if (ObjectUtils.isEmpty(selectOne)) throw new BusinessException("未获取到派出所信息");
  113. Police police = mapper.selectList(new LambdaQueryWrapper<Police>().eq(Police::getCompanyId, selectOne.getId()).eq(Police::getIsDelete, UserConstant.NOT_DELETE)).stream().findFirst().orElse(null);
  114. return MapperManager.mapper(police, DetailPoliceResp.class, p -> p.setId(selectOne.getId()).setBranchName(ObjectUtil.isEmpty(selectOne.getBranchName()) ? null : selectOne.getBranchName()));
  115. }
  116. /*
  117. * 修改账号
  118. * */
  119. @Override
  120. @Transactional
  121. public UpdatePoliceResp update(UpdatePoliceReq req) {
  122. if (getAccountInfo().getType() != 2) throw new BusinessException("非分局管理员不能修改!");
  123. Police account = mapper.selectOne(new LambdaQueryWrapper<Police>().eq(Police::getAccount, req.getAccount()).eq(Police::getIsDelete, UserConstant.NOT_DELETE));
  124. if (ObjectUtil.isEmpty(account)) throw new BusinessException(req.getAccount() + "已经停用或者不存在!不能修改");
  125. int insert = mapper.update(MapperManager.mapper(req, Police.class, p -> p.setPassword(MD5UtilNew.string2MD5(req.getPassword())).setUpdateTime(LocalDateTime.now())), new LambdaQueryWrapper<Police>().eq(Police::getAccount, account.getAccount())
  126. .eq(Police::getIsDelete, UserConstant.NOT_DELETE));
  127. if (insert == BigDecimal.ZERO.intValue()) throw new BusinessException("修改派出所管理员账号异常!");
  128. Company selectOne = companysMapper.selectOne(new LambdaQueryWrapper<Company>().eq(Company::getId, req.getId()));
  129. if (ObjectUtils.isEmpty(selectOne)) throw new BusinessException("没有该派出所");
  130. List<Company> companies = companysMapper.selectList(new LambdaQueryWrapper<Company>().eq(Company::getCompanyId, selectOne.getCompanyId()));
  131. long count = companies.stream().filter(p -> !p.getBranchName().equals(selectOne.getBranchName())).filter(p -> p.getBranchName().equals(req.getBranchName())).count();
  132. if(count != BigDecimal.ZERO.longValue()) throw new BusinessException("派出所名称已经存在!请更改");
  133. int insert1 = companysMapper.updateById(new Company().setBranchName(req.getBranchName()).setId(req.getId()));
  134. if (insert1 == BigDecimal.ZERO.intValue()) throw new BusinessException("修改派出所失败!");
  135. return new UpdatePoliceResp().setCount(insert + insert1);
  136. }
  137. /*
  138. * 删除账号
  139. * */
  140. @Override
  141. @Transactional
  142. public DeleteAccountResp delete(DeleteAccountReq req) {
  143. if (getAccountInfo().getType() != 2) throw new BusinessException("非分局管理员不能删除!");
  144. Company selectOne = companysMapper.selectOne(new LambdaQueryWrapper<Company>().eq(Company::getId, req.getId()));
  145. if (ObjectUtils.isEmpty(selectOne)) throw new BusinessException("未获取到派出所信息");
  146. List<Police> police = mapper.selectList(new LambdaQueryWrapper<Police>().eq(Police::getId, selectOne.getId()).eq(Police::getIsDelete, UserConstant.NOT_DELETE));
  147. if (CollectionUtils.isEmpty(police)) throw new BusinessException("派出所下有用户,不能删除");
  148. return new DeleteAccountResp().setCount(companysMapper.deleteById(req.getId()));
  149. }
  150. /**
  151. * 派出所下拉查询(分局、派出所)
  152. *
  153. * @param req
  154. * @return
  155. */
  156. @Override
  157. public List<SelectPoliceLevelResp> selectLevel(SelectPoliceLevelReq req) {
  158. List<Company> companies = companysMapper.selectList(new LambdaQueryWrapper<Company>().eq(StringUtils.isNotEmpty(req.getCompanyId()), Company::getCompanyId, req.getCompanyId()));
  159. if (CollectionUtils.isEmpty(companies)) return Lists.newArrayList();
  160. ArrayList<Company> companyList = companies.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Company::getCompanyId))), ArrayList::new));
  161. if (!req.getIsQueryBranch()) {
  162. return companyList.stream().map(p -> MapperManager.mapper(p, SelectPoliceLevelResp.class, q -> q.setId(p.getCompanyId()).setName(p.getCompanyName()))).collect(Collectors.toList());
  163. }
  164. return companyList.stream().map(p -> MapperManager.mapper(p, SelectPoliceLevelResp.class, q -> {
  165. List<Company> selectList = companysMapper.selectList(new LambdaQueryWrapper<Company>().eq(Company::getCompanyId, p.getCompanyId())
  166. .eq(StringUtils.isNotEmpty(req.getBranchId()), Company::getId, req.getBranchId()));
  167. if (!CollectionUtils.isEmpty(selectList)) {
  168. q.setId(p.getCompanyId()).setName(p.getCompanyName()).setBranchList(selectList.stream().map(
  169. t -> MapperManager.mapper(t, SelectPoliceLevelResp.BranchList.class, m -> m.setId(t.getBranchId()).setName(t.getBranchName()))
  170. ).collect(Collectors.toList()));
  171. }
  172. })).collect(Collectors.toList());
  173. }
  174. }