package com.metro.service.impl; import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.util.ObjectUtil; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.metro.constant.UserConstant; import com.metro.entity.po.Account; import com.metro.entity.po.Area; import com.metro.entity.po.Company; import com.metro.entity.po.Police; import com.metro.entity.ro.Police.*; import com.metro.entity.ro.account.*; import com.metro.exception.BusinessException; import com.metro.mapper.AccountsMapper; import com.metro.mapper.CompanysMapper; import com.metro.mapper.PoliceMapper; import com.metro.service.PoliceService; import com.metro.service.base.BaseService; import com.metro.utils.MD5UtilNew; import com.metro.utils.MapperManager; import com.metro.utils.StringUtil; import io.github.classgraph.utils.Join; import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.StringUtils; import org.assertj.core.util.Lists; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.CollectionUtils; import java.math.BigDecimal; import java.time.LocalDateTime; import java.util.*; import java.util.stream.Collectors; import static com.metro.service.impl.AccountServiceImpl.getUpdateAccountResp; /** * @ClassName: PoliceServiceImpl * @Description: TODO * @Author: ZS * @CreateName: lws * @Date 2022/3/18 1:25 * ... */ @Service public class PoliceServiceImpl extends BaseService implements PoliceService { @Autowired private PoliceMapper mapper; @Autowired private CompanysMapper companysMapper; @Autowired private AccountsMapper accountsMapper; @Override public IPage page(SelectPolicePageReq req) { Page companyPage = companysMapper.selectPage(new Page<>(req.getPageNum(), req.getPageSize()), pageWrapper(req)); if (CollectionUtils.isEmpty(companyPage.getRecords())) return new Page<>(); List accounts = accountsMapper.selectList(new LambdaQueryWrapper() .like(StringUtils.isNotEmpty(req.getAddress()), Account::getAddress, req.getAddress()) .like(StringUtils.isNotEmpty(req.getUserName()), Account::getUserName, req.getUserName()) .like(StringUtils.isNotEmpty(req.getPhone()), Account::getPhone, req.getPhone()) .in(Account::getCompanyId, companysMapper.selectList(new LambdaQueryWrapper() .in(Company::getCompanyId, companyPage.getRecords().stream().map(Company::getCompanyId).collect(Collectors.toSet()))) .stream().map(Company::getId).collect(Collectors.toSet()) )); if (CollectionUtils.isEmpty(accounts)) return new Page<>(); Map collect = accounts.stream().collect(Collectors.toMap(Account::getCompanyId, p -> p, (t1, t2) -> t1)); return companyPage.convert(p -> MapperManager.mapper(p, SelectPolicePageResp.class, q -> { q.setCompanyName(p.getBranchName()).setId(p.getId()); if (!CollectionUtils.isEmpty(collect)) { Account account = collect.get(p.getId() + ""); if (ObjectUtil.isNotEmpty(account)) { q.setCreateTime(account.getCreateTime()).setAccount(account.getAccount()).setAddress(account.getAddress()).setPhone(account.getPhone()).setUserName(account.getUserName()); } } })); } /* * 查询条件 * */ private LambdaQueryWrapper pageWrapper(SelectPolicePageReq req) { LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); wrapper.eq(Company::getCompanyId, getAccountInfo().getCompanyId()); wrapper.like(StringUtils.isNotEmpty(req.getCompanyName()), Company::getBranchName, req.getCompanyName()); return wrapper; } /* * 新增 * */ @Override @Transactional public InsertPoliceResp add(InsertPoliceReq req) { if (getAccountInfo().getType() != 2) throw new BusinessException("非分局管理员不能注册"); Police account = mapper.selectOne(new LambdaQueryWrapper().eq(Police::getAccount, req.getAccount()).eq(Police::getIsDelete, UserConstant.NOT_DELETE)); if (ObjectUtil.isNotEmpty(account)) throw new BusinessException(account.getAccount() + "已经存在!不能注册"); List companies = companysMapper.selectList(new LambdaQueryWrapper().eq(Company::getCompanyId, getAccountInfo().getCompanyId()).eq(Company::getBranchName, req.getBranchName())); if (!CollectionUtils.isEmpty(companies)) throw new BusinessException("派出所已经存在,请重新填写派所名"); Company company = companysMapper.selectList(new LambdaQueryWrapper().eq(Company::getCompanyId, getAccountInfo().getCompanyId()).orderByDesc(Company::getBranchId)).stream().findFirst().orElse(null); int insert1 = companysMapper.insert(company.setBranchId(String.valueOf(Integer.valueOf(company.getBranchId()) + 1)).setCompanyId(getAccountInfo().getCompanyId()).setBranchName(req.getBranchName())); if (insert1 == BigDecimal.ZERO.intValue()) throw new BusinessException("创建派出所失败!"); int insert = mapper.insert(MapperManager.mapper(req, Police.class, p -> p.setPassword(MD5UtilNew.string2MD5(req.getPassword())) .setCreateTime(LocalDateTime.now()).setCompanyId(company.getId() + "").setType(getAccountInfo().setAreaId(getAccountInfo().getAreaId()).getType() + 1))); if (insert == BigDecimal.ZERO.intValue()) throw new BusinessException("创建派出所管理员账号异常!"); return new InsertPoliceResp().setCount(insert + insert1); } /* * 查看详情 * */ @Override public DetailPoliceResp detail(DetailPoliceReq req) { if (getAccountInfo().getType() != 2) throw new BusinessException("非分局管理员不能修改!"); Company selectOne = companysMapper.selectOne(new LambdaQueryWrapper().eq(Company::getId, req.getId())); if (ObjectUtils.isEmpty(selectOne)) throw new BusinessException("未获取到派出所信息"); Police police = mapper.selectList(new LambdaQueryWrapper().eq(Police::getCompanyId, selectOne.getId()).eq(Police::getIsDelete, UserConstant.NOT_DELETE)).stream().findFirst().orElse(null); return MapperManager.mapper(police, DetailPoliceResp.class, p -> p.setId(selectOne.getId()).setBranchName(ObjectUtil.isEmpty(selectOne.getBranchName()) ? null : selectOne.getBranchName())); } /* * 修改账号 * */ @Override @Transactional public UpdatePoliceResp update(UpdatePoliceReq req) { if (getAccountInfo().getType() != 2) throw new BusinessException("非分局管理员不能修改!"); Police account = mapper.selectOne(new LambdaQueryWrapper().eq(Police::getAccount, req.getAccount()).eq(Police::getIsDelete, UserConstant.NOT_DELETE)); if (ObjectUtil.isEmpty(account)) throw new BusinessException(req.getAccount() + "已经停用或者不存在!不能修改"); int insert = mapper.update(MapperManager.mapper(req, Police.class, p -> p.setPassword(MD5UtilNew.string2MD5(req.getPassword())).setUpdateTime(LocalDateTime.now())), new LambdaQueryWrapper().eq(Police::getAccount, account.getAccount()) .eq(Police::getIsDelete, UserConstant.NOT_DELETE)); if (insert == BigDecimal.ZERO.intValue()) throw new BusinessException("修改派出所管理员账号异常!"); Company selectOne = companysMapper.selectOne(new LambdaQueryWrapper().eq(Company::getId, req.getId())); if (ObjectUtils.isEmpty(selectOne)) throw new BusinessException("没有该派出所"); List companies = companysMapper.selectList(new LambdaQueryWrapper().eq(Company::getCompanyId, selectOne.getCompanyId())); long count = companies.stream().filter(p -> !p.getBranchName().equals(selectOne.getBranchName())).filter(p -> p.getBranchName().equals(req.getBranchName())).count(); if(count != BigDecimal.ZERO.longValue()) throw new BusinessException("派出所名称已经存在!请更改"); int insert1 = companysMapper.updateById(new Company().setBranchName(req.getBranchName()).setId(req.getId())); if (insert1 == BigDecimal.ZERO.intValue()) throw new BusinessException("修改派出所失败!"); return new UpdatePoliceResp().setCount(insert + insert1); } /* * 删除账号 * */ @Override @Transactional public DeleteAccountResp delete(DeleteAccountReq req) { if (getAccountInfo().getType() != 2) throw new BusinessException("非分局管理员不能删除!"); Company selectOne = companysMapper.selectOne(new LambdaQueryWrapper().eq(Company::getId, req.getId())); if (ObjectUtils.isEmpty(selectOne)) throw new BusinessException("未获取到派出所信息"); List police = mapper.selectList(new LambdaQueryWrapper().eq(Police::getId, selectOne.getId()).eq(Police::getIsDelete, UserConstant.NOT_DELETE)); if (CollectionUtils.isEmpty(police)) throw new BusinessException("派出所下有用户,不能删除"); return new DeleteAccountResp().setCount(companysMapper.deleteById(req.getId())); } /** * 派出所下拉查询(分局、派出所) * * @param req * @return */ @Override public List selectLevel(SelectPoliceLevelReq req) { List companies = companysMapper.selectList(new LambdaQueryWrapper().eq(StringUtils.isNotEmpty(req.getCompanyId()), Company::getCompanyId, req.getCompanyId())); if (CollectionUtils.isEmpty(companies)) return Lists.newArrayList(); ArrayList companyList = companies.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Company::getCompanyId))), ArrayList::new)); if (!req.getIsQueryBranch()) { return companyList.stream().map(p -> MapperManager.mapper(p, SelectPoliceLevelResp.class, q -> q.setId(p.getCompanyId()).setName(p.getCompanyName()))).collect(Collectors.toList()); } return companyList.stream().map(p -> MapperManager.mapper(p, SelectPoliceLevelResp.class, q -> { List selectList = companysMapper.selectList(new LambdaQueryWrapper().eq(Company::getCompanyId, p.getCompanyId()) .eq(StringUtils.isNotEmpty(req.getBranchId()), Company::getId, req.getBranchId())); if (!CollectionUtils.isEmpty(selectList)) { q.setId(p.getCompanyId()).setName(p.getCompanyName()).setBranchList(selectList.stream().map( t -> MapperManager.mapper(t, SelectPoliceLevelResp.BranchList.class, m -> m.setId(t.getBranchId()).setName(t.getBranchName())) ).collect(Collectors.toList())); } })).collect(Collectors.toList()); } }