package com.casic.missiles.service.Impl.cockpit;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.casic.missiles.dto.cockpit.*;
import com.casic.missiles.mapper.business.BusinessOrderMapper;
import com.casic.missiles.mapper.business.BusinessOrderSampleRelationMapper;
import com.casic.missiles.mapper.customer.CustomerAdviceMapper;
import com.casic.missiles.mapper.customer.CustomerMapper;
import com.casic.missiles.mapper.finance.FinanceAgreementInfoMapper;
import com.casic.missiles.mapper.finance.FinanceBusinessSettlementInfoMapper;
import com.casic.missiles.model.customer.CustomerAdviceInfo;
import com.casic.missiles.model.customer.CustomerInfo;
import com.casic.missiles.service.cockpit.ICustomerBusinessService;
import com.casic.missiles.utils.ConvertUtils;
import com.casic.missiles.utils.DateUtils;
import com.casic.missiles.utils.DictCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description:
 * @Author: wangpeng
 * @Date: 2023/6/14 17:18
 */
@Service
public class CustomerBusinessServiceImpl implements ICustomerBusinessService {
    @Autowired
    private CustomerAdviceMapper customerAdviceMapper;
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private BusinessOrderMapper orderMapper;
    @Autowired
    private BusinessOrderSampleRelationMapper orderSampleRelationMapper;
    @Autowired
    private FinanceAgreementInfoMapper agreementInfoMapper;
    @Autowired
    private FinanceBusinessSettlementInfoMapper settlementInfoMapper;

    @Override
    public List<BusTrendResponse> busTrendList() {
        //今年各月订单数量
        List<BusTrendResponse> list = orderMapper.selectPreMonthCountByYear();
        List<String> dateList = DateUtils.getCurrentDateMonthList();
        List<BusTrendResponse> finalList = new ArrayList<>();
        if(CollectionUtils.isEmpty(list)){
            for (String date : dateList) {
                BusTrendResponse busTrendResponse = new BusTrendResponse();
                busTrendResponse.setDimension(date);
                busTrendResponse.setOrderCount(0);
                finalList.add(busTrendResponse);
            }
        }else{
            for (String date : dateList) {
                Integer flag = 0;
                for (BusTrendResponse dto : list) {
                    if (dto.getDimension().equals(date)) {
                        finalList.add(dto);
                        flag = 1;
                    }
                }
                if (flag == 0) {
                    BusTrendResponse busTrendResponse = new BusTrendResponse();
                    busTrendResponse.setDimension(date);
                    busTrendResponse.setOrderCount(0);
                    finalList.add(busTrendResponse);
                }
            }
        }
        //各月样品数量
        List<BusTrendResponse> sampleList = orderSampleRelationMapper.selectPreMonthCountByYear();
        for (BusTrendResponse busTrendResponse : finalList) {
            Integer flag = 0;
            for (BusTrendResponse response : sampleList) {
                if(busTrendResponse.getDimension().equals(response.getDimension())){
                    busTrendResponse.setSampleCount(response.getSampleCount());
                    flag = 1;
                }
            }
            if(flag == 0){
                busTrendResponse.setSampleCount(0);
            }
        }
        return finalList;
    }

