liujie
3 天以前 ce531d3bb32f5221b35f9e24d027ef83450ebbc6
ruoyi-system/src/main/java/com/ruoyi/system/service/impl/TErpProcurementServiceImpl.java
@@ -7,6 +7,7 @@
import com.ruoyi.common.core.domain.BaseModel;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.dto.AddProcurementDto;
import com.ruoyi.system.export.PlatformLedgerReportExport;
import com.ruoyi.system.export.ProcurementReportClinicExport;
@@ -15,6 +16,7 @@
import com.ruoyi.system.mapper.*;
import com.ruoyi.system.model.*;
import com.ruoyi.system.query.ClinicProcurementReportQuery;
import com.ruoyi.system.query.PerformanceReportQuery;
import com.ruoyi.system.query.TErpGoodsQuery;
import com.ruoyi.system.query.TErpProcurementQuery;
import com.ruoyi.system.service.TErpGoodsService;
@@ -111,6 +113,14 @@
    @Resource
    private TCrmWarehouseMapper crmWarehouseMapper;
    @Resource
    private TCrmBranchMapper crmBranchMapper;
    @Resource
    private TCrmSalespersonMapper crmSalespersonMapper;
    @Resource
    private TCrmChangePointsMapper crmChangePointsMapper;
    @Resource
    private TErpProcurementCommissionMapper erpProcurementCommissionMapper;
    @Override
@@ -149,25 +159,25 @@
        List<String> ids = list.stream().map(TErpProcurementVo::getId).collect(Collectors.toList());
        List<TErpProcurementGoods> tErpProcurementGoods = erpProcurementGoodsMapper.selectList
                (new LambdaQueryWrapper<TErpProcurementGoods>().in(TErpProcurementGoods::getProcurementId, ids));
        List<TSysCommission> tSysCommissions = new ArrayList<>();
        if (!tErpProcurementGoods.isEmpty()) {
            List<String> collect = tErpProcurementGoods.stream().map(TErpProcurementGoods::getId).collect(Collectors.toList());
            tSysCommissions = sysCommissionMapper.selectList(new LambdaQueryWrapper<TSysCommission>().in(TSysCommission::getTErpProcurementGoodsId, collect));
        }
////        List<TSysCommission> tSysCommissions = new ArrayList<>();
//        if (!tErpProcurementGoods.isEmpty()) {
////            List<String> collect = tErpProcurementGoods.stream().map(TErpProcurementGoods::getId).collect(Collectors.toList());
////            tSysCommissions = sysCommissionMapper.selectList(new LambdaQueryWrapper<TSysCommission>().in(TSysCommission::getTErpProcurementGoodsId, collect));
//        }
        for (TErpProcurementVo tErpProcurementVo : list) {
            List<TErpProcurementGoods> collect = tErpProcurementGoods.stream().filter(e -> e.getProcurementId().equals(tErpProcurementVo.getId())).collect(Collectors.toList());
            int size = collect.stream().map(TErpProcurementGoods::getGoodsId).collect(Collectors.toSet()).size();
            tErpProcurementVo.setTypeNum(size);
            BigDecimal reduce = collect.stream().map(TErpProcurementGoods::getTotalPrice).reduce(BigDecimal.ZERO, BigDecimal::add);
            tErpProcurementVo.setTotalPrice(reduce);
            if (!collect.isEmpty()) {
                List<String> collect1 = collect.stream().map(TErpProcurementGoods::getId).collect(Collectors.toList());
                List<TSysCommission> collect2 = tSysCommissions.stream().filter(e -> collect1.contains(e.getTErpProcurementGoodsId())).collect(Collectors.toList());
                BigDecimal reduce1 = collect2.stream().filter(e -> e.getType() == 1).map(TSysCommission::getMoney).reduce(BigDecimal.ZERO, BigDecimal::add);
                BigDecimal reduce2 = collect2.stream().filter(e -> e.getType() == 2).map(TSysCommission::getMoney).reduce(BigDecimal.ZERO, BigDecimal::add);
                tErpProcurementVo.setPlatformMoney(reduce1);
                tErpProcurementVo.setSupplierMoney(reduce2);
            }
//            if (!collect.isEmpty()) {
////                List<String> collect1 = collect.stream().map(TErpProcurementGoods::getId).collect(Collectors.toList());
////                List<TSysCommission> collect2 = tSysCommissions.stream().filter(e -> collect1.contains(e.getTErpProcurementGoodsId())).collect(Collectors.toList());
////                BigDecimal reduce1 = collect2.stream().filter(e -> e.getType() == 1).map(TSysCommission::getMoney).reduce(BigDecimal.ZERO, BigDecimal::add);
////                BigDecimal reduce2 = collect2.stream().filter(e -> e.getType() == 2).map(TSysCommission::getMoney).reduce(BigDecimal.ZERO, BigDecimal::add);
////                tErpProcurementVo.setPlatformMoney(reduce1);
////                tErpProcurementVo.setSupplierMoney(reduce2);
//            }
        }
