package com.ozs.plan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ozs.common.core.domain.AjaxResult;
import com.ozs.common.core.domain.entity.SysDept;
import com.ozs.common.core.domain.entity.SysDictData;
import com.ozs.common.core.domain.model.LoginUser;
import com.ozs.common.enums.ProjectStatus;
import com.ozs.common.enums.ProjectTypes;
import com.ozs.common.enums.SysFileRefEnum;
import com.ozs.common.exception.ServiceException;
import com.ozs.common.utils.StringUtils;
import com.ozs.common.utils.bean.BeanUtils;
import com.ozs.plan.doman.PlanQuarter;
import com.ozs.plan.doman.PlanYears;
import com.ozs.plan.doman.vo.requestVo.PlanQuarterStandardVo;
import com.ozs.plan.doman.vo.responseVo.PlanQuarterResponseVo;
import com.ozs.plan.mapper.PlanQuarterMapper;
import com.ozs.plan.mapper.PlanYearsMapper;
import com.ozs.plan.service.PlanQuarterService;
import com.ozs.system.domain.SysFileInfo;
import com.ozs.system.domain.SysFileRef;
import com.ozs.system.domain.vo.responseVo.SysDeptResponseVo;
import com.ozs.system.mapper.SysDeptMapper;
import com.ozs.system.mapper.SysFileRefMapper;
import com.ozs.system.service.ISysDeptService;
import com.ozs.system.service.ISysDictTypeService;
import com.ozs.system.service.SysFileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.validation.Validator;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author buzhanyi
 */
@Service
@Slf4j
public class PlanQuarterServiceImpl implements PlanQuarterService {

    @Autowired
    PlanQuarterMapper planQuarterMapper;
    @Autowired
    PlanYearsMapper planYearsMapper;
    @Autowired
    protected Validator validator;
    @Autowired
    private ISysDictTypeService dictTypeService;
    @Autowired
    private ISysDeptService deptService;
    @Autowired
    SysFileRefMapper sysFileRefMapper;
    @Autowired
    private SysDeptMapper deptMapper;
    @Autowired
    private SysFileService fileService;

    @Override
    public PageInfo<PlanQuarter> selectPlanQuarterList(PlanQuarterStandardVo vo) {
        PlanQuarter quarters = new PlanQuarter();
        List<PlanQuarter> planQuarter;
        List<PlanQuarter> planQuarterList = new ArrayList<>();
        PageHelper.startPage(vo.getPageNum().intValue(), vo.getPageSize().intValue());
        try {
            BeanUtils.copyProperties(vo, quarters);
            planQuarter = planQuarterMapper.selectPlanQuarterList(quarters);
            planQuarterList = changeTo(planQuarter);
        } catch (Exception e) {
            e.printStackTrace();
        }
        PageInfo<PlanQuarter> pageInfo = new PageInfo<PlanQuarter>(planQuarterList);
        return pageInfo;
    }

