package weaver.interfaces.shuzhi.action;

import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

/**
 * 更新待入职生成流程的员工gh
 *
 * @author wangj
 * @version 1.00版本
 * @Date 2023/7/4
 */

public class PendingEmploymentModeAction implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        BaseBean bb = new BaseBean();
        RecordSet rs = new RecordSet();
        String tableName = requestInfo.getRequestManager().getBillTableName();
        String lcid = requestInfo.getRequestid();
        String gzdw = "";
        String sfzh = "";
        String sql = "select gzdw,sfzh from " + tableName + " where requestid = '" + lcid + "'";
        rs.execute(sql);
        while (rs.next()) {
            gzdw = Util.null2String(rs.getString("gzdw"));
            sfzh = Util.null2String(rs.getString("sfzh"));
        }
        bb.writeLog("PendingEmploymentModeAction--------lcid-:" + lcid);
        bb.writeLog("PendingEmploymentModeAction--------gzdw-:" + gzdw);
        bb.writeLog("PendingEmploymentModeAction--------sfzh-:" + sfzh);


        if (!"".equals(lcid) && !"".equals(gzdw) && !"".equals(sfzh)) {

            String workcode = "";
            sql = "SELECT  " +
                    "CASE  " +
                    "    WHEN  " +
                    "        MAX( workcode ) IS NULL THEN  " +
                    "            (  " +
                    "                ( select subcompanycode from HrmSubCompany where id =  '" + gzdw + "' ) + RIGHT ( DATEPART ( yy, GETDATE ( ) ), 2 ) + '0001'   " +
                    "            ) ELSE cast( MAX( workcode ) AS INT ) + 1   " +
                    "        END AS 'workcode'   " +
                    "    FROM  " +
                    "        HrmResource   " +
                    "WHERE  " +
                    "    workcode LIKE ( select subcompanycode from HrmSubCompany where id =  '" + gzdw + "' ) + RIGHT ( DATEPART ( yy, GETDATE ( ) ), 2 ) + '%'";

            rs.execute(sql);
            while (rs.next()) {
                workcode = Util.null2String(rs.getString("workcode"));
            }

            if (!"".equals(workcode)) {
                updateWfdata(lcid, workcode, sfzh, tableName);
            }

        }
        return null;
    }

    private void updateWfdata(String lcid, String workcode, String sfzh, String tableName) {
        String sql = "update " + tableName + " set gh = '" + workcode + "' where requestid = '" + lcid + "'";
        RecordSet rs = new RecordSet();
        rs.execute(sql);

        sql = "update hrmresource set workcode = '" + workcode + "' where certificatenum= '" + sfzh + "' and status != '5'";
        rs.execute(sql);
        new BaseBean().writeLog("PendingEmploymentModeAction--updateWfdata sql:" + sql);

//        //入职确认 sfzh
//        sql = "update uf_rzgljmb set gh = '" + workcode + "' where sfzh = '" + sfzh + "'";
//        rs.execute(sql);
//
//        ////入职确认 uf_syqglbd
//        sql = "update uf_syqglbd set gh = '" + workcode + "' where sjid = '" + lcid + "'";
//        rs.execute(sql);
    }
}
