package com.ylx.order.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ylx.common.utils.DateUtils;
import com.ylx.order.domain.RefundRuleDetail;
import com.ylx.order.mapper.RefundRuleDetailMapper;
import com.ylx.order.service.RefundRuleDetailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Service
public class RefundRuleDetailServiceImpl extends ServiceImpl<RefundRuleDetailMapper, RefundRuleDetail> implements RefundRuleDetailService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void clearDetailsByStage(Integer stageType, String operator) {
        if (ObjectUtil.isNull(stageType)) {
            throw new IllegalArgumentException("阶段类型不能为空");
        }
        // 构建更新条件：只修改 is_delete 和 update_by/update_time
        LambdaUpdateWrapper<RefundRuleDetail> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(RefundRuleDetail::getStageType, stageType)
                .eq(RefundRuleDetail::getIsDelete, 0) // 确保只处理未删除的数据
                .set(RefundRuleDetail::getIsDelete, 1) // 逻辑删除标记
                .set(RefundRuleDetail::getUpdateBy, operator)
                .set(RefundRuleDetail::getUpdateTime, DateUtils.getNowDate());

        int count = this.baseMapper.update(null, updateWrapper);
        log.info("逻辑删除阶段 [{}] 的退款规则明细，影响行数: {}", stageType, count);

    }
}
