package weaver.interfaces.mfkj.action;

import com.alibaba.fastjson.JSONObject;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.mfkj.util.HttpUtils;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

/**
 * @Title
 * @Author wangchaofa
 * @CreateDate 2023/8/14
 * @Version 1.0
 * @Description
 */
public class ChangeApplyStatusAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        RecordSet rs = new RecordSet();
        RecordSet rs1 = new RecordSet();
        RecordSet rst = new RecordSet();

        String tablename = requestInfo.getRequestManager().getBillTableName();
        String requestid = requestInfo.getRequestid();

        //变更入职状态 接口 url
        String changeApplyStatus_url = getPropValue("syn_othersys_mfkj","changeApplyStatus_url");

        //企业编码
        String corpCode = getPropValue("syn_othersys_mfkj","corpCode");
        //用户名称
        String userName = getPropValue("syn_othersys_mfkj","userName");
        //加密密码
        String password = getPropValue("syn_othersys_mfkj","password");
        //获取 token  url
        String token_url = getPropValue("syn_othersys_mfkj","token_url");
        //uf_dy_token   modeuuid
        String token_uuid = getPropValue("syn_othersys_mfkj","token_uuid");

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("corpCode",corpCode);
        jsonObject.put("userName",userName);
        jsonObject.put("password",password);

        Map<String,Object> tokenMap = new HashMap<>();
        Map<String,String> headers = new HashMap<>();
        String encode = "utf-8";
        String token = "";
        String expire = "";

        Calendar cal1 = Calendar.getInstance();
        long now_time = cal1.getTimeInMillis();

        try {
            rst.executeQuery("select * from uf_dy_token where modeuuid='" + token_uuid + "'");
            rst.next();
            expire = Util.null2o(rst.getString("expire"));
            token = Util.null2o(rst.getString("token"));

            long timestamp = Long.parseLong(expire) * 1000;
            //token已过期，重新调用获取新的token
            if (now_time > timestamp) {
                tokenMap = HttpUtils.getTokenInfo(token_url, headers, jsonObject.toString(), encode);
                if (tokenMap.size() > 0) {
                    token = (String) tokenMap.get("token");
                    expire = (String) tokenMap.get("expire");
                    //更新token和有效时间戳
                    rs1.executeUpdate("update uf_dy_token set token='" + token + "',expire='" + expire + "' where modeuuid='" + token_uuid + "'");
                }
            }
            JSONObject json = new JSONObject();
            String dataSql = " select * from "+tablename+" where requestid="+requestid;
            rs.execute(dataSql);
            while(rs.next()) {
                String applyid = Util.null2String(rs.getString("applyid"));
                // 0 拒绝入职  1 未入职  2 已入职
                String rzzt = Util.null2String(rs.getString("rzzt"));
                String applyStatus = "";
                if("0".equals(rzzt)){
                    applyStatus = "-2";
                }
                if("1".equals(rzzt)){
                    applyStatus = "-1";
                }
                if("2".equals(rzzt)){
                    applyStatus = "21";
                }
                String rzrq = Util.null2String(rs.getString("rzrq"));
                String qtsm = Util.null2String(rs.getString("qtsm"));

                json.put("applyIds",applyid);
                json.put("applyStatus",applyStatus);
                json.put("entryDate",rzrq);
                json.put("remark",qtsm);

            }
            headers.put("Authorization", "Bearer "+token);

            boolean bool = HttpUtils.pushPostJsoninfo(changeApplyStatus_url,headers,json.toString(),"UTF-8");
            writeLog("========== change apply status  result  ==========  "+ bool);

        }catch (Exception e){
            e.printStackTrace();
            writeLog(e);
            requestInfo.getRequestManager().setMessagecontent("入职状态变更失败，请联系管理员！");
            return Action.FAILURE_AND_CONTINUE;
        }
        return Action.SUCCESS;
    }
}
