package nccloud.openapi.mmbd.routing;

import com.alibaba.fastjson.JSONObject;

import java.util.*;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

import nc.bs.dao.BaseDAO;
import nc.bs.dao.DAOException;
import nc.bs.framework.common.InvocationInfoProxy;
import nc.bs.framework.common.NCLocator;
import nc.itf.bd.rt.rt0008.IRtQuery;
import nc.util.bd.bom.openapi.BillFieldsCodeToPkUtil;
import nc.util.mmf.framework.base.MMValueCheck;
import nc.util.mmf.framework.db.MMSqlBuilder;
import nc.util.mmpub.dpub.base.ValueCheckUtil;
import nc.vo.bd.rt.rt0008.entity.AggRtVO;
import nc.vo.bd.rt.rt0008.entity.AssOrgVO;
import nc.vo.bd.rt.rt0008.entity.RtActivityVO;
import nc.vo.bd.rt.rt0008.entity.RtHeadVO;
import nc.vo.bd.rt.rt0008.entity.RtItemVO;
import nc.vo.bd.rt.rt0008.entity.RtOpacVO;
import nc.vo.pub.BusinessException;
import nc.vo.pub.CircularlyAccessibleValueObject;
import nc.vo.pub.ISuperVO;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;
import nc.vo.pubapp.pattern.pub.SqlBuilder;
import nc.ws.opm.pub.utils.result.APIErrCodeEnum;
import nccloud.api.mmbd.routing.IAPIRoutingBusinessService;
import nccloud.api.rest.utils.ResultMessageUtil;
import nccloud.pubitf.mapub.costtype.IOrgQueryService;
import nccloud.ws.rest.resource.AbstractNCCRestResource;
import org.json.JSONString;

@Path("mmbd/routing")
public class RoutingResource extends AbstractNCCRestResource {
    private static String PD_RT = "pd_rt";

    private static String PD_RT_B = "pd_rt_b";

    private static String PD_RT_ASSORG = "pd_rt_assorg";

    private static String AGG = "agg";

    private static String MARKWORD = "markword";

    private static String RTO = "pd_rt_opac";

    private static String RTA = "pd_rt_activity";

    private static String CRTID = "crtid";

    public String getModule() {
        return "mmbd";
    }

    @POST
    @Path("operation/save")
    @Consumes({"application/json"})
    @Produces({"application/json"})
    public JSONString saveRouting(JSONObject para) {
        try {
            List<Map<String, Object>> paramList = (List) para.get("data");
            AggRtVO[] retVos = null;
            for (Map<String, Object> map : paramList) {
                if (!map.containsKey(PD_RT_B) || !map.containsKey(PD_RT)) {
                    return ResultMessageUtil.exceptionToJSON("传入数据异常，参数要包含表头信息和表体信息",
                            APIErrCodeEnum.BUSINESSEXCCODE.getCode());
                }
            }
            String markword = (String) getAggRtVO(paramList).get(MARKWORD);
            if (MMValueCheck.isNotEmpty(markword)) {
                String exception = markword.substring(0, markword.length() - 2);
                return ResultMessageUtil.exceptionToJSON(exception, APIErrCodeEnum.BUSINESSEXCCODE.getCode());
            }
            AggRtVO[] aggVos = (AggRtVO[]) getAggRtVO(paramList).get(AGG);
            IAPIRoutingBusinessService service = (IAPIRoutingBusinessService) NCLocator.getInstance()
                    .lookup(IAPIRoutingBusinessService.class);

            retVos = service.save(aggVos);
            return ResultMessageUtil.toJSON(retVos, "工艺路线保存成功");
        } catch (BusinessException e) {
            return ResultMessageUtil.exceptionToJSON(new BusinessException(e.getMessage(), e));
        }
    }

