package com.yolo.keyboard.service.userpurchaserecords;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.yolo.keyboard.controller.admin.userpurchaserecords.vo.*;
import com.yolo.keyboard.dal.dataobject.userpurchaserecords.KeyboardUserPurchaseRecordsDO;
import com.yolo.keyboard.framework.common.pojo.PageResult;
import com.yolo.keyboard.framework.common.pojo.PageParam;
import com.yolo.keyboard.framework.common.util.object.BeanUtils;

import com.yolo.keyboard.dal.mysql.userpurchaserecords.KeyboardUserPurchaseRecordsMapper;

import static com.yolo.keyboard.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.yolo.keyboard.framework.common.util.collection.CollectionUtils.convertList;
import static com.yolo.keyboard.framework.common.util.collection.CollectionUtils.diffList;
import static com.yolo.keyboard.module.infra.enums.ErrorCodeConstants.USER_PURCHASE_RECORDS_NOT_EXISTS;

/**
 * 用户内购记录 Service 实现类
 *
 * @author ziin
 */
@Service
@Validated
public class KeyboardUserPurchaseRecordsServiceImpl implements KeyboardUserPurchaseRecordsService {

    @Resource
    private KeyboardUserPurchaseRecordsMapper userPurchaseRecordsMapper;

    @Override
    public Integer createUserPurchaseRecords(KeyboardUserPurchaseRecordsSaveReqVO createReqVO) {
        // 插入
        KeyboardUserPurchaseRecordsDO userPurchaseRecords = BeanUtils.toBean(createReqVO, KeyboardUserPurchaseRecordsDO.class);
        userPurchaseRecordsMapper.insert(userPurchaseRecords);

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

    @Override
    public void updateUserPurchaseRecords(KeyboardUserPurchaseRecordsSaveReqVO updateReqVO) {
        // 校验存在
        validateUserPurchaseRecordsExists(updateReqVO.getId());
        // 更新
        KeyboardUserPurchaseRecordsDO updateObj = BeanUtils.toBean(updateReqVO, KeyboardUserPurchaseRecordsDO.class);
        userPurchaseRecordsMapper.updateById(updateObj);
    }

    @Override
    public void deleteUserPurchaseRecords(Integer id) {
        // 校验存在
        validateUserPurchaseRecordsExists(id);
        // 删除
        userPurchaseRecordsMapper.deleteById(id);
    }

    @Override
        public void deleteUserPurchaseRecordsListByIds(List<Integer> ids) {
        // 删除
        userPurchaseRecordsMapper.deleteByIds(ids);
        }


    private void validateUserPurchaseRecordsExists(Integer id) {
        if (userPurchaseRecordsMapper.selectById(id) == null) {
            throw exception(USER_PURCHASE_RECORDS_NOT_EXISTS);
        }
    }

    @Override
    public KeyboardUserPurchaseRecordsDO getUserPurchaseRecords(Integer id) {
        return userPurchaseRecordsMapper.selectById(id);
    }

    @Override
    public PageResult<KeyboardUserPurchaseRecordsDO> getUserPurchaseRecordsPage(KeyboardUserPurchaseRecordsPageReqVO pageReqVO) {
        return userPurchaseRecordsMapper.selectPage(pageReqVO);
    }

}