package com.trs.digital.module.manager.service.errorwold;

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

import java.util.*;
import com.trs.digital.module.manager.controller.admin.errorwold.vo.*;
import com.trs.digital.module.manager.dal.dataobject.errorwold.ErrorwoldDO;
import com.trs.digital.framework.common.pojo.PageResult;
import com.trs.digital.framework.common.pojo.PageParam;
import com.trs.digital.framework.common.util.object.BeanUtils;

import com.trs.digital.module.manager.dal.mysql.errorwold.ErrorwoldMapper;

import static com.trs.digital.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.trs.digital.module.system.enums.ErrorCodeConstants.ERRORWOLD_NOT_EXISTS;

/**
 * 搜索词纠错 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class ErrorwoldServiceImpl implements ErrorwoldService {

    @Resource
    private ErrorwoldMapper errorwoldMapper;

    @Override
    public Long createErrorwold(ErrorwoldSaveReqVO createReqVO) {
        // 插入
        ErrorwoldDO errorwold = BeanUtils.toBean(createReqVO, ErrorwoldDO.class);
        errorwoldMapper.insert(errorwold);
        // 返回
        return errorwold.getId();
    }

    @Override
    public void updateErrorwold(ErrorwoldSaveReqVO updateReqVO) {
        // 校验存在
        validateErrorwoldExists(updateReqVO.getId());
        // 更新
        ErrorwoldDO updateObj = BeanUtils.toBean(updateReqVO, ErrorwoldDO.class);
        errorwoldMapper.updateById(updateObj);
    }

    @Override
    public void deleteErrorwold(Long id) {
        // 校验存在
        validateErrorwoldExists(id);
        // 删除
        errorwoldMapper.deleteById(id);
    }

    private void validateErrorwoldExists(Long id) {
        if (errorwoldMapper.selectById(id) == null) {
            throw exception(ERRORWOLD_NOT_EXISTS);
        }
    }

    @Override
    public ErrorwoldDO getErrorwold(Long id) {
        return errorwoldMapper.selectById(id);
    }

    @Override
    public PageResult<ErrorwoldDO> getErrorwoldPage(ErrorwoldPageReqVO pageReqVO) {
        return errorwoldMapper.selectPage(pageReqVO);
    }

}