package com.yem.or.api;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yem.em.utils.AtlasUtils;
import com.yem.em.utils.SubRelationUtils;
import com.yem.or.utils.HttpUtils;
import com.yem.or.utils.OrderApiUtils;
import com.yem.wm.utils.DynamicObjectUtil;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.dataentity.serialization.SerializationUtils;
import kd.bos.openapi.common.custom.annotation.ApiController;
import kd.bos.openapi.common.custom.annotation.ApiParam;
import kd.bos.openapi.common.custom.annotation.ApiPostMapping;
import kd.bos.openapi.common.result.CustomApiResult;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.AttachmentServiceHelper;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.QueryServiceHelper;
import org.jetbrains.annotations.NotNull;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Stream;

/**
 * @author zhouc
 * @date 2024/3/23 15:58
 * @className AssepcDynamicPart
 * @description 图册明细查询
 */
@ApiController(value = "AssepcDynamicPart", desc = "图册明细查询")
public class AssepcDynamicPart {
    /**
     * 获取零件信息
     *
     * @return
     * @throws IOException
     */
    @ApiPostMapping("/assepcdynamic/getPart")
    public CustomApiResult<Object> quePart(@NotNull @ApiParam("部件编码") String number,
                                           @NotNull @ApiParam("上级部件") String parnumber,
                                           @ApiParam("整机编码") String complete) throws IOException {
        JSONObject object = new JSONObject();
        Boolean isComplete = false;
        Date yem_completetime = null;
        String yem_replacement = "Par";
        try {
            if (complete.length() > 0) {
                isComplete = true;
                QFilter qFilters = new QFilter("yem_framenumber", QCP.equals, complete);
                DynamicObjectCollection yem_em_devicefiles = QueryServiceHelper.query("yem_em_devicefiles",
                        "id,number,name,yem_model.number,yem_analysistdate", new QFilter[]{qFilters});
                if (yem_em_devicefiles.size() > 0) {
                    String yem_model = yem_em_devicefiles.get(0).getString("yem_model.number");//资源号
                    Date yem_analysistdate = yem_em_devicefiles.get(0).getDate("yem_analysistdate");//料单分析时间
                    if (yem_analysistdate == null) {
                        CustomApiResult<Object> fail = CustomApiResult.fail("503", "整机编号【" + complete + "】领料分析时间为空，禁止查询图册！");
                        return fail;
                    } else {
                        isComplete = true;
                        yem_completetime = yem_analysistdate;
                    }
                } else {
                    CustomApiResult<Object> fail = CustomApiResult.fail("503", "未获取到整机编号【" + complete + "】的信息，禁止查询图册！");
                    return fail;
                }
            }
            DynamicObjectCollection data = getData(parnumber, number, complete, isComplete, yem_completetime, yem_replacement);
            //零件信息
            String param = SerializationUtils.toJsonString(data);
            //图片信息
            JSONArray getfile = getfile(number, isComplete, yem_completetime);
            if (param.length() > 0) {
                object.put("status", "success");
                object.put("mag", "获取成功!");
                object.put("Partlist", param);
                object.put("picturelist", getfile);
            }

        } catch (Exception e) {
            CustomApiResult<Object> fail = CustomApiResult.fail("501", e.getMessage());
            return fail;
        }
        CustomApiResult<Object> ces = CustomApiResult.success(object);
        return ces;
    }

