package com.jeesharp.business.modules.pay.paysubmerchantapplyment.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jeesharp.business.modules.pay.paysubmerchantapplyment.entity.PaySubMerchantCancel;
import com.jeesharp.business.modules.pay.paysubmerchantapplyment.enums.PaySubMerchantCancelStateEnum;
import com.jeesharp.business.modules.pay.paysubmerchantapplyment.mapper.PaySubMerchantCancelMapper;
import com.jeesharp.business.modules.pay.paysubmerchantapplyment.service.PaySubMerchantCancelService;
import com.jeesharp.business.modules.pay.paysubmerchantapplyment.vo.PaySubMerchantCancelQueryVO;
import com.jeesharp.framework.mybatis.core.service.CrudService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 商户注销领域服务实现。
 *
 * @author jeesharp
 * @since 2026-06-15
 */
@Service
@Transactional(readOnly = true)
public class PaySubMerchantCancelServiceImpl
        extends CrudService<PaySubMerchantCancelMapper, PaySubMerchantCancel, PaySubMerchantCancelQueryVO>
        implements PaySubMerchantCancelService {

    @Override
    @Transactional(readOnly = false)
    public PaySubMerchantCancel createCancel(PaySubMerchantCancel entity) {
        if (StrUtil.isNotBlank(entity.getId())) {
            throw new IllegalStateException("新增商户注销记录不应预置主键");
        }
        if (StrUtil.isBlank(entity.getCancelState())) {
            entity.setCancelState(PaySubMerchantCancelStateEnum.ACCEPTED.getValue());
        }
        entity.preInsert();
        getBaseMapper().insert(entity);
        return entity;
    }

    @Override
    @Transactional(readOnly = false)
    public void updateCancel(PaySubMerchantCancel entity) {
        if (StrUtil.isBlank(entity.getId())) {
            throw new IllegalArgumentException("更新商户注销记录主键不能为空");
        }
        if (get(entity.getId()) == null) {
            throw new IllegalArgumentException("商户注销记录不存在");
        }
        entity.preUpdate();
        getBaseMapper().updateById(entity);
    }

    @Override
    public PaySubMerchantCancel findInProgressByApplymentId(String applymentId) {
        if (StrUtil.isBlank(applymentId)) {
            return null;
        }
        LambdaQueryWrapper<PaySubMerchantCancel> w = new LambdaQueryWrapper<>();
        w.eq(PaySubMerchantCancel::getApplymentId, applymentId.trim());
        w.notIn(PaySubMerchantCancel::getCancelState,
                PaySubMerchantCancelStateEnum.REJECTED.getValue(),
                PaySubMerchantCancelStateEnum.FINISH.getValue());
        w.orderByDesc(PaySubMerchantCancel::getSubmitDate);
        w.last("LIMIT 1");
        return getBaseMapper().selectOne(w);
    }

    @Override
    public PaySubMerchantCancel findByOutRequestNo(String outRequestNo) {
        if (StrUtil.isBlank(outRequestNo)) {
            return null;
        }
        LambdaQueryWrapper<PaySubMerchantCancel> w = new LambdaQueryWrapper<>();
        w.eq(PaySubMerchantCancel::getOutRequestNo, outRequestNo.trim());
        w.last("LIMIT 1");
        return getBaseMapper().selectOne(w);
    }

    @Override
    public List<PaySubMerchantCancel> listByApplymentId(String applymentId) {
        if (StrUtil.isBlank(applymentId)) {
            return List.of();
        }
        LambdaQueryWrapper<PaySubMerchantCancel> w = new LambdaQueryWrapper<>();
        w.eq(PaySubMerchantCancel::getApplymentId, applymentId.trim());
        w.orderByDesc(PaySubMerchantCancel::getSubmitDate);
        w.orderByDesc(PaySubMerchantCancel::getCreateDate);
        return getBaseMapper().selectList(w);
    }
}
