package cn.com.countrygarden.bi.main.service.performance.impl.indicators;

import cn.com.countrygarden.bi.main.model.performance.ParkingCost;
import cn.com.countrygarden.bi.main.model.performance.PerformanceArea;
import cn.com.countrygarden.bi.main.model.performance.PerformanceScore;
import cn.com.countrygarden.bi.main.model.performance.PerformanceTarget;
import cn.com.countrygarden.bi.main.model.performance.dto.PerformanceShowDataDto;
import cn.com.countrygarden.bi.main.model.performance.dto.RecordPerformanceDto;
import cn.com.countrygarden.bi.main.service.performance.indicators.AbstractIndicatorService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

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

/**
 * 停车收费业务实现类
 *
 * @Author：Wyman
 * @Date:2018/11/3
 */
@Service
public class ParkingCostServiceImpl<T>  extends AbstractIndicatorService<T> {
    @Override
    public Object showData(long elementId, int year, int quarter, int pageCurr, int pageSize, String typeNo) {

        //展示的dto实体
        PerformanceShowDataDto result = this.setVacancyInfo(elementId,year,quarter,typeNo);

        //使用mybatis PageHelper类进行分页
        PageHelper.startPage(pageCurr,pageSize);
        List<PerformanceArea> organList = this.performanceAreaService.getAllAreasByType(typeNo);//查出来的分页记录数

        if(organList.size() != 0){

            //组装返回的数据
            List<ParkingCost> returnList = new ArrayList<>();

            PerformanceTarget target;
            PerformanceScore score;
            long recordId;
            long elementId1 = 1014001;//停车收费双百目标应实施数 停车收费双百目标已实施数
            long elementId2 = 1014002;//停车收费收入目标值  停车收费收入实际值

            for(PerformanceArea p : organList){

                String hierachyId = p.getHierachyId();
                ParkingCost parkingCost = new ParkingCost();
                parkingCost.setHierarchyId(hierachyId);//组织id
                parkingCost.setSecond_organization(p.getHierachyName());//二级组织
                parkingCost.setThird_organization(p.getHierachyName());//三级组织
                parkingCost.setYear(year);//设置年份
                parkingCost.setQuarter(String.valueOf(quarter));

                //----------停车收费
                recordId = performanceRecordService.getRecordId(elementId,year,quarter,hierachyId);
                score = performanceRecordService.getScore(recordId);
                if(score != null){
                    parkingCost.setPlus_OR_Less((int)score.getScore());
                }

                //----------停车收费双百
                //---应实施数
                recordId = performanceRecordService.getRecordId(elementId1,year,quarter,hierachyId);
                target = performanceRecordService.getTarget(recordId);
                if(target != null){
                    parkingCost.setShould_Count(target.getHowlTarget());//停车收费双百目标应实施数
                }
                score = performanceRecordService.getScore(recordId);
                if(score != null){
                    parkingCost.setDone_Count(String.valueOf((int)score.getComplete())); //停车收费双百目标已实施数
                }

                //----------停车收费收入
                //---实际值
                recordId = performanceRecordService.getRecordId(elementId2,year,quarter,hierachyId);
                score = performanceRecordService.getScore(recordId);
                if(score != null){
                    parkingCost.setRealIncome(String.valueOf((int)score.getComplete()));//停车收费收入实际值
                }
                target = performanceRecordService.getTarget(recordId);
                if(target != null){
                    parkingCost.setPointIncome(target.getHowlTarget());//停车收费双百目标应实施数
                }
                returnList.add(parkingCost);
            }
            result.setDataList(returnList);
        }
            return result;
    }

