package com.yolo.keyboard.service.themepurchase;

import com.yolo.keyboard.controller.admin.themepurchase.vo.KeyboardThemePurchasePageReqVO;
import com.yolo.keyboard.controller.admin.themepurchase.vo.KeyboardThemePurchaseSaveReqVO;
import com.yolo.keyboard.dal.dataobject.themepurchase.KeyboardThemePurchaseDO;
import com.yolo.keyboard.dal.mysql.themepurchase.KeyboardThemePurchaseMapper;
import com.yolo.keyboard.framework.common.pojo.PageResult;
import com.yolo.keyboard.framework.common.util.object.BeanUtils;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.List;

import static com.yolo.keyboard.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.yolo.keyboard.module.infra.enums.ErrorCodeConstants.THEME_PURCHASE_NOT_EXISTS;


/**
 * 皮肤购买记录表（积分支付） Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class KeyboardThemePurchaseServiceImpl implements KeyboardThemePurchaseService {

    @Resource
    private KeyboardThemePurchaseMapper themePurchaseMapper;

    @Override
    public Long createThemePurchase(KeyboardThemePurchaseSaveReqVO createReqVO) {
        // 插入
        KeyboardThemePurchaseDO themePurchase = BeanUtils.toBean(createReqVO, KeyboardThemePurchaseDO.class);
        themePurchaseMapper.insert(themePurchase);

        // 返回
        return themePurchase.getId();
    }

    @Override
    public void updateThemePurchase(KeyboardThemePurchaseSaveReqVO updateReqVO) {
        // 校验存在
        validateThemePurchaseExists(updateReqVO.getId());
        // 更新
        KeyboardThemePurchaseDO updateObj = BeanUtils.toBean(updateReqVO, KeyboardThemePurchaseDO.class);
        themePurchaseMapper.updateById(updateObj);
    }

    @Override
    public void deleteThemePurchase(Long id) {
        // 校验存在
        validateThemePurchaseExists(id);
        // 删除
        themePurchaseMapper.deleteById(id);
    }

    @Override
        public void deleteThemePurchaseListByIds(List<Long> ids) {
        // 删除
        themePurchaseMapper.deleteByIds(ids);
        }


    private void validateThemePurchaseExists(Long id) {
        if (themePurchaseMapper.selectById(id) == null) {
            throw exception(THEME_PURCHASE_NOT_EXISTS);
        }
    }

    @Override
    public KeyboardThemePurchaseDO getThemePurchase(Long id) {
        return themePurchaseMapper.selectById(id);
    }

    @Override
    public PageResult<KeyboardThemePurchaseDO> getThemePurchasePage(KeyboardThemePurchasePageReqVO pageReqVO) {
        return themePurchaseMapper.selectPage(pageReqVO);
    }

}