package com.usoftchina.qywx.sdk.test;

import com.alibaba.fastjson.JSON;
import com.usoftchina.qywx.sdk.AddrBookSdk;
import com.usoftchina.qywx.sdk.dto.*;
import org.junit.Test;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.List;

/**
 * @author yingp
 */
public class AddrBookSdkTest extends BaseTest {

    private static AddrBookSdk sdk;

    static {
        sdk = new AddrBookSdk(properties);
    }

    @Test
    public void getDepartmentList() {
        List<GetDepartmentListResp.Department> departmentList = sdk.getDepartmentList(1543022720);
        if (!CollectionUtils.isEmpty(departmentList)) {
            System.out.println(JSON.toJSONString(departmentList));
            departmentList.forEach(department -> {
                if (department.getName().equals("创泰事业群")) {
                    System.out.println(JSON.toJSONString(department));
                }
            });
        }
    }

    @Test
    public void getDepartment() {
        GetDepartmentListResp.Department department = sdk.getDepartment(1543022854);
        if (null != department) {
            System.out.println(JSON.toJSONString(department));
        }
    }

    @Test
    public void getUserList() {
        List<GetUserListResp.User> userList = sdk.getUserList(1, true);
        if (!CollectionUtils.isEmpty(userList)) {
//            System.out.println(JSON.toJSONString(userList));
            for (GetUserListResp.User user : userList) {
                if ("谭慧".equals(user.getName()) || "18529532060".equals(user.getMobile()) || "tanh@usoftchina.com".equals(user.getEmail())
                        || "".equals(user.getUserid())) {
                    System.out.println(JSON.toJSONString(user));
                }
            }
        }
    }

    @Test
    public void deleteUser() {
        sdk.deleteUser("qy01d3ffd8cd39d7002a66ffc62c");
    }

    @Test
    public void updateUser() {
//        sdk.updateUser(new UpdateUserReq().userId("YingPeng").alias("鹏"));

        List<GetUserListResp.User> userList = sdk.getUserList(1, true);
        if (!CollectionUtils.isEmpty(userList)) {
            userList.forEach(user -> {
                if (!user.getUserid().equals("YingPeng")) {
                    sdk.updateUser(new UpdateUserReq().userId(user.getUserid()).enable(false));
                }
            });
        }
    }

    @Test
    public void createUser() {
        sdk.createUser(new CreateUserReq().toInvite(false).name("测试").mobile("15361512390")
                .department(Arrays.asList(new CreateUserReq.Department(1, null, false)))
                .userId("Test15361512390")
                .position("测试人员")
                .gender(CreateUserReq.Gender.MALE));
    }

    @Test
    public void updateDepartment() {
        sdk.updateDepartment(new UpdateDepartmentReq().id(1).name("优软科技事业部"));
    }

}
