package nccloud.api.mmpac.productreport;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import nc.bs.cmp.util.SqlUtils;
import nc.bs.dao.BaseDAO;
import nc.bs.framework.common.InvocationInfoProxy;
import nc.bs.framework.common.NCLocator;
import nc.bs.scmpub.query.SCMBillQuery;
import nc.itf.mmpac.pmo.pac0002.IPMOBusinessService;
import nc.itf.mmpac.pmo.pac0002.IPMOQueryService;
import nc.itf.mmpac.wr.IWrBusinessService;
import nc.itf.mmpac.wr.IWrMaintainService;
import nc.itf.mmpac.wr.IWrQueryService;
import nc.itf.uap.pf.IplatFormEntry;
import nc.jdbc.framework.processor.BeanListProcessor;
import nc.jdbc.framework.processor.ColumnProcessor;
import nc.jdbc.framework.processor.MapListProcessor;
import nc.pubitf.ic.m46.m55a4.IProductInPubServiceFor55A4;
import nc.util.mmf.busi.service.PFPubService;
import nc.util.mmf.framework.base.MMValueCheck;
import nc.util.mmpac.wr.WrTransTypeUtil;
import nc.util.mmpac.wr.vochange.WrBusiVOToChangeVO;
import nc.vo.am.common.util.StringUtils;
import nc.vo.ic.m46.entity.FinProdInBodyVO;
import nc.vo.ic.m46.entity.FinProdInHeadVO;
import nc.vo.ic.m46.entity.FinProdInVO;
import nc.vo.ic.pub.define.ICPubMetaNameConst;
import nc.vo.ml.NCLangRes4VoTransl;
import nc.vo.mmpac.pmo.pac0002.entity.PMOAggVO;
import nc.vo.mmpac.pmo.pac0002.entity.PMOItemVO;
import nc.vo.mmpac.wr.entity.*;
import nc.vo.pub.BusinessException;
import nc.vo.pub.VOStatus;
import nc.vo.pub.lang.UFDate;
import nc.vo.pub.lang.UFDateTime;
import nc.vo.pub.lang.UFDouble;
import nc.vo.pubapp.AppContext;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;
import nccloud.api.ic.m46.IAPIProductInMaitain;
import nccloud.api.rest.utils.NCCRestUtils;
import nccloud.api.rest.utils.ResultMessageUtil;
import nccloud.dto.mmpub.script.entity.MMScriptResultDTO;
import nccloud.openapi.scmpub.util.BillFieldsCodeToPkUtil;
import nccloud.openapi.scmpub.util.CallReturnBuildUtil;
import nccloud.pubitf.mmpub.commit.service.IBatchRunScriptService;
import nccloud.pubitf.riart.pflow.CloudPFlowContext;
import nccloud.ws.rest.resource.AbstractNCCRestResource;
import org.apache.commons.beanutils.BeanUtils;
import org.json.JSONString;

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

/**
 * 生产报告和流程生产订单API接口
 * 2005适配2312
 *
 * @author mzr
 * @date 2025/4/7
 */
@Path("/mmpac/productReport")
public class ProductReportResources extends AbstractNCCRestResource {
    @Override
    public String getModule() {
        return "mmpac";
    }

    private static String BODYTABLE = "ic_finprodin_b";
    private static String HEADTABLE = "ic_finprodin_h";
    public BaseDAO dao = null;

    public BaseDAO getDao() {
        if (dao == null) {
            dao = new BaseDAO();
        }
        return dao;
    }

    IWrQueryService query = (IWrQueryService) NCLocator.getInstance().lookup(IWrQueryService.class);
    IWrBusinessService service = (IWrBusinessService) NCLocator.getInstance().lookup(IWrBusinessService.class);
    IplatFormEntry iIplatFormEntry = NCLocator.getInstance().lookup(IplatFormEntry.class);

    /**
     * 生产报告新增
     */
    @POST
    @Path("/addProductReport")
    @Consumes({"application/json"})
    @Produces({"application/json"})
    public JSONString addProductReportInfo(JSONString json1) throws BusinessException {
        JSONObject returnJson = new JSONObject();

        JSONObject jObject = JSON.parseObject(json1.toJSONString());
        if (jObject == null) {
            return ResultMessageUtil.exceptionToJSON(new BusinessException("参数不能为空"));
        }
        JSONObject headObj = (JSONObject) jObject.get("head");
        JSONArray bodyObj = (JSONArray) jObject.get("body");
        if (headObj == null || bodyObj == null || bodyObj.size() == 0) {
            return ResultMessageUtil.exceptionToJSON(new BusinessException("参数head/body不能为空"));
        }
        // 校验数据合法性
        Map<String, Object> data = validateData(headObj, bodyObj);
        if (!"1".equals(data.get("code"))) {
            return ResultMessageUtil.exceptionToJSON(new BusinessException((String) data.get("data")));
        }
        AggWrVO vo = (AggWrVO) data.get("data");
        vo.getParent().setStatus(2);
        AggWrVO[] clientvos = new AggWrVO[]{vo};
        AggWrVO[] vos = (AggWrVO[]) clientvos.clone();
        InvocationInfoProxy.getInstance().setBizDateTime(vo.getParentVO().getDbilldate().getMillis());
        IWrMaintainService service = (IWrMaintainService) NCLocator.getInstance().lookup(IWrMaintainService.class);
        try {
            vos = service.insert(vos);
            // 更新单据日期
//            for (int i = 0; i < vos.length; i++) {
//            	String sql=" update mm_wr set Creationtime='"+clientvos[i].getParentVO().getCreationtime()+"' , Dmakedate='"+clientvos[i].getParentVO().getDmakedate()+"' where pk_wr='"+vos[i].getParentVO().getPk_wr()+"' ";
//            	new BaseDAO().executeUpdate(sql);
//			}


        } catch (Exception var13) {
            ExceptionUtils.wrappBusinessException(var13.getMessage());
            return ResultMessageUtil.exceptionToJSON(new BusinessException("新增失败，" + var13.getMessage()));
        }

        returnJson.put("message", "成功");
        returnJson.put("data", vos[0]);
        return ResultMessageUtil.toJSON(returnJson);
    }

    /**
     * 生产报告审核
     */
    @POST
    @Path("/commitProductReport")
    @Consumes({"application/json"})
    @Produces({"application/json"})
    public JSONString commitProductReportInfo(JSONString json1) throws BusinessException {
        JSONObject jObject = JSON.parseObject(json1.toJSONString());
        if (jObject == null) {
            return ResultMessageUtil.exceptionToJSON(new BusinessException("失败！参数不能为空"));
        }
        String id = (String) jObject.get("id");
        String ts = (String) jObject.get("ts");
//        String taudittime = (String)jObject.get("taudittime");

        if (StringUtils.isBlank(id) || StringUtils.isBlank(ts)) {
            return ResultMessageUtil.exceptionToJSON(new BusinessException("失败！参数id/ts不能为空"));
        }

        IWrQueryService query = (IWrQueryService) NCLocator.getInstance().lookup(IWrQueryService.class);
        String[] ids = new String[1];
        ids[0] = id;

        AggWrVO[] aggvos = query.queryByPk(ids);
        if (null == aggvos || aggvos.length == 0) {
            return ResultMessageUtil.exceptionToJSON(new BusinessException("失败！未找到对应单据，请检查id值"));
        }

        InvocationInfoProxy.getInstance().setBizDateTime(new UFDate(ts).getMillis());
        for (int i = 0; i < aggvos.length; i++) {
            aggvos[i].getParentVO().setTaudittime(new UFDate(ts));
        }

        Integer fbillstatus = aggvos[0].getParentVO().getFbillstatus();
        if (fbillstatus != null && fbillstatus != 1) {
            return ResultMessageUtil.exceptionToJSON(new BusinessException("失败！自由态的单据才可以审批"));
        }

        try {
            CloudPFlowContext context = new CloudPFlowContext();
            context.setBillType("55A4");
            context.setActionName("SAVE");

            context.setBillVos(aggvos);
            IBatchRunScriptService batchRunScriptService = NCLocator.getInstance().lookup(IBatchRunScriptService.class);
            MMScriptResultDTO obj = batchRunScriptService.runBacth(context, AggWrVO.class);
        } catch (Exception e) {
            return ResultMessageUtil.exceptionToJSON(new BusinessException("失败！" + e.getMessage()));
        }

        JSONObject returnJson = new JSONObject();
        returnJson.put("message", id + " 审批成功");
        return ResultMessageUtil.toJSON(returnJson);
    }


