package nckd.jxccl.hr.gzbb;

import kd.bos.context.RequestContext;
import kd.bos.dataentity.entity.CloneUtils;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.dataentity.utils.StringUtils;
import kd.bos.login.utils.DateUtils;
import kd.bos.message.api.MessageChannels;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.AttachmentServiceHelper;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.QueryServiceHelper;
import kd.bos.servicehelper.coderule.CodeRuleServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;
import kd.bos.servicehelper.workflow.MessageCenterServiceHelper;
import kd.bos.web.actions.utils.FilePathUtil;
import kd.bos.workflow.engine.msg.info.MessageInfo;

import java.io.IOException;
import java.math.BigDecimal;
import java.net.URLDecoder;
import java.util.*;
import java.util.stream.Collectors;

/**数据采集单据工具类*/
public class PushSjcjDataUtils {

    /**推送消息*/
    public static void savePushBillMsg (DynamicObject dynamicObject, List<Long> receivers) {
        Date nckd_ssny = dynamicObject.getDate("nckd_ssny");// 所属年月
        String ssnyStr = DateUtils.formatDate(nckd_ssny, "yyyy年MM月");
        Date nckd_tbjzsj = dynamicObject.getDate("nckd_tbjzsj");// 填报截止时间
        String nckd_tbjzsjStr = DateUtils.formatDate(nckd_tbjzsj, "yyyy年MM月dd日");
        String billname = dynamicObject.getString("nckd_billname");// 任务名称
        String title = billname+"填报通知";
        String content = "请尽快完成"+ssnyStr+"的数据填报任务，截止时间为:"+nckd_tbjzsjStr;
        String senderName = RequestContext.get().getUserName();
        MessageInfo message = new MessageInfo();
        message.setType(MessageInfo.TYPE_MESSAGE);//推送消息的类型
        StringBuilder notifyType = new StringBuilder();
        notifyType.append(MessageChannels.MC);
        notifyType.append(MessageChannels.YUNZHIJIA);
        message.setNotifyType(notifyType.toString());
        message.setTitle(title);
        message.setUserIds(receivers);//接收用户
        message.setSenderName(senderName);
        message.setContent(content);//详情消息描述
        MessageCenterServiceHelper.sendMessage(message);//推送消息
    }

    /**推送消息*/
    public static void savePushDelBillMsg (DynamicObject dynamicObject, List<Long> receivers) {
        Date nckd_ssny = dynamicObject.getDate("nckd_ssny");// 所属年月
        String ssnyStr = DateUtils.formatDate(nckd_ssny, "yyyy年MM月");
        //Date nckd_tbjzsj = dynamicObject.getDate("nckd_tbjzsj");// 填报截止时间
        String billname = dynamicObject.getString("nckd_billname");// 任务名称
        String title = billname+"作废通知";
        String content = ssnyStr+"的数据填报任务已作废";
        String senderName = RequestContext.get().getUserName();
        MessageInfo message = new MessageInfo();
        message.setType(MessageInfo.TYPE_MESSAGE);//推送消息的类型
        StringBuilder notifyType = new StringBuilder();
        notifyType.append(MessageChannels.MC);
        notifyType.append(MessageChannels.YUNZHIJIA);
        message.setNotifyType(notifyType.toString());
        message.setTitle(title);
        message.setUserIds(receivers);//接收用户
        message.setSenderName(senderName);
        message.setContent(content);//详情消息描述
        MessageCenterServiceHelper.sendMessage(message);//推送消息
    }

    /**推送消息*/
    public static void savePushCuiBanMsg (DynamicObject dynamicObject, List<Long> receivers) {
        Date nckd_ssny = dynamicObject.getDate("nckd_tbsjny");// 所属年月
        String ssnyStr = DateUtils.formatDate(nckd_ssny, "yyyy年MM月");
        Date nckd_tbjzsj = dynamicObject.getDate("nckd_tbjzrq");// 填报截止时间
        String nckd_tbjzsjStr = DateUtils.formatDate(nckd_tbjzsj, "yyyy年MM月dd日");
        String billname = dynamicObject.getString("nckd_rwmc");// 任务名称
        String title = billname+"催办通知";
        String content = "请尽快完成"+ssnyStr+"的数据填报任务，截止时间为:"+nckd_tbjzsjStr;
        String senderName = RequestContext.get().getUserName();
        MessageInfo message = new MessageInfo();
        message.setType(MessageInfo.TYPE_MESSAGE);//推送消息的类型
        StringBuilder notifyType = new StringBuilder();
        notifyType.append(MessageChannels.MC);
        notifyType.append(MessageChannels.YUNZHIJIA);
        message.setNotifyType(notifyType.toString());
        message.setTitle(title);
        message.setUserIds(receivers);//接收用户
        message.setSenderName(senderName);
        message.setContent(content);//详情消息描述
        MessageCenterServiceHelper.sendMessage(message);//推送消息
    }

