package cn.reghao.tnb.content.app.mall.service;

import cn.reghao.file.api.dto.ObjectMeta;
import cn.reghao.file.api.iface.OssService;
import cn.reghao.jutil.jdk.web.db.PageList;
import cn.reghao.jutil.jdk.web.result.Result;
import cn.reghao.jutil.jdk.serializer.JsonConverter;
import cn.reghao.jutil.jdk.string.SnowFlake;
import cn.reghao.tnb.auth.api.iface.AccountQuery;
import cn.reghao.tnb.content.api.dto.TaobaoItem;
import cn.reghao.tnb.content.app.mall.db.mapper.ProductMapper;
import cn.reghao.tnb.content.app.mall.db.repository.MallRepository;
import cn.reghao.tnb.content.app.mall.feign.UserDiscoveryClient;
import cn.reghao.tnb.content.app.mall.feign.UserLoadBalancerClient;
import cn.reghao.tnb.content.app.mall.model.dto.ProductAddDto;
import cn.reghao.tnb.content.app.mall.model.po.Product;
import cn.reghao.tnb.content.app.mall.feign.UserFeignClient;
import cn.reghao.tnb.user.api.dto.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author reghao
 * @date 2024-04-16 11:00:17
 */
@Slf4j
@Service
public class ProductService {
    @DubboReference(check = false)
    private OssService ossService;
    @DubboReference(check = false, retries = 0, timeout = 1000, mock = "fallback")
    private AccountQuery accountQuery;

    private final int pageSize = 12;
    private final SnowFlake idGenerator;
    private final ProductMapper productMapper;
    private final MallRepository mallRepository;
    private final UserFeignClient userFeignClient;
    private UserDiscoveryClient userDiscoveryClient;
    private UserLoadBalancerClient userLoadBalancerClient;

    public ProductService(ProductMapper productMapper, MallRepository mallRepository,
                          UserFeignClient userFeignClient, UserDiscoveryClient userDiscoveryClient,
                          UserLoadBalancerClient userLoadBalancerClient) {
        this.idGenerator = new SnowFlake(1L, 1L);
        this.productMapper = productMapper;
        this.mallRepository = mallRepository;
        this.userFeignClient = userFeignClient;
        this.userDiscoveryClient = userDiscoveryClient;
        this.userLoadBalancerClient = userLoadBalancerClient;
    }

    public void add(TaobaoItem taobaoItem) {
        Product product = new Product(taobaoItem);
        productMapper.save(product);
    }

    public Result addProduct(ProductAddDto productAddDto) throws Exception {
        String coverFileId = productAddDto.getCoverFileId();
        ObjectMeta objectMeta = ossService.getObjectMeta(coverFileId);
        if (objectMeta == null) {
            String errMsg = String.format("封面文件 %s 在 oss 中不存在", coverFileId);
            return Result.fail(errMsg);
        }

        long productId = idGenerator.nextId();
        String coverUrl = objectMeta.getUrl();
        Product product = new Product(productAddDto, productId, coverUrl);
        productMapper.save(product);
        return Result.success();
    }

    public PageList<Product> getPage(int pageNumber) {
        int total = 1;
        List<Product> list = productMapper.findAll();
        list.forEach(product -> {
            long sellerId = product.getSellerId();
            String userInfoJson = userFeignClient.getUserInfo(sellerId);
            UserInfo userInfo = JsonConverter.jsonToObject(userInfoJson, UserInfo.class);
        });

        return PageList.pageList(pageNumber, pageSize, total, list);
    }

    public Product getProduct(Long itemId) {
        Product product = mallRepository.getProduct(itemId);
        long sellerId = product.getSellerId();
        String userInfoJson = userFeignClient.getUserInfo(sellerId);
        UserInfo userInfo = JsonConverter.jsonToObject(userInfoJson, UserInfo.class);
        //UserInfo userInfo1 = userDiscoveryClient.getUserInfo(sellerId);
        //UserInfo userInfo2 = userLoadBalancerClient.getUserInfo(sellerId);

        return mallRepository.getProduct(itemId);
    }
}
