package weaver.interfaces.mfkj.action;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.company.DepartmentComInfo;
import weaver.hrm.job.JobTitlesComInfo;
import weaver.hrm.resource.ResourceComInfo;
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/8
 * @Version 1.0
 * @Description
 */
public class RecruitNeedAction 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 synch_url = getPropValue("syn_othersys_mfkj","synch_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");

        String recruiter_hr01 = getPropValue("syn_othersys_mfkj","recruiter_hr01");

        String recruiter_hr02 = getPropValue("syn_othersys_mfkj","recruiter_hr02");

        String recruiter_hr03 = getPropValue("syn_othersys_mfkj","recruiter_hr03");

        String recruiter_hr04 = getPropValue("syn_othersys_mfkj","recruiter_hr04");


        JSONArray jsonArray = new JSONArray();
        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();

        writeLog("========== syn Recruit  Need  start  ==========  "+ requestid);

        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 +"'");
                }
            }

            String dataSql = " select * from "+tablename+" where requestid="+requestid;
            rs.execute(dataSql);
            while(rs.next()) {
                JSONObject json = new JSONObject();
                //单据编号
                String djbh = Util.null2String(rs.getString("djbh"));
                //申请日期
                String sqrq = Util.null2String(rs.getString("sqrq"));
                //申请人
                String sqr = Util.null2String(rs.getString("sqr"));

                //招聘部门
                String zpbm = Util.null2String(rs.getString("zpbm"));
                String deptcode = new DepartmentComInfo().getDepartmentCode(zpbm);

                //岗位名称
                String gwmc = Util.null2String(rs.getString("gwmc"));
                String gwname = new JobTitlesComInfo().getJobTitlesname(gwmc);

                //用工类型 0 正式 1 劳务 2 实习
                String yglx = Util.null2String(rs.getString("yglx"));
                //招聘人数
                String zprs = Util.null2String(rs.getString("zprs"));
                //职级 0:P1 1:P1*  2:P2  3:P2*  4:P3  5:P3*  6:P4 7:P4* 8:P5  9:P5*  10:P6  11:P6*  12:P7
                String zj = Util.null2String(rs.getString("zj"));
                zj = getZjcode(zj);
                //需求原因 0 扩编型需求  1 替补型需求   2 替补型需求  3 组织架构型需求
                String xqyy = Util.null2String(rs.getString("xqyy"));
                xqyy = getXqyycode(xqyy);
                //直接上级
                String zjsj = Util.null2String(rs.getString("zjsj"));
                //工作地点
                String gzdd = Util.null2String(rs.getString("gzdd"));
                gzdd = getWorkCode(gzdd);
                //期望到岗时间
                String qwdgsj = Util.null2String(rs.getString("qwdgsj"));
                //岗位职责
                String gwzz = Util.null2String(rs.getString("gwzz"));
                //岗位要求
                String gwyq = Util.null2String(rs.getString("gwyq"));
                //备注说明
                String bzsm = Util.null2String(rs.getString("bzsm"));
                //工作时间 0 做五休二  1 做六休一
                String gzsj = Util.null2String(rs.getString("gzsj"));
                //考勤制 0 责任制 1 非责任制
                String kqz = Util.null2String(rs.getString("kqz"));
                //工资上限
                String gzsx = Util.null2String(rs.getString("gzsx"));
                //工资下限
                String gzxx = Util.null2String(rs.getString("gzxx"));
                //其他福利
                String qtfl = Util.null2String(rs.getString("qtfl"));
                //学历 0 初中及以下 1 中专 2 高中 3 大专 4 本科 5 硕士及以上
                String xl = Util.null2String(rs.getString("xl"));
                //性别 0 男 1 女  2 不限
                String xb = Util.null2String(rs.getString("xb"));
                if("0".equals(xb)){
                    xb = "0/599/604";
                }else{
                    xb = "0/599/605";
                }
                //年龄上限（最小）
                String nlxxzx = Util.null2String(rs.getString("nlxxzx"));
                //年龄上限（最大）
                String nlsxzd = Util.null2String(rs.getString("nlsxzd"));
                //工作经验（最短）
                String gzjyzd = Util.null2String(rs.getString("gzjyzd"));
                //工作经验（最长）
                String gzjyzc = Util.null2String(rs.getString("gzjyzc"));
                //期望工作地
                String qwgzd = Util.null2String(rs.getString("qwgzd"));
                //其他要求
                String qtyq = Util.null2String(rs.getString("qtyq"));
                //电脑需求 0 需要 1 不需要
                String dnxq = Util.null2String(rs.getString("dnxq"));
                //电脑数量
                String dnsl = Util.null2String(rs.getString("dnsl"));
                //备注说明2
                String bzsm2 = Util.null2String(rs.getString("bzsm2"));

                //Recruiter
                String recruiter = Util.null2String(rs.getString("recruiter"));
                if(recruiter.equals(recruiter_hr01)){
                    recruiter = "hr01";
                }else if(recruiter.equals(recruiter_hr02)){
                    recruiter = "hr02";
                }else if(recruiter.equals(recruiter_hr03)){
                    recruiter = "hr03";
                }else if(recruiter.equals(recruiter_hr04)){
                    recruiter = "hr04";
                }
                //背调 0 需要 1 不需要
                String bd = Util.null2String(rs.getString("bd"));
                if("0".equals(bd)){
                    bd = getIsOrNot("是");
                }else{
                    bd = getIsOrNot("否");
                }
                //测评 0 需要 1 不需要
                String cp = Util.null2String(rs.getString("cp"));
                if("0".equals(cp)){
                    cp = getIsOrNot("是");
                }else{
                    cp = getIsOrNot("否");
                }
                //对外发布名称
                String dwfbmc = Util.null2String(rs.getString("dwfbmc"));
                //招聘需求 0 校园 1 社会 2 内招 3 猎头 4 内推 5 实习生 6 海外
                String zpxq = Util.null2String(rs.getString("zpxq"));

                JSONArray jsonArray1 = new JSONArray();
                JSONObject json1 = new JSONObject();

                json.put("externalPostKey",requestid);
                json.put("isLongRelease","0");
                json.put("expectEntryDate",qwdgsj);
                json.put("recruiterCode",recruiter);
                json.put("externalName",dwfbmc);
                json.put("name",gwname);
                json.put("recruitNum",zprs);
                json.put("workPlace",gzdd);
                json.put("externalOrgKey",deptcode);

                JSONObject json2 = new JSONObject();
                json2.put("beidiao",bd);
                json2.put("ceping",cp);
                json2.put("zj",zj);
                json2.put("xqyy",xqyy);
                json.put("expend",json2);

                json1.put("gender",xb);
                json1.put("serviceCondition",gwyq);
                json1.put("recruitType",zpxq);
                json1.put("workContent",gwzz);
//                json1.put("education","");
//                json1.put("educationAbove","");
//                json1.put("subject","");
//                json1.put("workType","");
//                json1.put("recruitEmail","");

                jsonArray1.add(json1);

                json.put("recruitTypeDetailList",jsonArray1);

                jsonArray.add(json);
            }

            writeLog("=================== Recruit  Need  jsonArray ========================  "+ jsonArray.toString());

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

            boolean bool = HttpUtils.pushPostJsoninfo(synch_url,headers,jsonArray.toString(),"UTF-8");
            writeLog("========== syn Recruit  Need  result  ==========  "+ bool);
            if(bool){
                return Action.SUCCESS;
            }else{
                requestInfo.getRequestManager().setMessagecontent("招聘需求数据推送失败，请联系管理员！");
                return Action.FAILURE_AND_CONTINUE;
            }
        }catch (Exception e){
            e.printStackTrace();
            requestInfo.getRequestManager().setMessagecontent("招聘需求数据推送失败，请联系管理员！");
            return Action.FAILURE_AND_CONTINUE;
        }

    }

    /**
     * 是和否
     * @param name
     * @return
     */
    public static String getIsOrNot(String name){
        String result = "";
        RecordSet rs = new RecordSet();
        rs.executeQuery("select * from uf_shifou where zwmc='"+ name +"'");
        while(rs.next()){
            result = Util.null2String(rs.getString("code"));
        }
        return result;
    }

    /**
     * 职级 code
     * @param value
     * @return
     */
    public static String getZjcode(String value){
        String result = "";
        RecordSet rs = new RecordSet();
        String zj_fieldid = rs.getPropValue("syn_othersys_mfkj","zj_fieldid");
        rs.executeQuery("select * from uf_shifou where fieldid=? and zhi=?",zj_fieldid,value);
        while(rs.next()){
            result = Util.null2String(rs.getString("code"));
        }
        return result;
    }

    /**
     * 职级 code
     * @param value
     * @return
     */
    public static String getXqyycode(String value){
        String result = "";
        RecordSet rs = new RecordSet();
        String xqyy_fieldid = rs.getPropValue("syn_othersys_mfkj","xqyy_fieldid");
        rs.executeQuery("select * from uf_shifou where fieldid=? and zhi=?",xqyy_fieldid,value);
        while(rs.next()){
            result = Util.null2String(rs.getString("code"));
        }
        return result;
    }

    /**
     * 工作地点 code
     * @param name
     * @return
     */
    public static String getWorkCode(String name){
        String result = "";
        RecordSet rs = new RecordSet();
        String jdid = "";
        if(name.contains("_")){
            jdid = name.split("_")[1];
        }
        rs.executeQuery("select * from uf_gzdd where bjdid='"+ jdid +"'");
        while(rs.next()){
            result = Util.null2String(rs.getString("code"));
        }
        return result;
    }

}
