package org.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 org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.system.entity.SysCategory;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.model.DepartIdModel;
import org.jeecg.modules.system.service.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.stm.rate.entity.*;
import org.stm.rate.mapper.RateTaskdeptMapper;
import org.stm.rate.service.*;
import org.springframework.stereotype.Service;
import org.apache.shiro.SecurityUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.stm.rate.vo.*;

import java.util.ArrayList;
import java.util.List;
import java.util.*;

/**
 * @Description: 任务分配
 * @Author: jeecg-boot
 * @Date:   2023-09-23
 * @Version: V1.0
 */
@Service
public class RateTaskdeptServiceImpl extends ServiceImpl<RateTaskdeptMapper, RateTaskdept> implements IRateTaskdeptService {
    @Autowired
    private IRateDomainService irateDomainService;
    @Autowired
    private ISysDictService iSysDictService;
    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ISysDepartService sysDepartService;

    @Autowired
    private ISysUserRoleService sysUserRoleService;

    @Autowired
    private ISysUserDepartService sysUserDepartService;
    @Autowired
    private IRateKeyitemService irateKeyitemService;
    @Autowired
    private IRateOptService irateOptService;
    @Autowired
    private IRateTaskuserService irateTaskuserService;



    @Override
    public IPage<RateTaskdept> pagePrepar(Page<RateTaskdept> page, QueryWrapper<RateTaskdept> queryWrapper) {
        IPage< RateTaskdept > pageList =page(page, queryWrapper);
        List<RateTaskdept> list = pageList.getRecords();
        List<RateTaskdept> voList = new ArrayList<>();
        if(CollectionUtils.isEmpty(list)){
            return null;
        }
        for(RateTaskdept entity:list) {
            RateTaskdeptVo rateTaskVo = new RateTaskdeptVo();
            BeanUtils.copyProperties(entity, rateTaskVo);
            String rateUserid= entity.getRateUser();
            SysUser userbean = sysUserService.getById(rateUserid);
            if(userbean!=null){
                rateTaskVo.setRateUserName(userbean.getRealname());
            }


            QueryWrapper<SysDepart> deptqueryWrapper = new QueryWrapper<SysDepart>();
            deptqueryWrapper.eq("org_code", entity.getRateDept());
            SysDepart sysDepart = sysDepartService.getOne(deptqueryWrapper);
            if(sysDepart!=null){
                rateTaskVo.setRateDeptName(sysDepart.getDepartName());
            }


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

    @Override
    public TaskDeptDomainVo taskDeptDomainTreeListByTaskid(String taskid) {

        TaskDeptDomainVo rateStdPfVo = new TaskDeptDomainVo();
        List<RateDomainTreeVo> treeVos = new ArrayList<>();
        List<String> keys = new ArrayList<>();
        List<String> expandedKeys = new ArrayList<>();
        QueryWrapper<RateTaskdept> domainQueryWrapper  = new QueryWrapper<>();
        domainQueryWrapper.eq("rate_task_id",taskid);
       // domainQueryWrapper.isNull("rate_user");
        domainQueryWrapper.orderByAsc("valindex");
        List<RateTaskdept> domainlist = this.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",taskid);
            sondomainQueryWrapper.eq("ratedomain",domainary.get(n));
           // sondomainQueryWrapper.isNull("rate_user");
            sondomainQueryWrapper.orderByAsc("valindex");
            List<RateTaskdept> sondomainlist = this.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.TRUE);
                expandedKeys.add(sondomian.getRateDomainId());

                //第三级 无
                keys.add(sondomian.getRateDomainId());


                childs.add(child);


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

        rateStdPfVo.setTreeList(treeVos);
        rateStdPfVo.setKeys(keys);
        rateStdPfVo.setExpandedKeys(expandedKeys);
       // rateStdPfVo.setRecord(rateTbVoList);
        return rateStdPfVo;
    }

    @Override
    public TaskDeptDomainVo taskDeptDomainTreeListByid(String id) {

        TaskDeptDomainVo rateStdPfVo = new TaskDeptDomainVo();
        List<RateDomainTreeVo> treeVos = new ArrayList<>();
        List<String> keys = new ArrayList<>();
        List<String> expandedKeys = new ArrayList<>();


        QueryWrapper<RateTaskdept> domainQueryWrapper  = new QueryWrapper<>();
        domainQueryWrapper.eq("id",id);

        List<RateTaskdept> domainlist = this.list(domainQueryWrapper);
        String taskid ="";
        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();
            taskid=std.getRateTaskId();
            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("id",id);
           // sondomainQueryWrapper.eq("rate_task_id",taskid);
            //sondomainQueryWrapper.eq("ratedomain",domainary.get(n));
           // sondomainQueryWrapper.orderByAsc("valindex");
            List<RateTaskdept> sondomainlist = this.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.TRUE);
                expandedKeys.add(sondomian.getRateDomainId());

                //第三级 无
                keys.add(sondomian.getRateDomainId());


                childs.add(child);


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

        rateStdPfVo.setTreeList(treeVos);
        rateStdPfVo.setKeys(keys);
        rateStdPfVo.setExpandedKeys(expandedKeys);
        // rateStdPfVo.setRecord(rateTbVoList);
        return rateStdPfVo;
    }
    public void updateTaskDeptInitById(RateTaskdept rateTaskdept){

        //{rateTaskId: '1705475082033250306', rateDomainId: '"1705036104612061185,1705036191018917890,1"', rateUser: '1646704435679059969', id: undefined}
        String rateDomainId=rateTaskdept.getRateDomainId();
        rateDomainId=rateDomainId.replaceAll("\"","");
        String rateDomainIdary[]=rateDomainId.split(",");
        String rateUser=rateTaskdept.getRateUser();
        SysUser sysUser = sysUserService.getById(rateUser);
       // sysUser.getOrgCode();
        LoginUser loginsysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

        for(int i=0;i<rateDomainIdary.length;i++){
            QueryWrapper<RateTaskdept> domainQueryWrapper  = new QueryWrapper<>();
            domainQueryWrapper.eq("rate_task_id",rateTaskdept.getRateTaskId());
            domainQueryWrapper.eq("rate_domain_id",rateDomainIdary[i]);

            List<RateTaskdept> domainlist = this.list(domainQueryWrapper);
            if(domainlist!=null&&domainlist.size()>0){
                RateTaskdept taskdept=domainlist.get(0);
                taskdept.setOptionUser(loginsysUser.getUsername());
                taskdept.setOptionTime(new Date());
                taskdept.setRateUser(rateUser);
                taskdept.setRateDept(sysUser.getOrgCode());

                this.updateById(taskdept);
            }
        }



    }



