package com.yolo.keyboard.service.productitems;

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.productitems.vo.*;
import com.yolo.keyboard.dal.dataobject.productitems.KeyboardProductItemsDO;
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.productitems.KeyboardProductItemsMapper;

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.PRODUCT_ITEMS_NOT_EXISTS;

/**
 * 内购商品 Service 实现类
 *
 * @author ziin
 */
@Service
@Validated
public class KeyboardProductItemsServiceImpl implements KeyboardProductItemsService {

    @Resource
    private KeyboardProductItemsMapper productItemsMapper;

    @Override
    public Long createProductItems(KeyboardProductItemsSaveReqVO createReqVO) {
        // 插入
        KeyboardProductItemsDO productItems = BeanUtils.toBean(createReqVO, KeyboardProductItemsDO.class);
        productItemsMapper.insert(productItems);

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

    @Override
    public void updateProductItems(KeyboardProductItemsSaveReqVO updateReqVO) {
        // 校验存在
        validateProductItemsExists(updateReqVO.getId());
        // 更新
        KeyboardProductItemsDO updateObj = BeanUtils.toBean(updateReqVO, KeyboardProductItemsDO.class);
        productItemsMapper.updateById(updateObj);
    }

    @Override
    public void deleteProductItems(Long id) {
        // 校验存在
        validateProductItemsExists(id);
        // 删除
        productItemsMapper.deleteById(id);
    }

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


    private void validateProductItemsExists(Long id) {
        if (productItemsMapper.selectById(id) == null) {
            throw exception(PRODUCT_ITEMS_NOT_EXISTS);
        }
    }

    @Override
    public KeyboardProductItemsDO getProductItems(Long id) {
        return productItemsMapper.selectById(id);
    }

    @Override
    public PageResult<KeyboardProductItemsDO> getProductItemsPage(KeyboardProductItemsPageReqVO pageReqVO) {
        return productItemsMapper.selectPage(pageReqVO);
    }

}