package com.prolog.cs.biz.inbound.service.impl;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext;
import com.prolog.cs.biz.inbound.Application;
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.IncomingRecordD;
import com.prolog.cs.biz.inbound.model.IncomingRecordH;
import com.prolog.cs.biz.inbound.model.rcv.RcvInstockLines;
import com.prolog.cs.biz.inbound.service.impl.SupervisionCodeServiceImpl;
import com.prolog.cs.biz.inbound.service.rcv.RcvHeadersService;
import com.prolog.cs.biz.inbound.utils.ObjectUtils;
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.cs.biz.inbound.vo.IncomingRecordQueryVO;
import com.prolog.framework.authority.util.HystrixVarUtil;
import com.prolog.framework.core.exception.BizException;
import com.prolog.framework.core.restriction.Criteria;
import com.prolog.framework.utils.AssertUtils;
import com.prolog.framework.utils.MapUtils;
import com.prolog.framework.utils.StringUtils;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.io.Resource;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

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


/**
 * 基础测试类
 *
 * @author tianhl
 * @Date 2017/5/21 16:10
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
@WebAppConfiguration
//@Transactional //打开的话测试之后数据可自动回滚
public class DeviceJunit {

    @Autowired
    WebApplicationContext webApplicationContext;

    protected MockMvc mockMvc;

    @Autowired
    private RcvHeadersService rcvHeadersService;
    
    @Autowired
    private SupervisionCodeServiceImpl SupervisionCodeServiceImpl;

    @Value("${prolog.wms.instockCallId}")
    private String instockCallId;

    @Value("${prolog.wms.instockTransforId}")
    private String instockTransforId;

    @Value("classpath:rcvDTO.json")
    private Resource rcvDTO;

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

    @Autowired
    private IncomingRecordBackHMapper incomingRecordBackHMapper;

    @Autowired
    private IncomingRecordBackDMapper incomingRecordBackDMapper;

    @Autowired
    private IncomingRecordHMapper incomingRecordHMapper;

    @Autowired
    private IncomingRecordDMapper incomingRecordDMapper;

    @Autowired
    private com.prolog.cs.biz.inbound.service.impl.rcv.RcvInstockLinesServiceImpl RcvInstockLinesServiceImpl;

    @Before
    public void setupMockMvc(){
        mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
        HystrixRequestContext context = HystrixRequestContext.initializeContext();
        HystrixVarUtil.setUserKey("rh_admin");
    }

    //空入入库
    @Test
    public void excuteCallBackOms(){
        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();
            }
        }
    }


    private void excuteSaveCallBackData(List<RcvInstockLines> callList, String callType) {
        IncomingRecordQueryVO vo = new IncomingRecordQueryVO();
        if (callType.equals("1")) {
            // 获取所有收货单头以及单头对应的上架明细
            String asnHeaderId = callList.get(0).getAsnHeaderId();
            AssertUtils.isNotBlank(asnHeaderId, "asn单id不可为空!");
            vo.setAsnHeaderId(asnHeaderId);

        } else if (callType.equals("2")) {
            String rcvHeaderId = callList.get(0).getRcvHeaderId();
            AssertUtils.isNotBlank(rcvHeaderId, "收货单单id不可为空!");
            List<String> rcvHeaderIds = new ArrayList<>();
            rcvHeaderIds.add(rcvHeaderId);
            vo.setRcvHeaderIds(rcvHeaderIds);
            //此处调整查询数据回写客户中间表
            //getSyncErpDataInfo(rcvHeaderId);
        }

        List<IncomingRecordH> insertData = incomingRecordHMapper.getInsertData(vo);
        if (ObjectUtils.isNotNull(insertData)) {
            this.incomingRecordHMapper.saveBatch(insertData);
        }
        List<IncomingRecordD> insertDataD = incomingRecordDMapper.getInsertData(vo);
        if (ObjectUtils.isNotNull(insertData)) {
            this.incomingRecordDMapper.saveBatch(insertDataD);
        }
    }



}
