package com.weaver.seconddev.interfaces.workflow.action;

import com.ibm.icu.util.Calendar;
import  com.weaver.seconddev.interfaces.swfa.BillFieldUtil;
import java.util.Date;
import com.weaver.common.i18n.tool.util.I18nContextUtil;
import com.weaver.verupgrade.conn.CONN_TYPE;
import java.lang.*;
import java.util.*;
import com.weaver.verupgrade.workflow.request.RequestManager;
import com.weaver.esb.api.rpc.EsbServerlessRpcRemoteInterface;
import com.weaver.common.base.entity.result.WeaResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import com.weaver.verupgrade.soa.workflow.request.RequestInfo;
import java.util.Map;
import java.util.HashMap;
import com.weaver.verupgrade.conn.RecordSet;
import com.weaver.verupgrade.conn.RecordSetDataSource;
import com.weaver.verupgrade.general.BaseBean;
import com.weaver.verupgrade.general.Util;
import com.weaver.verupgrade.soa.workflow.request.Property;
import com.weaver.verupgrade.interfaces.workflow.action.Action;

@org.springframework.stereotype.Service("workflow_action_HTWLJiaBanAction")
public class HTWLJiaBanAction extends BaseBean implements Action, EsbServerlessRpcRemoteInterface {

    // 对应E9的请求信息对象
    @Autowired
    private RequestInfo requestInfoTemp;

    private static final Logger logger_983b20e3 = LoggerFactory.getLogger(HTWLJiaBanAction.class);

