package com.shulead.datasync.manager;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.util.DateUtils;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.shulead.datasync.bo.ClassBO;
import com.shulead.datasync.bo.StudentBO;
import com.shulead.datasync.entity.MClass;
import com.shulead.datasync.entity.UfHzqy;
import com.shulead.datasync.entity.ViewPortalClassConsociation;
import com.shulead.datasync.portalcoupon.PortalCouponParams;
import com.shulead.datasync.portalcoupon.PortalCouponWSProxy;
import com.shulead.datasync.portalprice.PortalPriceParams;
import com.shulead.datasync.portalprice.PortalPriceWSProxy;
import com.shulead.datasync.portalprice.Result;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.rmi.RemoteException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @Classname DataSyncManager
 * @Description TODO
 * @Version 1.0.0
 * @Date 2024/5/7 10:01
 * @Created by liuxiangshun
 */
@Slf4j
@Component
public class DataSyncManager {
    @Resource
    private YiDaManager yiDaManager;

    @Resource
    private StudentManager studentManager;

    @Resource
    private ClassManager classManager;

    @Resource
    private MEmployeeManager employeeManager;

    @Resource
    private UfHzqyManager ufHzqyManager;

    @Resource
    private ViewPortalClassConsociationManager viewPortalClassConsociationManager;

    private final AtomicLong startId = new AtomicLong(251805);

    public void syncStudent(List<StudentBO> studentBOList) {
        String formUuId = "FORM-E4CF1445F81E486E8DBCDF85F240E57CFALF";
        for (StudentBO studentBO : studentBOList) {
            JSONArray searchConditions = new JSONArray();
            JSONObject searchCondition = new JSONObject();
            searchCondition.put("key", "textField_lvc4dfk4");
            searchCondition.put("value", studentBO.getId().toString());
            searchCondition.put("type", "TEXT");
            searchCondition.put("operator", "eq");
            searchCondition.put("componentName", "TextField");
            searchConditions.add(searchCondition);

            JSONObject instance = new JSONObject();
            instance.put("textField_lvc4dfk4", studentBO.getId());
            instance.put("textField_ltwr5u40", studentBO.getParentName());
            instance.put("textField_ltwr5u3z", studentBO.getStudentName());
            instance.put("textField_ltwr5u49", studentBO.getMobile());
            instance.put("textField_ltwr5u4a", studentBO.getOrgName());
            instance.put("textField_lup9gnlf", studentBO.getSchoolArea());
            YiDaManager.YiDaResponse response = yiDaManager.createOrUpdateInstance(formUuId, searchConditions.toJSONString(), instance.toJSONString());
            if (!response.getOk()) {
                log.error("同步学生信息失败，studentBO:{}, response:{}", JSON.toJSONString(studentBO), JSON.toJSONString(response));
            }
        }
    }

    public void syncClass(List<ClassBO> classBOList) {
        String formUuId = "FORM-AE935E52ABC54CC9B8E0BF6965472FD1WZLU";
        for (ClassBO classBO : classBOList) {
            JSONArray searchConditions = new JSONArray();
            JSONObject searchCondition = new JSONObject();
            searchCondition.put("key", "textField_lvc4ncmx");
            searchCondition.put("value", classBO.getId().toString());
            searchCondition.put("type", "TEXT");
            searchCondition.put("operator", "eq");
            searchCondition.put("componentName", "TextField");
            searchConditions.add(searchCondition);

            JSONObject instance = new JSONObject();
            instance.put("textField_lvc4ncmx", classBO.getId());
            instance.put("numberField_lty2c63v", classBO.getLessonFee());
            instance.put("textField_lty2c63q", classBO.getClassName());
            instance.put("numberField_lup7xnj6", classBO.getTotalPrice());
            instance.put("numberField_lup7xnj7", classBO.getTotalLessonFee());
            instance.put("textField_lty2c63t", classBO.getType());
            instance.put("numberField_lty2c63u", classBO.getTotalInstalments());
            YiDaManager.YiDaResponse response = yiDaManager.createOrUpdateInstance(formUuId, searchConditions.toJSONString(), instance.toJSONString());
            if (!response.getOk()) {
                log.error("同步班型信息失败， classBO:{}, response:{}", JSON.toJSONString(classBO), JSON.toJSONString(response));
            }
        }
    }

