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

import cn.com.countrygarden.bi.main.model.performance.PerManagementArea;
import cn.com.countrygarden.bi.main.model.performance.PerformanceArea;
import cn.com.countrygarden.bi.main.model.performance.PerformanceRecord;
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 cn.com.countrygarden.bi.main.util.erp.StringUtil;
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 PermanagementareaServiceImpl<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<PerManagementArea> returnList = new ArrayList<>();

            PerformanceScore score;
            PerformanceRecord record;
            long recordId;

            for(PerformanceArea p : organList){

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

                String hierachyId = p.getHierachyId();
                record = performanceRecordService.getRecordSelective(elementId,year,quarter,hierachyId);
                if(record != null){

                    //是否考核,1表示考核，0表示不考核
                    perManagementArea.setIs_check(record.getIsExamine() == 1 ? 1 : 0);

                    recordId = record.getRecordId();
                    score = performanceRecordService.getScore(recordId);
                    if(score != null){
                        perManagementArea.setArea_RealValue(String.valueOf(score.getComplete()));//实际值
                        perManagementArea.setIndividualScore(String.valueOf(score.getScore()));// 指标单项得分
                    }
                }
                returnList.add(perManagementArea);
            }
            result.setDataList(returnList);
        }
        return result;
    }

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

        String jsonString = JSONObject.toJSONString(values);

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

        for (PerManagementArea value : list) {
            String hierachyId = value.getHierarchyId();

            String Completes = value.getArea_RealValue();
            String score = value.getIndividualScore();

            if (StringUtil.isBlankOrEmpty(Completes)) {
                continue;
            }

            RecordPerformanceDto performanceDto = new RecordPerformanceDto();
            performanceDto.setHierachyId(hierachyId);
            performanceDto.setYear(year);
            performanceDto.setQuarter(quarter);
            performanceDto.setElementId(elementId);
            performanceDto.setComplete(Float.parseFloat(Completes));
            performanceDto.setScore(Float.parseFloat(score));
            performanceDto.setIsExamine((short) value.getIs_check());
            performanceDto.setWeight(0.05);
            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.permanagementareaUpload(mf, year, 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);
    }
}