    /**
     * 获取零件信息
     *
     * @param ParentNodeId
     * @param selectid
     * @param complete
     * @param isComplete
     * @param yem_completetime
     * @param yem_replacement
     * @return
     */
    public DynamicObjectCollection getData(String ParentNodeId, String selectid, String complete, boolean isComplete, Date yem_completetime, String yem_replacement) {
        if (selectid.contains("_123245678910")) {
            String[] selec = selectid.split("_");
            String selectfield = DynamicObjectUtil.getSelectfields("yem_em_devicefiles", false);
            String selectfields = DynamicObjectUtil.getEntrySelectfields(selectfield, "yem_em_devicefiles", "yem_retrofitting", false);
            QFilter qFilterdev = new QFilter("yem_framenumber", QCP.equals, complete);
            if (selec[0].length() > 0) {
                qFilterdev.and(new QFilter("yem_retrofitting.yem_albumassemblyno.number", QCP.equals, selec[0]));
            } else {
                qFilterdev.and(new QFilter("yem_retrofitting.yem_albumassemblyno.number", QCP.equals, null));
            }
            DynamicObjectCollection yem_retrofitting = QueryServiceHelper.query("yem_em_devicefiles", selectfields, new QFilter[]{qFilterdev}, null);
            if (yem_retrofitting.size() > 0) {
                //重新构建Data
                DynamicObjectCollection data = reconData(yem_retrofitting);
                return data;
            }
        } else {
            String selectFieids = "id,number,name,yem_atlasno,yem_partenname,yem_partqty,yem_partisreplace,yem_partremark,yem_partweight," +
                    "yem_price,yem_qtytext,yem_pricetext,yem_weighttext,yem_isrepgroup,yem_partmaterielmodel";
            List getassatlaspartdetlist = getassatlaspartdet(selectid, isComplete, yem_completetime);
            QFilter qFilter = new QFilter("id", QCP.in, getassatlaspartdetlist);
            DynamicObjectCollection data = QueryServiceHelper.query("yem_em_assatlaspartdet", selectFieids, new QFilter[]{qFilter}, "length(yem_atlasno),yem_atlasno");
            // 把列数据返回
            DynamicObjectCollection datas = DealData(ParentNodeId, selectid, isComplete, yem_completetime, yem_replacement, data);
            return datas;
        }
        return EmptData();
    }


    public DynamicObjectCollection EmptData() {
        DynamicObjectCollection NewdataCollection = new DynamicObjectCollection();
        DynamicObject dynamicObject = BusinessDataServiceHelper.newDynamicObject("yem_em_asspart");
        DynamicObject newDynObject = new DynamicObject(dynamicObject.getDynamicObjectType());
        NewdataCollection.add(newDynObject);
        return NewdataCollection;
    }

    /**
     * 重新构建Data
     *
     * @param data 待处理数据包
     * @return 返回数据包
     */
    public DynamicObjectCollection reconData(DynamicObjectCollection data) {
        DynamicObjectCollection NewdataCollection = new DynamicObjectCollection();
        DynamicObject dynamicObject = BusinessDataServiceHelper.newDynamicObject("yem_em_assatlaspartdet");
        for (int i = 0; i < data.size(); i++) {
            DynamicObject dynamicObject1 = data.get(i);
            long aLong = dynamicObject1.getLong("yem_retrofitting.yem_materiel");
            DynamicObject dynamicObject2 = BusinessDataServiceHelper.loadSingle(aLong, "bd_material");
            DynamicObject newDynObject = new DynamicObject(dynamicObject.getDynamicObjectType());
            newDynObject.set("yem_atlasno", i + 1);//图册编号
            newDynObject.set("number", dynamicObject2.getString("number"));//零件编号
            newDynObject.set("name", dynamicObject2.getString("name"));//零件名称
            newDynObject.set("yem_partenname", dynamicObject1.getString("yem_retrofitting.yem_enname"));//英文名称
            BigDecimal yem_qty1 = dynamicObject1.getBigDecimal("yem_retrofitting.yem_qty1");
            newDynObject.set("yem_qtytext", yem_qty1.setScale(4, BigDecimal.ROUND_HALF_UP).toString());//数量
            newDynObject.set("yem_partremark", dynamicObject1.getString("yem_retrofitting.yem_desc"));//备注
            NewdataCollection.add(newDynObject);
        }
        return NewdataCollection;
    }

