package cn.reghao.tnb.user.app.rpc;

import cn.reghao.jutil.jdk.db.PageList;
import cn.reghao.tnb.account.api.dto.AccountAvatar;
import cn.reghao.tnb.account.api.iface.AccountQuery;
import cn.reghao.tnb.content.api.iface.UserContentService;
import cn.reghao.tnb.user.api.dto.UserInfo;
import cn.reghao.tnb.user.api.dto.UserSearch;
import cn.reghao.tnb.user.api.dto.VipPlanInfo;
import cn.reghao.tnb.user.api.iface.AdminUserService;
import cn.reghao.tnb.user.app.model.po.VipPlan;
import cn.reghao.tnb.user.app.service.UserVipService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author reghao
 * @date 2024-02-15 09:17:08
 */
@DubboService
@Service
public class AdminUserServiceImpl implements AdminUserService {
    @DubboReference(check = false, retries = 0)
    private AccountQuery accountQuery;
    @DubboReference(check = false, retries = 0, timeout = 30_000)
    private UserContentService userContentService;
    private final UserVipService userVipService;

    public AdminUserServiceImpl(UserVipService userVipService) {
        this.userVipService = userVipService;
    }

    @Override
    public PageList<UserInfo> getUserList(UserSearch userSearch) {
        return PageList.empty();
    }

    @Override
    public List<AccountAvatar> getPublishUsers(int publishType) {
        List<Long> userIds = userContentService.getContentUser(publishType);
        return accountQuery.getAccountAvatars(userIds);
    }

    @Override
    public List<VipPlanInfo> getVipPlans() {
        List<VipPlan> list = userVipService.getVipPlans();
        return list.stream().map(this::getVipPlanInfo).collect(Collectors.toList());
    }

    private VipPlanInfo getVipPlanInfo(VipPlan vipPlan) {
        int planId = vipPlan.getPlanId();
        String name = vipPlan.getName();
        double price = vipPlan.getPrice();
        int duration = vipPlan.getDuration();
        return new VipPlanInfo(planId, name, price, duration);
    }
}
