package com.weaver.seconddev.chapanda.beisen.util;

import com.alibaba.fastjson.JSONObject;
import com.weaver.seconddev.chapanda.beisen.constant.Constants;
import okhttp3.*;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.List;


@Component
public class BlackList2BeiSenUtil {
    private final static Logger log = LoggerFactory.getLogger(BlackList2BeiSenUtil.class);

    /***
     *
     * @param token
     * @return
     */
    public String createBlackList4NonSystem(String token,String name,String staffEmail,String blackAddReason,String blackStaffDesc){

        log.error("createBlackList4NonSystem");
        String objectId = "";

        // 选项名称        选项编码        启用状态
        //重大违纪        1        启用
        //诚信问题        2        启用
        //稳定性差        3        启用
        //辞职        4        启用
        //其他        5        启用
        JSONObject bodyJson = new JSONObject();
        JSONObject dataJson = new JSONObject();
        dataJson.put("name",name);
        dataJson.put("staffEmail",staffEmail);
        dataJson.put("blackAddReason",blackAddReason);
        dataJson.put("blackStaffDesc",blackStaffDesc);

//        dataJson.put("name","王芳");
//        dataJson.put("staffEmail","798989044@qq.com");
//        dataJson.put("blackAddReason","3");
//        dataJson.put("blackStaffDesc","做事不认真");

        bodyJson.put("data",dataJson);
        log.error("bodyJson:{}",bodyJson.toJSONString());

        // https://openapi.italent.cn/TenantBaseExternal/api/v5/BlackList/Create
        // "{\n  \"data\": {\n    \"name\": \"测试王芳\",\n    \"staffEmail\": \"798989044@qq.com\",\n    \"blackAddReason\":\"3\",\n    \"blackStaffDesc\":\"做事不认真\"\n  }\n}"

        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        MediaType mediaType = MediaType.parse("application/json");
        RequestBody body = RequestBody.create(mediaType, bodyJson.toJSONString());

        try {
            Request request = new Request.Builder()
                    .url(Constants.beishenHost+ Constants.createBlackListUrl)
                    .method("POST", body)
                    .addHeader("Authorization", "Bearer "+token)
                    .addHeader("Content-Type", "application/json")
                    .build();
            Response response = client.newCall(request).execute();

            int code = response.code();
            String msg = response.body().string();
            log.error("msg:{}",msg);
            log.error("code:{}",code);

            if(code == 200){
                if(StringUtils.isNotBlank(msg)){
                    JSONObject bodyData = JSONObject.parseObject(msg);
                    if(bodyData.containsKey("code")){
                        String dataCode = bodyData.getString("code");
                        if("200".equals(dataCode)){
                            JSONObject dataObject = bodyData.getJSONObject("data");
                            if(dataObject.containsKey("objectId")){
                                objectId = dataObject.getString("objectId");
                            }
                        }
                    }
                }
            }else{
                log.error("msg:"+msg);
            }

            log.error("objectId:{}",objectId);

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return objectId;
    }


    public String createBlackList4ResignUser(String token,String userID,String lastWorkDate,String blackAddReason,String blackStaffDesc){
        log.error("createBlackList4ResignUser");
        String objectId = "";

        // 选项名称        选项编码        启用状态
        //重大违纪        1        启用
        //诚信问题        2        启用
        //稳定性差        3        启用
        //辞职        4        启用
        //其他        5        启用

        JSONObject bodyJson = new JSONObject();
        JSONObject dataJson = new JSONObject();
        dataJson.put("userID",userID);
        dataJson.put("blackListSource","1");
        dataJson.put("lastWorkDate",lastWorkDate);
        dataJson.put("blackAddReason",blackAddReason);
        dataJson.put("blackStaffDesc",blackStaffDesc);

//        dataJson.put("userID","王芳");
//        dataJson.put("blackListSource","1");
//        dataJson.put("lastWorkDate","2025-05-30");
//        dataJson.put("blackAddReason","3");
//        dataJson.put("blackStaffDesc","做事不认真");

        bodyJson.put("data",dataJson);
        // https://openapi.italent.cn/TenantBaseExternal/api/v5/BlackList/Create
        // "{\n  \"data\": {\n    \"name\": \"测试王芳\",\n    \"staffEmail\": \"798989044@qq.com\",\n    \"blackAddReason\":\"3\",\n    \"blackStaffDesc\":\"做事不认真\"\n  }\n}"

        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        MediaType mediaType = MediaType.parse("application/json");
        RequestBody body = RequestBody.create(mediaType, bodyJson.toJSONString());

        try {
            Request request = new Request.Builder()
                    .url(Constants.beishenHost+Constants.createBlackListUrl)
                    .method("POST", body)
                    .addHeader("Authorization", "Bearer "+token)
                    .addHeader("Content-Type", "application/json")
                    .build();
            Response response = client.newCall(request).execute();

            int code = response.code();
            String msg = response.body().string();
            System.out.println("code:"+code);
            if(code == 200){
                if(StringUtils.isNotBlank(msg)){
                    JSONObject bodyData = JSONObject.parseObject(msg);
                    if(bodyData.containsKey("code")){
                        String dataCode = bodyData.getString("code");
                        if("200".equals(dataCode)){
                            JSONObject dataObject = bodyData.getJSONObject("data");
                            if(dataObject.containsKey("objectId")){
                                objectId = dataObject.getString("objectId");
                            }
                        }
                    }
                }
            }else{
                log.error("msg:"+msg);
            }

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return objectId;
    }

    /***
     * 批量删除黑名单
     * @param token
     * @param idList
     * @return
     */
    public String batchDeleteBlackList(String token,List<String> idList){

        String msg = "";
        JSONObject bodyJson = new JSONObject();
        bodyJson.put("Ids",idList);

        // https://openapi.italent.cn/TenantBaseExternal/api/v5/BlackList/BatchDelete
        // "{\n          \"Ids\":  [\n                    \"0441166f-468e-425f-9163-4a9d922169e3\"\n          ]\n}"

        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        MediaType mediaType = MediaType.parse("application/json");
        RequestBody body = RequestBody.create(mediaType, bodyJson.toJSONString());

        try {
            Request request = new Request.Builder()
                    .url(Constants.beishenHost+Constants.deleteBlackListUrl)
                    .method("DELETE", body)
                    .addHeader("Authorization", "Bearer "+token)
                    .addHeader("Content-Type", "application/json")
                    .build();
            Response response = client.newCall(request).execute();

            int code = response.code();
            if(code == 200 ){
                msg = response.body().string();
            }
            log.error("msg:"+msg);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return msg;
    }
}
