
package com.yami.shop.platform.controller.smqjh;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yami.shop.bean.model.Product;
import com.yami.shop.bean.model.Sku;
import com.yami.shop.bean.vo.XSBProductVO;
import com.yami.shop.bean.vo.XSBSkuVO;
import com.yami.shop.service.ProductService;
import com.yami.shop.service.SkuService;
import com.yami.shop.utils.SmqjhUtil;
import lombok.AllArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 星闪豹数据同步问题
 *
 * @author FBJ
 * @version 1.0.0
 * @since 2025-12-15
 */
@RestController
@RequestMapping("/platform/xsb")
@AllArgsConstructor
@Log4j2
public class XSBController {
    private final ProductService productService;
    private final SkuService skuService;
    private final SmqjhUtil smqjhUtil;

    /**
     * 同步商品
     */
    @PostMapping("/syncProd")
    public String syncProd() {
        List<Product> list = productService.list(new LambdaQueryWrapper<Product>()
                .eq(Product::getIsDelete, 0)
                .eq(Product::getStatus, 1));
        try {
            List<XSBProductVO> shopSkuList = new ArrayList<>();
            for (Product productAdd : list) {
                XSBProductVO xsbProductVO = new XSBProductVO();
                BeanUtils.copyProperties(productAdd, xsbProductVO);
                xsbProductVO.setThirdSpuId(String.valueOf(productAdd.getProdId()));
                xsbProductVO.setBusinessType("XSB");
                shopSkuList.add(xsbProductVO);
            }
            String syncUrl = smqjhUtil.getSmqjhPmsUrl() + "/syncProductData";
            log.info("推送url: {}",syncUrl);
            // 将frontCategory对象作为JSON放入请求体
            String response = com.yami.shop.common.util.HttpUtil.post(syncUrl, shopSkuList);
            log.info("推送frontCategory到同步服务器结果: {}", response);
        } catch (Exception e) {
            log.error("推送frontCategory到同步服务器失败: {}", e.getMessage(), e);
        }
        return "success";
    }

    /**
     * 同步商品SKUcc
     */
    @PostMapping("/syncSku")
    public String syncSku() {
        try {
            List<XSBSkuVO> shopSkuList = new ArrayList<>();
            List<Sku> list = skuService.list(new LambdaQueryWrapper<Sku>().eq(Sku::getIsDelete, 0));
            for (Sku sku : list) {
                XSBSkuVO xsbSkuVO = new XSBSkuVO();
                BeanUtils.copyProperties(sku, xsbSkuVO);
                xsbSkuVO.setThirdSkuId(String.valueOf(sku.getSkuId()));
                xsbSkuVO.setBusinessType("XSB");
                shopSkuList.add(xsbSkuVO);
            }
            String syncUrl = smqjhUtil.getSmqjhPmsUrl() + "/syncSkuData";
            log.info("推送url: {}",syncUrl);
            // 将frontCategory对象作为JSON放入请求体
            String response = com.yami.shop.common.util.HttpUtil.post(syncUrl, shopSkuList);
            log.info("推送frontCategory到同步服务器结果: {}", response);
        } catch (Exception e) {
            log.error("推送frontCategory到同步服务器失败: {}", e.getMessage(), e);
        }

        return "";
    }
}
