<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="java.util.*" %>
<%@ page import="weaver.conn.RecordSet" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="weaver.mfbdt.util.MfUtil" %>
<%@ page import="com.time.util.DateUtil" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="weaver.mfbdt.util.WorkflowCreateHandler" %>


<%--
  User: wangj
  Design Ideas：
  铭沣-获取时间段内外勤打卡记录同步到原始外勤打卡审批表

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

    JSONObject data = new JSONObject();
    boolean flag = false;
    String sdate = Util.null2String(request.getParameter("sdate"));
    String edate = Util.null2String(request.getParameter("edate"));
    String userid = Util.null2String(request.getParameter("userid"));
    if ("".equals(sdate) || "".equals(edate) || "".equals(userid)) {
        data.put("flag", flag);
        out.print(data.toJSONString());
    }else{
        List<String> datelist = new ArrayList<>(100);
        datelist = dateUtil(sdate, edate);
        String sql = "";
        for (String datestr : datelist){
            sql = "select sqr from uf_wqdkysjl where dkrq = '" + datestr + "' and sqr = '"+userid+"' group by sqr";
            doJob(sql,datestr);
            flag = true;
            data.put("flag", flag);
        }
        out.print(data.toJSONString());
    }



%>

<%!
    public void doJob(String sql,String date){
        List<String> userList = new ArrayList<>(100);
        RecordSet rs = new RecordSet();
        rs.execute(sql);
        while (rs.next()) {
            String sqr = com.wbi.util.Util.null2String(rs.getString("sqr"));
            userList.add(sqr);
        }

        sql = "select * from uf_wqdkysjl where sqr = ? and dkrq = ?";


        String sqrq = DateUtil.getCurrentTime("yyyy-MM-dd");


        if (userList.size() > 0) {
            for (String sqr : userList) {
                String id = "";
                String gsmc = "";
                String szzw = "";
                String szbm = "";
                String ygbh = "";

                String dkrq = "";
                String dksj = "";
                String dkwz = "";
                String gzrq = "";
                String bz = "";
                String wqdkysjlid = "";
                String lastname = getLastname(sqr);
                List<Map<String, String>> detailList = new ArrayList<>(100);
                rs.executeQuery(sql, sqr, date);
                while (rs.next()) {
                    id = com.wbi.util.Util.null2String(rs.getString("id"));
                    gsmc = com.wbi.util.Util.null2String(rs.getString("gsmc"));
                    szzw = com.wbi.util.Util.null2String(rs.getString("szzw"));
                    szbm = com.wbi.util.Util.null2String(rs.getString("szbm"));
                    ygbh = com.wbi.util.Util.null2String(rs.getString("ygbh"));
                    wqdkysjlid = wqdkysjlid + "," + id;

                    Map<String, String> map = new HashMap<>(100);
                    dkrq = com.wbi.util.Util.null2String(rs.getString("dkrq"));
                    dksj = com.wbi.util.Util.null2String(rs.getString("dksj"));
                    dkwz = com.wbi.util.Util.null2String(rs.getString("dkwz"));
                    gzrq = com.wbi.util.Util.null2String(rs.getString("gzrq"));
                    bz = com.wbi.util.Util.null2String(rs.getString("bz"));
                    map.put("dkrq", dkrq);
                    map.put("dksj", dksj);
                    map.put("dkwz", dkwz);
                    map.put("gzrq", gzrq);
                    map.put("bz", bz);
                    detailList.add(map);
                }
                wqdkysjlid = wqdkysjlid.substring(1, wqdkysjlid.length());
                String createrid = sqr + "";
                String workflowId = com.wbi.util.Util.null2String(rs.getPropValue("kq_config", "outSign_wfid"));
                String requestName = MfUtil.getWorkflowname(workflowId);
                requestName = requestName + "-" + lastname + "-" + sqrq;
                String isNextFlow = "1";
                Map<String, String> requestMainMap = new HashMap<>();

                requestMainMap.put("sqr", createrid);
                requestMainMap.put("sqrq", sqrq);
                requestMainMap.put("gsmc", gsmc);
                requestMainMap.put("wqdkysjlid", wqdkysjlid);
                if (!"".equals(gsmc)) {
                    requestMainMap.put("gsmc", gsmc);
                }
                if (!"".equals(szzw)) {
                    requestMainMap.put("szzw", szzw);
                }

                if (!"".equals(szbm)) {
                    requestMainMap.put("szbm", szbm);
                }

                if (!"".equals(ygbh)) {
                    requestMainMap.put("ygbh", ygbh);
                }

                WorkflowCreateHandler workflowCreateHandler = new WorkflowCreateHandler();
                String requestid = workflowCreateHandler.workflowCreateByRequestMap(createrid, workflowId, requestName, isNextFlow, requestMainMap);
                if (com.wbi.util.Util.getIntValue(requestid) > 0) {
                    //更新明细数据
                    updateDetailData(detailList,requestid);
                }

            }
        }
    }
%>

<%!
    public String getLastname(String userid) {
        RecordSet rs = new RecordSet();
        String lastname = "";
        rs.execute("select lastname from hrmresource where id = '" + userid + "'");
        while (rs.next()) {
            lastname = com.wbi.util.Util.null2String(rs.getString("lastname"));
        }
        return lastname;
    }
%>

<%!
    public void updateDetailData(List<Map<String, String>> datalist, String requestid) {
        RecordSet rs = new RecordSet();
        String mainid = "";
        String tableName = com.wbi.util.Util.null2String(rs.getPropValue("kq_config", "wq_wf_tablename"));
        rs.execute("select id from " + tableName + " where requestid = '" + requestid + "'");
        while (rs.next()) {
            mainid = com.wbi.util.Util.null2String(rs.getString("id"));
        }

        for (Map<String, String> map : datalist) {
            String dkrq = com.wbi.util.Util.null2String(map.get("dkrq"));
            String dksj = com.wbi.util.Util.null2String(map.get("dksj"));
            String dkwz = com.wbi.util.Util.null2String(map.get("dkwz"));
            String gzrq = com.wbi.util.Util.null2String(map.get("gzrq"));
            String bz = com.wbi.util.Util.null2String(map.get("bz"));

            String sql = "insert into " + tableName + "_dt1 (mainid,dkrq,dksj,dkwz,gzrq,bz) values (?,?,?,?,?,?)";
            rs.executeUpdate(sql, mainid, dkrq, dksj, dkwz, gzrq, bz);
        }
    }
%>

<%!
    /**
     * @Description: 获取起止日期集合
     * @Param:
     * @return:
     */
    public static List<String> dateUtil(String start, String end) {
        List<String> list = new ArrayList<String>();
        Date sdate = DateUtil.parseDate(start, "yyyy-MM-dd");
        Date edate = DateUtil.parseDate(end, "yyyy-MM-dd");
        long s = sdate.getTime();
        long e = edate.getTime();

        Long oneDay = 1000 * 60 * 60 * 24l;

        while (s <= e) {
            sdate = new Date(s);
            System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(sdate));
            list.add(new SimpleDateFormat("yyyy-MM-dd").format(sdate));
            s += oneDay;
        }
        return list;
    }
%>