    @Override
    public TaskDeptDomainVo taskUserDeptDomainTreeListByTaskDeptid(String taskdeptId) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

        String username=sysUser.getUsername();
        String userid=sysUser.getId();
        RateTaskdept dept=this.getById(taskdeptId);
        String taskid=dept.getRateTaskId();

        TaskDeptDomainVo rateStdPfVo = new TaskDeptDomainVo();
        List<RateDomainTreeVo> treeVos = new ArrayList<>();
        List<String> keys = new ArrayList<>();
        List<String> expandedKeys = new ArrayList<>();
        QueryWrapper<RateTaskdept> domainQueryWrapper  = new QueryWrapper<>();
        domainQueryWrapper.eq("rate_task_id",taskid);
        domainQueryWrapper.eq("rate_user",userid);
        domainQueryWrapper.orderByAsc("valindex");
        List<RateTaskdept> domainlist = this.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",taskid);
            sondomainQueryWrapper.eq("ratedomain",domainary.get(n));
            sondomainQueryWrapper.eq("rate_user",userid);
            sondomainQueryWrapper.orderByAsc("valindex");
            List<RateTaskdept> sondomainlist = this.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.TRUE);
                expandedKeys.add(sondomian.getRateDomainId());

                //第三级 无
                // keys.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",taskid);
                     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);

                }
                child.setChildren(mxchilds);

                childs.add(child);


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

        rateStdPfVo.setTreeList(treeVos);
        rateStdPfVo.setKeys(keys);
        rateStdPfVo.setExpandedKeys(expandedKeys);
        // rateStdPfVo.setRecord(rateTbVoList);
        return rateStdPfVo;
    }

    @Override
    public TaskDeptDomainVo taskUserDeptDomainTreeListByTaskId(String taskid) {
        //why mod

        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        String username=sysUser.getUsername();
        String userid=sysUser.getId();
        //RateTaskdept dept=this.getById(taskdeptId);
        //String taskid=dept.getRateTaskId();
        List<String> zlyidbyuser = new ArrayList<>();
        List<String> keyitemidbyuser = new ArrayList<>();

        TaskDeptDomainVo rateStdPfVo = new TaskDeptDomainVo();
        List<RateDomainTreeVo> treeVos = new ArrayList<>();
        List<String> keys = new ArrayList<>();
        List<String> expandedKeys = new ArrayList<>();
        QueryWrapper<RateTaskuser> domainQueryWrapper  = new QueryWrapper<>();
        domainQueryWrapper.eq("rate_task_id",taskid);
        domainQueryWrapper.eq("option_user",userid);
      //  domainQueryWrapper.isNull("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",taskid);
            zlysondomainQueryWrapper.eq("ratedomain",domainary.get(n));
         //   zlysondomainQueryWrapper.isNull("rate_user");
            zlysondomainQueryWrapper.eq("option_user",userid);
            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<>();

            QueryWrapper<RateTaskuser> sondomainQueryWrapper  = new QueryWrapper<>();
            sondomainQueryWrapper.eq("rate_task_id",taskid);
            sondomainQueryWrapper.eq("ratedomain",domainary.get(n));
            sondomainQueryWrapper.eq("option_user",userid);
          //  sondomainQueryWrapper.isNull("rate_user");
            sondomainQueryWrapper.orderByAsc("valindex");
            List<RateTaskuser> sondomainlist = irateTaskuserService.list(sondomainQueryWrapper);

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

                //第三级 无
                // keys.add(sondomian.getRateDomainId());


                //第三级 无
                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",taskid);
                     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);

                }
                child.setChildren(mxchilds);

                childs.add(child);


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

        rateStdPfVo.setTreeList(treeVos);
        rateStdPfVo.setKeys(keys);
        rateStdPfVo.setExpandedKeys(expandedKeys);
        // rateStdPfVo.setRecord(rateTbVoList);
        return rateStdPfVo;
    }



    @Override
    public TaskDeptDomainVo taskUserDeptDomainTreeListByid(String id) {



        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        String username=sysUser.getUsername();
        String userid=sysUser.getId();
        //RateTaskdept dept=this.getById(taskdeptId);
        //String taskid=dept.getRateTaskId();
        List<String> zlyidbyuser = new ArrayList<>();
        List<String> keyitemidbyuser = new ArrayList<>();

        TaskDeptDomainVo rateStdPfVo = new TaskDeptDomainVo();
        List<RateDomainTreeVo> treeVos = new ArrayList<>();
        List<String> keys = new ArrayList<>();
        List<String> expandedKeys = new ArrayList<>();
        QueryWrapper<RateTaskuser> domainQueryWrapper  = new QueryWrapper<>();
        domainQueryWrapper.eq("id",id);

        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("id",id);

            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<>();

            QueryWrapper<RateTaskuser> sondomainQueryWrapper  = new QueryWrapper<>();
            sondomainQueryWrapper.eq("id",id);

            List<RateTaskuser> sondomainlist = irateTaskuserService.list(sondomainQueryWrapper);

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

                //第三级 无
                // keys.add(sondomian.getRateDomainId());


                //第三级 无
                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",taskid);
                     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);

                }
                child.setChildren(mxchilds);

                childs.add(child);


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

        rateStdPfVo.setTreeList(treeVos);
        rateStdPfVo.setKeys(keys);
        rateStdPfVo.setExpandedKeys(expandedKeys);
        // rateStdPfVo.setRecord(rateTbVoList);
        return rateStdPfVo;
    }


}