    /**
     * 产成品入库
     */
    @POST
    @Path("/addrefbill")
    @Consumes({"application/json"})
    @Produces({"application/json"})
    public JSONString addRefbill(List<Map<String, Object>> paramList) throws BusinessException {
        List<FinProdInVO> aggVOList = new ArrayList<FinProdInVO>();
        try {
            String scddid = "";

            for (Map<String, Object> map : paramList) {
                // 判断json格式是否合法
                if (!map.containsKey(HEADTABLE) || !map.containsKey(BODYTABLE)) {
                    return ResultMessageUtil.exceptionToJSON(new BusinessException("表头表体数据为空"));
                }
                // 将json转换为产成品入库VO
                Map<String, String> headInfo = (Map<String, String>) map.get(HEADTABLE);
                List<Map<String, String>> bodyInfo = (List<Map<String, String>>) map.get(BODYTABLE);
                FinProdInVO aggvo = new FinProdInVO();
                FinProdInHeadVO hvo = new FinProdInHeadVO();
                hvo.setAttributeValue(ICPubMetaNameConst.PK_GROUP, AppContext.getInstance().getPkGroup());
                for (String headkey : headInfo.keySet()) {
                    hvo.setAttributeValue(headkey, headInfo.get(headkey));
                }

                InvocationInfoProxy.getInstance().setBizDateTime(hvo.getDbilldate().getMillis());
                aggvo.setParentVO(hvo);
                List<FinProdInBodyVO> bvoList = new ArrayList<>();
                for (Map<String, String> bodyMap : bodyInfo) {
                    FinProdInBodyVO bvo = new FinProdInBodyVO();
                    for (String bodykey : bodyMap.keySet()) {
                        bvo.setAttributeValue(bodykey, bodyMap.get(bodykey));
                    }
                    bvo.setPk_batchcode(bvo.getVbatchcode());// 将批次号注入进去走翻译

                    bvoList.add(bvo);
                }
                aggvo.setChildrenVO(bvoList.toArray(new FinProdInBodyVO[bvoList.size()]));
                aggVOList.add(aggvo);
            }
            FinProdInVO[] proinVOs = aggVOList.toArray(new FinProdInVO[aggVOList.size()]);
            // 将产成品入库按照编码翻译
            BillFieldsCodeToPkUtil.doTranslateVOFields(proinVOs);

            List<FinProdInVO> lastList = new ArrayList<FinProdInVO>();
            for (FinProdInVO proinVO : proinVOs) {
                FinProdInVO lastAgg = new FinProdInVO();
                FinProdInHeadVO headVO = (FinProdInHeadVO) proinVO.getParentVO();// 表头
                FinProdInBodyVO[] proinBVOs = proinVO.getBodys();// 表体
                // 1.根据产成品入库单表体查询出上游来源单据(生产报告)
                AggWrVO[] wrAggs = this.getWrByFinProdBodys(proinBVOs);

                // 2.模仿前台合格入库，构造生产报告质量信息WrQualityVO
                this.fillquality(wrAggs);

                // 3.将产成品入库的部分进行过滤
                AggWrVO[] aggWrVOs = service.prodInFilter(wrAggs);


                if (aggWrVOs == null || aggWrVOs[0].getChildrenVO() == null) {
                    throw new BusinessException("未查到对应的生产报告，或生产报告已生成入库单");
                }
                // 4.调用转换规则
                FinProdInVO[] transVOs = this.wrChange46(aggWrVOs, headVO);
                // 获取转换后的单据表头信息
                FinProdInHeadVO tranhead = transVOs[0].getHead();
                // 获取转换后的单据表体信息
                Map<String, FinProdInBodyVO> tranbMap = new HashMap<String, FinProdInBodyVO>();
                for (FinProdInVO tranAggs : transVOs) {
                    FinProdInBodyVO[] items = (FinProdInBodyVO[]) tranAggs.getChildrenVO();
                    for (FinProdInBodyVO item : items) {
                        tranbMap.put(item.getVsourcerowno(), item);
                    }
                }
                // 3.处理表头
                tranhead.setPk_org(headVO.getPk_org());
                tranhead.setDbilldate(headVO.getDbilldate());
                tranhead.setCwarehouseid(headVO.getCwarehouseid());
                tranhead.setVtrantypecode(headVO.getVtrantypecode());
                String ctrantypeid = getValueByCondtion("bd_billtype", "  pk_billtypeid ", "  istransaction = 'Y'  and nvl ( islock, 'N' ) = 'N' and parentbilltype = '46' and pk_group = '"
                        + InvocationInfoProxy.getInstance().getGroupId() + "' and  pk_billtypecode = '" + headVO.getVtrantypecode() + "' ");
                tranhead.setCtrantypeid(ctrantypeid);
                tranhead.setCbizid(headVO.getCbizid());

                String Cdptid = getValueByCondtion("org_dept_v", "  pk_dept ", " pk_vid='" + tranhead.getCdptvid() + "' ");
                if (Cdptid.equals("")) {
                    throw new BusinessException("生产部门不能为空");
                }

                tranhead.setCdptid(Cdptid);
                tranhead.setVnote(headVO.getVnote());
                tranhead.setVdef12(headVO.getVdef12());
                tranhead.setStatus(VOStatus.NEW);
                // 4.处理表体
                Integer count = 10;
                FinProdInBodyVO[] bodys = new FinProdInBodyVO[proinBVOs.length];
                for (int i = 0; i < proinBVOs.length; i++) {
                    FinProdInBodyVO proinBVO = proinBVOs[i];
                    String vsourcerowno = proinBVO.getVsourcerowno();
                    FinProdInBodyVO transbody = tranbMap.get(vsourcerowno);
                    transbody.setCmaterialvid(proinBVO.getCmaterialvid());
                    transbody.setNnum(proinBVO.getNnum());
                    transbody.setNassistnum(proinBVO.getNnum());
                    transbody.setVbatchcode(proinBVO.getVbatchcode());
                    transbody.setPk_batchcode(proinBVO.getPk_batchcode());
                    transbody.setDbizdate(proinBVO.getDvalidate());
                    transbody.setDproducedate(proinBVO.getDproducedate());
                    transbody.setVfree1(proinBVO.getVfree1());
                    transbody.setVfree2(proinBVO.getVfree2());
                    transbody.setVfree3(proinBVO.getVfree3());
                    transbody.setVfree4(proinBVO.getVfree4());
                    transbody.setVfree5(proinBVO.getVfree5());
                    transbody.setVfree6(proinBVO.getVfree6());
                    transbody.setVfree7(proinBVO.getVfree7());
                    transbody.setCrowno(String.valueOf(count));
                    transbody.setCsourcetype(proinBVO.getCsourcetype());
                    transbody.setCsourcebillhid(proinBVO.getCsourcebillhid());
                    transbody.setVnotebody(proinBVO.getVnotebody());
                    transbody.setVproductbatch(proinBVO.getVproductbatch());
                    scddid = proinBVO.getVproductbatch();
                    count = count + 10;
                    transbody.setStatus(VOStatus.NEW);
                    bodys[i] = transbody;
                }
                lastAgg.setParentVO(tranhead);
                lastAgg.setChildrenVO(bodys);
                lastList.add(lastAgg);
            }
            IProductInPubServiceFor55A4 productService = NCLocator.getInstance().lookup(IProductInPubServiceFor55A4.class);
            FinProdInVO[] results = productService.pushSave(lastList.toArray(new FinProdInVO[0]));
            List<String> hpklist = new ArrayList<String>();

            for (FinProdInVO result : results) {
                String hpk = result.getParentVO().getPrimaryKey();
                hpklist.add(hpk);
            }
            // 更新产成品入库关闭时间


            // 自动签字代码
            SCMBillQuery<FinProdInVO> queryTool = new SCMBillQuery<FinProdInVO>(FinProdInVO.class);
            FinProdInVO[] prodInVOs = queryTool.queryVOByIDs(hpklist.toArray(new String[0]));
            if (prodInVOs.length == 0) {
                return NCCRestUtils.toJSONString(CallReturnBuildUtil.buildHeadBodyResult());
            } else {
                for (int i = 0; i < prodInVOs.length; i++) {

                    FinProdInBodyVO[] bvos = prodInVOs[i].getBodys();
                    for (int j = 0; j < bvos.length; j++) {
                        String scddh = bvos[j].getCfirstbillbid();
                        String pk_org = prodInVOs[i].getHead().getPk_org();
                        UFDate dbildate = prodInVOs[i].getHead().getDbilldate();
                        String sql = " update mm_mo set tmoclosedtime='" + dbildate + "' where  cmoid='" + scddh + "' and pk_org='" + pk_org + "' and dr=0 ";
                        getDao().executeUpdate(sql);
                    }

                }
                prodInVOs = nc.bs.framework.common.NCLocator.getInstance().lookup(IAPIProductInMaitain.class).sign(prodInVOs);
            }
            return ResultMessageUtil.toJSON(proinVOs, nc.vo.ml.NCLangRes4VoTransl.getNCLangRes().getStrByID("4008027_0",
                    "04008027-0434")/* @res "产成品入库单保存成功" */);
        } catch (Exception e) {
            return ResultMessageUtil.exceptionToJSON(new BusinessException("失败！" + e.getMessage()));
        }
    }


    /**
     * 生产报告提交
     */
    @POST
    @Path("/commitPmoReport")
    @Consumes({"application/json"})
    @Produces({"application/json"})
    public JSONString commitPmoReport(JSONString json1) throws BusinessException {
        if (json1 == null) {
            return ResultMessageUtil.toJSON("失败！参数不能为空");
        }

        JSONArray jObject = JSON.parseArray(json1.toString());
        if (jObject == null) {
            return ResultMessageUtil.exceptionToJSON(new BusinessException("失败！参数不能为空"));
        }
        ArrayList<Map<String, String>> map = new ArrayList<Map<String, String>>();


        for (int i = 0; i < jObject.size(); i++) {

            Map<String, String> pkmap = new HashMap<String, String>();


            try {
                JSONObject ssjson = jObject.getJSONObject(i);
                String id = ssjson.getString("id");
                pkmap.put("id", id);
                String usercode = ssjson.getString("usercode");
                if (StringUtils.isBlank(id) || StringUtils.isBlank(usercode)) {
                    throw new BusinessException("失败！参数id/usercode不能为空");
                }
                String userid = getPk("sm_user", "cuserid", "user_code", usercode, "");
                if (userid.equals("")) {
                    throw new BusinessException("审核人不存在" + usercode);
                }
                InvocationInfoProxy.getInstance().setUserId(userid);

                IPMOQueryService query = (IPMOQueryService) NCLocator.getInstance().lookup(IPMOQueryService.class);
                String[] ids = new String[1];
                ids[0] = id;

                PMOAggVO[] rvo = query.queryByPks(ids);
                if (null == rvo || rvo.length == 0) {
                    throw new BusinessException("失败！未找到对应单据，请检查id值");
                }
                Integer fbillstatus = rvo[0].getParentVO().getFbillstatus();
                if (fbillstatus != null && fbillstatus != 0) {
                    throw new BusinessException("失败！自由态的单据才可以审批");
                }
                CloudPFlowContext context = new CloudPFlowContext();
                context.setBillType("55A2");
                context.setActionName("SAVE");
                context.setBillVos(rvo);
                IBatchRunScriptService batchRunScriptService = NCLocator.getInstance().lookup(IBatchRunScriptService.class);
                batchRunScriptService.runBacth(context, PMOAggVO.class);
                pkmap.put("status", "0");
                map.add(pkmap);
            } catch (BusinessException e) {
                pkmap.put("errmsg", e.getMessage());
                pkmap.put("status", "1");
                map.add(pkmap);
            }

        }


        return ResultMessageUtil.toJSON(map);
    }