    @POST
    @Path("save")
    @Consumes({"application/json"})
    @Produces({"application/json"})
    public JSONString saveRouting(List<Map<String, Object>> paramList) {
        AggRtVO[] retVos = null;
        try {
            for (Map<String, Object> map : paramList) {
                if (!map.containsKey(PD_RT_B) || !map.containsKey(PD_RT)) {
                    return ResultMessageUtil.exceptionToJSON("传入数据异常，参数要包含表头信息和表体信息",
                            APIErrCodeEnum.BUSINESSEXCCODE.getCode());
                }
            }
            String markword = (String) getAggRtVO(paramList).get(MARKWORD);
            if (MMValueCheck.isNotEmpty(markword)) {
                String exception = markword.substring(0, markword.length() - 2);
                return ResultMessageUtil.exceptionToJSON(exception, APIErrCodeEnum.BUSINESSEXCCODE.getCode());
            }
            AggRtVO[] aggVos = (AggRtVO[]) getAggRtVO(paramList).get(AGG);
            IAPIRoutingBusinessService service = (IAPIRoutingBusinessService) NCLocator.getInstance()
                    .lookup(IAPIRoutingBusinessService.class);

            retVos = service.save(aggVos);
            return ResultMessageUtil.toJSON(retVos, "工艺路线保存成功");
        } catch (BusinessException e) {
            return ResultMessageUtil.exceptionToJSON(new BusinessException(e.getMessage(), e));
        }
    }

    @POST
    @Path("operation/update")
    @Consumes({"application/json"})
    @Produces({"application/json"})
    public JSONString updateRouting(JSONObject para) {
        try {
            List<Map<String, Object>> paramList = (List) para.get("data");
            AggRtVO[] retVos = null;
            for (Map<String, Object> map : paramList) {
                if (!map.containsKey(PD_RT_B) || !map.containsKey(PD_RT_ASSORG)) {
                    return ResultMessageUtil.exceptionToJSON("传入数据异常，参数要包含表头信息和表体信息",
                            APIErrCodeEnum.BUSINESSEXCCODE.getCode());
                }
            }
            String markword = (String) getAggRtVO(paramList).get(MARKWORD);
            if (MMValueCheck.isNotEmpty(markword)) {
                String exception = markword.substring(0, markword.length() - 2);
                return ResultMessageUtil.exceptionToJSON(exception, APIErrCodeEnum.BUSINESSEXCCODE.getCode());
            }
            AggRtVO[] aggVos = (AggRtVO[]) getAggRtVO(paramList).get(AGG);
            IAPIRoutingBusinessService service = (IAPIRoutingBusinessService) NCLocator.getInstance()
                    .lookup(IAPIRoutingBusinessService.class);

            retVos = service.update(aggVos);
            return ResultMessageUtil.toJSON(retVos, "工艺路线修改成功");
        } catch (BusinessException e) {
            return ResultMessageUtil.exceptionToJSON(new BusinessException(e.getMessage(), e));
        }
    }

    @POST
    @Path("update")
    @Consumes({"application/json"})
    @Produces({"application/json"})
    public JSONString updateRouting(List<Map<String, Object>> paramList) {
        try {
            AggRtVO[] retVos = null;
            for (Map<String, Object> map : paramList) {
                if (!map.containsKey(PD_RT_B) || !map.containsKey(PD_RT_ASSORG)) {
                    return ResultMessageUtil.exceptionToJSON("传入数据异常，参数要包含表头信息和表体信息",
                            APIErrCodeEnum.BUSINESSEXCCODE.getCode());
                }
            }
            String markword = (String) getAggRtVO(paramList).get(MARKWORD);
            if (MMValueCheck.isNotEmpty(markword)) {
                String exception = markword.substring(0, markword.length() - 2);
                return ResultMessageUtil.exceptionToJSON(exception, APIErrCodeEnum.BUSINESSEXCCODE.getCode());
            }
            AggRtVO[] aggVos = (AggRtVO[]) getAggRtVO(paramList).get(AGG);
            IAPIRoutingBusinessService service = (IAPIRoutingBusinessService) NCLocator.getInstance()
                    .lookup(IAPIRoutingBusinessService.class);

            retVos = service.update(aggVos);
            return ResultMessageUtil.toJSON(retVos, "工艺路线修改成功");
        } catch (BusinessException e) {
            return ResultMessageUtil.exceptionToJSON(new BusinessException(e.getMessage(), e));
        }
    }

