package org.wfc.user.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.wfc.common.security.utils.SecurityUtils;
import org.wfc.user.domain.UAccount;
import org.wfc.user.domain.UAccountPackage;
import org.wfc.user.mapper.UAccountMapper;
import org.wfc.user.mapper.UAccountPackageMapper;
import org.wfc.user.service.IUAccountPackageService;

import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 用户平台-账户套餐表 服务实现类
 * </p>
 *
 * @author sys
 * @since 2025-03-13
 */
@Service
public class UAccountPackageServiceImpl extends ServiceImpl<UAccountPackageMapper, UAccountPackage> implements IUAccountPackageService {

    @Autowired
    private UAccountMapper accountMapper;

    @Override
    public List<UAccountPackage> queryList(UAccountPackage uAccountPackage) {
        UAccount account = accountMapper.selectOne(Wrappers.<UAccount>lambdaQuery().eq(UAccount::getUserId, SecurityUtils.getLoginUser().getUserid()), false);
        if (ObjectUtil.isNull(account)) {
            return Collections.emptyList();
        }
        uAccountPackage.setAccountId(account.getId());
        return this.list(buildQueryWrapper(uAccountPackage));
    }

    private LambdaQueryWrapper<UAccountPackage> buildQueryWrapper(UAccountPackage bo) {
        LambdaQueryWrapper<UAccountPackage> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getAccountId() != null, UAccountPackage::getAccountId, bo.getAccountId());
        lqw.eq(bo.getPackageId() != null, UAccountPackage::getPackageId, bo.getPackageId());
        lqw.eq(bo.getTraffic() != null, UAccountPackage::getTraffic, bo.getTraffic());
        lqw.eq(bo.getDuration() != null, UAccountPackage::getDuration, bo.getDuration());
        lqw.eq(bo.getClientNum() != null, UAccountPackage::getClientNum, bo.getClientNum());
        lqw.eq(bo.getExpiredTime() != null, UAccountPackage::getExpiredTime, bo.getExpiredTime());
        lqw.like(StrUtil.isNotBlank(bo.getPackageName()), UAccountPackage::getPackageName, bo.getPackageName());
        lqw.eq(bo.getPeriodNum() != null, UAccountPackage::getPeriodNum, bo.getPeriodNum());
        lqw.eq(bo.getPeriodType() != null, UAccountPackage::getPeriodType, bo.getPeriodType());
        lqw.eq(bo.getPrice() != null, UAccountPackage::getPrice, bo.getPrice());
        lqw.like(StrUtil.isNotBlank(bo.getRemark()), UAccountPackage::getRemark, bo.getRemark());
        lqw.eq(bo.getRateLimitEnable() != null, UAccountPackage::getRateLimitEnable, bo.getRateLimitEnable());
        lqw.eq(bo.getTrafficEnable() != null, UAccountPackage::getTrafficEnable, bo.getTrafficEnable());
        lqw.eq(bo.getDurationEnable() != null, UAccountPackage::getDurationEnable, bo.getDurationEnable());
        lqw.eq(bo.getClientNumEnable() != null, UAccountPackage::getClientNumEnable, bo.getClientNumEnable());
        lqw.like(StrUtil.isNotBlank(bo.getRateLimitName()), UAccountPackage::getRateLimitName, bo.getRateLimitName());
        lqw.eq(bo.getDownLimit() != null, UAccountPackage::getDownLimit, bo.getDownLimit());
        lqw.eq(bo.getDownLimitEnable() != null, UAccountPackage::getDownLimitEnable, bo.getDownLimitEnable());
        lqw.eq(bo.getUpLimit() != null, UAccountPackage::getUpLimit, bo.getUpLimit());
        lqw.eq(bo.getUpLimitEnable() != null, UAccountPackage::getUpLimitEnable, bo.getUpLimitEnable());
        return lqw;
    }
}