    /**生成填报任务单据*/
    public static void savePushBillData(DynamicObject sjcjObject, DynamicObject sjcjObjectEntry) {
        String billno = sjcjObject.getString("billno");// 数据采集编号
        Long billid = sjcjObject.getLong("id");// 源单id
        Date nckd_ssny = sjcjObject.getDate("nckd_ssny");// 所属年月
        String billname = sjcjObject.getString("nckd_billname");// 任务名称
        Date nckd_tbkssj = sjcjObject.getDate("nckd_tbkssj");// 填报开始时间
        Date nckd_tbjzrq = sjcjObject.getDate("nckd_tbjzsj");// 填报截止时间
        String nckd_sfjtxf = sjcjObject.getString("nckd_sfjtxf");// 是否初始下发
        String nckd_remark = sjcjObject.getString("nckd_remark");// 描述

        String nckd_dxbbmc = sjcjObjectEntry.getString("nckd_dxbbmc");// 填报报名称
        DynamicObjectCollection nckd_tztbry = sjcjObjectEntry.getDynamicObjectCollection("nckd_tztbry");// 通知人员
        DynamicObject nckd_tbxzzz = sjcjObjectEntry.getDynamicObject("nckd_tbxzzz");// 填报组织
        DynamicObject sjtbTaskObject = BusinessDataServiceHelper.newDynamicObject("nckd_sjtbrw");
        String sjtbBillNo = CodeRuleServiceHelper.getNumber("nckd_sjtbrw", sjtbTaskObject, null);
        sjtbTaskObject.set("billno", sjtbBillNo);// 单据编号
        sjtbTaskObject.set("billstatus", "A");// 暂存
        sjtbTaskObject.set("nckd_rwmc", billname);
        sjtbTaskObject.set("org", nckd_tbxzzz.getLong("id"));// 填报组织
        sjtbTaskObject.set("nckd_bblx", "1");// 报表类型
        sjtbTaskObject.set("nckd_tbsjny", nckd_ssny);// 数据填报年月

        DynamicObjectCollection userColl = sjtbTaskObject.getDynamicObjectCollection("nckd_zrr");
        for (DynamicObject user : nckd_tztbry) {
            DynamicObject userObject = user.getDynamicObject("fbasedataId");
            DynamicObject newUser = new DynamicObject(userColl.getDynamicObjectType());
            newUser.set("fbasedataId", userObject);
            userColl.add(newUser);
        }
        sjtbTaskObject.set("nckd_zrr", userColl);// 责任人

        String nckd_ydcbm = billno + "!";// 源单长编码
        if ("0".equals(nckd_sfjtxf)) {// 非集团下发
            String nckd_ydcbm_sjcj = sjcjObject.getString("nckd_ydcbm");// 源单长编码
            nckd_ydcbm = nckd_ydcbm_sjcj + billno + "!";
        }
        sjtbTaskObject.set("nckd_ydcbm", nckd_ydcbm);// 源单长编码
        sjtbTaskObject.set("nckd_ydbh", billno);// 源单编号
        sjtbTaskObject.set("nckd_ydpk", billid);// 源单ID
        sjtbTaskObject.set("nckd_ydst", "nckd_sjcjrw");// 源单实体
        sjtbTaskObject.set("nckd_sfyxscxjrw", "1");// 是否生成的填报任务0否1是
        sjtbTaskObject.set("nckd_rwzt", "1");// 任务状态，待上报
        sjtbTaskObject.set("nckd_asb", nckd_remark);// 描述
        if (nckd_tbkssj != null) {
            sjtbTaskObject.set("nckd_tbkssj", nckd_tbkssj);// 填报开始时间
        }
        sjtbTaskObject.set("nckd_tbjzrq", nckd_tbjzrq);// 填报截止日期
        if (nckd_dxbbmc.contains(",1,")) {
            sjtbTaskObject.set("nckd_zsba", "1");// 展示表a
        } else {
            sjtbTaskObject.set("nckd_zsba", "0");// 展示表a
        }
        if (nckd_dxbbmc.contains(",2,")) {
            sjtbTaskObject.set("nckd_zsbb", "1");// 展示表b
        } else {
            sjtbTaskObject.set("nckd_zsbb", "0");// 展示表b
        }
        if (nckd_dxbbmc.contains(",3,")) {
            sjtbTaskObject.set("nckd_zsbc", "1");// 展示表c
        } else {
            sjtbTaskObject.set("nckd_zsbc", "0");// 展示表c
        }
        if (nckd_dxbbmc.contains(",4,")) {
            sjtbTaskObject.set("nckd_zsbd", "1");// 展示表d
        } else {
            sjtbTaskObject.set("nckd_zsbd", "0");// 展示表d
        }
        if (nckd_dxbbmc.contains(",5,")) {
            sjtbTaskObject.set("nckd_zsbe", "1");// 展示表e
        } else {
            sjtbTaskObject.set("nckd_zsbe", "0");// 展示表e
        }
        if (nckd_dxbbmc.contains(",6,")) {
            sjtbTaskObject.set("nckd_zsbf", "1");// 展示表f
        } else {
            sjtbTaskObject.set("nckd_zsbf", "0");// 展示表f
        }
        if (nckd_dxbbmc.contains(",7,")) {
            sjtbTaskObject.set("nckd_zsbca", "1");// 展示表ca 财务表
        } else {
            sjtbTaskObject.set("nckd_zsbca", "0");// 展示表ca 财务表
        }
        if (nckd_dxbbmc.contains(",8,")) {
            sjtbTaskObject.set("nckd_zsbcb", "1");// 展示表cb 人事表
        } else {
            sjtbTaskObject.set("nckd_zsbcb", "0");// 展示表cb 人事表
        }
        Object[] result = SaveServiceHelper.save(new DynamicObject[]{sjtbTaskObject});
        DynamicObject dynamicObject = (DynamicObject) result[0];
        Long sjtbTaskId = dynamicObject.getLong("id");

        //通过AttachmentServiceHelper.getAttachments(formId, pkId, attachKey)获取源单据附件面板附件数据
        List<Map<String, Object>> attachmentData = AttachmentServiceHelper.getAttachments("nckd_sjcjrw", billid, "attachmentpanel");
        if (attachmentData.size() > 0) {
            //调用AttachmentServiceHelper.upload(formId, pkId, attachKey,  attachments)将附件数据上传到目标附件面板
            //AttachmentServiceHelper.upload("nckd_sjtbrw", sjtbTaskId, "nckd_atbsmfjmb", attachmentData);
            AttachFileUtils.copyFileFromAToB("nckd_sjcjrw", billid, "attachmentpanel", "nckd_sjtbrw", sjtbTaskId, "nckd_atbsmfjmb");
        }
    }

    /**获取文件服务器相对路径*/
    public static String getPathfromDownloadUrl(String url) throws IOException {
        String path = StringUtils.substringAfter(url, "path=");
        path = URLDecoder.decode(path, "UTF-8");
        return FilePathUtil.dealPath(path, "attach");
    }

    /**根据数据采集任务编号作废填报任务单据*/
    public static void deleteSjtbBillData(DynamicObject sjcjrwObject) {
        String billno = sjcjrwObject.getString("billno");
        List<QFilter> qfilter = new ArrayList<QFilter>();
        qfilter.add(new QFilter("nckd_ydcbm", "like", billno+"!%"));
        QFilter[] filters = (QFilter[]) qfilter.toArray(new QFilter[qfilter.size()]);
        DynamicObjectCollection xjtbrwObjects = QueryServiceHelper.query("nckd_sjtbrw", "id,billno", filters);
        if (xjtbrwObjects.size() > 0) {
            DynamicObject [] saveDynamicObjects = new DynamicObject[xjtbrwObjects.size()];
            List<Long> receiverList = new ArrayList<Long>();
            for (int i = 0; i < xjtbrwObjects.size(); i++) {
                DynamicObject sjtbrw = xjtbrwObjects.get(i);
                Long id = sjtbrw.getLong("id");
                DynamicObject dynamicObject = BusinessDataServiceHelper.loadSingle(id, "nckd_sjtbrw");
                dynamicObject.set("nckd_rwzt", "3");// 任务状态，作废
                saveDynamicObjects[i] = dynamicObject;
                DynamicObjectCollection nckd_zrr = dynamicObject.getDynamicObjectCollection("nckd_zrr");// 责任人
                List<Long> receivers = nckd_zrr.stream().map(zrrObject -> zrrObject.getLong("fbasedataid.id")).collect(Collectors.toList());
                receiverList.addAll(receivers);
            }
            SaveServiceHelper.save(saveDynamicObjects);
            PushSjcjDataUtils.savePushDelBillMsg(sjcjrwObject, receiverList);
        }
    }

