package com.lideeyunji.service.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.lideeyunji.tool.framework.common.enums.CommonStatusEnum;
import com.lideeyunji.tool.framework.common.pojo.PageResult;
import com.lideeyunji.tool.framework.common.util.object.BeanUtils;
import com.lideeyunji.service.system.controller.vo.dept.position.PositionPageReqVO;
import com.lideeyunji.service.system.controller.vo.dept.position.PositionSaveReqVO;
import com.lideeyunji.service.system.entity.PositionDO;
import com.lideeyunji.service.system.mapper.PositionMapper;
import com.lideeyunji.service.system.constant.ErrorCodeConstants;
import com.lideeyunji.service.system.service.IPositionService;
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.lideeyunji.tool.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.lideeyunji.tool.framework.common.util.collection.CollectionUtils.convertMap;


/**
 * 职位 Service 实现类
 *
 * @author 金灯剑客
 */
@Service
@Validated
public class PositionServiceImpl implements IPositionService {

    @Resource
    private PositionMapper positionMapper;

    @Override
    public Long createPosition(PositionSaveReqVO createReqVO) {
        // 校验正确性
        validatePositionForCreateOrUpdate(null, createReqVO.getName());

        // 插入职位
        PositionDO Position = BeanUtils.toBean(createReqVO, PositionDO.class);
        positionMapper.insert(Position);
        return Position.getId();
    }

    @Override
    public void updatePosition(PositionSaveReqVO updateReqVO) {
        // 校验正确性
        validatePositionForCreateOrUpdate(updateReqVO.getId(), updateReqVO.getName());

        // 更新职位
        PositionDO updateObj = BeanUtils.toBean(updateReqVO, PositionDO.class);
        positionMapper.updateById(updateObj);
    }

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

    private void validatePositionForCreateOrUpdate(Long id, String name) {
        // 校验自己存在
        validatePositionExists(id);
        // 校验职位名的唯一性
        validatePositionNameUnique(id, name);
    }

    private void validatePositionNameUnique(Long id, String name) {
        PositionDO Position = positionMapper.selectByName(name);
        if (Position == null) {
            return;
        }
        // 如果 id 为空，说明不用比较是否为相同 id 的职位
        if (id == null) {
            throw exception(ErrorCodeConstants.POSITION_NAME_DUPLICATE);
        }
        if (!Position.getId().equals(id)) {
            throw exception(ErrorCodeConstants.POSITION_NAME_DUPLICATE);
        }
    }

    private void validatePositionExists(Long id) {
        if (id == null) {
            return;
        }
        if (positionMapper.selectById(id) == null) {
            throw exception(ErrorCodeConstants.POSITION_NOT_FOUND);
        }
    }

    @Override
    public List<PositionDO> getPositionList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return Collections.emptyList();
        }
        return positionMapper.selectBatchIds(ids);
    }

    @Override
    public List<PositionDO> getPositionList() {
        return positionMapper.selectList();
    }

    @Override
    public List<PositionDO> getPositionList(Collection<Long> ids, Collection<Integer> statuses) {
        return positionMapper.selectList(ids, statuses);
    }

    @Override
    public PageResult<PositionDO> getPositionPage(PositionPageReqVO reqVO) {
        return positionMapper.selectPage(reqVO);
    }

    @Override
    public PositionDO getPosition(Long id) {
        return positionMapper.selectById(id);
    }

    @Override
    public void validatePositionList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return;
        }
        // 获得职位信息
        List<PositionDO> Positions = positionMapper.selectBatchIds(ids);
        Map<Long, PositionDO> PositionMap = convertMap(Positions, PositionDO::getId);
        // 校验
        ids.forEach(id -> {
            PositionDO Position = PositionMap.get(id);
            if (Position == null) {
                throw exception(ErrorCodeConstants.POSITION_NOT_FOUND);
            }
            if (!CommonStatusEnum.ENABLE.getStatus().equals(Position.getStatus())) {
                throw exception(ErrorCodeConstants.POSITION_NOT_ENABLE, Position.getName());
            }
        });
    }
}
