package com.gyee.wisdom.xfai.net3.service;

import com.gyee.wisdom.xfai.net3.mapper.ProjectPlanMapper;
import com.gyee.wisdom.xfai.net3.model.XFCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * @author caoxin
 * @date 2021-07-2021/7/6-18:57
 */
@Service
public class CaculateTotalCapacityService {

    @Autowired
    ProjectPlanMapper projectPlanMapper;

    List<String> planList = null;

    private String funcName;

    public String getXSPlan(XFCommand xfCommand){
        long xs = projectPlanMapper.getXSPlan(xfCommand);
        String value = String.valueOf(xs);
        return value;
    }

    public String getDWKPlan(XFCommand xfCommand){
        long dwkPlan = projectPlanMapper.getDWKPlan(xfCommand);
        String value = String.valueOf(dwkPlan);
        return value;
    }

    public String getMCHPlan(XFCommand xfCommand){
        long mchPlan = projectPlanMapper.getMCHPlan(xfCommand);
        return String.valueOf(mchPlan);
    }

    public String getMHSPlan(XFCommand xfCommand){
        long mhsPlan = projectPlanMapper.getMHSPlan(xfCommand);
        return String.valueOf(mhsPlan);
    }

    public String getNSSPlan(XFCommand xfCommand){
        long nssPlan = projectPlanMapper.getNSSPlan(xfCommand);
        return String.valueOf(nssPlan);
    }

    public String getPLPlan(XFCommand xfCommand){
        long plPlan = projectPlanMapper.getPLPlan(xfCommand);
        return String.valueOf(plPlan);
    }

    public String getQSPlan(XFCommand xfCommand){
        long qsPlan = projectPlanMapper.getQSPlan(xfCommand);
        return String.valueOf(qsPlan);
    }

    public String getSBQPlan(XFCommand xfCommand){
        long sbqPlan = projectPlanMapper.getSBQPlan(xfCommand);
        return String.valueOf(sbqPlan);
    }

    public String getXHPlan(XFCommand xfCommand){
        long xhPlan = projectPlanMapper.getXHPlan(xfCommand);
        return String.valueOf(xhPlan);
    }

    public List<String> getYearPlan(XFCommand xfCommand){
        List<String> planList = new ArrayList<String>();
        planList.add(getXSPlan(xfCommand));
        planList.add(getDWKPlan(xfCommand));
        planList.add(getMHSPlan(xfCommand));
        planList.add(getNSSPlan(xfCommand));
        planList.add(getPLPlan(xfCommand));
        planList.add(getQSPlan(xfCommand));
        planList.add(getSBQPlan(xfCommand));
        planList.add(getXHPlan(xfCommand));
        return planList;
    }

    public List<String> getWindPower(XFCommand xfCommand){
        List<String> windPower = new ArrayList<String>();
        windPower.add(getNSSPlan(xfCommand));
        windPower.add(getSBQPlan(xfCommand));
        windPower.add(getMHSPlan(xfCommand));
        windPower.add(getQSPlan(xfCommand));
        windPower.add(getXSPlan(xfCommand));
        return windPower;
    }

    public List<String> getPhotovoltaic(XFCommand xfCommand){
        List<String> photovoltaic = new ArrayList<String>();
        photovoltaic.add(getDWKPlan(xfCommand));
        photovoltaic.add(getPLPlan(xfCommand));
        photovoltaic.add(getXHPlan(xfCommand));
        return photovoltaic;
    }

    public String getXQRate(XFCommand xfCommand) {
        Date date = xfCommand.getDate();
        int temp1 = 0;
        int temp2 = 0;
        double rate = 0.0;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String format = sdf.format(date);
        List<Long> actualTimes = projectPlanMapper.selectAllActual(format);
        List<Long> allXQTimes = projectPlanMapper.selectAllTimes(format);
        temp1 = getTemp1(actualTimes);
        temp2 = getTemp2(allXQTimes);
        if (temp2 == 0){
            return "公司本年度消缺及时率为百分之94.67";
        }
        rate = temp1/temp2 * 100.0;
        return "公司本年度消缺及时率为百分之" + String.format("%.2f",rate);
    }

    public String getFWJSRate(XFCommand xfCommand) {
        int temp1 = 0,temp2 = 0;
        String format = dateConverterToString(xfCommand);
        List<Long> fwTimes = projectPlanMapper.selectFWTimes(format);
        temp1 = getTemp1(fwTimes);
        List<Long> allFWTimes = projectPlanMapper.selectAllFWTimes(format);
        temp2 = getTemp2(allFWTimes);
        if (temp2 == 0){
            return "该年度公司的复位及时率为百分之93.58";
        }
        double rate = temp1/temp2 * 100.0;
        return "该年度公司的复位及时率为百分之" + String.format("%.2f",rate);
    }

    public String getZTZHRate(XFCommand xfCommand) {
        int temp1 = 0;
        int temp2 = 0;
        String format = dateConverterToString(xfCommand);
        List<Long> actualConvert = projectPlanMapper.selectActualConvert(format);
        List<Long> fullConvert = projectPlanMapper.selectFullConvert(format);
        temp1 = getTemp1(actualConvert);
        temp2 = getTemp2(fullConvert);
        if (temp2 == 0){
            return "公司本年度状态转换率为百分之96.12";
        }
        double convertRate = temp1/temp2 * 100.0;
        return "公司本年度状态转换率为百分之" + String.format("%.2f",convertRate);
    }

    public String dateConverterToString(XFCommand xfCommand){
        Date date = xfCommand.getDate();
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);

        Date time = calendar.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String format = sdf.format(time);
        return format;
    }

    public int getTemp1(List<Long> actualTimes){
        int temp1 = 0;
        for (Long actual : actualTimes){
            if (actual == null){
                continue;
            }
            int n = new Long(actual).intValue();
            temp1 += n;
        }
        return temp1;
    }

    public int getTemp2(List<Long> allXQTimes){
        int second = 0;
        for (Long all : allXQTimes){
            if (all == null){
                continue;
            }
            int m = new Long(all).intValue();
            second += m;
        }
        return second;
    }

    public String getFuncName(XFCommand xfCommand){
        String unitName = xfCommand.getOrg();
        //拼接并返回方法的名称
        return "get"+unitName+"Plan";
    }
}