    public WeaResult<Map<String, Object>> execute(Map<String, Object> requestInfoMap) {
        Map<String, Object> weaverResultMap = new HashMap<>();
        Long requestIdTemp = Long.parseLong(String.valueOf(requestInfoMap.getOrDefault("requestid", -1)));
        Long userIdTemp = Long.parseLong(String.valueOf(requestInfoMap.getOrDefault("userid", -1)));
        RequestInfo request = requestInfoTemp.getRequestInfo(requestIdTemp, userIdTemp);
        try {
            String requestid = request.getRequestid();
            logger_983b20e3.error(String.valueOf("HTWLJiaBanAction--start--" + requestid));
            String wfid = request.getWorkflowid();
            BillFieldUtil util = new BillFieldUtil();
//            String tablename = util.getTablename(wfid);
            String tablename = request.getRequestManager().getBillTableName();
            Property[] aproperty = request.getMainTableInfo().getProperty();
            String jbrq = "";
            String jbrqx = "";
            String gh = "";
            // 实际加班结束日期
            String jbjsrq = "";
            String mSJJB_jsrq_x = "";
            String mGZSD = "";
            // 实际加班开始日期
            String mSJJB_ksrq = "";
            String mSJJB_ksrq_X = "";
            for (int i = 0; i < aproperty.length; i++) {
                String name = aproperty[i].getName().toUpperCase();
                String value = Util.null2String(aproperty[i].getValue());
                if ("JBRQ".equals(name)) {
                    // 加班日期
                    jbrq = value;
                    jbrqx = value.replaceAll("-", "");
                }
                if ("GH".equals(name)) {
                    // 工号
                    gh = value;
                }
                if ("SJJBJSJRQ".equals(name)) {
                    // 实际加班结束日期
                    jbjsrq = value;
                    mSJJB_jsrq_x = value.replaceAll("-", "");
                }
                // add by wangling
                if ("GZSD".equals(name)) {
                    // 工作时段
                    mGZSD = value;
                }
                if ("SJJBKSRQ".equals(name)) {
                    // 实际加班开始日期
                    mSJJB_ksrq = value;
                    mSJJB_ksrq_X = value.replaceAll("-", "");
                }
            }
            Calendar c = Calendar.getInstance();
            c.set(Integer.parseInt(jbjsrq.substring(0, 4)), Integer.parseInt(jbjsrq.substring(5, 7)) - 1, Integer.parseInt(jbjsrq.substring(8, 10)) + 1, 7, 0, 0);
            if (c.getTime().before(new Date())) {
                // 上午打卡记录
                String swdkjl = "";
                // 下午打卡记录
                String xwdkjl = "";
                // TODO 有参构造 :new RecordSetDataSource("onecard") 实例化方式改变,请检查是否有对应有参实现
//                RecordSetDataSource rsds = I18nContextUtil.getBean(RecordSetDataSource.class).setPoolname("onecard");
                RecordSet rsds = I18nContextUtil.getBean(RecordSet.class);
                if ("0".equals(mGZSD)) {
                    // 白班，根据实际加班开始日期 获取当天最早打卡和最晚打卡
                    String sql = "SELECt (SELECT min(AttendanceDateTime) FROM e10_common.dbo.view_attendance where AttendanceDate='" + mSJJB_ksrq_X + "' and UserNo='" + gh + "') as time1, " + " (SELECT max(AttendanceDateTime) FROM `view_attendance` where AttendanceDate='" + mSJJB_ksrq_X + "' and UserNo='" + gh + "') as time2";
                    logger_983b20e3.error(String.valueOf("查询打卡记录sql:" + sql));
                    String poolname_y6 = "";
                    // TODO E10必须指定poolname数据源
                    boolean b = rsds.executeSql(sql, "onecard");
                    logger_983b20e3.error(String.valueOf("查询打卡记录结果:" + b));
                } else if ("1".equals(mGZSD)) {
                    // 晚班，根据实际开始日期的最晚打卡时间，根据实际结束日期的最早打卡时间
                    String sql = "SELECt (SELECT max(AttendanceDateTime) FROM e10_common.dbo.view_attendance where AttendanceDate='" + mSJJB_ksrq_X + "' and UserNo='" + gh + "') as time1, " + " (SELECT min(AttendanceDateTime) FROM `view_attendance` where AttendanceDate='" + mSJJB_jsrq_x + "' and UserNo='" + gh + "') as time2";
                    logger_983b20e3.error(String.valueOf("查询打卡记录sql:" + sql));
                    String poolname_g7 = "";
                    // TODO E10必须指定poolname数据源
                    boolean b = rsds.executeSql(sql, "onecard");
                    logger_983b20e3.error(String.valueOf("查询打卡记录结果:" + b));
                } else {
                    // RecordSetDataSource rsds = new RecordSetDataSource("onecard");
                    String sql = "SELECt (SELECT min(AttendanceDateTime) FROM e10_common.dbo.view_attendance where AttendanceDate='" + jbrqx + "' and UserNo='" + gh + "' and AttendanceDateTime<'" + jbrq + " 12:00:00" + "') as time1, " + " (SELECT max(AttendanceDateTime) FROM `view_attendance` where AttendanceDate='" + jbrqx + "' and UserNo='" + gh + "' and AttendanceDateTime>'" + jbrq + " 12:00:00" + "') as time2";
                    logger_983b20e3.error(String.valueOf("查询打卡记录sql:" + sql));
                    String poolname = "";
                    // TODO E10必须指定poolname数据源
                    boolean b = rsds.executeSql(sql, "onecard");
                    logger_983b20e3.error(String.valueOf("查询打卡记录结果:" + b));
                    // String swdkjl = "";//上午打卡记录
                    // String xwdkjl = "";//下午打卡记录
                    // while (rsds.next()) {
                    // swdkjl = Util.null2String(rsds.getString("time1"));
                    // xwdkjl = Util.null2String(rsds.getString("time2"));
                    // }
                    // if (!"".equals(swdkjl) && swdkjl.length() > 19) {
                    // swdkjl = swdkjl.substring(0, 19);
                    // }
                    // if (!"".equals(xwdkjl) && xwdkjl.length() > 19) {
                    // xwdkjl = xwdkjl.substring(0, 19);
                    // }
                    // RecordSet rs = new RecordSet();
                    // rs.executeSql("update " + tablename + " set jbdtzzdksj='" + swdkjl + "',jbdtzwdksj='" + xwdkjl + "' where requestId=" + requestid);
                }
                while (rsds.next()) {
                    swdkjl = Util.null2String(rsds.getString("time1"));
                    xwdkjl = Util.null2String(rsds.getString("time2"));
                }
                if (!"".equals(swdkjl) && swdkjl.length() > 19) {
                    swdkjl = swdkjl.substring(0, 19);
                }
                if (!"".equals(xwdkjl) && xwdkjl.length() > 19) {
                    xwdkjl = xwdkjl.substring(0, 19);
                }
                RecordSet rs = I18nContextUtil.getBean(RecordSet.class);
                String poolname_ux = CONN_TYPE.workflow.getType();
                // TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
                boolean b = rs.executeSql("update e10_core_business.dbo." + tablename + " set jbdtzzdksj='" + swdkjl + "',jbdtzwdksj='" + xwdkjl + "' where id=" + requestid, poolname_ux);
                logger_983b20e3.error(String.valueOf("HTWLJiaBanAction update:" + b));
            } else {
                request.getRequestManager().setMessageid("Error");
                request.getRequestManager().setMessagecontent("请在实际加班结束日期次日7点后提交！");
                return  WeaResult.fail(500, "请在实际加班结束日期次日7点后提交！", getResultMapForAction(weaverResultMap, "result","请在实际加班结束日期次日7点后提交！", request.getRequestManager()));

            }
            logger_983b20e3.error(String.valueOf("HTWLJiaBanAction--end--" + requestid));
            return WeaResult.success(getResultMapForAction(weaverResultMap, "result", Action.SUCCESS, request.getRequestManager()));
        } catch (Exception e) {
            e.printStackTrace();
            logger_983b20e3.error("HTWLJiaBanAction e: {}", e.getMessage());
            return  WeaResult.fail(500, "HTWLJiaBanAction e: {}"+ e, getResultMapForAction(weaverResultMap, "result",e.getMessage(), request.getRequestManager()));
        }
    }

    Map<String, Object> getResultMapForAction(Map<String, Object> map, String key, Object value, RequestManager requestManager) {
        if (key != null && !key.isEmpty()) {
            map.put(key, value);
        }
        String msgContent = requestManager.getMessagecontent();
        if (msgContent != null && !msgContent.isEmpty()) {
            map.put("msgContent", msgContent);
        }
        String msgId = requestManager.getMessageid();
        if (msgId != null && !msgId.isEmpty()) {
            map.put("msgId", msgId);
        }
        return map;
    }
}