    @POST
    @Path("query")
    @Consumes({"application/json"})
    @Produces({"application/json"})
    public JSONString queryRouting(Map<String, Object> paramMap) {
        AggRtVO[] retVos = null;
        IAPIRoutingBusinessService service = (IAPIRoutingBusinessService) NCLocator.getInstance()
                .lookup(IAPIRoutingBusinessService.class);
        try {
            retVos = service.query(paramMap);
            RtHeadVO[] hvos = null;
            if (!ValueCheckUtil.isNullORZeroLength(retVos)) {
                hvos = getHVO(retVos);
            }
            String usercode = InvocationInfoProxy.getInstance().getUserCode();
            if ("plm".equals(usercode) || "gaoning".equals(usercode)) {
                return ResultMessageUtil.toJSONUnTranslate(retVos, "工艺路线查询成功");
            }
            return ResultMessageUtil.toJSON(hvos, "工艺路线查询成功");
        } catch (BusinessException e) {
            return ResultMessageUtil.exceptionToJSON(new BusinessException(e.getMessage(), e));
        }
    }

    private RtHeadVO[] getHVO(AggRtVO[] vos) {
        RtHeadVO[] hvos = new RtHeadVO[vos.length];
        if (MMValueCheck.isNotEmpty(vos)) {
            for (int i = 0; i < hvos.length; i++) {
                hvos[i] = (RtHeadVO) vos[i].getParentVO();
            }
        }
        return hvos;
    }

