package com.chinacreator.video.restful.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.chinacreator.video.bean.OrderInfo;
import com.chinacreator.video.dto.DataOutPipe;
import com.chinacreator.video.exception.BussinessException;
import com.chinacreator.video.restful.service.BackBusiOrderAsynService;
import com.chinacreator.video.restful.service.ChannelSyncService;
import com.chinacreator.video.restful.service.RelationQueryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

/**
 * 订购关系查询，只查询前向产品
 * zhengrong.yan
 * 20210804
 */
@RestController
public class RelationQueryController {

    private static Logger logger = Logger.getLogger(String.valueOf(RelationQueryController.class));

    private static Logger log = Logger.getLogger("relationQuery");

    @Autowired
    private RelationQueryService service;

    @Autowired
    private BackBusiOrderAsynService backBusiOrderAsynService;

    @Autowired
    private ChannelSyncService syncService;


    /**
     * POST方式办理业务
     *
     * @param
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/relationQuery", method = RequestMethod.POST, produces = "application/json")
    public DataOutPipe<?> order(@RequestBody(required = false) OrderInfo orderInfo, HttpServletRequest request) throws Exception {
        DataOutPipe outpipe = new DataOutPipe();
        Map<String, Object> logmap = new HashMap<String, Object>();
        logmap.put("forward", "relationQuery");
        logmap.put("reqData", orderInfo);
        String resultcode = "";
        String errorinfo = "";
        JSONArray array = null;
        try {
            orderInfo.setTimes(System.currentTimeMillis() + "");
            service.busiCheck(orderInfo);
            array = service.busiOper(orderInfo);
            resultcode = "0";
            errorinfo = "";
        } catch (Exception e) {
            e.printStackTrace();
            if (e instanceof BussinessException) {
                resultcode = ((BussinessException) e).getCode();
                errorinfo = ((BussinessException) e).getMessage();
            } else {
                resultcode = "8000";
                errorinfo = "出现异常，" + e.getMessage();
            }
        } finally {
            logmap.put("resultcode", resultcode);
            logmap.put("errorinfo", errorinfo);
            logmap.put("rspData", outpipe.getData());
            log.info(JSON.toJSONString(logmap));
            outpipe.setResultcode(resultcode);
            outpipe.setErrorinfo(errorinfo);
            outpipe.setData(array);
            return outpipe;
        }
    }
}
