BusinessAnalysisService.java 19.1 KB
package cn.com.glsx.busassistant.service;

import cn.com.glsx.busassistant.request.businessAnalysis.BusinessAnalysisRequest;
import cn.com.glsx.busassistant.response.businessAnalysis.BusinessAnalysisFlowAnalysisResponse;
import cn.com.glsx.busassistant.response.businessAnalysis.BusinessAnalysisOverviewResponse;
import cn.com.glsx.busassistant.response.businessAnalysis.BusinessAnalysisPurchaseTendencyResponse;
import cn.com.glsx.busassistant.util.CommonUtil;
import cn.com.glsx.busassistant.util.DateUtils;
import cn.com.glsx.dj.smartcarlife.constant.CommonConstants;
import cn.com.glsx.dj.smartcarlife.dto.LifeCustomerCouponDTO;
import cn.com.glsx.dj.smartcarlife.dto.OrderSummaryDTO;
import cn.com.glsx.dj.smartcarlife.dto.businessassistant.BusinessAnalysisFlowRankDTO;
import cn.com.glsx.dj.smartcarlife.dto.businessassistant.MerchantPurchaseTendencyDiagramDTO;
import cn.com.glsx.dj.smartcarlife.dto.businessassistant.NameValueDTO;
import cn.com.glsx.dj.smartcarlife.dto.businessassistant.PurchaseOrderGoodsBaseDTO;
import cn.com.glsx.dj.smartcarlife.enums.SceneTypeEnum;
import cn.com.glsx.dj.smartcarlife.remote.LifeCustomerCouponRemote;
import cn.com.glsx.dj.smartcarlife.remote.OrderSummaryRemote;
import cn.com.glsx.dj.smartcarlife.remote.PurchaseOrderRemote;
import cn.com.glsx.framework.core.beans.rpc.RpcPagination;
import cn.com.glsx.framework.core.beans.rpc.RpcResponse;
import cn.com.glsx.framework.core.enums.DefaultErrorEnum;
import cn.com.glsx.framework.core.exception.RpcServiceException;
import cn.com.glsx.framework.rop.response.RopResponse;
import com.google.common.collect.Lists;
import com.rop.AbstractRopRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

@Slf4j
@Service
@Transactional(propagation = Propagation.SUPPORTS, rollbackFor = RpcServiceException.class)
public class BusinessAnalysisService {
    private static final Integer SOLAR_FILE_TYPE = 1;
    private static final String REFUSAL_REASON1 = "客户体验了,还是不想升级";
    private static final String REFUSAL_REASON2 = "价格太高";
    private static final String REFUSAL_REASON3 = "客户没来现场,他人代办";
    private static final String REFUSAL_REASON4 = "不是本人的车";
    private static final String REFUSAL_REASON5 = "其他";

    @Autowired
    private OrderSummaryRemote orderSummaryRemote;
    @Autowired
    private LifeCustomerCouponRemote lifeCustomerCouponRemote;
    @Autowired
    private PurchaseOrderRemote purchaseOrderRemote;


    public RopResponse<BusinessAnalysisOverviewResponse> overview(BusinessAnalysisRequest request) {
        try {
            BusinessAnalysisOverviewResponse businessAnalysisOverviewResponse = new BusinessAnalysisOverviewResponse(0, 0);
            //查询所有关联4s店的业务产值数据
            setBusinessAnalysisOverviewTotalMoney(businessAnalysisOverviewResponse, request);
            //查询所有关联园区店的某月采购支出数据总值
            setBusinessAnalysisOverviewPurchaseMoney(businessAnalysisOverviewResponse, request);
            return RopResponse.success(businessAnalysisOverviewResponse);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new RpcServiceException(DefaultErrorEnum.SERVICE_ERROR);
        }
    }