    private Map<String, Object> getAggRtVO(List<Map<String, Object>> paramList) throws BusinessException {
        List<AggRtVO> aggVoList = new ArrayList<AggRtVO>();
        Map<String, AggRtVO> pkmap = new HashMap<String, AggRtVO>();
        List<String> pklist = new ArrayList<String>();
        for (Map<String, Object> map : paramList) {

            Map<String, Object> head = (Map) map.get(PD_RT);
            if (MMValueCheck.isNotEmpty(head.get(CRTID))) {
                pklist.add((String) head.get(CRTID));
            }
        }
        String[] pks = (String[]) pklist.toArray(new String[0]);
        AggRtVO[] newaggvos = null;
        if (!ValueCheckUtil.isEmpty(pks)) {
            newaggvos = queryVOByIDs(pks);
        }
        if (MMValueCheck.isNotEmpty(newaggvos)) {
            for (int i = 0; i < newaggvos.length; i++) {
                if (MMValueCheck.isNotEmpty(newaggvos[i].getPrimaryKey())) {
                    pkmap.put(newaggvos[i].getPrimaryKey(), newaggvos[i]);
                }
            }
        }
        for (Map<String, Object> map : paramList) {

            Map<String, Set<String>> tableid = new HashMap<String, Set<String>>();

            Map<String, Object> idvo = new HashMap<String, Object>();
            AggRtVO aggvo = new AggRtVO();

            Map<String, Object> head = (Map) map.get(PD_RT);

            List<Map<String, Object>> body_b = (List) map.get(PD_RT_B);

            List<Map<String, String>> body_s = (List) map.get(PD_RT_ASSORG);
            RtHeadVO hvo = new RtHeadVO();
            for (String headkey : head.keySet()) {
                hvo.setAttributeValue(headkey, head.get(headkey));
            }
            if (MMValueCheck.isEmpty(hvo.getPrimaryKey())) {
                hvo.setStatus(2);
            } else {
                hvo.setStatus(1);
            }
            aggvo.setParent(hvo);
            AggRtVO newRtvo = null;
            for (String pk : pkmap.keySet()) {
                if (pk.equals(hvo.getPrimaryKey())) {
                    newRtvo = (AggRtVO) pkmap.get(pk);
                }
            }
            if (MMValueCheck.isNotEmpty(newRtvo)) {
                Set<String> sethvo = new HashSet<String>();
                RtHeadVO newhvo = (RtHeadVO) newRtvo.getParentVO();
                sethvo.add(newhvo.getPrimaryKey());
                tableid.put(PD_RT, sethvo);
                Set<String> setitemvo = new HashSet<String>();
                RtItemVO[] newitemvos = (RtItemVO[]) newRtvo.getChildren(RtItemVO.class);
                if (MMValueCheck.isNotEmpty(newitemvos)) {
                    for (RtItemVO itemvo : newitemvos) {
                        setitemvo.add(itemvo.getPrimaryKey());
                        idvo.put(itemvo.getPrimaryKey(), itemvo);
                        Set<String> setrtovo = new HashSet<String>();
                        RtOpacVO[] newrtovos = itemvo.getRtopacitems();
                        if (MMValueCheck.isNotEmpty(newrtovos)) {
                            for (RtOpacVO newrtavo : newrtovos) {
                                setrtovo.add(newrtavo.getPrimaryKey());
                                idvo.put(newrtavo.getPrimaryKey(), newrtavo);
                            }
                        }
                        tableid.put(RTO, setrtovo);
                        Set<String> setrtavo = new HashSet<String>();
                        RtActivityVO[] newrtavos = itemvo.getRtactitems();
                        if (MMValueCheck.isNotEmpty(newrtavos)) {
                            for (RtActivityVO newrtovo : newrtavos) {
                                setrtavo.add(newrtovo.getPrimaryKey());
                                idvo.put(newrtovo.getPrimaryKey(), newrtovo);
                            }
                        }
                        tableid.put(RTA, setrtavo);
                    }
                }
                tableid.put(PD_RT_B, setitemvo);
                AssOrgVO[] newassvos = (AssOrgVO[]) newRtvo.getChildren(AssOrgVO.class);
                Set<String> setassvo = new HashSet<String>();
                if (MMValueCheck.isNotEmpty(newassvos)) {
                    for (AssOrgVO assvo : newassvos) {
                        setassvo.add(assvo.getPrimaryKey());
                        idvo.put(assvo.getPrimaryKey(), assvo);
                    }
                }
                tableid.put(PD_RT_ASSORG, setassvo);
            }

            List<RtItemVO> rtItemVoList = new ArrayList<RtItemVO>();
            Set<String> body_b_set = (Set) tableid.get(PD_RT_B);
            if (MMValueCheck.isNotEmpty(body_b)) {
                for (Map<String, Object> body_b_map : body_b) {
                    RtItemVO itemVo = new RtItemVO();
                    for (String body_b_key : body_b_map.keySet()) {
                        itemVo.setAttributeValue(body_b_key, body_b_map.get(body_b_key));
                        if (RTO.equals(body_b_key)) {
                            List<RtOpacVO> rtoVoList = new ArrayList<RtOpacVO>();

                            List<Map<String, Object>> rto = (List) body_b_map.get(body_b_key);
                            Set<String> rto_set = (Set) tableid.get(RTO);
                            if (MMValueCheck.isNotEmpty(rto)) {
                                for (Map<String, Object> rtomap : rto) {
                                    RtOpacVO rtoVo = new RtOpacVO();
                                    if (MMValueCheck.isNotEmpty(rtomap)) {
                                        for (String rtomapkey : rtomap.keySet()) {
                                            rtoVo.setAttributeValue(rtomapkey, rtomap.get(rtomapkey));
                                        }
                                        if (MMValueCheck.isNotEmpty(rtoVo.getPrimaryKey())) {
                                            rtoVo.setStatus(1);
                                        } else {
                                            rtoVo.setStatus(2);
                                        }
                                        if (MMValueCheck.isNotEmpty(rtoVo.getPrimaryKey())) {
                                            rto_set.remove(rtoVo.getPrimaryKey());
                                        }
                                        rtoVoList.add(rtoVo);
                                    }
                                }
                                if (MMValueCheck.isNotEmpty(rto_set)) {
                                    for (String rto_set_pk : rto_set) {
                                        RtOpacVO setrtovo = (RtOpacVO) idvo.get(rto_set_pk);
                                        setrtovo.setStatus(3);
                                        rtoVoList.add(setrtovo);
                                    }

                                }
                            } else if (MMValueCheck.isNotEmpty(rto_set)) {
                                for (String rto_set_pk : rto_set) {
                                    RtOpacVO setrtovo = (RtOpacVO) idvo.get(rto_set_pk);
                                    setrtovo.setStatus(3);
                                    rtoVoList.add(setrtovo);
                                }
                            }

                            itemVo.setRtopacitems((RtOpacVO[]) rtoVoList.toArray(new RtOpacVO[0]));
                        }
                        if (RTA.equals(body_b_key)) {
                            List<RtActivityVO> rtaVoList = new ArrayList<RtActivityVO>();

                            List<Map<String, Object>> rta = (List) body_b_map.get(body_b_key);
                            Set<String> rta_set = (Set) tableid.get(RTA);
                            if (MMValueCheck.isNotEmpty(rta)) {
                                for (Map<String, Object> rtamap : rta) {
                                    RtActivityVO rtoVa = new RtActivityVO();
                                    for (String rtomapkey : rtamap.keySet()) {
                                        rtoVa.setAttributeValue(rtomapkey, rtamap.get(rtomapkey));
                                    }
                                    if (MMValueCheck.isNotEmpty(rtoVa.getPrimaryKey())) {
                                        rtoVa.setStatus(1);
                                    } else {
                                        rtoVa.setStatus(2);
                                    }
                                    if (MMValueCheck.isNotEmpty(rtoVa.getPrimaryKey())) {
                                        rta_set.remove(rtoVa.getPrimaryKey());
                                    }
                                    rtaVoList.add(rtoVa);
                                }
                                if (MMValueCheck.isNotEmpty(rta_set)) {
                                    for (String rta_set_pk : rta_set) {
                                        RtActivityVO setrtavo = (RtActivityVO) idvo.get(rta_set_pk);
                                        setrtavo.setStatus(3);
                                        rtaVoList.add(setrtavo);
                                    }

                                }
                            } else if (MMValueCheck.isNotEmpty(rta_set)) {
                                for (String rta_set_pk : rta_set) {
                                    RtActivityVO setrtavo = (RtActivityVO) idvo.get(rta_set_pk);
                                    setrtavo.setStatus(3);
                                    rtaVoList.add(setrtavo);
                                }
                            }

                            itemVo.setRtactitems((RtActivityVO[]) rtaVoList.toArray(new RtActivityVO[0]));
                        }
                    }
                    if (MMValueCheck.isNotEmpty(itemVo.getPrimaryKey())) {
                        itemVo.setStatus(1);
                    } else {
                        itemVo.setStatus(2);
                    }
                    if (MMValueCheck.isNotEmpty(itemVo.getPrimaryKey())) {
                        body_b_set.remove(itemVo.getPrimaryKey());
                    }
                    rtItemVoList.add(itemVo);
                }
                if (MMValueCheck.isNotEmpty(body_b_set)) {
                    for (String body_b_setpk : body_b_set) {
                        RtItemVO setitemVo = (RtItemVO) idvo.get(body_b_setpk);
                        setitemVo.setStatus(3);
                        rtItemVoList.add(setitemVo);
                    }

                }
            } else if (MMValueCheck.isNotEmpty(body_b_set)) {
                for (String body_b_setpk : body_b_set) {
                    RtItemVO setitemVo = (RtItemVO) idvo.get(body_b_setpk);
                    setitemVo.setStatus(3);
                    rtItemVoList.add(setitemVo);
                }
            }

            aggvo.setChildren(RtItemVO.class, (ISuperVO[]) rtItemVoList.toArray(new RtItemVO[0]));

            List<AssOrgVO> assOrgVoList = new ArrayList<AssOrgVO>();
            Set<String> body_s_set = (Set) tableid.get(PD_RT_ASSORG);
            if (MMValueCheck.isNotEmpty(body_s)) {
                for (Map<String, String> body_s_map : body_s) {
                    AssOrgVO orgVo = new AssOrgVO();
                    for (String body_s_key : body_s_map.keySet()) {
                        orgVo.setAttributeValue(body_s_key, body_s_map.get(body_s_key));
                    }
                    if (MMValueCheck.isNotEmpty(orgVo.getPrimaryKey())) {
                        orgVo.setStatus(1);
                    } else {
                        orgVo.setStatus(2);
                    }
                    if (MMValueCheck.isNotEmpty(orgVo.getPrimaryKey())) {
                        body_s_set.remove(orgVo.getPrimaryKey());
                    }
                    assOrgVoList.add(orgVo);
                }
                if (MMValueCheck.isNotEmpty(body_s_set)) {
                    for (String body_s_setpk : body_s_set) {
                        AssOrgVO setorgVo = (AssOrgVO) idvo.get(body_s_setpk);
                        setorgVo.setStatus(3);
                        assOrgVoList.add(setorgVo);
                    }

                }
            } else if (MMValueCheck.isNotEmpty(body_s_set)) {
                for (String body_s_setpk : body_s_set) {
                    AssOrgVO setorgVo = (AssOrgVO) idvo.get(body_s_setpk);
                    setorgVo.setStatus(3);
                    assOrgVoList.add(setorgVo);
                }
            }

            aggvo.setChildren(AssOrgVO.class, (ISuperVO[]) assOrgVoList.toArray(new AssOrgVO[0]));
            aggVoList.add(aggvo);
        }
        AggRtVO[] aggVos = (AggRtVO[]) aggVoList.toArray(new AggRtVO[0]);
        RtHeadVO[] hvos = new RtHeadVO[aggVos.length];
        String markword = null;
        for (int i = 0; i < aggVos.length; i++) {
            hvos[i] = (RtHeadVO) aggVos[i].getParent();
            RtItemVO[] itemvos = (RtItemVO[]) aggVos[i].getChildren(RtItemVO.class);
            String exceptionMessage = getHeadInputField(hvos) + getHeadInputField(hvos);
            if (MMValueCheck.isNotEmpty(exceptionMessage)) {
                markword = "您有必输项未输," + exceptionMessage;
            }
        }
        //2025年5月8日13点41分--重新翻译工序 --start
        Map<String, Set<String>> queryParamList = getQueryParamList(aggVos);
        reTranslateCrcIdByCode(aggVos, queryParamList);
        //2025年5月8日13点41分--重新翻译工序 --end
        BillFieldsCodeToPkUtil.doTranslateVOFields(aggVos);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put(AGG, aggVos);
        map.put(MARKWORD, markword);
        return map;
    }