@@ -319,7 +329,7 @@
    @Override
    public PageInfo<TErpGoods> pageGoodsPageList(TErpGoodsQuery query, SysUser user) {
        PageInfo<TErpGoods> pageInfo = new PageInfo<>(query.getPageNum(), query.getPageSize());
        PageInfo<TErpGoods> p = erpGoodsMapper.selectPage(pageInfo, new LambdaQueryWrapper<TErpGoods>().eq(TErpGoods::getGoodsSource, 1).isNotNull(TErpGoods::getClinicPurchasePrice).orderByDesc(BaseModel::getCreateTime));
        PageInfo<TErpGoods> p = erpGoodsMapper.selectPage(pageInfo, new LambdaQueryWrapper<TErpGoods>().eq(TErpGoods::getState,1).eq(TErpGoods::getGoodsSource, 1).isNotNull(TErpGoods::getClinicPurchasePrice).orderByDesc(BaseModel::getCreateTime));
        List<TErpGoods> list = p.getRecords();
        List<String> typeIds = list.stream().map(TErpGoods::getTypeId).collect(Collectors.toList());
@@ -384,7 +394,7 @@
            List<AddProcurementDto> value = entry.getValue();
            TErpProcurement tErpProcurement = new TErpProcurement();
            tErpProcurement.setClinicId(clinicSupplierId);
            tErpProcurement.setProcurementCode("C" + DateUtils.dateTimeNow());
            tErpProcurement.setSupplierId(supplierClinicId);
            tErpProcurement.setTermNo(supplier.getTermNo());
@@ -446,7 +456,37 @@
                throw new RuntimeException("金额设置错误,请联系平台管理员");
            }
            tErpProcurement.setSupplierMoney(supplierCommissionPrice);
            this.save(tErpProcurement);
            // 获取上一条最新的数据
            TErpProcurement tErpProcurement1 = this.baseMapper.selectOne(new QueryWrapper<TErpProcurement>().orderByDesc("create_time").last("limit 1"));
            if(tErpProcurement1==null){
                tErpProcurement.setProcurementCode("C" + DateUtils.dateTimeNow()+"00001");
            }else {
                String lastProcurementCode = tErpProcurement1.getProcurementCode();
                String datePart = "C" + DateUtils.dateTimeNow();
                // 直接获取后五位数字并+1,不考虑是否同一天
                try {
                    String numberPart = lastProcurementCode.substring(Math.max(0, lastProcurementCode.length() - 5));
                    int number = Integer.parseInt(numberPart);
                    if (number < 99999) {
                        number++;
                    } else {
                        number = 2; // 达到99999后重置为00002
                    }
                    tErpProcurement.setProcurementCode(datePart + String.format("%05d", number));
                } catch (NumberFormatException e) {
                    // 如果解析失败,默认从00001开始
                    tErpProcurement.setProcurementCode(datePart + "00001");
                }
            }
            try {
                this.save(tErpProcurement);
            }catch (Exception e){
                throw new RuntimeException("网络繁忙,请重试");
            }
            tErpProcurementGoods1.forEach(e -> e.setProcurementId(tErpProcurement.getId()));
            erpProcurementGoodsService.saveBatch(tErpProcurementGoods1);
        }
