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

import cn.hutool.core.bean.BeanUtil;
import com.ningdatech.basic.model.PageVo;
import com.ningdatech.carapi.car.vehicle.model.NdVehicleBaseInfo;
import com.ningdatech.carapi.car.vehicle.manage.VehicleManage;
import com.ningdatech.carapi.car.vehicle.model.vo.VehicleBaseVO;
import com.ningdatech.carapi.car.vehicle.model.dto.*;
import com.ningdatech.carapi.car.vehicle.service.IVehicleBaseInfoService;
import com.ningdatech.carapi.common.util.ExcelDownUtil;
import com.ningdatech.carapi.user.util.LoginUserUtil;
import com.ningdatech.log.annotation.WebLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author PoffyZhang
 * @since 2022-10-09
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/api/archives/vehicle")
@Api(value = "archives", tags = "车辆管理-档案管理")
@RequiredArgsConstructor
public class VehicleController {

    private final VehicleManage vehicleManage;
    private final IVehicleBaseInfoService vehicleBaseInfoService;

    @ApiOperation(value = "车辆列表查询", notes = "车辆列表查询")
    @GetMapping("/list")
    public PageVo<VehicleBaseVO> queryList(@Valid @ModelAttribute VehicleArchivesPageQuery vehicleArchivesPageQuery) {
        return vehicleManage.queryNewAddList(vehicleArchivesPageQuery);
    }

    @ApiOperation(value = "车辆列表导出", notes = "车辆列表导出")
    @GetMapping("/exportBase")
    public void export(VehicleArchivesPageQuery req, HttpServletResponse response) {
        ExcelDownUtil.downXls(response, req, vehicleManage::exportNewAddBase);
    }

    @ApiOperation(value = "车辆档案列表查询", notes = "车辆档案列表查询")
    @GetMapping("/archivesList")
    public PageVo<VehicleBaseVO> queryArchivesList(@Valid @ModelAttribute VehicleArchivesPageQuery vehicleArchivesPageQuery) {
        return vehicleManage.queryList(vehicleArchivesPageQuery);
    }

    @ApiOperation(value = "车辆档案导出", notes = "车辆档案导出")
    @GetMapping("/exportArchives")
    @WebLog(value = "导出车辆档案")
    public void exportArchives(VehicleArchivesPageQuery req, HttpServletResponse response) {
        ExcelDownUtil.downXls(response, req, vehicleManage::exportArchives);
    }

    @ApiOperation(value = "车辆待审核列表查询", notes = "车辆待审核列表查询")
    @GetMapping("/toBeAuditList")
    public PageVo<VehicleBaseVO> queryToBeAudit(@Valid @ModelAttribute VehicleAuditPageQuery vehicleAuditPageQuery) {
        return vehicleManage.queryToBeAudit(vehicleAuditPageQuery);
    }

    @ApiOperation(value = "车辆已审核列表查询", notes = "车辆已审核列表查询")
    @GetMapping("/auditedList")
    public PageVo<VehicleBaseVO> queryAuditedList(@Valid @ModelAttribute VehicleAuditPageQuery vehicleAuditPageQuery) {
        return vehicleManage.queryAuditedList(vehicleAuditPageQuery);
    }

    @ApiOperation(value = "车辆审核列表导出", notes = "车辆审核列表导出")
    @GetMapping("/exportAudit")
    @WebLog
    public void exportAudit(VehicleAuditPageQuery req, HttpServletResponse response) {
        ExcelDownUtil.downXls(response, req, vehicleManage::exportAudit);
    }

    /**
     * 查询车辆基本信息
     *
     * @param id 主键id
     * @return 查询结果
     */
    @ApiOperation(value = "查询车辆基本信息", notes = "查询车辆基本信息")
    @GetMapping("/baseInfo")
    public VehicleArchivesQueryDTO getDetails(@Valid @RequestParam Long id) {
        return vehicleManage.getDetails(id);
    }

    @ApiOperation(value = "保存新车辆", notes = "保存新车辆")
    @PostMapping("/save")
    @WebLog
    public VehicleArchivesQueryDTO handlerSave(@Valid @RequestBody VehicleSaveDTO data) {
        NdVehicleBaseInfo entity = vehicleManage.save(data, LoginUserUtil.getUserId());
        return BeanUtil.toBean(entity, VehicleArchivesQueryDTO.class);
    }

    @ApiOperation(value = "编辑车辆(重新提交)", notes = "编辑车辆(重新提交)")
    @PostMapping("/modify")
    @WebLog
    public VehicleArchivesQueryDTO handlerUpdate(@Valid @RequestBody VehicleUpdateDTO data) {
        NdVehicleBaseInfo entity = vehicleManage.update(data, LoginUserUtil.getUserId());
        return BeanUtil.toBean(entity, VehicleArchivesQueryDTO.class);
    }

    @ApiOperation(value = "删除车辆", notes = "删除车辆")
    @PostMapping("/remove")
    @WebLog
    public Boolean handlerDelete(@Valid @RequestBody List<Long> ids) {
        return vehicleManage.removeByIdWithCache(ids);
    }

    @ApiOperation(value = "车辆审核", notes = "车辆审核")
    @PostMapping("/audit")
    @WebLog
    public Boolean handlerAudit(@Valid @RequestBody VehicleAuditDTO data) {
        return vehicleBaseInfoService.audit(data, LoginUserUtil.getUserId());
    }

    @ApiOperation(value = "编辑车辆基本信息", notes = "编辑车辆基本信息")
    @PostMapping("/modifyBaseInfo")
    @WebLog
    public VehicleArchivesQueryDTO modifyUpdate(@Valid @RequestBody VehicleUpdateDTO data) {
        NdVehicleBaseInfo entity = vehicleManage.modifyAndUpdate(data, LoginUserUtil.getUserId());
        return BeanUtil.toBean(entity, VehicleArchivesQueryDTO.class);
    }

    @ApiOperation(value = "更新天表在线时间", notes = "更新天表在线时间")
    @GetMapping("/updateOnlineTime")
    public String updateOnlineTime() {
        vehicleManage.updateOnlineTime();
        return "更新成功";
    }

    @ApiOperation(value = "取消车辆与所属企业、使用企业及区域的绑定")
    @GetMapping("/cancel-relevance")
    public String cancelRelevance(@Valid @RequestParam Long id){
        return vehicleManage.cancelRelevance(id);
    }

}