    private void reTranslateCrcIdByCode(AggRtVO[] aggVos, Map<String, Set<String>> queryParamMap) throws BusinessException {
        //根据map里的参数查询出对应的ID,判空,
        Set<String> bpkorgs = queryParamMap.get("bpkorg");
        Set<String> hpkorgs = queryParamMap.get("hpkorg");
        Set<String> crcids = queryParamMap.get("crcid");
        if (isNotNull(bpkorgs) && isNotNull(crcids)) {
            List<Map<String, Object>> maps = queryIDAndCode(bpkorgs, crcids);
            Map<String, String> key_crcMap = new HashMap<>();
            for (Map<String, Object> map : maps) {
                String crcid = (String) map.getOrDefault("crcid", "");
                String vrccode = (String) map.getOrDefault("vrccode", "");
                String orgcode = (String) map.getOrDefault("orgcode", "");
                key_crcMap.put(orgcode + "_" + vrccode, crcid);
            }
            for (AggRtVO aggVo : aggVos) {
                RtItemVO[] itemVos = (RtItemVO[]) aggVo.getChildren(RtItemVO.class);
                for (RtItemVO itemVo : itemVos) {
                    String crcid = itemVo.getCrcid();
                    String orgcode = itemVo.getPk_org();
                    String crcid_new = key_crcMap.get(orgcode + "_" + crcid);
                    if (crcid_new != null) {
                        itemVo.setCrcid(crcid_new);
                    }
                }
            }
        } else if (!isNotNull(bpkorgs) && isNotNull(crcids) && isNotNull(hpkorgs)) {
            //保险
            List<Map<String, Object>> maps = queryIDAndCode(hpkorgs, crcids);
            Map<String, String> key_crcMap = new HashMap<>();
            for (Map<String, Object> map : maps) {
                String crcid = (String) map.getOrDefault("crcid", "");
                String vrccode = (String) map.getOrDefault("vrccode", "");
                String orgcode = (String) map.getOrDefault("orgcode", "");
                key_crcMap.put(orgcode + "_" + vrccode, crcid);
            }
            for (AggRtVO aggVo : aggVos) {
                RtHeadVO parentVO = (RtHeadVO) aggVo.getParent();
                RtItemVO[] itemVos = (RtItemVO[]) aggVo.getChildren(RtItemVO.class);
                String orgcode = parentVO.getPk_org();
                for (RtItemVO itemVo : itemVos) {
                    String crcid = itemVo.getCrcid();
                    String crcid_new = key_crcMap.get(orgcode + "_" + crcid);
                    if (crcid_new != null) {
                        itemVo.setCrcid(crcid_new);
                    }
                }
            }
        }
    }

