package com.chinacreator.video.restful.controller;

import com.alibaba.fastjson.JSON;
import com.chinacreator.video.exception.BussinessException;
import com.chinacreator.video.restful.service.NewYearVideoService;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.swing.*;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

/**
 * @author zhengrong.yan
 * @Date 20210131
 * 新春拜年ai换脸功能前端->后台对接
 */
@RestController
public class NewYearVideoController {

    private Logger logger = Logger.getLogger("newyearvideo");
    @Autowired
    private NewYearVideoService newYearVideoService;

    /**
     * @return
     */
    //@PostMapping("/videoInsertInfo")
    @RequestMapping(value = "/videoInsertInfo", method = RequestMethod.POST, produces = "application/json")
    public Map<String, String> insertUserInfo(@RequestBody Map<String, Object> params) {
        Map<String, String> rspMap = new HashMap<>();
        logger.info("新年视频前端请求参数==========" + params);
        System.out.println("新年视频前端请求参数==========" + params);
        String resultcode = "0";
        String errorinfo = "";
        String now_number = "";
        try {
            //参数校验
            newYearVideoService.busiCheck(params);
            //解密数据
            String wxuserid = newYearVideoService.validateWX(String.valueOf(params.get("wxuserid")), String.valueOf(params.get("channel")));
            params.put("wxuserid", wxuserid);//放入解密后的微信号
            //插入用户信息
            String id = newYearVideoService.insertUserInfo(params);
            //获取当前的序号
            now_number = newYearVideoService.getNowNumber(id) + "";
        } catch (Exception e) {
            e.printStackTrace();
            if (e instanceof BussinessException) {
                resultcode = ((BussinessException) e).getCode();
                errorinfo = ((BussinessException) e).getMessage();
            } else {
                resultcode = "8000";
                errorinfo = "出现异常，" + e.getMessage();
            }
        } finally {
            rspMap.put("resultcode", resultcode);
            rspMap.put("errorinfo", errorinfo);
            if (!"".equals(now_number)) {
                rspMap.put("nownumber", now_number);
            }
            logger.info(rspMap);
        }
        return rspMap;
    }

    /**
     * @return
     */
    //@PostMapping("/queryVideoInfo")
    @RequestMapping(value = "/queryVideoInfo", method = RequestMethod.POST, produces = "application/json")
    public Map<String, Object> queryUserInfo(@RequestBody Map<String, Object> params) {
        System.out.println("新年视频前端查询请求参数==========" + params);
        Map<String, Object> rspMap = new HashMap<>();
        logger.info("新年视频前端查询请求参数==========" + params);
        String resultcode = "0";
        String errorinfo = "";
        Map<String, Object> data = new HashMap<>();
        try {
            //参数校验
            newYearVideoService.busiQueryCheck(params);
            //解密数据
            String wxuserid = newYearVideoService.validateWX(String.valueOf(params.get("wxuserid")), String.valueOf(params.get("channel")));
            params.put("wxuserid", wxuserid);//放入解密后的微信号
            //查询用户信息
            data = newYearVideoService.queryVideoInfo(params);
            if (data == null || data.size() < 1) {
                resultcode = "1000";
                errorinfo = "暂无视频";
            }
        } catch (Exception e) {
            e.printStackTrace();
            if (e instanceof BussinessException) {
                resultcode = ((BussinessException) e).getCode();
                errorinfo = ((BussinessException) e).getMessage();
            } else {
                resultcode = "8000";
                errorinfo = "出现异常，" + e.getMessage();
            }
        } finally {
            rspMap.put("resultcode", resultcode);
            rspMap.put("errorinfo", errorinfo);
            if(data!=null && data.size() >1) {
                rspMap.put("videourl", data.get("VIDEOURL"));
                rspMap.put("modulesname", data.get("MODULESNAME"));
                rspMap.put("finishtime", data.get("FINISHTIME"));
            }
            logger.info(rspMap);
        }
        return rspMap;
    }

    @RequestMapping(value = "/videoQueryNumber", method = RequestMethod.POST, produces = "application/json")
    public Map<String, String> videoQueryNumber(@RequestBody Map<String, Object> params) {
        Map<String, String> rspMap = new HashMap<>();
        logger.info("新年视频前端请求参数==========" + params);
        System.out.println("新年视频前端请求参数==========" + params);
        String resultcode = "0";
        String errorinfo = "";
        String now_number = "";
        try {
            //参数校验
            newYearVideoService.busiQueryCheck(params);
            //解密数据
            String wxuserid = newYearVideoService.validateWX(String.valueOf(params.get("wxuserid")), String.valueOf(params.get("channel")));
            params.put("wxuserid", wxuserid);//放入解密后的微信号
            //获取当前的序号
            now_number = newYearVideoService.getNowNumber2(wxuserid) + "";
        } catch (Exception e) {
            e.printStackTrace();
            if (e instanceof BussinessException) {
                resultcode = ((BussinessException) e).getCode();
                errorinfo = ((BussinessException) e).getMessage();
            } else {
                resultcode = "8000";
                errorinfo = "出现异常，" + e.getMessage();
            }
        } finally {
            rspMap.put("resultcode", resultcode);
            rspMap.put("errorinfo", errorinfo);
            if (!"".equals(now_number)) {
                rspMap.put("nownumber", now_number);
            }
            logger.info(rspMap);
        }
        return rspMap;
    }

