package com.yc.ship.module.miniapplet.service.advertlog;

import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.common.util.object.BeanUtils;
import com.yc.ship.module.miniapplet.controller.admin.advertlog.vo.AdvertLogPageReqVO;
import com.yc.ship.module.miniapplet.controller.admin.advertlog.vo.AdvertLogSaveReqVO;
import com.yc.ship.module.miniapplet.dal.dataobject.advertlog.AdvertLogDO;
import com.yc.ship.module.miniapplet.dal.mysql.advertlog.AdvertLogMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import static com.yc.ship.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.yc.ship.module.miniapplet.enums.ErrorCodeConstants.ADVERT_LOG_NOT_EXISTS;


/**
 * 广告操作日志管理 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class AdvertLogServiceImpl implements AdvertLogService {

    @Resource
    private AdvertLogMapper advertLogMapper;

    @Override
    public Long createAdvertLog(AdvertLogSaveReqVO createReqVO) {
        // 插入
        AdvertLogDO advertLog = BeanUtils.toBean(createReqVO, AdvertLogDO.class);
        advertLogMapper.insert(advertLog);
        // 返回
        return advertLog.getId();
    }

    @Override
    public void updateAdvertLog(AdvertLogSaveReqVO updateReqVO) {
        // 校验存在
        validateAdvertLogExists(updateReqVO.getId());
        // 更新
        AdvertLogDO updateObj = BeanUtils.toBean(updateReqVO, AdvertLogDO.class);
        advertLogMapper.updateById(updateObj);
    }

    @Override
    public void deleteAdvertLog(Long id) {
        // 校验存在
        validateAdvertLogExists(id);
        // 删除
        advertLogMapper.deleteById(id);
    }

    private void validateAdvertLogExists(Long id) {
        if (advertLogMapper.selectById(id) == null) {
            throw exception(ADVERT_LOG_NOT_EXISTS);
        }
    }

    @Override
    public AdvertLogDO getAdvertLog(Long id) {
        return advertLogMapper.selectById(id);
    }

    @Override
    public PageResult<AdvertLogDO> getAdvertLogPage(AdvertLogPageReqVO pageReqVO) {
        return advertLogMapper.selectPage(pageReqVO);
    }

}