@@ -459,7 +499,7 @@
        TErpProcurement tErpProcurement = this.getById(id);
        if (tErpProcurement.getWarehousingUserId()!=null && !tErpProcurement.getWarehousingUserId().isEmpty()) {
            SysUser sysUser = sysUserMapper.selectUserById(Long.valueOf(tErpProcurement.getWarehousingUserId()));
            detailProcurementVO.setWarehousingUserName(sysUser.getUserName());
            detailProcurementVO.setWarehousingUserName(sysUser.getNickName());
        }
        detailProcurementVO.setWarehousingTime(tErpProcurement.getWarehousingTime());
@@ -469,7 +509,7 @@
        detailProcurementVO.setSendTime(tErpProcurement.getSendTime());
        TCrmSupplier supplier = crmSupplierMapper.selectById(tErpProcurement.getSupplierId());
        detailProcurementVO.setSupplierName(supplier.getSupplierName());
        detailProcurementVO.setUserName(sysUserMapper.selectUserById(Long.valueOf(tErpProcurement.getCreateId())).getUserName());
        detailProcurementVO.setUserName(sysUserMapper.selectUserById(Long.valueOf(tErpProcurement.getCreateId())).getNickName());
        TCrmClinic tCrmClinic = crmClinicMapper.selectById(tErpProcurement.getClinicId());
        detailProcurementVO.setClinicName(tCrmClinic.getClinicName());
        detailProcurementVO.setClinicAddress(tCrmClinic.getClinicAddress());
@@ -566,8 +606,34 @@
                        tErpSupplierOutbound.setOutboundType(7);
                        //TODO 确定这个是采购价还是销售价格
                        tErpSupplierOutbound.setTotalMoney(tErpProcurementGoods.getPurchasePrice().multiply(new BigDecimal(purchaseCount)));
                        tErpSupplierOutbound.setOutboundNumber("G" + DateUtils.dateTimeNow());
                        erpSupplierOutboundMapper.insert(tErpSupplierOutbound);
                        TErpSupplierOutbound last = erpSupplierOutboundMapper.selectOne(new LambdaQueryWrapper<TErpSupplierOutbound>().orderByDesc(BaseModel::getCreateTime).last("limit 1"));
                        if(last==null){
                            tErpSupplierOutbound.setOutboundNumber("G" + DateUtils.dateTimeNow()+"00001");
                        }else {
                            String lastProcurementCode = last.getOutboundNumber();
                            String datePart = "G" + DateUtils.dateTimeNow();
                            // 直接获取后五位数字并+1,不考虑是否同一天
                            try {
                                String numberPart = lastProcurementCode.substring(Math.max(0, lastProcurementCode.length() - 5));
                                int number = Integer.parseInt(numberPart);
                                if (number < 99999) {
                                    number++;
                                } else {
                                    number = 2; // 达到99999后重置为00002
                                }
                                tErpSupplierOutbound.setOutboundNumber(datePart + String.format("%05d", number));
                            } catch (NumberFormatException e) {
                                // 如果解析失败,默认从00001开始
                                tErpSupplierOutbound.setOutboundNumber(datePart + "00001");
                            }
                        }
                        try {
                            erpSupplierOutboundMapper.insert(tErpSupplierOutbound);
                        }catch (Exception e){
                            throw new RuntimeException("网络繁忙,请重试");
                        }
                        TErpSupplierOutboundGoods tErpSupplierOutboundGoods = new TErpSupplierOutboundGoods();
                        tErpSupplierOutboundGoods.setOutboundId(tErpSupplierOutbound.getId());
