package org.jeecg.modules.stm.rate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.stm.rate.entity.*;
import org.jeecg.modules.stm.rate.mapper.RateTaskMapper;
import org.jeecg.modules.stm.rate.service.*;
import org.jeecg.modules.stm.rate.vo.*;
import org.jeecg.modules.stm.utils.UserUtil;
import org.jeecg.modules.system.service.ISysDictService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import org.apache.shiro.SecurityUtils;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * @Description: 任务分配
 * @Author: jeecg-boot
 * @Date:   2023-09-22
 * @Version: V1.0
 */
@Service
public class RateTaskServiceImpl extends ServiceImpl<RateTaskMapper, RateTask> implements IRateTaskService {

    @Autowired
    private IRateRuleService irateRuleService;
    @Autowired
    private IRateTaskdeptService irateTaskdeptService;
    @Autowired
    private IRateDomainService irateDomainService;
    @Autowired
    private ISysDictService iSysDictService;
    @Autowired
    private IRateKeyitemService irateKeyitemService;
    @Autowired
    private IRateOptService irateOptService;
    @Autowired
    private IRateFileService irateFileService;
    @Autowired
    private IRateStdService irateStdService;
    @Autowired
    private IRateTaskuserService irateTaskuserService;
    @Autowired
    private IRateDomainscoreService irateDomainscoreService;
    @Autowired
    private IRateDomaininfoService irateDomaininfoService;
    @Autowired
    private IRateUserscoreService rateUserscoreService;

    @Override
    public IPage<RateTask> pagePrepar(Page<RateTask> page, QueryWrapper<RateTask> queryWrapper) {
        //queryWrapper.eq("APPROVE_FLAG","1");
         IPage< RateTask > pageList =page(page, queryWrapper);
        List<RateTask> list = pageList.getRecords();
        List<RateTask> voList = new ArrayList<>();
        if(CollectionUtils.isEmpty(list)){
            return null;
        }
        for(RateTask entity:list) {
            RateTaskVo rateTaskVo = new RateTaskVo();
            BeanUtils.copyProperties(entity, rateTaskVo);
           String ruleid= entity.getRuleId();
            RateRule rateRule = irateRuleService.getById(ruleid);
            if(rateRule!=null){
                rateTaskVo.setRulename(rateRule.getRulename());
            }

            /**
            if(entity.getRateTaskstatus().equals("0")){
                rateTaskVo.setRateTaskstatus("false");
            }else{
                rateTaskVo.setRateTaskstatus("true");
            }*/

            voList.add(rateTaskVo);
        }
        pageList.setRecords(voList);
        return pageList;
    }

   public void taskDeptInit(String id){
     //System.out.println("why ok -------------------------why ok -------------------");
     //1 查询taskDept 看是否存在任务的初始化数据，无则插入
       QueryWrapper<RateTaskdept> rateTaskdeptQueryWrapper = new QueryWrapper<>();
       rateTaskdeptQueryWrapper.eq("rate_task_id",id);
       List<RateTaskdept> list = irateTaskdeptService.list(rateTaskdeptQueryWrapper);
       if(list.size()==0){
           //
           System.out.println("insert into taskDeptInit-------------------------why ok -------------------");
           QueryWrapper<RateTask> rateTaskQueryWrapper = new QueryWrapper<>();
           rateTaskQueryWrapper.eq("id",id);
           RateTask rateTask = this.getOne(rateTaskQueryWrapper);
           if(rateTask!=null&&rateTask.getRuleId()!=null&&!rateTask.getRuleId().equals("")){
               QueryWrapper<RateDomain> rateDomainQueryWrapper = new QueryWrapper<>();
               rateDomainQueryWrapper.eq("rule_id",rateTask.getRuleId());
               List<RateDomain> domainlist = irateDomainService.list(rateDomainQueryWrapper);
               for(int i=0;i<domainlist.size();i++){
                   RateDomain rateDomain =domainlist.get(i);
                   RateTaskdept rateTaskdept = new RateTaskdept();
                   rateTaskdept.setRateTaskId(id);
                   rateTaskdept.setRuleId(rateTask.getRuleId());
                   rateTaskdept.setRateDomainId(rateDomain.getId());
                   rateTaskdept.setRatedomain(rateDomain.getRatedomain());
                   rateTaskdept.setSondomain(rateDomain.getSondomain());
                   rateTaskdept.setValindex(rateDomain.getValindex());
                   irateTaskdeptService.save(rateTaskdept);


               }

           }

       }

       // 查询taskUser 看是否存在任务的初始化数据，无则插入
       QueryWrapper<RateTaskuser> rateTaskuserQueryWrapper = new QueryWrapper<>();
       rateTaskuserQueryWrapper.eq("rate_task_id",id);
       List<RateTaskuser> listuser = irateTaskuserService.list(rateTaskuserQueryWrapper);
       if(listuser.size()==0){
           QueryWrapper<RateTask> rateTaskQueryWrapper = new QueryWrapper<>();
           rateTaskQueryWrapper.eq("id",id);
           RateTask rateTask = this.getOne(rateTaskQueryWrapper);
           if(rateTask!=null&&rateTask.getRuleId()!=null&&!rateTask.getRuleId().equals("")){
               QueryWrapper<RateDomain> rateDomainQueryWrapper = new QueryWrapper<>();
               rateDomainQueryWrapper.eq("rule_id",rateTask.getRuleId());
               List<RateDomain> domainlist = irateDomainService.list(rateDomainQueryWrapper);
               for(int j=0;j<domainlist.size();j++){
                   RateDomain rateDomain =domainlist.get(j);
                   String rate_domain_id=rateDomain.getId();
                   QueryWrapper<RateKeyitem> rateKeyitemQueryWrapper = new QueryWrapper<>();
                   rateKeyitemQueryWrapper.eq("rate_domain_id",rate_domain_id);
                       List<RateKeyitem> keyitemlist = irateKeyitemService.list(rateKeyitemQueryWrapper);
                   for(int k=0;k<keyitemlist.size();k++){
                       RateKeyitem rateKeyitem =keyitemlist.get(k);

                       RateTaskuser rateTaskuser = new RateTaskuser();
                       rateTaskuser.setRateTaskId(id);
                       rateTaskuser.setRuleId(rateTask.getRuleId());
                       rateTaskuser.setRateDomainId(rateDomain.getId());
                       rateTaskuser.setRatedomain(rateDomain.getRatedomain());
                       rateTaskuser.setSondomain(rateDomain.getSondomain());
                       rateTaskuser.setValindex(rateDomain.getValindex());
                       //要素信息
                       rateTaskuser.setKeyindex(rateKeyitem.getKeyindex());
                       rateTaskuser.setKeyitem(rateKeyitem.getKeyitem());
                       rateTaskuser.setKeyitemId(rateKeyitem.getId());
                       irateTaskuserService.save(rateTaskuser);


                   }

               }

           }


       }


   }


