package com.ningdatech.carapi.safe.controller;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

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

import com.ningdatech.basic.model.PageVo;
import com.ningdatech.carapi.safe.entity.VehicleAccidentReport;
import com.ningdatech.carapi.safe.manage.VehicleAccidentManage;
import com.ningdatech.carapi.safe.model.dto.AccidentAuditDTO;
import com.ningdatech.carapi.safe.model.dto.AccidentPageQuery;
import com.ningdatech.carapi.safe.model.dto.VehicleAccidentReportDTO;
import com.ningdatech.carapi.safe.model.dto.VehicleAccidentReportQueryDTO;
import com.ningdatech.carapi.safe.model.vo.NdVehicleAccidentAnalysisVO;
import com.ningdatech.carapi.safe.model.vo.NdVehicleAccidentDeadVO;
import com.ningdatech.carapi.safe.model.vo.NdVehicleAccidentReportVO;
import com.ningdatech.carapi.safe.model.vo.NdVehicleAccidentVO;
import com.ningdatech.carapi.sys.helper.RegionsCacheHelper;
import com.ningdatech.carapi.user.util.LoginUserUtil;
import com.ningdatech.log.annotation.WebLog;

import cn.hutool.core.bean.BeanUtil;
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-12
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/api/safe/accident")
@Api(value = "Accident", tags = "安全管理-事故管理")
@RequiredArgsConstructor
public class NdVehicleAccidentController {

    private final VehicleAccidentManage vehicleAccidentManage;

    private final RegionsCacheHelper regionsCacheHelper;

    @ApiOperation(value = "车辆事故信息", notes = "车辆事故信息")
    @GetMapping("/vehicle")
    public PageVo<NdVehicleAccidentVO> queryVehicle(@Valid @ModelAttribute AccidentPageQuery accidentPageQuery) {
        return vehicleAccidentManage.queryVehicleAccident(accidentPageQuery);
    }

    @ApiOperation(value = "死亡事故资料", notes = "死亡事故资料")
    @GetMapping("/dead")
    public PageVo<NdVehicleAccidentDeadVO> queryDead(@Valid @ModelAttribute AccidentPageQuery accidentPageQuery) {
        return vehicleAccidentManage.queryDead(accidentPageQuery);
    }

    @ApiOperation(value = "事故路段分析", notes = "事故路段分析")
    @GetMapping("/analysis")
    public PageVo<NdVehicleAccidentAnalysisVO> queryAccidentAnalysis(@Valid @ModelAttribute AccidentPageQuery accidentPageQuery) {
        return vehicleAccidentManage.queryAccidentAnalysis(accidentPageQuery);
    }

    @ApiOperation(value = "事故上报审核列表", notes = "事故上报审核列表")
    @GetMapping("/reportList")
    public PageVo<NdVehicleAccidentReportVO> reportAccidentInfo(@Valid @ModelAttribute AccidentPageQuery accidentPageQuery) {
        return vehicleAccidentManage.reportAccidentInfo(accidentPageQuery);
    }

    @ApiOperation(value = "上报事故信息",notes = "上报事故信息")
    @PostMapping("/save")
    public VehicleAccidentReportQueryDTO report(@Valid @RequestBody VehicleAccidentReportDTO data){
        VehicleAccidentReport entity = vehicleAccidentManage.save(data, LoginUserUtil.getUserId());
        return BeanUtil.toBean(entity, VehicleAccidentReportQueryDTO.class);
    }

    @ApiOperation(value = "获取审核记录详情",notes = "获取审核记录详情")
    @GetMapping("/detail/{id}")
    public VehicleAccidentReport getDetail(@Valid @PathVariable Long id){
        return vehicleAccidentManage.getDetail(id);
    }

    @ApiOperation(value = "删除审核记录",notes = "删除审核记录")
    @DeleteMapping("/delete/{id}")
    public Boolean delete(@Valid @PathVariable Long id){
        return vehicleAccidentManage.delete(id);
    }

    @ApiOperation(value = "审核上报事故信息")
    @PostMapping("/audit")
    public Boolean audit(@Valid @RequestBody AccidentAuditDTO param){
        return vehicleAccidentManage.audit(param);
    }

    @PostMapping("/vehicle/data/export")
    @ApiOperation(value = "车辆事故信息导出",notes = "车辆事故信息导出")
    @WebLog(value = "车辆事故信息导出", modular = "事故管理-车辆事故信息导出")
    public void uploadDataQueryExport(@Valid @RequestBody AccidentPageQuery accidentPageQuery, HttpServletResponse response) {
        vehicleAccidentManage.exportVehicleAccidentData(accidentPageQuery,response);
    }
}
