package com.tmerclub.cloud.systemtest.data.supplier;

import com.alibaba.fastjson2.JSON;
import com.tmerclub.cloud.api.product.dto.CategoryShopDTO;
import com.tmerclub.cloud.common.product.dto.SkuDTO;
import com.tmerclub.cloud.common.product.dto.SpuDTO;
import com.tmerclub.cloud.common.product.vo.SpuVO;
import com.tmerclub.cloud.common.product.vo.app.CategoryAppVO;
import com.tmerclub.cloud.product.dto.SpuPageSearchDTO;
import com.moyuer.cloud.systemtest.api.amount.CategoryApi;
import com.moyuer.cloud.systemtest.api.amount.ProductApi;
import com.moyuer.cloud.systemtest.api.store.ShopSubstituteSalesApi;
import com.moyuer.cloud.systemtest.api.supplier.SupplierProductApi;
import com.moyuer.cloud.systemtest.data.amount.SpuData;
import com.moyuer.cloud.systemtest.data.platform.CategoryDtoData;
import com.moyuer.cloud.systemtest.data.store.ShopSubstituteSalesDtoData;

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

/**
 * @author Tu
 */
public class SupplierProductData {
    private static final Integer SUPPLIER_DELIVERY = 1;
    private static final Integer SHOP_DELIVERY = 2;

    /**
     * 供应商发布商品
     * @param supplyPrice 供货价
     * @param deliverTemplateId 运费模板id
     * @param isMainSupplier 是否是主供应商：supplier_token
     */
    public static Long getSupplierProductId(Long supplyPrice, Long deliverTemplateId, Boolean isMainSupplier) {
        List<CategoryAppVO> supplierCategoryList = CategoryApi.listSupplierCategory(isMainSupplier);
        if (supplierCategoryList.size() == 0) {
            CategoryApi.addSupplierCategory(getCategoryShopDtoList(CategoryApi.listSupplierApplyCategory(isMainSupplier).get(0).getCategoryId()), isMainSupplier);
        }
        Long categoryId = CategoryApi.listSupplierCategory(isMainSupplier).get(0).getCategoryId();

        double deductionRateTemp = 1.99;
        if (CategoryApi.getCategoryInfo(categoryId).getDeductionRate() != deductionRateTemp) {
            CategoryApi.update(CategoryDtoData.getCategoryDTO(categoryId));
        }
        return SupplierProductApi.createProduct(SpuData.getSupplierSpu(categoryId, deliverTemplateId, supplyPrice), isMainSupplier);
    }

    /**
     * 商家代销供应商商品
     * @param supplierProductId 供应商商品id
     * @param priceFee 销售价
     * @param supplierDeliveryType 发货方式 1 供应商发货 2 商家发货
     * @param deliverTemplateId 商家发货时运费模板id
     * @param isMainSupplier 是否是主供应商的商品：supplier_token
     * @param isMainShop 是否是主商家 :shop_token
     */
    public static Long getProductId(Long supplierProductId, Long priceFee, Integer supplierDeliveryType, Long deliverTemplateId, Boolean isMainSupplier, Boolean isMainShop) {
        //修改代销设置为手动设置，直接转为待采购订单
        ShopSubstituteSalesApi.updateShopSubstituteSales(ShopSubstituteSalesDtoData.getShopSubstituteSalesDTO(1, 0, null, 0), true);

        //如果商家端没有签约即将发布商品的分类，则签约
        Long categoryId = SupplierProductApi.info(supplierProductId, isMainSupplier).getCategoryId();
        List<CategoryAppVO> categoryAppVOList = CategoryApi.listSigningCategory(isMainShop);
        List<Long> collect = categoryAppVOList.stream().map(CategoryAppVO::getCategoryId).toList();
        if(!collect.contains(categoryId)) {
            CategoryApi.addSigningCategory(getCategoryShopDtoList(categoryId), isMainShop);
        }
        Object obj = ProductApi.addSupplierProduct(Collections.singletonList(supplierProductId), isMainShop).get(0);
        Long productId;
        if (obj instanceof Integer) {
            Integer ob = (Integer) obj;
            productId = ob.longValue();
        } else {
            productId = (Long) obj;
        }
        SpuVO spuVO = ProductApi.info(productId, isMainShop);
        SpuDTO spuDTO = JSON.parseObject(JSON.toJSONString(spuVO), SpuDTO.class);
        List<SkuDTO> skuDTOList = JSON.parseArray(JSON.toJSONString(spuVO.getSkus()), SkuDTO.class);
        if (supplierDeliveryType.equals(SUPPLIER_DELIVERY) && ShopSubstituteSalesApi.getShopSubstituteSales().getSalesType().equals(0)) {
            spuDTO.setSkuList(skuDTOList);
        }
        if (supplierDeliveryType.equals(SUPPLIER_DELIVERY) && ShopSubstituteSalesApi.getShopSubstituteSales().getSalesType().equals(1)) {
            skuDTOList.forEach(i->i.setPriceFee(priceFee));
            spuDTO.setSkuList(skuDTOList);
        }
        if (supplierDeliveryType.equals(SHOP_DELIVERY)) {
            skuDTOList.forEach(i->i.setPriceFee(priceFee));
            skuDTOList.forEach(i->i.setStock(100));
            skuDTOList.forEach(i->i.setChangeStock(100));
            spuDTO.setSkuList(skuDTOList);
        }
        spuDTO.setSkuList(skuDTOList);
        Long shopCategoryId = CategoryApi.listShopCategory(isMainShop).get(1).getCategoryId();
        spuDTO.setShopCategoryId(shopCategoryId);
        spuDTO.setPriceFee(priceFee);
        spuDTO.setSupplierDeliveryType(supplierDeliveryType);
        if (supplierDeliveryType.equals(SHOP_DELIVERY)) {
            spuDTO.setDeliveryTemplateId(deliverTemplateId);
        }

        ProductApi.update(spuDTO, isMainShop);
        ProductApi.onlineProduct(getSpuPageSearchDTO(productId), isMainShop);
        return productId;
    }

    public static List<CategoryShopDTO> getCategoryShopDtoList(Long categoryId) {
        List<CategoryShopDTO> categoryShopDTOList= new ArrayList<>();
        CategoryShopDTO categoryShopDTO = new CategoryShopDTO();
        categoryShopDTO.setCategoryId(categoryId);
        categoryShopDTO.setQualifications(null);
        categoryShopDTOList.add(categoryShopDTO);
        return categoryShopDTOList;
    }
    public static SpuPageSearchDTO getSpuPageSearchDTO(Long productId) {
        SpuPageSearchDTO spuPageSearchDTO = new SpuPageSearchDTO();
//        spuPageSearchDTO.setIsCommission(1);
        spuPageSearchDTO.setSpuId(productId);
        spuPageSearchDTO.setStatus(1);
        return spuPageSearchDTO;
    }
}
