package com.chinacreator.video.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.chinacreator.video.bean.OrderInfo;
import com.chinacreator.video.dto.DataOutPipe;
import com.chinacreator.video.exception.BussinessException;
import com.chinacreator.video.mapper.DictionaryDao;
import com.chinacreator.video.service.BackBusiQueryService;
import com.chinacreator.video.service.handle.CommHandleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 订单状态查询
 * @author shuchuan.jin
 * @date 20190724
 */

@RestController
public class BackBusiQueryControl {

    private static final Logger logger = LoggerFactory.getLogger(BackBusiQueryControl.class);

    private static final  Logger log = LoggerFactory.getLogger("querybackorder");

    @Autowired
    private BackBusiQueryService service;
    
	@Autowired
	private DictionaryDao dictionaryDao;

    @RequestMapping("/querybackorder")
    public DataOutPipe<?> hand(OrderInfo orderInfo) throws Exception{
        Map<String,Object> logmap = new HashMap<>();
        DataOutPipe outpipe = new DataOutPipe();
        String resultcode = "";
        String errorinfo = "";
        logmap.put("forward", "querybackorder");
        logmap.put("userInfo", orderInfo);
        try {
            orderInfo.setInvokeface("querybackorder");
            service.busiCheck(orderInfo);
            logmap.put("userInfo", orderInfo);
            Map map = service.queryOrderStatus(orderInfo);
            if(map == null){
                throw new BussinessException("10010","订单号不存在");
            }
            String result = JSON.toJSONString(map);
            JSONObject obj = JSON.parseObject(result);
            //赠送会员状态，0成功，1待赠送，2赠送中，3失败，4不赠送，5超时9070，6重试中，7重试失败，8已冻结
            String code = obj.getString("VIPSTATUS");
            String retrycount = obj.getString("RETRYCOUNT");
            String returncode = obj.getString("RESULTCODE");
            String returninfo =  obj.getString("RESULTINFO");
            String trycountconf = dictionaryDao.getValue("vipasyntrycount");
            String info = "";
            if("2".equals(code)){
            	info = "会员赠送中";
            }else if("3".equals(code)){
            	info = "会员赠送失败";
            }else if("1".equals(code)){
            	info = "待处理";
            }else if("0".equals(code)){
            	info = "赠送成功";
            }else if("4".equals(code)){
            	info = "不赠送";
            }else if("8".equals(code)){
            	info = "已冻结";
            }else{
            	info = "其他源因会员赠送失败";
            }
            
            //赠送失败，且重试次数小于指定次数
            if("3".equals(code) && trycountconf != null && !"".equals(trycountconf)
            		&& retrycount != null && !"".equals(retrycount)
            		&& Integer.parseInt(retrycount)<Integer.parseInt(trycountconf)){
            	info = "正在重试中";
            }
            if(!"0".equals(returncode)){
            	info = returninfo;
            }
            
            resultcode=code;
            errorinfo=info;
        } catch (Exception e) {
            if(e instanceof BussinessException) {
                resultcode = ((BussinessException) e).getCode();
                errorinfo = ((BussinessException) e).getMessage();
                throw e;
            }else {
                e.printStackTrace();
                resultcode = "80003";
                errorinfo = "出现异常，"+e.getMessage();
                //不返回videoif相关的错误信息
            	if(errorinfo != null && errorinfo.indexOf("videoif") != -1){
            		 throw new BussinessException(resultcode, "系统忙，请稍候再试。");
            	}else{
            		 throw new BussinessException(resultcode, errorinfo);
            	}
            }
        } finally {
            outpipe.setResultcode(resultcode);
            outpipe.setErrorinfo(errorinfo);
            logmap.put("resultcode",resultcode);
            logmap.put("errorinfo",errorinfo);
            log.info(JSON.toJSONString(logmap));
        }

        return outpipe;
    }


}
