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.mapper.ChannelDao;
import com.chinacreator.video.mapper.CouponDao;
import com.chinacreator.video.restful.service.CouponService;
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
 * 20210618
 */
@RestController
public class CouponController {

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

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

    private static Logger log2 = Logger.getLogger("couponverify");

    @Autowired
    ChannelDao channelDao;

    @Autowired
    CouponService service;

    @Autowired
    CouponDao couponDao;



    /**
     * POST方式办理业务
     *
     * @param
     * @param request
     * @return
     * @throws Exception
     */
    /*券码查询接口*/
    @RequestMapping(value = "/couponquery", method = RequestMethod.POST, produces = "application/json")
    public DataOutPipe<?> couponQuery(@RequestBody(required = false) OrderInfo orderInfo, HttpServletRequest request) throws Exception {
        DataOutPipe outpipe = new DataOutPipe();
        Map<String, Object> logmap = new HashMap<String, Object>();
        logmap.put("forward", "couponquery");
        logmap.put("reqData", orderInfo);
        long beginTime = System.currentTimeMillis();
        JSONArray data = null;
        String resultcode = "";
        String errorinfo = "";
        try {
            service.busiCheck(orderInfo, 0);//type 0 query 1 verify
            data = service.query(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.toString();
            }
        } finally {
            logmap.put("resultcode", resultcode);
            logmap.put("errorinfo", errorinfo);
            logmap.put("data",data);
            logmap.put("times",(System.currentTimeMillis() - beginTime));
            log.info(JSON.toJSONString(logmap));
            outpipe.setResultcode(resultcode);
            outpipe.setErrorinfo(errorinfo);
            outpipe.setData(data);
            return outpipe;
        }
    }


    /**
     * POST方式办理业务
     *
     * @param
     * @param request
     * @return
     * @throws Exception
     */
    /*券码核销接口*/
    @RequestMapping(value = "/couponverify", method = RequestMethod.POST, produces = "application/json")
    public DataOutPipe<?> couponverify(@RequestBody(required = false) OrderInfo orderInfo, HttpServletRequest request) throws Exception {
        DataOutPipe outpipe = new DataOutPipe();
        Map<String, Object> logmap = new HashMap<String, Object>();
        logmap.put("forward", "couponverify");
        logmap.put("reqData", orderInfo);
        String resultcode = "0";
        String errorinfo = "";
        try {
            service.busiCheck(orderInfo, 1);//type 0 query 1 verify
            service.verify(orderInfo);
        } catch (Exception e) {
            e.printStackTrace();
            if (e instanceof BussinessException) {
                resultcode = ((BussinessException) e).getCode();
                errorinfo = ((BussinessException) e).getMessage();
            } else {
                resultcode = "8000";
                errorinfo = "出现异常，" + e.toString();
            }
        } finally {
            logmap.put("resultcode", resultcode);
            logmap.put("errorinfo", errorinfo);
            log2.info(JSON.toJSONString(logmap));
            outpipe.setResultcode(resultcode);
            outpipe.setErrorinfo(errorinfo);
            return outpipe;
        }
    }



}