    /**
     * 总成查询图册部件明细分录
     *
     * @param selectid         部件编码
     * @param isComplete       是否总成
     * @param yem_completetime 领料分析时间
     * @return
     */
    public List getassatlaspartdet(String selectid, boolean isComplete, Date yem_completetime) {
        List list = new ArrayList<>();
        QFilter qFilters = new QFilter("number", QCP.equals, selectid);
        if (isComplete) {
            List getassatlasdet = AtlasUtils.getassatlasdet(selectid, yem_completetime);
            if (getassatlasdet.size() > 0) {
                qFilters.and(new QFilter("yem_entryentity.yem_partnumber", QCP.not_in, getassatlasdet));
            }
        } else {
            qFilters.and(new QFilter("yem_entryentity.yem_partisreplace", QCP.equals, false));
        }
        DynamicObjectCollection yem_em_assatlasdet = QueryServiceHelper.query("yem_em_assatlasdet",
                "yem_entryentity.id,yem_entryentity.yem_replace,yem_entryentity.yem_completetime,yem_entryentity.yem_partnumber"
                , new QFilter[]{qFilters});
        for (int i = 0; i < yem_em_assatlasdet.size(); i++) {
            DynamicObject object = yem_em_assatlasdet.get(i);
            long yem_entryentityid = object.getLong("yem_entryentity.id");
            if (isComplete) {
                String yem_replace = object.getString("yem_entryentity.yem_replace");
                Date completetime = object.getDate("yem_entryentity.yem_completetime");
                if (yem_replace.length() > 0) {
                    //新增
                    if (yem_replace.equals("D")) {
                        list.add(yem_entryentityid);
                    }
                    if (yem_replace.equals("C") || yem_replace.equals("E")) {
                        if (completetime == null) {
                            list.add(yem_entryentityid);
                        } else {
                            if (yem_completetime.before(completetime) && completetime.before(new Date())) {
                                list.add(yem_entryentityid);
                            }
                        }
                    }
                } else {
                    list.add(yem_entryentityid);
                }
            } else {
                list.add(yem_entryentityid);
            }
        }
        return list;
    }

    /**
     * 处理特殊逻辑
     * 1. 数量
     * 2. 替换件
     *
     * @param data 查询的数据集合
     * @return 返回数据包
     */
    public DynamicObjectCollection DealData(String ParentNodeId, String selectid, boolean isComplete, Date yem_completetime, String yem_replacement, DynamicObjectCollection data) {
        for (int i = 0; i < data.size(); i++) {
            DynamicObject newDynObject = data.get(i);
            //通过零件编码过滤出替换零件编码
            String yem_pnumber = newDynObject.getString("number");
            //是否替换
            DynamicObject yem_em_substitution = SubRelationUtils.queryAssemblyrep(selectid, yem_pnumber, "Sub", isComplete, yem_completetime);
            if (yem_em_substitution != null) {
                String yem_replace = yem_em_substitution.getString("yem_entryentity.yem_replace");
                if (yem_replace.equals("E")) {
                    newDynObject.set("number", yem_em_substitution.getString("yem_entryentity.yem_onumber"));
                    newDynObject.set("name", yem_em_substitution.getString("yem_entryentity.yem_oname"));
                    newDynObject.set("yem_partenname", yem_em_substitution.getString("yem_entryentity.yem_oenname"));
                    newDynObject.set("yem_qtytext", yem_em_substitution.getString("yem_entryentity.yem_oqty"));
                } else {
                    newDynObject.set("number", yem_em_substitution.getString("yem_entryentity.yem_repnumber"));
                    newDynObject.set("name", yem_em_substitution.getString("yem_entryentity.yem_repname"));
                    newDynObject.set("yem_partenname", yem_em_substitution.getString("yem_entryentity.yem_repenname"));
                    newDynObject.set("yem_qtytext", yem_em_substitution.getString("yem_entryentity.yem_repenqty"));
                }

                newDynObject.set("yem_partisreplace", true);
            } else {
                //总成间零件替换关系
                DynamicObjectCollection pra = SubRelationUtils.queryassemtopartAll(ParentNodeId, selectid, yem_pnumber, "rep", yem_replacement, isComplete, yem_completetime);
                if (pra.size() > 0) {
                    newDynObject.set("yem_partisreplace", true);
                } else {
                    newDynObject.set("yem_partisreplace", false);
                }
            }

            //替换组
            long ReplaceGroupid = SubRelationUtils.queryReplaceGroup(selectid, yem_pnumber, isComplete, yem_completetime);
            if (ReplaceGroupid > 0) {
                newDynObject.set("yem_isrepgroup", true);
            } else {
                newDynObject.set("yem_isrepgroup", false);
            }
            BigDecimal yem_qty = newDynObject.getBigDecimal("yem_partqty");
            newDynObject.set("yem_qtytext", yem_qty.setScale(4, RoundingMode.HALF_UP));
            newDynObject.set("yem_weighttext", newDynObject.getBigDecimal("yem_partweight").setScale(4, RoundingMode.HALF_UP));
            newDynObject.set("yem_pricetext", newDynObject.getBigDecimal("yem_price").setScale(4, RoundingMode.HALF_UP));
        }
        return data;
    }

