package com.muyu.breakdown.controller;

import com.muyu.breakdown.domain.BreakDown;
import com.muyu.breakdown.domain.MessageMap;
import com.muyu.breakdown.service.BreakDownService;
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.security.annotation.RequiresPermissions;
import com.muyu.common.security.utils.SecurityUtils;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

/**
 * @ Tool：IntelliJ IDEA
 * @ Author：CHX
 * @ Date：2024-09-17-15:13
 * @ Version：1.0
 * @ Description：故障管理实体类
 * @author Lenovo
 */
@RestController
@RequestMapping("/breakdown")
public class BreakDownController extends BaseController {

    @Autowired
    private BreakDownService breakDownService;
    /**
     * 查询车辆故障列表
     */
    @RequiresPermissions("breakdown:breakdown:list")
    @GetMapping("/list")
    public Result<TableDataInfo<BreakDown>> list(BreakDown breakDown)
    {
        startPage();
        List<BreakDown> list = breakDownService.selectBreakDownList(breakDown);
        return getDataTable(list);
    }

    /**
     * 导出车辆故障列表
     */
    @RequiresPermissions("breakdown:breakdown:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, BreakDown breakDown)
    {
        List<BreakDown> list = breakDownService.selectBreakDownList(breakDown);
        ExcelUtil<BreakDown> util = new ExcelUtil<BreakDown>(BreakDown.class);
        util.exportExcel(response, list, "车辆故障数据");
    }

    /**
     * 获取车辆故障详细信息
     */
    @RequiresPermissions("breakdown:breakdown:query")
    @GetMapping(value = "/{id}")
    public Result<List<BreakDown>> getInfo(@PathVariable("id") Long id)
    {
        return success(breakDownService.selectBreakDownById(id));
    }

    /**
     * 新增车辆故障
     */
    @RequiresPermissions("breakdown:breakdown:add")
    @PostMapping
    public Result<Integer> add(
            @Validated @RequestBody BreakDown breakDown)
    {
        if (breakDownService.checkIdUnique(breakDown)) {
            return error("新增 车辆故障 '" + breakDown + "'失败，车辆故障已存在");
        }
        breakDown.setCreateBy(SecurityUtils.getUsername());
        return toAjax(breakDownService.save(breakDown));
    }

    /**
     * 修改车辆故障
     */
    @RequiresPermissions("breakdown:breakdown:edit")
    @PutMapping
    public Result<Integer> edit(
            @Validated @RequestBody BreakDown breakDown)
    {
        if (!breakDownService.checkIdUnique(breakDown)) {
            return error("修改 车辆故障 '" + breakDown + "'失败，车辆故障不存在");
        }
        breakDown.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(breakDownService.updateById(breakDown));
    }

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

    public Result getMessages(MessageMap messageMap) {
      Map<String,String> messages =breakDownService.getMessages(messageMap);
      return Result.success(messages);
    }

}
