/**
 * Copyright (c) 2018 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package io.renren.controller;

import io.renren.annotation.Login;
import io.renren.annotation.LoginUser;
import io.renren.common.utils.Result;
import io.renren.entity.UserEntity;
import io.renren.service.AutoTaskService;
import io.renren.service.CarService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 测试接口
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestController
@RequestMapping("/api")
@Api(tags="测试接口")
public class ApiTestController {

    @Autowired
    private AutoTaskService autoTaskService;

    @Autowired
    private CarService carService;


    @Login
    @GetMapping("userInfo")
    @ApiOperation(value="获取用户信息", response=UserEntity.class)
    public Result<UserEntity> userInfo(@ApiIgnore @LoginUser UserEntity user){
        return new Result<UserEntity>().ok(user);
    }

    @Login
    @GetMapping("userId")
    @ApiOperation("获取用户ID")
    public Result<Long> userInfo(@ApiIgnore @RequestAttribute("userId") Long userId){
        return new Result<Long>().ok(userId);
    }

    @GetMapping("notToken")
    @ApiOperation("忽略Token验证测试")
    public Result<String> notToken(){
        return new Result<String>().ok("无需token也能访问。。。");
    }


    //Post请求
    @Async
    //每3分钟循环执行一次
    //@Scheduled(cron = "0 0/3 * * * *")
    public  void TestTask(){
        System.out.println("每3分钟循环执行一次数据同步............");
        //请求获取同步数据
        autoTaskService.getCarInfo("","");//不放时间说明获取当天数据
        //同步下发海康平台
        autoTaskService.syncHk();
    }

    @GetMapping("syncHk")
    @ApiOperation("同步海康白名单")
    public Result<String> syncHk(){
        int i=autoTaskService.syncHk();
        return new Result<String>().ok(i+"");
    }

    @GetMapping("getCarInfo")
    @ApiOperation("获取汽车数据")
    public Result<String> getCarInfo(@ApiIgnore String time1,@ApiIgnore String time2){
        int i=autoTaskService.getCarInfo(time1,time2);
        return new Result<String>().ok(i+"");
    }

/*    @GetMapping("backCar")
    @ApiOperation("获取汽车回调数据")
    public HkResult backCar(HttpServletRequest request){
        Enumeration<String> parameters = request.getParameterNames();
        Map<String, String> params = new HashMap<>();
        while (parameters.hasMoreElements()) {
            String parameter = parameters.nextElement();
            String value = request.getParameter(parameter);
            if (StringUtils.isNotBlank(value)) {
                params.put(parameter, value);
            }
        }
        System.out.println("=============获取车辆通信信息1===="+params);
        return new HkResult().error(0,"");
    }*/
/*

    @PostMapping("backCar")
    @ApiOperation("获取汽车回调数据")
    public HkResult backCar(@ApiIgnore @RequestBody CarRecordEntity car){
        //System.out.println("=============获取车辆通行信息"+car.toString());
        autoTaskService.getCarRecord(car);
        return new HkResult().error(0,"");
    }
*/

}