    private void setBusinessAnalysisOverviewTotalMoney(BusinessAnalysisOverviewResponse businessAnalysisOverviewResponse, BusinessAnalysisRequest request) {
        List<Integer> merchantIdsByPartnerMerchantId = CommonUtil.getShopMerchantIds(SceneTypeEnum.SCENE1_1.getValue());
        OrderSummaryDTO orderSummaryDTO = new OrderSummaryDTO();
        orderSummaryDTO.setShopMerchantIdList(merchantIdsByPartnerMerchantId);
        orderSummaryDTO.setStartPayTime(request.getStartDate() + CommonConstants.DATE_START_TIME);
        orderSummaryDTO.setEndPayTime(request.getEndDate() + CommonConstants.DATE_END_TIME);
        log.info("生意助手经营分析,查询overview业务产值数据入参:{}", orderSummaryDTO.toString());
        RpcResponse<List<OrderSummaryDTO>> summaryOutputResponse = orderSummaryRemote.getSummaryOutputValueList(orderSummaryDTO);
        log.info("生意助手经营分析,查询overview业务产值数据返回结果:{}", summaryOutputResponse.getResult());
        if (summaryOutputResponse.hasResult()) {
            //太阳膜总产值
            BigDecimal solarTotalMoney = summaryOutputResponse.getResult()
                    .stream().map(OrderSummaryDTO::getTotalSubOrderOrderMoney).reduce(BigDecimal::add).orElse(BigDecimal.ZERO);
            solarTotalMoney = solarTotalMoney.setScale(0, BigDecimal.ROUND_HALF_UP);
            businessAnalysisOverviewResponse.setTotalMoney(solarTotalMoney.intValue());
        } else {
            log.error("生意助手经营分析,查询overview业务产值数据失败:{}", summaryOutputResponse.getMessage());
            throw new RpcServiceException(DefaultErrorEnum.SERVICE_ERROR);
        }
    }

    private void setBusinessAnalysisOverviewPurchaseMoney(BusinessAnalysisOverviewResponse businessAnalysisOverviewResponse, BusinessAnalysisRequest request) {
        List<Integer> allMerchantIds = CommonUtil.getAllMerchantIds();
        PurchaseOrderGoodsBaseDTO condition = new PurchaseOrderGoodsBaseDTO();
        //查询总采购价
        condition.setGoodsTypeList(Lists.newArrayList(SOLAR_FILE_TYPE));
        condition.setMerchantIdList(allMerchantIds);
        condition.setOrderTimeStart(request.getStartDate() + CommonConstants.DATE_START_TIME);
        condition.setOrderTimeEnd(request.getEndDate() + CommonConstants.DATE_END_TIME);
        log.info("生意助手经营分析,查询overview采购支出数据入参:{}", condition);
        RpcResponse<PurchaseOrderGoodsBaseDTO> response = purchaseOrderRemote.getPurchaseOrderGoodsInfo(condition);
        log.info("生意助手经营分析,查询overview采购支出数据结果:{}", response.getResult());
        if (response.isSuccess()) {
            PurchaseOrderGoodsBaseDTO result = response.getResult();
            if (Objects.isNull(result)) {
                return;
            }
            BigDecimal purchaseMoney = result.getGoodsTotalPrice();
            if (Objects.nonNull(purchaseMoney)) {
                purchaseMoney = purchaseMoney.setScale(0, BigDecimal.ROUND_HALF_UP);
                businessAnalysisOverviewResponse.setPurchaseMoney(purchaseMoney.intValue());
            }
        } else {
            log.error("生意助手经营分析,查询overview采购支出数据失败:{}", response.getMessage());
            throw new RpcServiceException(DefaultErrorEnum.SERVICE_ERROR);
        }
    }

    public RopResponse<BusinessAnalysisFlowAnalysisResponse> flowRankInfo(BusinessAnalysisRequest request) {
        try {
            BusinessAnalysisFlowAnalysisResponse businessAnalysisFlowAnalysisResponse = new BusinessAnalysisFlowAnalysisResponse();
            LifeCustomerCouponDTO condition = buildFlowRankCondition(request);
            condition.setLimit(3);
            //查询门店引流排行前三数据
            setFlowRankList(businessAnalysisFlowAnalysisResponse, condition);
            //查询引流分析数据
            setPieChartList(businessAnalysisFlowAnalysisResponse, condition);
            return RopResponse.success(businessAnalysisFlowAnalysisResponse);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new RpcServiceException(DefaultErrorEnum.SERVICE_ERROR);
        }
    }