    public void syncHzqy(List<UfHzqy> ufHzqyList) {
        String formUuId = "FORM-11B80FC621CA4DB9A35364CFBFF7287538KR";
        for (UfHzqy ufHzqy : ufHzqyList) {
            JSONArray searchConditions = new JSONArray();
            JSONObject searchCondition = new JSONObject();
            searchCondition.put("key", "textField_lvc4ktjc");
            searchCondition.put("value", ufHzqy.getId().toString());
            searchCondition.put("type", "TEXT");
            searchCondition.put("operator", "eq");
            searchCondition.put("componentName", "TextField");
            searchConditions.add(searchCondition);

            JSONObject instance = new JSONObject();
            instance.put("textField_lvc4ktjc", ufHzqy.getId().toString());
            // 区域代码
            instance.put("textField_lu295ft6", ufHzqy.getQydm());
            // 合作区域
            instance.put("textField_lu295ft5", ufHzqy.getHz());
            // 运营主管
//            Integer yyzg = ufHzqy.getYyzg();
//            MEmployee yyzge = employeeManager.getById(yyzg);
//            if (null != yyzge) {
//                String mobliephone = yyzge.getMobliephone();
//                if (StringUtils.isNotBlank(mobliephone)) {
//                    // 查询 userId
//                    String userId = yiDaManager.getUserIdByPhone(mobliephone);
//                    if (StringUtils.isNotBlank(userId)) {
//                        instance.put("employeeField_lut8rnei", userId);
//                    }
//                }
//            }
            // 运营顾问
//            Integer yygw = ufHzqy.getYygw();
//            MEmployee yygwe = employeeManager.getById(yygw);
//            if (null != yygwe) {
//                String mobliephone = yygwe.getMobliephone();
//                if (StringUtils.isNotBlank(mobliephone)) {
//                    // 查询 userId
//                    String userId = yiDaManager.getUserIdByPhone(mobliephone);
//                    if (StringUtils.isNotBlank(userId)) {
//                        instance.put("employeeField_lut8rneh", userId);
//                    }
//                }
//            }
            // 封存状态
            Integer fczt = ufHzqy.getFczt();
            instance.put("selectField_lu295fta", fczt);

            // 投资人
//            Integer tzr = ufHzqy.getTzr();
//            MEmployee tzre = employeeManager.getById(tzr);
//            if (null != tzre) {
//                String mobliephone = tzre.getMobliephone();
//                if (StringUtils.isNotBlank(mobliephone)) {
//                    // 查询 userId
//                    String userId = yiDaManager.getUserIdByPhone(mobliephone);
//                    if (StringUtils.isNotBlank(userId)) {
//                        instance.put("employeeField_lut8rnej", userId);
//                    }
//                }
//            }

            // 学术主管
//            Integer xszg = ufHzqy.getXszg();
//            MEmployee xszge = employeeManager.getById(xszg);
//            if (null != xszge) {
//                String mobliephone = xszge.getMobliephone();
//                if (StringUtils.isNotBlank(mobliephone)) {
//                    // 查询 userId
//                    String userId = yiDaManager.getUserIdByPhone(mobliephone);
//                    if (StringUtils.isNotBlank(userId)) {
//                        instance.put("employeeField_lut8rnel", userId);
//                    }
//                }
//            }

            // 学术督导
//            Integer xsdd = ufHzqy.getXsdd();
//            MEmployee xsdde = employeeManager.getById(xsdd);
//            if (null != xsdde) {
//                String mobliephone = xsdde.getMobliephone();
//                if (StringUtils.isNotBlank(mobliephone)) {
//                    // 查询 userId
//                    String userId = yiDaManager.getUserIdByPhone(mobliephone);
//                    if (StringUtils.isNotBlank(userId)) {
//                        instance.put("employeeField_lut8rnek", userId);
//                    }
//                }
//            }
            YiDaManager.YiDaResponse response = yiDaManager.createOrUpdateInstance(formUuId, searchConditions.toJSONString(), instance.toJSONString());
            if (!response.getOk()) {
                log.error("同步合作信息失败， classBO:{}, response:{}", JSON.toJSONString(ufHzqy), JSON.toJSONString(response));
            }
        }
    }

