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;

public class ParamUtil {

    public String param(String request, String method , FormDataVo formDataVo) throws NoSuchFieldException {
        String param = "";
        switch(method){
            case "/u8cloud/api/uapbd/custmandoc/insert":
                param = insertCustmandocParam(request,formDataVo);
                break;
            case "/u8cloud/api/uapbd/custdoc/insert":
                param = request;
                break;
            case "/u8cloud/api/cmp/fj/insert":
                param = request;
                break;
            case "/u8cloud/api/cmp/sj/insert":
                param = request;
                break;
            case "/u8cloud/api/arap/yf/insert":
                param = request;
                break;
            case "/u8cloud/api/uapbd/custdoc/assign":
                param = allocationCustdocParam (request);
                break;
            case "/u8cloud/api/uapbd/custdoc/update":
                param = request;
                break;
        }
        return param;
    }



    /**
     *客商分配
     * @return
     */
    public String allocationCustdocParam (String request) throws NoSuchFieldException {
//        获取指定的json格式
        JSONObject jsonObject = JSON.parseObject(request);
        JSONArray custbasvos = jsonObject.getJSONArray("custbasvo");
        JSONObject custbasvo = custbasvos.getJSONObject(0);
        String pkCorps = custbasvo.getString("pk_corp");
        String[] corpCodes = pkCorps.split(",");
        JSONObject ret = new JSONObject();
        JSONArray array = new JSONArray();

        for (String corpCode : corpCodes) {
            JSONObject newCustbasvo = new JSONObject();
            newCustbasvo.put("pk_cubasdoc",custbasvo.getString("pk_cubasdoc"));
            newCustbasvo.put("custprop",custbasvo.getString("custprop"));
            newCustbasvo.put("pk_corp",corpCode);
            array.add(newCustbasvo);
        }
        ret.put("custbasvo",array);

        return ret.toString();

    }

    /**
     * 客商管理档案新增，接口参数调整
     * @param request  自动获取接口原参数
     * @param formDataVo    表单字段对象
     * @return
     * @throws NoSuchFieldException
     */
    public String insertCustmandocParam(String request, FormDataVo formDataVo) throws NoSuchFieldException {
//        获取指定的json格式
        JSONObject jsonObject = JSON.parseObject(request);
        JSONArray billvos = jsonObject.getJSONArray("billvo");
        JSONObject billvo = billvos.getJSONObject(0);
        FieldDataVo custmandocType = formDataVo.getFieldData("客商类型");
        String custmandocTypeValue = custmandocType.getStringValue();
        JSONArray childrenvos = new JSONArray();
        JSONObject childrenvo1 = new JSONObject();
        JSONObject childrenvo2 = new JSONObject();
        if("客户".equals(custmandocTypeValue)){
            childrenvo1.put("custflag","0");
            childrenvo2.put("custflag","4");
            childrenvos.add(childrenvo1);
            childrenvos.add(childrenvo2);
            billvo.put("childrenvo",childrenvos);
        }else if ("供应商".equals(custmandocTypeValue)){
            childrenvo1.put("custflag","1");
            childrenvo2.put("custflag","");
            childrenvos.add(childrenvo1);
            childrenvos.add(childrenvo2);
            billvo.put("childrenvo",childrenvos);
        }else if ("客商".equals(custmandocTypeValue)){
            childrenvo1.put("custflag","2");
            childrenvo2.put("custflag","3");
            childrenvos.add(childrenvo1);
            childrenvos.add(childrenvo2);
            billvo.put("childrenvo",childrenvos);
        }
        System.out.println(jsonObject);
        return jsonObject.toString();
    }








}
