package com.muyu.breakdown.controller;

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

import com.muyu.breakdown.domain.FaultLog;
import com.muyu.breakdown.service.IFaultLogService;
import jakarta.servlet.http.HttpServletResponse;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.muyu.common.security.annotation.RequiresPermissions;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.poi.ExcelUtil;
import com.muyu.common.security.utils.SecurityUtils;
import org.springframework.validation.annotation.Validated;
import com.muyu.common.core.web.page.TableDataInfo;

/**
 * 故障日志Controller
 *
 * @author muyu
 * @date 2024-09-20
 */
@RestController
@RequestMapping("/log")
public class FaultLogController extends BaseController
{
    @Resource
    private IFaultLogService faultLogService;

    /**
     * 查询故障日志列表
     */
    @RequiresPermissions("platform:log:list")
    @GetMapping("/list")
    public Result<TableDataInfo<FaultLog>> list(FaultLog faultLog)
    {
        startPage();
        List<FaultLog> list = faultLogService.selectFaultLogList(faultLog);
        return getDataTable(list);
    }

    /**
     * 导出故障日志列表
     */
    @RequiresPermissions("platform:log:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, FaultLog faultLog)
    {
        List<FaultLog> list = faultLogService.selectFaultLogList(faultLog);
        ExcelUtil<FaultLog> util = new ExcelUtil<FaultLog>(FaultLog.class);
        util.exportExcel(response, list, "故障日志数据");
    }

    /**
     * 获取故障日志详细信息
     */
    @RequiresPermissions("platform:log:query")
    @GetMapping(value = "/{id}")
    public Result<List<FaultLog>> getInfo(@PathVariable("id") Long id)
    {
        return success(faultLogService.selectFaultLogById(id));
    }

    /**
     * 新增故障日志
     */
    @RequiresPermissions("platform:log:add")
    @PostMapping
    public Result<Integer> add(
            @Validated @RequestBody FaultLog faultLog)
    {
        if (faultLogService.checkIdUnique(faultLog)) {
            return error("新增 故障日志 '" + faultLog + "'失败，故障日志已存在");
        }
        faultLog.setCreateBy(SecurityUtils.getUsername());
        return toAjax(faultLogService.save(faultLog));
    }

    /**
     * 修改故障日志
     */
    @RequiresPermissions("platform:log:edit")
    @PutMapping
    public Result<Integer> edit(
            @Validated @RequestBody FaultLog faultLog)
    {
        if (!faultLogService.checkIdUnique(faultLog)) {
            return error("修改 故障日志 '" + faultLog + "'失败，故障日志不存在");
        }
        faultLog.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(faultLogService.updateById(faultLog));
    }

    /**
     * 删除故障日志
     */
    @RequiresPermissions("platform:log:remove")
	@DeleteMapping("/{ids}")
    public Result<Integer> remove(@PathVariable("ids") Long[] ids)
    {
        faultLogService.removeBatchByIds(Arrays.asList(ids));
        return success();
    }
}
