package com.fs.erp.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.PropertyNamingStrategy;
import com.alibaba.fastjson.parser.ParserConfig;
import com.fs.erp.domain.ErpGoods;
import com.fs.erp.domain.ErpGoodsStock;
import com.fs.erp.dto.*;
import com.fs.erp.dto.sdk.wangdian.api.WdtClient;
import com.fs.erp.dto.wdt.*;
import com.fs.erp.service.IErpGoodsService;
import com.hc.openapi.tool.util.ObjectUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.http.util.Asserts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
@Primary
public class WdtErpGoodsServiceImpl implements IErpGoodsService {
    @Autowired
    private WdtClient client;

    @Override
    public BaseResponse addGoods(ErpGoods goods) {
        return null;
    }

    @Override
    public ErpGoodsQueryResponse getGoods(ErpGoodsQueryRequert param) {
        Map<String,String> map = new HashMap<>();
        map.put("spec_no",param.getCode());
        Asserts.notBlank(param.getCode(),"barcode不能为空!");

        try {
            String response = client.execute("goods_query.php", map);
            ParserConfig config = new ParserConfig();
            config.propertyNamingStrategy = PropertyNamingStrategy.CamelCase;
            ErpWdtBaseResponseDTO erpWdtBaseResponseDTO =
                    JSON.parseObject(response, ErpWdtBaseResponseDTO.class, config);
            if(ObjectUtils.equals(0, erpWdtBaseResponseDTO.getCode())){
                List<ErpGoods> list = new ArrayList<>();

                List<ErpWdtGoodsDto> goodsList = erpWdtBaseResponseDTO.getGoodsList();
                for (ErpWdtGoodsDto goodsDto : goodsList) {
                    ErpGoods erpGoods = new ErpGoods();
                    erpGoods.setCode(param.getCode());
                    erpGoods.setName(goodsDto.getGoodsName());
                    erpGoods.setSimple_name(goodsDto.getShortName());
                    erpGoods.setCategory_code(String.valueOf(goodsDto.getGoodsType()));
                    List<ErpWdtSpecDto> specList = goodsDto.getSpecList();
                    ErpWdtSpecDto erpWdtSpecDto = specList.get(0);

                    // 市场价
                    erpGoods.setCost_price(erpWdtSpecDto.getMarketPrice());
                    // 零售价
                    erpGoods.setSales_price(erpWdtSpecDto.getRetailPrice());
                    list.add(erpGoods);
                }

                ErpGoodsQueryResponse erpGoodsQueryResponse = new ErpGoodsQueryResponse();
                erpGoodsQueryResponse.setItems(list);
                return erpGoodsQueryResponse;
            } else {
                log.info("获取erp商品失败! 错误原因: {}", JSON.toJSONString(erpWdtBaseResponseDTO));
                throw new RuntimeException(erpWdtBaseResponseDTO.getMessage());
            }

        } catch (IOException e) {
            log.info("获取erp商品失败! 错误原因: {}", ExceptionUtils.getStackTrace(e));
            throw new RuntimeException(e);
        }

    }

    @Override
    public ErpGoodsStockQueryResponse getGoodsStock(ErpGoodsStockQueryRequert param) {
        String barcode = param.getBarcode();
        Asserts.notBlank(barcode,"barcode不能为空!");

        Map<String,String> map = new HashMap<>();
        map.put("spec_no",barcode);
        try {
            String response = client.execute("stock_query.php", map);
            ParserConfig config = new ParserConfig();
            config.propertyNamingStrategy = PropertyNamingStrategy.CamelCase;
            ErpWdtStockRespDTO erpWdtStockRespDTO = JSON.parseObject(response, ErpWdtStockRespDTO.class,config);
            List<ErpGoodsStock> list = new ArrayList<>();
            if(ObjectUtils.equals(0,erpWdtStockRespDTO.getCode())){
                List<ErpWdtStockDTO> stocks = erpWdtStockRespDTO.getStocks();
                for (ErpWdtStockDTO stock : stocks) {
                    ErpGoodsStock erpGoodsStock = new ErpGoodsStock();
                    erpGoodsStock.setBarcode(barcode);
                    erpGoodsStock.setQty(stock.getStock_num());
                    erpGoodsStock.setSalable_qty(stock.getAvaliable_num());
                    list.add(erpGoodsStock);
                }
            } else {
                log.info("获取erp库存失败! 错误原因: {}", JSON.toJSONString(erpWdtStockRespDTO));
                throw new RuntimeException(erpWdtStockRespDTO.getMessage());
            }
            ErpGoodsStockQueryResponse erpGoodsStockQueryResponse = new ErpGoodsStockQueryResponse();
            erpGoodsStockQueryResponse.setStocks(list);
            return erpGoodsStockQueryResponse;
        } catch (IOException e) {
            log.info("获取erp库存失败! 错误原因: {}", ExceptionUtils.getStackTrace(e));
            throw new RuntimeException(e);
        }
    }
}

