package com.muyu.enterprise.controller;


import com.muyu.enterprise.service.FaultLogService;
import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.poi.ExcelUtil;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.core.web.page.TableDataInfo;
import com.muyu.common.log.enums.BusinessType;
import com.muyu.common.security.annotation.RequiresPermissions;

import com.muyu.domain.FaultLog;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.muyu.common.log.annotation.Log;

import java.util.Arrays;
import java.util.List;

/**
 * @Author：ChenYan
 * @Project：2112-car-cloud-server
 * @Package：com.muyu.fault.controller
 * @Filename：FaultLogController
 * @Description TODO
 * @Date：2024/9/16 20:20
 */
@RestController
@RequestMapping("/log")
public class FaultLogController extends BaseController {
    @Autowired
    private FaultLogService faultLogService;

    /**
     * 查询车辆故障管理列表
     */
    @GetMapping("/list")
    @RequiresPermissions("fault:log:list")
    @Operation(summary = "查询集合", description = "查询车辆故障管理列表")
    public Result<TableDataInfo<FaultLog>> list(FaultLog fault) {
        startPage();
        List<FaultLog> list = faultLogService.pageQuery(fault);
        return getDataTable(list);
    }


    /**
     * 导出车辆故障管理列表
     */
    @Log(title = "车辆故障管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @RequiresPermissions("fault:log:export")
    public void export(HttpServletResponse response, FaultLog fault) {
        List<FaultLog> list = faultLogService.pageQuery(fault);
        ExcelUtil<FaultLog> util = new ExcelUtil<FaultLog>(FaultLog.class);
        util.exportExcel(response, list, "车辆故障管理数据");
    }


    /**
     * 获取车辆故障管理详细信息
     */
    @GetMapping(value = "/{id}")
    public Result<String> getConfigKey (@PathVariable("id") String id) {
        return success(faultLogService.getById(id));
    }


    /**
     * 新增车辆故障管理
     */
    @Log(title = "车辆故障管理", businessType = BusinessType.INSERT)
    @PostMapping
    @RequiresPermissions("fault:log:add")
    public Result add(@RequestBody FaultLog fault) {
        faultLogService.save(fault);
        return Result.success(null, "成功");
    }



    /**
     * 修改车故障管理
     */
    @Log(title = "车辆故障管理", businessType = BusinessType.UPDATE)
    @PutMapping
    @RequiresPermissions("fault:log:edit")
    public Result edit(@RequestBody FaultLog fault) {
        faultLogService.updateById(fault);
        return Result.success(null, "成功");
    }



    /**
     * 删除车辆故障管理
     */
    @Log(title = "车辆故障管理删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    @RequiresPermissions("fault:log:remove")
    public Result remove(@PathVariable("id") Long[] id) {
        return Result.success(faultLogService.removeBatchByIds(Arrays.asList(id)), "成功");
    }


    /**
     * 新增故障日志
     */
    @PostMapping("/addLog")
    public Result addLog(@RequestBody FaultLog log){
        faultLogService.insertFaultLog(log);
        return Result.success();
    }

    /**
     * 修改结束时间
     */
    @PostMapping("/updateLog")
    public Result updateLog(@RequestBody FaultLog log){
        faultLogService.updateFaultLogByStatus(log);
        return Result.success();
    }

}
