package weaver.mfbdt.job;

import com.time.util.DateUtil;
import com.wbi.util.Util;
import weaver.conn.RecordSet;
import weaver.interfaces.schedule.BaseCronJob;
import weaver.mfbdt.util.MfUtil;
import weaver.mfbdt.util.WorkflowCreateHandler;

import java.util.*;

/**
 * 定时统计外勤打卡数据创建外勤打卡审批流程
 *
 * @author wangj
 * @version 1.00版本
 * @Date 2023/11/6
 */

public class CreateWqWorkFlowJob extends BaseCronJob {
    @Override
    public void execute() {

        String date = DateUtil.formatDate(DateUtil.getDateAfterDays(new Date(), -1), "yyyy-MM-dd");
        String sql = "select sqr from uf_wqdkysjl where dkrq = '" + date + "' group by sqr";
        List<String> userList = new ArrayList<>(100);
        RecordSet rs = new RecordSet();
        rs.execute(sql);
        while (rs.next()) {
            String sqr = 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 = Util.null2String(rs.getString("id"));
                    gsmc = Util.null2String(rs.getString("gsmc"));
                    szzw = Util.null2String(rs.getString("szzw"));
                    szbm = Util.null2String(rs.getString("szbm"));
                    ygbh = Util.null2String(rs.getString("ygbh"));
                    wqdkysjlid = wqdkysjlid + "," + id;

                    Map<String, String> map = new HashMap<>(100);
                    dkrq = Util.null2String(rs.getString("dkrq"));
                    dksj = Util.null2String(rs.getString("dksj"));
                    dkwz = Util.null2String(rs.getString("dkwz"));
                    gzrq = Util.null2String(rs.getString("gzrq"));
                    bz = 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 = 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 (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 = 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 = Util.null2String(rs.getPropValue("kq_config","wq_wf_tablename"));
        rs.execute("select id from "+tableName+" where requestid = '"+requestid+"'");
        while (rs.next()){
            mainid = Util.null2String(rs.getString("id"));
        }

        for(Map<String,String> map : datalist){
            String dkrq = Util.null2String(map.get("dkrq"));
            String dksj = Util.null2String(map.get("dksj"));
            String dkwz = Util.null2String(map.get("dkwz"));
            String gzrq = Util.null2String(map.get("gzrq"));
            String bz = 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);
        }
    }


}
