package org.dromara.elevator.controller;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

import cn.hutool.core.io.IoUtil;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.elevator.domain.bo.EConfirmMessageBo;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.elevator.domain.vo.EFaultMessageVo;
import org.dromara.elevator.domain.bo.EFaultMessageBo;
import org.dromara.elevator.service.IEFaultMessageService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 故障消息;
 *
 * @author zy
 * @date 2024-07-29
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/fault/faultMessage")
public class EFaultMessageController extends BaseController {

    private final IEFaultMessageService eFaultMessageService;

    /**
     * 查询故障消息;列表
     */
    @SaCheckPermission("elevator:faultMessage:list")
    @GetMapping("/list")
    public TableDataInfo<EFaultMessageVo> list(EFaultMessageBo bo, PageQuery pageQuery) {
        return eFaultMessageService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出故障消息;列表
     */
    @SaCheckPermission("elevator:faultMessage:export")
    @Log(title = "故障消息;", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(EFaultMessageBo bo, HttpServletResponse response) {
        List<EFaultMessageVo> list = eFaultMessageService.queryList(bo);
        ExcelUtil.exportExcel(list, "故障消息;", EFaultMessageVo.class, response);
    }

    /**
     * 导出图片
     * @param param
     * @param resp
     */
    @PostMapping("/exportPicture")
    public void exportPicture(EFaultMessageBo param, HttpServletResponse resp) throws IOException {
        eFaultMessageService.exportPicture(param, resp);

    }

    /**
     * 获取故障消息;详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("elevator:faultMessage:query")
    @GetMapping("/{id}")
    public R<EFaultMessageVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(eFaultMessageService.queryById(id));
    }

    /**
     * 新增故障消息;
     */
    @SaCheckPermission("elevator:faultMessage:add")
    @Log(title = "故障消息;", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody EFaultMessageBo bo) {
        return toAjax(eFaultMessageService.insertByBo(bo));
    }

    /**
     * 修改故障消息;
     */
    @SaCheckPermission("elevator:faultMessage:edit")
    @Log(title = "故障消息;", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody EFaultMessageBo bo) {
        return toAjax(eFaultMessageService.updateByBo(bo));
    }

    /**
     * 删除故障消息;
     *
     * @param ids 主键串
     */
    @SaCheckPermission("elevator:faultMessage:remove")
    @Log(title = "故障消息;", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(eFaultMessageService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 确认故障消息;
     */
    @SaCheckPermission("elevator:faultMessage:edit")
    @Log(title = "故障消息;", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/confirm")
    public R<Void> confirmMessage(@Validated(EditGroup.class) @RequestBody EConfirmMessageBo bo) {
        return toAjax(eFaultMessageService.confirmMessage(bo));
    }
}