    @Override
    public RateKeyIWriVo rateKeyItemTreeListByid(String id, String rateTaskId, String ruleId) {
        //传进来的 是 rate_taskdetail 表的 id ，需要使用 该bean 的 rule_id 规则id 。然后去 rate_domain 表中层层生成 评估要素 树图
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        String userid=sysUser.getId();
        String username=sysUser.getUsername();

        RateKeyIWriVo rateKeyIWriVo=new RateKeyIWriVo();

        List<RateDomainTreeVo> treeVos = new ArrayList<>();
        List<String> keys = new ArrayList<>();
        List<RateTbVo> rateTbVoList = new ArrayList<>();
        List<String> expandedKeys = new ArrayList<>();
        QueryWrapper<RateTaskdept> domainQueryWrapper  = new QueryWrapper<>();
        domainQueryWrapper.eq("rate_task_id",rateTaskId);
        domainQueryWrapper.apply(" (rate_user='"+userid+"' or option_user='"+username+"')");
       // domainQueryWrapper.eq("rate_user",userid);


        domainQueryWrapper.orderByAsc("valindex");
        List<RateTaskdept> domainlist = irateTaskdeptService.list(domainQueryWrapper);

        List<String> domainnameary = new ArrayList<>();
        List<String> domainary = new ArrayList<>();
        String domaintemp="";
        for(int m=0;m<domainlist.size();m++){
            RateTaskdept std=domainlist.get(m);
            String domainval=std.getRatedomain();
            if(m==0){
                domainary.add(std.getRatedomain());
                domaintemp=std.getRatedomain();
                String domName = iSysDictService.queryDictTextByKey("rate_domain",std.getRatedomain());
                domainnameary.add(domName);
            }else{
                if(domaintemp.equals(domainval)){
                    continue;
                }else{
                    domainary.add(std.getRatedomain());
                    domaintemp=std.getRatedomain();
                    String domName = iSysDictService.queryDictTextByKey("rate_domain",std.getRatedomain());
                    domainnameary.add(domName);
                }

            }
        }

        for(int n=0;n<domainary.size();n++){
            RateDomainTreeVo pRateStdTreeVo = new RateDomainTreeVo();
            pRateStdTreeVo.setTitle(domainnameary.get(n));
            pRateStdTreeVo.setTitle_bak(domainnameary.get(n));
            pRateStdTreeVo.setKey(domainary.get(n));
            expandedKeys.add(domainary.get(n));
            List<RateDomainTreeVo> childs = new ArrayList<>();

            QueryWrapper<RateTaskdept> sondomainQueryWrapper  = new QueryWrapper<>();
            sondomainQueryWrapper.eq("rate_task_id",rateTaskId);
            sondomainQueryWrapper.eq("ratedomain",domainary.get(n));
            sondomainQueryWrapper.apply(" (rate_user='"+userid+"' or option_user='"+username+"')");
            //sondomainQueryWrapper.eq("rate_user",userid);
            sondomainQueryWrapper.orderByAsc("valindex");
            List<RateTaskdept> sondomainlist = irateTaskdeptService.list(sondomainQueryWrapper);

            for(int p=0;p<sondomainlist.size();p++){
                RateDomainTreeVo child = new RateDomainTreeVo();
                RateTaskdept sondomian=sondomainlist.get(p);
                child.setTitle(sondomian.getSondomain());
                child.setTitle_bak(sondomian.getSondomain());
                child.setKey(sondomian.getRateDomainId());
                child.setLeaf(Boolean.FALSE);
                expandedKeys.add(sondomian.getRateDomainId());

                //第三级 无
                List<RateDomainTreeVo> mxchilds = new ArrayList<>();
                QueryWrapper<RateKeyitem> keyitemWrapper = new QueryWrapper<>();
                keyitemWrapper.eq("rate_domain_id",sondomian.getRateDomainId());

                keyitemWrapper.orderByAsc("keyindex");
                List<RateKeyitem> keyitemlist = irateKeyitemService.list(keyitemWrapper);

                for(int j=0;j<keyitemlist.size();j++){
                    RateKeyitem keyitemmx=keyitemlist.get(j);
                    RateDomainTreeVo keyitemchild = new RateDomainTreeVo();
                    keyitemchild.setTitle(keyitemmx.getKeyitem());
                    keyitemchild.setTitle_bak(keyitemmx.getKeyitem());
                    keyitemchild.setKey(keyitemmx.getId());
                    keyitemchild.setLeaf(Boolean.TRUE);
                    keyitemchild.setDes(keyitemmx.getKeyitem());
                    keyitemchild.setScore(keyitemmx.getKeyitemScore());
                    //需要查询下是否有选项

                    QueryWrapper<RateOpt> rateOptQueryWrapper = new QueryWrapper<>();
                    rateOptQueryWrapper.eq("task_id",rateTaskId);
                    rateOptQueryWrapper.eq("keyitem_id",keyitemmx.getId());
                    RateOpt rateOpt = irateOptService.getOne(rateOptQueryWrapper);
                    if(rateOpt!=null&&rateOpt.getStdId()!=null&&!rateOpt.getStdId().equals("")){
                        keyitemchild.setIcon("ant-design:check-outlined");
                    }
                    keys.add(keyitemmx.getId());
                    expandedKeys.add(keyitemmx.getId());

                    mxchilds.add(keyitemchild);
                    RateTbVo rateTbVo  = getRateTbVo(rateTaskId,keyitemmx.getId(),keyitemchild.getDes(),keyitemchild.getTitle_bak());
                    rateTbVoList.add(rateTbVo);
                }
                child.setChildren(mxchilds);

               // keys.add(sondomian.getRateDomainId());


                childs.add(child);


            }
            pRateStdTreeVo.setChildren(childs);
            treeVos.add(pRateStdTreeVo);
        }

        rateKeyIWriVo.setTreeList(treeVos);
        rateKeyIWriVo.setKeys(keys);
        rateKeyIWriVo.setExpandedKeys(expandedKeys);
        rateKeyIWriVo.setRecord(rateTbVoList);
        rateKeyIWriVo.setRateTaskId(rateTaskId);
        rateKeyIWriVo.setId(id);

        return rateKeyIWriVo;
    }


    @Override
    public RateKeyIWriVo rateKeyItemTreeListByidUser(String id,String rateTaskId,String ruleId) {
        //传进来的 是 rate_taskdetail 表的 id ，需要使用 该bean 的 rule_id 规则id 。然后去 rate_domain 表中层层生成 评估要素 树图
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        String userid=sysUser.getId();
        String username=sysUser.getUsername();
        List<String> keyitemidbyuser = new ArrayList<>();
        RateKeyIWriVo rateKeyIWriVo=new RateKeyIWriVo();

        List<RateDomainTreeVo> treeVos = new ArrayList<>();
        List<String> keys = new ArrayList<>();
        List<RateTbVo> rateTbVoList = new ArrayList<>();
        List<String> expandedKeys = new ArrayList<>();
        QueryWrapper<RateTaskuser> domainQueryWrapper  = new QueryWrapper<>();
        domainQueryWrapper.eq("rate_task_id",rateTaskId);
        domainQueryWrapper.apply(" (rate_user='"+userid+"' or option_user='"+username+"')");
        // domainQueryWrapper.eq("rate_user",userid);


        domainQueryWrapper.orderByAsc("valindex");
        List<RateTaskuser> domainlist = irateTaskuserService.list(domainQueryWrapper);

        List<String> domainnameary = new ArrayList<>();
        List<String> domainary = new ArrayList<>();
        String domaintemp="";
        for(int m=0;m<domainlist.size();m++){
            RateTaskuser std=domainlist.get(m);
            String domainval=std.getRatedomain();
            keyitemidbyuser.add(std.getKeyitemId());
            if(m==0){
                domainary.add(std.getRatedomain());
                domaintemp=std.getRatedomain();
                String domName = iSysDictService.queryDictTextByKey("rate_domain",std.getRatedomain());
                domainnameary.add(domName);
            }else{
                if(domaintemp.equals(domainval)){
                    continue;
                }else{
                    domainary.add(std.getRatedomain());
                    domaintemp=std.getRatedomain();
                    String domName = iSysDictService.queryDictTextByKey("rate_domain",std.getRatedomain());
                    domainnameary.add(domName);
                }

            }
        }

        for(int n=0;n<domainary.size();n++){
            //利用 domainary.get(n) 和其他条件，查询出下面的 子领域id
            List<String> zlyid = new ArrayList<>();
            List<String> zlyname = new ArrayList<>();
            QueryWrapper<RateTaskuser> zlysondomainQueryWrapper  = new QueryWrapper<>();
            //sondomainQueryWrapper.select("  distinct rate_domain_id,sondomain ");
            zlysondomainQueryWrapper.eq("rate_task_id",rateTaskId);
            zlysondomainQueryWrapper.eq("ratedomain",domainary.get(n));
            zlysondomainQueryWrapper.apply(" (rate_user='"+userid+"' or option_user='"+username+"')");
            zlysondomainQueryWrapper.orderByAsc("valindex");
            List<RateTaskuser> zlysondomainlist = irateTaskuserService.list(zlysondomainQueryWrapper);
            String zdomaintemp="";

            for(int z=0;z<zlysondomainlist.size();z++){
                RateTaskuser zly=zlysondomainlist.get(z);
                String zdomainval=zly.getRateDomainId();

                if(z==0){
                    zdomaintemp=zdomainval;
                    zlyid.add(zdomainval);
                    zlyname.add(zly.getSondomain());

                }else{
                    if(zdomaintemp.equals(zdomainval)){
                        continue;
                    }else{
                        zlyid.add(zdomainval);
                        zdomaintemp=zdomainval;
                        zlyname.add(zly.getSondomain());

                    }

                }


            }



            RateDomainTreeVo pRateStdTreeVo = new RateDomainTreeVo();
            pRateStdTreeVo.setTitle(domainnameary.get(n));
            pRateStdTreeVo.setTitle_bak(domainnameary.get(n));
            pRateStdTreeVo.setKey(domainary.get(n));
            expandedKeys.add(domainary.get(n));
            List<RateDomainTreeVo> childs = new ArrayList<>();



            for(int p=0;p<zlyid.size();p++){
                RateDomainTreeVo child = new RateDomainTreeVo();

                child.setTitle(zlyname.get(p));
                child.setTitle_bak(zlyname.get(p));
                child.setKey(zlyid.get(p));
                child.setLeaf(Boolean.FALSE);
                expandedKeys.add(zlyid.get(p));

                //第三级 无
                List<RateDomainTreeVo> mxchilds = new ArrayList<>();
                QueryWrapper<RateKeyitem> keyitemWrapper = new QueryWrapper<>();
                keyitemWrapper.eq("rate_domain_id",zlyid.get(p));

                keyitemWrapper.orderByAsc("keyindex");
                List<RateKeyitem> keyitemlist = irateKeyitemService.list(keyitemWrapper);

                for(int j=0;j<keyitemlist.size();j++){
                    RateKeyitem keyitemmx=keyitemlist.get(j);
                    boolean isok=false;
                    for(int e=0;e<keyitemidbyuser.size();e++){
                        if(keyitemmx.getId().equals(keyitemidbyuser.get(e))){
                            isok=true;
                        }
                    }
                    if(!isok){
                        continue;
                    }

                    RateDomainTreeVo keyitemchild = new RateDomainTreeVo();
                    keyitemchild.setTitle(keyitemmx.getKeyitem());
                    keyitemchild.setTitle_bak(keyitemmx.getKeyitem());
                    keyitemchild.setKey(keyitemmx.getId());
                    keyitemchild.setLeaf(Boolean.TRUE);
                    keyitemchild.setDes(keyitemmx.getKeyitem());
                    keyitemchild.setScore(keyitemmx.getKeyitemScore());
                    //需要查询下是否有选项

                    QueryWrapper<RateOpt> rateOptQueryWrapper = new QueryWrapper<>();
                    rateOptQueryWrapper.eq("task_id",rateTaskId);
                    rateOptQueryWrapper.eq("keyitem_id",keyitemmx.getId());
                    RateOpt rateOpt = irateOptService.getOne(rateOptQueryWrapper);
                    if(rateOpt!=null&&rateOpt.getStdId()!=null&&!rateOpt.getStdId().equals("")){
                        keyitemchild.setIcon("ant-design:check-outlined");
                    }
                    keys.add(keyitemmx.getId());
                    expandedKeys.add(keyitemmx.getId());

                    mxchilds.add(keyitemchild);
                    RateTbVo rateTbVo  = getRateTbVo(rateTaskId,keyitemmx.getId(),keyitemchild.getDes(),keyitemchild.getTitle_bak());
                    rateTbVoList.add(rateTbVo);
                }
                child.setChildren(mxchilds);

                // keys.add(sondomian.getRateDomainId());


                childs.add(child);


            }
            pRateStdTreeVo.setChildren(childs);
            treeVos.add(pRateStdTreeVo);
        }

        rateKeyIWriVo.setTreeList(treeVos);
        rateKeyIWriVo.setKeys(keys);
        rateKeyIWriVo.setExpandedKeys(expandedKeys);
        rateKeyIWriVo.setRecord(rateTbVoList);
        rateKeyIWriVo.setRateTaskId(rateTaskId);
        rateKeyIWriVo.setId(id);

        return rateKeyIWriVo;
    }



