package com.malk.hangshi.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.malk.hangshi.service.HSService;
import com.malk.server.dingtalk.DDConf;
import com.malk.service.dingtalk.DDClient;
import com.malk.service.dingtalk.DDClient_Contacts;
import com.malk.utils.UtilMap;
import lombok.Synchronized;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
@Slf4j
public class HSImplService implements HSService {

    @Autowired
    private DDClient ddClient;

    @Autowired
    private DDClient_Contacts ddClient_contacts;

    private Map DEPT_INFO;

    @Override
    @Synchronized
    public Map getDeptInfo(boolean isClear) {

        if (isClear) {
            DEPT_INFO = null;
        }

        if (ObjectUtil.isNull(DEPT_INFO)) {
            int total = 0;
            Map deptInfo = UtilMap.map("东方航空食品投资有限公司", ddClient_contacts.getUserCount(ddClient.getAccessToken(), true));
            // 一级部门累计
            List<Map> deptInfos = ddClient_contacts.listSubDepartmentDetail(ddClient.getAccessToken(), DDConf.TOP_DEPARTMENT);
            for (Map dept : deptInfos) {
                String deptName = String.valueOf(dept.get("name"));
                if (!deptName.contains("航食")) {
                    continue;
                }
                long deptId = UtilMap.getLong(dept, "dept_id");
                int count = 0;
                for (long sub : ddClient_contacts.getDepartmentId_all(ddClient.getAccessToken(), true, deptId)) {
                    count += ddClient_contacts.listDepartmentUserId(ddClient.getAccessToken(), sub).size();
                }
                total += count;
                deptInfo.put(dept.get("name"), count);
                // 上海航食处理
                if (deptName.equals("上海航食")) {
                    List<Map> subDeptInfos = ddClient_contacts.listSubDepartmentDetail(ddClient.getAccessToken(), deptId);
                    for (Map subDept : subDeptInfos) {
                        if (Arrays.asList("虹桥中心", "浦东中心").contains(subDept.get("name"))) {
                            int num = 0;
                            for (long sub : ddClient_contacts.getDepartmentId_all(ddClient.getAccessToken(), true, UtilMap.getLong(subDept, "dept_id"))) {
                                num += ddClient_contacts.listDepartmentUserId(ddClient.getAccessToken(), sub).size();
                            }
                            deptInfo.put(subDept.get("name"), num);
                        }
                    }
                    deptInfo.put("各职能业务部门及事业部", UtilMap.getInt(deptInfo, "上海航食") - UtilMap.getInt(deptInfo, "虹桥中心") - UtilMap.getInt(deptInfo, "浦东中心"));
                }
            }
//            deptInfo.put("航食", total);
            DEPT_INFO = deptInfo;
        }
        return DEPT_INFO;
    }

    @Override
    public int getCorpCount() {
        return ddClient_contacts.getUserCount(ddClient.getAccessToken(), true);
    }
}
