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

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.wqwt.vo.*;
import com.jojubanking.boot.module.system.dal.dataobject.wqwt.WqwtDO;
import com.jojubanking.boot.framework.common.pojo.PageResult;

import com.jojubanking.boot.module.system.convert.wqwt.WqwtConvert;
import com.jojubanking.boot.module.system.dal.mysql.wqwt.WqwtMapper;

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 WqwtServiceImpl implements WqwtService {

    @Resource
    private WqwtMapper wqwtMapper;

    @Override
    public Integer createWqwt(WqwtCreateReqVO createReqVO) {
        // 插入
        WqwtDO wqwt = WqwtConvert.INSTANCE.convert(createReqVO);
        wqwtMapper.insert(wqwt);
        // 返回
        return wqwt.getId();
    }

    @Override
    public void updateWqwt(WqwtUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateWqwtExists(updateReqVO.getId());
        // 更新
        WqwtDO updateObj = WqwtConvert.INSTANCE.convert(updateReqVO);
        wqwtMapper.updateById(updateObj);
    }

    @Override
    public void deleteWqwt(Integer id) {
        // 校验存在
        this.validateWqwtExists(id);
        // 删除
        wqwtMapper.deleteById(id);
    }

    private void validateWqwtExists(Integer id) {
        if (wqwtMapper.selectById(id) == null) {
            throw exception(WQWT_NOT_EXISTS);
        }
    }

    @Override
    public WqwtDO getWqwt(Integer id) {
        return wqwtMapper.selectById(id);
    }

    @Override
    public List<WqwtDO> getWqwtList(Collection<Integer> ids) {
        return wqwtMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<WqwtDO> getWqwtPage(WqwtPageReqVO pageReqVO) {
        return wqwtMapper.selectPage(pageReqVO);
    }

    @Override
    public List<WqwtDO> getWqwtList(WqwtExportReqVO exportReqVO) {
        return wqwtMapper.selectList(exportReqVO);
    }

}