    @Override
    public List<PlanQuarter> selectPlanQuarterListEXP(PlanQuarterStandardVo vo) {
        List<PlanQuarter> list = new ArrayList<>();
        List<PlanQuarter> planQuarterList = new ArrayList<>();
        try {
            int num = 1;
            int size = 200;
            while (size == 200) {
                LambdaQueryWrapper<PlanQuarter> lw = new LambdaQueryWrapper<PlanQuarter>();
                if (!ObjectUtils.isEmpty(vo.getProjectName())) {
                    lw.like(PlanQuarter::getProjectName, vo.getProjectName());
                }
                if (!ObjectUtils.isEmpty(vo.getPurchaseServices())) {
                    lw.eq(PlanQuarter::getPurchaseServices, vo.getPurchaseServices());
                }
                if (!ObjectUtils.isEmpty(vo.getIsExcess())) {
                    lw.eq(PlanQuarter::getIsExcess, vo.getIsExcess());
                }
                if (!ObjectUtils.isEmpty(vo.getProjectStatus())) {
                    lw.eq(PlanQuarter::getProjectStatus, vo.getProjectStatus());
                }
                if (!ObjectUtils.isEmpty(vo.getBeginTime())) {
                    lw.ge(PlanQuarter::getPlanDemandSubTime, vo.getBeginTime());
                }
                if (!ObjectUtils.isEmpty(vo.getEndTime())) {
                    lw.le(PlanQuarter::getPlanDemandSubTime, vo.getEndTime());
                }
                Page<PlanQuarter> page = planQuarterMapper.selectPage(new Page<PlanQuarter>(num, size, false), lw);
                list.addAll(page.getRecords());
                size = page.getRecords().size();
                num++;
            }
            planQuarterList = changeTo(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return planQuarterList;
    }

    @Override
    public PageInfo<PlanQuarter> selectPlanQuarterExamineList(PlanQuarterStandardVo vo) {
        PlanQuarter quarters = new PlanQuarter();
        PageHelper.startPage(vo.getPageNum().intValue(), vo.getPageSize().intValue());
        List<PlanQuarter> planQuarter;
        List<PlanQuarter> planQuarterList = new ArrayList<>();
        try {
            BeanUtils.copyProperties(vo, quarters);
            planQuarter = planQuarterMapper.selectPlanQuarterExamineList(quarters);
            planQuarterList = changeTo(planQuarter);
        } catch (Exception e) {
            e.printStackTrace();
        }
        PageInfo<PlanQuarter> pageInfo = new PageInfo<PlanQuarter>(planQuarterList);
        return pageInfo;
    }

    @Override
    public List<PlanQuarter> selectPlanQuarterExamineListEXP(PlanQuarterStandardVo vo) {
        List<PlanQuarter> list = new ArrayList<>();
        List<PlanQuarter> planQuarterList = new ArrayList<>();
        try {
            int num = 1;
            int size = 200;
            while (size == 200) {
                LambdaQueryWrapper<PlanQuarter> lw = new LambdaQueryWrapper<PlanQuarter>();
                if (!ObjectUtils.isEmpty(vo.getProjectName())) {
                    lw.like(PlanQuarter::getProjectName, vo.getProjectName());
                }
                if (!ObjectUtils.isEmpty(vo.getPurchaseServices())) {
                    lw.eq(PlanQuarter::getPurchaseServices, vo.getPurchaseServices());
                }
                if (!ObjectUtils.isEmpty(vo.getIsExcess())) {
                    lw.eq(PlanQuarter::getIsExcess, vo.getIsExcess());
                }
                if (!ObjectUtils.isEmpty(vo.getProjectStatus())) {
                    lw.eq(PlanQuarter::getProjectStatus, vo.getProjectStatus());
                } else {
                    lw.in(PlanQuarter::getProjectStatus, "1,3");
                }
                if (!ObjectUtils.isEmpty(vo.getBeginTime())) {
                    lw.ge(PlanQuarter::getPlanDemandSubTime, vo.getBeginTime());
                }
                if (!ObjectUtils.isEmpty(vo.getEndTime())) {
                    lw.le(PlanQuarter::getPlanDemandSubTime, vo.getEndTime());
                }
                Page<PlanQuarter> page = planQuarterMapper.selectPage(new Page<PlanQuarter>(num, size, false), lw);
                list.addAll(page.getRecords());
                size = page.getRecords().size();
                num++;
            }
            planQuarterList = changeTo(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return planQuarterList;
    }

    @Transactional
    @Override
    public AjaxResult insertPlanQuarter(PlanQuarterStandardVo quarterStandardVo) {
        if (planQuarterMapper.countProjectName(quarterStandardVo.getProjectName()) > 0) {
            return AjaxResult.error("该项目名称已经存在");
        }
        PlanQuarter quarters = new PlanQuarter();
        try {
            BeanUtils.copyProperties(quarterStandardVo, quarters);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //判断是否为超额计划
        quarters.setIsExcess(isExcessOrNo(quarters.getProjectType(), quarters.getEvaluation()));
        quarters.setProjectStatus(ProjectStatus.PLANWAITCOMMIT.getCode());
        quarters.setCreateTime(new Date());
        planQuarterMapper.insertPlanQuarter(quarters);
        log.info("id:{}", quarters.getPlanPracticalId());
        List<SysFileRef> sysFileRefs = quarterStandardVo.getSysFileRefs();
        if (!ObjectUtils.isEmpty(sysFileRefs)) {
            for (SysFileRef ref : sysFileRefs) {
                ref.setRedId(quarters.getPlanPracticalId());
                ref.setType(SysFileRefEnum.PLAN_TEMPORARY.getType());
                ref.setCreated(quarters.getCreated());
                ref.setCreateTime(new Date());
                ref.setUpdated(quarters.getCreated());
                ref.setUpdateTime(new Date());
                sysFileRefMapper.insert(ref);
            }
        }
        return AjaxResult.success();
    }

    @Override
    public AjaxResult deletePlanQuarterByIds(Long[] planIds) {
        planQuarterMapper.deletePlanQuarterByIds(planIds);
        return AjaxResult.success();
    }

    @Override
    public AjaxResult deletePlanQuarterById(Long planId) {
        planQuarterMapper.deletePlanQuarterById(planId);
        return AjaxResult.success();
    }

    @Override
    public AjaxResult view(PlanQuarterStandardVo quarterStandardVo) {

        PlanQuarterResponseVo responseVo = new PlanQuarterResponseVo();
        PlanQuarter byId = planQuarterMapper.getById(quarterStandardVo.getPlanPracticalId());
        if (byId == null) {
            return AjaxResult.error("数据查询失败");
        }
        SysDeptResponseVo sysDeptResponseVo = (SysDeptResponseVo) deptService.selectDeptById(byId.getPurchaseDeptId()).get("sysDept");
        byId.setPurchaseDeptName(sysDeptResponseVo.getDeptName());
        HashMap<String, Object> map = new HashMap<>();
        map.put("red_id", byId.getPlanPracticalId());
        map.put("type", SysFileRefEnum.PLAN_TEMPORARY.getType());
        List<SysFileRef> fileRefs = sysFileRefMapper.selectByMap(map);
        List<SysFileInfo> fileInfos = new ArrayList<>();
        BeanUtils.copyProperties(byId, responseVo);
        if (!ObjectUtils.isEmpty(fileRefs)) {
            for (SysFileRef ref : fileRefs) {
                SysFileInfo fileInfo = fileService.getById(ref.getFileId());
                fileInfos.add(fileInfo);
            }
            responseVo.setFileInfos(fileInfos);
        }
        return AjaxResult.success(responseVo);
    }

    @Transactional
    @Override
    public AjaxResult update(PlanQuarterStandardVo quarterStandardVo) {
        if (planQuarterMapper.countProjectNameOther(quarterStandardVo.getProjectName(), String.valueOf(quarterStandardVo.getPlanPracticalId())) > 0) {
            return AjaxResult.error("该项目名称已经存在");
        }
        PlanQuarter quarters = new PlanQuarter();
        try {
            BeanUtils.copyProperties(quarterStandardVo, quarters);
        } catch (Exception e) {
            e.printStackTrace();
        }
        quarters.setIsExcess(isExcessOrNo(quarters.getProjectType(), quarters.getEvaluation()));
        quarters.setUpdateTime(new Date());
        planQuarterMapper.updateInfoById(quarters);
        List<SysFileRef> sysFileRefs = quarterStandardVo.getSysFileRefs();
        HashMap<String, Object> map = new HashMap<>();
        map.put("red_id", quarters.getPlanPracticalId());
        map.put("type", SysFileRefEnum.PLAN_TEMPORARY.getType());
        sysFileRefMapper.deleteByMap(map);
        if (!ObjectUtils.isEmpty(sysFileRefs)) {
            for (SysFileRef ref : sysFileRefs) {
                ref.setRedId(quarters.getPlanPracticalId());
                ref.setType(SysFileRefEnum.PLAN_TEMPORARY.getType());
                ref.setCreated(quarterStandardVo.getUpdated());
                ref.setCreateTime(new Date());
                ref.setUpdated(quarterStandardVo.getUpdated());
                ref.setUpdateTime(new Date());
                sysFileRefMapper.insert(ref);
            }
        }
        return AjaxResult.success();
    }

    @Override
    public String importPlanQuarter(List<PlanQuarterStandardVo> planQuarters, boolean isUpdateSupport, LoginUser loginUser) {
        if (StringUtils.isNull(planQuarters) || planQuarters.size() == 0) {
            throw new ServiceException("导入季度计划数据不能为空！");
        }
        List<PlanQuarter> list = new ArrayList<>();
        for (PlanQuarterStandardVo standardVo : planQuarters) {
            PlanQuarter quarter = new PlanQuarter();
            BeanUtils.copyProperties(standardVo, quarter);
            quarter.setPurchaseDeptName(standardVo.getPurchaseDeptId());
            list.add(quarter);
        }
        HashMap<String, HashMap<String, String>> planEnums = dictTypeService.getAboutEnums();
        HashMap<String, String> projectTypesMap = planEnums.get("projectTypes");
        HashMap<String, String> planPurchaseModesMap = planEnums.get("planPurchaseModes");
        HashMap<String, String> projectAttributes = planEnums.get("projectAttributes");
        HashMap<String, String> purchaseServices = planEnums.get("purchaseServices");

        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        //deptService
        for (PlanQuarter ofYear : list) {
            try {
                //验证项目名称是否重复导入
                //将录入信息中的值更改为要保存的数据
                if (planQuarterMapper.countProjectName(ofYear.getProjectName()) == 0) {
                    //采购单位
                    SysDept info = deptMapper.checkDeptNameOnlyOne(ofYear.getPurchaseDeptName());
                    if (!ObjectUtils.isEmpty(info)) {
                        ofYear.setPurchaseDeptId(info.getDeptId());
                    } else {
                        ++failureNum;
                        failureMsg.append("*" + failureNum + "*采购单位“" + ofYear.getPurchaseDeptName() + "”不存在");
                    }
                    ofYear.setProjectType(projectTypesMap.get(ofYear.getProjectType()));
                    ofYear.setProjectStatus(ProjectStatus.PLANWAITCOMMIT.getCode());
                    ofYear.setPurchaseMode(planPurchaseModesMap.get(ofYear.getPurchaseMode()));
                    //项目属性是多选字段
                    StringBuilder builder = new StringBuilder();
                    if (ofYear.getProjectAttr().contains(",")) {
                        String[] split = ofYear.getProjectAttr().split(",");
                        for (String s : split) {
                            builder.append(projectAttributes.get(s) + ",");
                        }
                    } else {
                        builder.append(projectAttributes.get(ofYear.getProjectAttr()));
                    }
                    ofYear.setProjectAttr(builder.toString());
                    ofYear.setPurchaseServices(purchaseServices.get(ofYear.getPurchaseServices()));
                    ofYear.setIsExcess(isExcessOrNo(ofYear.getProjectType(), ofYear.getEvaluation()));
                    ofYear.setCreated(String.valueOf(loginUser.getUserId()));
                    ofYear.setCreateTime(new Date());
                    planQuarterMapper.insertPlanQuarter(ofYear);
                    successNum++;
                    successMsg.append("*" + successNum + "、项目 " + ofYear.getProjectName() + " 导入成功!");
                    //} else if (isUpdateSupport) {
                    //    PlanQuarter quarter = plan.get(0);
                    //    BeanValidators.validateWithException(validator, ofYear);
                    //    quarter.setCreateTime(new Date());
                    //    quarter.setProjectStatus(ProjectStatus.PLANWAITCOMMIT.getCode());
                    //    planQuarterMapper.updateById(quarter);
                    //    successNum++;
                    //    successMsg.append("<br/>" + successNum + "、项目 " + ofYear.getProjectName() + " 更新成功");
                } else {
                    failureNum++;
                    failureMsg.append(failureNum + "*项目 " + ofYear.getProjectName() + " 已存在！/n");
                }
            } catch (Exception exc) {
                failureNum++;
                String msg = "*" + successNum + "*项目 " + ofYear.getProjectName() + " 导入失败";
                failureMsg.append(msg + exc.getMessage());
                log.error(msg, exc);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "导入失败！共 " + failureNum + " 条数据格式不正确：");
            throw new ServiceException(failureMsg.toString());
        } else {
            successMsg.insert(0, "导入成功！共 " + successNum + " 条。");
        }
        return successMsg.toString();
    }

    @Override
    public AjaxResult commit(PlanQuarterStandardVo quarterStandardVo) {
        //PlanQuarter byId = PlanQuarterMapper.getById(quarterStandardVo.getPlanPracticalId());
        //BigDecimal evaluation = byId.getEvaluation();
        //BigDecimal threshold = new BigDecimal(0);
        ////获取各个项目类型设定的概算金额阈值
        //List<SysDictData> data = dictTypeService.selectDictDataByType("sys_over_limit_threshold");
        //HashMap<String, String> thresholdMap = new LinkedHashMap<>();
        ////各个类型的概算金额阈值
        //for (SysDictData dictData : data) {
        //    //    类型----阈值
        //    thresholdMap.put(dictData.getDictLabel(), dictData.getDictValue());
        //}
        ////项目类型
        //for (ProjectTypes value : ProjectTypes.values()) {
        //    if (byId.getProjectType().equals(value.getCode())) {
        //        threshold = BigDecimal.valueOf(Long.parseLong(thresholdMap.get(value.getInfo())));
        //    }
        //    break;
        //}
        //
        //if (evaluation.compareTo(threshold) == 1) {
        //    //是超额项目
        //}

        int commit = planQuarterMapper.commit(quarterStandardVo.getPlanPracticalId());
        if (commit != 1) {
            return AjaxResult.error("项目状态数据异常");
        }
        return AjaxResult.success();
    }

    @Override
    @Transactional
    public AjaxResult reviewTo(PlanQuarterStandardVo vo) {
        PlanQuarter quarters = new PlanQuarter();
        BeanUtils.copyProperties(vo, quarters);
        PlanQuarter byId = planQuarterMapper.getById(vo.getPlanPracticalId());
        if (ObjectUtils.isEmpty(byId.getPlanYearId())) {
            PlanYears ofYears = new PlanYears();
            BeanUtils.copyProperties(vo, ofYears);
            ofYears.setProjectStatus(ProjectStatus.PLANTOEXAMINE.getCode());
            planYearsMapper.insertPlanYears(ofYears);
            quarters.setPlanYearId(ofYears.getPlanYearId());
        }
        quarters.setProjectStatus(ProjectStatus.PLANTOEXAMINE.getCode());
        return review(vo, quarters);
    }

    @Override
    @Transactional
    public AjaxResult reviewReturn(PlanQuarterStandardVo vo) {
        PlanQuarter quarters = new PlanQuarter();
        BeanUtils.copyProperties(vo, quarters);
        quarters.setProjectStatus(ProjectStatus.PLANTOBACK.getCode());
        return review(vo, quarters);
    }

    private AjaxResult review(PlanQuarterStandardVo vo, PlanQuarter quarters) {
        List<SysFileRef> sysFileRefs = vo.getSysFileRefs();
        if (!ObjectUtils.isEmpty(sysFileRefs)) {
            for (SysFileRef ref : sysFileRefs) {
                ref.setRedId(vo.getPlanPracticalId());
                ref.setType(SysFileRefEnum.PLAN_TEMPORARY.getType());
                ref.setCreated(vo.getUpdated());
                ref.setCreateTime(new Date());
                ref.setUpdated(vo.getUpdated());
                ref.setUpdateTime(new Date());
                sysFileRefMapper.insert(ref);
            }
        }
        int review = planQuarterMapper.review(quarters);
        if (review != 1) {
            return AjaxResult.error("项目状态数据异常");
        }
        return AjaxResult.success();
    }

    @Override
    public AjaxResult appUpdate(PlanQuarterStandardVo vo) {
        PlanQuarter quarters = new PlanQuarter();
        BeanUtils.copyProperties(vo, quarters);
        //申请后会成为未提交的状态，可以进行修改
        quarters.setProjectStatus(ProjectStatus.PLANWAITCOMMIT.getCode());
        int review = planQuarterMapper.review(quarters);
        if (review != 1) {
            return AjaxResult.error("项目状态数据异常");
        }
        return AjaxResult.success();
    }

    @Override
    public AjaxResult sendLetter(PlanQuarterStandardVo quarterStandardVo) {
        int review = planQuarterMapper.sendLetter(quarterStandardVo.getPlanPracticalId());
        if (review != 1) {
            return AjaxResult.error("项目状态数据异常");
        }
        return AjaxResult.success();
    }

    @Override
    public AjaxResult upLoadPlanFile(PlanQuarterStandardVo quarterStandardVo) {
        List<SysFileRef> sysFileRefs = quarterStandardVo.getSysFileRefs();
        if (!ObjectUtils.isEmpty(sysFileRefs)) {
            for (SysFileRef ref : sysFileRefs) {
                ref.setRedId(quarterStandardVo.getPlanPracticalId());
                ref.setType(SysFileRefEnum.PLAN_TEMPORARY.getType());
                sysFileRefMapper.insert(ref);
            }
        }
        return AjaxResult.success();
    }

    @Override
    public AjaxResult downLoadPlanFile(PlanQuarterStandardVo quarterStandardVo) {

        return AjaxResult.success();
    }

    //判断是否为超额计划
    public String isExcessOrNo(String projectType, BigDecimal evaluation) {
        BigDecimal threshold = new BigDecimal(0);
        //获取各个项目类型设定的概算金额阈值
        List<SysDictData> data = dictTypeService.selectDictDataByType("sys_over_limit_threshold");
        HashMap<String, String> thresholdMap = new LinkedHashMap<>();
        //各个类型的概算金额阈值
        for (SysDictData dictData : data) {
            //    类型----阈值
            thresholdMap.put(dictData.getDictLabel(), dictData.getDictValue());
        }
        //项目类型
        for (ProjectTypes value : ProjectTypes.values()) {
            if (projectType.equals(value.getCode())) {
                threshold = BigDecimal.valueOf(Long.parseLong(thresholdMap.get(value.getInfo())));
            }
            break;
        }
        if (evaluation.compareTo(threshold) == 1) {
            //是超额计划
            return ("1");
        } else {
            return ("0");
        }
    }

    //字段赋值对应的名称
    public List<PlanQuarter> changeTo(List<PlanQuarter> planQuarter) {

        HashMap<String, HashMap<String, String>> planEnums = dictTypeService.getAboutEnums();
        HashMap<String, String> projectTypesMap = planEnums.get("projectTypes");
        HashMap<String, String> planPurchaseModesMap = planEnums.get("planPurchaseModes");
        HashMap<String, String> projectAttributes = planEnums.get("projectAttributes");
        HashMap<String, String> purchaseServices = planEnums.get("purchaseServices");
        List<PlanQuarter> list = new ArrayList<PlanQuarter>();
        for (PlanQuarter planYear : planQuarter) {
            SysDeptResponseVo sysDeptResponseVo = (SysDeptResponseVo) deptService.selectDeptById(planYear.getPurchaseDeptId()).get("sysDept");
            planYear.setPurchaseDeptName(sysDeptResponseVo.getDeptName());

            for (Map.Entry<String, String> entry : projectTypesMap.entrySet()) {
                if (planYear.getProjectType().equals(entry.getValue())) {
                    planYear.setProjectTypeStr(entry.getKey());
                    break;
                }
            }
            for (Map.Entry<String, String> entry : planPurchaseModesMap.entrySet()) {
                if (planYear.getPurchaseMode().equals(entry.getValue())) {
                    planYear.setPurchaseModeStr(entry.getKey());
                    break;
                }
            }
            //项目属性是拼接的
            if (!ObjectUtils.isEmpty(planYear.getProjectAttr())) {
                if (planYear.getProjectAttr().length() > 1) {
                    StringBuilder builder = new StringBuilder();
                    String[] split = planYear.getProjectAttr().split(",");
                    for (String s : split) {
                        for (Map.Entry<String, String> entry : projectAttributes.entrySet()) {
                            if (s.equals(entry.getValue())) {
                                builder.append(entry.getKey() + ",");
                                break;
                            }
                        }
                    }
                    planYear.setProjectAttrStr(builder.toString());
                } else {
                    for (Map.Entry<String, String> entry : projectAttributes.entrySet()) {
                        if (planYear.getProjectAttr().equals(entry.getValue())) {
                            planYear.setProjectAttrStr(entry.getKey());
                            break;
                        }
                    }
                }
            }
            for (Map.Entry<String, String> entry : purchaseServices.entrySet()) {
                if (planYear.getPurchaseServices().equals(entry.getValue())) {
                    planYear.setPurchaseServicesStr(entry.getKey());
                    break;
                }
            }

            list.add(planYear);
        }
        return list;
    }
}