    public LifeCustomerCouponDTO buildFlowRankCondition(BusinessAnalysisRequest request){
        LifeCustomerCouponDTO condition = new LifeCustomerCouponDTO();
        condition.setMerchantIdList(CommonUtil.getShopMerchantIds(SceneTypeEnum.SCENE1_1.getValue()));
        condition.setCouponTypes(Lists.newArrayList(SOLAR_FILE_TYPE.byteValue()));
        String startTime = request.getStartDate() + CommonConstants.DATE_START_TIME;
        String endTime = request.getEndDate() + CommonConstants.DATE_END_TIME;
        //筛选发券时间
        condition.setStartDate(startTime);
        condition.setEndDate(endTime);
        return condition;
    }

    /**
     * 门店排行数据
     * @param businessAnalysisFlowAnalysisResponse
     * @param condition
     */
    private void setFlowRankList(BusinessAnalysisFlowAnalysisResponse businessAnalysisFlowAnalysisResponse, LifeCustomerCouponDTO condition) {
        log.info("生意助手经营分析,查询门店引流排行前三入参:{}", condition);
        RpcResponse<List<BusinessAnalysisFlowRankDTO>> response = lifeCustomerCouponRemote.listForCouponRank(condition);
        log.info("生意助手经营分析,查询门店引流排行前三结果:{}", response.getResult());
        if (response.hasResult()) {
            businessAnalysisFlowAnalysisResponse.setFlowRankList(response.getResult());
        } else {
            log.error("生意助手经营分析,查询门店引流排行前三失败:{}", response.getMessage());
            throw new RpcServiceException(DefaultErrorEnum.SERVICE_ERROR);
        }
    }

    /**
     * 引流分析数据
     * @param businessAnalysisFlowAnalysisResponse
     * @param condition
     */
    private void setPieChartList(BusinessAnalysisFlowAnalysisResponse businessAnalysisFlowAnalysisResponse, LifeCustomerCouponDTO condition) {
        log.info("生意助手经营分析,查询引流分析数据入参:{}", condition);
        RpcResponse<List<BusinessAnalysisFlowRankDTO>> response = lifeCustomerCouponRemote.listForCouponNotUpgradeOrder(condition);
        List<BusinessAnalysisFlowRankDTO> result = response.getResult();
        log.info("生意助手经营分析,查询引流分析数据结果:{}", result);
        if (response.hasResult()) {
            buildPieChart(result, businessAnalysisFlowAnalysisResponse);
        } else {
            log.error("生意助手经营分析,查询引流分析数据结果失败:{}", response.getMessage());
            throw new RpcServiceException(DefaultErrorEnum.SERVICE_ERROR);
        }
    }

    /**
     * 构建引流分析饼图
     * @param result
     * @param businessAnalysisFlowAnalysisResponse
     */
    private void buildPieChart(List<BusinessAnalysisFlowRankDTO> result, BusinessAnalysisFlowAnalysisResponse businessAnalysisFlowAnalysisResponse) {
        if (CollectionUtils.isEmpty(result)) {
            businessAnalysisFlowAnalysisResponse.setPieChartList(Lists.newArrayList());
            return;
        }
        List<NameValueDTO> pieChartList = Lists.newArrayList();
        NameValueDTO chart1 = new NameValueDTO(REFUSAL_REASON1, 0);
        Integer chart1Value = 0;
        NameValueDTO chart2 = new NameValueDTO(REFUSAL_REASON2, 0);
        Integer chart2Value = 0;
        NameValueDTO chart3 = new NameValueDTO(REFUSAL_REASON3, 0);
        Integer chart3Value = 0;
        NameValueDTO chart4 = new NameValueDTO(REFUSAL_REASON4, 0);
        Integer chart4Value = 0;
        NameValueDTO chart5 = new NameValueDTO(REFUSAL_REASON5, 0);
        Integer chart5Value = 0;
        for (BusinessAnalysisFlowRankDTO businessAnalysisFlowRankDTO : result) {
            String refusalReason = businessAnalysisFlowRankDTO.getRefusalReason();
            if (StringUtils.isBlank(refusalReason)) {
                continue;
            }
            boolean containsGiveReason = false;
            if (refusalReason.indexOf(REFUSAL_REASON1) >= 0) {
                chart1Value++;
                containsGiveReason = true;
            }
            if (refusalReason.indexOf(REFUSAL_REASON2) >= 0) {
                chart2Value++;
                containsGiveReason = true;
            }
            if (refusalReason.indexOf(REFUSAL_REASON3) >= 0) {
                chart3Value++;
                containsGiveReason = true;
            }
            if (refusalReason.indexOf(REFUSAL_REASON4) >= 0) {
                chart4Value++;
                containsGiveReason = true;
            }
            // 不包含以上4种拒绝原因,算作其他
            if (!containsGiveReason) {
                chart5Value++;
            }
        }
        chart1.setValue(chart1Value);
        chart2.setValue(chart2Value);
        chart3.setValue(chart3Value);
        chart4.setValue(chart4Value);
        chart5.setValue(chart5Value);
        pieChartList.add(chart1);
        pieChartList.add(chart2);
        pieChartList.add(chart3);
        pieChartList.add(chart4);
        pieChartList.add(chart5);
        businessAnalysisFlowAnalysisResponse.setPieChartList(pieChartList);
    }

