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.PaySubMerchantSettlementModify;
import com.jeesharp.business.modules.pay.paysubmerchantapplyment.enums.PaySubMerchantSettlementModifyAuditEnum;
import com.jeesharp.business.modules.pay.paysubmerchantapplyment.mapper.PaySubMerchantSettlementModifyMapper;
import com.jeesharp.business.modules.pay.paysubmerchantapplyment.service.PaySubMerchantSettlementModifyService;
import com.jeesharp.business.modules.pay.paysubmerchantapplyment.vo.PaySubMerchantSettlementModifyQueryVO;
import com.jeesharp.framework.mybatis.core.service.CrudService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 修改结算账户领域服务实现。
 *
 * @author jeesharp
 * @since 2026-06-12
 */
@Service
@Transactional(readOnly = true)
public class PaySubMerchantSettlementModifyServiceImpl
        extends CrudService<PaySubMerchantSettlementModifyMapper, PaySubMerchantSettlementModify, PaySubMerchantSettlementModifyQueryVO>
        implements PaySubMerchantSettlementModifyService {

    @Override
    @Transactional(readOnly = false)
    public PaySubMerchantSettlementModify createSettlementModify(PaySubMerchantSettlementModify entity) {
        if (StrUtil.isNotBlank(entity.getId())) {
            throw new IllegalStateException("新增修改结算账户记录不应预置主键");
        }
        if (StrUtil.isBlank(entity.getAuditStatus())) {
            entity.setAuditStatus(PaySubMerchantSettlementModifyAuditEnum.AUDITING.getValue());
        }
        entity.preInsert();
        getBaseMapper().insert(entity);
        return entity;
    }

    @Override
    @Transactional(readOnly = false)
    public void updateSettlementModify(PaySubMerchantSettlementModify entity) {
        if (StrUtil.isBlank(entity.getId())) {
            throw new IllegalArgumentException("更新修改结算账户记录主键不能为空");
        }
        if (get(entity.getId()) == null) {
            throw new IllegalArgumentException("修改结算账户记录不存在");
        }
        entity.preUpdate();
        getBaseMapper().updateById(entity);
    }

    @Override
    public PaySubMerchantSettlementModify findAuditingByApplymentId(String applymentId) {
        if (StrUtil.isBlank(applymentId)) {
            return null;
        }
        LambdaQueryWrapper<PaySubMerchantSettlementModify> w = new LambdaQueryWrapper<>();
        w.eq(PaySubMerchantSettlementModify::getApplymentId, applymentId.trim());
        w.eq(PaySubMerchantSettlementModify::getAuditStatus, PaySubMerchantSettlementModifyAuditEnum.AUDITING.getValue());
        w.last("LIMIT 1");
        return getBaseMapper().selectOne(w);
    }

    @Override
    public long countTodaySubmitByApplymentId(String applymentId) {
        if (StrUtil.isBlank(applymentId)) {
            return 0;
        }
        LocalDateTime start = LocalDate.now().atStartOfDay();
        LocalDateTime end = LocalDate.now().atTime(LocalTime.MAX);
        LambdaQueryWrapper<PaySubMerchantSettlementModify> w = new LambdaQueryWrapper<>();
        w.eq(PaySubMerchantSettlementModify::getApplymentId, applymentId.trim());
        w.ge(PaySubMerchantSettlementModify::getSubmitDate, start);
        w.le(PaySubMerchantSettlementModify::getSubmitDate, end);
        return getBaseMapper().selectCount(w);
    }

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