@@ -589,8 +655,35 @@
                        tErpSupplierOutbound.setOutboundType(7);
                        //TODO 确定这个是采购价还是销售价格
                        tErpSupplierOutbound.setTotalMoney(tErpProcurementGoods.getPurchasePrice().multiply(new BigDecimal(purchaseCount)));
                        tErpSupplierOutbound.setOutboundNumber("G" + DateUtils.dateTimeNow());
                        erpSupplierOutboundMapper.insert(tErpSupplierOutbound);
                        TErpSupplierOutbound last = erpSupplierOutboundMapper.selectOne(new LambdaQueryWrapper<TErpSupplierOutbound>().orderByDesc(BaseModel::getCreateTime).last("limit 1"));
                        if(last==null){
                            tErpSupplierOutbound.setOutboundNumber("G" + DateUtils.dateTimeNow()+"00001");
                        }else {
                            String lastProcurementCode = last.getOutboundNumber();
                            String datePart = "G" + DateUtils.dateTimeNow();
                            // 直接获取后五位数字并+1,不考虑是否同一天
                            try {
                                String numberPart = lastProcurementCode.substring(Math.max(0, lastProcurementCode.length() - 5));
                                int number = Integer.parseInt(numberPart);
                                if (number < 99999) {
                                    number++;
                                } else {
                                    number = 2; // 达到99999后重置为00002
                                }
                                tErpSupplierOutbound.setOutboundNumber(datePart + String.format("%05d", number));
                            } catch (NumberFormatException e) {
                                // 如果解析失败,默认从00001开始
                                tErpSupplierOutbound.setOutboundNumber(datePart + "00001");
                            }
                        }
                        try {
                            erpSupplierOutboundMapper.insert(tErpSupplierOutbound);
                        }catch (Exception e){
                            throw new RuntimeException("网络繁忙,请重试");
                        }
                        TErpSupplierOutboundGoods tErpSupplierOutboundGoods = new TErpSupplierOutboundGoods();
                        tErpSupplierOutboundGoods.setOutboundId(tErpSupplierOutbound.getId());
@@ -699,7 +792,7 @@
                procurementReportClinicExport.setProcurementGoodsNum(sum);
            }
            if(Objects.nonNull(procurementReportClinicExport.getPayTime())){
                String payTimeStr = new SimpleDateFormat("yyyy-MM-dd").format(procurementReportClinicExport.getPayTime());
                String payTimeStr = new SimpleDateFormat("yyyy-MM-dd").format(DateUtils.localDateTimeToDate(procurementReportClinicExport.getPayTime()));
                procurementReportClinicExport.setPayTimeStr(payTimeStr);
            }
        }
@@ -714,7 +807,7 @@
        }
        for (ProcurementReportSupplierExport procurementReportSupplierExport : list) {
            if(Objects.nonNull(procurementReportSupplierExport.getPayTime())){
                String payTimeStr = new SimpleDateFormat("yyyy-MM-dd").format(procurementReportSupplierExport.getPayTime());
                String payTimeStr = new SimpleDateFormat("yyyy-MM-dd").format(DateUtils.localDateTimeToDate(procurementReportSupplierExport.getPayTime()));
                procurementReportSupplierExport.setPayTimeStr(payTimeStr);
            }
        }
@@ -729,7 +822,7 @@
        }
        for (ProcurementReportPlatformExport procurementReportPlatformExport : list) {
            if(Objects.nonNull(procurementReportPlatformExport.getPayTime())){
                String payTimeStr = new SimpleDateFormat("yyyy-MM-dd").format(procurementReportPlatformExport.getPayTime());
                String payTimeStr = new SimpleDateFormat("yyyy-MM-dd").format(DateUtils.localDateTimeToDate(procurementReportPlatformExport.getPayTime()));
                procurementReportPlatformExport.setPayTimeStr(payTimeStr);
            }
        }
