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.paychannelconfig.support.PayChannelTenantSupport;
import com.jeesharp.business.modules.pay.paysubmerchantapplyment.entity.PaySubMerchantApplyment;
import com.jeesharp.business.modules.pay.paysubmerchantapplyment.enums.PaySubMerchantApplyLocalStatusEnum;
import com.jeesharp.business.modules.pay.paysubmerchantapplyment.enums.PaySubMerchantApplyStatusEnum;
import com.jeesharp.business.modules.pay.paysubmerchantapplyment.mapper.PaySubMerchantApplymentMapper;
import com.jeesharp.business.modules.pay.paysubmerchantapplyment.service.PaySubMerchantApplymentService;
import com.jeesharp.business.modules.pay.paysubmerchantapplyment.vo.PaySubMerchantApplymentQueryVO;
import com.jeesharp.framework.mybatis.core.service.CrudService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 特约商户进件领域服务实现。
 *
 * @author jeesharp
 * @since 2026-05-26
 */
@Service
@Transactional(readOnly = true)
public class PaySubMerchantApplymentServiceImpl extends CrudService<PaySubMerchantApplymentMapper, PaySubMerchantApplyment, PaySubMerchantApplymentQueryVO>
        implements PaySubMerchantApplymentService {

    @Override
    public PaySubMerchantApplyment findByBusinessCode(String businessCode, String applymentTenantId) {
        if (!StringUtils.hasText(businessCode) || StrUtil.isBlank(applymentTenantId)) {
            return null;
        }
        LambdaQueryWrapper<PaySubMerchantApplyment> w = new LambdaQueryWrapper<>();
        w.eq(PaySubMerchantApplyment::getBusinessCode, businessCode.trim());
        w.eq(PaySubMerchantApplyment::getApplymentTenantId, applymentTenantId.trim());
        w.last("LIMIT 1");
        return getBaseMapper().selectOne(w);
    }

    @Override
    public PaySubMerchantApplyment findByApplymentTenantId(String applymentTenantId) {
        if (applymentTenantId == null && !PayChannelTenantSupport.isPlatformLevel(null)) {
            return null;
        }
        LambdaQueryWrapper<PaySubMerchantApplyment> w = new LambdaQueryWrapper<>();
        w.eq(PaySubMerchantApplyment::getApplymentTenantId, PayChannelTenantSupport.toStorageKey(applymentTenantId));
        w.last("LIMIT 1");
        return getBaseMapper().selectOne(w);
    }

    @Override
    public long countByApplymentTenantId(String applymentTenantId, String excludeId) {
        if (applymentTenantId == null && !PayChannelTenantSupport.isPlatformLevel(null)) {
            return 0;
        }
        LambdaQueryWrapper<PaySubMerchantApplyment> w = new LambdaQueryWrapper<>();
        w.eq(PaySubMerchantApplyment::getApplymentTenantId, PayChannelTenantSupport.toStorageKey(applymentTenantId));
        if (StrUtil.isNotBlank(excludeId)) {
            w.ne(PaySubMerchantApplyment::getId, excludeId.trim());
        }
        return getBaseMapper().selectCount(w);
    }

    @Override
    @Transactional(readOnly = false)
    public PaySubMerchantApplyment createPaySubMerchantApplyment(PaySubMerchantApplyment entity) {
        if (StrUtil.isNotBlank(entity.getId())) {
            throw new IllegalStateException("新增进件申请不应预置主键");
        }
        if (StrUtil.isBlank(entity.getApplyStatus())) {
            entity.setApplyStatus(PaySubMerchantApplyLocalStatusEnum.DRAFT.getValue());
        }
        if (StrUtil.isBlank(entity.getApplyStatusDesc())) {
            entity.setApplyStatusDesc(PaySubMerchantApplyLocalStatusEnum.getDescByValue(entity.getApplyStatus()));
        }
        if (StrUtil.isBlank(entity.getApplymentTenantId()) || StrUtil.isBlank(entity.getApplymentTenantCode())) {
            throw new IllegalArgumentException("进件租户信息不完整，applymentTenantId 与 applymentTenantCode 均须由前端传入");
        }
        entity.preInsert();
        getBaseMapper().insert(entity);
        return entity;
    }

    @Override
    @Transactional(readOnly = false)
    public void markSubMerchantCancelled(String applymentId, String statusDesc) {
        if (StrUtil.isBlank(applymentId)) {
            return;
        }
        PaySubMerchantApplyment applyment = get(applymentId.trim());
        if (applyment == null) {
            return;
        }
        if (PaySubMerchantApplyStatusEnum.SUB_MCH_CANCELLED.getValue().equals(applyment.getApplyStatus())) {
            return;
        }
        applyment.setApplyStatus(PaySubMerchantApplyStatusEnum.SUB_MCH_CANCELLED.getValue());
        applyment.setApplyStatusDesc(StrUtil.blankToDefault(
                statusDesc, PaySubMerchantApplyStatusEnum.SUB_MCH_CANCELLED.getDesc()));
        applyment.setLastSyncDate(LocalDateTime.now());
        updatePaySubMerchantApplyment(applyment);
    }

    @Override
    @Transactional(readOnly = false)
    public void updatePaySubMerchantApplyment(PaySubMerchantApplyment entity) {
        if (StrUtil.isBlank(entity.getId())) {
            throw new IllegalArgumentException("更新进件申请主键不能为空");
        }
        if (get(entity.getId()) == null) {
            throw new IllegalArgumentException("进件申请单不存在");
        }
        entity.preUpdate();
        getBaseMapper().update(entity);
    }

    @Override
    @Transactional(readOnly = false)
    public boolean delete(String id) {
        if (StrUtil.isBlank(id)) {
            return false;
        }
        return getBaseMapper().delete(id) > 0;
    }

    @Override
    @Transactional(readOnly = false)
    public boolean batchDelete(List<String> ids) {
        if (ids == null || ids.isEmpty()) {
            return true;
        }
        for (String id : ids) {
            if (StrUtil.isNotBlank(id)) {
                getBaseMapper().delete(id.trim());
            }
        }
        return true;
    }

}
