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

import cn.com.countrygarden.bi.main.model.performance.Accidents;
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.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 AccidentServiceImpl<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<Accidents> returnList = new ArrayList<>();

            long elementId2 = 1013001;//二级id 事故数量
            PerformanceScore score;
            long recordId;

            for(PerformanceArea p : organList){

                Accidents accidents = new Accidents();
                accidents.setHierarchyId(p.getHierachyId());//组织id
                accidents.setSecond_organization(p.getHierachyName());//二级组织
                accidents.setThird_organization(p.getHierachyName());//三级组织
                accidents.setYear(String.valueOf(year));//设置年份
                accidents.setQuarter(String.valueOf(quarter));
                accidents.setHierarchyname(p.getHierachyName());//获取区域的名字

                String hierachyId = p.getHierachyId();

                //----------事故数量
                recordId = performanceRecordService.getRecordId(elementId,year,quarter,hierachyId);
                score = performanceRecordService.getScore(recordId);
                if(score != null){
                    accidents.setCount(String.valueOf(score.getComplete()));
                }

                //----------加减分
                recordId = performanceRecordService.getRecordId(elementId2,year,quarter,hierachyId);
                score = performanceRecordService.getScore(recordId);
                if(score != null){
                    accidents.setPlus_OR_Less(String.valueOf(score.getScore()));
                }

                returnList.add(accidents);
            }
            result.setDataList(returnList);
        }
            return result;
    }

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

        int count;//事故数量
        int plus_OR_Less; //加减分

        String jsonString = JSONObject.toJSONString(values);

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

        //遍历集合将对象存入数据库中
        for(int i = 0; i < list.size(); i++){

            count = Integer.valueOf(list.get(i).getCount());
            plus_OR_Less = 2 * count;
            if (plus_OR_Less >= 10) {
                plus_OR_Less = 10;
            }
            plus_OR_Less = -plus_OR_Less;
            list.get(i).setPlus_OR_Less(String.valueOf(plus_OR_Less));
            //设置好数据就开始入库
            //先设置好事故数量
            String hierachyId = list.get(i).getHierarchyId();
            RecordPerformanceDto performanceDto = new RecordPerformanceDto();
            performanceDto.setYear(year);
            performanceDto.setQuarter(quarter);
            performanceDto.setHierachyId(hierachyId);
            performanceDto.setElementId(elementId);
            performanceDto.setScore(plus_OR_Less);
            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.accidentsUpload(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);
    }
}