    //获取用户状态
    @RequestMapping(value = "/queryUserStatus", method = RequestMethod.POST, produces = "application/json")
    public Map<String, String> queryUserStatus(@RequestBody Map<String, Object> params) {
        Map<String, String> rspMap = new HashMap<>();
        logger.info("新年视频前端请求参数==========" + params);
        System.out.println("新年视频前端请求参数==========" + params);
        String resultcode = "0";
        String errorinfo = "";
        String status = "";
        try {
            //参数校验
            newYearVideoService.busiQueryCheck(params);
            //解密数据
            String wxuserid = newYearVideoService.validateWX(String.valueOf(params.get("wxuserid")), String.valueOf(params.get("channel")));
            params.put("wxuserid", wxuserid);//放入解密后的微信号
            //获取当前的序号
            status = newYearVideoService.queryUserStatus(wxuserid);
        } catch (Exception e) {
            e.printStackTrace();
            if (e instanceof BussinessException) {
                resultcode = ((BussinessException) e).getCode();
                errorinfo = ((BussinessException) e).getMessage();
            } else {
                resultcode = "8000";
                errorinfo = "出现异常，" + e.getMessage();
            }
        } finally {
            rspMap.put("resultcode", resultcode);
            rspMap.put("errorinfo", errorinfo);
            if (!"".equals(status)) {
                rspMap.put("status", status);
            }
            logger.info(rspMap);
        }
        return rspMap;
    }

    //重新制作
    @RequestMapping(value = "/hasRset", method = RequestMethod.POST, produces = "application/json")
    public Map<String, String> hasRset(@RequestBody Map<String, Object> params) {
        Map<String, String> rspMap = new HashMap<>();
        logger.info("新年视频前端请求参数==========" + params);
        System.out.println("新年视频前端请求参数==========" + params);
        String resultcode = "0";
        String errorinfo = "";
        String status = "";
        try {
            //参数校验
            newYearVideoService.busiQueryCheck(params);
            //解密数据
            String wxuserid = newYearVideoService.validateWX(String.valueOf(params.get("wxuserid")), String.valueOf(params.get("channel")));
            params.put("wxuserid", wxuserid);//放入解密后的微信号
            System.out.println(wxuserid);
            newYearVideoService.hasRset(wxuserid);
        } catch (Exception e) {
            e.printStackTrace();
            if (e instanceof BussinessException) {
                resultcode = ((BussinessException) e).getCode();
                errorinfo = ((BussinessException) e).getMessage();
            } else {
                resultcode = "8000";
                errorinfo = "出现异常，" + e.getMessage();
            }
        } finally {
            rspMap.put("resultcode", resultcode);
            rspMap.put("errorinfo", errorinfo);
            logger.info(rspMap);
        }
        return rspMap;
    }

    //重新制作
    @RequestMapping(value = "/checkPhoto", method = RequestMethod.POST, produces = "application/json")
    public Map<String, String> checkPhoto(@RequestBody Map<String, Object> params) {
        Map<String, String> rspMap = new HashMap<>();
        logger.info("新年视频前端请求参数==========" + params);
        String resultcode = "0";
        String errorinfo = "";
        String status = "";
        try {
            //参数校验
            newYearVideoService.busiQueryCheck(params);
            String photos = String.valueOf(params.get("photos"));
            if (StringUtils.isEmpty(photos)){
                throw  new BussinessException("1021","照片参数不能为空");
            }
            String result = newYearVideoService.checkPhoto(photos);
            if("block".equals(result)){
                resultcode = "1999";
                errorinfo = "照片不合法！";
            }else if("false".equals(result)){
                resultcode = "2999";
                errorinfo = "照片审核失败！";
            }
        } catch (Exception e) {
            e.printStackTrace();
            if (e instanceof BussinessException) {
                resultcode = ((BussinessException) e).getCode();
                errorinfo = ((BussinessException) e).getMessage();
            } else {
                resultcode = "8000";
                errorinfo = "出现异常，" + e.getMessage();
            }
        } finally {
            rspMap.put("resultcode", resultcode);
            rspMap.put("errorinfo", errorinfo);
            logger.info(rspMap);
        }
        return rspMap;
    }

    /**
     * 合作方回调
     *
     * @return
     */
    @RequestMapping(value = "/makeFinish", method = RequestMethod.POST, produces = "application/json")
    public Map<String, Object> makeFinish(@RequestBody String param , HttpServletRequest request) {
        System.out.println("新年视频接口方请求参数=========="+param);
        Map<String, Object> params = (Map) JSON.parse(param);
        System.out.println("新年视频接口方请求参数==========" + params);
        Map<String, Object> rspMap = new HashMap<>();
        logger.info("新年视频接口方请求参数==========" + params);
        String resultcode = "200";
        String errorinfo = "success";
        Map<String, Object> data = new HashMap<>();
        try {
            //参数校验
            newYearVideoService.busiFinishCheck(params);
            System.out.println("finish1=============");
            //查询用户信息
            newYearVideoService.hasFinished(params);
            System.out.println("finish-----------");
        } catch (Exception e) {
            e.printStackTrace();
            if (e instanceof BussinessException) {
                resultcode = ((BussinessException) e).getCode();
                errorinfo = ((BussinessException) e).getMessage();
            } else {
                resultcode = "8000";
                errorinfo = "出现异常，" + e.getMessage();
            }
        } finally {
            rspMap.put("code", resultcode);
            rspMap.put("msg", errorinfo);
            rspMap.put("wid", String.valueOf(params.get("wid")));
            logger.info(rspMap);
        }
        return rspMap;
    }
}
