package com.yn.bftl.thirdparty.modules.u8.controller;

import com.alibaba.fastjson.JSON;
import com.yn.bftl.thirdparty.common.exception.YnceErrorException;
import com.yn.bftl.thirdparty.common.util.IPUtils;
import com.yn.bftl.thirdparty.config.U8Properties;
import com.yn.bftl.thirdparty.modules.business.dto.U8ShippingDTO;
import com.yn.bftl.thirdparty.modules.u8.service.U8ShippingService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;

@Slf4j
@RestController
@RequestMapping("/u8")
public class U8ShippingController {

    @Autowired
    private U8Properties u8Properties;

    @Autowired
    private U8ShippingService u8ShippingService;

    @RequestMapping(value = "/shipping", method = RequestMethod.POST)
    public void shipping(@RequestBody U8ShippingDTO shippingSynDataDTO) {
        log.info("=======================================u8请求参数" + JSON.toJSONString(shippingSynDataDTO));
        IPUtils.checkWhileIp(u8Properties.getWhiteIp());
        if (CollectionUtils.isEmpty(shippingSynDataDTO.getDetailList())) {
            log.error("订单项数据不能为空");
            throw new YnceErrorException("订单项数据不能为空");
        }

        u8ShippingService.saveU8SynData(shippingSynDataDTO);
    }

    @RequestMapping(value = "/shipping", method = RequestMethod.DELETE)
    public void unShipping(String shippingSn, String erpCode) {
        log.info("=======================================u8请求参数:" + shippingSn);
        if (StringUtils.isEmpty(shippingSn)) {
            throw new YnceErrorException("参数不正确");
        }
        IPUtils.checkWhileIp(u8Properties.getWhiteIp());
//        if (CollectionUtils.isEmpty(shippingSynDataDTO.getDetailList())) {
//            log.error("订单项数据不能为空");
//            throw new YnceErrorException("订单项数据不能为空");
//        }

        u8ShippingService.unShipping(shippingSn, erpCode);
    }

}
