package com.jeeplus.modules.wexinpackage.access.util.access;

import com.google.gson.Gson;
import com.jeeplus.modules.wexinpackage.access.entity.WeChatUser;
import com.jeeplus.modules.workstaff.entity.WorkStaffBasicInfo;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class AddressBookUtil {
    private static Logger log = (Logger) LoggerFactory.getLogger(RequestAccess.class);

    public static WeChatUser replaceUser(WorkStaffBasicInfo workStaffBasicInfo){
        WeChatUser user1 = new WeChatUser();
        user1.setUserid(workStaffBasicInfo.getNo());
        user1.setName(workStaffBasicInfo.getName());
        user1.setDepartment(2);
        user1.setMobile(workStaffBasicInfo.getMobile());
        user1.setEmail(workStaffBasicInfo.getEmail());
        user1.setGender(workStaffBasicInfo.getGender());
        user1.setPosition(workStaffBasicInfo.getDuty());
        return user1;
    }

    //1.创建成员
    public static void createUser(String accessToken,WeChatUser weChatUser) {
        //1.获取json字符串：将user对象转换为json字符串
        Gson gson = new Gson();
        String jsonU1 =gson.toJson(weChatUser);//使用gson.toJson(user)即可将user对象顺序转成json
        //2.获取请求的url
        String createUser = WeChatParamsUtil.CREATE_USER.replace("ACCESS_TOKEN", accessToken);
        //3.调用接口，发送请求，创建成员
        JSONObject jsonObject = RequestAccess.httpRequest(createUser, "POST", jsonU1);
        System.out.println("jsonObject:"+jsonObject.toString());
        //4.错误消息处理
        if (null != jsonObject) {
            if (0 != jsonObject.getInt("errcode")) {
                log.error("创建用户失败");
            }
        }
    }
    //修改用户
    public static void updateUser(String accessToken,WeChatUser weChatUser) {
        //1.获取json字符串：将user对象转换为json字符串
        Gson gson = new Gson();
        String jsonU1 =gson.toJson(weChatUser);//使用gson.toJson(user)即可将user对象顺序转成json
        //2.获取请求的url
        String updateUser = WeChatParamsUtil.UPDATE_USER.replace("ACCESS_TOKEN", accessToken);
        //3.调用接口，发送请求，创建成员
        JSONObject jsonObject = RequestAccess.httpRequest(updateUser, "POST", jsonU1);
        //4.错误消息处理
        if (null != jsonObject) {
            if (0 != jsonObject.getInt("errcode")) {
                log.error("修改用户失败");
            }
        }
    }
    //4.删除成员
    public static void deleteUser(String accessToken,String userId) {
        //1.获取请求的url
        String deleteUser = WeChatParamsUtil.DELETE_USER.replace("ACCESS_TOKEN", accessToken)
                .replace("USERID", userId);
        //2.调用接口，发送请求，删除成员
        JSONObject jsonObject = RequestAccess.httpRequest(deleteUser, "GET", null);
        //3.错误消息处理
        if (null != jsonObject) {
            if (0 != jsonObject.getInt("errcode")) {
                log.error("删除用户失败");
            }
        }
    }
    //获取用户
    public static String getUser(String accessToken, String userId) {
        //1.获取请求的url
        String getUser = WeChatParamsUtil.SELECT_USER.replace("ACCESS_TOKEN", accessToken)
                .replace("USERID",userId);

        //2.调用接口，发送请求，获取成员
        JSONObject jsonObject = RequestAccess.httpRequest(getUser, "GET", null);
        String string = jsonObject.toString();
        //3.错误消息处理
        if (null != jsonObject) {
            if (0 != jsonObject.getInt("errcode")) {
                log.error("查找用户失败");
            }
        }
        return string;
    }
}
