package com.chinacreator.video.restful.controller;

import com.chinacreator.video.exception.BussinessException;
import com.chinacreator.video.restful.service.AkUserService;
import com.chinacreator.video.util.AESUtil;
import com.chinacreator.video.util.DesUtil;
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 java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yu.su
 * @Date 2020.12.18
 * 安康市用户信息
 */
@RestController
public class AkUserController {

    private Logger logger=Logger.getLogger(AkUserController.class);
    @Autowired
    private AkUserService akUserService;
    /**
     *
     * @return
     */
    @PostMapping("/akInsertInfo")
    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="";
        try {
            String channelinfo = String.valueOf(params.get("channelinfo"));
            if (StringUtils.isNotEmpty(channelinfo)){
                params.put("channelinfo", URLDecoder.decode(channelinfo,"utf-8"));
            }
            String address = String.valueOf(params.get("address"));
            if (StringUtils.isNotEmpty(address)){
                params.put("address", URLDecoder.decode(address,"utf-8"));
            }else {
                throw new BussinessException("9001","地址不能为空");
            }

            String nickname = String.valueOf(params.get("nickname"));
            if (StringUtils.isNotEmpty(nickname)) {
                params.put("nickname", URLDecoder.decode(nickname, "utf-8"));
            }else{
                throw new BussinessException("9001","昵称不能为空");
            }
            //参数校验
            akUserService.busiCheck(params);
            //插入用户信息
            akUserService.insertUserInfo(params);
        } 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);
        }
        return rspMap;
    }

    /**
     *
     * @return
     */
    @GetMapping("/queryAkUserInfo")
    public Map<String,Object>  queryUserInfo(@RequestParam 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 {
            //参数校验
             akUserService.busiQueryCheck(params);
            //查询用户信息
            data = akUserService.queryUserInfo(params);
        } 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);
            rspMap.put("data",data);

        }
        return rspMap;
    }
}
