<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="weaver.conn.RecordSet" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="com.engine.kq.biz.KQFormatBiz" %>
<%@ page import="com.engine.kq.wfset.util.SplitActionUtil" %>
<%@ page import="weaver.general.BaseBean" %>
<%@ page import="com.alibaba.fastjson.JSONArray" %>


<%--
  User: wangj
  Design Ideas：
  铭沣-同步考勤打卡数据


--%>
<%@ page contentType="text/html;charset=UTF-8" %>
<%

    JSONObject data = new JSONObject();
    JSONArray datarr = new JSONArray();
    RecordSet rs = new RecordSet();
    boolean flag = false;
    BaseBean bb = new BaseBean();
    //外勤打卡原始记录id
    String wqdkysjlid = "";
    String sql = "select a.requestid,a.wqdkysjlid from formtable_main_632 a left join workflow_requestbase b on a.requestid = b.requestid where currentnodetype = '3' and wqdkysjlid is not null";
    rs.execute(sql);

    while (rs.next()){
        wqdkysjlid = Util.null2String(rs.getString("wqdkysjlid"));
        if(!"".equals(wqdkysjlid)){
            flag = true;
            updateKqRecord(wqdkysjlid);
            String requestid = Util.null2String(rs.getString("requestid"));
            datarr.add(requestid);
        }
    }

    data.put("flag", flag);
    data.put("datarr", datarr);
    out.print(data.toJSONString());
%>

<%!
    private void updateKqRecord(String wqdkysjlid){
        BaseBean bb = new BaseBean();
        String userId = "";
        String belongdate = "";
        RecordSet rs = new RecordSet();
        RecordSet rs1 = new RecordSet();

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


        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);

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

        }

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

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