package com.fs.his.controller;


import com.fs.common.core.domain.R;
import com.fs.common.utils.StringUtils;
import com.fs.erp.service.IErpOrderService;
import com.fs.his.param.HzOMSErpApiParam;
import com.fs.his.service.ErpApiService;
import com.fs.his.service.IFsStoreOrderService;
import com.fs.his.vo.HzOMSErpResponseVO;
import com.fs.hisStore.service.IFsStoreOrderScrmService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * 瀚智OMS Erp系统推送平数据接口
 */

@RestController
@RequestMapping("/erp/call")
@Slf4j
public class HzOMSErpApiController {

    @Autowired
    @Qualifier("hzOMSErpApiServiceImpl")
    ErpApiService hzOMSErpApiService;

    @Autowired
    @Qualifier("hzOMSErpOrderServiceImpl")
    IErpOrderService HzOMSErpOrderService;

    @Autowired
    private IFsStoreOrderService fsStoreOrderService;

    @Autowired
    private IFsStoreOrderScrmService fsStoreOrderScrmService;

    /**
     * 用于将瀚智商品库存同步到第三方
     *
     * @param appkey
     * @param timestamp
     * @param datapackage
     * @param sign
     * @return
     */
    @PostMapping("/ptgoods/stockSync")
    public HzOMSErpResponseVO stockSync(String appkey, String timestamp, String datapackage, String sign) {
        HzOMSErpApiParam param = new HzOMSErpApiParam();
        param.setAppkey(appkey);
        param.setSign(sign);
        param.setTimestamp(timestamp);
        param.setDatapakege(datapackage);
        HzOMSErpResponseVO hzOMSErpResponseVO = new HzOMSErpResponseVO();
        try {
            hzOMSErpResponseVO = hzOMSErpApiService.stockSync(param);
        } catch (Exception e) {
            e.printStackTrace();
            hzOMSErpResponseVO.setSuccess(Boolean.FALSE);
            hzOMSErpResponseVO.setMessage("同步商品库存同步失败");
        }
        return hzOMSErpResponseVO;
    }

    /**
     * 用于将瀚智商品库存同步到第三方
     *
     * @param appkey
     * @param timestamp
     * @param datapackage
     * @param sign
     * @return
     */
    @PostMapping("/ptgoods/skuStockSync")
    public HzOMSErpResponseVO skuStockSync(String appkey, String timestamp, String datapackage, String sign) {
        HzOMSErpApiParam param = new HzOMSErpApiParam();
        param.setAppkey(appkey);
        param.setSign(sign);
        param.setTimestamp(timestamp);
        param.setDatapakege(datapackage);
        HzOMSErpResponseVO hzOMSErpResponseVO = new HzOMSErpResponseVO();
        try {
            hzOMSErpResponseVO = hzOMSErpApiService.skuStockSync(param);
        } catch (Exception e) {
            e.printStackTrace();
            hzOMSErpResponseVO.setSuccess(Boolean.FALSE);
            hzOMSErpResponseVO.setMessage("同步商品规格库存失败");
        }
        return hzOMSErpResponseVO;
    }

    /**
     * 平台订单物流信息回传
     *
     * @param appkey
     * @param timestamp
     * @param datapackage
     * @param sign
     * @return
     */
    @PostMapping("/ptorder/logisticsAdd")
    public HzOMSErpResponseVO logisticsAdd(String appkey, String timestamp, String datapackage, String sign) {
        HzOMSErpApiParam param = new HzOMSErpApiParam();
        param.setAppkey(appkey);
        param.setSign(sign);
        param.setTimestamp(timestamp);
        param.setDatapakege(datapackage);
        return  hzOMSErpApiService.logisticsAdd(param);
    }

//    @PostMapping("/test/createOrder")
//    public HzOMSErpResponseVO createOrder(String appkey, String timestamp, String datapackage, String sign) {
//
//        ErpOrder order = new ErpOrder();
//        order.setPlatform_code(datapackage);
//
//        ErpOrderResponse erpOrderResponse = HzOMSErpOrderService.addOrder(order);
//        HzOMSErpResponseVO hzOMSErpResponseVO = new HzOMSErpResponseVO();
//        hzOMSErpResponseVO.setSuccess(true);
//        return hzOMSErpResponseVO;
//    }


    /**
     * 代服管家订单回调
     */
    @PostMapping("/dfNotifyUrl")
    public R dfOrderResult(HttpServletRequest request){
        String body = null;
        try {
            // 1. 先设置编码
            request.setCharacterEncoding("UTF-8");
            // 3. 按 UTF-8 解码
            body = IOUtils.toString(request.getInputStream(), StandardCharsets.UTF_8);
        } catch (IOException e) {
            log.error("读取 body 失败", e);
            return null;
        }
        log.info("Body UTF-8: {}", body);

        return StringUtils.isNotBlank(body)
                ? fsStoreOrderService.dfOrderResult(body)
                : R.ok();
    }

    /**
     * 代服管家订单状态回调
     */
    @PostMapping("/receiveWaybillPush")
    public R receiveWaybillPush(HttpServletRequest request) {
        String body = null;
        try {
            // 1. 先设置编码
            request.setCharacterEncoding("UTF-8");
            // 3. 按 UTF-8 解码
            body = IOUtils.toString(request.getInputStream(), StandardCharsets.UTF_8);
        } catch (IOException e) {
            log.error("读取 body 失败", e);
            return null;
        }
        log.info("Body UTF-8: {}", body);

        return StringUtils.isNotBlank(body)
                ? fsStoreOrderService.receiveWaybillPush(body)
                : R.ok();
    }

    /**
     * 代服管家订单回调
     */
    @PostMapping("/dfNotifyUrlScrm")
    public R dfOrderScrmResult(HttpServletRequest request){
        String body = null;
        try {
            // 1. 先设置编码
            request.setCharacterEncoding("UTF-8");
            // 3. 按 UTF-8 解码
            body = IOUtils.toString(request.getInputStream(), StandardCharsets.UTF_8);
        } catch (IOException e) {
            log.error("读取 body 失败", e);
            return null;
        }
        log.info("Body UTF-8: {}", body);

        return StringUtils.isNotBlank(body)
                ? fsStoreOrderScrmService.dfOrderResult(body)
                : R.ok();
    }

    /**
     * 代服管家订单状态回调
     */
    @PostMapping("/receiveWaybillPushScrm")
    public R receiveWaybillPushScrm(HttpServletRequest request) {
        String body = null;
        try {
            // 1. 先设置编码
            request.setCharacterEncoding("UTF-8");
            // 3. 按 UTF-8 解码
            body = IOUtils.toString(request.getInputStream(), StandardCharsets.UTF_8);
        } catch (IOException e) {
            log.error("读取 body 失败", e);
            return null;
        }
        log.info("Body UTF-8: {}", body);

        return StringUtils.isNotBlank(body)
                ? fsStoreOrderScrmService.receiveWaybillPush(body)
                : R.ok();
    }
}
