package cn.com.countrygarden.bi.main.controller.performance.indicators;

import cn.com.countrygarden.bi.main.core.Response;

import cn.com.countrygarden.bi.main.core.Result;
import cn.com.countrygarden.bi.main.exception.ServiceException;
import cn.com.countrygarden.bi.main.model.AccessToken;
import cn.com.countrygarden.bi.main.model.performance.*;

import cn.com.countrygarden.bi.main.service.AccessTokenService;
import cn.com.countrygarden.bi.main.service.performance.PerformanceEnvService;
import cn.com.countrygarden.bi.main.service.performance.PerformanceScoreService;
import cn.com.countrygarden.bi.main.util.Calculate;
import cn.com.countrygarden.bi.main.util.ExcelAnalysis;
import cn.com.countrygarden.bi.main.util.PerformancePower;
import cn.com.countrygarden.bi.main.util.erp.StringUtil;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//物业服务费欠费追缴率1004  不设门槛值
@Controller("GetArrearsPropertyCostsController")
@RequestMapping("/GetArrearsPropertyCostsController")
public class GetArrearsPropertyCostsController {

    private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(GetArrearsPropertyCostsController.class);

    @Autowired
    private cn.com.countrygarden.bi.main.service.performance.PerformanceTargetService PerformanceTargetService;

    @Autowired
    private PerformanceScoreService performanceScoreService;

    @Autowired
    private AccessTokenService accessTokenService;

    @Autowired
    private PerformanceEnvService performanceEnvService;

    @Autowired
    private ExcelAnalysis ea;

    @Autowired
    private PerformancePower performancePower;

