package weaver.mfbdt.job;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.time.util.DateUtil;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.TimeUtil;
import weaver.general.Util;
import weaver.interfaces.schedule.BaseCronJob;
import weaver.mfbdt.util.DigestUtil;
import weaver.mfbdt.util.HttpClientUtil;
import weaver.mfbdt.util.SignUtils;

import java.util.*;

/**
 * 定时同步离职人员的到旷世考勤系统的计划任务
 * ·1、查询离职人员的工号，更具工号查出旷世系统的人员uuid
 * ·2、通过uuid调用接口同步信息
 *
 * @author wangj
 * @version 1.00版本
 * @Date 2023/9/11
 */

public class SyncLeaveOfficeUserJob extends BaseCronJob {
    @Override
    public void execute() {
        BaseBean bb = new BaseBean();
        // appkey,固定，
        String cappKey = Util.null2String(bb.getPropValue("ks_config","cappKey"));
        // 秘钥，固定
        String secret = Util.null2String(bb.getPropValue("ks_config","secret"));
        //ip端口 //http://10.122.101.181:18082
        String ipport = Util.null2String(bb.getPropValue("ks_config","ipport"));


        JSONArray uuidList = new JSONArray();


        //1、查询离职人员的工号，更具工号查出旷世系统的人员uuid
        String workcode = "";
        String sql = "select workcode from hrmresource where workcode != '' and status = '5'";
        RecordSet rs = new RecordSet();
        List<String> codeList = new ArrayList<>(1000);
        rs.execute(sql);
        while (rs.next()){
            workcode = Util.null2String(rs.getString("workcode"));
            codeList.add(workcode);
        }

        for(String code : codeList){
            String ctimestamp = DateUtil.getCurrentTime("yyyy-MM-dd HH:mm:ss");
            Date datec = TimeUtil.getString2Date(ctimestamp, "yyyy-MM-dd HH:mm:ss");
            long time = datec.getTime();
            ctimestamp = Util.object2String(time);
            // 时间戳，用户自己生成
            String cnonce = DigestUtil.getCnonce();
            // 6位随机码，用户自己生成
            String requestParam = "";
            //请求参数，form表单方式，鸿图大部分接口都是body传参，故这里大部分都是""，如果有参数，查看文档说明

            String url = Util.null2String(bb.getPropValue("ks_config","personSelect_url"));
            // 要请求的url,看要请求的接口说明
            String method = "POST";
            // 请求方式,看要请求的接口说明
//
            JSONObject requestBody = new JSONObject();
            requestBody.put("code",code);
            // 构建请求体，看要请求的接口说明
            // 计算签名
            String csign = SignUtils.sign(ctimestamp, cnonce, requestBody, requestParam, url, method, cappKey, secret);
            // 发起请求

            String realUrl = ipport + url;
            bb.writeLog("-SyncLeaveOfficeUserJob-select");
            bb.writeLog("-SyncLeaveOfficeUserJob-ctimestamp:"+ctimestamp);
            bb.writeLog("-SyncLeaveOfficeUserJob-cnonce:"+cnonce);
            bb.writeLog("-SyncLeaveOfficeUserJob-cappKey:"+cappKey);
            bb.writeLog("-SyncLeaveOfficeUserJob-csign:"+csign);
            bb.writeLog("-SyncLeaveOfficeUserJob-requestBody:"+requestBody);
            bb.writeLog("-SyncLeaveOfficeUserJob-url:"+url);

            Map<String,String> header = new HashMap<>(100);
            header.put("ctimestamp",ctimestamp);
            header.put("csign",csign);
            header.put("cnonce",cnonce);
            header.put("cappKey",cappKey);

            String json = requestBody.toJSONString();
            String result = HttpClientUtil.doPostJsonHeader(realUrl,json,header);
            bb.writeLog("-SyncLeaveOfficeUserJob-realUrl:"+realUrl);
            bb.writeLog("-SyncLeaveOfficeUserJob-json:"+json);
            bb.writeLog("-SyncLeaveOfficeUserJob-result:"+result);
            JSONObject resObj = JSONObject.parseObject(result);
            int rescode = (int) resObj.get("code");
            if(rescode == 0){
                JSONObject data = (JSONObject) resObj.get("data");
                JSONArray list = (JSONArray) data.get("list");
                if(list.size() > 0){

                    for (int i = 0; i < list.size(); i++) {
                        JSONObject listObj = (JSONObject) list.get(i);
                        String uuid = Util.null2String(listObj.get("uuid"));
                        String ucode = Util.null2String(listObj.get("code")) ;
                        //记录数据，后调用批量删除数据接口
                        if(ucode.equals(code)){
                            uuidList.add(uuid);
                        }
                    }
                }
            }
        }

        if (uuidList.size() > 0){
            //调用批量删除信息接口
            String ctimestamp = DateUtil.getCurrentTime("yyyy-MM-dd HH:mm:ss");
            Date datec = TimeUtil.getString2Date(ctimestamp, "yyyy-MM-dd HH:mm:ss");
            long time = datec.getTime();
            ctimestamp = Util.object2String(time);
            // 时间戳，用户自己生成
            String cnonce = DigestUtil.getCnonce();
            // 6位随机码，用户自己生成
            String requestParam = "";
            //请求参数，form表单方式，鸿图大部分接口都是body传参，故这里大部分都是""，如果有参数，查看文档说明
            String url = Util.null2String(bb.getPropValue("ks_config","personDetele_url"));
            // 要请求的url,看要请求的接口说明
            String method = "POST";
            // 请求方式,看要请求的接口说明
//
            JSONObject requestBody = new JSONObject();
            requestBody.put("uuidList",uuidList);
            // 构建请求体，看要请求的接口说明
            // 计算签名
            String csign = SignUtils.sign(ctimestamp, cnonce, requestBody, requestParam, url, method, cappKey, secret);
            // 发起请求

            String realUrl = ipport + url;
            bb.writeLog("-SyncLeaveOfficeUserJob-detele");
            bb.writeLog("-SyncLeaveOfficeUserJob-ctimestamp:"+ctimestamp);
            bb.writeLog("-SyncLeaveOfficeUserJob-cnonce:"+cnonce);
            bb.writeLog("-SyncLeaveOfficeUserJob-cappKey:"+cappKey);
            bb.writeLog("-SyncLeaveOfficeUserJob-csign:"+csign);
            bb.writeLog("-SyncLeaveOfficeUserJob-requestBody:"+requestBody);
            bb.writeLog("-SyncLeaveOfficeUserJob-url:"+url);

            Map<String,String> header = new HashMap<>(100);
            header.put("ctimestamp",ctimestamp);
            header.put("csign",csign);
            header.put("cnonce",cnonce);
            header.put("cappKey",cappKey);

            String json = requestBody.toJSONString();
            String result = HttpClientUtil.doPostJsonHeader(realUrl,json,header);
            bb.writeLog("-SyncLeaveOfficeUserJob-realUrl:"+realUrl);
            bb.writeLog("-SyncLeaveOfficeUserJob-json:"+json);
            bb.writeLog("-SyncLeaveOfficeUserJob-result:"+result);

        }
    }
}