    private RateTbVo getRateTbVo(String rateTaskId, String keyitemId,String des,String title) {
        String valmodel="得";
        RateKeyitem keyitem = irateKeyitemService.getById(keyitemId);
        Double KeyitemScore=keyitem.getKeyitemScore();

        QueryWrapper<RateOpt> rateOptQueryWrapper = new QueryWrapper<>();
        rateOptQueryWrapper.eq("task_id",rateTaskId);
        rateOptQueryWrapper.eq("keyitem_id",keyitemId);
        RateOpt rateOpt = irateOptService.getOne(rateOptQueryWrapper);
        String xuanxiangidstr="";
        if(rateOpt!=null){
            xuanxiangidstr = rateOpt.getStdId()+",";
        }

        Double xxmax=0d;
        Double xxmin=0d;

        QueryWrapper<RateStd> rateStdQueryWrapper  = new QueryWrapper<>();
        rateStdQueryWrapper.eq("rate_keyitem_id",keyitemId);
        rateStdQueryWrapper.orderByAsc("val_index");
        List<RateStd> stdlist  = irateStdService.list(rateStdQueryWrapper);
        for(int k=0;k<stdlist.size();k++){

            RateStd rmStdVal=stdlist.get(k);
            String ids=rmStdVal.getId()+",";

            if(xuanxiangidstr.indexOf(ids)<0){

            }else{
                xxmax=xxmax+rmStdVal.getValScoremax();
                xxmin=xxmin+rmStdVal.getValScoremin();

            }


            Double valmax=rmStdVal.getValScoremax();
            if(valmax<0){
                valmodel="扣";
            }

        }

        double xxmaxjdz = Math.abs(xxmax);
        double xxminjdz = Math.abs(xxmin);
        if(xxminjdz>KeyitemScore){
            xxminjdz= KeyitemScore;
        }
        if(xxmaxjdz>KeyitemScore){
            xxmaxjdz= KeyitemScore;
        }


        //扣分（建议扣分1.5 - 2分）
        String jsKftitle=valmodel+"分（建议"+valmodel+"分"+xxminjdz+" - "+xxmaxjdz+"分）";
        String pfmodel="1";//1 得分 0扣分
        if(!valmodel.equals("得")){
            pfmodel="0";
        }


        RateTbVo rateTbVo = new RateTbVo();
        rateTbVo.setId(rateTaskId);
        rateTbVo.setRateId(keyitemId); //?
        rateTbVo.setJsKftitle(jsKftitle);
        rateTbVo.setKfmax(KeyitemScore);
        rateTbVo.setPfmodel(pfmodel);
        if(rateOpt!=null) {
            rateTbVo.setRateCheck(rateOpt.getStdId()); //选项id
            rateTbVo.setDes(rateOpt.getRemark());

            rateTbVo.setApproveremark(rateOpt.getApproveremark());
            rateTbVo.setApprovestatus(rateOpt.getApprovestatus());
            rateTbVo.setRateremark(rateOpt.getRateremark());
            rateTbVo.setKf(rateOpt.getScoreof());
        }
        if(StringUtils.hasText(des)){
            rateTbVo.setRateName(des);
        }else{
            rateTbVo.setRateName(title);
        }

        QueryWrapper<RateFile> fileQueryWrapper  = new QueryWrapper<>();
        fileQueryWrapper.eq("task_id",rateTbVo.getId());
        fileQueryWrapper.eq("keyitem_id",rateTbVo.getRateId());
        List<RateFile> list = irateFileService.list(fileQueryWrapper);
        for(int i=0;i<list.size();i++){
            RateFile files=list.get(i);
            if(files!=null&&!files.equals("")){
               int xh= files.getValIndex();
                if(xh==1){
                    rateTbVo.setFileUploadPath1(files.getFileString());
                }
                if(xh==2){
                    rateTbVo.setFileUploadPath2(files.getFileString());
                }
                if(xh==3){
                    rateTbVo.setFileUploadPath3(files.getFileString());
                }
                if(xh==4){
                    rateTbVo.setFileUploadPath4(files.getFileString());
                }
                if(xh==5){
                    rateTbVo.setFileUploadPath5(files.getFileString());
                }
                if(xh==6){
                    rateTbVo.setFileUploadPath6(files.getFileString());
                }
                if(xh==7){
                    rateTbVo.setFileUploadPath7(files.getFileString());
                }
                if(xh==8){
                    rateTbVo.setFileUploadPath8(files.getFileString());
                }
                if(xh==9){
                    rateTbVo.setFileUploadPath9(files.getFileString());
                }
                if(xh==10){
                    rateTbVo.setFileUploadPath10(files.getFileString());
                }



            }
        }
        /**
        if(!CollectionUtils.isEmpty(list)){
            List<String>  files = list.stream().map(RateFile::getFileString).collect(Collectors.toList());
            rateTbVo.setFileUploadPath(files);
        } */
        return rateTbVo;
    }


