package cn.iocoder.yudao.server.service;

import cn.iocoder.yudao.framework.common.exception.ErrorCode;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.server.controller.admin.vo.FlowGroupPageReqVO;
import cn.iocoder.yudao.server.controller.admin.vo.FlowGroupSaveReqVO;
import cn.iocoder.yudao.server.dal.dataobject.FlowGroupDO;
import cn.iocoder.yudao.server.dal.mysql.FlowGroupMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 门户流程分组信息 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class FlowGroupServiceImpl implements FlowGroupService {

    @Resource
    private FlowGroupMapper flowGroupMapper;

    @Override
    public Long createFlowGroup(FlowGroupSaveReqVO createReqVO) {
        // 插入
        FlowGroupDO flowGroup = BeanUtils.toBean(createReqVO, FlowGroupDO.class);
        flowGroup.setDeleted(false);
        flowGroupMapper.insert(flowGroup);
        // 返回
        return flowGroup.getId();
    }

    @Override
    public void updateFlowGroup(FlowGroupSaveReqVO updateReqVO) {
        // 校验存在
        validateFlowGroupExists(updateReqVO.getId());
        // 更新
        FlowGroupDO updateObj = BeanUtils.toBean(updateReqVO, FlowGroupDO.class);
        flowGroupMapper.updateById(updateObj);
    }

    @Override
    public void deleteFlowGroup(Long id) {
        // 校验存在
        validateFlowGroupExists(id);
        // 删除
        flowGroupMapper.deleteById(id);
    }

    private void validateFlowGroupExists(Long id) {
        if (flowGroupMapper.selectById(id) == null) {
            throw exception(new ErrorCode(1,"不存在"));
        }
    }

    @Override
    public FlowGroupDO getFlowGroup(Long id) {
        return flowGroupMapper.selectById(id);
    }

    @Override
    public PageResult<FlowGroupDO> getFlowGroupPage(FlowGroupPageReqVO pageReqVO) {
        return flowGroupMapper.selectPage(pageReqVO);
    }

}