    /**根据数据采集任务编号作废数据采集任务单据*/
    public static void deleteSjcjBillData(DynamicObject sjcjrwObject) {
        String billno = sjcjrwObject.getString("billno");
        List<QFilter> qfilter = new ArrayList<QFilter>();
        qfilter.add(new QFilter("nckd_ydcbm", "like", billno+"!%"));
        QFilter[] filters = (QFilter[]) qfilter.toArray(new QFilter[qfilter.size()]);
        DynamicObjectCollection xjtbrwObjects = QueryServiceHelper.query("nckd_sjcjrw", "id,billno", filters);
        if (xjtbrwObjects.size() > 0) {
            DynamicObject [] saveDynamicObjects = new DynamicObject[xjtbrwObjects.size()];
            List<Long> receiverList = new ArrayList<Long>();
            for (int i = 0; i < xjtbrwObjects.size(); i++) {
                DynamicObject sjtbrw = xjtbrwObjects.get(i);
                Long id = sjtbrw.getLong("id");
                DynamicObject dynamicObject = BusinessDataServiceHelper.loadSingle(id, "nckd_sjcjrw");
                dynamicObject.set("billstatus", "A");// 暂存
                dynamicObject.set("nckd_sjzt", "1");// 已作废
                saveDynamicObjects[i] = dynamicObject;
                // 获取任务配置分录
                DynamicObjectCollection dynamicObjectCollection = dynamicObject.getDynamicObjectCollection("nckd_sjcjrw_entry");
                for (DynamicObject dynamicObjectEntry: dynamicObjectCollection) {
                    DynamicObjectCollection nckd_tztbry = dynamicObjectEntry.getDynamicObjectCollection("nckd_tztbry");// 通知人员
                    List<Long> receivers = nckd_tztbry.stream().map(tztbryObject -> tztbryObject.getLong("fbasedataid.id")).collect(Collectors.toList());
                    receiverList.addAll(receivers);
                }
            }
            SaveServiceHelper.save(saveDynamicObjects);
            PushSjcjDataUtils.savePushDelBillMsg(sjcjrwObject, receiverList);
        }
    }

    /**合并数据填报任务单据*/
    public static boolean saveMergeSjtbBill (List<DynamicObject> dynamicObjectList) {
        // 需要获取到任务名称等信息
        DynamicObject firstObject = dynamicObjectList.get(0);
        String nckd_bblx = firstObject.getString("nckd_bblx");// 1填报表2汇总表
        String firstNckdYdpk = firstObject.getString("nckd_ydpk");// 源单ID
        String firstNckdYdst = firstObject.getString("nckd_ydst");// 源单实体
        String firstSourceBillName = "";
        Long firstSourceOrgId = 0L;
        Date firstNckdSsny = null;
        Date firstSourceNckdTbkssj = null;
        Date firstSourceNckdTbjzsj = null;
        String firstSourceNckdMs = "";
        DynamicObject sourceDynamicObject = null;
        if ("nckd_sjtbrw".equals(firstNckdYdst)) {
            if ("1".equals(nckd_bblx)) {// 填报表
                sourceDynamicObject = BusinessDataServiceHelper.loadSingle(Long.valueOf(firstNckdYdpk), "nckd_sjtbrw");
                firstSourceOrgId = sourceDynamicObject.getLong("org.id");// 填报组织
                firstSourceBillName = sourceDynamicObject.getString("nckd_rwmc");// 任务名称
                firstNckdSsny = sourceDynamicObject.getDate("nckd_tbsjny");// 所属年月
                firstSourceNckdTbkssj = sourceDynamicObject.getDate("nckd_tbkssj");// 填报开始时间
                firstSourceNckdTbjzsj = sourceDynamicObject.getDate("nckd_tbjzrq");// 填报截止时间
                firstSourceNckdMs = sourceDynamicObject.getString("nckd_asb");// 描述
            } else {// 汇总表
                sourceDynamicObject = dynamicObjectList.get(0);
                firstSourceOrgId = sourceDynamicObject.getLong("org.id");// 填报组织
                firstSourceBillName = sourceDynamicObject.getString("nckd_rwmc");// 任务名称
                firstNckdSsny = sourceDynamicObject.getDate("nckd_tbsjny");// 所属年月
                firstSourceNckdTbkssj = sourceDynamicObject.getDate("nckd_tbkssj");// 填报开始时间
                firstSourceNckdTbjzsj = sourceDynamicObject.getDate("nckd_tbjzrq");// 填报截止时间
                firstSourceNckdMs = sourceDynamicObject.getString("nckd_asb");// 描述
            }
        } else {
            sourceDynamicObject = BusinessDataServiceHelper.loadSingle(Long.valueOf(firstNckdYdpk), "nckd_sjcjrw");
            firstSourceOrgId = sourceDynamicObject.getLong("org.id");// 人事管理组织
            firstSourceBillName = sourceDynamicObject.getString("nckd_billname");// 单据名称
            firstSourceNckdTbkssj = sourceDynamicObject.getDate("nckd_tbkssj");// 填报开始时间
            firstSourceNckdTbjzsj = sourceDynamicObject.getDate("nckd_tbjzsj");// 填报截止时间
            firstSourceNckdMs = sourceDynamicObject.getString("nckd_remark");// 描述
            firstNckdSsny = sourceDynamicObject.getDate("nckd_ssny");// 所属年月
        }

        // 新建汇总单据
        DynamicObject sjhzTaskObject = BusinessDataServiceHelper.newDynamicObject("nckd_sjtbrw");
        String sjhzBillNo = CodeRuleServiceHelper.getNumber("nckd_sjtbrw", sjhzTaskObject, null);
        sjhzTaskObject.set("billno", sjhzBillNo);// 单据编号
        sjhzTaskObject.set("billstatus", "A");// 暂存
        sjhzTaskObject.set("nckd_rwmc", firstSourceBillName);
        sjhzTaskObject.set("org", firstSourceOrgId);// 填报组织
        sjhzTaskObject.set("nckd_bblx", "2");// 报表类型
        sjhzTaskObject.set("nckd_tbsjny", firstNckdSsny);// 数据填报年月
        DynamicObjectCollection userColl = sjhzTaskObject.getDynamicObjectCollection("nckd_zrr");

        DynamicObjectCollection firstSourceNckdZrr = new DynamicObjectCollection();
        if ("nckd_sjtbrw".equals(firstNckdYdst)) {
            firstSourceNckdZrr = sourceDynamicObject.getDynamicObjectCollection("nckd_zrr");// 责任人
        } else {
            DynamicObject userObject = sourceDynamicObject.getDynamicObject("creator");
            //DynamicObject userObject = BusinessDataServiceHelper.loadSingle(userPkId, "bos_user");
            DynamicObject newUser = new DynamicObject(userColl.getDynamicObjectType());
            newUser.set("fbasedataId", userObject);
            firstSourceNckdZrr.add(newUser);
        }
        for (DynamicObject user : firstSourceNckdZrr) {
            DynamicObject userObject = user.getDynamicObject("fbasedataId");
            DynamicObject newUser = new DynamicObject(userColl.getDynamicObjectType());
            newUser.set("fbasedataId", userObject);
            userColl.add(newUser);
        }
        sjhzTaskObject.set("nckd_zrr", userColl);// 责任人

        String nckd_ydcbm = sjhzBillNo + "!";// 源单长编码
        sjhzTaskObject.set("nckd_ydcbm", nckd_ydcbm);// 源单长编码
        sjhzTaskObject.set("nckd_ydbh", "");// 源单编号
        sjhzTaskObject.set("nckd_ydpk", "");// 源单ID
        sjhzTaskObject.set("nckd_ydst", "nckd_sjtbrw");// 源单实体
        sjhzTaskObject.set("nckd_sfyxscxjrw", "0");// 是否生成的填报任务0否1是
        sjhzTaskObject.set("nckd_rwzt", "1");// 任务状态，待上报
        if (firstSourceNckdTbkssj != null) {
            sjhzTaskObject.set("nckd_tbkssj", firstSourceNckdTbkssj);// 填报开始时间
        }
        sjhzTaskObject.set("nckd_tbjzrq", firstSourceNckdTbjzsj);// 填报截止日期
        sjhzTaskObject.set("nckd_asb", firstSourceNckdMs);// 描述
        //sjhzTaskObject.set("nckd_zsba", "1");// 展示表a
        sjhzTaskObject = saveMergeSjtbBillEntry(sjhzTaskObject, dynamicObjectList);
        DynamicObject [] saveDynamicObjects = new DynamicObject[1];
        saveDynamicObjects[0] = sjhzTaskObject;
        Object[] resultObject = SaveServiceHelper.save(saveDynamicObjects);
        // 正常汇总完成后，合并单位人工成本表财务表A与人事表B的数据
        saveMergeRgcbtjb(resultObject[0]);
        return true;
    }

