package com.ho.user.service.impl;

import com.ho.common.tools.constant.CommonConstant;
import com.ho.common.tools.exception.BaseResponseCode;
import com.ho.common.tools.exception.BusinessException;
import com.ho.user.entity.SysPosition;
import com.ho.user.entity.SysUserPosition;
import com.ho.user.mapper.SysPositionMapper;
import com.ho.user.mapper.SysUserPositionMapper;
import com.ho.user.service.PositionService;
import com.ho.user.vo.req.PositionAddReqVO;
import com.ho.user.vo.req.PositionQueryReqVO;
import com.ho.user.vo.req.PositionUpdateReqVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Description 岗位管理业务实现类
 * Author yule
 * Date 2022/12/1 15:58
 */
@Service
@Slf4j
public class PositionServiceImpl implements PositionService {

    @Autowired
    SysPositionMapper sysPositionMapper;

    @Autowired
    SysUserPositionMapper sysUserPositionMapper;


    @Override
    public int add(PositionAddReqVO vo) {
        SysPosition sysPosition = new SysPosition();
        BeanUtils.copyProperties(vo, sysPosition);
        sysPosition.setDeleted(1);
        sysPosition.setStatus(vo.getStatus());
        sysPosition.setCreateTime(new Date());
        sysPosition.setUpdateTime(new Date());
        int i = sysPositionMapper.insertSelective(sysPosition);
        return i;
    }

    @Override
    public SysPosition selectById(Integer id) {
        SysPosition sysPosition = sysPositionMapper.selectByPrimaryKey(id);
        return sysPosition;
    }

    @Override
    public List<SysPosition> selectAccuratePositionByInfo(SysPosition sysPosition) {
        List<SysPosition> positions = sysPositionMapper.selectByInfo(sysPosition);
        return positions;
    }

    @Override
    public List<SysPosition> getPositionByTop() {
        List<SysPosition> positions = sysPositionMapper.getPositionByTop();
        return positions;
    }

    @Override
    public List<SysPosition> selectAll() {
        List<SysPosition> positions = sysPositionMapper.selectByInfo(null);
        return positions;
    }

    //查询所有可用的
    @Override
    public List<SysPosition> selectAvailable() {
        SysPosition vo = new SysPosition();
        vo.setStatus(1);
        List<SysPosition> positions = sysPositionMapper.select(vo);
        return positions;
    }

    @Override
    public List<SysPosition> selectObscure(PositionQueryReqVO vo) {
        List<SysPosition> positions = sysPositionMapper.selectObscure(vo);
        return positions;
    }

    @Override
    public void deleted(Integer id) {
        //根据id查询
        SysPosition position = sysPositionMapper.selectByPrimaryKey(id);
        if (position == null) {
            throw new BusinessException(BaseResponseCode.POSITION_NOT_EXISTS);
        }
        //如果为禁用状态时，说明岗位下没有人使用
        //判断所删除的状态是否未启用状态，如果未启用状态则不能删除
        if (position.getStatus().equals(CommonConstant.ONE)) {
            throw new BusinessException(BaseResponseCode.POSITION_CANNOT_NOT_STATE_DELETE);
        }
        position.setUpdateTime(new Date());
        position.setDeleted(0);

        int count = sysPositionMapper.updateByPrimaryKeySelective(position);
        if (count != 1) {
            throw new BusinessException(BaseResponseCode.OPERATION_ERRO);
        }
    }

    @Override
    public void updatePosition(PositionUpdateReqVO vo) {
        SysPosition sysPosition = new SysPosition();
        BeanUtils.copyProperties(vo, sysPosition);
        sysPosition.setUpdateTime(new Date());

        SysPosition origPosition = sysPositionMapper.selectByPrimaryKey(vo.getId());
        if (origPosition == null) {
            throw new BusinessException(BaseResponseCode.POSITION_NOT_EXISTS);
        }
        //修改判断
        //修改为禁用时，判断下面是否有人员
        if (CommonConstant.ZERO.equals(vo.getStatus())) {
            //根据岗位id查询
            List<SysUserPosition> sysUserPositions = sysUserPositionMapper.selectByPostId(vo.getId());
            if (!sysUserPositions.isEmpty() || sysUserPositions == null) {
                throw new BusinessException(BaseResponseCode.POSITION_ASSOCIATED_USER_CANNOT_DISABLE);
            }
        }
        sysPositionMapper.updateByPrimaryKeySelective(sysPosition);
    }

    @Override
    public List<Integer> selectPostIds(List<String> postCodes) {
        List<Integer> postIds = sysPositionMapper.selectPostIds(postCodes);
        return postIds;
    }
}