    private List<Map<String, Object>> queryIDAndCode(Set<String> bpkorgs, Set<String> crcids) throws BusinessException {
        SqlBuilder sql = new SqlBuilder();
        sql.append(" SELECT rc.crcid,rc.vrccode,orgs.code orgcode FROM pd_rc rc ");
        sql.append(" LEFT JOIN org_orgs orgs ON rc.PK_ORG =orgs.PK_ORG ");
        sql.append(" WHERE ");
        sql.append(" rc.dr=0 and orgs.dr=0 and ");
        sql.append(" rc.vrccode ", crcids.toArray(new String[0]));
        sql.append(" AND ");
        sql.append(" orgs.CODE ", bpkorgs.toArray(new String[0]));
        List<Map<String, Object>> remain = (List<Map<String, Object>>) new BaseDAO().executeQuery(sql.toString(), new nc.jdbc.framework.processor.MapListProcessor());
        if (remain == null || remain.size() < 1) {
            throw new BusinessException("工序查询为空：sql【" + sql.toString() + "】");
        }
        return remain;
    }

    private boolean isNotNull(Set<String> set) {
        return set == null || set.size() < 1 ? false : true;
    }

    public Map<String, Set<String>> getQueryParamList(AggRtVO[] aggVos) {
        Map<String, Set<String>> queryParamMap = new HashMap<>();
        Set<String> bOrgList = new HashSet<>();
        Set<String> hOrgList = new HashSet<>();
        Set<String> crcIdList = new HashSet<>();
        for (AggRtVO aggVo : aggVos) {
            RtHeadVO hvo = (RtHeadVO) aggVo.getParentVO();
            if(hvo.getStatus()==3){
                continue;
            }
            hOrgList.add(hvo.getPk_org());
            RtItemVO[] itemVos = (RtItemVO[]) aggVo.getChildren(RtItemVO.class);
            if (MMValueCheck.isNotEmpty(itemVos)) {
                for (RtItemVO itemVo : itemVos) {
                    if (itemVo.getStatus()==3){
                        continue;
                    }
                    String pkOrg = itemVo.getPk_org();
                    String crcId = itemVo.getCrcid();
                    if (pkOrg != null) {
                        bOrgList.add(pkOrg);
                    }
                    if (crcId != null) {
                        crcIdList.add(crcId);
                    }
                }
            }
        }
        queryParamMap.put("bpkorg", bOrgList);
        queryParamMap.put("hpkorg", hOrgList);
        queryParamMap.put("crcid", crcIdList);
        return queryParamMap;
    }

