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

import com.casic.missiles.dto.business.order.SampleOverdueResponse;
import com.casic.missiles.dto.business.order.SampleOverdueTotalResponse;
import com.casic.missiles.dto.business.order.labIncomeResponse;
import com.casic.missiles.dto.business.order.labIncomeTotalResponse;
import com.casic.missiles.dto.cockpit.AnnualDataResponse;
import com.casic.missiles.dto.cockpit.MeasureWorkResponse;
import com.casic.missiles.dto.cockpit.TrafficStatisticsResponse;
import com.casic.missiles.dto.customer.CustomerExcellentResponse;
import com.casic.missiles.enums.business.AgreementStatusEnum;
import com.casic.missiles.mapper.business.BusinessOrderMapper;
import com.casic.missiles.mapper.business.BusinessOrderSampleRelationMapper;
import com.casic.missiles.mapper.finance.FinanceAgreementInfoMapper;
import com.casic.missiles.service.cockpit.IHomepageService;
import com.casic.missiles.service.finance.IFinanceBusinessSettlementInfoService;
import lombok.AllArgsConstructor;
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.Objects;

/**
 * @Description: 计量管理驾驶舱首页
 * @Author: wangpeng
 * @Date: 2023/6/5 13:56
 */
@Service
@AllArgsConstructor
public class HomepageServiceImpl implements IHomepageService {
    private final BusinessOrderMapper businessOrderMapper;

    private final BusinessOrderSampleRelationMapper orderSampleRelationMapper;

    private final  FinanceAgreementInfoMapper agreementInfoMapper;

    @Autowired
    private IFinanceBusinessSettlementInfoService settlementInfoService;

    @Override
    public MeasureWorkResponse measureWork() {
        MeasureWorkResponse response = new MeasureWorkResponse();
        //年度订单
        Integer ordersThisYear = businessOrderMapper.ordersThisYear();
        //月度订单
        Integer ordersThisMonth = businessOrderMapper.ordersThisMonth();
        //今日订单数
        Integer ordersToday = businessOrderMapper.ordersToday();
        //本月业务量，本月检完的样品数，样品检完状态/待归还状态/和已归还状态和updateTime
        Integer samplesThisMonth = orderSampleRelationMapper.selectCountThisMonthForMeasureWork();
        //上月业务量，上月检完的样品数
        Integer samplesLastMonth = orderSampleRelationMapper.selectCountLastMonthForMeasureWork();

        response.setOrdersThisYear(ordersThisYear);
        response.setOrdersThisMonth(ordersThisMonth);
        response.setOrdersToday(ordersToday);
        response.setTrafficThisMonth(samplesThisMonth);
        response.setTrafficLastMonth(samplesLastMonth);
        return response;
    }

    @Override
    public List<TrafficStatisticsResponse> trafficStatistics(Integer months) {
        if(Objects.isNull(months)){
            months = 12;
        }
        List<TrafficStatisticsResponse> responseList = orderSampleRelationMapper.selectCountForTrafficStatisticsByMonths(months);
        return responseList;
    }

    @Override
    public AnnualDataResponse annualData() {
        //本年签订收入合同总金额
        Integer agreementYear = agreementInfoMapper.selectCompleteAmount("year", AgreementStatusEnum.ALREADY_COMPLETE);
        //本月签订收入合同总金额
        Integer agreementMonth = agreementInfoMapper.selectCompleteAmount("month", AgreementStatusEnum.ALREADY_COMPLETE);
        //本年业务结算总金额
        Integer settleYear = settlementInfoService.selectCompleteAmount("year");
        //本月业务结算总金额
        Integer settleMonth = settlementInfoService.selectCompleteAmount("month");
        Integer year = agreementYear + settleYear;
        Integer month = agreementMonth + settleMonth;
        String outputYear = BigDecimal.valueOf(year).divide(BigDecimal.valueOf(100), 2, BigDecimal.ROUND_HALF_UP).toPlainString();
        String outputMonth = BigDecimal.valueOf(month).divide(BigDecimal.valueOf(100), 2, BigDecimal.ROUND_HALF_UP).toPlainString();
        AnnualDataResponse response = new AnnualDataResponse();
        response.setOutputThisYear(outputYear);
        response.setOutputThisMonth(outputMonth);
        return response;
    }

    @Override
    public List<labIncomeResponse> labIncomeList(String dimension) {
        List<labIncomeResponse> list = agreementInfoMapper.selectAmountByDept(dimension);
        if(CollectionUtils.isEmpty(list)) {
            return new ArrayList<>();
        }
        for (labIncomeResponse labIncomeResponse : list) {
            String s = BigDecimal.valueOf(labIncomeResponse.getAmount()).divide(BigDecimal.valueOf(100), 2, BigDecimal.ROUND_HALF_UP).toPlainString();
            labIncomeResponse.setAmountYuan(s);
        }
        return list;
    }

    @Override
    public labIncomeTotalResponse labIncomeTotal(String dimension) {
        List<labIncomeResponse> list = agreementInfoMapper.selectAmountByDept(dimension);
        if(CollectionUtils.isEmpty(list)) {
            return new labIncomeTotalResponse();
        }
        int sum = list.stream().mapToInt(labIncomeResponse::getAmount).sum();
        String s = BigDecimal.valueOf(sum).divide(BigDecimal.valueOf(100), 2, BigDecimal.ROUND_HALF_UP).toPlainString();
        labIncomeTotalResponse response = new labIncomeTotalResponse();
        response.setAmount(sum);
        response.setAmountYuan(s);
        return response;
    }

    @Override
    public List<SampleOverdueResponse> sampleOverdue() {
        //1、超期未交付：超期且非已归还样品
        Integer delayedDelivery = orderSampleRelationMapper.selectCountForCockpit(1, null);
        //2、超期交付：超期且已归还
        Integer overdueDelivery = orderSampleRelationMapper.selectCountForCockpit(2, null);
        //3、按时交付：未超期且已归还
        Integer onTimeDelivery = orderSampleRelationMapper.selectCountForCockpit(3, null);
        List<SampleOverdueResponse> list = new ArrayList<>();
        SampleOverdueResponse response = new SampleOverdueResponse();
        response.setStatus("超期未交付");
        response.setCount(delayedDelivery);
        list.add(response);
        SampleOverdueResponse response1 = new SampleOverdueResponse();
        response1.setStatus("超期交付");
        response1.setCount(overdueDelivery);
        list.add(response1);
        SampleOverdueResponse response2 = new SampleOverdueResponse();
        response2.setStatus("按时交付");
        response2.setCount(onTimeDelivery);
        list.add(response2);
        return list;
    }

    @Override
    public SampleOverdueTotalResponse sampleOverdueTotal() {
        List<SampleOverdueResponse> list = this.sampleOverdue();
        int sum = list.stream().mapToInt(SampleOverdueResponse::getCount).sum();
        SampleOverdueTotalResponse response = new SampleOverdueTotalResponse();
        response.setCount(sum);
        return response;
    }

    @Override
    public List<CustomerExcellentResponse> customerExcellentList() {
        List<CustomerExcellentResponse> list = agreementInfoMapper.selectCustomerExcellentForCockpit();
        if(CollectionUtils.isEmpty(list)) {
            return new ArrayList<>();
        }
        for (CustomerExcellentResponse response : list) {
            String s = BigDecimal.valueOf(response.getTotalAmount()).divide(BigDecimal.valueOf(100), 2, BigDecimal.ROUND_HALF_UP).toPlainString();
            response.setTotalAmountYuan(s);
        }
        return list;
    }
}
