package weaver.interfaces.workflow.action.javacode;

import com.engine.kq.biz.KQFormatBiz;
import com.wbi.util.Util;
import org.jsoup.helper.StringUtil;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashSet;
import java.util.Random;

/**
 * Online custom action interface  生产环境补卡流程
 */
public class Action20240312103913 extends BaseBean implements Action {
    /**
     * After selecting aciton after the process path node, this method will be executed after the node is submitted.
     */
    @Override
    public String execute(RequestInfo request) {
        try {
            String requestId = request.getRequestid();
            String tablename = request.getRequestManager().getBillTableName();
            RecordSet rs = new RecordSet();
            RecordSet rs1 = new RecordSet();
            String sql = "select ry , dd,rq,sj,lx from " + tablename + "_dt1 dt left join " +
                    tablename + " main on dt.mainid = main.id " +
                    "where main.requestid = ? ";
            String dmlsql = "insert into " +
                    "hrmschedulesign(userid,usertype,signtype,signdate,signtime,isincom,clientaddress,signfrom,addr) " +
                    "values(?,1,?,?,?,1,'60.30.92.222','e9mobile',?) ";
            rs.executeQuery(sql, requestId);
            KQFormatBiz kqFormatBiz = new KQFormatBiz();
            HashSet<String> rqSet = new HashSet<>();
            String mainry = "";
            while (rs.next()) {
                String ry = Util.null2String(rs.getString("ry"));
                String dd = Util.null2String(rs.getString("dd"));
                String rq = Util.null2String(rs.getString("rq"));
                String sj = Util.null2String(rs.getString("sj"));
                String lx = Util.null2String(rs.getString("lx"));
                mainry = ry;
                Random random = new Random();
                int randomNumber = random.nextInt(60) + 1; // 生成1到60的随机数
                rs1.executeUpdate(dmlsql, ry, lx, rq, sj + ":" + randomNumber, dd);
                rqSet.add(rq);

            }
            System.out.println("mainry:"+mainry);
            if (!StringUtil.isBlank(mainry)) {
                for (String rq : rqSet) {
                    System.out.println("执行更新考勤数据:"+mainry+"---"+rq);
                    kqFormatBiz.formatDate(mainry, rq);
                }

            }

            return Action.SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return Action.FAILURE_AND_CONTINUE;
        }

    }
}
