package com.yolo.keyboard.service.tenantwithdraworder;

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.tenantwithdraworder.vo.*;
import com.yolo.keyboard.dal.dataobject.tenantwithdraworder.KeyboardTenantWithdrawOrderDO;
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.tenantwithdraworder.KeyboardTenantWithdrawOrderMapper;

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_WITHDRAW_ORDER_NOT_EXISTS;

/**
 * 租户提现订单表（申请-审核-打款-完成/失败） Service 实现类
 *
 * @author ziin
 */
@Service
@Validated
public class KeyboardTenantWithdrawOrderServiceImpl implements KeyboardTenantWithdrawOrderService {

    @Resource
    private KeyboardTenantWithdrawOrderMapper tenantWithdrawOrderMapper;

    @Override
    public Long createTenantWithdrawOrder(KeyboardTenantWithdrawOrderSaveReqVO createReqVO) {
        // 插入
        KeyboardTenantWithdrawOrderDO tenantWithdrawOrder = BeanUtils.toBean(createReqVO, KeyboardTenantWithdrawOrderDO.class);
        tenantWithdrawOrderMapper.insert(tenantWithdrawOrder);

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

    @Override
    public void updateTenantWithdrawOrder(KeyboardTenantWithdrawOrderSaveReqVO updateReqVO) {
        // 校验存在
        validateTenantWithdrawOrderExists(updateReqVO.getId());
        // 更新
        KeyboardTenantWithdrawOrderDO updateObj = BeanUtils.toBean(updateReqVO, KeyboardTenantWithdrawOrderDO.class);
        tenantWithdrawOrderMapper.updateById(updateObj);
    }

    @Override
    public void deleteTenantWithdrawOrder(Long id) {
        // 校验存在
        validateTenantWithdrawOrderExists(id);
        // 删除
        tenantWithdrawOrderMapper.deleteById(id);
    }

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


    private void validateTenantWithdrawOrderExists(Long id) {
        if (tenantWithdrawOrderMapper.selectById(id) == null) {
            throw exception(TENANT_WITHDRAW_ORDER_NOT_EXISTS);
        }
    }

    @Override
    public KeyboardTenantWithdrawOrderDO getTenantWithdrawOrder(Long id) {
        return tenantWithdrawOrderMapper.selectById(id);
    }

    @Override
    public PageResult<KeyboardTenantWithdrawOrderDO> getTenantWithdrawOrderPage(KeyboardTenantWithdrawOrderPageReqVO pageReqVO) {
        return tenantWithdrawOrderMapper.selectPage(pageReqVO);
    }

}