package com.ruoyi.project.pssr.controller;

import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.config.RuoYiConfig;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.pssr.domain.TPssrAlarm;
import com.ruoyi.project.pssr.mapper.TPssrAlarmMapper;
import com.ruoyi.project.pssr.service.ITPssrAlarmService;
import com.ruoyi.project.pssr.service.ITPssrFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * 报警联锁值确认Controller
 *
 * @author ssy
 * @date 2024-09-18
 */
@RestController
@RequestMapping("/pssr/alarm")
public class TPssrAlarmController extends BaseController {
    @Autowired
    private ITPssrFileService tPssrFileService;
    @Autowired
    private ITPssrAlarmService tPssrAlarmService;
    @Resource
    private TPssrAlarmMapper tPssrAlarmMapper;
    /**
     * 查询报警联锁值确认列表
     */
    @PreAuthorize("@ss.hasPermi('pssr:alarm:list')")
    @GetMapping("/list")
    public TableDataInfo list(TPssrAlarm tPssrAlarm) {
        startPage();
        List<TPssrAlarm> list = tPssrAlarmService.selectTPssrAlarmList(tPssrAlarm);
        return getDataTable(list);
    }

    /**
     * 导出报警联锁值确认列表
     */
    @PreAuthorize("@ss.hasPermi('pssr:alarm:export')")
    @Log(title = "报警联锁值确认", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TPssrAlarm tPssrAlarm) {
        List<TPssrAlarm> list = tPssrAlarmService.selectTPssrAlarmList(tPssrAlarm);
        ExcelUtil<TPssrAlarm> util = new ExcelUtil<TPssrAlarm>(TPssrAlarm.class);
        return util.exportExcel(list, "alarm");
    }

    /**
     * 获取报警联锁值确认详细信息
     */
    @PreAuthorize("@ss.hasPermi('pssr:alarm:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        TPssrAlarm item = tPssrAlarmService.selectTPssrAlarmById(id);
        return AjaxResult.success(item);
    }

    /**
     * 新增报警联锁值确认
     */
    @PreAuthorize("@ss.hasPermi('pssr:alarm:add')")
    @Log(title = "报警联锁值确认", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TPssrAlarm tPssrAlarm) {
        return toAjax(tPssrAlarmService.insertTPssrAlarm(tPssrAlarm));
    }

    /**
     * 修改报警联锁值确认
     */
    @PreAuthorize("@ss.hasPermi('pssr:alarm:edit')")
    @Log(title = "报警联锁值确认", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TPssrAlarm tPssrAlarm) {
        TPssrAlarm entity = tPssrAlarmService.selectTPssrAlarmById(tPssrAlarm.getId());
        if (entity.getApproveStatus() != 1 && entity.getApproveStatus() != 0) {
            return  AjaxResult.error("当前状态不可修改！");
        }
        return toAjax(tPssrAlarmService.updateTPssrAlarm(tPssrAlarm));
    }

    /**
     * 删除报警联锁值确认
     */
    @PreAuthorize("@ss.hasPermi('pssr:alarm:remove')")
    @Log(title = "报警联锁值确认", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tPssrAlarmService.deleteTPssrAlarmByIds(ids));
    }


    @Log(title = "报警联锁值确认附件上传", businessType = BusinessType.UPDATE)
    @PostMapping("/uploadFile")
    public AjaxResult uploadFile(@RequestParam("file") MultipartFile file, Long subId) throws IOException {
        if (!file.isEmpty()) {
            String url = FileUploadUtils.upload(RuoYiConfig.getFilePath("/pssr/bjlszqr"), file);
            TPssrAlarm test = new TPssrAlarm();
            test.setSubId(subId);
            test.setFileUrl(url);
            test.setFileName(file.getOriginalFilename());
            test.setApproveStatus(0L);
            test.setCreatedate(new Date());
            test.setCreaterCode(getUserId().toString());
            tPssrAlarmService.insertTPssrAlarm(test);
            return AjaxResult.success();
        }
        return AjaxResult.error("上传失败，请联系管理员");
    }
}