    @Override
    public void addRate(RateTbVo rateTbVo) {
        String  pfmodel = rateTbVo.getPfmodel(); //0 扣分 1 得分
        QueryWrapper<RateOpt> rateOptQueryWrapper = new QueryWrapper<>();
        rateOptQueryWrapper.eq("task_id",rateTbVo.getId());
        rateOptQueryWrapper.eq("keyitem_id",rateTbVo.getRateId());
        RateOpt rateOpt = irateOptService.getOne(rateOptQueryWrapper);
        String rm_std_main_id="";
        double countscore = 0D;
        String opids[]=rateTbVo.getRateCheck().split(",");
        for(int i=0;i<opids.length;i++){
            String opid=opids[i];
            RateStd rmStdVal = irateStdService.getById(opid);
            double score = 0D;
            if(rmStdVal!=null){
                rm_std_main_id=rmStdVal.getRateKeyitemId();
                if(rmStdVal.getValScore()!=null){
                    score = rmStdVal.getValScore();
                }/**else {
                    if(rmStdVal.getValScoremin()!=null){
                        score = rmStdVal.getValScoremin();
                    }else if(rmStdVal.getValScoremax()!=null){
                        score = rmStdVal.getValScoremax();
                    }
                } */

                countscore=countscore+score;
            }

        }
        double countscorejueduiv = Math.abs(countscore);
        double scoreget = 0D;

        RateKeyitem rmStdMain = irateKeyitemService.getById(rm_std_main_id);

        if(rmStdMain!=null){
            if(rmStdMain.getKeyitemScore()!=null){
                double maxscore = rmStdMain.getKeyitemScore();
                if(pfmodel.equals("0")) {//扣分

                    if (countscorejueduiv > maxscore) {
                        countscore = -maxscore;
                    }

                    scoreget = maxscore + countscore;

                }else{
                    //得分
                    if (countscorejueduiv > maxscore) {
                        scoreget =maxscore;
                        countscore = 0;
                    }else{
                        scoreget =countscorejueduiv;
                        countscore=maxscore-countscorejueduiv;

                    }

                }

            }
        }


        String userCode = UserUtil.getUserCode();
        RateOpt entity = new RateOpt();
        if(rateOpt!=null){
            entity.setId(rateOpt.getId());
            entity.setUpdateBy(userCode);
            entity.setStdId(rateTbVo.getRateCheck());
            entity.setRemark(rateTbVo.getDes());
            entity.setInputscore(scoreget); //得分
            entity.setInputscoreof(Math.abs(countscore)); //扣分
            irateOptService.updateById(entity);
        }else{
            entity.setCreateBy(userCode);
            entity.setUpdateBy(userCode);
            entity.setInputuser(userCode);
            entity.setKeyitemId(rateTbVo.getRateId());
            entity.setTaskId(rateTbVo.getId());
            entity.setStdId(rateTbVo.getRateCheck());
            entity.setRemark(rateTbVo.getDes());
            entity.setInputscore(scoreget); //得分
            entity.setInputscoreof(Math.abs(countscore)); //扣分
            irateOptService.save(entity);
        }
        QueryWrapper<RateFile> fileQueryWrapper  = new QueryWrapper<>();
        fileQueryWrapper.eq("task_id",rateTbVo.getId());
        fileQueryWrapper.eq("keyitem_id",rateTbVo.getRateId());
        long count = irateFileService.count(fileQueryWrapper);
        if(count>0){
            irateFileService.remove(fileQueryWrapper);
        }
        /**
        List<String> files = rateTbVo.getFileUploadPath();
        if(!CollectionUtils.isEmpty(files)) {
            for (String file : files) {
                RateFile rmRateFile = new RateFile();
                rmRateFile.setKeyitemId(rateTbVo.getRateId());
                rmRateFile.setTaskId(rateTbVo.getId());
                rmRateFile.setFileString(file);
                irateFileService.save(rmRateFile);
            }
        }
        */

        String files1 = rateTbVo.getFileUploadPath1();
        if(files1!=null&&!files1.equals("")){
            RateFile rmRateFile = new RateFile();
            rmRateFile.setKeyitemId(rateTbVo.getRateId());
            rmRateFile.setTaskId(rateTbVo.getId());
            rmRateFile.setFileString(files1);
            rmRateFile.setValIndex(1);
            irateFileService.save(rmRateFile);
        }
        String files2 = rateTbVo.getFileUploadPath2();
        if(files2!=null&&!files2.equals("")){
            RateFile rmRateFile = new RateFile();
            rmRateFile.setKeyitemId(rateTbVo.getRateId());
            rmRateFile.setTaskId(rateTbVo.getId());
            rmRateFile.setFileString(files2);
            rmRateFile.setValIndex(2);
            irateFileService.save(rmRateFile);
        }
        String files3 = rateTbVo.getFileUploadPath3();
        if(files3!=null&&!files3.equals("")){
            RateFile rmRateFile = new RateFile();
            rmRateFile.setKeyitemId(rateTbVo.getRateId());
            rmRateFile.setTaskId(rateTbVo.getId());
            rmRateFile.setFileString(files3);
            rmRateFile.setValIndex(3);
            irateFileService.save(rmRateFile);
        }
        String files4 = rateTbVo.getFileUploadPath4();
        if(files4!=null&&!files4.equals("")){
            RateFile rmRateFile = new RateFile();
            rmRateFile.setKeyitemId(rateTbVo.getRateId());
            rmRateFile.setTaskId(rateTbVo.getId());
            rmRateFile.setFileString(files4);
            rmRateFile.setValIndex(4);
            irateFileService.save(rmRateFile);
        }
        String files5 = rateTbVo.getFileUploadPath5();
        if(files5!=null&&!files5.equals("")){
            RateFile rmRateFile = new RateFile();
            rmRateFile.setKeyitemId(rateTbVo.getRateId());
            rmRateFile.setTaskId(rateTbVo.getId());
            rmRateFile.setFileString(files5);
            rmRateFile.setValIndex(5);
            irateFileService.save(rmRateFile);
        }
        String files6 = rateTbVo.getFileUploadPath6();
        if(files6!=null&&!files6.equals("")){
            RateFile rmRateFile = new RateFile();
            rmRateFile.setKeyitemId(rateTbVo.getRateId());
            rmRateFile.setTaskId(rateTbVo.getId());
            rmRateFile.setFileString(files6);
            rmRateFile.setValIndex(6);
            irateFileService.save(rmRateFile);
        }
        String files7 = rateTbVo.getFileUploadPath7();
        if(files7!=null&&!files7.equals("")){
            RateFile rmRateFile = new RateFile();
            rmRateFile.setKeyitemId(rateTbVo.getRateId());
            rmRateFile.setTaskId(rateTbVo.getId());
            rmRateFile.setFileString(files7);
            rmRateFile.setValIndex(7);
            irateFileService.save(rmRateFile);
        }
        String files8 = rateTbVo.getFileUploadPath8();
        if(files8!=null&&!files8.equals("")){
            RateFile rmRateFile = new RateFile();
            rmRateFile.setKeyitemId(rateTbVo.getRateId());
            rmRateFile.setTaskId(rateTbVo.getId());
            rmRateFile.setFileString(files8);
            rmRateFile.setValIndex(8);
            irateFileService.save(rmRateFile);
        }
        String files9 = rateTbVo.getFileUploadPath9();
        if(files9!=null&&!files9.equals("")){
            RateFile rmRateFile = new RateFile();
            rmRateFile.setKeyitemId(rateTbVo.getRateId());
            rmRateFile.setTaskId(rateTbVo.getId());
            rmRateFile.setFileString(files9);
            rmRateFile.setValIndex(9);
            irateFileService.save(rmRateFile);
        }
        String files10 = rateTbVo.getFileUploadPath10();
        if(files10!=null&&!files10.equals("")){
            RateFile rmRateFile = new RateFile();
            rmRateFile.setKeyitemId(rateTbVo.getRateId());
            rmRateFile.setTaskId(rateTbVo.getId());
            rmRateFile.setFileString(files10);
            rmRateFile.setValIndex(10);
            irateFileService.save(rmRateFile);
        }

    }