    /**合并数据填报分录*/
    public static DynamicObject saveMergeSjtbBillEntry(DynamicObject sjhzTaskObject, List<DynamicObject> dynamicObjectList) {
        DynamicObject [] saveSourceDynamicObjects = new DynamicObject[dynamicObjectList.size()];
        List<String>  sourceBillNoList = new ArrayList<String>();
        for (int i = 0;i < dynamicObjectList.size(); i++) {
            DynamicObject dynamicObject = dynamicObjectList.get(i);
            dynamicObject.set("nckd_rwzt", "5");
            saveSourceDynamicObjects[i] = dynamicObject;
            sourceBillNoList.add(dynamicObject.getString("billno"));
            // 各级单位信息统计表
            DynamicObjectCollection nckdGjdwtjbentry = dynamicObject.getDynamicObjectCollection("nckd_gjdwtjbentry");
            if (nckdGjdwtjbentry.size() > 0) {
                sjhzTaskObject.set("nckd_zsba", "1");// 展示表a
                sjhzTaskObject = saveMergeEntry(sjhzTaskObject, nckdGjdwtjbentry, "nckd_gjdwtjbentry");
            }
            // 各级单位参股企业信息统计表
            DynamicObjectCollection nckdGjdwcgqyxxtjb = dynamicObject.getDynamicObjectCollection("nckd_dwcgxxentry");
            if (nckdGjdwcgqyxxtjb.size() > 0) {
                sjhzTaskObject.set("nckd_zsbb", "1");// 展示表b
                sjhzTaskObject = saveMergeEntry(sjhzTaskObject, nckdGjdwcgqyxxtjb, "nckd_dwcgxxentry");
            }
            // 各级单位人工成本统计表
            DynamicObjectCollection nckdDwrgcbentry = dynamicObject.getDynamicObjectCollection("nckd_dwrgcbentry");
            if (nckdDwrgcbentry.size() > 0) {
                sjhzTaskObject.set("nckd_zsbc", "1");// 展示表c
                sjhzTaskObject = saveMergeEntry(sjhzTaskObject, nckdDwrgcbentry, "nckd_dwrgcbentry");
            }
            // 各级单位职工收入情况统计表
            DynamicObjectCollection nckdDwzgsrentry = dynamicObject.getDynamicObjectCollection("nckd_dwzgsrentry");
            if (nckdDwzgsrentry.size() > 0) {
                sjhzTaskObject.set("nckd_zsbd", "1");// 展示表d
                sjhzTaskObject = saveMergeEntry(sjhzTaskObject, nckdDwzgsrentry, "nckd_dwzgsrentry");
            }

            // 各级单位劳动情况统计表
            DynamicObjectCollection nckdDwldqkentry = dynamicObject.getDynamicObjectCollection("nckd_dwldqkentry");
            if (nckdDwldqkentry.size() > 0) {
                sjhzTaskObject.set("nckd_zsbe", "1");// 展示表e
                sjhzTaskObject = saveMergeEntry(sjhzTaskObject, nckdDwldqkentry, "nckd_dwldqkentry");
            }

            // 企业负责人履职待遇、业务支出和社会保险统计表
            DynamicObjectCollection nckdDwdybxentry = dynamicObject.getDynamicObjectCollection("nckd_dwdybxentry");
            if (nckdDwdybxentry.size() > 0) {
                sjhzTaskObject.set("nckd_zsbf", "1");// 展示表f
                sjhzTaskObject = saveMergeEntry(sjhzTaskObject, nckdDwdybxentry, "nckd_dwdybxentry");
            }

            // 各级单位人工成本统计表-财务
            DynamicObjectCollection nckdDwrgcbentrycw = dynamicObject.getDynamicObjectCollection("nckd_dwrgcbentrycw");
            if (nckdDwrgcbentrycw.size() > 0) {
                sjhzTaskObject.set("nckd_zsbca", "1");// 展示表ca
                sjhzTaskObject = saveMergeEntry(sjhzTaskObject, nckdDwrgcbentrycw, "nckd_dwrgcbentrycw");
            }

            // 各级单位人工成本统计表-人事
            DynamicObjectCollection nckdDwrgcbentryrs = dynamicObject.getDynamicObjectCollection("nckd_dwrgcbentryrs");
            if (nckdDwrgcbentryrs.size() > 0) {
                sjhzTaskObject.set("nckd_zsbcb", "1");// 展示表cb
                sjhzTaskObject = saveMergeEntry(sjhzTaskObject, nckdDwrgcbentryrs, "nckd_dwrgcbentryrs");
            }
        }
        // 更改源单任务状态
        SaveServiceHelper.save(saveSourceDynamicObjects);
        //获取单据体对象并绑定源单单据编号
        DynamicObjectCollection dynamicObjects = (DynamicObjectCollection) sjhzTaskObject.get("nckd_glsjtbrw");
        for (String billNo : sourceBillNoList) {
            //创建一条单据体数据
            DynamicObject dynamicObjectEntry = dynamicObjects.addNew();
            dynamicObjectEntry.set("nckd_hzbh", billNo);
        }
        return sjhzTaskObject;
    }

    /**分录表合并*/
    public static DynamicObject saveMergeEntry(DynamicObject sjhzTaskObject, DynamicObjectCollection nckdGjdwtjbentry, String entryName) {
        CloneUtils cloneUtils = new CloneUtils(false, true);
        DynamicObjectCollection sjtbEntryCollection = (DynamicObjectCollection) sjhzTaskObject.get(entryName);
        for (DynamicObject dynamicObject : nckdGjdwtjbentry) {
            DynamicObject sjtbEntry = (DynamicObject) cloneUtils.clone(dynamicObject);
            sjtbEntryCollection.add(sjtbEntry);
        }
        return sjhzTaskObject;
    }