    /**
     * 流程生产订单行投放
     */
    @POST
    @Path("/putRowPmoReport")
    @Consumes({"application/json"})
    @Produces({"application/json"})
    public JSONString putRowPmoReport(JSONString json1) throws BusinessException {
        if (json1 == null) {
            return ResultMessageUtil.toJSON("失败！参数不能为空");
        }

        JSONArray jObject = JSON.parseArray(json1.toString());
        if (jObject == null) {
            return ResultMessageUtil.exceptionToJSON(new BusinessException("失败！参数不能为空"));
        }
        ArrayList<Map<String, String>> map = new ArrayList<Map<String, String>>();


        for (int i = 0; i < jObject.size(); i++) {

            Map<String, String> pkmap = new HashMap<String, String>();


            try {
                JSONObject ssjson = jObject.getJSONObject(i);
                String id = ssjson.getString("id");
                String rowid = ssjson.getString("rowid");
                pkmap.put("id", id);

                pkmap.put("rowid", rowid);
                String usercode = ssjson.getString("usercode");
                if (StringUtils.isBlank(id) || StringUtils.isBlank(usercode) || StringUtils.isBlank(rowid)) {
                    throw new BusinessException("失败！参数id/usercode不能为空");
                }
                String userid = getPk("sm_user", "cuserid", "user_code", usercode, "");
                if (userid.equals("")) {
                    throw new BusinessException("审核人不存在" + usercode);
                }
                InvocationInfoProxy.getInstance().setUserId(userid);

                IPMOQueryService query = (IPMOQueryService) NCLocator.getInstance().lookup(IPMOQueryService.class);
                String[] ids = new String[1];
                ids[0] = id;

                PMOAggVO[] rvo = query.queryByPks(ids);
                if (null == rvo || rvo.length == 0) {
                    throw new BusinessException("失败！未找到对应单据，请检查id值");
                }
                Integer fbillstatus = rvo[0].getParentVO().getFbillstatus();
                if (fbillstatus != null && fbillstatus != 1) {
                    throw new BusinessException("失败！审核通过后能投放");
                }
                PMOItemVO[] vbos = rvo[i].getChildrenVO();

                ArrayList<PMOItemVO> arrb = new ArrayList<PMOItemVO>();
                if (vbos != null && vbos.length > 0) {
                    for (int j = 0; j < vbos.length; j++) {
                        if (vbos[j].getCmoid().equals(rowid) && vbos[j].getFitemstatus() == 4) {
                            arrb.add(vbos[j]);
                        }
                    }
                }
                if (arrb.size() < 1) {
                    throw new BusinessException("投放的rowid不存在或已投放");
                }
                rvo[i].setChildrenVO(arrb.toArray(new PMOItemVO[arrb.size()]));
                IPMOBusinessService batchRunScriptService = NCLocator.getInstance().lookup(IPMOBusinessService.class);
                batchRunScriptService.rowput(rvo);
                pkmap.put("status", "0");
                map.add(pkmap);
            } catch (BusinessException e) {
                pkmap.put("errmsg", e.getMessage());
                pkmap.put("status", "1");
                map.add(pkmap);
            }

        }

        return ResultMessageUtil.toJSON(map);
    }

    /**
     * 生产报告审核
     */
    @POST
    @Path("/putPmoReport")
    @Consumes({"application/json"})
    @Produces({"application/json"})
    public JSONString putPmoReport(JSONString json1) throws BusinessException {
        if (json1 == null) {
            return ResultMessageUtil.toJSON("失败！参数不能为空");
        }

        JSONArray jObject = JSON.parseArray(json1.toString());
        if (jObject == null) {
            return ResultMessageUtil.exceptionToJSON(new BusinessException("失败！参数不能为空"));
        }
        ArrayList<Map<String, String>> map = new ArrayList<Map<String, String>>();


        for (int i = 0; i < jObject.size(); i++) {

            Map<String, String> pkmap = new HashMap<String, String>();


            try {
                JSONObject ssjson = jObject.getJSONObject(i);
                String id = ssjson.getString("id");
                pkmap.put("id", id);
                String usercode = ssjson.getString("usercode");
                if (StringUtils.isBlank(id) || StringUtils.isBlank(usercode)) {
                    throw new BusinessException("失败！参数id/usercode不能为空");
                }
                String userid = getPk("sm_user", "cuserid", "user_code", usercode, "");
                if (userid.equals("")) {
                    throw new BusinessException("审核人不存在" + usercode);
                }
                InvocationInfoProxy.getInstance().setUserId(userid);

                IPMOQueryService query = (IPMOQueryService) NCLocator.getInstance().lookup(IPMOQueryService.class);
                String[] ids = new String[1];
                ids[0] = id;

                PMOAggVO[] rvo = query.queryByPks(ids);
                if (null == rvo || rvo.length == 0) {
                    throw new BusinessException("失败！未找到对应单据，请检查id值");
                }
                Integer fbillstatus = rvo[0].getParentVO().getFbillstatus();
                if (fbillstatus != null && fbillstatus != 1) {
                    throw new BusinessException("失败！审核通过后能投放");
                }
                IPMOBusinessService batchRunScriptService = NCLocator.getInstance().lookup(IPMOBusinessService.class);
                batchRunScriptService.rowput(rvo);
                pkmap.put("status", "0");
                map.add(pkmap);
            } catch (BusinessException e) {
                pkmap.put("errmsg", e.getMessage());
                pkmap.put("status", "1");
                map.add(pkmap);
            }

        }

        return ResultMessageUtil.toJSON(map);
    }

