package com.chinacreator.video.restful.controller;

import com.chinacreator.video.bean.OrderInfo;
import com.chinacreator.video.exception.BussinessException;
import com.chinacreator.video.restful.service.TbActivityService;
import com.chinacreator.video.util.DesUtil;
import org.apache.log4j.Logger;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

@RestController
public class TbActivityController {

    @Autowired
    private TbActivityService tbActivityService;

    private final Logger logger=Logger.getLogger("tbActivity");

    @PostMapping("/taobaoActivity")
    public Map<String,String> insertTb(@RequestBody OrderInfo orderInfo){
        Map<String,String> resMap=new HashMap<>();
        String resultcode="0";
        String resultinfo="";
        try {
            tbActivityService.busiCheck(orderInfo);
            tbActivityService.insertTb(orderInfo);
        } catch (Exception e) {
            e.printStackTrace();
            logger.info("userid:"+orderInfo.getUserid()+"spid:"+orderInfo.getSpid()+"orderid:"+orderInfo.getOrderid()+"==="+e.getMessage());
            if (e instanceof  BussinessException){
                resultcode=((BussinessException) e).getCode();
                resultinfo=e.getMessage();
            }else {
                resultcode="8000";
                resultinfo="系统忙";
            }
        }
        resMap.put("resultcode",resultcode);
        resMap.put("resultinfo",resultinfo);
        return resMap;
    }

    public static void main(String[] args) throws Exception {
        String userid = DesUtil.encode("17673136322", "jfgku@WS");
        userid = URLEncoder.encode(userid, "utf-8");
        String params = "spid=" + "1027" + "&cpid=" + "ali" + "&userid=" + userid + "&apptype=2&channel=" + "tbcjhd"+"&entype=1" ;
        System.out.println(params);
    }
}
