package com.chinacreator.video.restful.controller;

import com.alibaba.fastjson.JSON;
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.PayConverService;
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
 * 20210508
 */
@RestController
public class PayConverController {

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

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

    @Autowired
    private PayConverService service;

    @Autowired
    private BackBusiOrderAsynService backBusiOrderAsynService;

    @Autowired
    private ChannelSyncService syncService;


    /**
     * POST方式办理业务
     *
     * @param inputObj
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/payconver", 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", "payconver");
        logmap.put("reqData", orderInfo);
        String resultcode = "";
        String errorinfo = "";
        String url = "";
        try {
            orderInfo.setTimes(System.currentTimeMillis() + "");
            service.busiCheck(orderInfo);
            url = 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 {
            if(!"".equals(url)){
                outpipe.setData(url);
            }
            logmap.put("resultcode", resultcode);
            logmap.put("errorinfo", errorinfo);
            logmap.put("rspData", outpipe.getData());
            log.info(JSON.toJSONString(logmap));
            outpipe.setResultcode(resultcode);
            outpipe.setErrorinfo(errorinfo);
            return outpipe;
        }
    }
}