    public RopResponse<RpcPagination<BusinessAnalysisFlowRankDTO>> flowRankPage(BusinessAnalysisRequest businessAnalysisRequest) {
        try {
            RpcPagination<LifeCustomerCouponDTO> rpcPagination = CommonUtil.getRpcPagination(LifeCustomerCouponDTO.class);
            LifeCustomerCouponDTO condition = buildFlowRankCondition(businessAnalysisRequest);
            rpcPagination.setCondition(condition);
            RpcResponse<RpcPagination<BusinessAnalysisFlowRankDTO>> response = lifeCustomerCouponRemote.listForCouponRankPage(rpcPagination);
            if (!response.hasResult()) {
                throw new RpcServiceException(DefaultErrorEnum.SERVICE_ERROR);
            }
            return RopResponse.success(response.getResult());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new RpcServiceException(DefaultErrorEnum.SERVICE_ERROR);
        }
    }

    public RopResponse<BusinessAnalysisPurchaseTendencyResponse> purchaseTendency(AbstractRopRequest request) {
        BusinessAnalysisPurchaseTendencyResponse result = new BusinessAnalysisPurchaseTendencyResponse();
        PurchaseOrderGoodsBaseDTO condition = buildPurchaseOrderGoodsBaseDTO();
        //查询总采购价、采购数量
        setPurchaseTotalInfo(condition, result);
        // 查询近半年采购金额趋势数据
        setDiagramList(condition, result);
        return RopResponse.success(result);
    }

    private PurchaseOrderGoodsBaseDTO buildPurchaseOrderGoodsBaseDTO() {
        List<Integer> allMerchantIds = CommonUtil.getAllMerchantIds();
        PurchaseOrderGoodsBaseDTO condition = new PurchaseOrderGoodsBaseDTO();
        //查询总采购价
        condition.setGoodsTypeList(Lists.newArrayList(SOLAR_FILE_TYPE));
        condition.setMerchantIdList(allMerchantIds);
        return condition;
    }

