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.SysCarFault;
import com.muyu.common.security.annotation.RequiresPermissions;
import com.muyu.common.security.utils.SecurityUtils;
import com.muyu.server.service.SysCarFaultService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.log4j.Log4j2;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

/**
 * 车辆故障码 接口控制层
 * @author 袁子龙
 * @package: com.muyu.breakdown.controller
 * @name: SysCarFaultController
 * @date: 2024/9/20 11:00
 */
@RestController
@RequestMapping("/breakdown")
@Tag(name = "车辆故障码",description = "车辆故障码管理")
@Log4j2
public class SysCarFaultController extends BaseController
{
    @Resource
    private SysCarFaultService sysCarFaultService;

    /**
     * 查询车辆故障码管理列表
     */
    @RequiresPermissions("breakdown:breakdown:list")
    @GetMapping("/list")
    @Operation(summary = "查询车辆故障码管理列表",description ="查询车辆故障码管理列表")
    public Result<TableDataInfo<SysCarFault>> list(SysCarFault sysCarFault)
    {
        startPage();
        List<SysCarFault> list = sysCarFaultService.selectSysCarFaultList(sysCarFault);
        return getDataTable(list);
    }



    /**
     * 导出车辆故障码管理列表
     */
    @RequiresPermissions("breakdown:breakdown:export")
    @PostMapping("/export")
    @Operation(summary = "导出车辆故障码管理列表",description = "导出车辆故障码管理列表")
    public void export(HttpServletResponse response, SysCarFault sysCarFault)
    {
        List<SysCarFault> list = sysCarFaultService.selectSysCarFaultList(sysCarFault);
        ExcelUtil<SysCarFault> util = new ExcelUtil<SysCarFault>(SysCarFault.class);
        util.exportExcel(response, list, "车辆故障管理数据");
    }

    /**
     * 获取车辆故障码管理详细信息
     */
    @RequiresPermissions("breakdown:breakdown:query")
    @GetMapping(value = "/{id}")
    @Operation(summary = "获取车辆故障码管理详细信息")
    public Result<List<SysCarFault>> getInfo(@PathVariable("id") Long id)
    {
        return success(sysCarFaultService.selectSysCarFaultById(id));
    }

    /**
     * 新增车辆故障码管理
     */
    @RequiresPermissions("breakdown:breakdown:add")
    @PostMapping
    @Operation(summary = "新增车辆故障码管理")
    public Result<Integer> add(
            @Validated @RequestBody SysCarFault sysCarFault)
    {
        //判断故障码是否重复
        SysCarFault selectFaultByFaultCode = sysCarFaultService.selectFaultByFaultCode(sysCarFault.getFaultCode());
        if (selectFaultByFaultCode!=null){
            return error("新增车辆故障 ,故障码已存在");
        }
        sysCarFault.setCreateBy(SecurityUtils.getUsername());
        return toAjax(sysCarFaultService.save(sysCarFault));
    }


    /**
     * 修改车辆故障码管理
     */
    @RequiresPermissions("breakdown:breakdown:edit")
    @PutMapping
    @Operation(summary = "修改车辆故障码管理")
    public Result<Integer> edit(
            @Validated @RequestBody SysCarFault sysCarFault)
    {

        sysCarFault.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(sysCarFaultService.updateById(sysCarFault));
    }

    /**
     * 删除车辆故障码管理
     */
    @RequiresPermissions("breakdown:breakdown:remove")
    @DeleteMapping("/{ids}")
    @Operation(summary = "删除车辆故障码管理")
    public Result<Integer> remove(@PathVariable("ids") Long[] ids)
    {
        sysCarFaultService.removeBatchByIds(Arrays.asList(ids));
        return success();
    }

    /**
     * 启用故障码警告
     * @param id
     */
    @PutMapping("/enableWarningsById/{id}")
    @Operation(summary = "启用故障码警告")
    public void enableWarningsById(@PathVariable("id")Long id){
        UpdateWrapper<SysCarFault> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",id);
        wrapper.set("warn_status",0);
        sysCarFaultService.update(wrapper);
    }
    /**
     * 禁用故障码告警
     * @param id
     */
    @PutMapping("/disableWarningsById/{id}")
    @Operation(summary = "禁用故障码告警")
    public void disableWarningsById(@PathVariable("id")Long id){
        UpdateWrapper<SysCarFault> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",id);
        wrapper.set("warn_status",1);
        sysCarFaultService.update(wrapper);
    }

}