    /**
     * 获取部件附件信息
     *
     * @param number           获取附件信息
     * @param isComplete
     * @param analysistdate
     * @return
     */
    public JSONArray getfile(String number, Boolean isComplete, Date analysistdate) throws Exception {
        String url = OrderApiUtils.ENDPOINTS + "/admin-api/infra/file/upload";
        JSONArray jsonArray = new JSONArray();
        DynamicObject file = BusinessDataServiceHelper.loadSingle("yem_em_assatlasdet"
                , new QFilter[]{new QFilter("number", QFilter.equals, number)});
        if (file != null) {
//            Set<String> fids = initLatestImage(file, isComplete, analysistdate);
            List<Map<String, Object>> atts = AttachmentServiceHelper.getAttachments("yem_em_assatlasdet", file.getPkValue().toString(), "yem_attachmentpanelap");
            for (int i = 0; i < atts.size(); i++) {
                JSONObject object = new JSONObject();
                Map<String, Object> map = atts.get(i);
                String uid = String.valueOf(map.get("uid"));
//                if (!fids.contains(uid)) {
//                    continue;
//                }
                Map<String, String> token = OrderApiUtils.getToken();
                if (token.size() > 0) {
                    if (token.get("status").equals("success")) {
                        Map bomap = new HashMap<>();
                        bomap.put("Authorization", "Bearer " + token.get("token"));
                        MultipartFile multipartFile = HttpUtils.urlToMultipartFile(map.get("url").toString(), map.get("name").toString());
                        if (multipartFile != null) {
                            String result = HttpUtils.doPostFormData(url, "file", multipartFile, new HashMap<>(), bomap);
                            JSONObject jsonObject = JSONObject.parseObject(result);
                            int code = jsonObject.getIntValue("code");
                            if (code == 0) {
                                object.put("url", jsonObject.getString("data"));
                                object.put("erpurl", map.get("url").toString());
                                object.put("msg", "");
                                jsonArray.add(object);
                            } else {
                                object.put("url", jsonObject.getString("data"));
                                object.put("erpurl", map.get("url").toString());
                                object.put("msg", jsonObject.getString("msg"));
                                jsonArray.add(object);
                            }
                        }
                    } else {
                        object.put("url", "");
                        object.put("erpurl", map.get("url").toString());
                        object.put("msg", "文件上传调用Token失败！");
                        jsonArray.add(object);
                    }
                }
            }
        }
        return jsonArray;
    }