    public void syncConsociation(List<ViewPortalClassConsociation> consociationList) {String formUuId = "FORM-13BBF743604942DCAD251E5E7FE381142ZBZ";
        for (ViewPortalClassConsociation consociation : consociationList) {
            JSONArray searchConditions = new JSONArray();
            JSONObject orgIdSearchCondition = new JSONObject();
            orgIdSearchCondition.put("key", "textField_lvc4kk54");
            orgIdSearchCondition.put("value", consociation.getOrgId().toString());
            orgIdSearchCondition.put("type", "TEXT");
            orgIdSearchCondition.put("operator", "eq");
            orgIdSearchCondition.put("componentName", "TextField");
            searchConditions.add(orgIdSearchCondition);

            JSONObject classIdSearchCondition = new JSONObject();
            classIdSearchCondition.put("key", "textField_lw641zsq");
            classIdSearchCondition.put("value", consociation.getClassId().toString());
            classIdSearchCondition.put("type", "TEXT");
            classIdSearchCondition.put("operator", "eq");
            classIdSearchCondition.put("componentName", "TextField");
            searchConditions.add(classIdSearchCondition);

            JSONObject instance = new JSONObject();
            instance.put("textField_lvc4kk54", consociation.getOrgId().toString());
            instance.put("textField_lw641zsq", consociation.getClassId().toString());
            instance.put("numberField_lut8lygt", consociation.getPrice());
            instance.put("numberField_lv3bo57w", consociation.getTotalClassFees());
            instance.put("textField_ltxtb30x", consociation.getClassName());
            instance.put("textField_lv3a47f5", consociation.getClassTypeName());
            instance.put("textField_lw62gk49", consociation.getInstallmentNums().toString());
            instance.put("textField_lw62gk48", consociation.getOrgName());
            instance.put("textField_lw62gk47", consociation.getOrgCode());
            YiDaManager.YiDaResponse response = yiDaManager.createOrUpdateInstance(formUuId, searchConditions.toJSONString(), instance.toJSONString());
            if (!response.getOk()) {
                log.error("同步合作价格信息失败， classBO:{}, response:{}", JSON.toJSONString(consociation), JSON.toJSONString(response));
            }
        }
    }