    @Override
    public Object saveData(long elementId, int year, int quarter, List<Object> values) {

        long elementId1 = 1014001;//停车收费双百目标应实施数 停车收费双百目标已实施数
        long elementId2 = 1014002;//停车收费收入目标值  停车收费收入实际值

        String jsonString = JSONObject.toJSONString(values);

        List<ParkingCost> list = JSON.parseObject(jsonString,new TypeReference<List<ParkingCost>>(){});

        for(ParkingCost p : list){
            String hierachyId = p.getHierarchyId();
            int should_Count = 0;
            int done_Count = 0;
            if (p.getShould_Count() != null && (!p.getShould_Count().equals(""))) {
                should_Count = Integer.valueOf(p.getShould_Count());// 停车收费双百目标应实施数
            }
            if (p.getDone_Count() != null && (!p.getDone_Count().equals(""))) {
                done_Count = Integer.valueOf(p.getDone_Count());// 停车收费双百目标已实施数
            }
            double realIncome = 0;// 实际收入
            String realIncome1 = p.getRealIncome();
            if (realIncome1 != null && !realIncome1.equals("")) {
                realIncome = Double.parseDouble(realIncome1);
            }

            double pointIncome = 0;// 停车收费收入目标值
            String pointIncome1 = p.getPointIncome();
            if (pointIncome1 != null && !pointIncome1.equals("")) {
                pointIncome = Double.parseDouble(pointIncome1);
            }
            int plus_OR_Less = p.getPlus_OR_Less(); // 加减分
            //modify by white
            //修改计算公式
            if (done_Count >= should_Count && realIncome >= pointIncome) {
                plus_OR_Less = 0;
            } else {
                plus_OR_Less = -3;
            }
//                if (done_Count  < should_Count && realIncome < pointIncome) {
//                    plus_OR_Less = plus_OR_Less - 3;
//                }
            p.setPlus_OR_Less(plus_OR_Less);// 设置计算结果返回值

            // 判断数据库是否已经存在该数据，有则更新，没有则插入
            RecordPerformanceDto performanceDto = new RecordPerformanceDto();
            performanceDto.setElementId(elementId);
            performanceDto.setYear(year);
            performanceDto.setQuarter(quarter);
            performanceDto.setHierachyId(hierachyId);
            performanceDto.setScore(plus_OR_Less);
            performanceRecordService.performanceRecord(performanceDto);

            performanceDto.setElementId(elementId1);
            performanceDto.setComplete((float)done_Count);
            performanceDto.setHowl(String.valueOf(should_Count));
            performanceRecordService.performanceRecord(performanceDto);

            performanceDto.setElementId(elementId2);
            performanceDto.setComplete((float) realIncome);
            performanceDto.setHowl(String.valueOf(pointIncome));
            performanceRecordService.performanceRecord(performanceDto);
        }
        performanceRecordService.saveQuartlyAverageScoreAndSumScore2(year,quarter);

        PerformanceShowDataDto dataDto = new PerformanceShowDataDto();
        dataDto.setDataList(values);
        //空缺值&空缺地区名称
        List<String> vacanHierachyList = performanceRecordService.getVacancyAreas(elementId,year,quarter,"1");
        dataDto.setNameList(vacanHierachyList);
        int vacancy = vacanHierachyList.size();
        dataDto.setVacancy(vacancy);
        return dataDto;
    }

    @Override
    public void uploadData(long elementId, int year, int quarter, MultipartFile mf, String OperatorId) {

        CommonsMultipartFile commonsMultipartFile = (CommonsMultipartFile)mf;
        String fileName = commonsMultipartFile.getOriginalFilename();
        if(!fileName.contains("停车收费")){
            throw new IllegalArgumentException("文件名不正确!");
        }

        //保存数据
        ea.parkingCostUpload(mf, year, String.valueOf(quarter), OperatorId);
        //保存平均分和总分
        this.performanceRecordService.saveQuartlyAverageScoreAndSumScore2(year,quarter);
    }

    @Override
    public Object selectAuthority(long elementId, Map<String, Object> requestMap, HttpServletRequest request) {
        return  this.performancePower.getUserPower(String.valueOf(elementId),requestMap,request);
    }
}
