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

import java.util.HashMap;
import java.util.Map;

import com.weaver.common.distribution.genid.IdGenerator;
import com.weaver.common.hrm.util.HrmContextUtil;
import com.weaver.common.i18n.tool.util.I18nContextUtil;
import com.weaver.ebuilder.form.client.ebform.data.EbFormDataDubboService;
import com.weaver.framework.rpc.annotation.RpcReference;
import com.weaver.seconddev.interfaces.workflow.util.CommonDevUtil;
import com.weaver.teams.domain.user.SimpleEmployee;
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 com.weaver.verupgrade.conn.RecordSet;
import com.weaver.verupgrade.general.Util;
import com.weaver.verupgrade.interfaces.workflow.action.Action;
import com.weaver.verupgrade.soa.workflow.request.Property;
import com.weaver.verupgrade.general.BaseBean;

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

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

    private static final Logger logger_f1f35583 = LoggerFactory.getLogger(BuQianKaAction.class);

    @Autowired
    private CommonDevUtil commonDevUtil;

    @RpcReference(group = "ebuilderform")
    private EbFormDataDubboService ebFormDataDubboService;

    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 {
            logger_f1f35583.error(String.valueOf("do action on request:" + request.getRequestid()));
            String requestid = request.getRequestid();
            String creater = request.getCreatorid();
            logger_f1f35583.error(String.valueOf("creater:" + creater));
            RecordSet rs = I18nContextUtil.getBean(RecordSet.class);
            String createsqlString = "select id  from   eteams.dbo.employee as  hrmresource where lastname ='" + creater + "'";
            logger_f1f35583.error(String.valueOf("createsqlString  ===  " + createsqlString));

            String poolname = CONN_TYPE.hrm.getType();
            // TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
            boolean b11 = rs.executeSql(createsqlString, poolname);

            logger_f1f35583.error(String.valueOf("createsqlString flag ===  " + b11));

            rs.next();
            String createrId = rs.getString("id");
            Map<String, String> mainTableDataMap = new HashMap<String, String>();
            // 获取表单主字段信息
            Property[] properties = request.getMainTableInfo().getProperty();
            for (int i = 0; i < properties.length; i++) {
                // 主表数据
                // 字段名
                String name = properties[i].getName().toLowerCase();
                // 值
                String value = Util.null2String(properties[i].getValue());
                mainTableDataMap.put(name, value);
            }
            String GH = Util.null2String((String) mainTableDataMap.get("gh"));
            String BKRQ = Util.null2String((String) mainTableDataMap.get("bkrq"));
            String Punchtype = "";
            if (Util.null2String((String) mainTableDataMap.get("liangkz")).equals("1")) {
                Punchtype = "2";
            }
            if (Util.null2String((String) mainTableDataMap.get("sikz")).equals("1")) {
                Punchtype = "4";
            }
            if (Util.null2String((String) mainTableDataMap.get("liukz")).equals("1")) {
                Punchtype = "6";
            }
            String Punchreason = "";
            String Punchtype1 = "";
            String TimeCardDate = "";
            String TimeCardTime = "";
            logger_f1f35583.error(String.valueOf("Punchtype:" + Punchtype));
            logger_f1f35583.error(String.valueOf("liangkz:" + mainTableDataMap.get("liangkz")));
            if (Util.null2String((String) mainTableDataMap.get("liangkz")).equals("1")) {
                if (!Util.null2String((String) mainTableDataMap.get("yyy1")).equals("")) {
                    TimeCardTime = Util.null2String((String) mainTableDataMap.get("ldate1"));
                    String time = Util.null2String((String) mainTableDataMap.get("ltime1"));
                    TimeCardDate = Util.null2String((String) mainTableDataMap.get("ls1"));
                    TimeCardTime = TimeCardTime + " " + time;
                    Punchreason = Util.null2String((String) mainTableDataMap.get("yyy1"));
                    Punchtype1 = Punchtype + "_1";
//                    String sql = "insert into e10_common.dbo.buka_log(requestid,gh,punchreason,punchtype,TimeCardDate,TimeCardTime,createrId,BKRQ) values" + "(" + requestid + ",'" + GH + "','" + Punchreason + "','" + Punchtype1 + "','" + TimeCardDate + "','" + TimeCardTime + "','" + createrId + "','" + BKRQ + "')";
                    Long uf_CustomFlow_formId = commonDevUtil.getObjId("buka_log");
                    logger_f1f35583.error("uf_CustomFlow_formId = " + uf_CustomFlow_formId);
                    SimpleEmployee currentEmployee = HrmContextUtil.getCurrentUser();
                    Long recodId = ebFormDataDubboService.addEmptyData(uf_CustomFlow_formId, currentEmployee);// 获取插入的空的建模id
                    String sql = "update e10_common.dbo.buka_log set requestid = " + requestid + ",gh = '" + GH + "',punchreason='" + Punchreason + "',punchtype='" + Punchtype1 + "',TimeCardDate='" + TimeCardDate + "',TimeCardTime='" + TimeCardTime + "',createrId='" + createrId + "',BKRQ='" + BKRQ + "' where id  =  " + recodId + " and delete_type = 0 ";
                    boolean b1 = rs.executeSql(sql, poolname);
                    logger_f1f35583.error(String.valueOf("sql1:" + sql));
//                    String poolname_z7 = CONN_TYPE.ebuilder.getType();
//                    /* *TODO  检测到insert语句 -> E9插入sql迁移到E10需要特殊处理请参考文档进行调整  https://www.e-cology.com.cn/sp/doc/docShare/1027709301045985331?tenantKey=t7akvdnf84
// TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源*/
//                    long generate = IdGenerator.generate();
//                    rs.executeSql(sql, poolname_z7,generate);
                }
                logger_f1f35583.error(String.valueOf("yyy2:" + mainTableDataMap.get("yyy2")));
                if (!Util.null2String((String) mainTableDataMap.get("yyy2")).equals("")) {
                    TimeCardTime = Util.null2String((String) mainTableDataMap.get("ldate2"));
                    String time = Util.null2String((String) mainTableDataMap.get("ltime2"));
                    TimeCardDate = Util.null2String((String) mainTableDataMap.get("ls2"));
                    Punchreason = Util.null2String((String) mainTableDataMap.get("yyy2"));
                    TimeCardTime = TimeCardTime + " " + time;
                    Punchtype1 = Punchtype + "_2";
//                    String sql = "insert into e10_common.dbo.buka_log(requestid,gh,punchreason,punchtype,TimeCardDate,TimeCardTime,createrId,BKRQ) values" + "(" + requestid + ",'" + GH + "','" + Punchreason + "','" + Punchtype1 + "','" + TimeCardDate + "','" + TimeCardTime + "','" + createrId + "','" + BKRQ + "')";
//                    logger_f1f35583.error(String.valueOf("sql2:" + sql));
//                    String poolname_ae = CONN_TYPE.ebuilder.getType();
//                    /* *TODO  检测到insert语句 -> E9插入sql迁移到E10需要特殊处理请参考文档进行调整  https://www.e-cology.com.cn/sp/doc/docShare/1027709301045985331?tenantKey=t7akvdnf84
// TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源*/
//                    long generate = IdGenerator.generate();
//                    rs.executeSql(sql, poolname_ae,generate);
                    Long uf_CustomFlow_formId1 = commonDevUtil.getObjId("buka_log");
                    logger_f1f35583.error("uf_CustomFlow_formId = " + uf_CustomFlow_formId1);
                    SimpleEmployee currentEmployee = HrmContextUtil.getCurrentUser();
                    Long recodId = ebFormDataDubboService.addEmptyData(uf_CustomFlow_formId1, currentEmployee);
                    String sql = "update e10_common.dbo.buka_log set requestid = " + requestid + ",gh = '" + GH + "',punchreason='" + Punchreason + "',punchtype='" + Punchtype1 + "',TimeCardDate='" + TimeCardDate + "',TimeCardTime='" + TimeCardTime + "',createrId='" + createrId + "',BKRQ='" + BKRQ + "' where id  =  " + recodId+ " and delete_type = 0 ";
                    boolean b1 = rs.executeSql(sql, poolname);
                    logger_f1f35583.error(String.valueOf("sql2:" + sql));
                    logger_f1f35583.error(String.valueOf("b1:" + b1));
                }
            }
            logger_f1f35583.error(String.valueOf("sikz:" + mainTableDataMap.get("sikz")));
            if (Util.null2String((String) mainTableDataMap.get("sikz")).equals("1")) {
                for (int i = 1; i <= 4; i++) {
                    if (!Util.null2String((String) mainTableDataMap.get("yyy" + (2 + i))).equals("")) {
                        TimeCardTime = Util.null2String((String) mainTableDataMap.get("sdate" + i));
                        String time = Util.null2String((String) mainTableDataMap.get("stime" + i));
                        TimeCardDate = Util.null2String((String) mainTableDataMap.get("ls" + (2 + i)));
                        Punchreason = Util.null2String((String) mainTableDataMap.get("yyy" + (2 + i)));
                        TimeCardTime = TimeCardTime + " " + time;
                        Punchtype1 = Punchtype + "_" + i;
//                        String sql = "insert into e10_common.dbo.buka_log(requestid,gh,punchreason,punchtype,TimeCardDate,TimeCardTime,createrId,BKRQ) values" + "(" + requestid + ",'" + GH + "','" + Punchreason + "','" + Punchtype1 + "','" + TimeCardDate + "','" + TimeCardTime + "','" + createrId + "','" + BKRQ + "')";
//                        logger_f1f35583.error(String.valueOf("sql3:" + sql));
//                        String poolname_oa = CONN_TYPE.ebuilder.getType();
//                        /* *TODO  检测到insert语句 -> E9插入sql迁移到E10需要特殊处理请参考文档进行调整  https://www.e-cology.com.cn/sp/doc/docShare/1027709301045985331?tenantKey=t7akvdnf84
// TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源*/
//                        long generate = IdGenerator.generate();
//                        rs.executeSql(sql, poolname_oa,generate);
                        Long uf_CustomFlow_formId2 = commonDevUtil.getObjId("buka_log");
                        logger_f1f35583.error("uf_CustomFlow_formId = " + uf_CustomFlow_formId2);
                        SimpleEmployee currentEmployee = HrmContextUtil.getCurrentUser();
                        Long recodId = ebFormDataDubboService.addEmptyData(uf_CustomFlow_formId2, currentEmployee);
                        String sql = "update e10_common.dbo.buka_log set requestid = " + requestid + ",gh = '" + GH + "',punchreason='" + Punchreason + "',punchtype='" + Punchtype1 + "',TimeCardDate='" + TimeCardDate + "',TimeCardTime='" + TimeCardTime + "',createrId='" + createrId + "',BKRQ='" + BKRQ + "' where id  =  " + recodId+ " and delete_type = 0 ";
                        boolean b1 = rs.executeSql(sql, poolname);
                        logger_f1f35583.error(String.valueOf("sql3:" + sql));
                        logger_f1f35583.error(String.valueOf("b1:" + b1));
                    }
                }
            }
            logger_f1f35583.error(String.valueOf("liukz:" + mainTableDataMap.get("liukz")));
            if (Util.null2String((String) mainTableDataMap.get("liukz")).equals("1")) {
                for (int i = 1; i <= 6; i++) {
                    if (!Util.null2String((String) mainTableDataMap.get("yyy" + (6 + i))).equals("")) {
                        TimeCardTime = Util.null2String((String) mainTableDataMap.get("sdate" + i));
                        String time = Util.null2String((String) mainTableDataMap.get("stime" + i));
                        TimeCardDate = Util.null2String((String) mainTableDataMap.get("ls" + (6 + i)));
                        Punchreason = Util.null2String((String) mainTableDataMap.get("yyy" + (6 + i)));
                        TimeCardTime = TimeCardTime + " " + time;
                        Punchtype1 = Punchtype + "_" + i;
//                        String sql = "insert into e10_common.dbo.buka_log(requestid,gh,punchreason,punchtype,TimeCardDate,TimeCardTime,createrId,BKRQ) values" + "(" + requestid + ",'" + GH + "','" + Punchreason + "','" + Punchtype1 + "','" + TimeCardDate + "','" + TimeCardTime + "','" + createrId + "','" + BKRQ + "')";
//                        logger_f1f35583.error(String.valueOf("sql4:" + sql));
//                        String poolname_31 = CONN_TYPE.ebuilder.getType();
//                        /* *TODO  检测到insert语句 -> E9插入sql迁移到E10需要特殊处理请参考文档进行调整  https://www.e-cology.com.cn/sp/doc/docShare/1027709301045985331?tenantKey=t7akvdnf84
// TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源*/
//                        long generate = IdGenerator.generate();
//                        rs.executeSql(sql, poolname_31,generate);
                        Long uf_CustomFlow_formId3 = commonDevUtil.getObjId("buka_log");
                        logger_f1f35583.error("uf_CustomFlow_formId = " + uf_CustomFlow_formId3);
                        SimpleEmployee currentEmployee = HrmContextUtil.getCurrentUser();
                        Long recodId = ebFormDataDubboService.addEmptyData(uf_CustomFlow_formId3, currentEmployee);
                        String sql = "update e10_common.dbo.buka_log set requestid = " + requestid + ",gh = '" + GH + "',punchreason='" + Punchreason + "',punchtype='" + Punchtype1 + "',TimeCardDate='" + TimeCardDate + "',TimeCardTime='" + TimeCardTime + "',createrId='" + createrId + "',BKRQ='" + BKRQ + "' where id  =  " + recodId+ " and delete_type = 0 ";
                        boolean b1 = rs.executeSql(sql, poolname);
                        logger_f1f35583.error(String.valueOf("sql4:" + sql));
                        logger_f1f35583.error(String.valueOf("b1:" + b1));
                    }
                }
            }
            logger_f1f35583.error(String.valueOf("stop BuQianKaAction"));
            return WeaResult.success(getResultMapForAction(weaverResultMap, "result", Action.SUCCESS, request.getRequestManager()));
        } catch (Exception e) {
            e.printStackTrace();
            logger_f1f35583.error("BuQianKaAction e: {}", e.getMessage());
            return  WeaResult.fail(500, "BuQianKaAction 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;
    }
}
