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

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

            PerformanceTarget target;
            PerformanceScore score;
            PerformanceRecord record;
            long recordId;
            long elementId2 = 1011001;//二级id 品质黄牌
            long elementId3 = 1011002;//二级id 品质红牌


            for(PerformanceArea p : organList){

                QualityOFRYCards cards = new QualityOFRYCards();
                String hierachyId = p.getHierachyId();

                cards.setHierarchyId(hierachyId);
                cards.setSecond_organization(p.getHierachyName());
                cards.setThird_organization(p.getHierachyName());
                cards.setHierarchyname(p.getHierachyName());
                cards.setQuarter(String.valueOf(quarter));
                cards.setYear(String.valueOf(year));
                //插入黄牌
                recordId = performanceRecordService.getRecordId(elementId2,year,quarter,hierachyId);
                score = performanceRecordService.getScore(recordId);
                if(score != null){
                    cards.setYellowcards(String.valueOf(score.getComplete()));
                }
                //插入红牌
                recordId = performanceRecordService.getRecordId(elementId3,year,quarter,hierachyId);
                score = performanceRecordService.getScore(recordId);
                if(score != null){
                    cards.setRedcards(String.valueOf(score.getComplete()));
                }
                //插入分数
                recordId = performanceRecordService.getRecordId(elementId,year,quarter,hierachyId);
                score = performanceRecordService.getScore(recordId);
                if(score != null){
                    cards.setPlus_OR_Less(String.valueOf(score.getScore()));
                }
                returnList.add(cards);
            }
            result.setDataList(returnList);
        }
            return result;
    }

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

        long elementId2 = 1011001;//二级id 品质黄牌
        long elementId3 = 1011002;//二级id 品质红牌
        int redcards=0;//红牌数
        int yellowcards=0;//黄牌数
        int plus_OR_Less; // 加减分


        String jsonString = JSONObject.toJSONString(values);

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

        for(QualityOFRYCards qualityOFRYCards : list){

            String hierachyId = qualityOFRYCards.getHierarchyId();

            RecordPerformanceDto performanceDto = new RecordPerformanceDto();
            if(qualityOFRYCards.getRedcards()!=null){
                redcards = (int)Double.parseDouble(qualityOFRYCards.getRedcards());
            }
            if(qualityOFRYCards.getYellowcards()!=null){
                yellowcards = (int)Double.parseDouble(qualityOFRYCards.getYellowcards());
            }
            plus_OR_Less = -(redcards * 2 + yellowcards);
            qualityOFRYCards.setPlus_OR_Less(String.valueOf(plus_OR_Less));
            ////////////以下进入入库和放重复update操作,入库的内容有 红牌数，黄牌数
            ///////////////////////1.黄牌数
            performanceDto.setYear(year);
            performanceDto.setQuarter(quarter);
            performanceDto.setElementId(elementId2);
            performanceDto.setHierachyId(hierachyId);
            performanceDto.setComplete(yellowcards);
            performanceRecordService.performanceRecord(performanceDto);

            ///////////////////2.红牌
            performanceDto.setElementId(elementId3);
            performanceDto.setComplete(redcards);
            performanceRecordService.performanceRecord(performanceDto);

            /////////////3.分数
            performanceDto.setElementId(elementId);
            performanceDto.setScore(plus_OR_Less);
            performanceDto.setComplete(-1);
            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.cardsUpload(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);
    }
}
