package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.system.domain.dto.DepartMentDto;
import com.ruoyi.system.mapper.DepartmentMapper;
import com.ruoyi.system.service.DepartmentService;
import io.netty.util.internal.ObjectUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * (Department)表服务实现类
 *
 * @author makejava
 * @since 2024-05-27 17:35:59
 */
@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Resource
    private DepartmentMapper departmentDao;


    @Override
    public List<Map<String, Object>> getDepartments(String departmentName) {

        List<DepartMentDto> departments = departmentDao.getDepartments(departmentName);
        if (departments.isEmpty()){
            return null;
        }
        int departmentId = -123;
        List<DepartMentDto> copy = null;
        List<String> ids = null;
        List<String> names = null;
        List<String> phones = null;
        List<Map<String, Object>> maps = new ArrayList<>();
        int i = 0;
        for (DepartMentDto department : departments) {
            if (!department.getDepartmentId().equals(departmentId)) {
                departmentId = department.getDepartmentId();
                if (i!=0){
                    Map<String, Object> map = new HashMap<>();
                    map.put("DepartMentDto", copy.get(0));
                    map.put("kgyIds", ids);
                    map.put("kgyNames", names);
                    map.put("phones", phones);
                    maps.add(map);
                }
                copy = new ArrayList<>();
                ids = new ArrayList<>();
                names = new ArrayList<>();
                phones = new ArrayList<>();
            }
            department.setAssetNum(departmentDao.getDepartmentAssetNum(department.getDepartmentId(), null, null));
            copy.add(department);
            ids.add(Convert.toStr(department.getDutyId()));
            names.add(department.getPersonName());
            phones.add(department.getTelPhone());
            i++;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("DepartMentDto", copy.get(0));
        map.put("kgyIds", ids);
        map.put("kgyNames", names);
        map.put("phones", phones);
        maps.add(map);

        return maps;
    }

    @Override
    public List<Map<String, Object>> getDepartmentsV2(String departmentName, String goodsName) {
        List<Map<String, Object>> departments = departmentDao.getDepartmentsV2(departmentName);
        if (departments == null || departments.isEmpty()) {
            return null;
        }
        for (Map<String, Object> department : departments) {
            Integer departmentId = Convert.toInt(department.get("departmentId"));
            department.put("assetNum", departmentDao.getDepartmentAssetNum(departmentId, goodsName, null));
            department.put("wzTotal", this.sumDepartmentAssetNum(departmentId, goodsName, null));
            department.put("member", departmentDao.getMemberByDepartment(departmentId));
        }
        departments.sort((o1, o2) -> {
            Long count1 = Convert.toLong(o1.get("assetNum"));
            Long count2 = Convert.toLong(o2.get("assetNum"));
            return Long.compare(count2, count1);
        });
        return departments;
    }

    @Override
    public Map<String, Object> sumDepartmentAssetNum(Integer departmentId, String goodsName, String assetType) {
        Map<String, Object> result = new HashMap<>();
        result.put("total", departmentDao.sumDepartmentAssetNum(departmentId, goodsName, assetType, null));
        result.put("borrow", departmentDao.sumDepartmentAssetNum(departmentId, goodsName, assetType, 2));
        result.put("unused", departmentDao.sumDepartmentAssetNum(departmentId, goodsName, assetType, 0));
        return result;
    }
}
