package com.chinacreator.video.restful.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.chinacreator.video.dto.DataOutPipe;
import com.chinacreator.video.exception.BussinessException;
import com.chinacreator.video.restful.service.FeeInfoService;
import org.jboss.logging.Logger;
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.List;
import java.util.Map;

@RestController
public class FeeInfoController {

    @Autowired
    FeeInfoService feeInfoService;

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

    @RequestMapping(value = "/qryFeeInfo", method = RequestMethod.POST, produces = "application/json")
    public DataOutPipe<?> qryfbyproductid(@RequestBody(required=false) String params, HttpServletRequest request) throws Exception {
        DataOutPipe outpipe = new DataOutPipe();
        Map<String,Object> logmap = new HashMap<String,Object>();
        logmap.put("invoke", "qryFeeInfo");
        logmap.put("reqData", params);
        String resultcode = "";
        String errorinfo = "";
        try {
            JSONObject inputObj = new JSONObject().parseObject(params);
            //业务验证
            feeInfoService.busiCheck(inputObj, "qryFeeInfo");
            //获取话费余额数据
            feeInfoService.qryFeeInfo(inputObj);
            if(inputObj.get("data") != null){
                outpipe.setData(inputObj.get("data"));
            }
            resultcode = "0";
            errorinfo = "ok";
        } catch (Exception e) {
            if(e instanceof BussinessException) {
                resultcode = ((BussinessException) e).getCode();
                errorinfo = ((BussinessException) e).getMessage();
            }else{
                e.printStackTrace();
                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);
            return outpipe;
        }
    }
}
