package com.starry.admin.modules.withdraw.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.starry.admin.modules.withdraw.entity.FreezePolicyEntity;
import com.starry.admin.modules.withdraw.mapper.FreezePolicyMapper;
import com.starry.admin.modules.withdraw.service.IFreezePolicyService;
import org.springframework.stereotype.Service;

@Service
public class FreezePolicyServiceImpl extends ServiceImpl<FreezePolicyMapper, FreezePolicyEntity>
        implements IFreezePolicyService {

    @Override
    public int resolveFreezeHours(String tenantId, String clerkId) {
        // clerk override
        LambdaQueryWrapper<FreezePolicyEntity> q1 = new LambdaQueryWrapper<>();
        q1.eq(FreezePolicyEntity::getTenantId, tenantId).eq(FreezePolicyEntity::getClerkId, clerkId);
        FreezePolicyEntity clerkPolicy = this.baseMapper.selectOne(q1);
        if (clerkPolicy != null && clerkPolicy.getFreezeHours() != null) {
            return clerkPolicy.getFreezeHours();
        }
        // tenant default
        LambdaQueryWrapper<FreezePolicyEntity> q2 = new LambdaQueryWrapper<>();
        q2.eq(FreezePolicyEntity::getTenantId, tenantId).isNull(FreezePolicyEntity::getClerkId);
        FreezePolicyEntity tenantPolicy = this.baseMapper.selectOne(q2);
        if (tenantPolicy != null && tenantPolicy.getFreezeHours() != null) {
            return tenantPolicy.getFreezeHours();
        }
        return 24; // default
    }
}
