package com.yolo.keyboard.service.tenantcommission;

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.tenantcommission.vo.*;
import com.yolo.keyboard.dal.dataobject.tenantcommission.KeyboardTenantCommissionDO;
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.tenantcommission.KeyboardTenantCommissionMapper;

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

/**
 * 租户内购分成记录 Service 实现类
 *
 * @author ziin
 */
@Service
@Validated
public class KeyboardTenantCommissionServiceImpl implements KeyboardTenantCommissionService {

    @Resource
    private KeyboardTenantCommissionMapper tenantCommissionMapper;

    @Override
    public Long createTenantCommission(KeyboardTenantCommissionSaveReqVO createReqVO) {
        // 插入
        KeyboardTenantCommissionDO tenantCommission = BeanUtils.toBean(createReqVO, KeyboardTenantCommissionDO.class);
        tenantCommissionMapper.insert(tenantCommission);

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

    @Override
    public void updateTenantCommission(KeyboardTenantCommissionSaveReqVO updateReqVO) {
        // 校验存在
        validateTenantCommissionExists(updateReqVO.getId());
        // 更新
        KeyboardTenantCommissionDO updateObj = BeanUtils.toBean(updateReqVO, KeyboardTenantCommissionDO.class);
        tenantCommissionMapper.updateById(updateObj);
    }

    @Override
    public void deleteTenantCommission(Long id) {
        // 校验存在
        validateTenantCommissionExists(id);
        // 删除
        tenantCommissionMapper.deleteById(id);
    }

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


    private void validateTenantCommissionExists(Long id) {
        if (tenantCommissionMapper.selectById(id) == null) {
            throw exception(TENANT_COMMISSION_NOT_EXISTS);
        }
    }

    @Override
    public KeyboardTenantCommissionDO getTenantCommission(Long id) {
        return tenantCommissionMapper.selectById(id);
    }

    @Override
    public PageResult<KeyboardTenantCommissionDO> getTenantCommissionPage(KeyboardTenantCommissionPageReqVO pageReqVO) {
        return tenantCommissionMapper.selectPage(pageReqVO);
    }

}