    private void setDiagramList(PurchaseOrderGoodsBaseDTO condition, BusinessAnalysisPurchaseTendencyResponse result) {
        //查询近半年的数据
        List<MerchantPurchaseTendencyDiagramDTO> diagramList = Lists.newArrayList();
        List<String> monthList = DateUtils.getLatestMonthList(12);
        for (String month : monthList) {
            MerchantPurchaseTendencyDiagramDTO diagramDTO = new MerchantPurchaseTendencyDiagramDTO();
            diagramDTO.setAbscissaData(month);
            diagramList.add(diagramDTO);
        }
        condition.setOrderTimeStart(DateUtils.getMonthStartTime(0, 12, true));
        condition.setOrderTimeEnd(DateUtils.getMonthEndTime(0, 0, true));
        log.info("生意助手经营分析,查询采购金额走势数据入参:{}", condition);
        RpcResponse<List<PurchaseOrderGoodsBaseDTO>> response = purchaseOrderRemote.getPurchaseOrderGoodsPurchaseMoneyList(condition);
        log.info("生意助手经营分析,查询采购金额走势数据结果:{}", response.getResult());
        if (!response.hasResult()) {
            log.error("生意助手经营分析,查询门店引流排行前三失败:{}", response.getMessage());
            throw new RpcServiceException(DefaultErrorEnum.SERVICE_ERROR);
        }
        List<PurchaseOrderGoodsBaseDTO> stockMonthReportDTOList = response.getResult();
        for (MerchantPurchaseTendencyDiagramDTO diagramDTO : diagramList) {
            for (PurchaseOrderGoodsBaseDTO purchaseOrderGoodsBaseDTO : stockMonthReportDTOList) {
                if (Objects.equals(diagramDTO.getAbscissaData(), purchaseOrderGoodsBaseDTO.getDataMonth())) {
                    BigDecimal purchaseMoney = purchaseOrderGoodsBaseDTO.getGoodsTotalPrice();
                    if (Objects.nonNull(purchaseMoney)) {
                        purchaseMoney = purchaseMoney.setScale(0, BigDecimal.ROUND_HALF_UP);
                    }
                    diagramDTO.setPurchaseMoney(purchaseMoney.intValue());
                }
            }
        }
        result.setDiagramList(diagramList);
    }

    /**
     * 查询总采购价、总采购数量
     * @param condition
     * @param result
     */
    private void setPurchaseTotalInfo(PurchaseOrderGoodsBaseDTO condition, BusinessAnalysisPurchaseTendencyResponse result) {
        log.info("生意助手经营分析,查询总采购价、总采购数量入参:{}", condition);
        RpcResponse<PurchaseOrderGoodsBaseDTO> response = purchaseOrderRemote.getPurchaseOrderGoodsInfo(condition);
        PurchaseOrderGoodsBaseDTO purchaseOrderGoodsBaseDTO = response.getResult();
        log.info("生意助手经营分析,查询总采购价、总采购数量结果:{}", purchaseOrderGoodsBaseDTO);
        if (response.isSuccess()) {
            if (Objects.isNull(purchaseOrderGoodsBaseDTO)) {
                return;
            }
            Integer purchaseNum = purchaseOrderGoodsBaseDTO.getGoodsNumber();
            BigDecimal purchaseMoney = purchaseOrderGoodsBaseDTO.getGoodsTotalPrice();
            result.setPurchaseNum(purchaseNum);
            if (Objects.nonNull(purchaseMoney)) {
                purchaseMoney = purchaseMoney.setScale(0, BigDecimal.ROUND_HALF_UP);
                result.setPurchaseMoney(purchaseMoney.intValue());
            }
        } else {
            log.error("生意助手经营分析,查询引流分析数据结果失败:{}", response.getMessage());
            throw new RpcServiceException(DefaultErrorEnum.SERVICE_ERROR);
        }
    }

    public RopResponse<RpcPagination<PurchaseOrderGoodsBaseDTO>> purchasePage(AbstractRopRequest request) {
        try {
            RpcPagination<PurchaseOrderGoodsBaseDTO> rpcPagination = CommonUtil.getRpcPagination(PurchaseOrderGoodsBaseDTO.class);
            PurchaseOrderGoodsBaseDTO condition = buildPurchaseOrderGoodsBaseDTO();
            rpcPagination.setCondition(condition);
            log.info("生意助手经营分析,采购数量分页查询入参:{}", condition);
            RpcResponse<RpcPagination<PurchaseOrderGoodsBaseDTO>> response = purchaseOrderRemote.getPurchaseOrderGoodsPurchaseNumberRankPage(rpcPagination);
            log.info("生意助手经营分析,采购数量分页查询结果:{}", response.getResult());
            if (!response.hasResult()) {
                throw new RpcServiceException(DefaultErrorEnum.SERVICE_ERROR);
            }
            return RopResponse.success(response.getResult());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new RpcServiceException(DefaultErrorEnum.SERVICE_ERROR);
        }
    }

}