| | |
| | | import com.doumee.service.business.YwCustomerRechargeBizService; |
| | | import com.doumee.service.business.YwElectricalBizService; |
| | | import com.github.yulichang.wrapper.MPJLambdaWrapper; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | |
| | | import java.util.stream.Collectors; |
| | | |
| | | @Service |
| | | @Slf4j |
| | | public class YwCustomerRechargeBizServiceImpl implements YwCustomerRechargeBizService { |
| | | |
| | | private static final String ONLINE_TEXT = "在线"; |
| | | |
| | | @Autowired |
| | | private YwCustomerMapper ywCustomerMapper; |
| | | @Autowired |
| | | private MemberMapper memberMapper; |
| | | @Autowired |
| | | private YwCustomerGsMapper ywCustomerGsMapper; |
| | | @Autowired |
| | |
| | | |
| | | @Override |
| | | public PageData<YwCustomerRechargeMerchantVO> findMerchantPage(PageWrap<YwCustomerRechargeQueryDTO> pageWrap) { |
| | | if (pageWrap == null) { |
| | | pageWrap = new PageWrap<>(); |
| | | } |
| | | YwCustomerRechargeQueryDTO query = pageWrap.getModel() != null ? pageWrap.getModel() : new YwCustomerRechargeQueryDTO(); |
| | | boolean hasDeviceFilter = query.getElectricalStatusFilter() != null || query.getConditionerStatusFilter() != null; |
| | | |
| | | if (hasDeviceFilter) { |
| | | List<YwCustomer> all = ywCustomerMapper.selectList(new QueryWrapper<YwCustomer>().lambda() |
| | | .eq(YwCustomer::getIsdeleted, Constants.ZERO) |
| | | .like(StringUtils.isNotBlank(query.getNameKeyword()), YwCustomer::getName, query.getNameKeyword()) |
| | | .orderByDesc(YwCustomer::getCreateDate)); |
| | | List<YwCustomer> all = ywCustomerMapper.selectJoinList(YwCustomer.class, buildMerchantCustomerWrapper(query)); |
| | | List<YwCustomerRechargeMerchantVO> enriched = enrichMerchantList(all); |
| | | List<YwCustomerRechargeMerchantVO> filtered = enriched.stream() |
| | | .filter(vo -> matchDeviceFilter(vo, query)) |
| | |
| | | } |
| | | |
| | | IPage<YwCustomer> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity()); |
| | | IPage<YwCustomer> result = ywCustomerMapper.selectPage(page, new QueryWrapper<YwCustomer>().lambda() |
| | | .eq(YwCustomer::getIsdeleted, Constants.ZERO) |
| | | .like(StringUtils.isNotBlank(query.getNameKeyword()), YwCustomer::getName, query.getNameKeyword()) |
| | | .orderByDesc(YwCustomer::getCreateDate)); |
| | | IPage<YwCustomer> result = ywCustomerMapper.selectJoinPage(page, YwCustomer.class, buildMerchantCustomerWrapper(query)); |
| | | List<YwCustomerRechargeMerchantVO> list = enrichMerchantList(result.getRecords()); |
| | | PageData<YwCustomerRechargeMerchantVO> data = new PageData<>(); |
| | | data.setRecords(list); |
| | |
| | | data.setPage(result.getCurrent()); |
| | | data.setCapacity(result.getSize()); |
| | | return data; |
| | | } |
| | | |
| | | private MPJLambdaWrapper<YwCustomer> buildMerchantCustomerWrapper(YwCustomerRechargeQueryDTO query) { |
| | | return new MPJLambdaWrapper<YwCustomer>() |
| | | .selectAll(YwCustomer.class) |
| | | .selectAs(Member::getName, YwCustomer::getMemberName) |
| | | .selectAs(Member::getPhone, YwCustomer::getMemberPhone) |
| | | .leftJoin(Member.class, Member::getId, YwCustomer::getMemberId) |
| | | .eq(YwCustomer::getIsdeleted, Constants.ZERO) |
| | | .like(StringUtils.isNotBlank(query.getNameKeyword()), YwCustomer::getName, query.getNameKeyword()) |
| | | .orderByDesc(YwCustomer::getCreateDate); |
| | | } |
| | | |
| | | private PageData<YwCustomerRechargeMerchantVO> manualPage(List<YwCustomerRechargeMerchantVO> list, long page, long capacity) { |
| | |
| | | } |
| | | List<Integer> customerIds = customers.stream().map(YwCustomer::getId).collect(Collectors.toList()); |
| | | |
| | | Map<Integer, YwCustomerGs> gsMap = ywCustomerGsMapper.selectList(new QueryWrapper<YwCustomerGs>().lambda() |
| | | .eq(YwCustomerGs::getIsdeleted, Constants.ZERO) |
| | | .in(YwCustomerGs::getCustomerId, customerIds)) |
| | | .stream().collect(Collectors.toMap(YwCustomerGs::getCustomerId, g -> g, (a, b) -> a)); |
| | | Map<Integer, YwCustomerGs> gsMap = loadGsMap(customerIds); |
| | | |
| | | List<YwCustomerElectrical> relE = ywCustomerElectricalMapper.selectList(new QueryWrapper<YwCustomerElectrical>().lambda() |
| | | .eq(YwCustomerElectrical::getIsdeleted, Constants.ZERO) |
| | | .in(YwCustomerElectrical::getCustomerId, customerIds)); |
| | | List<YwCustomerElectrical> relE = loadCustomerElectricalRels(customerIds); |
| | | Map<Integer, List<Integer>> customerElectricalIds = relE.stream() |
| | | .collect(Collectors.groupingBy(YwCustomerElectrical::getCustomerId, |
| | | Collectors.mapping(YwCustomerElectrical::getElectricalId, Collectors.toList()))); |
| | | |
| | | List<YwCustomerConditioner> relC = ywCustomerConditionerMapper.selectList(new QueryWrapper<YwCustomerConditioner>().lambda() |
| | | .eq(YwCustomerConditioner::getIsdeleted, Constants.ZERO) |
| | | .in(YwCustomerConditioner::getCustomerId, customerIds)); |
| | | List<YwCustomerConditioner> relC = loadCustomerConditionerRels(customerIds); |
| | | Map<Integer, List<Integer>> customerConditionerIds = relC.stream() |
| | | .collect(Collectors.groupingBy(YwCustomerConditioner::getCustomerId, |
| | | Collectors.mapping(YwCustomerConditioner::getConditionerId, Collectors.toList()))); |
| | | |
| | | Set<Integer> allElectricalIds = relE.stream().map(YwCustomerElectrical::getElectricalId).collect(Collectors.toSet()); |
| | | Map<Integer, YwElectrical> electricalMap = allElectricalIds.isEmpty() ? Collections.emptyMap() |
| | | : ywElectricalMapper.selectBatchIds(allElectricalIds).stream() |
| | | .filter(e -> !Objects.equals(e.getIsdeleted(), Constants.ONE)) |
| | | .collect(Collectors.toMap(YwElectrical::getId, e -> e, (a, b) -> a)); |
| | | Map<Integer, YwElectrical> electricalMap = loadElectricalMap(allElectricalIds); |
| | | |
| | | Set<Integer> allConditionerIds = relC.stream().map(YwCustomerConditioner::getConditionerId).collect(Collectors.toSet()); |
| | | Map<Integer, YwConditioner> conditionerMap = allConditionerIds.isEmpty() ? Collections.emptyMap() |
| | | : ywConditionerMapper.selectBatchIds(allConditionerIds).stream() |
| | | .filter(c -> !Objects.equals(c.getIsdeleted(), Constants.ONE)) |
| | | .collect(Collectors.toMap(YwConditioner::getId, c -> c, (a, b) -> a)); |
| | | |
| | | Set<Integer> memberIds = customers.stream() |
| | | .map(YwCustomer::getMemberId) |
| | | .filter(Objects::nonNull) |
| | | .collect(Collectors.toSet()); |
| | | Map<Integer, Member> memberMap = memberIds.isEmpty() ? Collections.emptyMap() |
| | | : memberMapper.selectBatchIds(memberIds).stream() |
| | | .filter(m -> !Objects.equals(m.getIsdeleted(), Constants.ONE)) |
| | | .collect(Collectors.toMap(Member::getId, m -> m, (a, b) -> a)); |
| | | Map<Integer, YwConditioner> conditionerMap = loadConditionerMap(allConditionerIds); |
| | | |
| | | List<YwCustomerRechargeMerchantVO> list = new ArrayList<>(); |
| | | for (YwCustomer c : customers) { |
| | |
| | | vo.setName(c.getName()); |
| | | vo.setPhone(c.getPhone()); |
| | | vo.setCreateDate(c.getCreateDate()); |
| | | Member member = c.getMemberId() != null ? memberMap.get(c.getMemberId()) : null; |
| | | if (member != null) { |
| | | vo.setMemberName(member.getName()); |
| | | vo.setMemberPhone(member.getPhone()); |
| | | } |
| | | vo.setMemberName(c.getMemberName()); |
| | | vo.setMemberPhone(StringUtils.defaultIfBlank(c.getMemberPhone(), c.getPhone())); |
| | | |
| | | List<Integer> eIds = customerElectricalIds.getOrDefault(c.getId(), Collections.emptyList()); |
| | | vo.setElectricalCount(eIds.size()); |
| | |
| | | list.add(vo); |
| | | } |
| | | return list; |
| | | } |
| | | |
| | | private Map<Integer, YwCustomerGs> loadGsMap(List<Integer> customerIds) { |
| | | if (CollectionUtils.isEmpty(customerIds)) { |
| | | return Collections.emptyMap(); |
| | | } |
| | | try { |
| | | return ywCustomerGsMapper.selectList(new QueryWrapper<YwCustomerGs>().lambda() |
| | | .select(YwCustomerGs::getId, YwCustomerGs::getCustomerId, |
| | | YwCustomerGs::getLeftMoney, YwCustomerGs::getSyncDate, YwCustomerGs::getPlatformGsId) |
| | | .eq(YwCustomerGs::getIsdeleted, Constants.ZERO) |
| | | .in(YwCustomerGs::getCustomerId, customerIds)) |
| | | .stream().collect(Collectors.toMap(YwCustomerGs::getCustomerId, g -> g, (a, b) -> a)); |
| | | } catch (Exception e) { |
| | | log.warn("load yw_customer_gs failed, skip gs stats: {}", e.getMessage()); |
| | | return Collections.emptyMap(); |
| | | } |
| | | } |
| | | |
| | | private List<YwCustomerElectrical> loadCustomerElectricalRels(List<Integer> customerIds) { |
| | | if (CollectionUtils.isEmpty(customerIds)) { |
| | | return Collections.emptyList(); |
| | | } |
| | | try { |
| | | return ywCustomerElectricalMapper.selectList(new QueryWrapper<YwCustomerElectrical>().lambda() |
| | | .select(YwCustomerElectrical::getCustomerId, YwCustomerElectrical::getElectricalId) |
| | | .eq(YwCustomerElectrical::getIsdeleted, Constants.ZERO) |
| | | .in(YwCustomerElectrical::getCustomerId, customerIds)); |
| | | } catch (Exception e) { |
| | | log.warn("load yw_customer_electrical failed, skip electrical stats: {}", e.getMessage()); |
| | | return Collections.emptyList(); |
| | | } |
| | | } |
| | | |
| | | private List<YwCustomerConditioner> loadCustomerConditionerRels(List<Integer> customerIds) { |
| | | if (CollectionUtils.isEmpty(customerIds)) { |
| | | return Collections.emptyList(); |
| | | } |
| | | try { |
| | | return ywCustomerConditionerMapper.selectList(new QueryWrapper<YwCustomerConditioner>().lambda() |
| | | .select(YwCustomerConditioner::getCustomerId, YwCustomerConditioner::getConditionerId) |
| | | .eq(YwCustomerConditioner::getIsdeleted, Constants.ZERO) |
| | | .in(YwCustomerConditioner::getCustomerId, customerIds)); |
| | | } catch (Exception e) { |
| | | log.warn("load yw_customer_conditioner failed, skip conditioner stats: {}", e.getMessage()); |
| | | return Collections.emptyList(); |
| | | } |
| | | } |
| | | |
| | | private Map<Integer, YwElectrical> loadElectricalMap(Set<Integer> electricalIds) { |
| | | if (CollectionUtils.isEmpty(electricalIds)) { |
| | | return Collections.emptyMap(); |
| | | } |
| | | try { |
| | | return ywElectricalMapper.selectList(new QueryWrapper<YwElectrical>().lambda() |
| | | .select(YwElectrical::getId, YwElectrical::getName, YwElectrical::getAddress, |
| | | YwElectrical::getBalance, YwElectrical::getOnline, YwElectrical::getIsdeleted) |
| | | .in(YwElectrical::getId, electricalIds) |
| | | .eq(YwElectrical::getIsdeleted, Constants.ZERO)) |
| | | .stream().collect(Collectors.toMap(YwElectrical::getId, e -> e, (a, b) -> a)); |
| | | } catch (Exception e) { |
| | | log.warn("load electrical for merchant page failed: {}", e.getMessage()); |
| | | return Collections.emptyMap(); |
| | | } |
| | | } |
| | | |
| | | private Map<Integer, YwConditioner> loadConditionerMap(Set<Integer> conditionerIds) { |
| | | if (CollectionUtils.isEmpty(conditionerIds)) { |
| | | return Collections.emptyMap(); |
| | | } |
| | | try { |
| | | return ywConditionerMapper.selectList(new QueryWrapper<YwConditioner>().lambda() |
| | | .select(YwConditioner::getId, YwConditioner::getOnline, YwConditioner::getIsdeleted) |
| | | .in(YwConditioner::getId, conditionerIds) |
| | | .eq(YwConditioner::getIsdeleted, Constants.ZERO)) |
| | | .stream().collect(Collectors.toMap(YwConditioner::getId, c -> c, (a, b) -> a)); |
| | | } catch (Exception e) { |
| | | log.warn("load conditioner for merchant page failed: {}", e.getMessage()); |
| | | return Collections.emptyMap(); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | |
| | | |
| | | @Override |
| | | public YwCustomerGs getCustomerGsConfig(Integer customerId) { |
| | | try { |
| | | return ywCustomerGsMapper.selectOne(new QueryWrapper<YwCustomerGs>().lambda() |
| | | .eq(YwCustomerGs::getCustomerId, customerId) |
| | | .eq(YwCustomerGs::getIsdeleted, Constants.ZERO) |
| | | .last("limit 1")); |
| | | } catch (Exception e) { |
| | | log.warn("load yw_customer_gs failed, retry without stop_money: {}", e.getMessage()); |
| | | return ywCustomerGsMapper.selectOne(new QueryWrapper<YwCustomerGs>().lambda() |
| | | .select(YwCustomerGs::getId, YwCustomerGs::getCustomerId, YwCustomerGs::getPlatformGsId, |
| | | YwCustomerGs::getIsPwr, YwCustomerGs::getIsRestStop, YwCustomerGs::getGsBz, |
| | | YwCustomerGs::getLeftMoney, YwCustomerGs::getSyncDate, |
| | | YwCustomerGs::getCreator, YwCustomerGs::getCreateDate, |
| | | YwCustomerGs::getEditor, YwCustomerGs::getEditDate, YwCustomerGs::getIsdeleted) |
| | | .eq(YwCustomerGs::getCustomerId, customerId) |
| | | .eq(YwCustomerGs::getIsdeleted, Constants.ZERO) |
| | | .last("limit 1")); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void saveCustomerGsConfig(YwCustomerGsConfigDTO dto, LoginUserInfo user) { |
| | | YwCustomer customer = requireCustomer(dto.getCustomerId()); |
| | | if (CollectionUtils.isEmpty(dto.getConditioners())) { |
| | | throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "请至少关联一台空调内机"); |
| | | } |
| | | validateGsConfigRequired(dto); |
| | | conditionerBizService.ensureLogin(); |
| | | |
| | | List<Integer> conditionerIds = dto.getConditioners().stream() |
| | |
| | | } |
| | | gs.setEditor(user.getId()); |
| | | gs.setEditDate(new Date()); |
| | | gs.setIsPwr(dto.getIsPwr() != null ? dto.getIsPwr() : Constants.ONE); |
| | | gs.setIsRestStop(dto.getIsRestStop() != null ? dto.getIsRestStop() : Constants.ZERO); |
| | | gs.setIsPwr(dto.getIsPwr()); |
| | | gs.setIsRestStop(dto.getIsRestStop()); |
| | | gs.setGsBz(StringUtils.defaultString(dto.getGsBz())); |
| | | gs.setStopMoney(dto.getStopMoney() != null ? dto.getStopMoney() : BigDecimal.ZERO); |
| | | gs.setStopMoney(dto.getStopMoney()); |
| | | |
| | | if (StringUtils.isBlank(customer.getName())) { |
| | | throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "客户名称不能为空"); |
| | |
| | | companyReq.setIs_rest_stop(gs.getIsRestStop()); |
| | | companyReq.setGs_bz(gs.getGsBz()); |
| | | companyReq.setStop_money(gs.getStopMoney()); |
| | | companyReq.setLi_dev(liDev); |
| | | companyReq.setD_dev(dDev); |
| | | |
| | | if (gs.getPlatformGsId() == null) { |
| | | ConditionerBaseResponse<Object> addResp = ConditionerUtil.addGs(companyReq); |
| | |
| | | |
| | | refreshGsLeftMoney(gs); |
| | | if (gs.getId() == null) { |
| | | try { |
| | | ywCustomerGsMapper.insert(gs); |
| | | } catch (Exception e) { |
| | | if (gs.getStopMoney() != null) { |
| | | log.warn("insert yw_customer_gs with stop_money failed, retry without stop_money: {}", e.getMessage()); |
| | | gs.setStopMoney(null); |
| | | ywCustomerGsMapper.insert(gs); |
| | | } else { |
| | | throw e; |
| | | } |
| | | } |
| | | } else { |
| | | try { |
| | | ywCustomerGsMapper.updateById(gs); |
| | | } catch (Exception e) { |
| | | if (gs.getStopMoney() != null) { |
| | | log.warn("update yw_customer_gs with stop_money failed, retry without stop_money: {}", e.getMessage()); |
| | | BigDecimal stopMoney = gs.getStopMoney(); |
| | | gs.setStopMoney(null); |
| | | ywCustomerGsMapper.updateById(gs); |
| | | gs.setStopMoney(stopMoney); |
| | | } else { |
| | | throw e; |
| | | } |
| | | } |
| | | } |
| | | |
| | | saveCustomerConditionerRels(dto, user); |
| | | } |
| | | |
| | | /** |
| | | * 关联内机 upsert:表上有 uk(customer_id, conditioner_id),不能软删后重复 insert。 |
| | | */ |
| | | private void saveCustomerConditionerRels(YwCustomerGsConfigDTO dto, LoginUserInfo user) { |
| | | List<YwCustomerConditioner> existingRels = ywCustomerConditionerMapper.selectList( |
| | | new QueryWrapper<YwCustomerConditioner>().lambda() |
| | | .eq(YwCustomerConditioner::getCustomerId, dto.getCustomerId())); |
| | | Map<Integer, YwCustomerConditioner> relByCondId = existingRels.stream() |
| | | .collect(Collectors.toMap(YwCustomerConditioner::getConditionerId, r -> r, (a, b) -> a)); |
| | | |
| | | Set<Integer> targetIds = dto.getConditioners().stream() |
| | | .map(YwCustomerGsConfigDTO.ConditionerItem::getConditionerId) |
| | | .filter(Objects::nonNull) |
| | | .collect(Collectors.toSet()); |
| | | |
| | | Date now = new Date(); |
| | | for (YwCustomerConditioner rel : existingRels) { |
| | | if (!targetIds.contains(rel.getConditionerId()) |
| | | && Objects.equals(rel.getIsdeleted(), Constants.ZERO)) { |
| | | ywCustomerConditionerMapper.update(null, new UpdateWrapper<YwCustomerConditioner>().lambda() |
| | | .set(YwCustomerConditioner::getIsdeleted, Constants.ONE) |
| | | .set(YwCustomerConditioner::getEditDate, new Date()) |
| | | .set(YwCustomerConditioner::getEditDate, now) |
| | | .set(YwCustomerConditioner::getEditor, user.getId()) |
| | | .eq(YwCustomerConditioner::getCustomerId, dto.getCustomerId()) |
| | | .eq(YwCustomerConditioner::getIsdeleted, Constants.ZERO)); |
| | | .eq(YwCustomerConditioner::getId, rel.getId())); |
| | | } |
| | | } |
| | | |
| | | for (YwCustomerGsConfigDTO.ConditionerItem item : dto.getConditioners()) { |
| | | YwCustomerConditioner rel = new YwCustomerConditioner(); |
| | | rel.setCreator(user.getId()); |
| | | rel.setCreateDate(new Date()); |
| | | rel.setEditor(user.getId()); |
| | | rel.setEditDate(new Date()); |
| | | if (item.getConditionerId() == null) { |
| | | continue; |
| | | } |
| | | int ratio = item.getDevRatio() != null ? item.getDevRatio() : 100; |
| | | YwCustomerConditioner rel = relByCondId.get(item.getConditionerId()); |
| | | if (rel != null) { |
| | | rel.setIsdeleted(Constants.ZERO); |
| | | rel.setCustomerId(dto.getCustomerId()); |
| | | rel.setConditionerId(item.getConditionerId()); |
| | | rel.setDevRatio(item.getDevRatio() != null ? item.getDevRatio() : 100); |
| | | ywCustomerConditionerMapper.insert(rel); |
| | | rel.setDevRatio(ratio); |
| | | rel.setEditor(user.getId()); |
| | | rel.setEditDate(now); |
| | | ywCustomerConditionerMapper.updateById(rel); |
| | | } else { |
| | | YwCustomerConditioner created = new YwCustomerConditioner(); |
| | | created.setCreator(user.getId()); |
| | | created.setCreateDate(now); |
| | | created.setEditor(user.getId()); |
| | | created.setEditDate(now); |
| | | created.setIsdeleted(Constants.ZERO); |
| | | created.setCustomerId(dto.getCustomerId()); |
| | | created.setConditionerId(item.getConditionerId()); |
| | | created.setDevRatio(ratio); |
| | | ywCustomerConditionerMapper.insert(created); |
| | | relByCondId.put(item.getConditionerId(), created); |
| | | } |
| | | } |
| | | } |
| | | |
| | |
| | | } |
| | | } |
| | | |
| | | private void validateGsConfigRequired(YwCustomerGsConfigDTO dto) { |
| | | if (dto.getStopMoney() == null) { |
| | | throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "欠费额度不能为空"); |
| | | } |
| | | if (dto.getStopMoney().compareTo(BigDecimal.ZERO) < 0) { |
| | | throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "欠费额度不能小于0"); |
| | | } |
| | | if (dto.getIsPwr() == null) { |
| | | throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "计费开关不能为空"); |
| | | } |
| | | if (dto.getIsRestStop() == null) { |
| | | throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "是否停机不能为空"); |
| | | } |
| | | if (CollectionUtils.isEmpty(dto.getConditioners())) { |
| | | throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "请至少关联一台空调内机"); |
| | | } |
| | | } |
| | | |
| | | private String apiMsg(ConditionerBaseResponse<?> resp, String def) { |
| | | return resp != null && StringUtils.isNotBlank(resp.getMessage()) ? resp.getMessage() : def; |
| | | } |