package com.prolog.cs.biz.inbound.controller;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.prolog.cs.biz.inbound.dao.IncomingRecordBackDMapper;
import com.prolog.cs.biz.inbound.dao.IncomingRecordBackHMapper;
import com.prolog.cs.biz.inbound.dao.IncomingRecordDMapper;
import com.prolog.cs.biz.inbound.dao.IncomingRecordHMapper;
import com.prolog.cs.biz.inbound.model.IncomingRecordH;
import com.prolog.cs.biz.inbound.vo.IncomingRecordBackDVO;
import com.prolog.cs.biz.inbound.vo.IncomingRecordBackHVO;
import com.prolog.cs.biz.inbound.vo.IncomingRecordBackVO;
import com.prolog.framework.core.exception.BizException;
import com.prolog.framework.core.restriction.Criteria;
import com.prolog.framework.utils.MapUtils;
import com.prolog.framework.utils.StringUtils;
import org.junit.Test;
import org.junit.Before; 
import org.junit.After;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

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

/** 
* SupervisionCodeXmlController Tester. 
* 
* @author <Authors name> 
* @since <pre>3�� 18, 2022</pre> 
* @version 1.0 
*/ 
public class SupervisionCodeXmlControllerTest { 

@Before
public void before() throws Exception { 
} 

@After
public void after() throws Exception { 
}

    @Value("${oms.incomingRecordBack}")
    private String incomingRecordBack;

    @Autowired
    private IncomingRecordBackHMapper incomingRecordBackHMapper;

    @Autowired
    private IncomingRecordBackDMapper incomingRecordBackDMapper;

    @Autowired
    private IncomingRecordHMapper incomingRecordHMapper;

    @Autowired
    private IncomingRecordDMapper incomingRecordDMapper;

/** 
* 
* Method: exportXml(@RequestParam Integer supervisionType, @RequestParam String ids, HttpServletResponse response) 
* 
*/ 
@Test
public void testExportXml() throws Exception {
    String rcvHeaderId="808005692286963712";
    List<IncomingRecordBackVO> backList = new ArrayList<>();
    List<IncomingRecordBackHVO> backHVOList = incomingRecordBackHMapper.getInsertData(rcvHeaderId);
    if(backHVOList.size()>0) {
        List<IncomingRecordBackDVO> backDList = incomingRecordBackDMapper.getInsertData(rcvHeaderId);
        for (IncomingRecordBackHVO incomingRecordBackHVO : backHVOList) {
            IncomingRecordBackVO backVO = new IncomingRecordBackVO();
            backVO.setContent(incomingRecordBackHVO);
            backVO.setDetailList(backDList);
            backList.add(backVO);
        }
        //TODO 调用接口平台回传OMS
        try {
//                String url = "http://integration.upcloud.prolog.org/api/rcvInstockLines/infTaskInBack";
            if (StringUtils.isEmpty(incomingRecordBack)) {
                throw new BizException("请求路径配置为空");
            }
            System.out.println(JSON.toJSONString(backList));
            String url = incomingRecordBack;
            String result = HttpUtil.post(url, JSONObject.toJSONString(backList));
            JSONObject parse = JSONObject.parseObject(result);
            if(parse.get("success")!=null&&(Boolean)parse.get("success")){
                Criteria crt = Criteria.forClass(IncomingRecordH.class);
                crt.restrictionBuilder().eq("billid",rcvHeaderId);
                crt.buildRestriction();
                incomingRecordHMapper.updateMapByCriteria(MapUtils.put("attrStr1","Y").getMap(),crt);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

/** 
* 
* Method: exportXmlAll(@RequestParam String warehouseId, @RequestParam String cargoOwnerId, @RequestParam Integer supervisionType, @RequestParam(required = false) String srcCode, HttpServletResponse response) 
* 
*/ 
@Test
public void testExportXmlAll() throws Exception { 
//TODO: Test goes here... 
} 


} 
