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.dto.OrganizationStructureDto;
import cn.wisenergy.service.WorkDeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

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

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

    @Autowired
    private WorkDeptMapper workDeptMapper;

    @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 deptId) {
        log.info("WorkDeptServiceImpl[]getAllDeptInfo" + deptId);
        //获取部门信息
        Map<String, Object> map = new HashMap<>();
        map.put("id", deptId);
        List<WorkDept> workDepts = workDeptMapper.getDeptByCondition(map);
        if (CollectionUtil.isEmpty(workDepts)) {
            throw new BaseCustomException(BASE_RESP_CODE_ENUM.DEPT_NOT_FOUND);
        }
        return workDepts;
    }

    @Override
    public List<OrganizationStructureDto> getAllDeptByAllCentreId() {
        log.info("WorkDeptServiceImpl[]getAllDeptByAllCentreId[]");
        List<OrganizationStructureDto> allDeptByAllCentre = workDeptMapper.getAllDeptByAllCentre();
        if (CollectionUtils.isEmpty(allDeptByAllCentre)) {
            throw new BaseCustomException(BASE_RESP_CODE_ENUM.DEPT_IS_NULL);
        }
        return allDeptByAllCentre;
    }

    @Override
    public Boolean updateManagerIdISNullById(Integer id) {
        log.info("WorkDeptServiceImpl[]updateManagerIdISNullById[]input.param.id:{}" + id);
        if (null == id) {
            throw new BaseCustomException(BASE_RESP_CODE_ENUM.INPUT_PARAM_IS_NULL);
        }
        int i = workDeptMapper.updateManagerIdISNullById(id);
        return i > 0;
    }

    @Override
    public Boolean updateById(WorkDept workDept) {
        log.info("WorkDeptServiceImpl[]updateById[]input.param.workDept:{}" + workDept);
        if (null == workDept || null == workDept.getId()) {
            throw new BaseCustomException(BASE_RESP_CODE_ENUM.INPUT_PARAM_IS_NULL);
        }
        int i = workDeptMapper.updateById(workDept);
        return i>0;
    }

    @Override
    public List<WorkDept> getDeptByManagerId(Integer userId) {
        log.info("WorkDeptServiceImpl[]getDeptByManagerId[]input.param.userId:{}" + userId);
        if (null == userId) {
            throw new BaseCustomException(BASE_RESP_CODE_ENUM.INPUT_PARAM_IS_NULL);
        }
        List<WorkDept> workDeptList = workDeptMapper.getDeptByManagerId(userId);
        if (CollectionUtils.isEmpty(workDeptList)) {
            return null;
        }
        return workDeptList;
    }
}
