package com.muyu.server.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
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.domain.SysCarFaultLog;
import com.muyu.server.service.SysCarFaultLogService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * 车辆故障记录 接口控制层
 * @author 袁子龙
 * @package: com.muyu.breakdown.controller
 * @name: SysCarFaultLogController
 * @date: 2024/9/22 21:08
 */
@RestController
@RequestMapping("/log")
@Tag(name = "车辆故障记录",description = "车辆故障记录管理")
@Log4j2
public class SysCarFaultLogController extends BaseController {
    @Autowired
    private SysCarFaultLogService service;

    /**
     * 查询故障记录列表
     * @param sysCarFaultLog
     * @return
     */
    @PostMapping("/list")
    @Operation(summary = "查询故障记录列表",description = "查询故障记录列表")
    public Result<TableDataInfo<SysCarFaultLog>> list(@RequestBody SysCarFaultLog sysCarFaultLog){
        startPage();
        List<SysCarFaultLog> list = service.selectSysCarFaultLogList(sysCarFaultLog);
        return getDataTable(list);
    }

    /**
     *  查询故障记录状态为忽略的数据列表
     * @param sysCarFaultLog
     * @return
     */
    @PostMapping("/listStatusIgnore")
    @Operation(summary = "查询故障记录状态为忽略的数据列表",description = "查询故障记录状态为忽略的数据列表")
    public Result<TableDataInfo<SysCarFaultLog>>listStatusIgnore(@RequestBody  SysCarFaultLog sysCarFaultLog){
        startPage();
        List<SysCarFaultLog> list = service.listStatusIgnore(sysCarFaultLog);
        return getDataTable(list);
    }


    /**
     *  查询故障记录状态为已解决的数据列表
     * @param sysCarFaultLog
     * @return
     */
    @PostMapping("/listStatusSolve")
    @Operation(summary = "查询故障记录状态为已解决的数据列表",description = "查询故障记录状态为已解决的数据列表")
    public Result<TableDataInfo<SysCarFaultLog>>listStatusSolve(@RequestBody SysCarFaultLog sysCarFaultLog){
        startPage();
        List<SysCarFaultLog> list = service.listStatusSolve(sysCarFaultLog);
        return getDataTable(list);
    }

    /**
     * 查询故障记录状态为处理中的数据列表
     * @param sysCarFaultLog
     * @return
     */
    @PostMapping("/listStatusProcess")
    @Operation(summary = "查询故障记录状态为处理中的数据列表",description = "查询故障记录状态为处理中的数据列表")
    public Result<TableDataInfo<SysCarFaultLog>>listStatusProcess(@RequestBody SysCarFaultLog sysCarFaultLog){
        startPage();
        List<SysCarFaultLog> list = service.listStatusProcess(sysCarFaultLog);
        return getDataTable(list);
    }

    /**
     * 新增故障记录
     * @param sysCarFaultLog
     * @return
     */
    @PostMapping("/add")
    @Operation(summary = "新增故障记录",description = "新增故障记录")
    public Result<Integer> add(@RequestBody SysCarFaultLog sysCarFaultLog){

        return toAjax(service.save(sysCarFaultLog));
    }

    /**
     * 导出故障记录
     * @param response
     * @param sysCarFaultLog
     */
    @PostMapping("/export")
    @Operation(summary = "导出故障记录",description = "导出故障记录")
    public void  export(HttpServletResponse response, SysCarFaultLog sysCarFaultLog){
        List<SysCarFaultLog> list = service.selectSysCarFaultLogList(sysCarFaultLog);
        ExcelUtil<SysCarFaultLog> util = new ExcelUtil<SysCarFaultLog>(SysCarFaultLog.class);
        util.exportExcel(response,list,"车辆故障记录数据");
    }

    /**
     *  把处理中的数据修改为忽略
     * @param idsStr
     * @return
     */
    @PutMapping("/updateStatusById/{ids}")
    @Operation(summary = "把处理中的数据修改为忽略",description = "把处理中的数据修改为忽略")
    public Result updateStatusById(@PathVariable("ids")String idsStr){
        Long[] ids = Arrays.stream(idsStr.split(","))
                .map(Long::valueOf)
                .toArray(Long[]::new);
        UpdateWrapper<SysCarFaultLog> wrapper = new UpdateWrapper<>();
        wrapper.in("id",ids).set("status",3);
        boolean update = service.update(null, wrapper);
        return Result.success(update);
    }


}
