package com.jojubanking.boot.module.system.service.dzquestion;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.jojubanking.boot.module.system.controller.admin.dzquestion.vo.*;
import com.jojubanking.boot.module.system.dal.dataobject.dzquestion.DzQuestionDO;
import com.jojubanking.boot.framework.common.pojo.PageResult;

import com.jojubanking.boot.module.system.convert.dzquestion.DzQuestionConvert;
import com.jojubanking.boot.module.system.dal.mysql.dzquestion.DzQuestionMapper;

import static com.jojubanking.boot.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.jojubanking.boot.module.system.enums.ErrorCodeConstants.*;

/**
 * 导诊管理 Service 实现类
 *
 * @author admin
 */
@Service
@Validated
public class DzQuestionServiceImpl implements DzQuestionService {

    @Resource
    private DzQuestionMapper dzQuestionMapper;

    @Override
    public Integer createDzQuestion(DzQuestionCreateReqVO createReqVO) {
        // 插入
        DzQuestionDO dzQuestion = DzQuestionConvert.INSTANCE.convert(createReqVO);
        dzQuestionMapper.insert(dzQuestion);
        // 返回
        return dzQuestion.getId();
    }

    @Override
    public void updateDzQuestion(DzQuestionUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateDzQuestionExists(updateReqVO.getId());
        // 更新
        DzQuestionDO updateObj = DzQuestionConvert.INSTANCE.convert(updateReqVO);
        dzQuestionMapper.updateById(updateObj);
    }

    @Override
    public void deleteDzQuestion(Integer id) {
        // 校验存在
        this.validateDzQuestionExists(id);
        // 删除
        dzQuestionMapper.deleteById(id);
    }

    private void validateDzQuestionExists(Integer id) {
        if (dzQuestionMapper.selectById(id) == null) {
            throw exception(DZ_QUESTION_NOT_EXISTS);
        }
    }

    @Override
    public DzQuestionDO getDzQuestion(Integer id) {
        return dzQuestionMapper.selectById(id);
    }

    @Override
    public List<DzQuestionDO> getDzQuestionList(Collection<Integer> ids) {
        return dzQuestionMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<DzQuestionDO> getDzQuestionPage(DzQuestionPageReqVO pageReqVO) {
        return dzQuestionMapper.selectPage(pageReqVO);
    }

    @Override
    public List<DzQuestionDO> getDzQuestionList(DzQuestionExportReqVO exportReqVO) {
        return dzQuestionMapper.selectList(exportReqVO);
    }

}
