package cn.newfeifan.mall.module.distri.service.usersigninlog;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import cn.newfeifan.mall.module.distri.controller.admin.usersigninlog.vo.*;
import cn.newfeifan.mall.module.distri.dal.dataobject.usersigninlog.UserSignInLogDO;
import cn.newfeifan.mall.framework.common.pojo.PageResult;
import cn.newfeifan.mall.framework.common.util.object.BeanUtils;

import cn.newfeifan.mall.module.distri.dal.mysql.usersigninlog.UserSignInLogMapper;

import static cn.newfeifan.mall.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.newfeifan.mall.module.distri.enums.ErrorCodeConstants.*;

/**
 * 用户签到日志 Service 实现类
 *
 * @author 非繁人
 */
@Service
@Validated
public class UserSignInLogServiceImpl implements UserSignInLogService {

    @Resource
    private UserSignInLogMapper userSignInLogMapper;

    @Override
    public Long createUserSignInLog(UserSignInLogSaveReqVO createReqVO) {
        // 插入
        UserSignInLogDO userSignInLog = BeanUtils.toBean(createReqVO, UserSignInLogDO.class);
        userSignInLogMapper.insert(userSignInLog);
        // 返回
        return userSignInLog.getId();
    }

    @Override
    public void updateUserSignInLog(UserSignInLogSaveReqVO updateReqVO) {
        // 校验存在
        validateUserSignInLogExists(updateReqVO.getId());
        // 更新
        UserSignInLogDO updateObj = BeanUtils.toBean(updateReqVO, UserSignInLogDO.class);
        userSignInLogMapper.updateById(updateObj);
    }

    @Override
    public void deleteUserSignInLog(Long id) {
        // 校验存在
        validateUserSignInLogExists(id);
        // 删除
        userSignInLogMapper.deleteById(id);
    }

    private void validateUserSignInLogExists(Long id) {
        if (userSignInLogMapper.selectById(id) == null) {
            throw exception(USER_SIGN_IN_LOG_NOT_EXISTS);
        }
    }

    @Override
    public UserSignInLogDO getUserSignInLog(Long id) {
        return userSignInLogMapper.selectById(id);
    }

    @Override
    public PageResult<UserSignInLogDO> getUserSignInLogPage(UserSignInLogPageReqVO pageReqVO) {
        return userSignInLogMapper.selectPage(pageReqVO);
    }

}