package com.jeelowcode.service.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.jeelowcode.tool.framework.common.enums.CommonStatusEnum;
import com.jeelowcode.tool.framework.common.pojo.PageResult;
import com.jeelowcode.tool.framework.common.util.object.BeanUtils;
import com.jeelowcode.service.system.controller.vo.dept.rank.RankPageReqVO;
import com.jeelowcode.service.system.controller.vo.dept.rank.RankSaveReqVO;
import com.jeelowcode.service.system.entity.RankDO;
import com.jeelowcode.service.system.mapper.RankMapper;
import com.jeelowcode.service.system.constant.ErrorCodeConstants;
import com.jeelowcode.service.system.service.IRankService;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import static com.jeelowcode.tool.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.jeelowcode.tool.framework.common.util.collection.CollectionUtils.convertMap;


/**
 * 职级 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class RankServiceImpl implements IRankService {

    @Resource
    private RankMapper rankMapper;

    @Override
    public Long createRank(RankSaveReqVO createReqVO) {
        // 校验正确性
        validateRankForCreateOrUpdate(null, createReqVO.getName());

        // 插入职级
        RankDO Rank = BeanUtils.toBean(createReqVO, RankDO.class);
        rankMapper.insert(Rank);
        return Rank.getId();
    }

    @Override
    public void updateRank(RankSaveReqVO updateReqVO) {
        // 校验正确性
        validateRankForCreateOrUpdate(updateReqVO.getId(), updateReqVO.getName());

        // 更新职级
        RankDO updateObj = BeanUtils.toBean(updateReqVO, RankDO.class);
        rankMapper.updateById(updateObj);
    }

    @Override
    public void deleteRank(Long id) {
        // 校验是否存在
        validateRankExists(id);
        // 删除部门
        rankMapper.deleteById(id);
    }

    private void validateRankForCreateOrUpdate(Long id, String name) {
        // 校验自己存在
        validateRankExists(id);
        // 校验职级名的唯一性
        validateRankNameUnique(id, name);
    }

    private void validateRankNameUnique(Long id, String name) {
        RankDO Rank = rankMapper.selectByName(name);
        if (Rank == null) {
            return;
        }
        // 如果 id 为空，说明不用比较是否为相同 id 的职级
        if (id == null) {
            throw exception(ErrorCodeConstants.RANK_NAME_DUPLICATE);
        }
        if (!Rank.getId().equals(id)) {
            throw exception(ErrorCodeConstants.RANK_NAME_DUPLICATE);
        }
    }

    private void validateRankExists(Long id) {
        if (id == null) {
            return;
        }
        if (rankMapper.selectById(id) == null) {
            throw exception(ErrorCodeConstants.RANK_NOT_FOUND);
        }
    }

    @Override
    public List<RankDO> getRankList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return Collections.emptyList();
        }
        return rankMapper.selectBatchIds(ids);
    }

    @Override
    public List<RankDO> getRankList() {
        return rankMapper.selectList();
    }

    @Override
    public List<RankDO> getRankList(Collection<Long> ids, Collection<Integer> statuses) {
        return rankMapper.selectList(ids, statuses);
    }

    @Override
    public PageResult<RankDO> getRankPage(RankPageReqVO reqVO) {
        return rankMapper.selectPage(reqVO);
    }

    @Override
    public RankDO getRank(Long id) {
        return rankMapper.selectById(id);
    }

    @Override
    public void validateRankList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return;
        }
        // 获得职级信息
        List<RankDO> Ranks = rankMapper.selectBatchIds(ids);
        Map<Long, RankDO> RankMap = convertMap(Ranks, RankDO::getId);
        // 校验
        ids.forEach(id -> {
            RankDO Rank = RankMap.get(id);
            if (Rank == null) {
                throw exception(ErrorCodeConstants.RANK_NOT_FOUND);
            }
            if (!CommonStatusEnum.ENABLE.getStatus().equals(Rank.getStatus())) {
                throw exception(ErrorCodeConstants.RANK_NOT_ENABLE, Rank.getName());
            }
        });
    }
}
