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 com.yolo.keyboard.framework.tenant.core.context.TenantContextHolder;
import com.yolo.keyboard.module.system.dal.dataobject.tenant.TenantDO;
import com.yolo.keyboard.module.system.dal.mysql.tenant.TenantMapper;

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;

    @Resource
    private TenantMapper tenantMapper;

    @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) {
        // 如果当前租户的 tenantLevel 不等于 0，只能查询属于自己的数据
        Long currentTenantId = TenantContextHolder.getTenantId();
        if (currentTenantId != null) {
            TenantDO currentTenant = tenantMapper.selectById(currentTenantId);
            if (currentTenant != null && currentTenant.getTenantLevel() != null
                    && currentTenant.getTenantLevel() != 0) {
                pageReqVO.setTenantId(currentTenantId);
            }
        }
        return tenantCommissionMapper.selectPage(pageReqVO);
    }

}