    private String getHeadInputField(RtHeadVO[] hvos) {
        String retstr = "";
        for (RtHeadVO hvo : hvos) {
            if (hvo.getPk_org() == null || MMValueCheck.isEmpty(hvo.getPk_org())) {
                retstr = retstr + "pk_org,";
            }
            if (hvo.getCmaterialid() == null || MMValueCheck.isEmpty(hvo.getCmaterialid())) {
                retstr = retstr + "cmaterialid,";
            }
            if (hvo.getNastnum() == null || MMValueCheck.isEmpty(hvo.getNastnum())) {
                retstr = retstr + "nastnum,";
            }
            if (hvo.getVersion() == null || MMValueCheck.isEmpty(hvo.getVersion())) {
                retstr = retstr + "version,";
            }
            if (hvo.getFversiontype() == null || MMValueCheck.isEmpty(hvo.getFversiontype())) {
                retstr = retstr + "fversiontype,";
            }
        }
        return retstr;
    }

    private String getBodyInputField(RtItemVO[] bvos) {
        String retstr = "";
        for (RtItemVO bvo : bvos) {
            if (bvo.getVprocessno() == null || MMValueCheck.isEmpty(bvo.getVprocessno())) {
                retstr = retstr + "vprocessno,";
            }
            if (bvo.getProcessmovetype() == null || MMValueCheck.isEmpty(bvo.getProcessmovetype())) {
                retstr = retstr + "processmovetype,";
            }
            if (bvo.getDeffectdate() == null || MMValueCheck.isEmpty(bvo.getDeffectdate())) {
                retstr = retstr + "deffectdate,";
            }
            if (bvo.getDinvaliddate() == null || MMValueCheck.isEmpty(bvo.getDinvaliddate())) {
                retstr = retstr + "dinvaliddate,";
            }
            if (bvo.getBmainprocess() == null || MMValueCheck.isEmpty(bvo.getBmainprocess())) {
                retstr = retstr + "bmainprocess,";
            }
        }
        return retstr;
    }

    private AggRtVO[] queryVOByIDs(String[] pks) {
        AggRtVO[] aggvos = null;
        IRtQuery rtquery = (IRtQuery) NCLocator.getInstance().lookup(IRtQuery.class);
        MMSqlBuilder sql = new MMSqlBuilder();
        sql.append(" crtid ", pks);
        try {
            aggvos = rtquery.queryRtByWhereSql(sql.toString());
        } catch (BusinessException e) {
            ExceptionUtils.wrappException(e);
        }
        return aggvos;
    }
}