@@ -752,11 +845,267 @@
        }
        for (PlatformLedgerReportExport platformLedgerReportExport : list) {
            if(Objects.nonNull(platformLedgerReportExport.getPayTime())){
                String payTimeStr = new SimpleDateFormat("yyyy-MM-dd").format(platformLedgerReportExport.getPayTime());
                String payTimeStr = new SimpleDateFormat("yyyy-MM-dd").format(DateUtils.localDateTimeToDate(platformLedgerReportExport.getPayTime()));
                platformLedgerReportExport.setPayTimeStr(payTimeStr);
            }
        }
        return list;
    }
    @Override
    public PageInfo<BranchPerformanceReportVO> branchPerformanceReport(PerformanceReportQuery query) {
        List<String> clinicIds = new ArrayList<>();
        if(StringUtils.isNotBlank(query.getBranchName())){
            // 查询分公司名称
            List<TCrmBranch> crmBranches = crmBranchMapper.selectList(Wrappers.lambdaQuery(TCrmBranch.class)
                    .like(TCrmBranch::getBranchName, query.getBranchName()));
            if(CollectionUtils.isEmpty(crmBranches)){
                return new PageInfo<>();
            }
            List<String> branchIds = crmBranches.stream().map(TCrmBranch::getId).collect(Collectors.toList());
            List<TCrmClinic> crmClinics = crmClinicMapper.selectList(Wrappers.lambdaQuery(TCrmClinic.class)
                    .in(TCrmClinic::getBranchId, branchIds));
            if(CollectionUtils.isEmpty(crmClinics)){
                return new PageInfo<>();
            }
            List<String> ids = crmClinics.stream().map(TCrmClinic::getId).collect(Collectors.toList());
            clinicIds.addAll(ids);
        }
        query.setClinicIds(clinicIds);
        PageInfo<BranchPerformanceReportVO> pageInfo = new PageInfo<>(query.getPageNum(), query.getPageSize());
        List<BranchPerformanceReportVO> list = this.baseMapper.branchPerformanceReport(query, pageInfo);
        if(CollectionUtils.isEmpty(list)){
            return pageInfo;
        }
        List<String> procurementIds = list.stream().map(BranchPerformanceReportVO::getId).collect(Collectors.toList());
        // 查询可获得积分
        List<TCrmChangePoints> crmChangePoints = crmChangePointsMapper.selectList(Wrappers.lambdaQuery(TCrmChangePoints.class)
                .in(TCrmChangePoints::getProcurementId, procurementIds)
                .eq(TCrmChangePoints::getUserType, 1)
                .eq(TCrmChangePoints::getChangeType, 1)
                .eq(TCrmChangePoints::getChangeReason, 1));
        // 查询业绩
        List<TErpProcurementCommission> erpProcurementCommissions = erpProcurementCommissionMapper.selectList(Wrappers.lambdaQuery(TErpProcurementCommission.class)
                .in(TErpProcurementCommission::getProcurementId, procurementIds)
                .eq(TErpProcurementCommission::getCommissionType, 1));
        for (BranchPerformanceReportVO branchPerformanceReportVO : list) {
            // 查询可获得积分
            List<TCrmChangePoints> changePoints = crmChangePoints.stream().filter(tCrmChangePoints -> branchPerformanceReportVO.getId().equals(tCrmChangePoints.getProcurementId())).collect(Collectors.toList());
            if(!CollectionUtils.isEmpty(changePoints)){
                branchPerformanceReportVO.setAvailablePoints(changePoints.stream().mapToInt(TCrmChangePoints::getChangeValue).sum());
            }
            // 查询业绩
            List<TErpProcurementCommission> commissions = erpProcurementCommissions.stream().filter(tErpProcurementCommission -> branchPerformanceReportVO.getId().equals(tErpProcurementCommission.getProcurementId())).collect(Collectors.toList());
            if(!CollectionUtils.isEmpty(commissions)){
                branchPerformanceReportVO.setPerformance(commissions.stream().map(TErpProcurementCommission::getMoney).reduce(BigDecimal.ZERO, BigDecimal::add));
            }
        }
        pageInfo.setRecords(list);
        return pageInfo;
    }
    @Override
    public PageInfo<SalespersonPerformanceReportVO> salespersonPerformanceReport(PerformanceReportQuery query) {
        List<String> clinicIds = new ArrayList<>();
        if(StringUtils.isNotBlank(query.getBranchName())){
            // 查询分公司名称
            List<TCrmBranch> crmBranches = crmBranchMapper.selectList(Wrappers.lambdaQuery(TCrmBranch.class)
                    .like(TCrmBranch::getBranchName, query.getBranchName()));
            if(CollectionUtils.isEmpty(crmBranches)){
                return new PageInfo<>();
            }
            List<String> branchIds = crmBranches.stream().map(TCrmBranch::getId).collect(Collectors.toList());
            List<TCrmClinic> crmClinics = crmClinicMapper.selectList(Wrappers.lambdaQuery(TCrmClinic.class)
                    .in(TCrmClinic::getBranchId, branchIds));
            if(CollectionUtils.isEmpty(crmClinics)){
                return new PageInfo<>();
            }
            List<String> ids = crmClinics.stream().map(TCrmClinic::getId).collect(Collectors.toList());
            clinicIds.addAll(ids);
        }
        if(StringUtils.isNotBlank(query.getSalespersonName())){
            // 查询业务员名称
            List<TCrmSalesperson> crmSalespeopleList = crmSalespersonMapper.selectList(Wrappers.lambdaQuery(TCrmSalesperson.class)
                    .like(TCrmSalesperson::getSalespersonName, query.getSalespersonName()));
            if(CollectionUtils.isEmpty(crmSalespeopleList)){
                return new PageInfo<>();
            }
            List<String> salespersonIds = crmSalespeopleList.stream().map(TCrmSalesperson::getId).collect(Collectors.toList());
            List<TCrmClinic> crmClinics = crmClinicMapper.selectList(Wrappers.lambdaQuery(TCrmClinic.class)
                    .in(TCrmClinic::getSalespersonId, salespersonIds));
            if(CollectionUtils.isEmpty(crmClinics)){
                return new PageInfo<>();
            }
            List<String> ids = crmClinics.stream().map(TCrmClinic::getId).collect(Collectors.toList());
            clinicIds.addAll(ids);
        }
        clinicIds = clinicIds.stream().distinct().collect(Collectors.toList());
        query.setClinicIds(clinicIds);
        PageInfo<SalespersonPerformanceReportVO> pageInfo = new PageInfo<>(query.getPageNum(), query.getPageSize());
        List<SalespersonPerformanceReportVO> list = this.baseMapper.salespersonPerformanceReport(query, pageInfo);
        if(CollectionUtils.isEmpty(list)){
            return pageInfo;
        }
        List<String> procurementIds = list.stream().map(SalespersonPerformanceReportVO::getId).collect(Collectors.toList());
        // 查询可获得积分
        List<TCrmChangePoints> crmChangePoints = crmChangePointsMapper.selectList(Wrappers.lambdaQuery(TCrmChangePoints.class)
                .in(TCrmChangePoints::getProcurementId, procurementIds)
                .eq(TCrmChangePoints::getUserType, 2)
                .eq(TCrmChangePoints::getChangeType, 1)
                .eq(TCrmChangePoints::getChangeReason, 1));
        // 查询业绩
        List<TErpProcurementCommission> erpProcurementCommissions = erpProcurementCommissionMapper.selectList(Wrappers.lambdaQuery(TErpProcurementCommission.class)
                .in(TErpProcurementCommission::getProcurementId, procurementIds)
                .eq(TErpProcurementCommission::getCommissionType, 2));
        for (SalespersonPerformanceReportVO salespersonPerformanceReportVO : list) {
            // 查询可获得积分
            List<TCrmChangePoints> changePoints = crmChangePoints.stream().filter(tCrmChangePoints -> salespersonPerformanceReportVO.getId().equals(tCrmChangePoints.getProcurementId())).collect(Collectors.toList());
            if(!CollectionUtils.isEmpty(changePoints)){
                salespersonPerformanceReportVO.setAvailablePoints(changePoints.stream().mapToInt(TCrmChangePoints::getChangeValue).sum());
            }
            // 查询业绩
            List<TErpProcurementCommission> commissions = erpProcurementCommissions.stream().filter(tErpProcurementCommission -> salespersonPerformanceReportVO.getId().equals(tErpProcurementCommission.getProcurementId())).collect(Collectors.toList());
            if(!CollectionUtils.isEmpty(commissions)){
                salespersonPerformanceReportVO.setPerformance(commissions.stream().map(TErpProcurementCommission::getMoney).reduce(BigDecimal.ZERO, BigDecimal::add));
            }
        }
        pageInfo.setRecords(list);
        return pageInfo;
    }
    @Override
    public List<BranchPerformanceReportVO> exportBranchPerformanceReport(PerformanceReportQuery query) {
        List<String> clinicIds = new ArrayList<>();
        if(StringUtils.isNotBlank(query.getBranchName())){
            // 查询分公司名称
            List<TCrmBranch> crmBranches = crmBranchMapper.selectList(Wrappers.lambdaQuery(TCrmBranch.class)
                    .like(TCrmBranch::getBranchName, query.getBranchName()));
            if(CollectionUtils.isEmpty(crmBranches)){
                return new ArrayList<>();
            }
            List<String> branchIds = crmBranches.stream().map(TCrmBranch::getId).collect(Collectors.toList());
            List<TCrmClinic> crmClinics = crmClinicMapper.selectList(Wrappers.lambdaQuery(TCrmClinic.class)
                    .in(TCrmClinic::getBranchId, branchIds));
            if(CollectionUtils.isEmpty(crmClinics)){
                return new ArrayList<>();
            }
            List<String> ids = crmClinics.stream().map(TCrmClinic::getId).collect(Collectors.toList());
            clinicIds.addAll(ids);
        }
        query.setClinicIds(clinicIds);
        List<BranchPerformanceReportVO> list = this.baseMapper.exportBranchPerformanceReport(query);
        if(CollectionUtils.isEmpty(list)){
            return new ArrayList<>();
        }
        List<String> procurementIds = list.stream().map(BranchPerformanceReportVO::getId).collect(Collectors.toList());
        // 查询可获得积分
        List<TCrmChangePoints> crmChangePoints = crmChangePointsMapper.selectList(Wrappers.lambdaQuery(TCrmChangePoints.class)
                .in(TCrmChangePoints::getProcurementId, procurementIds)
                .eq(TCrmChangePoints::getUserType, 1)
                .eq(TCrmChangePoints::getChangeType, 1)
                .eq(TCrmChangePoints::getChangeReason, 1));
        // 查询业绩
        List<TErpProcurementCommission> erpProcurementCommissions = erpProcurementCommissionMapper.selectList(Wrappers.lambdaQuery(TErpProcurementCommission.class)
                .in(TErpProcurementCommission::getProcurementId, procurementIds)
                .eq(TErpProcurementCommission::getCommissionType, 1));
        for (BranchPerformanceReportVO branchPerformanceReportVO : list) {
            // 查询可获得积分
            List<TCrmChangePoints> changePoints = crmChangePoints.stream().filter(tCrmChangePoints -> branchPerformanceReportVO.getId().equals(tCrmChangePoints.getProcurementId())).collect(Collectors.toList());
            if(!CollectionUtils.isEmpty(changePoints)){
                branchPerformanceReportVO.setAvailablePoints(changePoints.stream().mapToInt(TCrmChangePoints::getChangeValue).sum());
            }
            // 查询业绩
            List<TErpProcurementCommission> commissions = erpProcurementCommissions.stream().filter(tErpProcurementCommission -> branchPerformanceReportVO.getId().equals(tErpProcurementCommission.getProcurementId())).collect(Collectors.toList());
            if(!CollectionUtils.isEmpty(commissions)){
                branchPerformanceReportVO.setPerformance(commissions.stream().map(TErpProcurementCommission::getMoney).reduce(BigDecimal.ZERO, BigDecimal::add));
            }
            if(Objects.nonNull(branchPerformanceReportVO.getPayTime())){
                String payTimeStr = new SimpleDateFormat("yyyy-MM-dd").format(DateUtils.localDateTimeToDate(branchPerformanceReportVO.getPayTime()));
                branchPerformanceReportVO.setPayTimeStr(payTimeStr);
            }
        }
        return list;
    }
    @Override
    public List<SalespersonPerformanceReportVO> exportSalespersonPerformanceReport(PerformanceReportQuery query) {
        List<String> clinicIds = new ArrayList<>();
        if(StringUtils.isNotBlank(query.getBranchName())){
            // 查询分公司名称
            List<TCrmBranch> crmBranches = crmBranchMapper.selectList(Wrappers.lambdaQuery(TCrmBranch.class)
                    .like(TCrmBranch::getBranchName, query.getBranchName()));
            if(CollectionUtils.isEmpty(crmBranches)){
                return new ArrayList<>();
            }
            List<String> branchIds = crmBranches.stream().map(TCrmBranch::getId).collect(Collectors.toList());
            List<TCrmClinic> crmClinics = crmClinicMapper.selectList(Wrappers.lambdaQuery(TCrmClinic.class)
                    .in(TCrmClinic::getBranchId, branchIds));
            if(CollectionUtils.isEmpty(crmClinics)){
                return new ArrayList<>();
            }
            List<String> ids = crmClinics.stream().map(TCrmClinic::getId).collect(Collectors.toList());
            clinicIds.addAll(ids);
        }
        if(StringUtils.isNotBlank(query.getSalespersonName())){
            // 查询业务员名称
            List<TCrmSalesperson> crmSalespeopleList = crmSalespersonMapper.selectList(Wrappers.lambdaQuery(TCrmSalesperson.class)
                    .like(TCrmSalesperson::getSalespersonName, query.getSalespersonName()));
            if(CollectionUtils.isEmpty(crmSalespeopleList)){
                return new ArrayList<>();
            }
            List<String> salespersonIds = crmSalespeopleList.stream().map(TCrmSalesperson::getId).collect(Collectors.toList());
            List<TCrmClinic> crmClinics = crmClinicMapper.selectList(Wrappers.lambdaQuery(TCrmClinic.class)
                    .in(TCrmClinic::getSalespersonId, salespersonIds));
            if(CollectionUtils.isEmpty(crmClinics)){
                return new ArrayList<>();
            }
            List<String> ids = crmClinics.stream().map(TCrmClinic::getId).collect(Collectors.toList());
            clinicIds.addAll(ids);
        }
        clinicIds = clinicIds.stream().distinct().collect(Collectors.toList());
        query.setClinicIds(clinicIds);
        List<SalespersonPerformanceReportVO> list = this.baseMapper.exportSalespersonPerformanceReport(query);
        if(CollectionUtils.isEmpty(list)){
            return new ArrayList<>();
        }
        List<String> procurementIds = list.stream().map(SalespersonPerformanceReportVO::getId).collect(Collectors.toList());
        // 查询可获得积分
        List<TCrmChangePoints> crmChangePoints = crmChangePointsMapper.selectList(Wrappers.lambdaQuery(TCrmChangePoints.class)
                .in(TCrmChangePoints::getProcurementId, procurementIds)
                .eq(TCrmChangePoints::getUserType, 2)
                .eq(TCrmChangePoints::getChangeType, 1)
                .eq(TCrmChangePoints::getChangeReason, 1));
        // 查询业绩
        List<TErpProcurementCommission> erpProcurementCommissions = erpProcurementCommissionMapper.selectList(Wrappers.lambdaQuery(TErpProcurementCommission.class)
                .in(TErpProcurementCommission::getProcurementId, procurementIds)
                .eq(TErpProcurementCommission::getCommissionType, 2));
        for (SalespersonPerformanceReportVO salespersonPerformanceReportVO : list) {
            // 查询可获得积分
            List<TCrmChangePoints> changePoints = crmChangePoints.stream().filter(tCrmChangePoints -> salespersonPerformanceReportVO.getId().equals(tCrmChangePoints.getProcurementId())).collect(Collectors.toList());
            if(!CollectionUtils.isEmpty(changePoints)){
                salespersonPerformanceReportVO.setAvailablePoints(changePoints.stream().mapToInt(TCrmChangePoints::getChangeValue).sum());
            }
            // 查询业绩
            List<TErpProcurementCommission> commissions = erpProcurementCommissions.stream().filter(tErpProcurementCommission -> salespersonPerformanceReportVO.getId().equals(tErpProcurementCommission.getProcurementId())).collect(Collectors.toList());
            if(!CollectionUtils.isEmpty(commissions)){
                salespersonPerformanceReportVO.setPerformance(commissions.stream().map(TErpProcurementCommission::getMoney).reduce(BigDecimal.ZERO, BigDecimal::add));
            }
            if(Objects.nonNull(salespersonPerformanceReportVO.getPayTime())){
                String payTimeStr = new SimpleDateFormat("yyyy-MM-dd").format(DateUtils.localDateTimeToDate(salespersonPerformanceReportVO.getPayTime()));
                salespersonPerformanceReportVO.setPayTimeStr(payTimeStr);
            }
        }
        return list;
    }
}