    public void initSyncStudent(Long start, Integer limit) {
        if (null == limit) {
            limit = 1000;
        }

        Integer finalLimit = limit;

        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                List<StudentBO> studentBOS = studentManager.pageStudent(startId.getAndAdd(finalLimit), finalLimit);
                while (CollectionUtils.isNotEmpty(studentBOS)) {
                    syncStudent(studentBOS);
                    studentBOS = studentManager.pageStudent(startId.getAndAdd(finalLimit), finalLimit);
                }
            }).start();
        }
    }

    public void initSyncClass(Long start, Integer limit) {
        if (null == limit) {
            limit = 1000;
        }
        List<ClassBO> classBOList = classManager.pageClass(start, limit);
        while (CollectionUtils.isNotEmpty(classBOList)) {
            syncClass(classBOList);
            start += limit;
            classBOList = classManager.pageClass(start, limit);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }

    public void initSyncHzqy() {
        List<UfHzqy> ufHzqies = ufHzqyManager.findAll();
        syncHzqy(ufHzqies);
    }

    public void initSyncConsociation() {
        List<ViewPortalClassConsociation> consociations = viewPortalClassConsociationManager.findAll();
        syncConsociation(consociations);
    }

    public StudentBO getStudentByMobile(String mobile) {
        return studentManager.getStudentByMobile(mobile);
    }

    public Object createHz06(String id, String formUuId) {
        // 获取实例
        YiDaManager.YiDaResponse hz06Response = yiDaManager.getFormInstanceById(formUuId, id);
        if (!hz06Response.getOk() || null == hz06Response.getData()) {
            return null;
        }
        List<JSONObject> hz06DataList = JSONArray.parseArray(hz06Response.getData().toString(), JSONObject.class);
        if (CollectionUtils.isEmpty(hz06DataList)) {
            return null;
        }
        // 转化为map
        Map<String, JSONObject> hz06Map = hz06DataList.stream().collect(Collectors.toMap(jsonObject -> jsonObject.getString("fieldId"), Function.identity()));
        // 获取公司信息
        JSONObject companyJsonObject = hz06Map.get("associationFormField_ltse8sq4");
        JSONObject companyFieldDataJsonObject = companyJsonObject.getObject("fieldData", JSONObject.class);
        JSONArray companyValueJsonArray = companyFieldDataJsonObject.getJSONArray("value");
        String companyCode = companyValueJsonArray.getJSONObject(0).getString("subTitle");
//        JSONObject companyJsonObject = hz06Map.get("associationFormField_luhuq5nz");
//        JSONObject companyFieldDataJsonObject = companyJsonObject.getObject("fieldData", JSONObject.class);
//        JSONArray companyValueJsonArray = companyFieldDataJsonObject.getJSONArray("value");
//        String companyFormUuid = companyValueJsonArray.getJSONObject(0).getString("formUuid");
//        String companyInstanceId = companyValueJsonArray.getJSONObject(0).getString("instanceId");
//        YiDaManager.YiDaResponse companyResponse = yiDaManager.getFormInstanceById(companyFormUuid, companyInstanceId);
//        if (!companyResponse.getOk() || null == companyResponse.getData()) {
//            return null;
//        }
//
//        List<JSONObject> companyList = JSONArray.parseArray(companyResponse.getData().toString(), JSONObject.class);
//        if (CollectionUtils.isEmpty(companyList)) {
//            return null;
//        }
//        // 转化为map
//        Map<String, JSONObject> companyMap = companyList.stream().collect(Collectors.toMap(jsonObject -> jsonObject.getString("fieldId"), Function.identity()));
//        String companyCode = companyMap.get("textField_lu84158s").getJSONObject("fieldData").getString("value");

        // 获取班级信息
        JSONArray classValues = hz06Map.get("tableField_ltse8sqr").getJSONObject("fieldData").getJSONArray("value");
        if (CollectionUtils.isEmpty(classValues)) {
            return null;
        }
        for (int i = 0; i < classValues.size(); i++) {
            JSONArray classValue = classValues.getJSONArray(i);
            Map<String, JSONObject> classValueMap = new HashMap<>();
            for (int j = 0; j < classValue.size(); j++) {
                String fieldId = classValue.getJSONObject(j).getString("fieldId");
                classValueMap.put(fieldId, classValue.getJSONObject(j));
            }
            JSONObject classValueJsonObject = classValueMap.get("associationFormField_ltse8squ");
            String classFormUuid = classValueJsonObject.getJSONObject("fieldData").getJSONArray("value").getJSONObject(0).getString("formUuid");
            String classInstanceId = classValueJsonObject.getJSONObject("fieldData").getJSONArray("value").getJSONObject(0).getString("instanceId");
            YiDaManager.YiDaResponse classResponse = yiDaManager.getFormInstanceById(classFormUuid, classInstanceId);
            List<JSONObject> classList = JSONArray.parseArray(classResponse.getData().toString(), JSONObject.class);
            Map<String, JSONObject> classMap = classList.stream().collect(Collectors.toMap(jsonObject -> jsonObject.getString("fieldId"), Function.identity()));
            JSONObject classIdJsonObject = classMap.get("textField_lw641zsq");
            if (null == classIdJsonObject) {
                continue;
            }
            String classId = classIdJsonObject.getJSONObject("fieldData").getString("value");
            if (StringUtils.isEmpty(classId)) {
                continue;
            }

            PortalPriceParams portalPriceParams = new PortalPriceParams();
            // 书本费
            String bookFees = classValueMap.get("numberField_ltse8sqy").getJSONObject("fieldData").getString("value");
            bookFees = StringUtils.isBlank(bookFees) ? "0" : bookFees;
            portalPriceParams.setBookFees(bookFees);
            // 班级 id
            portalPriceParams.setClassId(classId);
            // 阶段 id
            String classLevelId = classValueMap.get("textField_lwirthrf").getJSONObject("fieldData").getString("value");
            portalPriceParams.setClassLevelId(classLevelId);
            // 公司 code
            portalPriceParams.setCompanyCode(companyCode);
            // 实施日期
            JSONObject dateFieldJsonObject = hz06Map.get("dateField_ltse8sq6");
            if (null != dateFieldJsonObject) {
                JSONObject dateFieldData = dateFieldJsonObject.getJSONObject("fieldData");
                if (null != dateFieldData) {
                    Long value = dateFieldData.getLong("value");
                    if (null != value) {
                        String effectiveDate = DateUtils.format(new Date(value), "yyyy-MM-dd HH:mm:ss");
                        portalPriceParams.setEffectiveDate(effectiveDate);
                    }
                }
            }

            // 总课时费
            String totalClassFees = classValueMap.get("numberField_ltse8sqx").getJSONObject("fieldData").getString("value");
            totalClassFees = StringUtils.isBlank(totalClassFees) ? "0" : totalClassFees;
            portalPriceParams.setTotalClassFees(totalClassFees);

            // 类型
            String type = classValueMap.get("textField_lw7kek23").getJSONObject("fieldData").getString("value");
            String nurseFees = classValueMap.get("numberField_lwokgua1").getJSONObject("fieldData").getString("value");
            nurseFees = StringUtils.isBlank(nurseFees) ? "0" : nurseFees;
            if ("全天".equals(type)) {
                // 保育费
                portalPriceParams.setNurseFees(nurseFees);
                // 总费用费
                portalPriceParams.setTotalFees((Double.parseDouble(bookFees) + Double.parseDouble(nurseFees) + Double.parseDouble(totalClassFees)) + "");
            } else {
                // 保育费
                portalPriceParams.setNurseFees("0");
                // 总费用
                portalPriceParams.setTotalFees((Double.parseDouble(bookFees) + Double.parseDouble(totalClassFees)) + "");
            }

            PortalPriceParams[] params = new PortalPriceParams[1];
            params[0] = portalPriceParams;

            PortalPriceWSProxy portalPriceWSProxy = new PortalPriceWSProxy();

            try {
                Result result = portalPriceWSProxy.createPortalPrice(params);
                System.out.println(result);
            } catch (RemoteException e) {
                throw new RuntimeException(e);
            }
        }
        return null;
    }

    public Object createHz06School(String id, String formUuId) {
        // 获取实例
        YiDaManager.YiDaResponse hz06Response = yiDaManager.getFormInstanceById(formUuId, id);
        if (!hz06Response.getOk() || null == hz06Response.getData()) {
            return null;
        }
        List<JSONObject> hz06DataList = JSONArray.parseArray(hz06Response.getData().toString(), JSONObject.class);
        if (CollectionUtils.isEmpty(hz06DataList)) {
            return null;
        }
        // 转化为map
        Map<String, JSONObject> hz06Map = hz06DataList.stream().collect(Collectors.toMap(jsonObject -> jsonObject.getString("fieldId"), Function.identity()));
        // 获取公司信息
        JSONObject companyJsonObject = hz06Map.get("associationFormField_ltse8sq4");
        JSONObject companyFieldDataJsonObject = companyJsonObject.getObject("fieldData", JSONObject.class);
        JSONArray companyValueJsonArray = companyFieldDataJsonObject.getJSONArray("value");
        String companyCode = companyValueJsonArray.getJSONObject(0).getString("subTitle");
//        JSONObject companyJsonObject = hz06Map.get("associationFormField_luhuq5nz");
//        JSONObject companyFieldDataJsonObject = companyJsonObject.getObject("fieldData", JSONObject.class);
//        JSONArray companyValueJsonArray = companyFieldDataJsonObject.getJSONArray("value");
//        String companyFormUuid = companyValueJsonArray.getJSONObject(0).getString("formUuid");
//        String companyInstanceId = companyValueJsonArray.getJSONObject(0).getString("instanceId");
//        YiDaManager.YiDaResponse companyResponse = yiDaManager.getFormInstanceById(companyFormUuid, companyInstanceId);
//        if (!companyResponse.getOk() || null == companyResponse.getData()) {
//            return null;
//        }
//
//        List<JSONObject> companyList = JSONArray.parseArray(companyResponse.getData().toString(), JSONObject.class);
//        if (CollectionUtils.isEmpty(companyList)) {
//            return null;
//        }
//        // 转化为map
//        Map<String, JSONObject> companyMap = companyList.stream().collect(Collectors.toMap(jsonObject -> jsonObject.getString("fieldId"), Function.identity()));
//        String companyCode = companyMap.get("textField_lu84158s").getJSONObject("fieldData").getString("value");

        // 获取班级信息
        JSONArray classValues = hz06Map.get("tableField_ltse8sqr").getJSONObject("fieldData").getJSONArray("value");
        if (CollectionUtils.isEmpty(classValues)) {
            return null;
        }
        for (int i = 0; i < classValues.size(); i++) {
            JSONArray classValue = classValues.getJSONArray(i);
            Map<String, JSONObject> classValueMap = new HashMap<>();
            for (int j = 0; j < classValue.size(); j++) {
                String fieldId = classValue.getJSONObject(j).getString("fieldId");
                classValueMap.put(fieldId, classValue.getJSONObject(j));
            }
            JSONObject classValueJsonObject = classValueMap.get("associationFormField_ltse8squ");
            String classFormUuid = classValueJsonObject.getJSONObject("fieldData").getJSONArray("value").getJSONObject(0).getString("formUuid");
            String classInstanceId = classValueJsonObject.getJSONObject("fieldData").getJSONArray("value").getJSONObject(0).getString("instanceId");
            YiDaManager.YiDaResponse classResponse = yiDaManager.getFormInstanceById(classFormUuid, classInstanceId);
            List<JSONObject> classList = JSONArray.parseArray(classResponse.getData().toString(), JSONObject.class);
            Map<String, JSONObject> classMap = classList.stream().collect(Collectors.toMap(jsonObject -> jsonObject.getString("fieldId"), Function.identity()));
            JSONObject classIdJsonObject = classMap.get("textField_lw641zsq");
            if (null == classIdJsonObject) {
                continue;
            }
            String classId = classIdJsonObject.getJSONObject("fieldData").getString("value");
            if (StringUtils.isEmpty(classId)) {
                continue;
            }

            PortalPriceParams portalPriceParams = new PortalPriceParams();
            // 书本费
            String bookFees = classValueMap.get("numberField_ltse8sqy").getJSONObject("fieldData").getString("value");
            bookFees = StringUtils.isBlank(bookFees) ? "0" : bookFees;
            portalPriceParams.setBookFees(bookFees);
            // 班级 id
            portalPriceParams.setClassId(classId);
            // 阶段 id
            String classLevelId = classValueMap.get("textField_lwok80hx").getJSONObject("fieldData").getString("value");
            portalPriceParams.setClassLevelId(classLevelId);
            // 公司 code
            portalPriceParams.setCompanyCode(companyCode);
            // 实施日期
            JSONObject dateFieldJsonObject = hz06Map.get("dateField_ltse8sq6");
            if (null != dateFieldJsonObject) {
                JSONObject dateFieldData = dateFieldJsonObject.getJSONObject("fieldData");
                if (null != dateFieldData) {
                    Long value = dateFieldData.getLong("value");
                    if (null != value) {
                        String effectiveDate = DateUtils.format(new Date(value), "yyyy-MM-dd HH:mm:ss");
                        portalPriceParams.setEffectiveDate(effectiveDate);
                    }
                }
            }
            // 类型
            String type = classValueMap.get("textField_lwooo6bu").getJSONObject("fieldData").getString("value");

            String nurseFees = classValueMap.get("numberField_lwokgua1").getJSONObject("fieldData").getString("value");
            nurseFees = StringUtils.isBlank(nurseFees) ? "0" : nurseFees;
            // 总课时费
            String totalClassFees = classValueMap.get("numberField_ltse8sqv").getJSONObject("fieldData").getString("value");
            totalClassFees = StringUtils.isBlank(totalClassFees) ? "0" : totalClassFees;
            portalPriceParams.setTotalClassFees(totalClassFees);

            if ("全天".equals(type)) {
                // 保育费
                portalPriceParams.setNurseFees(nurseFees);
                // 总课时费
                portalPriceParams.setTotalFees((Double.parseDouble(bookFees) + Double.parseDouble(nurseFees) + Double.parseDouble(totalClassFees)) + "");
            } else {
                // 总课时费
                portalPriceParams.setNurseFees("0");
                portalPriceParams.setTotalFees((Double.parseDouble(bookFees) + Double.parseDouble(totalClassFees)) + "");
            }

            PortalPriceParams[] params = new PortalPriceParams[1];
            params[0] = portalPriceParams;

            PortalPriceWSProxy portalPriceWSProxy = new PortalPriceWSProxy();

            try {
                Result result = portalPriceWSProxy.createPortalPrice(params);
                System.out.println(result);
            } catch (RemoteException e) {
                throw new RuntimeException(e);
            }
        }
        return null;
    }

    public Object createCoupon(String id, String formUuId) {
        YiDaManager.YiDaResponse couponResponse = yiDaManager.getFormInstanceById(formUuId, id);
        if (!couponResponse.getOk() || null == couponResponse.getData()) {
            return null;
        }
        List<JSONObject> couponDataList = JSONArray.parseArray(couponResponse.getData().toString(), JSONObject.class);
        if (CollectionUtils.isEmpty(couponDataList)) {
            return null;
        }
        // 转化为map
        Map<String, JSONObject> couponMap = couponDataList.stream().collect(Collectors.toMap(jsonObject -> jsonObject.getString("fieldId"), Function.identity()));

        // 获取 processCode
        String processCode = couponMap.get("serialNumberField_ltwejsxb").getJSONObject("fieldData").getString("value");

        // 获取优惠券类型id
        String valueStr = couponMap.get("textField_lwr7fmw7").getJSONObject("fieldData").getString("value");

        // 获取优惠券类型名称
        String valueName = couponMap.get("selectField_ltwj8vk5").getJSONObject("fieldData").getString("value");

        // 获取申请人工号
        String applicantCode = couponMap.get("textField_lu2cry3a").getJSONObject("fieldData").getString("value");

        // 获取申请人名称
        String applicantName = couponMap.get("employeeField_lu2cry34").getJSONObject("fieldData").getJSONArray("value").getJSONObject(0).getString("label");

        // 获取优惠券
        JSONArray couponValues = couponMap.get("tableField_ltwj8vke").getJSONObject("fieldData").getJSONArray("value");
        if (CollectionUtils.isEmpty(couponValues)) {
            return null;
        }

        for (int i = 0; i < couponValues.size(); i++) {
            JSONArray couponValue = couponValues.getJSONArray(i);
            Map<String, JSONObject> couponValueMap = new HashMap<>();
            for (int j = 0; j < couponValue.size(); j++) {
                String fieldId = couponValue.getJSONObject(j).getString("fieldId");
                couponValueMap.put(fieldId, couponValue.getJSONObject(j));
            }

            // 获取折扣金额
            String discountAmt = couponValueMap.get("numberField_ltwj8vkt").getJSONObject("fieldData").getString("value");
            discountAmt = StringUtils.isBlank(discountAmt) ? "0" : discountAmt;

            // 获取学生 id
            String studentId = couponValueMap.get("textField_lwrx4sxc").getJSONObject("fieldData").getString("value");

            // 获取手机号
            String mobile = couponValueMap.get("textField_ltwj8vkf").getJSONObject("fieldData").getString("value");

            // 获取 classId
            String classId = couponValueMap.get("textField_lwr8xfcu").getJSONObject("fieldData").getString("value");

            // 根据 id 查询 brandId
            MClass mClass = classManager.getById(Long.parseLong(classId));
            String brandId = mClass.getBrandId() == null ? "" : mClass.getBrandId().toString();

            // 获取 orgCode
            String orgCode = couponValueMap.get("textField_lx1gtlsk").getJSONObject("fieldData").getString("value");

            PortalCouponWSProxy proxy = new PortalCouponWSProxy();
            PortalCouponParams params = new PortalCouponParams();
            params.setProcessCode(processCode);   // 未找到
            params.setValueStr(valueStr);  // 优惠券类型
            params.setOrgCode(orgCode);   // 不确定
            params.setBrandId(brandId);  // 没有放进去
            params.setValueName(valueName); // 优惠券类型
            params.setApplicantCode(applicantCode); // 申请人
            params.setApplicantName(applicantName);  // 申请人
            params.setDiscountAmt(discountAmt); // 折扣金额
            params.setStudentId(studentId);  // 学生 id
            params.setMobile(mobile);  // 手机号
            params.setClassId(classId);  // 班级 id
            params.setApplicableNum("0");
            try {
                com.shulead.datasync.portalcoupon.Result result =  proxy.createPortalCoupon(params);
                System.out.println("..");
            } catch (RemoteException e) {
                throw new RuntimeException(e);
            }
        }
        return null;
    }
}
