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

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.digsearch.vo.*;
import com.trs.digital.module.manager.dal.dataobject.digsearch.DigsearchDO;
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.digsearch.DigsearchMapper;

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

/**
 * 资源搜索 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class DigsearchServiceImpl implements DigsearchService {

    @Resource
    private DigsearchMapper digsearchMapper;

    @Override
    public Long createDigsearch(DigsearchSaveReqVO createReqVO) {
        // 插入
        DigsearchDO digsearch = BeanUtils.toBean(createReqVO, DigsearchDO.class);
        digsearchMapper.insert(digsearch);
        // 返回
        return digsearch.getId();
    }

    @Override
    public void updateDigsearch(DigsearchSaveReqVO updateReqVO) {
        // 校验存在
        validateDigsearchExists(updateReqVO.getId());
        // 更新
        DigsearchDO updateObj = BeanUtils.toBean(updateReqVO, DigsearchDO.class);
        digsearchMapper.updateById(updateObj);
    }

    @Override
    public void deleteDigsearch(Long id) {
        // 校验存在
        validateDigsearchExists(id);
        // 删除
        digsearchMapper.deleteById(id);
    }

    private void validateDigsearchExists(Long id) {
        if (digsearchMapper.selectById(id) == null) {
            throw exception(DIG_SEARCH_NOT_EXISTS);
        }
    }

    @Override
    public DigsearchDO getDigsearch(Long id) {
        return digsearchMapper.selectById(id);
    }

    @Override
    public PageResult<DigsearchDO> getDigsearchPage(DigsearchPageReqVO pageReqVO) {
        return digsearchMapper.selectPage(pageReqVO);
    }

}