package cn.iocoder.yudao.module.member.service.appup;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.member.controller.admin.appup.vo.AppUpPageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.appup.vo.AppUpSaveReqVO;
import cn.iocoder.yudao.module.member.controller.app.appup.vo.AppAppUpPageReqVO;
import cn.iocoder.yudao.module.member.controller.app.appup.vo.AppAppUpSaveReqVO;
import cn.iocoder.yudao.module.member.dal.dataobject.appup.AppUpDO;
import cn.iocoder.yudao.module.member.dal.mysql.appup.AppUpMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.APP_UP_NOT_EXISTS;

/**
 * app更新 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class AppUpServiceImpl implements AppUpService {

    @Resource
    private AppUpMapper appUpMapper;

    @Override
    public Long createAppUp(AppUpSaveReqVO createReqVO) {
        // 插入
        AppUpDO appUp = BeanUtils.toBean(createReqVO, AppUpDO.class);
        appUpMapper.insert(appUp);
        // 返回
        return appUp.getId();
    }

    @Override
    public Long createAppUp(AppAppUpSaveReqVO createReqVO) {
        // 插入
        AppUpDO appUp = BeanUtils.toBean(createReqVO, AppUpDO.class);
        appUpMapper.insert(appUp);
        // 返回
        return appUp.getId();
    }

    @Override
    public void updateAppUp(AppUpSaveReqVO updateReqVO) {
        // 校验存在
        validateAppUpExists(updateReqVO.getId());
        // 更新
        AppUpDO updateObj = BeanUtils.toBean(updateReqVO, AppUpDO.class);
        appUpMapper.updateById(updateObj);
    }

    @Override
    public void updateAppUp(AppAppUpSaveReqVO updateReqVO) {
        // 校验存在
        validateAppUpExists(updateReqVO.getId());
        // 更新
        AppUpDO updateObj = BeanUtils.toBean(updateReqVO, AppUpDO.class);
        appUpMapper.updateById(updateObj);
    }

    @Override
    public void deleteAppUp(Long id) {
        // 校验存在
        validateAppUpExists(id);
        // 删除
        appUpMapper.deleteById(id);
    }

    private void validateAppUpExists(Long id) {
        if (appUpMapper.selectById(id) == null) {
            throw exception(APP_UP_NOT_EXISTS);
        }
    }

    @Override
    public AppUpDO getAppUp(Long id) {
        return appUpMapper.selectById(id);
    }

    @Override
    public PageResult<AppUpDO> getAppUpPage(AppUpPageReqVO pageReqVO) {
        return appUpMapper.selectPage(pageReqVO);
    }

    @Override
    public PageResult<AppUpDO> getAppUpPage(AppAppUpPageReqVO pageReqVO) {
        return appUpMapper.selectPage(pageReqVO);
    }


}