    @Override
    public void addRateOk(RateTbVo rateTbVo) {
        this.addRate(rateTbVo);
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        String userid=sysUser.getId();
        List<String> key = rateTbVo.getTbIds();
        int size = key.size();
        QueryWrapper<RateOpt> rmRateScoQueryWrapper = new QueryWrapper<>();
        rmRateScoQueryWrapper.eq("task_id",rateTbVo.getId());
        rmRateScoQueryWrapper.in("keyitem_id",key);
        long count = irateOptService.count(rmRateScoQueryWrapper);
        if(size!=count){
            throw new JeecgBootException("存在未完成的填报项，请检查填报");
        }


        QueryWrapper<RateTaskuser> rateTaskuserQueryWrapper = new QueryWrapper<>();
        rateTaskuserQueryWrapper.eq("rate_task_id",rateTbVo.getId());
        rateTaskuserQueryWrapper.in("keyitem_id",key);
        List<RateTaskuser> domainlist = irateTaskuserService.list(rateTaskuserQueryWrapper);
        //取维度
        List<String> domainary = new ArrayList<>();
        List<String> domainnameary = new ArrayList<>();
        String domainakeyitem[][]=new String[8][size];
        String domaintemp="";
        int domainindex=0;
        int keyindex=0;
        for(int m=0;m<domainlist.size();m++){
            RateTaskuser std=domainlist.get(m);
            String domainval=std.getRatedomain();
            if(m==0){
                domainary.add(std.getRatedomain());
                domaintemp=std.getRatedomain();
                String domName = iSysDictService.queryDictTextByKey("rate_domain",std.getRatedomain());
                domainnameary.add(domName);
                domainakeyitem[domainindex][keyindex]=std.getKeyitemId();
            }else{
                if(domaintemp.equals(domainval)){
                    keyindex++;
                    domainakeyitem[domainindex][keyindex]=std.getKeyitemId();
                    continue;
                }else{
                    domainary.add(std.getRatedomain());
                    domaintemp=std.getRatedomain();
                    String domName = iSysDictService.queryDictTextByKey("rate_domain",std.getRatedomain());
                    domainnameary.add(domName);
                    domainindex++;
                    keyindex=0;
                    domainakeyitem[domainindex][keyindex]=std.getKeyitemId();

                }

            }
        }
        //按维度算分
        for(int i=0;i<domainary.size();i++){
            String domain=domainary.get(i);
            rmRateScoQueryWrapper = new QueryWrapper<>();
            rmRateScoQueryWrapper.select("sum(inputscore) as inputscore , sum(inputscoreof) as inputscoreof");
            rmRateScoQueryWrapper.eq("task_id",rateTbVo.getId());
            rmRateScoQueryWrapper.in("keyitem_id",domainakeyitem[i]);
            RateOpt rmRateSco = irateOptService.getOne(rmRateScoQueryWrapper);

            double pf = rmRateSco.getInputscore();
            double pfkf = Math.abs(rmRateSco.getInputscoreof());

            boolean iskey=false;
            RateDomainscore domainscoreben = new RateDomainscore();

            QueryWrapper<RateOpt> stdidQueryWrapper = new QueryWrapper<>();
            stdidQueryWrapper.select("std_id ");
            stdidQueryWrapper.eq("task_id",rateTbVo.getId());
            stdidQueryWrapper.in("keyitem_id",domainakeyitem[i]);
            List<RateOpt> stdidlist = irateOptService.list(stdidQueryWrapper);
            for(int j=0;j<stdidlist.size();j++){
                RateOpt opt= stdidlist.get(j);
                String stdstr=opt.getStdId();
                String stdary[]=stdstr.split(",");
                for(int a=0;a<stdary.length;a++){
                    RateStd std  =irateStdService.getById(stdary[a]);
                    if(std.getLimitstatus().equals("1")){
                        iskey=true;
                        break;
                    }
                }
                if(iskey){
                    break;
                }
            }
            RateDomainscore domainscorebean=new RateDomainscore();
            QueryWrapper<RateDomainscore> rateDomaininscoreQueryWrapper = new QueryWrapper<>();
            rateDomaininscoreQueryWrapper.eq("ratedomain_id",domain);
            rateDomaininscoreQueryWrapper.eq("rate_task_id",rateTbVo.getId());
            RateDomainscore domainscore = irateDomainscoreService.getOne(rateDomaininscoreQueryWrapper);


            RateUserscore rateUserscorebean=new RateUserscore();
            QueryWrapper<RateUserscore> rateUserscoreQueryWrapper = new QueryWrapper<>();
            rateUserscoreQueryWrapper.eq("rate_task_id",rateTbVo.getId());
            rateUserscoreQueryWrapper.eq("rate_user",userid);
            RateUserscore rateUserscore = rateUserscoreService.getOne(rateUserscoreQueryWrapper);

            QueryWrapper<RateDomaininfo> rateDomaininfoQueryWrapper = new QueryWrapper<>();
            rateDomaininfoQueryWrapper.eq("ratedomain_id",domain);
            RateDomaininfo domaininfo = irateDomaininfoService.getOne(rateDomaininfoQueryWrapper);
            Double keydomainscore=domaininfo.getScoremax();

            if(iskey){
                //那就从 rate_domaininfo 取出domain的限制分。和以上的评分比较，取最大值
                if(pf>keydomainscore){
                    pf =  keydomainscore;
                }
                if(domainscore!=null&&domainscore.getInputscore()!=null){
                    domainscoreben.setInputscore(pf);
                    domainscoreben.setId(domainscore.getId());
                    domainscoreben.setInputscoreof(pfkf);
                    domainscoreben.setLimitstatus("1");
                    irateDomainscoreService.updateById(domainscoreben);
                }else{
                    domainscoreben.setRatedomainId(domain);
                    domainscoreben.setRatedomain(domainnameary.get(i));
                    domainscoreben.setRateTaskId(rateTbVo.getId());
                    domainscoreben.setInputscore(pf);
                    domainscoreben.setInputscoreof(pfkf);
                    domainscoreben.setLimitstatus("1");
                    irateDomainscoreService.save(domainscoreben);
                }


                if(rateUserscore!=null&&rateUserscore.getInputscore()!=null){
                    Double defen=rateUserscore.getInputscore();
                    Double koufen=rateUserscore.getInputscoreof();

                    rateUserscore.setInputscore(pf);
                    rateUserscore.setId(rateUserscore.getId());
                    rateUserscore.setInputscoreof(pfkf+koufen);
                    rateUserscoreService.updateById(rateUserscore);
                }else{

                    rateUserscorebean.setRateTaskId(rateTbVo.getId());
                    rateUserscorebean.setRateUser(userid);
                    rateUserscorebean.setInputscore(pf);
                    rateUserscorebean.setInputscoreof(pfkf);
                    rateUserscoreService.save(rateUserscorebean);
                }




            }else{

                if(domainscore!=null&&domainscore.getInputscore()!=null){
                    Double defen=domainscore.getInputscore();
                    Double koufen=domainscore.getInputscoreof();
                    Double pfdefen=pf+defen;
                    if(domainscore.getLimitstatus()!=null&&domainscore.getLimitstatus().equals("1")){
                        if(pfdefen>keydomainscore){
                            pfdefen =  keydomainscore;
                        }

                    }

                    domainscoreben.setInputscore(pfdefen);
                    domainscoreben.setId(domainscore.getId());
                    domainscoreben.setInputscoreof(pfkf+koufen);
                    irateDomainscoreService.updateById(domainscoreben);
                }else{
                    domainscoreben.setRatedomainId(domain);
                    domainscoreben.setRatedomain(domainnameary.get(i));
                    domainscoreben.setRateTaskId(rateTbVo.getId());
                    domainscoreben.setInputscore(pf);
                    domainscoreben.setInputscoreof(pfkf);
                    irateDomainscoreService.save(domainscoreben);
                }



                if(rateUserscore!=null&&rateUserscore.getInputscore()!=null){
                    Double defen=rateUserscore.getInputscore();
                    Double koufen=rateUserscore.getInputscoreof();

                    rateUserscore.setInputscore(pf+defen);
                    rateUserscore.setId(rateUserscore.getId());
                    rateUserscore.setInputscoreof(pfkf+koufen);
                    rateUserscoreService.updateById(rateUserscore);
                }else{

                    rateUserscorebean.setRateTaskId(rateTbVo.getId());
                    rateUserscorebean.setRateUser(userid);
                    rateUserscorebean.setInputscore(pf);
                    rateUserscorebean.setInputscoreof(pfkf);
                    rateUserscoreService.save(rateUserscorebean);
                }



            }



        }
//从维度分数里取评分到任务表
        QueryWrapper<RateDomainscore> domsoreQueryWrapper = new QueryWrapper<>();
        domsoreQueryWrapper.select("ROUND(SUM(inputscore),2) as inputscore , ROUND(SUM(inputscoreof),2) as inputscoreof ");
        domsoreQueryWrapper.eq("rate_task_id",rateTbVo.getId());
       // domsoreQueryWrapper.in("keyitem_id",key);
        RateDomainscore rmRateSco = irateDomainscoreService.getOne(domsoreQueryWrapper);
        //double totalScore = 100D;
       // double pf = totalScore-rmRateSco.getInputscore();
        double pf = rmRateSco.getInputscore();
        double pfkf = rmRateSco.getInputscoreof();
       // double pfkf = Math.abs(rmRateSco.getInputscoreof());
        RateTask rmRate = new RateTask();
        rmRate.setId(rateTbVo.getId());
        rmRate.setInputscore(pf);
        rmRate.setInputscoreof(pfkf);
        this.updateById(rmRate);
    }



