package com.muzhi.dingtalk.test.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiDepartmentListRequest;
import com.dingtalk.api.response.OapiDepartmentListResponse;
import com.muzhi.dingtalk.test.entity.Department;
import com.muzhi.dingtalk.test.entity.Employee;
import com.muzhi.dingtalk.test.entity.vo.DepartmentDataVo;
import com.muzhi.dingtalk.test.entity.vo.DepartmentQuery;
import com.muzhi.dingtalk.test.entity.vo.EmployeeDataVo;
import com.muzhi.dingtalk.test.mapper.DepartmentMapper;
import com.muzhi.dingtalk.test.service.DepartmentService;
import com.muzhi.dingtalk.test.service.DingAccessTokenService;
import com.taobao.api.ApiException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DingAccessTokenService dingAccessTokenService;

    @Autowired
    private DepartmentMapper departmentMapper;

    public static String departmentList = null;
    public static String departmentXQ = null;

    //查询钉钉接口中的部门列表信息
    @Override
    public String getDingDepartment(){
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/department/list");
        OapiDepartmentListRequest request = new OapiDepartmentListRequest();
        try {
            request.setHttpMethod("GET");
            OapiDepartmentListResponse response = client.execute(request, dingAccessTokenService.getAccessToken());
            departmentList = response.getBody();
            System.out.println(response.getBody());
        } catch (ApiException e) {
            e.printStackTrace();
        }

        //通过fastjson工具转换成json对象,逐层解析
        //将String类型的部门列表json数据转换为Object数据对象
        JSONObject jsonObject = JSON.parseObject(departmentList);
        String department = jsonObject.getString("department");

        return department;
    }

    //获取部门列表
    @Override
    public DepartmentDataVo<Department> getDepartmentList(Integer page, Integer limit, DepartmentQuery departmentQuery) {
        //直接在该实现类查询数据库实体类的数据
        DepartmentDataVo departmentDataVo = new DepartmentDataVo();
        departmentDataVo.setCode(0);
        departmentDataVo.setMsg("");

        //分页查询,创建page对象
        Page<Department> departmentPage = new Page<>(page,limit);

        //构建查询条件
        QueryWrapper<Department> wrapper = new QueryWrapper<>();
        //多条件组合查询（类似mybatis中动态sql）
        String name = departmentQuery.getName();
        //判断条件值是否为空，如果不为空则拼接条件
        if (!StringUtils.isEmpty(name)) {
            //构建条件
            wrapper.like("name", name);
        }

        IPage<Department> result = departmentMapper.selectPage(departmentPage, wrapper);

        //查询分页信息记录（根据分页要求来查）
        List<Department> departmentList = result.getRecords();
        //查询总数方法2
        departmentDataVo.setCount(result.getTotal());

        departmentDataVo.setData(departmentList);
        return departmentDataVo;
    }



    //TODO 获取部门详情列表
    @Override
    public String getDepartmentXQ() {

        return null;
    }
}