package com.seeyon.apps.ydctu8c.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.seeyon.apps.ext.workflow.vo.FieldDataVo;
import com.seeyon.apps.ext.workflow.vo.FormDataVo;
import com.seeyon.apps.ydctu8c.vo.U8cResponse;
import com.seeyon.cap4.form.api.FormDataApi4Cap4;
import com.seeyon.cap4.form.bean.FormDataMasterBean;
import com.seeyon.cap4.form.bean.FormDataSubBean;
import com.seeyon.ctp.common.exceptions.BusinessException;
import nc.vo.jcom.lang.StringUtil;

import java.util.List;
import java.util.Map;

public class Cap4ReturnUtil {

    public void setParamReturn(String method, U8cResponse u8cResponse, FormDataVo formDataVo,String nodeId) throws BusinessException, NoSuchFieldException {
        if("/u8cloud/api/uapbd/custdoc/insert".equals(method)){
            String data = u8cResponse.getData();
            JSONArray jsonArray = (JSONArray) JSON.parse(data);
            JSONObject jsonObject = jsonArray.getJSONObject(0);
            JSONObject parentvo = jsonObject.getJSONObject("parentvo");
            formDataVo.getNewFieldDataMap().put("U8C-客商主键", parentvo.getString("pk_cubasdoc"));
            JSONArray banks = jsonObject.getJSONArray("BANK");
//            银行id
            String bankIds = "";
            if(banks.size()>0){
                for(int i = 0 ; i < banks.size() ; i++){
                    JSONObject bank = banks.getJSONObject(i);
                    String bankNo = bank.getString("account");
                    String bankId = bank.getString("pk_custbank");
                    bankIds = bankIds+bankNo+":"+bankId+";";
                }
                bankIds = bankIds.substring(0,bankIds.length() - 1);
                formDataVo.getNewFieldDataMap().put("U8C-返回客商银行对照", bankIds);
            }
        }else if("/u8cloud/api/uapbd/custdoc/update".equals(method)){
            String data = u8cResponse.getData();
            JSONArray jsonArray = (JSONArray) JSON.parse(data);
            JSONObject jsonObject = jsonArray.getJSONObject(0);
            JSONArray banks = jsonObject.getJSONArray("BANK");
//            银行id
            String bankIds = "";
            if(banks.size()>0){
                for(int i = 0 ; i < banks.size() ; i++){
                    JSONObject bank = banks.getJSONObject(i);
                    String bankNo = bank.getString("account");
                    String bankId = bank.getString("pk_custbank");
                    bankIds = bankIds+bankNo+":"+bankId+";";
                }
                bankIds = bankIds.substring(0,bankIds.length() - 1);
                formDataVo.getNewFieldDataMap().put("U8C-返回客商银行对照", bankIds);
            }
        }else if("/u8cloud/api/cmp/fj/insert".equals(method)){
            String data = u8cResponse.getData();
            JSONArray jsonArray = (JSONArray) JSON.parse(data);
            JSONObject jsonObject = jsonArray.getJSONObject(0);
            JSONObject parentvo = jsonObject.getJSONObject("parentvo");
            if("DiaoBoYdctPaymentNode".equals(nodeId)){
                formDataVo.getNewFieldDataMap().put("U8C-单据号付", parentvo.getString("djbh"));
//                税金回写
                JSONArray childrenvos = jsonObject.getJSONArray("childrenvo");
                Map<String, List<FormDataVo>> subFormMap =  formDataVo.getSubFormMap();
                List<FormDataVo> subFormDataVos = subFormMap.get("U8C-结算明细付");
                for(int i = 0 ; i < subFormDataVos.size() ; i++){
                FormDataVo subFormDataVo = subFormDataVos.get(i);
                FieldDataVo u8cSubNo = subFormDataVo.getFieldData("U8C-明细表编号付");
                String u8cSubNoValue = u8cSubNo.getStringValue();
                System.out.println("当前明细行编号为"+u8cSubNoValue);
                for(int n = 0 ; n < childrenvos.size() ; n++){
                    JSONObject childrenvo = childrenvos.getJSONObject(n);
                    String zyx20 = childrenvo.getString("zyx20");
                    System.out.println(u8cSubNoValue.equals(zyx20)+":"+u8cSubNoValue+"="+zyx20);
                    if(u8cSubNoValue.equals(zyx20)){
                        String jfybsj = childrenvo.getString("jfybsj");
                        String jfybwsje = childrenvo.getString("jfybwsje");
                        System.out.println("jfybsj"+jfybsj);
                        System.out.println("jfybwsje"+jfybwsje);
                        double jfybsjd = 0;
                        double jfybwsjed = 0;
                        System.out.println(!StringUtil.isEmpty(jfybsj));
                        if(!StringUtil.isEmpty(jfybsj)){
                            jfybsjd = Double.parseDouble(jfybsj);
                        }
                            System.out.println(!StringUtil.isEmpty(jfybwsje));
                            if(!StringUtil.isEmpty(jfybwsje)){
                                jfybwsjed = Double.parseDouble(jfybwsje);
                            }
                            subFormDataVo.getNewFieldDataMap().put("U8C-本币税金付", jfybsjd);
                            subFormDataVo.getNewFieldDataMap().put("U8C-本币无税金额付", jfybwsjed);
                        }
                    }
                }
            }else{
//                单据号回写
                formDataVo.getNewFieldDataMap().put("U8C-单据号", parentvo.getString("djbh"));

                JSONArray childrenvos = jsonObject.getJSONArray("childrenvo");
                if("TongYongzhifuYdctPaymentNode".equals(nodeId)){
                    JSONObject childrenvo = childrenvos.getJSONObject(0);
                    String jfybsj = childrenvo.getString("jfybsj");
                    String jfybwsje = childrenvo.getString("jfybwsje");
                    double jfybsjd = 0;
                    double jfybwsjed = 0;
                    System.out.println(!StringUtil.isEmpty(jfybsj));
                    if(!StringUtil.isEmpty(jfybsj)){
                        jfybsjd = Double.parseDouble(jfybsj);
                    }
                    System.out.println(!StringUtil.isEmpty(jfybwsje));
                    if(!StringUtil.isEmpty(jfybwsje)){
                        jfybwsjed = Double.parseDouble(jfybwsje);
                    }
                    formDataVo.getNewFieldDataMap().put("主表-U8C本币税金", jfybsjd);
                    formDataVo.getNewFieldDataMap().put("主表-U8C本币无税金额", jfybwsjed);
                }else{
                    //                税金回写
                    Map<String, List<FormDataVo>> subFormMap =  formDataVo.getSubFormMap();

                    for(Map.Entry<String, List<FormDataVo>> entry : subFormMap.entrySet()){
                        String key = entry.getKey();
                        boolean startsWith = key.startsWith("U8C-");
                        if(startsWith){
                            List<FormDataVo> subFormDataVos = entry.getValue();
                            for(int i = 0 ; i < subFormDataVos.size() ; i++){
                                FormDataVo subFormDataVo = subFormDataVos.get(i);
                                FieldDataVo u8cSubNo = subFormDataVo.getFieldData("U8C-明细表编号");
                                String u8cSubNoValue = u8cSubNo.getStringValue();
                                System.out.println("当前明细行编号为"+u8cSubNoValue);
                                for(int n = 0 ; n < childrenvos.size() ; n++){
                                    JSONObject childrenvo = childrenvos.getJSONObject(n);
                                    String zyx20 = childrenvo.getString("zyx20");
                                    System.out.println(u8cSubNoValue.equals(zyx20)+":"+u8cSubNoValue+"="+zyx20);
                                    if(u8cSubNoValue.equals(zyx20)){
                                        String jfybsj = childrenvo.getString("jfybsj");
                                        String jfybwsje = childrenvo.getString("jfybwsje");
                                        System.out.println("jfybsj"+jfybsj);
                                        System.out.println("jfybwsje"+jfybwsje);
                                        double jfybsjd = 0;
                                        double jfybwsjed = 0;
                                        System.out.println(!StringUtil.isEmpty(jfybsj));
                                        if(!StringUtil.isEmpty(jfybsj)){
                                            jfybsjd = Double.parseDouble(jfybsj);
                                        }
                                        System.out.println(!StringUtil.isEmpty(jfybwsje));
                                        if(!StringUtil.isEmpty(jfybwsje)){
                                            jfybwsjed = Double.parseDouble(jfybwsje);
                                        }
                                        subFormDataVo.getNewFieldDataMap().put("U8C-本币税金", jfybsjd);
                                        subFormDataVo.getNewFieldDataMap().put("U8C-本币无税金额", jfybwsjed);
                                    }
                                }

                            }
                        }
                    }
                }
            }
        }else if("/u8cloud/api/cmp/sj/insert".equals(method)){
            String data = u8cResponse.getData();
            JSONArray jsonArray = (JSONArray) JSON.parse(data);
            JSONObject jsonObject = jsonArray.getJSONObject(0);
            JSONObject parentvo = jsonObject.getJSONObject("parentvo");
            if("DiaoBoYdctCollectionNode".equals(nodeId)){
                formDataVo.getNewFieldDataMap().put("U8C-单据号收", parentvo.getString("djbh"));

                Map<String, List<FormDataVo>> subFormMap =  formDataVo.getSubFormMap();
                JSONArray childrenvos = jsonObject.getJSONArray("childrenvo");
                List<FormDataVo> subFormDataVos = subFormMap.get("U8C-结算明细收");
                for(int i = 0 ; i < subFormDataVos.size() ; i++){
                    FormDataVo subFormDataVo = subFormDataVos.get(i);
                    FieldDataVo u8cSubNo = subFormDataVo.getFieldData("U8C-明细表编号收");
                    String u8cSubNoValue = u8cSubNo.getStringValue();
                    System.out.println("当前明细行编号为"+u8cSubNoValue);
                    for(int n = 0 ; n < childrenvos.size() ; n++){
                        JSONObject childrenvo = childrenvos.getJSONObject(n);
                        String zyx20 = childrenvo.getString("zyx20");
                        System.out.println(u8cSubNoValue.equals(zyx20)+":"+u8cSubNoValue+"="+zyx20);
                        if(u8cSubNoValue.equals(zyx20)){
                            String dfybsj = childrenvo.getString("dfybsj");
                            String dfybwsje = childrenvo.getString("dfybwsje");
                            double dfybsjd = 0;
                            double dfybwsjed = 0;
                            if(!StringUtil.isEmpty(dfybsj)){
                                dfybsjd = Double.parseDouble(dfybsj);
                            }
                            if(!StringUtil.isEmpty(dfybwsje)){
                                dfybwsjed = Double.parseDouble(dfybwsje);
                            }
                            subFormDataVo.getNewFieldDataMap().put("U8C-本币税金收", dfybsjd);
                            subFormDataVo.getNewFieldDataMap().put("U8C-本币无税金额收", dfybwsjed);
                        }
                    }
                }
            }else{
//                单据号回写
                formDataVo.getNewFieldDataMap().put("U8C-单据号", parentvo.getString("djbh"));
                //                税金回写
                Map<String, List<FormDataVo>> subFormMap =  formDataVo.getSubFormMap();
                JSONArray childrenvos = jsonObject.getJSONArray("childrenvo");
                for(Map.Entry<String, List<FormDataVo>> entry : subFormMap.entrySet()){
                    String key = entry.getKey();
                    boolean startsWith = key.startsWith("U8C-");
                    if(startsWith){
                        List<FormDataVo> subFormDataVos = entry.getValue();
                        for(int i = 0 ; i < subFormDataVos.size() ; i++){
                            FormDataVo subFormDataVo = subFormDataVos.get(i);
                            FieldDataVo u8cSubNo = subFormDataVo.getFieldData("U8C-明细表编号");
                            String u8cSubNoValue = u8cSubNo.getStringValue();
                            System.out.println("当前明细行编号为"+u8cSubNoValue);
                            for(int n = 0 ; n < childrenvos.size() ; n++){
                                JSONObject childrenvo = childrenvos.getJSONObject(n);
                                String zyx20 = childrenvo.getString("zyx20");
                                System.out.println(u8cSubNoValue.equals(zyx20)+":"+u8cSubNoValue+"="+zyx20);
                                if(u8cSubNoValue.equals(zyx20)){
                                    String dfybsj = childrenvo.getString("dfybsj");
                                    String dfybwsje = childrenvo.getString("dfybwsje");
                                    double dfybsjd = 0;
                                    double dfybwsjed = 0;
                                    if(!StringUtil.isEmpty(dfybsj)){
                                        dfybsjd = Double.parseDouble(dfybsj);
                                    }
                                    if(!StringUtil.isEmpty(dfybwsje)){
                                        dfybwsjed = Double.parseDouble(dfybwsje);
                                    }
                                    subFormDataVo.getNewFieldDataMap().put("U8C-本币税金", dfybsjd);
                                    subFormDataVo.getNewFieldDataMap().put("U8C-本币无税金额", dfybwsjed);
                                }
                            }
                        }
                    }
                }
            }
        }else if("/u8cloud/api/cmp/hj/insert".equals(method)){
            String data = u8cResponse.getData();
            JSONArray jsonArray = (JSONArray) JSON.parse(data);
            JSONObject jsonObject = jsonArray.getJSONObject(0);
            JSONObject parentvo = jsonObject.getJSONObject("parentvo");
            formDataVo.getNewFieldDataMap().put("U8C-单据号付", parentvo.getString("djbh"));
        }

    }


}
