package weaver.mfbdt.action;

import com.alibaba.fastjson.JSONObject;
import com.engine.kq.biz.KQFormatBiz;
import com.engine.kq.wfset.util.SplitActionUtil;
import weaver.common.DateUtil;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.mfbdt.util.ActionsUtil;
import weaver.soa.workflow.request.RequestInfo;

import java.util.Map;

/**
 * 更新人员外勤打卡的考勤数据action
 *
 * @author wangj
 * @version 1.00版本
 * @Date 2023/9/19
 */

public class InsertOutSignKqDataAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {


        //外勤打卡原始记录id
        String wqdkysjlid = "";

        String userId = "";
        String belongdate = "";
        String requestid = requestInfo.getRequestid();
        writeLog("-InsertOutSignKqDataAction-requestid：" + requestid);
        RecordSet rs = new RecordSet();
        RecordSet rs1 = new RecordSet();
        //        获取主表数据
        Map mainInfo = ActionsUtil.getMainInfo(requestInfo);
        writeLog("-InsertOutSignKqDataAction-主表数据是：" + JSONObject.toJSONString(mainInfo));

        wqdkysjlid = Util.null2String(mainInfo.get("wqdkysjlid"));

        //同步更新考勤数据到考勤报表
        try {

            String sql = "select userId,userType,signType,signDate,signTime,clientAddress,isInCom,timeZone,belongdate,signfrom,longitude,latitude,addr,deviceInfo from uf_wqdkysjl where id in ("+wqdkysjlid+")";
            rs.execute(sql);
            while (rs.next()) {
                userId = Util.null2String(rs.getString("userId"));
                String userType = Util.null2String(rs.getString("userType"));
                String signType = Util.null2String(rs.getString("signType"));
                String signDate = Util.null2String(rs.getString("signDate"));
                String signTime = Util.null2String(rs.getString("signTime"));
                String clientAddress = Util.null2String(rs.getString("clientAddress"));
                String isInCom = Util.null2String(rs.getString("isInCom"));
                String timeZone = Util.null2String(rs.getString("timeZone"));
                belongdate = Util.null2String(rs.getString("belongdate"));
                String signfrom = Util.null2String(rs.getString("signfrom"));
                String longitude = Util.null2String(rs.getString("longitude"));
                String latitude = Util.null2String(rs.getString("latitude"));
                String addr = Util.null2String(rs.getString("addr"));
                String deviceInfo = Util.null2String(rs.getString("deviceInfo"));

                //插入数据到系统打卡签到表
                String punchSql = "insert into HrmScheduleSign(userId,userType,signType,signDate,signTime,clientAddress,isInCom,timeZone,belongdate,signfrom,longitude,latitude,addr,deviceInfo) " +
                        " values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
                boolean isok = rs1.executeUpdate(punchSql, userId, userType, signType, signDate, signTime, clientAddress, isInCom,
                        timeZone, belongdate, signfrom, longitude, latitude, addr, deviceInfo);

                writeLog("punchSql", punchSql);
                writeLog("punchSql_isok", isok);

            }

            if (!"".equals(userId) && !"".equals(belongdate)) {

                String yesterday = com.time.util.DateUtil.formatDate(com.time.util.DateUtil.getDateAfterDays(DateUtil.parseToDate(belongdate), -1), "yyyy-MM-dd");
                new KQFormatBiz().formatDate("" + userId, yesterday);
                new KQFormatBiz().formatDate("" + userId, (belongdate.length() == 0 ? DateUtil.getCurrentDate() : belongdate));
                //外勤签到转的考勤 处理加班规则
                SplitActionUtil.pushOverTimeTasksAll(belongdate, belongdate, "" + userId);
            }

        } catch (Exception e) {
            requestInfo.getRequestManager().setMessagecontent(e.getMessage());
            return FAILURE_AND_CONTINUE;
        }


        return SUCCESS;
    }


}