    private Map<String, Object> validateData(JSONObject headObj, JSONArray bodyObj) {
        Map<String, Object> dataMap = new HashMap<>();
        BaseDAO baseDAO = new BaseDAO();

        String sql = "";
        String pk_group = (String) headObj.get("pk_group");
        if (StringUtils.isBlank(pk_group)) {
            dataMap.put("code", "2");
            dataMap.put("data", "head的pk_group不能为空");
            return dataMap;
        }
        String groupName = "";
        try {
            sql = " select name from org_group where pk_group = '" + pk_group + "' ";
            groupName = (String) baseDAO.executeQuery(sql, new ColumnProcessor());
            if (StringUtils.isEmpty(groupName)) {
                dataMap.put("code", "2");
                dataMap.put("data", "未查询到相应的集团");
                return dataMap;
            }
        } catch (Exception e) {
            dataMap.put("code", "2");
            dataMap.put("data", "未查询到相应的集团");
            return dataMap;
        }
        JSONObject pkGroup = new JSONObject();
        pkGroup.put("value", pk_group);
        pkGroup.put("display", groupName);

        String pk_org = (String) headObj.get("pk_org");
        if (StringUtils.isBlank(pk_org)) {
            dataMap.put("code", "2");
            dataMap.put("data", "head的pk_org不能为空");
            return dataMap;
        }
        String orgName = "";
        try {
            sql = " select name from org_orgs where pk_org = '" + pk_org + "' ";
            orgName = (String) baseDAO.executeQuery(sql, new ColumnProcessor());
            if (StringUtils.isEmpty(orgName)) {
                dataMap.put("code", "2");
                dataMap.put("data", "未查询到相应的组织");
                return dataMap;
            }
        } catch (Exception e) {
            dataMap.put("code", "2");
            dataMap.put("data", "未查询到相应的组织");
            return dataMap;
        }
        JSONObject pkOrg = new JSONObject();
        pkOrg.put("value", pk_org);
        pkOrg.put("display", orgName);

        String pk_org_v = (String) headObj.get("pk_org_v");
        if (StringUtils.isBlank(pk_org_v)) {
            dataMap.put("code", "2");
            dataMap.put("data", "head的pk_org_v不能为空");
            return dataMap;
        }
        String orgVName = "";
        try {
            sql = " select name from org_orgs_v where pk_vid = '" + pk_org_v + "' ";
            orgVName = (String) baseDAO.executeQuery(sql, new ColumnProcessor());
            if (StringUtils.isEmpty(orgVName)) {
                dataMap.put("code", "2");
                dataMap.put("data", "未查询到相应的组织版本");
                return dataMap;
            }
        } catch (Exception e) {
            dataMap.put("code", "2");
            dataMap.put("data", "未查询到相应的组织版本");
            return dataMap;
        }
        JSONObject pkOrgv = new JSONObject();
        pkOrgv.put("value", pk_org_v);
        pkOrgv.put("display", orgVName);

        String vtrantypeid = (String) headObj.get("vtrantypeid");
        if (StringUtils.isBlank(vtrantypeid)) {
            dataMap.put("code", "2");
            dataMap.put("data", "head的vtrantypeid不能为空");
            return dataMap;
        }
        String trantypeName = "";
        try {
            sql = " select billtypename from bd_billtype where pk_billtypeid = '" + vtrantypeid + "' ";
            trantypeName = (String) baseDAO.executeQuery(sql, new ColumnProcessor());
            if (StringUtils.isEmpty(trantypeName)) {
                dataMap.put("code", "2");
                dataMap.put("data", "未查询到相应的报告类型");
                return dataMap;
            }
        } catch (Exception e) {
            dataMap.put("code", "2");
            dataMap.put("data", "未查询到相应的报告类型");
            return dataMap;
        }
        JSONObject trantype = new JSONObject();
        trantype.put("value", vtrantypeid);
        trantype.put("display", trantypeName);

        String fprodmode = (String) headObj.get("fprodmode");
        if (StringUtils.isBlank(fprodmode)) {
            dataMap.put("code", "2");
            dataMap.put("data", "head的fprodmode不能为空");
            return dataMap;
        }
        JSONObject hfprodmode = new JSONObject();
        hfprodmode.put("value", fprodmode);
        hfprodmode.put("display", "1".equals(fprodmode) ? "流程制造" : "离散制造");

        String cdeptvid = (String) headObj.get("cdeptvid");
        if (StringUtils.isBlank(cdeptvid)) {
            JSONObject deptv = new JSONObject();
            deptv.put("value", "");
            deptv.put("display", "");
        } else {
            String deptvName = "";
            try {
                sql = " select name from org_dept_v where pk_vid = '" + cdeptvid + "' ";
                deptvName = (String) baseDAO.executeQuery(sql, new ColumnProcessor());
                if (StringUtils.isEmpty(deptvName)) {
                    dataMap.put("code", "2");
                    dataMap.put("data", "未查询到相应的组织_部门版本");
                    return dataMap;
                }
            } catch (Exception e) {
                dataMap.put("code", "2");
                dataMap.put("data", "未查询到相应的组织_部门版本");
                return dataMap;
            }
            JSONObject deptv = new JSONObject();
            deptv.put("value", cdeptvid);
            deptv.put("display", deptvName);
        }

        String cdeptid = (String) headObj.get("cdeptid");
        String deptName = "";
        if (StringUtils.isBlank(cdeptid)) {
            JSONObject dept = new JSONObject();
            dept.put("value", "");
            dept.put("display", "");
        } else {
            try {
                sql = " select name from org_dept where pk_dept = '" + cdeptid + "' ";
                deptName = (String) baseDAO.executeQuery(sql, new ColumnProcessor());
                if (StringUtils.isEmpty(deptName)) {
                    dataMap.put("code", "2");
                    dataMap.put("data", "未查询到相应的部门");
                    return dataMap;
                }
            } catch (Exception e) {
                dataMap.put("code", "2");
                dataMap.put("data", "未查询到相应的部门");
                return dataMap;
            }
            JSONObject dept = new JSONObject();
            dept.put("value", cdeptid);
            dept.put("display", deptName);
        }

        String cwkid = (String) headObj.get("cwkid");
        if (StringUtils.isEmpty(cwkid)) {
            JSONObject cwk = new JSONObject();
            cwk.put("value", "");
            cwk.put("display", "");

            JSONObject cwkidVwkname = new JSONObject();
            cwkidVwkname.put("value", "");
        } else {
            String cwkName = "";
            try {
                sql = " select vwkname from bd_wk where cwkid = '" + cwkid + "' ";
                cwkName = (String) baseDAO.executeQuery(sql, new ColumnProcessor());
                if (StringUtils.isEmpty(cwkName)) {
                    dataMap.put("code", "2");
                    dataMap.put("data", "未查询到相应的工作中心");
                    return dataMap;
                }
            } catch (Exception e) {
                dataMap.put("code", "2");
                dataMap.put("data", "未查询到相应的工作中心");
                return dataMap;
            }
            JSONObject cwk = new JSONObject();
            cwk.put("value", cwkid);
            cwk.put("display", cwkName);

            JSONObject cwkidVwkname = new JSONObject();
            cwkidVwkname.put("value", cwkName);
        }

        String cteamid = (String) headObj.get("cteamid");
        if (StringUtils.isBlank(cteamid)) {
            JSONObject cteam = new JSONObject();
            cteam.put("value", "");
            cteam.put("display", "");
        } else {
            String cteamidName = "";
            try {
                sql = " select vteamname from bd_team where cteamid = '" + cteamid + "' ";
                cteamidName = (String) baseDAO.executeQuery(sql, new ColumnProcessor());
                if (StringUtils.isEmpty(cteamidName)) {
                    dataMap.put("code", "2");
                    dataMap.put("data", "未查询到相应的班组");
                    return dataMap;
                }
            } catch (Exception e) {
                dataMap.put("code", "2");
                dataMap.put("data", "未查询到相应的班组");
                return dataMap;
            }
            JSONObject cteam = new JSONObject();
            cteam.put("value", cteamid);
            cteam.put("display", cteamidName);
        }

        String cshiftid = (String) headObj.get("cshiftid");
        if (StringUtils.isBlank(cshiftid)) {
            JSONObject cshift = new JSONObject();
            cshift.put("value", "");
            cshift.put("display", "");
        } else {
            String cshiftidName = "";
            try {
                sql = " select name from bd_shift where pk_shift = '" + cshiftid + "' ";
                cshiftidName = (String) baseDAO.executeQuery(sql, new ColumnProcessor());
                if (StringUtils.isEmpty(cshiftidName)) {
                    dataMap.put("code", "2");
                    dataMap.put("data", "未查询到相应的班次");
                    return dataMap;
                }
            } catch (Exception e) {
                dataMap.put("code", "2");
                dataMap.put("data", "未查询到相应的班次");
                return dataMap;
            }
            JSONObject cshift = new JSONObject();
            cshift.put("value", cshiftid);
            cshift.put("display", cshiftidName);
        }

        String cworkmanid = (String) headObj.get("cworkmanid");
        String cworkmanName = "";
        if (StringUtils.isBlank(cworkmanid)) {
            JSONObject workman = new JSONObject();
            workman.put("value", "");
            workman.put("display", "");
        } else {
            try {
                sql = " select name from bd_psndoc where pk_psndoc = '" + cworkmanid + "' ";
                cworkmanName = (String) baseDAO.executeQuery(sql, new ColumnProcessor());
                if (StringUtils.isEmpty(cworkmanName)) {
                    dataMap.put("code", "2");
                    dataMap.put("data", "未查询到相应的作业员");
                    return dataMap;
                }
            } catch (Exception e) {
                dataMap.put("code", "2");
                dataMap.put("data", "未查询到相应的作业员");
                return dataMap;
            }
            JSONObject workman = new JSONObject();
            workman.put("value", cworkmanid);
            workman.put("display", cworkmanName);
        }

        String dbilldate = (String) headObj.get("dbilldate");
        if (StringUtils.isBlank(dbilldate)) {
            dataMap.put("code", "2");
            dataMap.put("data", "head的dbilldate不能为空");
            return dataMap;
        } else {
            JSONObject billdate = new JSONObject();
            billdate.put("value", dbilldate);
        }

        String vdef1 = (String) headObj.get("vdef1");
        String vdef2 = (String) headObj.get("vdef2");
        String vdef3 = (String) headObj.get("vdef3");
        String vdef4 = (String) headObj.get("vdef4");
        String vdef5 = (String) headObj.get("vdef5");
        String vdef6 = (String) headObj.get("vdef6");
        String vdef7 = (String) headObj.get("vdef7");
        String vdef8 = (String) headObj.get("vdef8");
        String vdef9 = (String) headObj.get("vdef9");
        String vdef10 = (String) headObj.get("vdef10");
        String vdef11 = (String) headObj.get("vdef11");
        String vdef12 = (String) headObj.get("vdef12");
        String vdef13 = (String) headObj.get("vdef13");
        String vdef14 = (String) headObj.get("vdef14");
        String vdef15 = (String) headObj.get("vdef15");
        String vdef16 = (String) headObj.get("vdef16");
        String vdef17 = (String) headObj.get("vdef17");
        String vdef18 = (String) headObj.get("vdef18");
        String vdef19 = (String) headObj.get("vdef19");
        String vdef20 = (String) headObj.get("vdef20");

        String vnote = (String) headObj.get("vnote");

        String billmaker = (String) headObj.get("billmaker");
        String billmakerName = "";
        if (StringUtils.isBlank(billmaker)) {
            dataMap.put("code", "2");
            dataMap.put("data", "head的billmaker不能为空");
            return dataMap;
        } else {
            try {
                sql = " select user_name from sm_user where cuserid = '" + billmaker + "' ";
                billmakerName = (String) baseDAO.executeQuery(sql, new ColumnProcessor());
                if (StringUtils.isEmpty(billmakerName)) {
                    dataMap.put("code", "2");
                    dataMap.put("data", "未查询到相应的用户");
                    return dataMap;
                }
            } catch (Exception e) {
                dataMap.put("code", "2");
                dataMap.put("data", "未查询到相应的用户");
                return dataMap;
            }
            JSONObject hbillmaker = new JSONObject();
            hbillmaker.put("value", billmaker);
            hbillmaker.put("display", billmakerName);
        }

        String dmakedate = (String) headObj.get("dmakedate");
        if (StringUtils.isBlank(dmakedate)) {
            dataMap.put("code", "2");
            dataMap.put("data", "head的dmakedate不能为空");
            return dataMap;
        }
        JSONObject hdmakedate = new JSONObject();
        hdmakedate.put("value", dmakedate);

        String creator = (String) headObj.get("creator");
        String creatorName = "";
        if (StringUtils.isBlank(creator)) {
            dataMap.put("code", "2");
            dataMap.put("data", "head的creator不能为空");
            return dataMap;
        } else {
            try {
                sql = " select user_name from sm_user where cuserid = '" + creator + "' ";
                creatorName = (String) baseDAO.executeQuery(sql, new ColumnProcessor());
                if (StringUtils.isEmpty(creatorName)) {
                    dataMap.put("code", "2");
                    dataMap.put("data", "未查询到相应的用户");
                    return dataMap;
                }
            } catch (Exception e) {
                dataMap.put("code", "2");
                dataMap.put("data", "未查询到相应的用户");
                return dataMap;
            }
            JSONObject hcreator = new JSONObject();
            hcreator.put("value", creator);
            hcreator.put("display", creatorName);
        }

        String creationtime = (String) headObj.get("creationtime");
        if (StringUtils.isBlank(creationtime)) {
            dataMap.put("code", "2");
            dataMap.put("data", "head的creationtime不能为空");
            return dataMap;
        }
        JSONObject hcreationtime = new JSONObject();
        hcreationtime.put("value", creationtime);

        JSONObject vtrantypecode = new JSONObject();
        vtrantypecode.put("value", "55A4-01");

        List<WrItemVO> bvoItemList = new ArrayList<>();
        // 封装bodys
        Set<String> rownoSet = new HashSet<>();
        for (int i = 0; i < bodyObj.size(); i++) {
            JSONObject item = new JSONObject();
            try {
                item = (JSONObject) bodyObj.get(i);
            } catch (Exception e) {
                e.printStackTrace();
            }

            // 入库批次号
            String vbinbatchcode = (String) item.get("vbinbatchcode");

            // 计划完工主数量
            String nbplanwrnum = (String) item.get("nbplanwrnum");

            // cbmoid 生产订单
            // cbmobid 生产订单行id
            String cbmoid = (String) item.get("cbmoid");
            String cbmobid = (String) item.get("cbmobid");
            String vbmobillcode = "";
            String vbmorowno = "";
            if (StringUtils.isBlank(cbmoid) || StringUtils.isBlank(cbmobid)) {
            } else {
                try {
                    sql = " select vbillcode from mm_pmo where cpmohid = '" + cbmoid + "' ";
                    vbmobillcode = (String) baseDAO.executeQuery(sql, new ColumnProcessor());
                    if (StringUtils.isEmpty(vbmobillcode)) {
                        dataMap.put("code", "2");
                        dataMap.put("data", "未查询到相应的流程生产订单");
                        return dataMap;
                    }
                } catch (Exception e) {
                    dataMap.put("code", "2");
                    dataMap.put("data", "未查询到相应的流程生产订单");
                    return dataMap;
                }
                JSONObject bcbmoid = new JSONObject();
                bcbmoid.put("value", cbmoid);

                JSONObject bvbmobillcode = new JSONObject();
                bvbmobillcode.put("value", vbmobillcode);

                try {
                    sql = " select vrowno from mm_mo where cmoid = '" + cbmobid + "' ";
                    vbmorowno = (String) baseDAO.executeQuery(sql, new ColumnProcessor());
                    if (StringUtils.isEmpty(vbmorowno)) {
                        dataMap.put("code", "2");
                        dataMap.put("data", "未查询到相应的流程生产订单明细");
                        return dataMap;
                    }
                } catch (Exception e) {
                    dataMap.put("code", "2");
                    dataMap.put("data", "未查询到相应的流程生产订单");
                    return dataMap;
                }

                JSONObject bcbmobid = new JSONObject();
                bcbmobid.put("value", cbmobid);

                JSONObject bvbmorowno = new JSONObject();
                bvbmorowno.put("value", vbmorowno);
            }
            // vbmoparentbillcode 上级生产订单号
//            String vbmoparentbillcode = (String) item.get("vbmoparentbillcode");

            // cbfirstmoid 源头生产订单id
            String cbfirstmoid = (String) item.get("cbfirstmoid");
            // vbfirstmocode 源头生产订单号
            String vbfirstmocode = "";
            // cbfirstmobid 源头生产订单行id
            String cbfirstmobid = (String) item.get("cbfirstmobid");
            String vbfirstmorowno = "";
            // vbfirstmorowno 源头生产订单行号
            if (StringUtils.isBlank(cbfirstmoid) || StringUtils.isBlank(cbfirstmobid)) {
            } else {
                try {
                    sql = " select vbillcode from mm_pmo where cpmohid = '" + cbfirstmoid + "' ";
                    vbfirstmocode = (String) baseDAO.executeQuery(sql, new ColumnProcessor());
                    if (StringUtils.isEmpty(vbfirstmocode)) {
                        dataMap.put("code", "2");
                        dataMap.put("data", "未查询到相应的流程生产订单");
                        return dataMap;
                    }
                } catch (Exception e) {
                    dataMap.put("code", "2");
                    dataMap.put("data", "未查询到相应的流程生产订单");
                    return dataMap;
                }
                JSONObject bcbfirstmoid = new JSONObject();
                bcbfirstmoid.put("value", cbfirstmoid);

                JSONObject bvbfirstmocode = new JSONObject();
                bvbfirstmocode.put("value", vbfirstmocode);

                try {
                    sql = " select vrowno from mm_mo where cmoid = '" + cbfirstmobid + "' ";
                    vbfirstmorowno = (String) baseDAO.executeQuery(sql, new ColumnProcessor());
                    if (StringUtils.isEmpty(vbfirstmorowno)) {
                        dataMap.put("code", "2");
                        dataMap.put("data", "未查询到相应的流程生产订单明细");
                        return dataMap;
                    }
                } catch (Exception e) {
                    dataMap.put("code", "2");
                    dataMap.put("data", "未查询到相应的流程生产订单");
                    return dataMap;
                }

                JSONObject bcbfirstmobid = new JSONObject();
                bcbfirstmobid.put("value", cbfirstmobid);

                JSONObject bvbfirstmorowno = new JSONObject();
                bvbfirstmorowno.put("value", vbfirstmorowno);
            }

            // cbsrcmoid 来源生产订单id
            String cbsrcmoid = (String) item.get("cbsrcmoid");
            // vbsrcmocode 来源生产订单号
            String vbsrcmocode = "";
            // cbsrcmobid 来源生产订单行
            String cbsrcmobid = (String) item.get("cbsrcmobid");
            // vbsrcmorowno 来源生产订单行号
            String vbsrcmorowno = "";
            if (StringUtils.isBlank(cbsrcmoid) || StringUtils.isBlank(cbsrcmobid)) {
            } else {
                try {
                    sql = " select vbillcode from mm_pmo where cpmohid = '" + cbsrcmoid + "' ";
                    vbsrcmocode = (String) baseDAO.executeQuery(sql, new ColumnProcessor());
                    if (StringUtils.isEmpty(vbsrcmocode)) {
                        dataMap.put("code", "2");
                        dataMap.put("data", "未查询到相应的流程生产订单");
                        return dataMap;
                    }
                } catch (Exception e) {
                    dataMap.put("code", "2");
                    dataMap.put("data", "未查询到相应的流程生产订单");
                    return dataMap;
                }
                JSONObject bcbsrcmoid = new JSONObject();
                bcbsrcmoid.put("value", cbsrcmoid);

                JSONObject bvbsrcmocode = new JSONObject();
                bvbsrcmocode.put("value", vbsrcmocode);

                try {
                    sql = " select vrowno from mm_mo where cmoid = '" + cbsrcmobid + "' ";
                    vbsrcmorowno = (String) baseDAO.executeQuery(sql, new ColumnProcessor());
                    if (StringUtils.isEmpty(vbsrcmorowno)) {
                        dataMap.put("code", "2");
                        dataMap.put("data", "未查询到相应的流程生产订单明细");
                        return dataMap;
                    }
                } catch (Exception e) {
                    dataMap.put("code", "2");
                    dataMap.put("data", "未查询到相应的流程生产订单");
                    return dataMap;
                }

                JSONObject bcbsrcmobid = new JSONObject();
                bcbsrcmobid.put("value", cbsrcmobid);

                JSONObject vvbsrcmorowno = new JSONObject();
                vvbsrcmorowno.put("value", vbsrcmorowno);
            }


            // cbfirstranstype 源头交易类型
            String cbfirstranstype = (String) item.get("cbfirstranstype");
            // vbfirstranstype 源头交易类型编码
            String vbfirstranstype = (String) item.get("vbfirstranstype");
            // 源头单据类型
            String vbfirsttype = (String) item.get("vbfirsttype");
            // vbfirstid 源头单据id
            String vbfirstid = (String) item.get("vbfirstid");
            // 源头单据号  vbfirstcode
            String vbfirstcode = (String) item.get("vbfirstcode");
            // vbfirstrowid 源头单据明细id
            String vbfirstrowid = (String) item.get("vbfirstrowid");
            // vbfirstrowno 源头单据行号
            String vbfirstrowno = (String) item.get("vbfirstrowno");

            // cbsrctranstype 来源交易类型
            String cbsrctranstype = (String) item.get("cbsrctranstype");
            // 来源交易类型编码 vbsrctranstype
            String vbsrctranstype = (String) item.get("vbsrctranstype");
            // cbsrctype 来源单据类型
            String cbsrctype = (String) item.get("cbsrctype");
            // 来源单据
            String vbsrcid = (String) item.get("vbsrcid");
            // vbsrccode 来源单据号
            String vbsrccode = (String) item.get("vbsrccode");
            // vbsrcrowid 来源单据明细
            String vbsrcrowid = (String) item.get("vbsrcrowid");
            // vbsrcrowno 来源单据行号
            String vbsrcrowno = (String) item.get("vbsrcrowno");

            String cbmaterialvid = (String) item.get("cbmaterialvid");
            if (StringUtils.isBlank(cbmaterialvid)) {
                dataMap.put("code", "2");
                dataMap.put("data", "body的cbmaterialvid不能为空");
                return dataMap;
            }
            String materialvName = "";
            String materialvCode = "";
            String materialspec = "";
            String version = "";
            String graphid = "";
            String materialtype = "";
            List<Map<String, Object>> materialMapList = new ArrayList<>();
            try {
                sql = " select name, code, materialspec, version, graphid, materialtype from bd_material where pk_material = '" + cbmaterialvid + "' ";
                materialMapList = (List<Map<String, Object>>) baseDAO.executeQuery(sql, new MapListProcessor());
                if (materialMapList == null || materialMapList.size() == 0) {
                    dataMap.put("code", "2");
                    dataMap.put("data", "未查询到相应的物料");
                    return dataMap;
                }
                Map<String, Object> materialMap = materialMapList.get(0);
                materialvName = (String) materialMap.get("name");
                materialvCode = (String) materialMap.get("code");
                materialspec = (String) materialMap.get("materialspec");
                version = ((Integer) materialMap.get("version")).toString();
                graphid = (String) materialMap.get("graphid");
                materialtype = (String) materialMap.get("materialtype");
            } catch (Exception e) {
                dataMap.put("code", "2");
                dataMap.put("data", "未查询到相应的物料");
                return dataMap;
            }
            JSONObject bcbmaterialType = new JSONObject();
            bcbmaterialType.put("value", materialtype);
            bcbmaterialType.put("display", materialtype);

            String nbwrastnum = (String) item.get("nbwrastnum");
            if (StringUtils.isBlank(nbwrastnum)) {
                dataMap.put("code", "2");
                dataMap.put("data", "body的nbwrastnum不能为空");
                return dataMap;
            }

            String nbplanwrastnum = (String) item.get("nbplanwrastnum");

            String fbproducttype = (String) item.get("fbproducttype");
            if (StringUtils.isBlank(fbproducttype)) {
                dataMap.put("code", "2");
                dataMap.put("data", "body的fbproducttype不能为空");
                return dataMap;
            } else {
                String fbproducttypeName = "";
                switch (fbproducttype) {
                    case "1":
                        fbproducttypeName = "主产品";
                        break;
                    case "2":
                        fbproducttypeName = "联产品";
                        break;
                    case "3":
                        fbproducttypeName = "副产品";
                        break;
                    default:
                        fbproducttypeName = "主产品";
                        break;
                }
                JSONObject bfbproducttype = new JSONObject();
                bfbproducttype.put("display", fbproducttypeName);
                bfbproducttype.put("value", fbproducttype);
            }

            JSONObject bmaterialvidName = new JSONObject();
            bmaterialvidName.put("display", materialvName);
            bmaterialvidName.put("value", materialvName);

            String cbmainmaterialid = (String) item.get("cbmainmaterialid");
            if (StringUtils.isBlank(cbmainmaterialid)) {
                dataMap.put("code", "2");
                dataMap.put("data", "body的cbmainmaterialid不能为空");
                return dataMap;
            }
            String mainmaterialName = "";
            try {
                sql = " select name from bd_material_v where pk_source = '" + cbmainmaterialid + "' ";
                mainmaterialName = (String) baseDAO.executeQuery(sql, new ColumnProcessor());
                if (StringUtils.isBlank(mainmaterialName)) {
                    dataMap.put("code", "2");
                    dataMap.put("data", "未查询到相应的物料");
                    return dataMap;
                }
            } catch (Exception e) {
                dataMap.put("code", "2");
                dataMap.put("data", "未查询到相应的物料");
                return dataMap;
            }
            JSONObject bcbmainmaterialid = new JSONObject();
            bcbmainmaterialid.put("value", cbmainmaterialid);
            bcbmainmaterialid.put("display", mainmaterialName);

            String vbrowno = (String) item.get("vbrowno");
            if (StringUtils.isBlank(vbrowno)) {
                dataMap.put("code", "2");
                dataMap.put("data", "body的vbrowno不能为空");
                return dataMap;
            } else {
                if (rownoSet.contains(vbrowno)) {
                    dataMap.put("code", "2");
                    dataMap.put("data", "body的vbrowno不能重复");
                    return dataMap;
                } else {
                    rownoSet.add(vbrowno);
                }
            }
            JSONObject bvbrowno = new JSONObject();
            bvbrowno.put("value", vbrowno);

            JSONObject bcbmaterialvid = new JSONObject();
            bcbmaterialvid.put("value", cbmaterialvid);
            bcbmaterialvid.put("display", materialvName);

            String cbdeptvid = (String) item.get("cbdeptvid");
            if (StringUtils.isBlank(cbdeptvid)) {
                dataMap.put("code", "2");
                dataMap.put("data", "body的cbdeptvid不能为空");
                return dataMap;
            } else {
                String bdeptName = "";
                try {
                    sql = " select name from org_dept_v where pk_vid = '" + cbdeptvid + "' ";
                    bdeptName = (String) baseDAO.executeQuery(sql, new ColumnProcessor());
                    if (StringUtils.isEmpty(bdeptName)) {
                        dataMap.put("code", "2");
                        dataMap.put("data", "未查询到相应的组织_部门版本");
                        return dataMap;
                    }
                } catch (Exception e) {
                    dataMap.put("code", "2");
                    dataMap.put("data", "未查询到相应的组织_部门版本");
                    return dataMap;
                }
                JSONObject dept = new JSONObject();
                dept.put("value", cbdeptvid);
                dept.put("display", bdeptName);
            }

            String cbwkid = (String) item.get("cbwkid");
            String vwkname = "";
            if (!StringUtils.isBlank(cbwkid)) {
                try {
                    sql = " select vwkname from bd_wk where cwkid = '" + cbwkid + "' ";
                    vwkname = (String) baseDAO.executeQuery(sql, new ColumnProcessor());
                    if (StringUtils.isEmpty(vwkname)) {
                        dataMap.put("code", "2");
                        dataMap.put("data", "未查询到相应的工作中心");
                        return dataMap;
                    }
                } catch (Exception e) {
                    dataMap.put("code", "2");
                    dataMap.put("data", "未查询到相应的工作中心");
                    return dataMap;
                }
                JSONObject bcbwkid = new JSONObject();
                bcbwkid.put("value", cbwkid);
                bcbwkid.put("display", vwkname);
            }

            String cbprojectid = (String) item.get("cbprojectid");
            if (!StringUtils.isBlank(cbprojectid)) {
                String projectname = "";
                try {
                    sql = " select project_name from bd_project where pk_project = '" + cbprojectid + "' ";
                    projectname = (String) baseDAO.executeQuery(sql, new ColumnProcessor());
                    if (StringUtils.isEmpty(projectname)) {
                        dataMap.put("code", "2");
                        dataMap.put("data", "未查询到相应的项目");
                        return dataMap;
                    }
                } catch (Exception e) {
                    dataMap.put("code", "2");
                    dataMap.put("data", "未查询到相应的项目");
                    return dataMap;
                }
                JSONObject bcbprojectid = new JSONObject();
                bcbprojectid.put("value", cbprojectid);
                bcbprojectid.put("display", projectname);
            }

            String vbidentify = (String) item.get("vbidentify");
            JSONObject bvbidentify = new JSONObject();
            bvbidentify.put("value", vbidentify);

            if (!StringUtils.isBlank(cworkmanid)) {
                JSONObject workman = new JSONObject();
                workman.put("value", cworkmanid);
                workman.put("display", cworkmanName);
            }

            // vbbatchid 生产批次
            String vbbatchid = (String) item.get("vbbatchid");
            String vbbatchcode = "";
            if (!StringUtils.isBlank(vbbatchid)) {
                try {
                    sql = " select vprodbatchcode from pd_pb where pk_batchcode = '" + vbbatchid + "' ";
                    vbbatchcode = (String) baseDAO.executeQuery(sql, new ColumnProcessor());
                    if (StringUtils.isEmpty(vbbatchcode)) {
                        dataMap.put("code", "2");
                        dataMap.put("data", "未查询到相应的生产批次档案");
                        return dataMap;
                    }
                } catch (Exception e) {
                    dataMap.put("code", "2");
                    dataMap.put("data", "未查询到相应的生产批次档案");
                    return dataMap;
                }
                JSONObject bvbbatchid = new JSONObject();
                bvbbatchid.put("value", vbbatchid);
            }

            String cbshiftid = (String) item.get("cbshiftid");
            if (!StringUtils.isBlank(cbshiftid)) {
                String cbshiftidName = "";
                try {
                    sql = " select name from bd_shift where pk_shift = '" + cbshiftid + "' ";
                    cbshiftidName = (String) baseDAO.executeQuery(sql, new ColumnProcessor());
                    if (StringUtils.isEmpty(cbshiftidName)) {
                        dataMap.put("code", "2");
                        dataMap.put("data", "未查询到相应的班次");
                        return dataMap;
                    }
                } catch (Exception e) {
                    dataMap.put("code", "2");
                    dataMap.put("data", "未查询到相应的班次");
                    return dataMap;
                }
                JSONObject cbshift = new JSONObject();
                cbshift.put("value", cshiftid);
                cbshift.put("display", cbshiftidName);
            }

            String tbstarttime = (String) item.get("tbstarttime");
            if (StringUtils.isBlank(tbstarttime)) {
                dataMap.put("code", "2");
                dataMap.put("data", "body的tbstarttime不能为空");
                return dataMap;
            } else {
                JSONObject btbstarttime = new JSONObject();
                btbstarttime.put("value", tbstarttime);
            }

            // cbbomversionid 生产BOM版本
            String cbbomversionid = (String) item.get("cbbomversionid");
            String hversion = "";
            if (!StringUtils.isBlank(cbbomversionid)) {
                try {
                    sql = " select hversion from bd_bom where cbomid = '" + cbbomversionid + "' ";
                    hversion = (String) baseDAO.executeQuery(sql, new ColumnProcessor());
                    if (StringUtils.isEmpty(hversion)) {
                        dataMap.put("code", "2");
                        dataMap.put("data", "未查询到相应的物料清单");
                        return dataMap;
                    }
                } catch (Exception e) {
                    dataMap.put("code", "2");
                    dataMap.put("data", "未查询到相应的物料清单");
                    return dataMap;
                }
                JSONObject bcbbomversionid = new JSONObject();
                bcbbomversionid.put("value", cbbomversionid);
                bcbbomversionid.put("display", hversion);
            }

            // nbwrnum 完工主数量 = nbwrastnum 完工数量
            JSONObject nbwrnum = new JSONObject();
            nbwrnum.put("scale", "4");
            nbwrnum.put("value", nbwrastnum);

            JSONObject pkGroupItem = new JSONObject();
            pkGroupItem.put("value", pkGroup.get("value"));
            pkGroupItem.put("display", pkGroup.get("display"));

            if (!StringUtils.isBlank(cbbomversionid)) {
                JSONObject vbmainbomcode = new JSONObject();
                vbmainbomcode.put("value", hversion);

                JSONObject cbmainbomid = new JSONObject();
                cbmainbomid.put("value", cbbomversionid);
                cbmainbomid.put("display", hversion);
            }

            if (!StringUtils.isBlank(cbwkid)) {
                JSONObject bvwkname = new JSONObject();
                bvwkname.put("value", vwkname);
            }

            String tbendtime = (String) item.get("tbendtime");
            if (StringUtils.isBlank(tbendtime)) {
                dataMap.put("code", "2");
                dataMap.put("data", "body的tbendtime不能为空");
                return dataMap;
            } else {
                JSONObject btbendtime = new JSONObject();
                btbendtime.put("value", tbendtime);
            }
            JSONObject pkOrgItem = new JSONObject();
            pkOrgItem.put("value", pkOrg.get("value"));
            pkOrgItem.put("display", pkOrg.get("display"));

            JSONObject bcbmaterialSpec = new JSONObject();
            bcbmaterialSpec.put("value", materialspec);
            bcbmaterialSpec.put("display", materialspec);

            if (!StringUtils.isBlank(cdeptid)) {
                JSONObject bdept = new JSONObject();
                bdept.put("value", cdeptid);
                bdept.put("display", deptName);
            }

            JSONObject cbmainmaterialvid = new JSONObject();
            cbmainmaterialvid.put("value", cbmaterialvid);
            cbmainmaterialvid.put("display", materialvCode);

            String cbteamid = (String) item.get("cbteamid");
            String teamName = "";
            if (!StringUtils.isBlank(cbteamid)) {
                try {
                    sql = " select vteamname from bd_team where cteamid = '" + cbteamid + "' ";
                    teamName = (String) baseDAO.executeQuery(sql, new ColumnProcessor());
                    if (StringUtils.isEmpty(teamName)) {
                        dataMap.put("code", "2");
                        dataMap.put("data", "未查询到相应的班组");
                        return dataMap;
                    }
                } catch (Exception e) {
                    dataMap.put("code", "2");
                    dataMap.put("data", "未查询到相应的班组");
                    return dataMap;
                }
                JSONObject bcbteamid = new JSONObject();
                bcbteamid.put("value", cbteamid);
                bcbteamid.put("display", teamName);
            }

            String fbsourcetype = (String) item.get("fbsourcetype");
            if (StringUtils.isBlank(fbsourcetype)) {
                dataMap.put("code", "2");
                dataMap.put("data", "body的fbsourcetype不能为空");
                return dataMap;
            } else {
                String fbsourcetypeName = "";
                switch (fbsourcetype) {
                    case "1":
                        fbsourcetypeName = "手工自制";
                        break;
                    case "2":
                        fbsourcetypeName = "订单报产";
                        break;
                    case "3":
                        fbsourcetypeName = "工序完工";
                        break;
                    case "4":
                        fbsourcetypeName = "投放计划";
                        break;
                    case "5":
                        fbsourcetypeName = "收货单";
                        break;
                    default:
                        fbsourcetypeName = "手工自制";
                        break;
                }
                JSONObject bfbsourcetype = new JSONObject();
                bfbsourcetype.put("display", fbsourcetypeName);
                bfbsourcetype.put("value", fbsourcetype);
            }

            JSONObject pkOrgvItem = new JSONObject();
            try {
                BeanUtils.copyProperties(pkOrgvItem, pkOrgv);
            } catch (Exception e) {
                e.printStackTrace();
            }

            if (!StringUtils.isBlank(vbbatchid)) {
                JSONObject bvbbatchcode = new JSONObject();
                bvbbatchcode.put("value", vbbatchcode);
                bvbbatchcode.put("display", vbbatchcode);
            }

            JSONObject bcbmaterialVersion = new JSONObject();
            bcbmaterialVersion.put("value", version);
            bcbmaterialVersion.put("display", version);

            JSONObject bmaterialvName = new JSONObject();
            bmaterialvName.put("value", materialvName);
            bmaterialvName.put("display", materialvName);

            JSONObject bmaterialvgraphid = new JSONObject();
            bmaterialvgraphid.put("value", graphid);
            bmaterialvgraphid.put("display", graphid);

            // vbmainmorowno 主产品行号 todo
            String vbmainmorowno = (String) item.get("vbmainmorowno");

            String cbmaterialid = (String) item.get("cbmaterialid");
            String materialName = "";
            if (StringUtils.isBlank(cbmaterialid)) {
                dataMap.put("code", "2");
                dataMap.put("data", "body的cbmaterialid不能为空");
                return dataMap;
            } else {
                try {
                    sql = " select name from bd_material_v where pk_source = '" + cbmaterialid + "' ";
                    materialName = (String) baseDAO.executeQuery(sql, new ColumnProcessor());
                    if (StringUtils.isEmpty(materialName)) {
                        dataMap.put("code", "2");
                        dataMap.put("data", "未查询到相应的物料");
                        return dataMap;
                    }
                } catch (Exception e) {
                    dataMap.put("code", "2");
                    dataMap.put("data", "未查询到相应的物料");
                    return dataMap;
                }
                JSONObject bcbmaterialid = new JSONObject();
                bcbmaterialid.put("value", cbmaterialid);
                bcbmaterialid.put("display", materialName);
            }

            String cbastunitid = (String) item.get("cbastunitid");
            String unitName = "";
            if (!StringUtils.isBlank(cbastunitid)) {
                try {
                    sql = " select name from bd_measdoc where pk_measdoc = '" + cbastunitid + "' ";
                    unitName = (String) baseDAO.executeQuery(sql, new ColumnProcessor());
                    if (StringUtils.isEmpty(unitName)) {
                        dataMap.put("code", "2");
                        dataMap.put("data", "未查询到相应的计量单位");
                        return dataMap;
                    }
                } catch (Exception e) {
                    dataMap.put("code", "2");
                    dataMap.put("data", "未查询到相应的计量单位");
                    return dataMap;
                }
                JSONObject bcbunitid = new JSONObject();
                bcbunitid.put("value", cbastunitid);
                bcbunitid.put("display", unitName);

                JSONObject bcbastunitid = new JSONObject();
                bcbastunitid.put("value", cbastunitid);
                bcbastunitid.put("display", unitName);
            }

            WrItemVO bvo = new WrItemVO();
            bvo.setPk_group(pk_group);
            bvo.setPk_org(pk_org);
            bvo.setPk_org_v(pk_org_v);
            bvo.setVbrowno(vbrowno);
            bvo.setCbmoid(cbmoid);
            bvo.setVbmobillcode(vbmobillcode);
            bvo.setCbmobid(cbmobid);
            bvo.setVbmorowno(vbmorowno);
            // vbmoparentbillcode
            bvo.setCbfirstmoid(cbfirstmoid);
            bvo.setVbfirstmocode(vbfirstmocode);
            bvo.setCbfirstmobid(cbmobid);
            bvo.setVbfirstmorowno(vbfirstmorowno);
            bvo.setCbsrcmoid(cbsrcmoid);
            bvo.setVbsrcmocode(vbsrcmocode);
            bvo.setCbsrcmobid(cbsrcmobid);
            bvo.setVbsrcmorowno(vbsrcmorowno);
            bvo.setFbproducttype(Integer.parseInt(fbproducttype));
            bvo.setCbmaterialid(cbmaterialid);
            bvo.setCbmaterialvid(cbmaterialvid);
            bvo.setVbmainmorowno(vbmainmorowno);
            bvo.setCbbomversionid(cbbomversionid);
            bvo.setVbbomversioncode(hversion);
//            bvo.setCbpackbomid(cbpackbomid);
//            bvo.setCbmainbomid(cbmainbomid);
            bvo.setVbmainbomcode(hversion);
            bvo.setCbmainmaterialid(cbmainmaterialid);
            bvo.setCbmainmaterialvid(cbmaterialvid);

//            bvo.setCbrtversionid(cbrtversionid);
//            bvo.setVbrtversioncode(vbrtversioncode);
            bvo.setCbdeptid(cdeptid);
            bvo.setCbdeptvid(cbdeptvid);
            bvo.setCbwkid(cbwkid);
            bvo.setCbshiftid(cbshiftid);
            bvo.setCbteamid(cbteamid);
            bvo.setCbworkmanid(cworkmanid);
            bvo.setTbstarttime(new UFDateTime(tbstarttime));
            bvo.setTbendtime(new UFDateTime(tbendtime));
            bvo.setVbbatchid(vbbatchid);
            bvo.setVbbatchcode(vbbatchcode);
//            bvo.setVbinbatchid(vbinbatchid);
            bvo.setVbinbatchcode(vbinbatchcode);
            bvo.setFbsourcetype(Integer.parseInt(fbsourcetype));
            bvo.setCbunitid(cbastunitid);
            bvo.setCbastunitid(cbastunitid);
            bvo.setVbchangerate("1/1");
            bvo.setNbplanwrnum(new UFDouble(nbplanwrnum));
            bvo.setNbplanwrastnum(new UFDouble(nbplanwrastnum));
            bvo.setNbwrnum(new UFDouble(nbwrastnum));
            bvo.setNbwrastnum(new UFDouble(nbwrastnum));
//            bvo.setNbsldinastnum(null);
//            bvo.setNbsldinnum(null);
//            bvo.setNbsldchecknum(nbsldchecknum);
//            bvo.setNbsldcheckastnum(nbsldcheckastnum);
//            bvo.setNbchecknum(nbchecknum);
//            bvo.setNbcheckastnum(nbcheckastnum);
//            bvo.setBbhasbckfled(bbhasbckfled);
//            bvo.setBbhaspicked(bbhaspicked);
//            bvo.setBbstockbycheck(bbstockbycheck);
//            bvo.setBbisempass(bbisempass);
//            bvo.setBbchkflag(bbchkflag);
//            bvo.setBbinstock(bbinstock);
//            bvo.setBbotherreject(bbotherreject);
//            bvo.setBbsetmark(bbsetmark);
//            bvo.setCbempassid(cbempassid);
//            bvo.setCbempass_bid(cbempass_bid);
//            bvo.setCbempass_brow(cbempass_brow);
//            bvo.setCbempasscode(cbempasscode);
//            bvo.setNbempassnum(nbempassnum);
//            bvo.setNbempassastnum(nbempassastnum);
//            bvo.setNbaldempinnum(nbaldempinastnum);
//            bvo.setNbaldempinastnum(nbaldempinastnum);
//            bvo.setVbsalebillcode(vbsalebillcode);
//            bvo.setVbsalebillid(vbsalebillid);
            bvo.setVbsrctranstype(vbsrctranstype);
            bvo.setCbsrctranstype(cbsrctranstype);
            bvo.setCbsrctype(cbsrctype);
            bvo.setVbsrccode(vbsrccode);
            bvo.setVbsrcid(vbsrcid);
            bvo.setVbsrcrowid(vbsrcrowid);
            bvo.setVbsrcrowno(vbsrcrowno);
            bvo.setVbfirstranstype(vbfirstranstype);
            bvo.setCbfirstranstype(cbfirstranstype);
            bvo.setVbfirsttype(vbfirsttype);
            bvo.setVbfirstcode(vbfirstcode);
            bvo.setVbfirstid(vbfirstid);
            bvo.setVbfirstrowid(vbfirstrowid);
            bvo.setVbfirstrowno(vbfirstrowno);
            bvo.setCbprojectid(cbprojectid);

            bvo.setVbfirstranstype(vbfirstranstype);
            bvo.setCbfirstranstype(cbfirstranstype);
            bvo.setVbfirsttype(vbfirsttype);
            bvo.setVbfirstcode(vbfirstcode);
            bvo.setVbfirstid(vbfirstid);
            bvo.setVbfirstrowid(vbfirstrowid);
            bvo.setVbfirstrowno(vbfirstrowno);

            bvo.setVbsrctranstype(vbsrctranstype);
            bvo.setCbsrctranstype(cbsrctranstype);
            bvo.setCbsrctype(cbsrctype);
            bvo.setVbsrccode(vbsrccode);
            bvo.setVbsrcid(vbsrcid);
            bvo.setVbsrcrowid(vbsrcrowid);
            bvo.setVbsrcrowno(vbsrcrowno);

            bvoItemList.add(bvo);
        }

        WrVO hvo = new WrVO();
        hvo.setPk_group(pk_group);
        hvo.setPk_org(pk_org);
        hvo.setPk_org_v(pk_org_v);
        hvo.setVtrantypeid(vtrantypeid);
        hvo.setVtrantypecode("55A4-01");
        hvo.setFprodmode(Integer.parseInt(fprodmode));
        hvo.setCdeptid(cdeptid);
        hvo.setCdeptvid(cdeptvid);
        hvo.setCwkid(cwkid);
        hvo.setDbilldate(new UFDate(dbilldate));
        hvo.setCshiftid(cshiftid);
        hvo.setCteamid(cteamid);
        hvo.setCworkmanid(cworkmanid);
        hvo.setFbillstatus(1);
        hvo.setVnote(vnote);
        hvo.setBillmaker(billmaker);
        hvo.setDmakedate(new UFDate(dmakedate));
        hvo.setCreator(creator);
        hvo.setCreationtime(new UFDateTime(creationtime));
        hvo.setVdef1(vdef1);
        hvo.setVdef2(vdef2);
        hvo.setVdef3(vdef3);
        hvo.setVdef4(vdef4);
        hvo.setVdef5(vdef5);
        hvo.setVdef6(vdef6);
        hvo.setVdef7(vdef7);
        hvo.setVdef8(vdef8);
        hvo.setVdef9(vdef9);
        hvo.setVdef10(vdef10);
        hvo.setVdef11(vdef11);
        hvo.setVdef12(vdef12);
        hvo.setVdef13(vdef13);
        hvo.setVdef14(vdef14);
        hvo.setVdef15(vdef15);
        hvo.setVdef16(vdef16);
        hvo.setVdef17(vdef17);
        hvo.setVdef18(vdef18);
        hvo.setVdef19(vdef19);
        hvo.setVdef20(vdef20);

        WrItemVO[] bvos = bvoItemList.toArray(new WrItemVO[bvoItemList.size()]);
        AggWrVO aggWr = new AggWrVO();
        aggWr.setParent(hvo);
        aggWr.setChildrenVO(bvos);

        dataMap.put("code", "1");
        dataMap.put("data", aggWr);
        return dataMap;
    }

