package com.chinacreator.video.restful.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.chinacreator.video.bean.OrderInfo;
import com.chinacreator.video.exception.BussinessException;
import com.chinacreator.video.mapper.DictionaryDao;
import com.chinacreator.video.restful.dao.QueryCapOrderDao;
import com.chinacreator.video.restful.service.QueryCapOrderService;
import com.chinacreator.video.util.DesUtil;
import com.chinacreator.video.util.MD5;
import com.chinacreator.video.util.URLUtil;
import org.apache.commons.lang.StringUtils;
import org.bouncycastle.jcajce.provider.asymmetric.util.DESUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class QueryUserOrderController {


    private Logger logger = LoggerFactory.getLogger("queryCapOrder");


    @Autowired
    private DictionaryDao dictionaryDao;

    @Autowired
    private QueryCapOrderDao queryCapOrderDao;

    @Autowired
    private QueryCapOrderService queryCapOrderService;


    @PostMapping("/queryUserOrder")
    public Map<String, Object> queryCapOrder(@RequestBody OrderInfo orderInfo) {
        //日志记录
        Map<String, Object> logmap = new HashMap<String, Object>();
        logmap.put("invoke", "queryUserOrder");
        String resultCode = "-1";
        String resultInfo = "无订购关系";
        String msg = "";
        orderInfo.setType("");
        orderInfo.setInvokeface("queryUserOrder");
        try {
            orderInfo.setSpid("");
            queryCapOrderService.check(orderInfo);
            Map<String, String> map = queryCapOrderService.queryCap(orderInfo);
            resultCode = map.get(resultCode);
            if (!"-1".equals(resultCode)){
                resultCode="0";
                msg=map.get("msg");
                resultInfo=map.get("resultInfo");
            }
        } catch (Exception e) {
            if (e instanceof BussinessException) {
                resultCode = ((BussinessException) e).getCode();
                resultInfo = e.getMessage();
            } else {
                resultCode = "8000";
                resultInfo = "系统异常";
            }
            e.printStackTrace();

        }
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("resultCode", resultCode);
        resultMap.put("resultInfo", resultInfo);
        resultMap.put("msg", msg);
        //日志数据
        logmap.put("reqData", orderInfo);
        logmap.put("rspData", resultMap);
        logger.info(JSON.toJSONString(logmap));
        return resultMap;
    }

    public static String getMaxDayOfThisMonth() {
        SimpleDateFormat myFormatter = new SimpleDateFormat("yyyyMMdd");
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DATE, 1);    //主要就是这个roll方法
        cal.roll(Calendar.DATE, -1);
        return myFormatter.format(cal.getTime()) + "235959";
    }

    public static void main(String[] args) throws Exception {
        String decode = URLDecoder.decode("BvaMpAW4k9w%2B2XWt8mPWlw%3D%3D", "utf-8");
        System.out.println(URLDecoder.decode("BvaMpAW4k9w%2B2XWt8mPWlw%3D%3D", "utf-8"));
        System.out.println(DesUtil.decode(decode, "zfbyb!2a"));
        System.out.println(URLEncoder.encode("BvaMpAW4k9w%2B2XWt8mPWlw%3D%3D", "utf-8"));
        System.out.println(MD5.MD5Encode("youtu1048" + decode + "2000000000161643130402zfbyb!2a"));
        System.out.println(MD5.MD5Encode("youtu1048" + decode + "2000000000161643130402zfbyb!2a").toLowerCase().equals("f536c870f71a60913e2e59ffc3f295b6"));
        System.out.println(URLEncoder.encode(DesUtil.encode("17673136322", "tXNrhd#0"), "utf-8"));
    }
}