    private Set<String> initLatestImage(DynamicObject component, Boolean isComplete, Date analysistdate) {
        DynamicObjectCollection collection = component.getDynamicObjectCollection("yem_attdetail");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Map<String, List<DynamicObject>> map = new HashMap<>();
        if (isComplete) {
            for (DynamicObject dynamicObject : collection) {
                Date breakpointime = dynamicObject.getDate("yem_breakpointime");
                if (breakpointime.after(analysistdate)) {
                    String key = sdf.format(breakpointime);
                    List<DynamicObject> list;
                    if (map.containsKey(key)) {
                        list = map.get(key);
                    } else {
                        list = new ArrayList<>();
                    }
                    list.add(dynamicObject);
                    map.put(key, list);
                }
            }
        } else {
            for (DynamicObject dynamicObject : collection) {
                Date breakpointime = dynamicObject.getDate("yem_breakpointime");
                String key = sdf.format(breakpointime);
                List<DynamicObject> list;
                if (map.containsKey(key)) {
                    list = map.get(key);
                } else {
                    list = new ArrayList<>();
                }
                list.add(dynamicObject);
                map.put(key, list);
            }
        }

        List<String> keys = new ArrayList<>(map.keySet());
        keys.sort(Comparator.comparing(key -> {
            try {
                return sdf.parse(key);
            } catch (ParseException e) {
                return null;
            }
        }, Comparator.reverseOrder()));

        Set<String> set = new HashSet<>();
        if (!keys.isEmpty()) {
            List<DynamicObject> list = map.get(keys.get(0));
            for (DynamicObject dynamicObject : list) {
                String attid = dynamicObject.getString("yem_attid");
                set.add(attid);
            }
        }
        return set;
    }

    /**
     * 零件信息展开
     *
     * @param subnumber 部件编码
     * @param parnumber 上级部件
     * @param pnumber   零件编码
     * @param complete  总成编码
     * @return
     * @throws IOException
     */
    @ApiPostMapping("/assepc/partunfold")
    public CustomApiResult<Object> Completepartunfold(@NotNull @ApiParam("部件编码") String subnumber,
                                                      @NotNull @ApiParam("上级部件") String parnumber,
                                                      @NotNull @ApiParam("零件编码") String pnumber,
                                                      @ApiParam("整机编码") String complete) throws IOException {
        JSONObject object = new JSONObject();
        Boolean isComplete = false;
        Date yem_completetime = null;
        String yem_replacement = "Sub";
        try {
            if (complete.length() > 0) {
                isComplete = true;
                QFilter qFilters = new QFilter("yem_framenumber", QCP.equals, complete);
                DynamicObjectCollection yem_em_devicefiles = QueryServiceHelper.query("yem_em_devicefiles",
                        "id,number,name,yem_model.number,yem_analysistdate", new QFilter[]{qFilters});
                if (yem_em_devicefiles.size() > 0) {
                    String yem_model = yem_em_devicefiles.get(0).getString("yem_model.number");//资源号
                    Date yem_analysistdate = yem_em_devicefiles.get(0).getDate("yem_analysistdate");//料单分析时间
                    if (yem_analysistdate == null) {
                        CustomApiResult<Object> fail = CustomApiResult.fail("503", "整机编号【" + complete + "】领料分析时间为空，禁止查询图册！");
                        return fail;
                    } else {
                        isComplete = true;
                        yem_completetime = yem_analysistdate;
                    }
                } else {
                    CustomApiResult<Object> fail = CustomApiResult.fail("503", "未获取到整机编号【" + complete + "】的信息，禁止查询图册！");
                    return fail;
                }
            }
            /**
             * 替换关系
             */
            JSONArray jsonArray = new JSONArray();
            Map map = new HashMap<>();
            DynamicObjectCollection emsubstitution = SubRelationUtils.queryAssemblyrepAll(subnumber, pnumber, "Sub", isComplete, yem_completetime);
            if (emsubstitution != null && emsubstitution.size() > 0) {
                List list = SetAssignMap(emsubstitution, parnumber);
                if (list.size() >= 1) {
                    map = (Map) list.get(0);
                    jsonArray = (JSONArray) list.get(1);
                }
            }
            /**
             * 根据原零件编码查询总成间替换关系，
             * 获取原子项信息 通过原子项信息获替换关系
             */
            if (map.size() > 0) {
                DynamicObjectCollection sub = SubRelationUtils.queryreplacetoass(map, "Sub", isComplete, yem_replacement, yem_completetime);
                if (sub != null && sub.size() > 0) {
                    jsonArray = SetAssign(sub, jsonArray);
                }
            }
            /**
             * 总成间替换关系，
             */
            DynamicObjectCollection assemtopart = SubRelationUtils.queryassemtopartAll(parnumber, subnumber, pnumber, "rep", yem_replacement, isComplete, yem_completetime);
            if (assemtopart != null && assemtopart.size() > 0) {
                jsonArray = SetAssign(assemtopart, jsonArray);
            }
            object.put("status", "success");
            object.put("mag", "获取成功!");
            object.put("list", jsonArray);
        } catch (Exception e) {
            CustomApiResult<Object> fail = CustomApiResult.fail("501", e.getMessage());
            return fail;
        }
        CustomApiResult<Object> ces = CustomApiResult.success(object);
        return ces;
    }

