package cn.wisenergy.service.impl;

import cn.wisenergy.common.utils.exception.BASE_RESP_CODE_ENUM;
import cn.wisenergy.common.utils.exception.BaseCustomException;
import cn.wisenergy.mapper.WorkUserDeptMapper;
import cn.wisenergy.model.app.WorkUserDept;
import cn.wisenergy.service.WorkUserDeptService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.ArrayList;
import java.util.List;

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


    @Autowired
    private WorkUserDeptMapper workUserDeptMapper;
    @Override
    public Integer getDeptIdByUserId(Integer id) {
        log.info("WorkUserDeptServiceImpl[]getDeptIdByUserId[]input.param.id;{}" + id);
        if (null == id) {
            throw new BaseCustomException(BASE_RESP_CODE_ENUM.INPUT_PARAM_IS_NULL);
        }
        QueryWrapper<WorkUserDept> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", id);
        WorkUserDept workUserDept = workUserDeptMapper.selectOne(wrapper);
        if (null == workUserDept) {
            throw new BaseCustomException(BASE_RESP_CODE_ENUM.DEPT_NOT_HAVE_USER);
        }

        Integer deptId = workUserDept.getDeptId();
        if (null == deptId) {
                throw new BaseCustomException(BASE_RESP_CODE_ENUM.DEPT_NOT_HAVE_USER);
        }
        return deptId;
    }

    @Override
    public List<Integer> getUserIdsByDeptId(Integer deptId) {
        log.info("WorkUserDeptServiceImpl[]getUserIdsByDeptId[]input.param.deptId;{}" + deptId);
        if (null == deptId) {
            throw new BaseCustomException(BASE_RESP_CODE_ENUM.INPUT_PARAM_IS_NULL);
        }
        QueryWrapper<WorkUserDept> wrapper = new QueryWrapper<>();
        wrapper.eq("dept_id", deptId);
        List<WorkUserDept> workUserDepts = workUserDeptMapper.selectList(wrapper);
        if (CollectionUtils.isEmpty(workUserDepts)) {
            throw new BaseCustomException(BASE_RESP_CODE_ENUM.DEPT_NOT_HAVE_USER);
        }
        ArrayList<Integer> userIds = new ArrayList<>();
        for (WorkUserDept workUserDept : workUserDepts) {
            userIds.add(workUserDept.getUserId());
        }
        return userIds;
    }
}