    /**删除汇总表，将所有绑定的单据的任务状态改为已上报*/
    public static boolean deleteSjcjHzBill(Object pkValue) {
        List<QFilter> qfilterQuery = new ArrayList<QFilter>();
        qfilterQuery.add(new QFilter("id", "=", pkValue));
        QFilter[] filtersQuery = (QFilter[]) qfilterQuery.toArray(new QFilter[qfilterQuery.size()]);
        DynamicObjectCollection sourceObjects =  QueryServiceHelper.query("nckd_sjtbrw", "id,nckd_glsjtbrw.nckd_hzbh", filtersQuery);
        String[] billNos = new String[sourceObjects.size()];
        for (int i = 0; i < sourceObjects.size(); i++) {
            billNos[i] = sourceObjects.get(i).getString("nckd_glsjtbrw.nckd_hzbh");
        }

        List<QFilter> qfilterSave = new ArrayList<QFilter>();
        qfilterSave.add(new QFilter("billno", "in", billNos));
        QFilter[] filtersSave = (QFilter[]) qfilterSave.toArray(new QFilter[qfilterSave.size()]);
        DynamicObject[] dynamicObject = BusinessDataServiceHelper.load("nckd_sjtbrw", "id,billno,billstatus,nckd_rwzt", filtersSave);
        for (int i = 0; i < dynamicObject.length; i++) {
            dynamicObject[i].set("nckd_rwzt", "2");
        }
        SaveServiceHelper.save(dynamicObject);
        return true;
    }