    /**
     * 设备零件展开 动态表单赋值
     *
     * @param assemtopart 数据结果集合
     */
    public JSONArray SetAssign(DynamicObjectCollection assemtopart, JSONArray jsonArray) {
        if (assemtopart != null && assemtopart.size() > 0) {
            Stream<DynamicObject> distinct = assemtopart.stream().distinct();
            distinct.forEach((DynamicObject obj) -> {
                JSONObject jsonObject = new JSONObject();
                String yem_praonumber = obj.getString("yem_entryentity.yem_praonumber");
                String yem_onumber = obj.getString("yem_entryentity.yem_onumber");
                String yem_repno = obj.getString("yem_entryentity.yem_repno");
                String yem_repnumber = obj.getString("yem_entryentity.yem_repnumber");
                Boolean verify = verify(jsonArray, yem_praonumber, yem_onumber, yem_repno, yem_repnumber);
                if (!verify) {
                    jsonObject.put("yem_praonumber", obj.getString("yem_entryentity.yem_praonumber"));
                    jsonObject.put("yem_praoname", obj.getString("yem_entryentity.yem_praoname"));
                    jsonObject.put("yem_onumber", obj.getString("yem_entryentity.yem_onumber"));
                    jsonObject.put("yem_oname", obj.getString("yem_entryentity.yem_oname"));
                    jsonObject.put("yem_oenname", obj.getString("yem_entryentity.yem_oenname"));
                    jsonObject.put("yem_oqty", obj.getBigDecimal("yem_entryentity.yem_oqty"));
                    jsonObject.put("yem_repno", obj.getString("yem_entryentity.yem_repno"));
                    jsonObject.put("yem_repnoname", obj.getString("yem_entryentity.yem_repnoname"));
                    jsonObject.put("yem_repnumber", obj.getString("yem_entryentity.yem_repnumber"));
                    jsonObject.put("yem_repname", obj.getString("yem_entryentity.yem_repname"));
                    jsonObject.put("yem_repenname", obj.getString("yem_entryentity.yem_repenname"));
                    jsonObject.put("yem_repenqty", obj.getBigDecimal("yem_entryentity.yem_repenqty"));
                    jsonObject.put("yem_replace", obj.getString("yem_entryentity.yem_replace"));
                    jsonObject.put("yem_replacedate", obj.getDate("yem_entryentity.yem_completetime"));
                    jsonObject.put("yem_technology", obj.getDate("yem_entryentity.yem_technology"));
                    jsonObject.put("yem_trackingno", obj.getString("yem_entryentity.yem_trackingno"));
                    jsonObject.put("yem_remark", obj.getString("yem_entryentity.yem_remark"));
                    jsonObject.put("yem_alternatesource", "组织间替换");
                    jsonArray.add(jsonObject);
                }
            });
        }
        return jsonArray;
    }


