package cn.iocoder.yudao.module.system.api.dept;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.datapermission.core.annotation.DataPermission;
import cn.iocoder.yudao.module.system.api.dept.dto.DeptApiDTO;
import cn.iocoder.yudao.module.system.api.dept.dto.DeptApiVO;
import cn.iocoder.yudao.module.system.api.dept.dto.DeptRespDTO;
import cn.iocoder.yudao.module.system.controller.admin.dept.vo.dept.DeptListReqVO;
import cn.iocoder.yudao.module.system.controller.admin.dept.vo.dept.DeptSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.dept.DeptDO;
import cn.iocoder.yudao.module.system.service.dept.DeptService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;

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

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@RestController // 提供 RESTful API 接口，给 Feign 调用
@Validated
public class DeptApiImpl implements DeptApi {

    @Resource
    private DeptService deptService;

    @Override
    @DataPermission(enable = false)
    public CommonResult<DeptRespDTO> getDept(Long id) {
        DeptDO dept = deptService.getDept(id);
        return success(BeanUtils.toBean(dept, DeptRespDTO.class));
    }

    @Override
    @DataPermission(enable = false)
    public CommonResult<List<DeptRespDTO>> getDeptList(Collection<Long> ids) {
        List<DeptDO> depts = deptService.getDeptList(ids);
        return success(BeanUtils.toBean(depts, DeptRespDTO.class));
    }

    @Override
    public CommonResult<List<DeptApiVO>> getDeptList(DeptApiDTO dto) {
        List<DeptDO> list = deptService.getDeptList(dto);
        list.sort(Comparator.comparing(DeptDO::getSort));
        List<DeptApiVO> deptApiVOS = BeanUtil.copyToList(list, DeptApiVO.class);
        return success(deptApiVOS);
    }

    @Override
    public CommonResult<Boolean> validateDeptList(Collection<Long> ids) {
        deptService.validateDeptList(ids);
        return success(true);
    }

    @Override
    @DataPermission(enable = false)
    public CommonResult<List<DeptRespDTO>> getDeptByLeaderId(Long leaderUserId) {

        List<DeptDO> deptDO = deptService.getDeptByLeaderId(leaderUserId);
        return success(BeanUtils.toBean(deptDO, DeptRespDTO.class));
    }

    @Override
    public CommonResult<List<DeptRespDTO>> getDeptList() {

        //获取部门名称信息
        List<DeptDO> deptDOs = deptService.getDeptList(new DeptListReqVO().setStatus(CommonStatusEnum.ENABLE.getStatus()));

        return success(BeanUtils.toBean(deptDOs, DeptRespDTO.class));
    }

    @Override
    public CommonResult<Long> createDept(DeptRespDTO deptRespDTO) {

        DeptSaveReqVO deptSaveReqVO = BeanUtils.toBean(deptRespDTO, DeptSaveReqVO.class);

        return success(deptService.createDept(deptSaveReqVO));
    }

    @Override
    public void updateFactoryDept(DeptRespDTO deptRespDTO) {

        DeptSaveReqVO deptSaveReqVO = BeanUtils.toBean(deptRespDTO, DeptSaveReqVO.class);
        deptService.updateFactoryDept(deptSaveReqVO);
    }

    @Override
    @DataPermission(enable = false)
    public void deleteDept(Long factoryId) {

        DeptDO deptDO = deptService.getDeptByFactoryId(factoryId);
        if (deptDO != null) {

            deptService.deleteDept(deptDO.getId());
        }
    }

    @Override
    @DataPermission(enable = false)
    public CommonResult<DeptRespDTO> getDeptByFactoryId(Long factoryId) {

        DeptDO deptDO = deptService.getDeptByFactoryId(factoryId);
        return success(BeanUtils.toBean(deptDO, DeptRespDTO.class));
    }

    @Override
    @DataPermission(enable = false)
    public CommonResult<List<DeptRespDTO>> getDeptByFactoryIds(Collection<Long> factoryIds) {

        List<DeptDO> deptDOS = deptService.getDeptListByFactoryIds(factoryIds);
        return success(BeanUtils.toBean(deptDOS, DeptRespDTO.class));
    }

    @Override
    @DataPermission(enable = false)
    public CommonResult<List<Long>> getChildDeptList(Long deptId) {
        List<DeptDO> deptDOS = deptService.getChildDept(deptId);
        if (CollectionUtil.isEmpty(deptDOS)) {
            return success(Collections.emptyList());
        }
        List<Long> ids = deptDOS.stream().map(DeptDO::getId).collect(Collectors.toList());
        return success(ids);
    }

    @Override
    public CommonResult<Map<Long, Long>> getDeptListByFactoryIds(List<Long> factoryIds) {
        List<DeptDO> deptDOS = deptService.getDeptListByFactoryIds(factoryIds);
        if (CollectionUtil.isEmpty(deptDOS)) {
            return success(Collections.emptyMap());
        }
        Map<Long, Long> map = deptDOS.stream().collect(Collectors.toMap(DeptDO::getFactoryId, DeptDO::getId));
        return success(map);
    }

    @Override
    @DataPermission(enable = false)
    public CommonResult<List<DeptRespDTO>> getCompanyDept() {
        List<DeptDO> deptDOS = deptService.getCompanyDept(2);
        return success(BeanUtils.toBean(deptDOS, DeptRespDTO.class));
    }

    @Override
    @DataPermission(enable = false)
    public CommonResult<DeptRespDTO> getUserCompanyDept(Long userId) {

        DeptDO deptDO = deptService.getUserCompanyDept(userId);
        return success(BeanUtils.toBean(deptDO, DeptRespDTO.class));
    }

    @Override
    @DataPermission(enable = false)
    public CommonResult<List<DeptRespDTO>> getDeptListByType(String type) {

        List<DeptDO> deptList = deptService.getDeptListByType(type);
        return success(BeanUtils.toBean(deptList, DeptRespDTO.class));
    }

    @Override
    @DataPermission(enable = false)
    public CommonResult<DeptRespDTO> getCompanyByDept(Long deptId) {
        DeptDO dept = deptService.getCompanyByDept(deptId);
        return success(BeanUtils.toBean(dept, DeptRespDTO.class));
    }
}
