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

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

/**
 * 部门管理
 */
public class AddressDepartment {
    private static Logger log = (Logger) LoggerFactory.getLogger(RequestAccess.class);
    //1.创建部门
    public static void createDepartment(String accessToken, WxDepartment department) {
        //1.获取json字符串：将Department对象转换为json字符串
        Gson gson = new Gson();
        String jsonDepartment =gson.toJson(department);      //使用gson.toJson(jsonDepartment)即可将jsonDepartment对象顺序转成json
        //2.拼接请求的url
        String crate_department = WeChatParamsUtil.create_department.replace("ACCESS_TOKEN", accessToken);
        //3.调用接口，发送请求，创建部门
        JSONObject jsonObject = RequestAccess.httpRequest(crate_department, "POST", jsonDepartment);
        if (null != jsonObject) {
            if (0 != jsonObject.getInt("errcode")) {
                log.error("创建部门失败 errcode:{} errmsg:{}", jsonObject.getInt("errcode"), jsonObject.getString("errmsg"));
            }
        }
    }

    //2.更新部门
    public static void updateDepartment(String accessToken,WxDepartment department) {
        //1.获取json字符串：将Department对象转换为json字符串
        Gson gson = new Gson();
        String jsonDepartment =gson.toJson(department);      //使用gson.toJson(jsonDepartment)即可将jsonDepartment对象顺序转成json
        //2.拼接请求的url
        String update_department = WeChatParamsUtil.update_department.replace("ACCESS_TOKEN", accessToken);
        //3.调用接口，发送请求，更新部门
        JSONObject jsonObject = RequestAccess.httpRequest(update_department, "POST", jsonDepartment);
        System.out.println("jsonObject:"+jsonObject.toString());
        if (null != jsonObject) {
            if (0 != jsonObject.getInt("errcode")) {

                log.error("更新部门失败 errcode:{} errmsg:{}", jsonObject.getInt("errcode"), jsonObject.getString("errmsg"));
            }
        }
    }
    //3.删除部门
    public static void deleteDepartment(String accessToken,String departmentId) {
        //1.获取请求的url
        String delete_department=WeChatParamsUtil.delete_department.replace("ACCESS_TOKEN", accessToken)
                .replace("ID", departmentId);
        //2.调用接口，发送请求，删除部门
        JSONObject jsonObject = RequestAccess.httpRequest(delete_department, "GET", null);
        //3.错误消息处理
        if (null != jsonObject) {
            if (0 != jsonObject.getInt("errcode")) {
                log.error("删除部门失败 errcode:{} errmsg:{}", jsonObject.getInt("errcode"), jsonObject.getString("errmsg"));
            }
        }
    }
    //4.获取部门列表
    public void getDepartmentList(String accessToken,String departmentId) {
        //1.获取请求的url
        String select_department = WeChatParamsUtil.select_department.replace("ACCESS_TOKEN", accessToken)
                .replace("ID", departmentId);
        //2.调用接口，发送请求，获取成员
        JSONObject jsonObject = RequestAccess.httpRequest(select_department, "GET", null);
        //3.错误消息处理
        if (null != jsonObject) {
            if (0 != jsonObject.getInt("errcode")) {
                log.error("获取部门列表 errcode:{} errmsg:{}", jsonObject.getInt("errcode"), jsonObject.getString("errmsg"));
            }
        }
    }
}
