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

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

import com.jojubanking.boot.module.system.convert.information.InformationConvert;
import com.jojubanking.boot.module.system.dal.mysql.information.InformationMapper;

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 InformationServiceImpl implements InformationService {

    @Resource
    private InformationMapper informationMapper;

    @Override
    public Integer createInformation(InformationCreateReqVO createReqVO) {
        // 插入
        InformationDO information = InformationConvert.INSTANCE.convert(createReqVO);
        informationMapper.insert(information);
        // 返回
        return information.getId();
    }

    @Override
    public void updateInformation(InformationUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateInformationExists(updateReqVO.getId());
        // 更新
        InformationDO updateObj = InformationConvert.INSTANCE.convert(updateReqVO);
        informationMapper.updateById(updateObj);
    }

    @Override
    public void deleteInformation(Integer id) {
        // 校验存在
        this.validateInformationExists(id);
        // 删除
        informationMapper.deleteById(id);
    }

    private void validateInformationExists(Integer id) {
        if (informationMapper.selectById(id) == null) {
            throw exception(INFORMATION_NOT_EXISTS);
        }
    }

    @Override
    public InformationDO getInformation(Integer id) {
        return informationMapper.selectById(id);
    }

    @Override
    public List<InformationDO> getInformationList(Collection<Integer> ids) {
        return informationMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<InformationDO> getInformationPage(InformationPageReqVO pageReqVO) {
        return informationMapper.selectPage(pageReqVO);
    }

    @Override
    public List<InformationDO> getInformationList(InformationExportReqVO exportReqVO) {
        return informationMapper.selectList(exportReqVO);
    }

}
