package nckd.jimin.jyyy.hr.wtc.wtis.opplugin;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.args.AfterOperationArgs;
import kd.bos.entity.plugin.args.EndOperationTransactionArgs;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.sdk.plugin.Plugin;
import nckd.jimin.jyyy.hr.wtc.wtis.util.DingTalkSyncUtil;
import nckd.jimin.jyyy.hr.wtc.wtis.util.SyncAttendanceHelper;

/**
 * @description: 假勤类单据通用调用钉钉接口插件，包含：出差，休假，补签
 * @author Tyx
 * @date  2025/6/3
 */
public class CommonSyncDingTalkOpPlugin extends AbstractOperationServicePlugIn implements Plugin {

    private static Log log = LogFactory.getLog(CommonSyncDingTalkOpPlugin.class);


    @Override
    public void endOperationTransaction(EndOperationTransactionArgs e) {
        super.endOperationTransaction(e);
        DynamicObject[] bills = e.getDataEntities();
        for(DynamicObject bill : bills) {
            //SyncAttendanceHelper.execute(bill, "");
            //1：加班，2：出差，3：请假，4：补签
            String entityNumber = bill.getDataEntityType().getName();
            //根据entityNumber去commonparams里面找
            String bizType = DingTalkSyncUtil.getParamKey(entityNumber);
            log.info("{}准备执行同步钉钉，单据编号：{}, bizType： {}", entityNumber, bill.getString("billno"), bizType);
            //出差跟请假 这个参数没用 补签情况下，如果是变更就传true，
            if(entityNumber.equals("wtpm_supsignselfchange") || entityNumber.equals("wtpm_supsignpcchange")) {
                SyncAttendanceHelper.execute(bill, entityNumber, bizType, true);
            }
            else {
                SyncAttendanceHelper.execute(bill, entityNumber, bizType, false);
            }
        }
    }

    @Override
    public void afterExecuteOperationTransaction(AfterOperationArgs e) {
        super.afterExecuteOperationTransaction(e);
//        DynamicObject[] bills = e.getDataEntities();
//        for(DynamicObject bill : bills) {
//            //SyncAttendanceHelper.execute(bill, "");
//            //1：加班，2：出差，3：请假，4：补签
//            String entityNumber = bill.getDataEntityType().getName();
//            //根据entityNumber去commonparams里面找
//            String bizType = DingTalkSyncUtil.getParamKey(entityNumber);
//            log.info("{}准备执行同步钉钉，单据编号：{}, bizType： {}", entityNumber, bill.getString("billno"), bizType);
//            //出差跟请假 这个参数没用 补签情况下，如果是变更就传true，
//            if(entityNumber.equals("wtpm_supsignpcchange") || entityNumber.equals("wtpm_supsignpcchange")) {
//                SyncAttendanceHelper.execute(bill, entityNumber, bizType, true);
//            }
//            else {
//                SyncAttendanceHelper.execute(bill, entityNumber, bizType, false);
//            }
//        }
    }
}