    @Override
    public List<ManageTrendResponse> manageTrendList() {
        //今年各月收入合同金额
        List<ManageTrendResponse> list = agreementInfoMapper.selectPreMonthCountByYear();
        List<String> dateList = DateUtils.getCurrentDateMonthList();
        List<ManageTrendResponse> finalList = new ArrayList<>();
        if(CollectionUtils.isEmpty(list)){
            for (String date : dateList) {
                ManageTrendResponse trendResponse = new ManageTrendResponse();
                trendResponse.setDimension(date);
                trendResponse.setAmount(0);
                finalList.add(trendResponse);
            }
        }else{
            for (String date : dateList) {
                Integer flag = 0;
                for (ManageTrendResponse dto : list) {
                    if (dto.getDimension().equals(date)) {
                        finalList.add(dto);
                        flag = 1;
                    }
                }
                if (flag == 0) {
                    ManageTrendResponse trendResponse = new ManageTrendResponse();
                    trendResponse.setDimension(date);
                    trendResponse.setAmount(0);
                    finalList.add(trendResponse);
                }
            }
        }
        //所有样品标价和*建议折扣+附加费用
        //先查询委托单和对应所有样品标价和
        List<ManageTrendResponse> transitList = settlementInfoMapper.selectPreMonthAmountByYear();
        transitList.forEach(response -> {
            BigDecimal postPrice = new BigDecimal(response.getPostPrice());
            BigDecimal suggestedPrice = postPrice.multiply(BigDecimal.valueOf(response.getSuggestedDiscount()))
                    .divide(BigDecimal.valueOf(100)).setScale(0, BigDecimal.ROUND_HALF_UP);
            response.setAmount(response.getExtraCharge() + suggestedPrice.intValue());
        });
        //计算每月各委托书结算金额
        List<ManageTrendResponse> settlementList = transitList.stream().collect(Collectors.toMap(ManageTrendResponse::getDimension, a -> a, (o1, o2) -> {
            o1.setAmount(o1.getAmount() + o2.getAmount());
            return o1;
        })).values().stream().collect(Collectors.toList());

        for (ManageTrendResponse trendResponse : finalList) {
            for (ManageTrendResponse response : settlementList) {
                if(trendResponse.getDimension().equals(response.getDimension())){
                    trendResponse.setAmount(trendResponse.getAmount() + response.getAmount());
                }
            }
        }
        return finalList;
    }

    @Override
    public List<AdviceResponse> adviceList() throws Exception {
        QueryWrapper<CustomerAdviceInfo> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("advice_time");
        List<CustomerAdviceInfo> customerAdviceInfos = customerAdviceMapper.selectList(wrapper);
        for (CustomerAdviceInfo customerAdviceInfo : customerAdviceInfos) {
            DictCodeUtils.convertDictCodeToName(customerAdviceInfo);
        }
        List<AdviceResponse> adviceResponses = ConvertUtils.sourceToTarget(customerAdviceInfos, AdviceResponse.class);
        return adviceResponses;
    }

    @Override
    public CoverageResponse coverage() {
        QueryWrapper<CustomerInfo> provinceWrapper = new QueryWrapper<>();
        provinceWrapper.select("DISTINCT address_province");
        Long provinceCount = customerMapper.selectCount(provinceWrapper);
        QueryWrapper<CustomerInfo> cityWrapper = new QueryWrapper<>();
        cityWrapper.select("DISTINCT address_city");
        Long cityCount = customerMapper.selectCount(cityWrapper);
        QueryWrapper<CustomerInfo> areaWrapper = new QueryWrapper<>();
        areaWrapper.select("DISTINCT address_area");
        Long areaCount = customerMapper.selectCount(areaWrapper);
        CoverageResponse response = new CoverageResponse();
        response.setCoverageProvinceCount(provinceCount);
        response.setCoverageCityCount(cityCount);
        response.setCoverageAreaCount(areaCount);
        return response;
    }

    @Override
    public FeedbackResponse feedback() {
        QueryWrapper<CustomerAdviceInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("advice_class",0);
        Long adviceCount = customerAdviceMapper.selectCount(wrapper);
        QueryWrapper<CustomerAdviceInfo> complainWrapper = new QueryWrapper<>();
        complainWrapper.eq("advice_class",1);
        Long complainCount = customerAdviceMapper.selectCount(complainWrapper);
        QueryWrapper<CustomerAdviceInfo> customerWrapper = new QueryWrapper<>();
        customerWrapper.select("DISTINCT customer_id");
        Long customerCount = customerAdviceMapper.selectCount(customerWrapper);
        FeedbackResponse response = new FeedbackResponse();
        response.setAdviceCount(adviceCount);
        response.setComplainCount(complainCount);
        response.setCustomerCount(customerCount);
        return response;
    }

    @Override
    public List<CityTopResponse> cityTop() {
        //各城市订单数top10，分为检定和校准
        List<CityTopResponse> list = orderMapper.selectOrderCityRank();
        return list;
    }

}
