package cn.com.ctop.oa.modules.service.impl;

import cn.com.ctop.common.module.utils.Check;
import cn.com.ctop.common.module.utils.HttpUtils;
import cn.com.ctop.oa.modules.constant.QYWxConstant;
import cn.com.ctop.oa.modules.entity.WechatDepartment;
import cn.com.ctop.oa.modules.enums.DepartEnum;
import cn.com.ctop.oa.modules.mapper.WechatDepartmentMapper;
import cn.com.ctop.oa.modules.service.IWechatDepartmentService;
import cn.com.ctop.oa.modules.service.IWechatTokenService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxl.job.core.enums.NoEn;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * 部门列表
 *
 * @author jeecg-boot
 * @version V1.0
 * @date 2020-09-10
 */
@Service
@Slf4j
public class WechatDepartmentServiceImpl extends ServiceImpl<WechatDepartmentMapper, WechatDepartment> implements IWechatDepartmentService {
    @Autowired
    private IWechatTokenService tokenService;
    @Autowired
    private WechatDepartmentMapper wechatDepartmentMapper;

    @Override
    public void getDepartment() {
        try {
            Map<String, Object> qywxAccessToken = tokenService.getQYWXAccessToken();
            if ((Integer) qywxAccessToken.get("code") != 0) {
                log.error("企业微信获取token失败,返回信息：{}", qywxAccessToken.get("message"));
                throw new Exception("企业微信获取token失败");
            }
            TreeMap<String, Object> params = new TreeMap<>();
            params.put("access_token", qywxAccessToken.get("accessToken"));
            String resultStr = HttpUtils.httpGetRequest(QYWxConstant.QYWX_DEPART_LIST, null, params);
            JSONObject resultJson = JSONObject.parseObject(resultStr);
            Integer errcode = resultJson.getInteger("errcode");
            if (errcode == 0) {
                JSONArray department = resultJson.getJSONArray("department");
                if (!Check.isNull(department)) {
                    for (int i = 0; i < department.size(); i++) {
                        JSONObject departJson = department.getJSONObject(i);
                        if (!Check.isNull(departJson)) {
                            Boolean isTrueOrFalse = DepartEnum.getDepart(departJson.getString("name"));
                            if (!isTrueOrFalse) {
                                continue;
                            }
                            Map<String, Object> deleteMap = new HashMap<>();
                            deleteMap.put("depart_name", departJson.getString("name"));
                            wechatDepartmentMapper.deleteByMap(deleteMap);
                            WechatDepartment depart = new WechatDepartment();
                            depart.setDepartId(departJson.getString("id"));
                            depart.setDepartName(departJson.getString("name"));
                            depart.setParentId(departJson.getString("parentid"));
                            depart.setDepartOrder(departJson.getString("order"));
                            wechatDepartmentMapper.insert(depart);
                        }
                    }
                }
            } else {
                throw new Exception("企业微信获取部门列表");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<WechatDepartment> queryList(Integer dataType) {
        return wechatDepartmentMapper.queryList(dataType);
    }

}