    @Override
    public RateKeyIWriVo    approverateKeyItemTreeListByid(String id,String rateTaskId,String ruleId,String rate_user) {
        //传进来的 是 rate_taskdetail 表的 id ，需要使用 该bean 的 rule_id 规则id 。然后去 rate_domain 表中层层生成 评估要素 树图


        RateKeyIWriVo rateKeyIWriVo=new RateKeyIWriVo();

        List<RateDomainTreeVo> treeVos = new ArrayList<>();
        List<String> keys = new ArrayList<>();
        List<RateTbVo> rateTbVoList = new ArrayList<>();
        List<String> expandedKeys = new ArrayList<>();
        QueryWrapper<RateTaskdept> domainQueryWrapper  = new QueryWrapper<>();
        domainQueryWrapper.eq("rate_task_id",rateTaskId);
        domainQueryWrapper.eq("rate_user",rate_user);
        domainQueryWrapper.orderByAsc("valindex");
        List<RateTaskdept> domainlist = irateTaskdeptService.list(domainQueryWrapper);

        List<String> domainnameary = new ArrayList<>();
        List<String> domainary = new ArrayList<>();
        String domaintemp="";
        for(int m=0;m<domainlist.size();m++){
            RateTaskdept std=domainlist.get(m);
            String domainval=std.getRatedomain();
            if(m==0){
                domainary.add(std.getRatedomain());
                domaintemp=std.getRatedomain();
                String domName = iSysDictService.queryDictTextByKey("rate_domain",std.getRatedomain());
                domainnameary.add(domName);
            }else{
                if(domaintemp.equals(domainval)){
                    continue;
                }else{
                    domainary.add(std.getRatedomain());
                    domaintemp=std.getRatedomain();
                    String domName = iSysDictService.queryDictTextByKey("rate_domain",std.getRatedomain());
                    domainnameary.add(domName);
                }

            }
        }

        for(int n=0;n<domainary.size();n++){
            RateDomainTreeVo pRateStdTreeVo = new RateDomainTreeVo();
            pRateStdTreeVo.setTitle(domainnameary.get(n));
            pRateStdTreeVo.setTitle_bak(domainnameary.get(n));
            pRateStdTreeVo.setKey(domainary.get(n));
            expandedKeys.add(domainary.get(n));
            List<RateDomainTreeVo> childs = new ArrayList<>();

            QueryWrapper<RateTaskdept> sondomainQueryWrapper  = new QueryWrapper<>();
            sondomainQueryWrapper.eq("rate_task_id",rateTaskId);
            sondomainQueryWrapper.eq("ratedomain",domainary.get(n));
            sondomainQueryWrapper.eq("rate_user",rate_user);
            sondomainQueryWrapper.orderByAsc("valindex");
            List<RateTaskdept> sondomainlist = irateTaskdeptService.list(sondomainQueryWrapper);

            for(int p=0;p<sondomainlist.size();p++){
                RateDomainTreeVo child = new RateDomainTreeVo();
                RateTaskdept sondomian=sondomainlist.get(p);
                child.setTitle(sondomian.getSondomain());
                child.setTitle_bak(sondomian.getSondomain());
                child.setKey(sondomian.getRateDomainId());
                child.setLeaf(Boolean.FALSE);
                expandedKeys.add(sondomian.getRateDomainId());

                //第三级 无
                List<RateDomainTreeVo> mxchilds = new ArrayList<>();
                QueryWrapper<RateKeyitem> keyitemWrapper = new QueryWrapper<>();
                keyitemWrapper.eq("rate_domain_id",sondomian.getRateDomainId());

                keyitemWrapper.orderByAsc("keyindex");
                List<RateKeyitem> keyitemlist = irateKeyitemService.list(keyitemWrapper);

                for(int j=0;j<keyitemlist.size();j++){
                    RateKeyitem keyitemmx=keyitemlist.get(j);
                    RateDomainTreeVo keyitemchild = new RateDomainTreeVo();
                    keyitemchild.setTitle(keyitemmx.getKeyitem());
                    keyitemchild.setTitle_bak(keyitemmx.getKeyitem());
                    keyitemchild.setKey(keyitemmx.getId());
                    keyitemchild.setLeaf(Boolean.TRUE);
                    keyitemchild.setDes(keyitemmx.getKeyitem());
                    keyitemchild.setScore(keyitemmx.getKeyitemScore());
                    //需要查询下是否有选项

                    QueryWrapper<RateOpt> rateOptQueryWrapper = new QueryWrapper<>();
                    rateOptQueryWrapper.eq("task_id",rateTaskId);
                    rateOptQueryWrapper.eq("keyitem_id",keyitemmx.getId());
                    RateOpt rateOpt = irateOptService.getOne(rateOptQueryWrapper);
                    if(rateOpt!=null&&rateOpt.getApproveuser()!=null&&!rateOpt.getApproveuser().equals("")){
                        keyitemchild.setIcon("ant-design:check-outlined");
                    }
                    keys.add(keyitemmx.getId());
                    expandedKeys.add(keyitemmx.getId());

                    mxchilds.add(keyitemchild);
                    RateTbVo rateTbVo  = getRateTbVo(rateTaskId,keyitemmx.getId(),keyitemchild.getDes(),keyitemchild.getTitle_bak());
                    rateTbVoList.add(rateTbVo);
                }
                child.setChildren(mxchilds);

                // keys.add(sondomian.getRateDomainId());


                childs.add(child);


            }
            pRateStdTreeVo.setChildren(childs);
            treeVos.add(pRateStdTreeVo);
        }

        rateKeyIWriVo.setTreeList(treeVos);
        rateKeyIWriVo.setKeys(keys);
        rateKeyIWriVo.setExpandedKeys(expandedKeys);
        rateKeyIWriVo.setRecord(rateTbVoList);
        rateKeyIWriVo.setRateTaskId(rateTaskId);
        rateKeyIWriVo.setId(id);

        return rateKeyIWriVo;
    }


    @Override
    public RateKeyIWriVo    approverateKeyItemTreeListByidUser(String id,String rateTaskId,String ruleId,String rate_user) {
        //传进来的 是 rate_taskdetail 表的 id ，需要使用 该bean 的 rule_id 规则id 。然后去 rate_domain 表中层层生成 评估要素 树图

        List<String> keyitemidbyuser = new ArrayList<>();
        RateKeyIWriVo rateKeyIWriVo=new RateKeyIWriVo();

        List<RateDomainTreeVo> treeVos = new ArrayList<>();
        List<String> keys = new ArrayList<>();
        List<RateTbVo> rateTbVoList = new ArrayList<>();
        List<String> expandedKeys = new ArrayList<>();
        QueryWrapper<RateTaskuser> domainQueryWrapper  = new QueryWrapper<>();
        domainQueryWrapper.eq("rate_task_id",rateTaskId);
        domainQueryWrapper.eq("rate_user",rate_user);
        domainQueryWrapper.orderByAsc("valindex");
        List<RateTaskuser> domainlist = irateTaskuserService.list(domainQueryWrapper);

        List<String> domainnameary = new ArrayList<>();
        List<String> domainary = new ArrayList<>();
        String domaintemp="";
        for(int m=0;m<domainlist.size();m++){
            RateTaskuser std=domainlist.get(m);
            String domainval=std.getRatedomain();
            keyitemidbyuser.add(std.getKeyitemId());
            if(m==0){
                domainary.add(std.getRatedomain());
                domaintemp=std.getRatedomain();
                String domName = iSysDictService.queryDictTextByKey("rate_domain",std.getRatedomain());
                domainnameary.add(domName);
            }else{
                if(domaintemp.equals(domainval)){
                    continue;
                }else{
                    domainary.add(std.getRatedomain());
                    domaintemp=std.getRatedomain();
                    String domName = iSysDictService.queryDictTextByKey("rate_domain",std.getRatedomain());
                    domainnameary.add(domName);
                }

            }
        }

        for(int n=0;n<domainary.size();n++){
            //利用 domainary.get(n) 和其他条件，查询出下面的 子领域id
            List<String> zlyid = new ArrayList<>();
            List<String> zlyname = new ArrayList<>();
            QueryWrapper<RateTaskuser> zlysondomainQueryWrapper  = new QueryWrapper<>();
            //sondomainQueryWrapper.select("  distinct rate_domain_id,sondomain ");
            zlysondomainQueryWrapper.eq("rate_task_id",rateTaskId);
            zlysondomainQueryWrapper.eq("ratedomain",domainary.get(n));
            zlysondomainQueryWrapper.apply(" (rate_user='"+rate_user+"' or option_user='"+rate_user+"')");
            zlysondomainQueryWrapper.orderByAsc("valindex");
            List<RateTaskuser> zlysondomainlist = irateTaskuserService.list(zlysondomainQueryWrapper);
            String zdomaintemp="";

            for(int z=0;z<zlysondomainlist.size();z++){
                RateTaskuser zly=zlysondomainlist.get(z);
                String zdomainval=zly.getRateDomainId();

                if(z==0){
                    zdomaintemp=zdomainval;
                    zlyid.add(zdomainval);
                    zlyname.add(zly.getSondomain());

                }else{
                    if(zdomaintemp.equals(zdomainval)){
                        continue;
                    }else{
                        zlyid.add(zdomainval);
                        zdomaintemp=zdomainval;
                        zlyname.add(zly.getSondomain());

                    }

                }


            }

            RateDomainTreeVo pRateStdTreeVo = new RateDomainTreeVo();
            pRateStdTreeVo.setTitle(domainnameary.get(n));
            pRateStdTreeVo.setTitle_bak(domainnameary.get(n));
            pRateStdTreeVo.setKey(domainary.get(n));
            expandedKeys.add(domainary.get(n));
            List<RateDomainTreeVo> childs = new ArrayList<>();



            for(int p=0;p<zlyid.size();p++){
                RateDomainTreeVo child = new RateDomainTreeVo();

                child.setTitle(zlyname.get(p));
                child.setTitle_bak(zlyname.get(p));
                child.setKey(zlyid.get(p));
                child.setLeaf(Boolean.FALSE);
                expandedKeys.add(zlyid.get(p));

                //第三级 无
                List<RateDomainTreeVo> mxchilds = new ArrayList<>();
                QueryWrapper<RateKeyitem> keyitemWrapper = new QueryWrapper<>();
                keyitemWrapper.eq("rate_domain_id",zlyid.get(p));

                keyitemWrapper.orderByAsc("keyindex");
                List<RateKeyitem> keyitemlist = irateKeyitemService.list(keyitemWrapper);

                for(int j=0;j<keyitemlist.size();j++){
                    RateKeyitem keyitemmx=keyitemlist.get(j);
                    boolean isok=false;
                    for(int e=0;e<keyitemidbyuser.size();e++){
                        if(keyitemmx.getId().equals(keyitemidbyuser.get(e))){
                            isok=true;
                        }
                    }
                    if(!isok){
                        continue;
                    }
                    RateDomainTreeVo keyitemchild = new RateDomainTreeVo();
                    keyitemchild.setTitle(keyitemmx.getKeyitem());
                    keyitemchild.setTitle_bak(keyitemmx.getKeyitem());
                    keyitemchild.setKey(keyitemmx.getId());
                    keyitemchild.setLeaf(Boolean.TRUE);
                    keyitemchild.setDes(keyitemmx.getKeyitem());
                    keyitemchild.setScore(keyitemmx.getKeyitemScore());
                    //需要查询下是否有选项

                    QueryWrapper<RateOpt> rateOptQueryWrapper = new QueryWrapper<>();
                    rateOptQueryWrapper.eq("task_id",rateTaskId);
                    rateOptQueryWrapper.eq("keyitem_id",keyitemmx.getId());
                    RateOpt rateOpt = irateOptService.getOne(rateOptQueryWrapper);
                    if(rateOpt!=null&&rateOpt.getApproveuser()!=null&&!rateOpt.getApproveuser().equals("")){
                        keyitemchild.setIcon("ant-design:check-outlined");
                    }
                    keys.add(keyitemmx.getId());
                    expandedKeys.add(keyitemmx.getId());

                    mxchilds.add(keyitemchild);
                    RateTbVo rateTbVo  = getRateTbVo(rateTaskId,keyitemmx.getId(),keyitemchild.getDes(),keyitemchild.getTitle_bak());
                    rateTbVoList.add(rateTbVo);
                }
                child.setChildren(mxchilds);

                // keys.add(sondomian.getRateDomainId());


                childs.add(child);


            }
            pRateStdTreeVo.setChildren(childs);
            treeVos.add(pRateStdTreeVo);
        }

        rateKeyIWriVo.setTreeList(treeVos);
        rateKeyIWriVo.setKeys(keys);
        rateKeyIWriVo.setExpandedKeys(expandedKeys);
        rateKeyIWriVo.setRecord(rateTbVoList);
        rateKeyIWriVo.setRateTaskId(rateTaskId);
        rateKeyIWriVo.setId(id);

        return rateKeyIWriVo;
    }