    public String getPk(String tablename, String pkfile, String where, String value, String sqlwhere) throws BusinessException {
        String sql = " select " + pkfile + " from " + tablename + " where nvl(dr,0)=0 and " + where + "='" + value + "'  " + sqlwhere;
        Object o = getDao().executeQuery(sql, new ColumnProcessor());
        if (o != null) {
            return o.toString();
        }
        return "";
    }

    @SuppressWarnings("unchecked")
    private AggWrVO[] getWrByFinProdBodys(FinProdInBodyVO[] proinBVOs) throws BusinessException {

        if (proinBVOs == null || proinBVOs.length == 0) {
            throw new BusinessException("WMS传入产成品入库表体不能为空，请检查！");
        }

        List<String> srcbpklist = new ArrayList<String>();
        for (FinProdInBodyVO proinBVO : proinBVOs) {
            String vsourcerowno = proinBVO.getVsourcerowno();// 来源单据表体主键
            if (StringUtils.isEmpty(vsourcerowno)) {
                throw new BusinessException("WMS传入产成品入库表体来源生成报告表体（完工产出）主键不能为空，请检查！");
            }
            srcbpklist.add(vsourcerowno);
        }

        // 根据来源单据表体主键查询上游单据
        List<AggWrVO> listaggvo = new ArrayList<AggWrVO>();

        String condition = SqlUtils.getInStr("pk_wr_product", srcbpklist.toArray(new String[0]), false);
        String bsql = "SELECT * FROM mm_wr_product where nvl(dr,0) =0  and   " + condition;
        List<WrItemVO> itemlist = (List<WrItemVO>) getDao().executeQuery(bsql, new BeanListProcessor(WrItemVO.class));

        if (itemlist == null || itemlist.size() == 0) {
            throw new BusinessException("根据WMS传入来源生产报告表体（完工产出）主键在NCC中找不到对应的生产报告信息，请检查！");
        }

        List<String> hpklist = new ArrayList<String>();
        for (WrItemVO b : itemlist) {
            String pk_wr = b.getPk_wr();
            hpklist.add(pk_wr);
        }
        String hcondition = SqlUtils.getInStr("pk_wr", hpklist.toArray(new String[0]), false);

        String hsql = "SELECT * FROM mm_wr where nvl(dr,0) =0 and   " + hcondition;
        List<WrVO> wrHeadlist = (List<WrVO>) getDao().executeQuery(hsql, new BeanListProcessor(WrVO.class));

        // 把表体按照表头主键分类
        Map<String, List<WrItemVO>> map = new HashMap<String, List<WrItemVO>>();

        for (WrItemVO bodyVO : itemlist) {
            if (map.containsKey(bodyVO.getPk_wr())) {
                map.get(bodyVO.getPk_wr()).add(bodyVO);
            } else {
                List<WrItemVO> bodylist = new ArrayList<WrItemVO>();
                bodylist.add(bodyVO);
                map.put(bodyVO.getPk_wr(), bodylist);
            }
        }
        // 根据表头，匹配表体
        for (WrVO headVo : wrHeadlist) {
            AggWrVO aggVO = new AggWrVO();
            if (map.containsKey(headVo.getPk_wr())) {
                aggVO.setParent(headVo);
                List<WrItemVO> listBodyVO = map.get(headVo.getPk_wr());
                aggVO.setChildrenVO(listBodyVO.toArray(new WrItemVO[listBodyVO.size()]));
                listaggvo.add(aggVO);
            }
        }
        return listaggvo.toArray(new AggWrVO[0]);
    }