    @ResponseBody
    @RequestMapping(value = "/showArrearsPropertyCosts", method = {RequestMethod.POST, RequestMethod.GET})
    public Object showArrearsPropertyCosts(@RequestParam(value = "pageSize") int pageSize, @RequestParam(value = "pageCurr") int pageCurr, @RequestParam(value = "years") int year, @RequestParam(value = "quarterly") String quarterly) {
        Map<String, Object> returnMap = new HashMap<>();
        long elementId = 1004;//业服务费欠费追缴率1004

        List<GetArrearsPropertyCosts> returnList = new ArrayList<>();//初始化list 用来存东西

        PerformanceScore score = new PerformanceScore();//分数
        PerformanceScore sValue = new PerformanceScore();

        PerformanceTarget target = new PerformanceTarget();//目标值
        PerformanceTarget tValue = new PerformanceTarget();//目标值中间量

        String thisQuarter = quarterly;//设置季度值

        try {

            List<PerformanceEnv> envList = performanceEnvService.selectPerformanceOrgan();
            int count = envList.size();
            int start = (pageCurr - 1) * pageSize;
            int end = pageCurr * pageSize;
            envList = envList.subList(start < 0 ? 0 : start, end >= count ? count : end);

            //查询多少条数据是录入的
            PerformanceScore performanceScore = new PerformanceScore();
            performanceScore.setYear(year);//年
            performanceScore.setQuarterly(thisQuarter);//季度
            performanceScore.setElementId(elementId);
            int vacancy = count - performanceScoreService.getTotalPage(performanceScore).getTotalPage();//空缺，还有多少个区域指标没填
            if (vacancy < 0) {
                vacancy = 0;
            }
            List<String> vacancyHierarchyName = performanceEnvService.selectNotInName(performanceScore);

            //遍历  插入到bean
            for (PerformanceEnv performanceEnv : envList) {

                GetArrearsPropertyCosts propertyCosts = new GetArrearsPropertyCosts();

                //S1-S4
                //@Author:Geoffrey
                //[start]
                String hierarchyId = performanceEnv.getHierarchyId();
                propertyCosts.setThird_organization(performanceEnv.getHierarchyName());
                propertyCosts.setSecond_organization(performanceEnv.getHierarchyName());
                propertyCosts.setHierarchyId(hierarchyId);
                propertyCosts.setQuarter(quarterly);//设置季度值

                score.setHierarchyId(hierarchyId);
                score.setElementId(elementId);
                score.setYear(year);//设置年份

                target.setElementId(elementId);
                target.setHierarchyId(hierarchyId);
                target.setYear(year);

                for (int j = 1; j < 5; j++) {
                    score.setQuarterly(String.valueOf(j));//设置季度
                    target.setQuarterly(String.valueOf(j));

                    tValue = PerformanceTargetService.getNewByHierarchyId(target);
                    sValue = performanceScoreService.getscoreByHierarchyId(score);

                    if (tValue != null && sValue != null) {

                        if (j == 1) {
                            propertyCosts.setS1_threshold(tValue.getThresholdTarget());//门槛
                            propertyCosts.setS1_atLeast(tValue.getAtLeastTarget());//低保
                            propertyCosts.setS1_scream(tValue.getScreamTarget());//尖叫
                            propertyCosts.setS1_howl((tValue.getHowlTarget()));//嚎叫

                            propertyCosts.setS1_Complete_Value(String.valueOf(sValue.getComplete()));//插入完成值
                            propertyCosts.setS1_score(String.valueOf(sValue.getYearlyScore()));//插入细项得分
                        }
                        if (j == 2) {
                            propertyCosts.setS2_threshold(tValue.getThresholdTarget());//门槛
                            propertyCosts.setS2_atLeast(tValue.getAtLeastTarget());//低保
                            propertyCosts.setS2_scream(tValue.getScreamTarget());//尖叫
                            propertyCosts.setS2_howl((tValue.getHowlTarget()));//嚎叫

                            propertyCosts.setS2_Complete_Value(String.valueOf(sValue.getComplete()));//插入完成值
                            propertyCosts.setS2_score(String.valueOf(sValue.getYearlyScore()));//插入细项得分
                        }
                        if (j == 3) {
                            propertyCosts.setS3_threshold(tValue.getThresholdTarget());//门槛
                            propertyCosts.setS3_atLeast(tValue.getAtLeastTarget());//低保
                            propertyCosts.setS3_scream(tValue.getScreamTarget());//尖叫
                            propertyCosts.setS3_howl((tValue.getHowlTarget()));//嚎叫

                            propertyCosts.setS3_Complete_Value(String.valueOf(sValue.getComplete()));//插入完成值
                            propertyCosts.setS3_score(String.valueOf(sValue.getYearlyScore()));//插入细项得分
                        }
                        if (j == 4) {
                            propertyCosts.setS4_threshold(tValue.getThresholdTarget());//门槛
                            propertyCosts.setS4_atLeast(tValue.getAtLeastTarget());//低保
                            propertyCosts.setS4_scream(tValue.getScreamTarget());//尖叫
                            propertyCosts.setS4_howl((tValue.getHowlTarget()));//嚎叫

                            propertyCosts.setS4_Complete_Value(String.valueOf(sValue.getComplete()));//插入完成值
                            propertyCosts.setS4_score(String.valueOf(sValue.getYearlyScore()));//插入细项得分
                        }

                    }
                }
                //[end]

                //查出单项细数得分
                score.setYear(year);
                score.setHierarchyId(hierarchyId);
                score.setElementId(elementId);
                score.setQuarterly(quarterly);//设置季度
                sValue = performanceScoreService.getscoreByHierarchyId(score);
                if (sValue != null) {
                    propertyCosts.setIndividualScore(String.valueOf(sValue.getScore()));//单项系数得分
                }

                //////////////////////////
                PerformanceTarget tft = new PerformanceTarget();
                tft.setYear(year);//年
                tft.setQuarterly(quarterly);//季度
                tft.setHierarchyId(hierarchyId);//区域
                tft.setElementId(elementId);
                PerformanceTarget tftValue = PerformanceTargetService.getNewByHierarchyId(tft);
                if (tftValue != null) {
                    //是否考核,1表示考核，0表示不考核
                    if (StringUtil.isEmpty(tftValue.getIsCheck())) {
                        propertyCosts.setIs_check(0);
                    } else if (tftValue.getIsCheck().equals("1")) {
                        propertyCosts.setIs_check(1);
                    } else {
                        propertyCosts.setIs_check(0);
                    }
                }
                //////////////////////////

                propertyCosts.setYear(String.valueOf(year));
//                propertyCosts.setIs_check(1);
                returnList.add(propertyCosts);
            }
            returnMap.put("totalPage", count);
            returnMap.put("list", returnList);
            returnMap.put("vacancy", vacancy);
            returnMap.put("vacancyHierarchyName", vacancyHierarchyName);//无指标区域名称
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Response.response(returnMap);
    }

    @ResponseBody
    @RequestMapping(value = "/ArrearsPropertyCostsCal", method = {RequestMethod.POST, RequestMethod.GET})
    public Object ArrearsPropertyCostsCal(@RequestBody List<GetArrearsPropertyCosts> GetArrearsPropertyCosts) {
        Map<String, Object> map = new HashMap<>();
        long elementId1 = 1004;
        try {
            for (GetArrearsPropertyCosts costs : GetArrearsPropertyCosts) {
                //S1欠费追缴率
                double S1_Complete_Value = -1;          //S1欠费追缴率:完成值
                double S1_threshold;  // S1欠费追缴率:门槛值
                double S1_atLeast;    // S1欠费追缴率:保底
                double S1_scream;     // S1欠费追缴率:尖叫
                double S1_howl;       // S1欠费追缴率:嚎叫


                //S2欠费追缴率
                double S2_Complete_Value = -1;          //S2欠费追缴率:完成值
                double S2_threshold;  // S2欠费追缴率:门槛值
                double S2_atLeast;    // S2欠费追缴率:保底
                double S2_scream;     // S2欠费追缴率:尖叫
                double S2_howl;       // S2欠费追缴率:嚎叫


                //S3欠费追缴率
                double S3_Complete_Value = -1;          //S3欠费追缴率:完成值
                double S3_threshold;  // S3欠费追缴率:门槛值
                double S3_atLeast;    // S3欠费追缴率:保底
                double S3_scream;     // S3欠费追缴率:尖叫
                double S3_howl;       // S3欠费追缴率:嚎叫


                //S4欠费追缴率
                double S4_Complete_Value = -1;          //S4综合已收缴率完成值
                double S4_threshold;  // S4综合已收缴率:门槛值
                double S4_atLeast;    // S4综合已收缴率:保底
                double S4_scream;     // S4综合已收缴率:尖叫
                double S4_howl;       // S4综合已收缴率:嚎叫

                List<PerformanceScore> scores = new ArrayList<PerformanceScore>();//初始化list分数对象
                PerformanceScore PerformanceScore = new PerformanceScore();//初始化分数对象
                String S1_score = costs.getS1_score();      // S1欠费追缴率:细项目得分
                String S2_score = costs.getS2_score();      // S2欠费追缴率:细项目得分
                String S3_score = costs.getS3_score();      // S3欠费追缴率:细项目得分
                String S4_score = costs.getS4_score();      // S4综合已收缴率:细项目得分

                //因为计算必须要完成值所以就判断完成值是否为空
                //////////////////////////算出S1的细项得分
                if (costs.getQuarter().equals("1")) {
                    if (costs.getS1_Complete_Value() != null && !costs.getS1_Complete_Value().equals("")) {
                        try {
                            S1_Complete_Value = Double.parseDouble(costs.getS1_Complete_Value());
                            S1_threshold = Double.parseDouble(costs.getS1_threshold());
                            S1_atLeast = Double.parseDouble(costs.getS1_atLeast());
                            S1_scream = Double.parseDouble(costs.getS1_scream());
                            S1_howl = Double.parseDouble(costs.getS1_howl());
                        } catch (NumberFormatException e) {
                            S1_threshold = S1_atLeast = S1_scream = S1_howl = -1;
                        }
                        if (S1_Complete_Value != -1) {
                            S1_score = Calculate.InsertionMethod(S1_Complete_Value, -1, S1_atLeast, S1_scream, S1_howl, 1);// 计算出细项目得分综合收费
                            costs.setS1_score(S1_score);
                        } else {
                            S1_score = "-1";
                        }
                        //以下插入计算结果，先查询是否存在同样数据是的update，否则insert
                        PerformanceScore.setYearlyScore(Float.parseFloat(S1_score));//插入的细项得分
                        PerformanceScore.setElementId(elementId1);//按这个指标插入
                        PerformanceScore.setHierarchyId(costs.getHierarchyId());//按这个区域插
                        PerformanceScore.setYear(Integer.parseInt(costs.getYear()));//按年度
                        PerformanceScore.setQuarterly("1");//第一季度
                        PerformanceScore.setComplete((float) S1_Complete_Value);//插入实际值：就是完成值
                        scores.add(PerformanceScore);//因为getscoreByHierarchyId需要list对象所以这样处理
                        PerformanceScore objects = performanceScoreService.getscoreByHierarchyId(scores.get(0));//查询有没有这条记录


                        //判断是否重复是就更新否就插入完成值
                        if (objects == null) {
                            List<PerformanceScore> TScores = new ArrayList<>();
                            TScores.add(scores.get(0));
                            performanceScoreService.insertScoreList(TScores);
                            TScores.clear();
                            scores.clear();
                        } else {
                            performanceScoreService.UpdateNewScore(scores.get(0));
                            scores.clear();
                        }

                        //更新S1目标值
                        PerformanceTarget target = new PerformanceTarget();
                        target.setElementId(PerformanceScore.getElementId());
                        target.setHierarchyId(PerformanceScore.getHierarchyId());
                        target.setYear(PerformanceScore.getYear());
                        target.setQuarterly(PerformanceScore.getQuarterly());

                        target.setThresholdTarget(String.valueOf(S1_atLeast));
                        target.setAtLeastTarget(String.valueOf(S1_atLeast));
                        target.setScreamTarget(String.valueOf(S1_scream));
                        target.setHowlTarget(String.valueOf(S1_howl));

                        PerformanceTarget newTarget = PerformanceTargetService.getNewByHierarchyId(target);

                        if (newTarget != null) {
                            target.setTargetId(newTarget.getTargetId());
                            PerformanceTargetService.updateTargetByTargetId(target);
                        } else {
                            PerformanceTargetService.insertTargetSelective(target);
                        }

                    }
                }
                /////////////////////算出S2的细项得分
                if (costs.getQuarter().equals("2")) {
                    if (costs.getS2_Complete_Value() != null && !costs.getS2_Complete_Value().equals("")) {
                        S2_Complete_Value = Double.parseDouble(costs.getS2_Complete_Value());
                        if (costs.getS2_threshold() != null && !costs.getS2_threshold().equals("")) {
                            S2_threshold = Double.parseDouble(costs.getS2_threshold());
                        }
                        S2_atLeast = Double.parseDouble(costs.getS2_atLeast());
                        S2_scream = Double.parseDouble(costs.getS2_scream());
                        S2_howl = Double.parseDouble(costs.getS2_howl());
                        Calculate.InsertionMethod(S2_Complete_Value, -1, S2_atLeast, S2_scream, S2_howl, 1);
                        if (S2_Complete_Value != -1) {
                            S2_score = Calculate.InsertionMethod(S2_Complete_Value, -1, S2_atLeast, S2_scream, S2_howl, 1);// 计算出细项目得分综合收费
                            costs.setS2_score(S2_score);
                        } else {
                            S2_score = "-1";
                        }

                        //以下插入计算结果，先查询是否存在同样数据是的update，否则insert
                        PerformanceScore.setYearlyScore(Float.parseFloat(S2_score));//插入的细项得分
                        PerformanceScore.setElementId(elementId1);//按这个指标插入
                        PerformanceScore.setHierarchyId(costs.getHierarchyId());//按这个区域插
                        PerformanceScore.setYear(Integer.parseInt(costs.getYear()));//按年度
                        PerformanceScore.setQuarterly("2");//第一季度
                        PerformanceScore.setComplete((float) S2_Complete_Value);//插入实际值：就是完成值
                        scores.add(PerformanceScore);//因为getscoreByHierarchyId需要list对象所以这样处理
                        PerformanceScore objects = performanceScoreService.getscoreByHierarchyId(scores.get(0));//查询有没有这条记录
                        //判断是否重复是就更新否就插入
                        if (objects == null) {
                            List<PerformanceScore> TScores = new ArrayList<PerformanceScore>();
                            TScores.add(scores.get(0));
                            performanceScoreService.insertScoreList(TScores);
                            TScores.clear();
                            scores.clear();
                        } else {
                            performanceScoreService.UpdateNewScore(scores.get(0));
                            scores.clear();
                        }


                        //更新S2目标值
                        PerformanceTarget target = new PerformanceTarget();
                        target.setElementId(PerformanceScore.getElementId());
                        target.setHierarchyId(PerformanceScore.getHierarchyId());
                        target.setYear(PerformanceScore.getYear());
                        target.setQuarterly(PerformanceScore.getQuarterly());

                        target.setThresholdTarget(String.valueOf(S2_atLeast));
                        target.setAtLeastTarget(String.valueOf(S2_atLeast));
                        target.setScreamTarget(String.valueOf(S2_scream));
                        target.setHowlTarget(String.valueOf(S2_howl));

                        PerformanceTarget newTarget = PerformanceTargetService.getNewByHierarchyId(target);

                        if (newTarget != null) {
                            target.setTargetId(newTarget.getTargetId());
                            PerformanceTargetService.updateTargetByTargetId(target);
                        } else {
                            PerformanceTargetService.insertTargetSelective(target);
                        }

                    }
                }
                ///////////////////////算出s3
                if (costs.getQuarter().equals("3")) {
                    if (costs.getS3_Complete_Value() != null && !costs.getS3_Complete_Value().equals("")) {
                        S3_Complete_Value = Double.parseDouble(costs.getS3_Complete_Value());
                        if (costs.getS3_threshold() != null && !costs.getS3_threshold().equals("")) {
                            S3_threshold = Double.parseDouble(costs.getS3_threshold());
                        }
                        S3_atLeast = Double.parseDouble(costs.getS3_atLeast());
                        S3_scream = Double.parseDouble(costs.getS3_scream());
                        S3_howl = Double.parseDouble(costs.getS3_howl());
                        Calculate.InsertionMethod(S3_Complete_Value, -1, S3_atLeast, S3_scream, S3_howl, 1);
                        if (S3_Complete_Value != -1) {
                            S3_score = Calculate.InsertionMethod(S3_Complete_Value, -1, S3_atLeast, S3_scream, S3_howl, 1);// 计算出细项目得分综合收费
                            costs.setS3_score(S3_score);
                        } else {
                            S3_score = "-1";
                        }
                        //以下插入计算结果，先查询是否存在同样数据是的update，否则insert
                        PerformanceScore.setYearlyScore(Float.parseFloat(S3_score));//插入的细项得分
                        PerformanceScore.setElementId(elementId1);//按这个指标插入
                        PerformanceScore.setHierarchyId(costs.getHierarchyId());//按这个区域插
                        PerformanceScore.setYear(Integer.parseInt(costs.getYear()));//按年度
                        PerformanceScore.setQuarterly("3");//第3季度
                        PerformanceScore.setComplete((float) S3_Complete_Value);//插入实际值：就是完成值
                        scores.add(PerformanceScore);//因为getscoreByHierarchyId需要list对象所以这样处理
                        PerformanceScore objects = performanceScoreService.getscoreByHierarchyId(scores.get(0));//查询有没有这条记录
                        //判断是否重复是就更新否就插入
                        if (objects == null) {
                            List<PerformanceScore> TScores = new ArrayList<PerformanceScore>();
                            TScores.add(scores.get(0));
                            performanceScoreService.insertScoreList(TScores);
                            TScores.clear();
                            scores.clear();
                        } else {
                            performanceScoreService.UpdateNewScore(scores.get(0));
                            scores.clear();
                        }

                        //更新S3目标值
                        PerformanceTarget target = new PerformanceTarget();
                        target.setElementId(PerformanceScore.getElementId());
                        target.setHierarchyId(PerformanceScore.getHierarchyId());
                        target.setYear(PerformanceScore.getYear());
                        target.setQuarterly(PerformanceScore.getQuarterly());

                        target.setThresholdTarget(String.valueOf(S3_atLeast));
                        target.setAtLeastTarget(String.valueOf(S3_atLeast));
                        target.setScreamTarget(String.valueOf(S3_scream));
                        target.setHowlTarget(String.valueOf(S3_howl));

                        PerformanceTarget newTarget = PerformanceTargetService.getNewByHierarchyId(target);

                        if (newTarget != null) {
                            target.setTargetId(newTarget.getTargetId());
                            PerformanceTargetService.updateTargetByTargetId(target);
                        } else {
                            PerformanceTargetService.insertTargetSelective(target);
                        }
                    }
                }
                ////////////////算出s4
                if (costs.getQuarter().equals("4")) {
                    if (costs.getS4_Complete_Value() != null && !costs.getS4_Complete_Value().equals("")) {
                        S4_Complete_Value = Double.parseDouble(costs.getS4_Complete_Value());
                        if (costs.getS4_threshold() != null && !costs.getS4_threshold().equals("")) {
                            S4_threshold = Double.parseDouble(costs.getS4_threshold());
                        }
                        S4_atLeast = Double.parseDouble(costs.getS4_atLeast());
                        S4_scream = Double.parseDouble(costs.getS4_scream());
                        S4_howl = Double.parseDouble(costs.getS4_howl());
                        Calculate.InsertionMethod(S4_Complete_Value, -1, S4_atLeast, S4_scream, S4_howl, 1);
                        if (S4_Complete_Value != -1) {
                            S4_score = Calculate.InsertionMethod(S4_Complete_Value, -1, S4_atLeast, S4_scream, S4_howl, 1);// 计算出细项目得分综合收费
                            costs.setS4_score(S4_score);
                        } else {
                            S4_score = "-1";
                        }
                        //以下插入计算结果，先查询是否存在同样数据是的update，否则insert
                        PerformanceScore.setYearlyScore(Float.parseFloat(S4_score));//插入的细项得分
                        PerformanceScore.setElementId(elementId1);//按这个指标插入
                        PerformanceScore.setHierarchyId(costs.getHierarchyId());//按这个区域插
                        PerformanceScore.setYear(Integer.parseInt(costs.getYear()));//按年度
                        PerformanceScore.setQuarterly("4");//第3季度
                        PerformanceScore.setComplete((float) S4_Complete_Value);//插入实际值：就是完成值
                        scores.add(PerformanceScore);//因为getscoreByHierarchyId需要list对象所以这样处理
                        PerformanceScore objects = performanceScoreService.getscoreByHierarchyId(scores.get(0));//查询有没有这条记录
                        //判断是否重复是就更新否就插入
                        if (objects == null) {
                            List<PerformanceScore> TScores = new ArrayList<PerformanceScore>();
                            TScores.add(scores.get(0));
                            performanceScoreService.insertScoreList(TScores);
                            TScores.clear();
                            scores.clear();
                        } else {
                            performanceScoreService.UpdateNewScore(scores.get(0));
                            scores.clear();
                        }

                        //更新S4目标值
                        PerformanceTarget target = new PerformanceTarget();
                        target.setElementId(PerformanceScore.getElementId());
                        target.setHierarchyId(PerformanceScore.getHierarchyId());
                        target.setYear(PerformanceScore.getYear());
                        target.setQuarterly(PerformanceScore.getQuarterly());

                        target.setThresholdTarget(String.valueOf(S4_atLeast));
                        target.setAtLeastTarget(String.valueOf(S4_atLeast));
                        target.setScreamTarget(String.valueOf(S4_scream));
                        target.setHowlTarget(String.valueOf(S4_howl));

                        PerformanceTarget newTarget = PerformanceTargetService.getNewByHierarchyId(target);

                        if (newTarget != null) {
                            target.setTargetId(newTarget.getTargetId());
                            PerformanceTargetService.updateTargetByTargetId(target);
                        } else {
                            PerformanceTargetService.insertTargetSelective(target);
                        }
                        /////入库
                    }
                }
                //计算
                String IndividualScore = null;
                //IndividualScore = Calculate.CalnewS_Score(S1_score, S2_score, S3_score, S4_score);
                //根据不同的季度来选择不同的计算方式
                if (costs.getQuarter().equals("1")) {
                    IndividualScore = S1_score;
                } else if (costs.getQuarter().equals("2")) {
                    try {
                        double s1_temp = Double.parseDouble(S1_score);
                        double s2_temp = Double.parseDouble(S2_score);
                        IndividualScore = String.valueOf((s1_temp * 0.1 + s2_temp * 0.1) / 0.2);
                    } catch (NumberFormatException e) {
                        LOGGER.error("======GetArrearsPropertyCostsController.ArrearsPropertyCostsCal=====quarter：2，individualScore error");
                    }
                } else if (costs.getQuarter().equals("3")) {
                    try {
                        double s1_temp = Double.parseDouble(S1_score);
                        double s2_temp = Double.parseDouble(S2_score);
                        double s3_temp = Double.parseDouble(S3_score);
                        IndividualScore = String.valueOf((s1_temp * 0.1 + s2_temp * 0.1 + s3_temp * 0.1) / 0.3);
                    } catch (NumberFormatException e) {
                        LOGGER.error("======GetArrearsPropertyCostsController.ArrearsPropertyCostsCal=====quarter：3，individualScore error");
                    }
                } else if (costs.getQuarter().equals("4")) {
                    try {
                        double s1_temp = Double.parseDouble(S1_score);
                        double s2_temp = Double.parseDouble(S2_score);
                        double s3_temp = Double.parseDouble(S3_score);
                        double s4_temp = Double.parseDouble(S4_score);
                        IndividualScore = String.valueOf(s1_temp * 0.1 + s2_temp * 0.1
                                + s3_temp * 0.1 + s4_temp * 0.7);
                    } catch (NumberFormatException e) {
                        LOGGER.error("======GetArrearsPropertyCostsController.ArrearsPropertyCostsCal=====quarter：4，individualScore error");
                    }
                }
                if (costs.getIs_check() == 0) {
                    IndividualScore = "-1";
                }

                if (IndividualScore != null) {
                    costs.setIndividualScore(IndividualScore);
                    PerformanceScore.setScore(Float.parseFloat(IndividualScore));
                }

                //以下插入计算结果，先查询是否存在同样数据是的update，否则insert
                PerformanceScore.setElementId(elementId1);//按这个指标插入
                PerformanceScore.setHierarchyId(costs.getHierarchyId());//按这个区域插
                PerformanceScore.setYear(Integer.parseInt(costs.getYear()));//按年度
                PerformanceScore.setQuarterly(costs.getQuarter());//第几季度
                scores.add(PerformanceScore);//因为getscoreByHierarchyId需要list对象所以这样处理
                PerformanceScore objects = performanceScoreService.getscoreByHierarchyId(scores.get(0));//查询有没有这条记录

                //判断是否重复是就更新否就插入
                if (objects == null) {
                    List<PerformanceScore> TScores = new ArrayList<PerformanceScore>();
                    TScores.add(scores.get(0));
                    performanceScoreService.insertScoreList(TScores);
                    TScores.clear();
                    scores.clear();
                } else {
                    performanceScoreService.UpdateNewScore(scores.get(0));
                    scores.clear();
                }

                PerformanceTarget target = new PerformanceTarget();
                target.setHierarchyId(costs.getHierarchyId());
                target.setElementId(elementId1);
                target.setYear(Integer.valueOf(costs.getYear()));
                target.setQuarterly(costs.getQuarter());
                PerformanceTarget tValue = PerformanceTargetService.getNewByHierarchyId(target);
                if (tValue != null) {
                    tValue.setIsCheck(String.valueOf(costs.getIs_check()));
                    PerformanceTargetService.updateTargetSelective(tValue);
                } else {
                    target.setWeight(0.05);
                    target.setIsCheck(String.valueOf(costs.getIs_check()));
                    PerformanceTargetService.insertTargetSelective(target);
                }
            }
            map.put("list", GetArrearsPropertyCosts);
            List<PerformanceEnv> envList = performanceEnvService.selectPerformanceOrgan();
            int count = envList.size();
            PerformanceScore performanceScore = new PerformanceScore();
            performanceScore.setYear(Integer.parseInt(GetArrearsPropertyCosts.get(0).getYear()));//年
            performanceScore.setQuarterly(GetArrearsPropertyCosts.get(0).getQuarter());//季度
            performanceScore.setElementId(elementId1);
            int vacancy = count - performanceScoreService.getTotalPage(performanceScore).getTotalPage();//空缺，还有多少个区域指标没填
            if (vacancy < 0) {
                vacancy = 0;
            }
            map.put("vacancy", vacancy);//设置空缺数据
            List<String> vacancyHierarchyName = performanceEnvService.selectNotInName(performanceScore);
            map.put("vacancyHierarchyName", vacancyHierarchyName);//无指标区域名称
            performanceScoreService.saveQuartlyAverageScoreAndSumScore(Integer.parseInt(GetArrearsPropertyCosts.get(0).getYear()), Integer.parseInt(GetArrearsPropertyCosts.get(0).getQuarter()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Response.response(map);
    }

    @ResponseBody
    @RequestMapping(value = "/upload", produces = "application/json;charset=utf-8", method = RequestMethod.POST)
    public Result upload(@RequestParam(value = "year") int year, @RequestParam(value = "quarter") int quarter,
                         @RequestParam(value = "name") String name,
                         @RequestParam(value = "uploadfile", required = true) MultipartFile multipartFile,
                         HttpServletRequest request, HttpServletResponse response) throws ServiceException {
        String accessToken = request.getParameter("AccessToken");
        AccessToken token = accessTokenService.findByToken(accessToken);
        if (multipartFile == null) {
            return Response.responseFail("请选择需要上传的文件");
        } else if (name == null) {
            return Response.responseFail("请输入文件名");
        }
        CommonsMultipartFile commonsMultipartFile = (CommonsMultipartFile) multipartFile;
        if (commonsMultipartFile.isEmpty()) {
            return Response.responseFail("上传文件为空文件");
        }
        String fileName = commonsMultipartFile.getOriginalFilename();
        if (fileName.indexOf(".xlsx") <= 0 || fileName.indexOf(".xls") <= 0) {
            return Response.responseFail("上传文件应为excel文件格式为：xlsx 或 xls");
        }
        if (!fileName.contains("追缴率")) {
            return Response.responseFail("请选择正确的文件！");
        }
        try {
            ea.GetArrearsPropertyCostsUploadUpload(multipartFile, year, quarter, token.getUserCode());
            performanceScoreService.saveQuartlyAverageScoreAndSumScore(year, quarter);
            return Response.response("导入成功");
        } catch (RuntimeException e) {
            e.printStackTrace();
            return Response.responseFail(e.getMessage());
        }

    }

    /**
     * 查询和更新解锁状态，handle=1表示查询，handle=2表示更新
     *
     * @param requestMap json
     * @return Object
     * @author Max
     */
    @ResponseBody
    @RequestMapping(value = "/judgeLock")
    public Object judgeLock(@RequestBody Map<String, Object> requestMap, HttpServletRequest request) {
        String elementId = "1004";//业服务费欠费追缴率1004
        return Response.response(performancePower.getUserPower(elementId, requestMap, request));
    }
}