package com.ningdatech.carapi.car.datascreen.controller;

import java.util.List;

import javax.validation.Valid;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.alibaba.fastjson.JSONObject;
import com.ningdatech.carapi.car.datascreen.manage.DigitalCockpitManage;
import com.ningdatech.carapi.car.datascreen.model.dto.DataScreenParam;
import com.ningdatech.carapi.car.datascreen.model.vo.StatisticsCenterVO;
import com.ningdatech.carapi.car.position.model.vo.ResRealTimeMonitorVehicleGisListVO;
import com.ningdatech.carapi.common.model.MapDataVO;
import com.ningdatech.carapi.common.util.CodeUtil;
import com.ningdatech.carapi.user.util.LoginUserUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 * 数字驾驶舱
 * </p>
 *
 * @author PoffyZhang
 * @since 2022-10-22
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/api/v1/digital-cockpit")
@Api(value = "digital-cockpit", tags = "数字驾驶舱")
@RequiredArgsConstructor
public class DigitalCockpitController {

    private final DigitalCockpitManage digitalCockpitManage;

    private static final Integer OUT_DANGER = 1;
    private static final Integer INJURIES = 2;
    private static final Integer DEATH_TOLL = 3;

    @ApiOperation(value = "大屏中间统计数字", notes = "大屏中间统计数字")
    @GetMapping("/statistics-center")
    public StatisticsCenterVO statisticsCenter(@Valid @ModelAttribute DataScreenParam param) {
        CodeUtil.convertRegionCodeToId(param);
        param.setOperatorId(LoginUserUtil.getUserId());
        return digitalCockpitManage.statisticsCenter(param);
    }

    @ApiOperation(value = "车辆gps点分布", notes = "车辆gps点分布")
    @GetMapping("/gps")
    public List<ResRealTimeMonitorVehicleGisListVO> getVehicleGps(@Valid @ModelAttribute DataScreenParam param) {
        CodeUtil.convertRegionCodeToId(param);
        param.setOperatorId(LoginUserUtil.getUserId());
        return digitalCockpitManage.getVehicleGps(param);
    }

    @ApiOperation(value = "车辆超速次数", notes = "车辆超速次数")
    @GetMapping("/overspeed-group")
    public List<MapDataVO> overspeedGroup(@Valid @ModelAttribute DataScreenParam param) {
        CodeUtil.convertRegionCodeToId(param);
        param.setOperatorId(LoginUserUtil.getUserId());
        return digitalCockpitManage.overspeedGroup(param);
    }

    @ApiOperation(value = "车辆违章次数", notes = "车辆违章次数")
    @GetMapping("/violation-group")
    public List<MapDataVO> violationGroup(@Valid @ModelAttribute DataScreenParam param) {
        CodeUtil.convertRegionCodeToId(param);
        param.setOperatorId(LoginUserUtil.getUserId());
        return digitalCockpitManage.violationGroup(param);
    }

    @ApiOperation(value = "车辆出险次数", notes = "车辆出险次数")
    @GetMapping("/outDanger-group")
    public List<MapDataVO> outDangerGroup(@Valid @ModelAttribute DataScreenParam param) {
        CodeUtil.convertRegionCodeToId(param);
        param.setOperatorId(LoginUserUtil.getUserId());
        return digitalCockpitManage.outDangerGroup(param);
    }

    @ApiOperation(value = "车辆受伤人数", notes = "车辆受伤人数")
    @GetMapping("/injuries-group")
    public List<MapDataVO> injuriesGroup(@Valid @ModelAttribute DataScreenParam param) {
        CodeUtil.convertRegionCodeToId(param);
        param.setOperatorId(LoginUserUtil.getUserId());
        return digitalCockpitManage.injuriesGroup(param);
    }

    @ApiOperation(value = "车辆死亡人数", notes = "车辆死亡人数")
    @GetMapping("/death-group")
    public List<MapDataVO> deathGroup(@Valid @ModelAttribute DataScreenParam param) {
        CodeUtil.convertRegionCodeToId(param);
        param.setOperatorId(LoginUserUtil.getUserId());
        return digitalCockpitManage.deathGroup(param);
    }

    @ApiOperation(value = "万车死亡人数", notes = "万车死亡人数")
    @GetMapping("/death-group-ten-thousand")
    public List<MapDataVO> deathGroupTenThousand(@Valid @ModelAttribute DataScreenParam param) {
        CodeUtil.convertRegionCodeToId(param);
        param.setOperatorId(LoginUserUtil.getUserId());
        return digitalCockpitManage.deathGroupTenThousand(param);
    }

    @ApiOperation(value = "上线率分析", notes = "上线率分析")
    @GetMapping("/online-analysis")
    public JSONObject onlineAnalysis(@Valid @ModelAttribute DataScreenParam param) {
        CodeUtil.convertRegionCodeToId(param);
        param.setOperatorId(LoginUserUtil.getUserId());
        return digitalCockpitManage.onlineAnalysis(param);
    }

    @ApiOperation(value = "超速分析", notes = "超速分析")
    @GetMapping("/overspeed-analysis")
    public JSONObject overspeedAnalysis(@Valid @ModelAttribute DataScreenParam param) {
        CodeUtil.convertRegionCodeToId(param);
        param.setOperatorId(LoginUserUtil.getUserId());
        return digitalCockpitManage.overspeedAnalysis(param);
    }

    @ApiOperation(value = "全省月度超速对比", notes = "全省月度超速对比")
    @GetMapping("/monthly-overspeed-analysis")
    public JSONObject monthlyOverspeedAnalysis(@Valid @ModelAttribute DataScreenParam param) {
        CodeUtil.convertRegionCodeToId(param);
        param.setOperatorId(LoginUserUtil.getUserId());
        return digitalCockpitManage.monthlyOverspeedAnalysis(param);
    }

    @ApiOperation(value = "全省月度出险对比", notes = "全省月度出险对比")
    @GetMapping("/monthly-out-danger-analysis")
    public JSONObject monthlyOutDangerAnalysis(@Valid @ModelAttribute DataScreenParam param) {
        CodeUtil.convertRegionCodeToId(param);
        param.setType(OUT_DANGER);
        param.setOperatorId(LoginUserUtil.getUserId());
        return digitalCockpitManage.getInsureAnalysis(param,OUT_DANGER);
    }

    @ApiOperation(value = "全省月度受伤人数对比", notes = "全省月度受伤人数对比")
    @GetMapping("/monthly-injuries-analysis")
    public JSONObject monthlyInjuriesAnalysis(@Valid @ModelAttribute DataScreenParam param) {
        CodeUtil.convertRegionCodeToId(param);
        param.setType(INJURIES);
        param.setOperatorId(LoginUserUtil.getUserId());
        return digitalCockpitManage.getInsureAnalysis(param,INJURIES);
    }

    @ApiOperation(value = "全省月度死亡人数对比", notes = "全省月度死亡人数对比")
    @GetMapping("/monthly-death-analysis")
    public JSONObject monthlyDeathAnalysis(@Valid @ModelAttribute DataScreenParam param) {
        CodeUtil.convertRegionCodeToId(param);
        param.setType(DEATH_TOLL);
        param.setOperatorId(LoginUserUtil.getUserId());
        return digitalCockpitManage.getInsureAnalysis(param,DEATH_TOLL);
    }

    @ApiOperation(value = "清缓存", notes = "清缓存")
    @PostMapping("/remove-cache")
    public String removeCache(@RequestParam(required = false) String cacheName) {
        return digitalCockpitManage.removeCache(cacheName);
    }
}