    public void fillquality(AggWrVO[] wrAggs) {
        for (AggWrVO wrAgg : wrAggs) {
            WrItemVO[] itemvos = wrAgg.getChildrenVO();
            AggWrVO[] aggvos = null;
            try {
                aggvos = query.queryByPk(new String[]{wrAgg.getPrimaryKey()});
            } catch (BusinessException e) {
                ExceptionUtils.wrappException(e);
            }
            if (MMValueCheck.isEmpty(aggvos)) {
                ExceptionUtils.wrappBusinessException(NCLangRes4VoTransl.getNCLangRes().getStrByID("5008100_0", "05008100-0000"));
            }

            Map<String, WrQualityVO[]> map = new HashMap<String, WrQualityVO[]>();
            Map<String, WrItemVO> itemsMap = new HashMap<String, WrItemVO>();
            for (AggWrVO vo : aggvos) {
                if (MMValueCheck.isNotEmpty(vo.getChildrenVO())) {
                    for (WrItemVO itemvo : vo.getChildrenVO()) {
                        itemsMap.put(itemvo.getPrimaryKey(), itemvo);
                        map.put(itemvo.getPrimaryKey(), itemvo.getQualityvos());
                    }
                }
            }

            for (WrItemVO itemvo : itemvos) {

                itemvo.setQualityvos((WrQualityVO[]) map.get(itemvo.getPrimaryKey()));
                itemvo.setCbmaterialid(((WrItemVO) itemsMap.get(itemvo.getPrimaryKey())).getCbmaterialid());
                itemvo.setCbmaterialvid(((WrItemVO) itemsMap.get(itemvo.getPrimaryKey())).getCbmaterialvid());
                itemvo.setCbmainbomid(((WrItemVO) itemsMap.get(itemvo.getPrimaryKey())).getCbmainbomid());
                itemvo.setCbmainmaterialvid(((WrItemVO) itemsMap.get(itemvo.getPrimaryKey())).getCbmainmaterialvid());
                itemvo.setCbmainmaterialid(((WrItemVO) itemsMap.get(itemvo.getPrimaryKey())).getCbmainmaterialid());
                itemvo.setVbbatchid(((WrItemVO) itemsMap.get(itemvo.getPrimaryKey())).getVbbatchid());
                itemvo.setVbbatchcode(((WrItemVO) itemsMap.get(itemvo.getPrimaryKey())).getVbbatchcode());
                itemvo.setVbinbatchid(((WrItemVO) itemsMap.get(itemvo.getPrimaryKey())).getVbinbatchid());
                itemvo.setVbinbatchcode(((WrItemVO) itemsMap.get(itemvo.getPrimaryKey())).getVbinbatchcode());
                itemvo.setCbunitid(((WrItemVO) itemsMap.get(itemvo.getPrimaryKey())).getCbunitid());
                itemvo.setCbastunitid(((WrItemVO) itemsMap.get(itemvo.getPrimaryKey())).getCbastunitid());
                itemvo.setVbmobillcode(((WrItemVO) itemsMap.get(itemvo.getPrimaryKey())).getVbmobillcode());
                itemvo.setVbsrctranstype(((WrItemVO) itemsMap.get(itemvo.getPrimaryKey())).getVbsrctranstype());
                itemvo.setVbsrccode(((WrItemVO) itemsMap.get(itemvo.getPrimaryKey())).getVbsrccode());
                itemvo.setVbmorowno(((WrItemVO) itemsMap.get(itemvo.getPrimaryKey())).getVbmorowno());
                itemvo.setVbfirstcode(((WrItemVO) itemsMap.get(itemvo.getPrimaryKey())).getVbfirstcode());
                itemvo.setCbfirstranstype(((WrItemVO) itemsMap.get(itemvo.getPrimaryKey())).getCbfirstranstype());
                itemvo.setVbchangerate(((WrItemVO) itemsMap.get(itemvo.getPrimaryKey())).getVbchangerate());
                itemvo.setVbfirstmorowno(((WrItemVO) itemsMap.get(itemvo.getPrimaryKey())).getVbfirstmorowno());
                itemvo.setCbsrctranstype(((WrItemVO) itemsMap.get(itemvo.getPrimaryKey())).getCbsrctranstype());
                itemvo.setVbmainmorowno(((WrItemVO) itemsMap.get(itemvo.getPrimaryKey())).getVbmainmorowno());
                itemvo.setVbfirstrowno(((WrItemVO) itemsMap.get(itemvo.getPrimaryKey())).getVbfirstrowno());
            }
        }
    }

