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

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

import com.jojubanking.boot.module.system.convert.wq.WqConvert;
import com.jojubanking.boot.module.system.dal.mysql.wq.WqMapper;

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 WqServiceImpl implements WqService {

    @Resource
    private WqMapper wqMapper;

    @Override
    public Integer createWq(WqCreateReqVO createReqVO) {
        // 插入
        WqDO wq = WqConvert.INSTANCE.convert(createReqVO);
        wqMapper.insert(wq);
        // 返回
        return wq.getId();
    }

    @Override
    public void updateWq(WqUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateWqExists(updateReqVO.getId());
        // 更新
        WqDO updateObj = WqConvert.INSTANCE.convert(updateReqVO);
        wqMapper.updateById(updateObj);
    }

    @Override
    public void deleteWq(Integer id) {
        // 校验存在
        this.validateWqExists(id);
        // 删除
        wqMapper.deleteById(id);
    }

    private void validateWqExists(Integer id) {
        if (wqMapper.selectById(id) == null) {
            throw exception(WQ_NOT_EXISTS);
        }
    }

    @Override
    public WqDO getWq(Integer id) {
        return wqMapper.selectById(id);
    }

    @Override
    public List<WqDO> getWqList(Collection<Integer> ids) {
        return wqMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<WqDO> getWqPage(WqPageReqVO pageReqVO) {
        return wqMapper.selectPage(pageReqVO);
    }

    @Override
    public List<WqDO> getWqList(WqExportReqVO exportReqVO) {
        return wqMapper.selectList(exportReqVO);
    }

}
