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

import com.casic.missiles.dto.customer.CustomerExcellentResponse;
import com.casic.missiles.dto.customer.WorkbenceTrendResponse;
import com.casic.missiles.mapper.customer.CustomerWorkbenchMapper;
import com.casic.missiles.mapper.finance.FinanceAgreementInfoMapper;
import com.casic.missiles.service.customer.CustomerWorkbenchService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author cz
 */
@Service
@Slf4j
@AllArgsConstructor
public class CustomerWorkbenchServiceImpl implements CustomerWorkbenchService {

    private final CustomerWorkbenchMapper customerWorkbenchMapper;
    private final FinanceAgreementInfoMapper agreementInfoMapper;

    @Override
    public List<WorkbenceTrendResponse> sampleIncreaseTrend() {
        List<Map<String, Object>> sampleList = customerWorkbenchMapper.sampleIncreaseTrend();
        Map<String, String> trendMap = sampleList.stream().collect(
                Collectors.toMap(e -> String.valueOf(e.get("increaseTime")), e -> String.valueOf(e.get("count")))
        );
        return recentYearBuilder(trendMap);
    }

    @Override
    public List<WorkbenceTrendResponse> querySampleExpireTrend() {
        List<Map<String, Object>> sampleList = customerWorkbenchMapper.querySampleExpireTrend();
        Map<String, String> trendMap = sampleList.stream().collect(
                Collectors.toMap(e -> String.valueOf(e.get("expireTime")), e -> String.valueOf(e.get("count")))
        );
        return recentYearBuilder(trendMap);
    }

    @Override
    public List<WorkbenceTrendResponse> customerIncreaseTrend() {
        List<Map<String, Object>> sampleList = customerWorkbenchMapper.customerIncreaseTrend();
        Map<String, String> trendMap = sampleList.stream().collect(
                Collectors.toMap(e -> String.valueOf(e.get("increaseTime")), e -> String.valueOf(e.get("count")))
        );
        return recentYearBuilder(trendMap);
    }

    @Override
    public List<CustomerExcellentResponse> customerExcellentList() {
        return agreementInfoMapper.selectCustomerExcellentList();
    }

    private List<WorkbenceTrendResponse> recentYearBuilder(Map<String, String> trendMap) {
        List<WorkbenceTrendResponse> mapList = new ArrayList<>();
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMM");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年MM月");
        Calendar beginDate = Calendar.getInstance();
        beginDate.setTime(new Date());
        beginDate.add(Calendar.MONTH, -11);
        Calendar endDate = Calendar.getInstance();
        endDate.setTime(new Date());
        while (beginDate.getTime().compareTo(endDate.getTime()) <= 0) {
            WorkbenceTrendResponse workBenchResponse = new WorkbenceTrendResponse();
            workBenchResponse.setDate(sdf2.format(beginDate.getTime()));
            workBenchResponse.setCount(trendMap.containsKey(sdf1.format(beginDate.getTime())) ? trendMap.get(sdf1.format(beginDate.getTime())) : "0");
            mapList.add(workBenchResponse);
            beginDate.add(Calendar.MONTH, 1);
        }
        return mapList;
    }

}