    private FinProdInVO[] wrChange46(AggWrVO[] aggVOs, FinProdInHeadVO headVO) {
        WrItemVO[] items = null;

        AggWrVO[] newVOs = new AggWrVO[aggVOs.length];
        for (int i = 0; i < aggVOs.length; i++) {
            newVOs[i] = (AggWrVO) aggVOs[i].clone();
            WrTransTypeUtil.changeTransTypeCodeDefault(newVOs[i].getParentVO());
            items = (WrItemVO[]) newVOs[i].getChildren(WrItemVO.class);

            for (WrItemVO item : items) {
                if (null == item.getBbisempass() || !item.getBbisempass().booleanValue()) {
                    item.setNbempassastnum(null);
                    item.setNbempassnum(null);
                    item.setCbempass_bid(null);
                    item.setCbempass_brow(null);
                    item.setCbempasscode(null);
                    item.setCbempassid(null);

                }
            }
        }

        AggWrChangeVO[] aggChangeVOs = WrBusiVOToChangeVO.changeOnlyQualityVO(newVOs);
        if (aggChangeVOs == null) {
            ExceptionUtils.wrappBusinessException(NCLangRes4VoTransl.getNCLangRes().getStrByID("5008100_0", "05008100-0031"));
        }
        for (AggWrChangeVO changeAgg : aggChangeVOs) {
            WrChangeItemVO[] changebVOs = (WrChangeItemVO[]) changeAgg.getChildrenVO();
            for (WrChangeItemVO changeb : changebVOs) {
                changeb.setCgwarehouseid(headVO.getCwarehouseid());// 补充仓库

            }
        }

        try {
            return (FinProdInVO[]) PFPubService.runChangeData("55A9", "46", aggChangeVOs, null, 1);
        } catch (BusinessException e) {
            ExceptionUtils.wrappException(e);
            return null;
        }

    }

    private String getValueByCondtion(String tablename, String fieldname, String contion) throws BusinessException {
        BaseDAO dao = new BaseDAO();

        String result = "";
        StringBuffer sb = new StringBuffer();
        sb.append(" SELECT " + fieldname + "  FROM " + tablename + "  ");
        sb.append(" WHERE nvl(" + tablename + " .dr,0)= 0  ");
        sb.append(" and " + contion + " ");
        result = (String) dao.executeQuery(sb.toString(), new ColumnProcessor());
        return result;
    }
}