    @Override
    public void addAppRate(RateTbVo rateTbVo) {



        QueryWrapper<RateOpt> rateOptQueryWrapper = new QueryWrapper<>();
        rateOptQueryWrapper.eq("task_id",rateTbVo.getId());
        rateOptQueryWrapper.eq("keyitem_id",rateTbVo.getRateId());
        RateOpt rateOpt = irateOptService.getOne(rateOptQueryWrapper);

        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

        RateOpt entity = new RateOpt();
        if(rateOpt!=null){
            entity.setId(rateOpt.getId());
            entity.setApproveuser(sysUser.getUsername());
            entity.setApprovedept(sysUser.getOrgCode());
            entity.setApproveremark(rateTbVo.getApproveremark());
            entity.setApprovestatus(rateTbVo.getApprovestatus());
            entity.setApprovetime(new Date());
            irateOptService.updateById(entity);
        }
    }

    @Override
    public IPage<RateTaskOptVo> queryDomainScoreUser(IPage<RateTaskOptVo> page, String taskid) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

        RateTaskOptVo queryVo = new RateTaskOptVo();
        queryVo.setInputuser(sysUser.getUsername());
        queryVo.setTaskid(taskid);
        IPage<RateTaskOptVo> data = this.getBaseMapper().queryDomainScoreUser(page,queryVo);
        return data;
    }


    @Override
    public RateKeyIWriVo    raterateKeyItemTreeListByid(String rateTaskId) {

        RateKeyIWriVo rateKeyIWriVo=new RateKeyIWriVo();

        List<RateDomainTreeVo> treeVos = new ArrayList<>();
        List<String> keys = new ArrayList<>();
        List<RateTbVo> rateTbVoList = new ArrayList<>();
        List<String> expandedKeys = new ArrayList<>();
        QueryWrapper<RateTaskdept> domainQueryWrapper  = new QueryWrapper<>();
        domainQueryWrapper.eq("rate_task_id",rateTaskId);
        domainQueryWrapper.orderByAsc("valindex");
        List<RateTaskdept> domainlist = irateTaskdeptService.list(domainQueryWrapper);

        List<String> domainnameary = new ArrayList<>();
        List<String> domainary = new ArrayList<>();
        String domaintemp="";
        for(int m=0;m<domainlist.size();m++){
            RateTaskdept std=domainlist.get(m);
            String domainval=std.getRatedomain();
            if(m==0){
                domainary.add(std.getRatedomain());
                domaintemp=std.getRatedomain();
                String domName = iSysDictService.queryDictTextByKey("rate_domain",std.getRatedomain());
                domainnameary.add(domName);
            }else{
                if(domaintemp.equals(domainval)){
                    continue;
                }else{
                    domainary.add(std.getRatedomain());
                    domaintemp=std.getRatedomain();
                    String domName = iSysDictService.queryDictTextByKey("rate_domain",std.getRatedomain());
                    domainnameary.add(domName);
                }

            }
        }

        for(int n=0;n<domainary.size();n++){
            RateDomainTreeVo pRateStdTreeVo = new RateDomainTreeVo();
            pRateStdTreeVo.setTitle(domainnameary.get(n));
            pRateStdTreeVo.setTitle_bak(domainnameary.get(n));
            pRateStdTreeVo.setKey(domainary.get(n));
            expandedKeys.add(domainary.get(n));
            List<RateDomainTreeVo> childs = new ArrayList<>();

            QueryWrapper<RateTaskdept> sondomainQueryWrapper  = new QueryWrapper<>();
            sondomainQueryWrapper.eq("rate_task_id",rateTaskId);
            sondomainQueryWrapper.eq("ratedomain",domainary.get(n));
            sondomainQueryWrapper.orderByAsc("valindex");
            List<RateTaskdept> sondomainlist = irateTaskdeptService.list(sondomainQueryWrapper);

            for(int p=0;p<sondomainlist.size();p++){
                RateDomainTreeVo child = new RateDomainTreeVo();
                RateTaskdept sondomian=sondomainlist.get(p);
                child.setTitle(sondomian.getSondomain());
                child.setTitle_bak(sondomian.getSondomain());
                child.setKey(sondomian.getRateDomainId());
                child.setLeaf(Boolean.FALSE);
                expandedKeys.add(sondomian.getRateDomainId());

                //第三级 无
                List<RateDomainTreeVo> mxchilds = new ArrayList<>();
                QueryWrapper<RateKeyitem> keyitemWrapper = new QueryWrapper<>();
                keyitemWrapper.eq("rate_domain_id",sondomian.getRateDomainId());

                keyitemWrapper.orderByAsc("keyindex");
                List<RateKeyitem> keyitemlist = irateKeyitemService.list(keyitemWrapper);

                for(int j=0;j<keyitemlist.size();j++){
                    RateKeyitem keyitemmx=keyitemlist.get(j);
                    RateDomainTreeVo keyitemchild = new RateDomainTreeVo();
                    keyitemchild.setTitle(keyitemmx.getKeyitem());
                    keyitemchild.setTitle_bak(keyitemmx.getKeyitem());
                    keyitemchild.setKey(keyitemmx.getId());
                    keyitemchild.setLeaf(Boolean.TRUE);
                    keyitemchild.setDes(keyitemmx.getKeyitem());
                    keyitemchild.setScore(keyitemmx.getKeyitemScore());
                    //需要查询下是否有选项

                    QueryWrapper<RateOpt> rateOptQueryWrapper = new QueryWrapper<>();
                    rateOptQueryWrapper.eq("task_id",rateTaskId);
                    rateOptQueryWrapper.eq("keyitem_id",keyitemmx.getId());
                    RateOpt rateOpt = irateOptService.getOne(rateOptQueryWrapper);
                    if(rateOpt!=null&&rateOpt.getRateuser()!=null&&!rateOpt.getRateuser().equals("")){
                        keyitemchild.setIcon("ant-design:check-outlined"); //ant-design:check-outlined
                    }
                    keys.add(keyitemmx.getId());
                    expandedKeys.add(keyitemmx.getId());

                    mxchilds.add(keyitemchild);
                    RateTbVo rateTbVo  = getRateTbVo(rateTaskId,keyitemmx.getId(),keyitemchild.getDes(),keyitemchild.getTitle_bak());
                    rateTbVoList.add(rateTbVo);
                }
                child.setChildren(mxchilds);

                // keys.add(sondomian.getRateDomainId());


                childs.add(child);


            }
            pRateStdTreeVo.setChildren(childs);
            treeVos.add(pRateStdTreeVo);
        }

        rateKeyIWriVo.setTreeList(treeVos);
        rateKeyIWriVo.setKeys(keys);
        rateKeyIWriVo.setExpandedKeys(expandedKeys);
        rateKeyIWriVo.setRecord(rateTbVoList);
        rateKeyIWriVo.setRateTaskId(rateTaskId);
        rateKeyIWriVo.setId(rateTaskId);

        return rateKeyIWriVo;
    }


    @Override
    public void addRateRate(RateTbVo rateTbVo) {
        String  pfmodel = rateTbVo.getPfmodel();
        //逻辑 得到 扣分 得分得值 根据 pfmodel 判断 得扣 。和要素的分比较。
        RateKeyitem rmStdMain = irateKeyitemService.getById(rateTbVo.getRateId());
        double keyitemScore = rmStdMain.getKeyitemScore();
        QueryWrapper<RateOpt> rateOptQueryWrapper = new QueryWrapper<>();
        rateOptQueryWrapper.eq("task_id",rateTbVo.getId());
        rateOptQueryWrapper.eq("keyitem_id",rateTbVo.getRateId());
        RateOpt rateOpt = irateOptService.getOne(rateOptQueryWrapper);

        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

        RateOpt entity = new RateOpt();
        if(rateOpt!=null){
            entity.setId(rateOpt.getId());
            entity.setRateuser(sysUser.getUsername());
            entity.setRatedept(sysUser.getOrgCode());
            entity.setRateremark(rateTbVo.getRateremark());
            entity.setRatetime(new Date());

            if(pfmodel.equals("0")){
                //扣分
                entity.setScoreof(rateTbVo.getKf());
                //得分
                double getfen=keyitemScore-rateTbVo.getKf();
                entity.setScore(getfen);

            }else{
                //得分
                entity.setScore(rateTbVo.getKf());
                //扣分
                double koufen=keyitemScore-rateTbVo.getKf();
                entity.setScoreof(koufen);
            }
            irateOptService.updateById(entity);
        }
    }


    @Override
    public IPage<RateTaskOptVo> queryDomainResScoreUser(IPage<RateTaskOptVo> page, String taskid) {
        //LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

        RateTaskOptVo queryVo = new RateTaskOptVo();
        //queryVo.setInputuser(sysUser.getUsername());
        queryVo.setTaskid(taskid);
        IPage<RateTaskOptVo> data = this.getBaseMapper().queryDomainResScoreUser(page,queryVo);
        return data;
    }

    @Override
    public List<RateTaskOptVo> queryDomainResTabScoreUser( String taskid) {
        //LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

        RateTaskOptVo queryVo = new RateTaskOptVo();
        //queryVo.setInputuser(sysUser.getUsername());
        queryVo.setTaskid(taskid);
        List<RateTaskOptVo> data = this.getBaseMapper().queryDomainResTabScoreUser(queryVo);
        return data;
    }

    @Override
    public List<RateStaiVo> queryDomainStaiList( String taskid) {

        RateStaiVo queryVo = new RateStaiVo();
        //queryVo.setInputuser(sysUser.getUsername());
        queryVo.setTaskid(taskid);
        List<RateStaiVo> data = this.getBaseMapper().queryDomainStaiList(queryVo);
        return data;
    }
    @Override
    public List<RateStaiVo> querykeyitemStaiList( String taskid) {

        RateStaiVo queryVo = new RateStaiVo();
        //queryVo.setInputuser(sysUser.getUsername());
        queryVo.setTaskid(taskid);
        List<RateStaiVo> data = this.getBaseMapper().querykeyitemStaiList(queryVo);
        return data;
    }

    @Override
    public List<RateStaiVo> queryDomainScoreList( String taskid) {

        RateStaiVo queryVo = new RateStaiVo();
        //queryVo.setInputuser(sysUser.getUsername());
        queryVo.setTaskid(taskid);
        List<RateStaiVo> data = this.getBaseMapper(). queryDomainScoreList(queryVo);
        return data;
    }
    @Override
    public List<RateStaiVo> queryDomainScoreRatioList( String taskid) {

        RateStaiVo queryVo = new RateStaiVo();
        //queryVo.setInputuser(sysUser.getUsername());
        queryVo.setTaskid(taskid);
        List<RateStaiVo> data = this.getBaseMapper(). queryDomainScoreRatioList(queryVo);
        return data;
    }
    @Override
    public List<RateStaiVo> queryDomainRatioList( String taskid) {

        RateStaiVo queryVo = new RateStaiVo();
        //queryVo.setInputuser(sysUser.getUsername());
        queryVo.setTaskid(taskid);
        List<RateStaiVo> data = this.getBaseMapper(). queryDomainRatioList(queryVo);
        return data;
    }


    @Override
    public RateKeyIWriVo    raterateKeyItemTreeViewListByid(String rateTaskId) {

        RateKeyIWriVo rateKeyIWriVo=new RateKeyIWriVo();

        List<RateDomainTreeVo> treeVos = new ArrayList<>();
        List<String> keys = new ArrayList<>();
        List<RateTbVo> rateTbVoList = new ArrayList<>();
        List<String> expandedKeys = new ArrayList<>();
        QueryWrapper<RateTaskdept> domainQueryWrapper  = new QueryWrapper<>();
        domainQueryWrapper.eq("rate_task_id",rateTaskId);
        domainQueryWrapper.orderByAsc("valindex");
        List<RateTaskdept> domainlist = irateTaskdeptService.list(domainQueryWrapper);

        List<String> domainnameary = new ArrayList<>();
        List<String> domainary = new ArrayList<>();
        String domaintemp="";
        for(int m=0;m<domainlist.size();m++){
            RateTaskdept std=domainlist.get(m);
            String domainval=std.getRatedomain();
            if(m==0){
                domainary.add(std.getRatedomain());
                domaintemp=std.getRatedomain();
                String domName = iSysDictService.queryDictTextByKey("rate_domain",std.getRatedomain());
                domainnameary.add(domName);
            }else{
                if(domaintemp.equals(domainval)){
                    continue;
                }else{
                    domainary.add(std.getRatedomain());
                    domaintemp=std.getRatedomain();
                    String domName = iSysDictService.queryDictTextByKey("rate_domain",std.getRatedomain());
                    domainnameary.add(domName);
                }

            }
        }

        for(int n=0;n<domainary.size();n++){
            RateDomainTreeVo pRateStdTreeVo = new RateDomainTreeVo();
            pRateStdTreeVo.setTitle(domainnameary.get(n));
            pRateStdTreeVo.setTitle_bak(domainnameary.get(n));
            pRateStdTreeVo.setKey(domainary.get(n));
            expandedKeys.add(domainary.get(n));
            List<RateDomainTreeVo> childs = new ArrayList<>();

            QueryWrapper<RateTaskdept> sondomainQueryWrapper  = new QueryWrapper<>();
            sondomainQueryWrapper.eq("rate_task_id",rateTaskId);
            sondomainQueryWrapper.eq("ratedomain",domainary.get(n));
            sondomainQueryWrapper.orderByAsc("valindex");
            List<RateTaskdept> sondomainlist = irateTaskdeptService.list(sondomainQueryWrapper);

            for(int p=0;p<sondomainlist.size();p++){
                RateDomainTreeVo child = new RateDomainTreeVo();
                RateTaskdept sondomian=sondomainlist.get(p);
                child.setTitle(sondomian.getSondomain());
                child.setTitle_bak(sondomian.getSondomain());
                child.setKey(sondomian.getRateDomainId());
                child.setLeaf(Boolean.FALSE);
                expandedKeys.add(sondomian.getRateDomainId());

                //第三级 无
                List<RateDomainTreeVo> mxchilds = new ArrayList<>();
                QueryWrapper<RateKeyitem> keyitemWrapper = new QueryWrapper<>();
                keyitemWrapper.eq("rate_domain_id",sondomian.getRateDomainId());

                keyitemWrapper.orderByAsc("keyindex");
                List<RateKeyitem> keyitemlist = irateKeyitemService.list(keyitemWrapper);

                for(int j=0;j<keyitemlist.size();j++){
                    RateKeyitem keyitemmx=keyitemlist.get(j);
                    RateDomainTreeVo keyitemchild = new RateDomainTreeVo();
                    keyitemchild.setTitle(keyitemmx.getKeyitem());
                    keyitemchild.setTitle_bak(keyitemmx.getKeyitem());
                    keyitemchild.setKey(keyitemmx.getId());
                    keyitemchild.setLeaf(Boolean.TRUE);
                    keyitemchild.setDes(keyitemmx.getKeyitem());
                    keyitemchild.setScore(keyitemmx.getKeyitemScore());
                    //需要查询下是否有选项

                    QueryWrapper<RateOpt> rateOptQueryWrapper = new QueryWrapper<>();
                    rateOptQueryWrapper.eq("task_id",rateTaskId);
                    rateOptQueryWrapper.eq("keyitem_id",keyitemmx.getId());
                    /**
                    RateOpt rateOpt = irateOptService.getOne(rateOptQueryWrapper);
                    if(rateOpt!=null&&rateOpt.getRateuser()!=null&&!rateOpt.getRateuser().equals("")){
                        keyitemchild.setIcon("ant-design:check-outlined"); //ant-design:check-outlined
                    }
                    */
                    keys.add(keyitemmx.getId());
                    expandedKeys.add(keyitemmx.getId());

                    mxchilds.add(keyitemchild);
                    RateTbVo rateTbVo  = getRateTbVo(rateTaskId,keyitemmx.getId(),keyitemchild.getDes(),keyitemchild.getTitle_bak());
                    rateTbVoList.add(rateTbVo);
                }
                child.setChildren(mxchilds);

                // keys.add(sondomian.getRateDomainId());


                childs.add(child);


            }
            pRateStdTreeVo.setChildren(childs);
            treeVos.add(pRateStdTreeVo);
        }

        rateKeyIWriVo.setTreeList(treeVos);
        rateKeyIWriVo.setKeys(keys);
        rateKeyIWriVo.setExpandedKeys(expandedKeys);
        rateKeyIWriVo.setRecord(rateTbVoList);
        rateKeyIWriVo.setRateTaskId(rateTaskId);
        rateKeyIWriVo.setId(rateTaskId);

        return rateKeyIWriVo;
    }


    @Override
    public List<RateStaiVo> queryTaskDomainScoreList( String taskid) {

        RateStaiVo queryVo = new RateStaiVo();

        queryVo.setTaskid(taskid);
        List<RateStaiVo> data = this.getBaseMapper(). queryTaskDomainScoreList(queryVo);
        return data;
    }


}
