package com.kym.service.impl;

import com.kym.entity.RechargeConfig;
import com.kym.entity.RechargeConfigGroup;
import com.kym.mapper.RechargeConfigMapper;
import com.kym.service.RechargeConfigGroupService;
import com.kym.service.RechargeConfigService;
import com.kym.service.mybatisplus.MyBaseServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 充值配置项服务实现
 *
 * @author skyline
 * @since 2024-11-15
 */
@Slf4j
@Service
public class RechargeConfigServiceImpl extends MyBaseServiceImpl<RechargeConfigMapper, RechargeConfig> implements RechargeConfigService {

    private final RechargeConfigGroupService groupService;

    public RechargeConfigServiceImpl(@Lazy RechargeConfigGroupService groupService) {
        this.groupService = groupService;
    }

    @Override
    public boolean save(RechargeConfig entity) {
        if (Boolean.TRUE.equals(entity.getIsDefault())) {
            lambdaUpdate().eq(RechargeConfig::getGroupId, entity.getGroupId())
                    .set(RechargeConfig::getIsDefault, false)
                    .update();
        }
        return super.save(entity);
    }

    @Override
    public boolean updateById(RechargeConfig entity) {
        if (Boolean.TRUE.equals(entity.getIsDefault())) {
            lambdaUpdate().eq(RechargeConfig::getGroupId, entity.getGroupId())
                    .ne(RechargeConfig::getId, entity.getId())
                    .set(RechargeConfig::getIsDefault, false)
                    .update();
        }
        return super.updateById(entity);
    }

    @Override
    public List<RechargeConfig> listByStationId(String stationId) {
        log.info("listByStationId: stationId={}", stationId);
        // 优先站点专属分组
        RechargeConfigGroup group = groupService.getByStationId(stationId);
        log.info("listByStationId: stationGroup={}", group != null ? group.getId() : null);
        if (group != null) {
            var items = lambdaQuery().eq(RechargeConfig::getGroupId, group.getId()).list();
            log.info("listByStationId: stationGroup items count={}", items.size());
            if (!items.isEmpty()) {
                return items;
            }
        }
        // 回退默认分组
        group = groupService.getDefaultGroup();
        log.info("listByStationId: defaultGroup={}", group != null ? group.getId() : null);
        if (group != null) {
            var items = lambdaQuery().eq(RechargeConfig::getGroupId, group.getId()).list();
            log.info("listByStationId: defaultGroup items count={}", items.size());
            return items;
        }
        return List.of();
    }

    @Override
    public RechargeConfig getByStationAndAmount(String stationId, Integer amount) {
        // 优先站点专属分组
        RechargeConfigGroup group = groupService.getByStationId(stationId);
        if (group != null) {
            var config = lambdaQuery()
                    .eq(RechargeConfig::getGroupId, group.getId())
                    .eq(RechargeConfig::getRechargeAmount, amount)
                    .one();
            if (config != null) {
                return config;
            }
        }
        // 回退默认分组
        group = groupService.getDefaultGroup();
        if (group != null) {
            return lambdaQuery()
                    .eq(RechargeConfig::getGroupId, group.getId())
                    .eq(RechargeConfig::getRechargeAmount, amount)
                    .one();
        }
        return null;
    }

    @Override
    public List<RechargeConfig> listByGroupId(Long groupId) {
        return lambdaQuery().eq(RechargeConfig::getGroupId, groupId).list();
    }
}