    /**合并人工成本统计表-1*/
    public static void saveMergeRgcbtjb(Object objectData) {
        boolean isUpdate = false;
        DynamicObject dynamicObject = (DynamicObject) objectData;
        Map<Long, SjtbRgcbtjVO> rgcbtjMap = new LinkedHashMap<Long, SjtbRgcbtjVO>();
        // 各级单位人工成本统计表
        DynamicObjectCollection nckdDwrgcbentry = dynamicObject.getDynamicObjectCollection("nckd_dwrgcbentry");
        if (nckdDwrgcbentry.size() > 0) {
            for (DynamicObject dynamicObjectEntry : nckdDwrgcbentry) {
                Long nckd_czzdata = dynamicObjectEntry.getLong("nckd_czzdata.id");
                SjtbRgcbtjVO sjtbRgcbtjVO = new SjtbRgcbtjVO();
                sjtbRgcbtjVO.setDataId(dynamicObjectEntry.getLong("id"));
                sjtbRgcbtjVO.setNckd_ctjny(dynamicObjectEntry.getDate("nckd_ctjny"));
                sjtbRgcbtjVO.setNckd_czqyxxdm(dynamicObjectEntry.getString("nckd_czqyxxdm"));
                sjtbRgcbtjVO.setNckd_czzdata(nckd_czzdata);
                sjtbRgcbtjVO.setNckd_clrzeys(dynamicObjectEntry.getBigDecimal("nckd_clrzeys"));
                sjtbRgcbtjVO.setNckd_crgcbys(dynamicObjectEntry.getBigDecimal("nckd_crgcbys"));
                sjtbRgcbtjVO.setNckd_cgzzeys(dynamicObjectEntry.getBigDecimal("nckd_cgzzeys"));
                sjtbRgcbtjVO.setNckd_cyysr(dynamicObjectEntry.getBigDecimal("nckd_cyysr"));
                sjtbRgcbtjVO.setNckd_clrze(dynamicObjectEntry.getBigDecimal("nckd_clrze"));
                sjtbRgcbtjVO.setNckd_cldsczz(dynamicObjectEntry.getBigDecimal("nckd_cldsczz"));
                sjtbRgcbtjVO.setNckd_cyyzcb(dynamicObjectEntry.getBigDecimal("nckd_cyyzcb"));
                sjtbRgcbtjVO.setNckd_crgcb(dynamicObjectEntry.getBigDecimal("nckd_crgcb"));
                sjtbRgcbtjVO.setNckd_cgzze(dynamicObjectEntry.getBigDecimal("nckd_cgzze"));
                sjtbRgcbtjVO.setNckd_cshbx(dynamicObjectEntry.getBigDecimal("nckd_cshbx"));
                sjtbRgcbtjVO.setNckd_czfgjj(dynamicObjectEntry.getBigDecimal("nckd_czfgjj"));
                sjtbRgcbtjVO.setNckd_czfbt(dynamicObjectEntry.getBigDecimal("nckd_czfbt"));
                sjtbRgcbtjVO.setNckd_cqynj(dynamicObjectEntry.getBigDecimal("nckd_cqynj"));
                sjtbRgcbtjVO.setNckd_cbcyl(dynamicObjectEntry.getBigDecimal("nckd_cbcyl"));
                sjtbRgcbtjVO.setNckd_cflfy(dynamicObjectEntry.getBigDecimal("nckd_cflfy"));
                sjtbRgcbtjVO.setNckd_cldbhf(dynamicObjectEntry.getBigDecimal("nckd_cldbhf"));
                sjtbRgcbtjVO.setNckd_cgfzc(dynamicObjectEntry.getBigDecimal("nckd_cgfzc"));
                sjtbRgcbtjVO.setNckd_cqtrgcb(dynamicObjectEntry.getBigDecimal("nckd_cqtrgcb"));
                sjtbRgcbtjVO.setNckd_clwpqf(dynamicObjectEntry.getBigDecimal("nckd_clwpqf"));
                sjtbRgcbtjVO.setNckd_cpjzgs(dynamicObjectEntry.getBigDecimal("nckd_cpjzgs"));
                sjtbRgcbtjVO.setNckd_cpjzgzgrs(dynamicObjectEntry.getBigDecimal("nckd_cpjzgzgrs"));
                sjtbRgcbtjVO.setNckd_cpjcyrs(dynamicObjectEntry.getBigDecimal("nckd_cpjcyrs"));
                sjtbRgcbtjVO.setNckd_cpjlwgrs(dynamicObjectEntry.getBigDecimal("nckd_cpjlwgrs"));
                sjtbRgcbtjVO.setNckd_cpjlwpqrs(dynamicObjectEntry.getBigDecimal("nckd_cpjlwpqrs"));
                sjtbRgcbtjVO.setNckd_crgcblrl(dynamicObjectEntry.getBigDecimal("nckd_crgcblrl"));
                sjtbRgcbtjVO.setNckd_crsfyl(dynamicObjectEntry.getBigDecimal("nckd_crsfyl"));
                sjtbRgcbtjVO.setNckd_cqyldscl(dynamicObjectEntry.getBigDecimal("nckd_cqyldscl"));
                sjtbRgcbtjVO.setNckd_cgyzbzzl(dynamicObjectEntry.getBigDecimal("nckd_cgyzbzzl"));
                sjtbRgcbtjVO.setType(0);
                rgcbtjMap.put(nckd_czzdata, sjtbRgcbtjVO);
            }
        }
        // 各级单位人工成本统计表-财务
        DynamicObjectCollection nckdDwrgcbentrycw = dynamicObject.getDynamicObjectCollection("nckd_dwrgcbentrycw");
        if (nckdDwrgcbentrycw.size() > 0) {
            for (DynamicObject dynamicObjectEntry : nckdDwrgcbentrycw) {
                Long nckd_czzdata = dynamicObjectEntry.getLong("nckd_cazzdata.id");
                SjtbRgcbtjVO sjtbRgcbtjVO = new SjtbRgcbtjVO();
                if (rgcbtjMap.get(nckd_czzdata) != null) {
                    sjtbRgcbtjVO = rgcbtjMap.get(nckd_czzdata);
                } else {
                    sjtbRgcbtjVO.setDataId(0L);
                }
                sjtbRgcbtjVO.setCwRow(dynamicObjectEntry.getInt("seq"));
                sjtbRgcbtjVO.setNckd_ctjny(dynamicObjectEntry.getDate("nckd_catjny"));
                sjtbRgcbtjVO.setNckd_czqyxxdm(dynamicObjectEntry.getString("nckd_cazqyxxdm"));
                sjtbRgcbtjVO.setNckd_czzdata(nckd_czzdata);
                sjtbRgcbtjVO.setNckd_clrzeys(dynamicObjectEntry.getBigDecimal("nckd_calrzeys"));
                sjtbRgcbtjVO.setNckd_crgcbys(dynamicObjectEntry.getBigDecimal("nckd_cargcbys"));
                sjtbRgcbtjVO.setNckd_cgzzeys(dynamicObjectEntry.getBigDecimal("nckd_cagzzeys"));
                sjtbRgcbtjVO.setNckd_cyysr(dynamicObjectEntry.getBigDecimal("nckd_cayysr"));
                sjtbRgcbtjVO.setNckd_clrze(dynamicObjectEntry.getBigDecimal("nckd_calrze"));
                sjtbRgcbtjVO.setNckd_cldsczz(dynamicObjectEntry.getBigDecimal("nckd_caldsczz"));
                sjtbRgcbtjVO.setNckd_cyyzcb(dynamicObjectEntry.getBigDecimal("nckd_cayyzcb"));
                sjtbRgcbtjVO.setNckd_crgcb(dynamicObjectEntry.getBigDecimal("nckd_cargcb"));
                sjtbRgcbtjVO.setNckd_cgzze(dynamicObjectEntry.getBigDecimal("nckd_cagzze"));
                sjtbRgcbtjVO.setNckd_cshbx(dynamicObjectEntry.getBigDecimal("nckd_cashbx"));
                sjtbRgcbtjVO.setNckd_czfgjj(dynamicObjectEntry.getBigDecimal("nckd_cazfgjj"));
                sjtbRgcbtjVO.setNckd_czfbt(dynamicObjectEntry.getBigDecimal("nckd_cazfbt"));
                sjtbRgcbtjVO.setNckd_cqynj(dynamicObjectEntry.getBigDecimal("nckd_caqynj"));
                sjtbRgcbtjVO.setNckd_cbcyl(dynamicObjectEntry.getBigDecimal("nckd_cabcyl"));
                sjtbRgcbtjVO.setNckd_cflfy(dynamicObjectEntry.getBigDecimal("nckd_caflfy"));
                sjtbRgcbtjVO.setNckd_cldbhf(dynamicObjectEntry.getBigDecimal("nckd_caldbhf"));

                sjtbRgcbtjVO.setNckd_cghjf(dynamicObjectEntry.getBigDecimal("nckd_caghjf"));
                sjtbRgcbtjVO.setNckd_cjypxjf(dynamicObjectEntry.getBigDecimal("nckd_cajypxjf"));
                sjtbRgcbtjVO.setNckd_cjsjcj(dynamicObjectEntry.getBigDecimal("nckd_cajsjcj"));
                sjtbRgcbtjVO.setNckd_cctfl(dynamicObjectEntry.getBigDecimal("nckd_cactfl"));
                sjtbRgcbtjVO.setNckd_cfhbfl(dynamicObjectEntry.getBigDecimal("nckd_cafhbfl"));

                sjtbRgcbtjVO.setNckd_cgfzc(dynamicObjectEntry.getBigDecimal("nckd_cagfzc"));
                sjtbRgcbtjVO.setNckd_cqtrgcb(dynamicObjectEntry.getBigDecimal("nckd_caqtrgcb"));
                sjtbRgcbtjVO.setType(1);
                rgcbtjMap.put(nckd_czzdata, sjtbRgcbtjVO);
            }
        }
        // 各级单位人工成本统计表-人事
        DynamicObjectCollection nckdDwrgcbentryrs = dynamicObject.getDynamicObjectCollection("nckd_dwrgcbentryrs");
        if (nckdDwrgcbentryrs.size() > 0) {
            for (DynamicObject dynamicObjectEntry : nckdDwrgcbentryrs) {
                Long nckd_czzdata = dynamicObjectEntry.getLong("nckd_cbzzdata.id");
                SjtbRgcbtjVO sjtbRgcbtjVO = new SjtbRgcbtjVO();
                if (rgcbtjMap.get(nckd_czzdata) != null) {
                    sjtbRgcbtjVO = rgcbtjMap.get(nckd_czzdata);
                } else {
                    sjtbRgcbtjVO.setDataId(0L);
                }
                sjtbRgcbtjVO.setRsRow(dynamicObjectEntry.getInt("seq"));
                sjtbRgcbtjVO.setNckd_ctjny(dynamicObjectEntry.getDate("nckd_cbtjny"));
                sjtbRgcbtjVO.setNckd_czqyxxdm(dynamicObjectEntry.getString("nckd_cbzqyxxdm"));
                sjtbRgcbtjVO.setNckd_czzdata(nckd_czzdata);
                sjtbRgcbtjVO.setNckd_cqtrgcb(dynamicObjectEntry.getBigDecimal("nckd_cbqtrgcb"));
                sjtbRgcbtjVO.setNckd_clwpqf(dynamicObjectEntry.getBigDecimal("nckd_cblwpqf"));
                sjtbRgcbtjVO.setNckd_cpjzgs(dynamicObjectEntry.getBigDecimal("nckd_cbpjzgs"));
                sjtbRgcbtjVO.setNckd_cpjzgzgrs(dynamicObjectEntry.getBigDecimal("nckd_cbpjzgzgrs"));
                sjtbRgcbtjVO.setNckd_cpjcyrs(dynamicObjectEntry.getBigDecimal("nckd_cbpjcyrs"));
                sjtbRgcbtjVO.setNckd_cpjlwgrs(dynamicObjectEntry.getBigDecimal("nckd_cbpjlwgrs"));
                sjtbRgcbtjVO.setNckd_cpjlwpqrs(dynamicObjectEntry.getBigDecimal("nckd_cbpjlwpqrs"));
                sjtbRgcbtjVO.setType(1);
                rgcbtjMap.put(nckd_czzdata, sjtbRgcbtjVO);
            }
        }
        // 开始合并数据-处理旧数据
        BigDecimal bfb = new BigDecimal("100");
        Set<Long> oldEntrySet = new HashSet<Long>();
        if (nckdDwrgcbentry.size() > 0) {
            for (DynamicObject dynamicObjectEntry : nckdDwrgcbentry) {
                Long nckd_czzdata = dynamicObjectEntry.getLong("nckd_czzdata.id");
                if (rgcbtjMap.get(nckd_czzdata) != null) {
                    oldEntrySet.add(nckd_czzdata);
                    SjtbRgcbtjVO sjtbRgcbtjVO = rgcbtjMap.get(nckd_czzdata);
                    if (sjtbRgcbtjVO.getType().intValue() == 0) {// 没有找到合并的来源记录
                        continue;
                    }
                    BigDecimal cargcb = sjtbRgcbtjVO.getNckd_crgcb();// 人工成本
                    BigDecimal calrze = sjtbRgcbtjVO.getNckd_clrze();// 利润总额
                    BigDecimal cayysr = sjtbRgcbtjVO.getNckd_cyysr();// 营业总收入
                    BigDecimal cldsczz = sjtbRgcbtjVO.getNckd_cldsczz();// 劳动生成总值
                    BigDecimal cpjcyrs = sjtbRgcbtjVO.getNckd_cpjcyrs();// 平均从业人数
                    if (calrze.compareTo(BigDecimal.ZERO) != 0) {
                        //人工成本利润率=人工成本/利润总额
                        BigDecimal crgcblrl = cargcb.divide(calrze, 9, BigDecimal.ROUND_HALF_UP).multiply(bfb);
                        sjtbRgcbtjVO.setNckd_crgcblrl(crgcblrl);
                    }
                    if (cayysr.compareTo(BigDecimal.ZERO) != 0) {
                        //人事费用率=人工成本/营业总收入
                        BigDecimal crsfyl = cargcb.divide(cayysr, 9, BigDecimal.ROUND_HALF_UP).multiply(bfb);
                        sjtbRgcbtjVO.setNckd_crsfyl(crsfyl);
                    }
                    if (cpjcyrs.compareTo(BigDecimal.ZERO) != 0) {
                        //全员劳动生产率=劳动生产总值/平均从业人数
                        BigDecimal cqyldscl = cldsczz.divide(cpjcyrs, 9, BigDecimal.ROUND_HALF_UP).multiply(bfb);
                        sjtbRgcbtjVO.setNckd_cqyldscl(cqyldscl);
                    }
                    //dynamicObjectEntry.set("nckd_ctjny", sjtbRgcbtjVO.getNckd_ctjny());
                    //dynamicObjectEntry.set("nckd_czqyxxdm", sjtbRgcbtjVO.getNckd_czqyxxdm());
                    //dynamicObjectEntry.set("nckd_czzdata", sjtbRgcbtjVO.getNckd_czzdata());
                    dynamicObjectEntry.set("nckd_crgcbys", sjtbRgcbtjVO.getNckd_crgcbys());
                    dynamicObjectEntry.set("nckd_cgzzeys", sjtbRgcbtjVO.getNckd_cgzzeys());
                    dynamicObjectEntry.set("nckd_cyysr", sjtbRgcbtjVO.getNckd_cyysr());
                    dynamicObjectEntry.set("nckd_clrzeys", sjtbRgcbtjVO.getNckd_clrzeys());
                    dynamicObjectEntry.set("nckd_clrze", sjtbRgcbtjVO.getNckd_clrze());
                    dynamicObjectEntry.set("nckd_cldsczz", sjtbRgcbtjVO.getNckd_cldsczz());
                    dynamicObjectEntry.set("nckd_cyyzcb", sjtbRgcbtjVO.getNckd_cyyzcb());
                    dynamicObjectEntry.set("nckd_crgcb", sjtbRgcbtjVO.getNckd_crgcb());
                    dynamicObjectEntry.set("nckd_cgzze", sjtbRgcbtjVO.getNckd_cgzze());
                    dynamicObjectEntry.set("nckd_cshbx", sjtbRgcbtjVO.getNckd_cshbx());
                    dynamicObjectEntry.set("nckd_czfgjj", sjtbRgcbtjVO.getNckd_czfgjj());
                    dynamicObjectEntry.set("nckd_czfbt", sjtbRgcbtjVO.getNckd_czfbt());
                    dynamicObjectEntry.set("nckd_cqynj", sjtbRgcbtjVO.getNckd_cqynj());
                    dynamicObjectEntry.set("nckd_cbcyl", sjtbRgcbtjVO.getNckd_cbcyl());
                    dynamicObjectEntry.set("nckd_cflfy", sjtbRgcbtjVO.getNckd_cflfy());
                    dynamicObjectEntry.set("nckd_cldbhf", sjtbRgcbtjVO.getNckd_cldbhf());
                    dynamicObjectEntry.set("nckd_cghjf", sjtbRgcbtjVO.getNckd_cghjf());
                    dynamicObjectEntry.set("nckd_cjypxjf", sjtbRgcbtjVO.getNckd_cjypxjf());
                    dynamicObjectEntry.set("nckd_cjsjcj", sjtbRgcbtjVO.getNckd_cjsjcj());
                    dynamicObjectEntry.set("nckd_cctfl", sjtbRgcbtjVO.getNckd_cctfl());
                    dynamicObjectEntry.set("nckd_cfhbfl", sjtbRgcbtjVO.getNckd_cfhbfl());
                    dynamicObjectEntry.set("nckd_cgfzc", sjtbRgcbtjVO.getNckd_cgfzc());
                    dynamicObjectEntry.set("nckd_cqtrgcb", sjtbRgcbtjVO.getNckd_cqtrgcb());
                    dynamicObjectEntry.set("nckd_clwpqf", sjtbRgcbtjVO.getNckd_clwpqf());
                    dynamicObjectEntry.set("nckd_cpjzgs", sjtbRgcbtjVO.getNckd_cpjzgs());
                    dynamicObjectEntry.set("nckd_cpjzgzgrs", sjtbRgcbtjVO.getNckd_cpjzgzgrs());
                    dynamicObjectEntry.set("nckd_cpjcyrs", sjtbRgcbtjVO.getNckd_cpjcyrs());
                    dynamicObjectEntry.set("nckd_cpjlwgrs", sjtbRgcbtjVO.getNckd_cpjlwgrs());
                    dynamicObjectEntry.set("nckd_cpjlwpqrs", sjtbRgcbtjVO.getNckd_cpjlwpqrs());
                    dynamicObjectEntry.set("nckd_crgcblrl", sjtbRgcbtjVO.getNckd_crgcblrl());
                    dynamicObjectEntry.set("nckd_crsfyl", sjtbRgcbtjVO.getNckd_crsfyl());
                    dynamicObjectEntry.set("nckd_cqyldscl", sjtbRgcbtjVO.getNckd_cqyldscl());
                    dynamicObjectEntry.set("nckd_cgyzbzzl", sjtbRgcbtjVO.getNckd_cgyzbzzl());
                    dynamicObjectEntry.set("nckd_lycwh", sjtbRgcbtjVO.getCwRow());
                    dynamicObjectEntry.set("nckd_lyrsh", sjtbRgcbtjVO.getRsRow());
                    isUpdate = true;
                }
            }
        }
        // 开始合并数据-处理新增数据
        Set<Long> keySet = rgcbtjMap.keySet();
        for (Long key : keySet) {
            if (oldEntrySet.contains(key)) {
                continue;
            }
            DynamicObject dynamicObjectEntry = nckdDwrgcbentry.addNew();
            SjtbRgcbtjVO sjtbRgcbtjVO = rgcbtjMap.get(key);
            BigDecimal cargcb = sjtbRgcbtjVO.getNckd_crgcb();// 人工成本
            BigDecimal calrze = sjtbRgcbtjVO.getNckd_clrze();// 利润总额
            BigDecimal cayysr = sjtbRgcbtjVO.getNckd_cyysr();// 营业总收入
            BigDecimal cldsczz = sjtbRgcbtjVO.getNckd_cldsczz();// 劳动生成总值
            BigDecimal cpjcyrs = sjtbRgcbtjVO.getNckd_cpjcyrs();// 平均从业人数
            if (calrze.compareTo(BigDecimal.ZERO) != 0) {
                //人工成本利润率=人工成本/利润总额
                BigDecimal crgcblrl = cargcb.divide(calrze, 9, BigDecimal.ROUND_HALF_UP).multiply(bfb);
                sjtbRgcbtjVO.setNckd_crgcblrl(crgcblrl);
            }
            if (cayysr.compareTo(BigDecimal.ZERO) != 0) {
                //人事费用率=人工成本/营业总收入
                BigDecimal crsfyl = cargcb.divide(cayysr, 9, BigDecimal.ROUND_HALF_UP).multiply(bfb);
                sjtbRgcbtjVO.setNckd_crsfyl(crsfyl);
            }
            if (cpjcyrs.compareTo(BigDecimal.ZERO) != 0) {
                //全员劳动生产率=劳动生产总值/平均从业人数
                BigDecimal cqyldscl = cldsczz.divide(cpjcyrs, 9, BigDecimal.ROUND_HALF_UP).multiply(bfb);
                sjtbRgcbtjVO.setNckd_cqyldscl(cqyldscl);
            }
            dynamicObjectEntry.set("nckd_ctjny", sjtbRgcbtjVO.getNckd_ctjny());
            dynamicObjectEntry.set("nckd_czqyxxdm", sjtbRgcbtjVO.getNckd_czqyxxdm());
            dynamicObjectEntry.set("nckd_czzdata", sjtbRgcbtjVO.getNckd_czzdata());
            dynamicObjectEntry.set("nckd_crgcbys", sjtbRgcbtjVO.getNckd_crgcbys());
            dynamicObjectEntry.set("nckd_cgzzeys", sjtbRgcbtjVO.getNckd_cgzzeys());
            dynamicObjectEntry.set("nckd_cyysr", sjtbRgcbtjVO.getNckd_cyysr());
            dynamicObjectEntry.set("nckd_clrzeys", sjtbRgcbtjVO.getNckd_clrzeys());
            dynamicObjectEntry.set("nckd_clrze", sjtbRgcbtjVO.getNckd_clrze());
            dynamicObjectEntry.set("nckd_cldsczz", sjtbRgcbtjVO.getNckd_cldsczz());
            dynamicObjectEntry.set("nckd_cyyzcb", sjtbRgcbtjVO.getNckd_cyyzcb());
            dynamicObjectEntry.set("nckd_crgcb", sjtbRgcbtjVO.getNckd_crgcb());
            dynamicObjectEntry.set("nckd_cgzze", sjtbRgcbtjVO.getNckd_cgzze());
            dynamicObjectEntry.set("nckd_cshbx", sjtbRgcbtjVO.getNckd_cshbx());
            dynamicObjectEntry.set("nckd_czfgjj", sjtbRgcbtjVO.getNckd_czfgjj());
            dynamicObjectEntry.set("nckd_czfbt", sjtbRgcbtjVO.getNckd_czfbt());
            dynamicObjectEntry.set("nckd_cqynj", sjtbRgcbtjVO.getNckd_cqynj());
            dynamicObjectEntry.set("nckd_cbcyl", sjtbRgcbtjVO.getNckd_cbcyl());
            dynamicObjectEntry.set("nckd_cflfy", sjtbRgcbtjVO.getNckd_cflfy());
            dynamicObjectEntry.set("nckd_cldbhf", sjtbRgcbtjVO.getNckd_cldbhf());
            dynamicObjectEntry.set("nckd_cghjf", sjtbRgcbtjVO.getNckd_cghjf());
            dynamicObjectEntry.set("nckd_cjypxjf", sjtbRgcbtjVO.getNckd_cjypxjf());
            dynamicObjectEntry.set("nckd_cjsjcj", sjtbRgcbtjVO.getNckd_cjsjcj());
            dynamicObjectEntry.set("nckd_cctfl", sjtbRgcbtjVO.getNckd_cctfl());
            dynamicObjectEntry.set("nckd_cfhbfl", sjtbRgcbtjVO.getNckd_cfhbfl());
            dynamicObjectEntry.set("nckd_cgfzc", sjtbRgcbtjVO.getNckd_cgfzc());
            dynamicObjectEntry.set("nckd_cqtrgcb", sjtbRgcbtjVO.getNckd_cqtrgcb());
            dynamicObjectEntry.set("nckd_clwpqf", sjtbRgcbtjVO.getNckd_clwpqf());
            dynamicObjectEntry.set("nckd_cpjzgs", sjtbRgcbtjVO.getNckd_cpjzgs());
            dynamicObjectEntry.set("nckd_cpjzgzgrs", sjtbRgcbtjVO.getNckd_cpjzgzgrs());
            dynamicObjectEntry.set("nckd_cpjcyrs", sjtbRgcbtjVO.getNckd_cpjcyrs());
            dynamicObjectEntry.set("nckd_cpjlwgrs", sjtbRgcbtjVO.getNckd_cpjlwgrs());
            dynamicObjectEntry.set("nckd_cpjlwpqrs", sjtbRgcbtjVO.getNckd_cpjlwpqrs());
            dynamicObjectEntry.set("nckd_crgcblrl", sjtbRgcbtjVO.getNckd_crgcblrl());
            dynamicObjectEntry.set("nckd_crsfyl", sjtbRgcbtjVO.getNckd_crsfyl());
            dynamicObjectEntry.set("nckd_cqyldscl", sjtbRgcbtjVO.getNckd_cqyldscl());
            dynamicObjectEntry.set("nckd_cgyzbzzl", sjtbRgcbtjVO.getNckd_cgyzbzzl());
            dynamicObjectEntry.set("nckd_lycwh", sjtbRgcbtjVO.getCwRow());
            dynamicObjectEntry.set("nckd_lyrsh", sjtbRgcbtjVO.getRsRow());
            isUpdate = true;
        }
        if (isUpdate) {
            dynamicObject.set("nckd_zsbc", "1");// 展示表c
            DynamicObject [] saveDynamicObjects = new DynamicObject[1];
            saveDynamicObjects[0] = dynamicObject;
            SaveServiceHelper.save(saveDynamicObjects);
        }
    }
}