    /**
     * 设备零件展开动态表单赋值
     *
     * @param assemtopart 数据结果集合
     */
    public List SetAssignMap(DynamicObjectCollection assemtopart, String Parnumber) {
        List list = new ArrayList<>();
        JSONArray jsonArray = new JSONArray();
        Map<String, String> map = new HashMap<>();
        if (assemtopart != null && assemtopart.size() > 0) {
            if (assemtopart.size() > 0) {
                Stream<DynamicObject> distinct = assemtopart.stream().distinct();
                distinct.forEach((DynamicObject obj) -> {
                    String yem_praonumber = obj.getString("yem_entryentity.yem_praonumber");
                    String yem_onumber = obj.getString("yem_entryentity.yem_onumber");
                    String yem_repno = obj.getString("yem_entryentity.yem_repno");
                    String yem_repnumber = obj.getString("yem_entryentity.yem_repnumber");
                    Boolean verify = verify(jsonArray, yem_praonumber, yem_onumber, yem_repno, yem_repnumber);
                    if (!verify) {
                        String mapkey = obj.getString("yem_entryentity.yem_praonumber") + "_" + obj.getString("yem_entryentity.yem_onumber");
                        if (!map.containsKey(mapkey)) {
                            map.put(mapkey, Parnumber);
                        }
                        JSONObject jsonObject = new JSONObject();
                        jsonObject.put("yem_praonumber", obj.getString("yem_entryentity.yem_praonumber"));
                        jsonObject.put("yem_praoname", obj.getString("yem_entryentity.yem_praoname"));
                        jsonObject.put("yem_onumber", obj.getString("yem_entryentity.yem_onumber"));
                        jsonObject.put("yem_oname", obj.getString("yem_entryentity.yem_oname"));
                        jsonObject.put("yem_oenname", obj.getString("yem_entryentity.yem_oenname"));
                        jsonObject.put("yem_oqty", obj.getBigDecimal("yem_entryentity.yem_oqty"));
                        jsonObject.put("yem_repno", obj.getString("yem_entryentity.yem_repno"));
                        jsonObject.put("yem_repnoname", obj.getString("yem_entryentity.yem_repnoname"));
                        jsonObject.put("yem_repnumber", obj.getString("yem_entryentity.yem_repnumber"));
                        jsonObject.put("yem_repname", obj.getString("yem_entryentity.yem_repname"));
                        jsonObject.put("yem_repenname", obj.getString("yem_entryentity.yem_repenname"));
                        jsonObject.put("yem_repenqty", obj.getBigDecimal("yem_entryentity.yem_repenqty"));
                        jsonObject.put("yem_replace", obj.getString("yem_entryentity.yem_replace"));
                        jsonObject.put("yem_replacedate", obj.getDate("yem_entryentity.yem_completetime"));
                        jsonObject.put("yem_technology", obj.getDate("yem_entryentity.yem_technology"));
                        jsonObject.put("yem_trackingno", obj.getString("yem_entryentity.yem_trackingno"));
                        jsonObject.put("yem_remark", obj.getString("yem_entryentity.yem_remark"));
                        jsonObject.put("yem_alternatesource", "替换关系");
                        jsonArray.add(jsonObject);
                    }
                });
            }
        }
        list.add(map);
        list.add(jsonArray);
        return list;
    }

    public Boolean verify(JSONArray yem_partunfold, String yem_praonumber, String yem_onumber, String yem_repno, String yem_repnumber) {
        String key = yem_praonumber + "" + yem_onumber + "" + yem_repno + "" + yem_repnumber;
        Boolean retr = false;
        if (yem_partunfold.size() > 0) {
            for (int i = 0; i < yem_partunfold.size(); i++) {
                JSONObject object = yem_partunfold.getJSONObject(i);
                String praonumber = object.getString("yem_praonumber");
                String onumber = object.getString("yem_onumber");
                String repno = object.getString("yem_repno");
                String repnumber = object.getString("yem_repnumber");
                String key1 = praonumber + "" + onumber + "" + repno + "" + repnumber;
                if (key.equals(key1)) {
                    retr = true;
                }
            }
        }
        return retr;
    }
}
