package nckd.jxccl.hr.gzbb;

import kd.bos.context.RequestContext;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.entity.datamodel.ListSelectedRow;
import kd.bos.entity.datamodel.ListSelectedRowCollection;
import kd.bos.form.ConfirmTypes;
import kd.bos.form.MessageBoxOptions;
import kd.bos.form.events.BeforeDoOperationEventArgs;
import kd.bos.form.operate.FormOperate;
import kd.bos.list.plugin.AbstractListPlugin;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.login.utils.DateUtils;
import kd.bos.message.api.MessageChannels;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.coderule.CodeRuleServiceHelper;
import kd.bos.servicehelper.operation.DeleteServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;
import kd.bos.servicehelper.workflow.MessageCenterServiceHelper;
import kd.bos.workflow.engine.msg.info.MessageInfo;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 央国企人事云》国资报表ext》单据》数据采集
 * 列表插件
 * nckd_sjcjrw
 * duanyuqi
 */
public class SjcjCheckFormListPlugin extends AbstractListPlugin {
    private static final Log logger = LogFactory.getLog(SjcjCheckFormListPlugin.class);

    @Override
    public void beforeDoOperation(BeforeDoOperationEventArgs args) {
        FormOperate formOperate = (FormOperate)args.getSource();
        ListSelectedRowCollection listRows = args.getListSelectedData(); // 获取列表选中行数据
        int saveResult = 0;
        String detail = "";
        if (listRows.size() > 0) {
            if ("nckd_fblbbtn".equals(formOperate.getOperateKey())) {// 发布
                for (int i = 0; i < listRows.size(); i++) {
                    ListSelectedRow listSelectedRow = listRows.get(i);
                    Object pkValue = listSelectedRow.getPrimaryKeyValue();
                    // 获取数据填报任务
                    DynamicObject dynamicObject = BusinessDataServiceHelper.loadSingle(pkValue, "nckd_sjcjrw");
                    String nckd_sjzt = dynamicObject.getString("nckd_sjzt");// 数据状态
                    if ("1".equals(nckd_sjzt)) { // 暂存
                        savePushToNextBill(dynamicObject);
                        dynamicObject.set("billstatus", "C");// 已审核
                        dynamicObject.set("nckd_sjzt", "2");// 已下发
                        SaveServiceHelper.save(new DynamicObject[]{dynamicObject});
                        saveResult ++;
                    } else {
                        detail = detail +"单据"+dynamicObject.getString("billno")+" 数据状态不是暂存 \r\n";
                    }
                }
                if (listRows.size() == saveResult) {
                    this.getView().showSuccessNotification("发布成功");
                } else {
                    this.getView().showConfirm(saveResult + "条数据处理成功，" + (listRows.size() - saveResult) + "条数据处理失败", detail, MessageBoxOptions.OK, ConfirmTypes.Save,null,new HashMap<>(),null);
                }
                this.getView().invokeOperation("refresh");
            } else if ("nckd_zflbbtn".equals(formOperate.getOperateKey())) {// 作废
                for (int i = 0; i < listRows.size(); i++) {
                    ListSelectedRow listSelectedRow = listRows.get(i);
                    Object pkValue = listSelectedRow.getPrimaryKeyValue();
                    // 获取数据填报任务
                    DynamicObject dynamicObject = BusinessDataServiceHelper.loadSingle(pkValue, "nckd_sjcjrw");
                    String nckd_sjzt = dynamicObject.getString("nckd_sjzt");// 数据状态
                    if ("2".equals(nckd_sjzt)) { // 已下发
                        // 作废数据填报单据
                        PushSjcjDataUtils.deleteSjtbBillData(dynamicObject);
                        // 作废数据采集单据
                        PushSjcjDataUtils.deleteSjcjBillData(dynamicObject);
                        dynamicObject.set("nckd_sjzt", "3");// 已作废
                        SaveServiceHelper.save(new DynamicObject[]{dynamicObject});
                        saveResult ++;
                    } else {
                        detail = detail +"单据"+dynamicObject.getString("billno")+" 数据状态不是已下发 \r\n";
                    }
                }
                if (listRows.size() == saveResult) {
                    this.getView().showSuccessNotification("作废成功");
                } else {
                    this.getView().showConfirm(saveResult + "条数据处理成功，" + (listRows.size() - saveResult) + "条数据处理失败", detail, MessageBoxOptions.OK, ConfirmTypes.Save,null,new HashMap<>(),null);
                }
                this.getView().invokeOperation("refresh");
            }
        }
        super.beforeDoOperation(args);
    }


    /**下推生成数据填报任务*/
    public void savePushToNextBill(DynamicObject dynamicObject) {
        // 获取任务配置分录
        DynamicObjectCollection dynamicObjectCollection = dynamicObject.getDynamicObjectCollection("nckd_sjcjrw_entry");
        for (DynamicObject dynamicObjectEntry: dynamicObjectCollection) {
            String nckd_sfscrw = dynamicObjectEntry.getString("nckd_sfscrw");// 是否生成下级单位填报任务1是0否
            DynamicObjectCollection nckd_tztbry = dynamicObjectEntry.getDynamicObjectCollection("nckd_tztbry");// 通知人员
            List<Long> receivers = nckd_tztbry.stream().map(tztbryObject -> tztbryObject.getLong("fbasedataid.id")).collect(Collectors.toList());
            if ("1".equals(nckd_sfscrw)) { // 需要生成任务
                PushSjcjDataUtils.savePushBillData(dynamicObject, dynamicObjectEntry);
            }
            PushSjcjDataUtils.savePushBillMsg(dynamicObject, receivers);
        }
    }

}
