package cn.wisenergy.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.wisenergy.common.utils.exception.BASE_RESP_CODE_ENUM;
import cn.wisenergy.common.utils.exception.BaseCustomException;
import cn.wisenergy.mapper.WorkDeptMapper;
import cn.wisenergy.model.app.WorkDept;
import cn.wisenergy.model.app.WorkUser;
import cn.wisenergy.model.enums.ManagerEnum;
import cn.wisenergy.service.WorkDeptService;
import cn.wisenergy.service.WorkUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author chenqi
 * @since 2021-01-19
 */
@Service
@Slf4j
public class WorkDeptServiceImpl implements WorkDeptService {

    @Autowired
    private WorkDeptMapper workDeptMapper;

      WorkUserService workUserService;
    @Override
    public WorkDept getById(Integer id) {
        log.info("WorkDeptServiceImpl[]getById[]input.param.id" + id);
        if (null == id) {
            throw new BaseCustomException(BASE_RESP_CODE_ENUM.INPUT_PARAM_IS_NULL);
        }
        WorkDept workDept = workDeptMapper.selectById(id);
        if (null == workDept) {
            throw new BaseCustomException(BASE_RESP_CODE_ENUM.DEPT_IS_NULL);
        }
        return workDept;

    }
    @Override
    public List<WorkDept> getDeptInfo(Integer userId, Integer deptId) {
        log.info("WorkDeptServiceImpl[]getAllDeptInfo" + userId);
        if (userId == null || deptId == null) {
            throw new BaseCustomException(BASE_RESP_CODE_ENUM.INPUT_PARAM_IS_NULL);
        }
        //判断用户权限
        WorkUser user = workUserService.getUserById(userId);
        if (user.getLevel() == ManagerEnum.NOT_MANAGER.getCode()) {
            throw new BaseCustomException(BASE_RESP_CODE_ENUM.THE_USER_NOT_MANAGER_PLASE_MANAGER_LOGIN);
        }
        //获取部门信息
        List<WorkDept> workDepts = workDeptMapper.getDeptById(deptId);
        if (CollectionUtil.isEmpty(workDepts)) {
            throw new BaseCustomException(BASE_RESP_CODE_ENUM.DEPT_NOT_FOUND);
        }
        return workDepts;
    }
}
