package com.cpms.project.safety.controller;

import com.cpms.common.annotation.Log;
import com.cpms.common.core.controller.BaseController;
import com.cpms.common.core.domain.AjaxResult;
import com.cpms.common.core.page.TableDataInfo;
import com.cpms.common.enums.BusinessType;
import com.cpms.common.utils.poi.ExcelUtil;
import com.cpms.project.safety.domain.TSafetyEyeWasher;
import com.cpms.project.safety.domain.TSafetyFireExtinguisher;
import com.cpms.project.safety.domain.TSafetyFiredoor;
import com.cpms.project.safety.domain.TSafetyRecord;
import com.cpms.project.safety.service.ITSafetyEyeWasherService;
import com.cpms.project.safety.service.ITSafetyFireExtinguisherService;
import com.cpms.project.safety.service.ITSafetyFiredoorService;
import com.cpms.project.safety.service.ITSafetyRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 洗眼器检查记录Controller
 *
 * @author ruoyi
 * @date 2023-05-25
 */
@RestController
@RequestMapping("/safety/record")
public class TSafetyRecordController extends BaseController {
    @Autowired
    private ITSafetyRecordService tSafetyRecordService;

    @Autowired
    private ITSafetyEyeWasherService tSafetyEyeWasherService;

    @Autowired
    private ITSafetyFiredoorService tSafetyFiredoorService;

    @Autowired
    private ITSafetyFireExtinguisherService tSafetyFireExtinguisherService;

    /**
     * 查询洗眼器检查记录列表
     */
    @PreAuthorize("@ss.hasPermi('safety:record:list')")
    @GetMapping("/list")
    public TableDataInfo list(TSafetyRecord tSafetyRecord) {
        startPage();
        List<TSafetyRecord> list = tSafetyRecordService.selectTSafetyRecordList(tSafetyRecord);
        return getDataTable(list);
    }

    /**
     * 导出洗眼器检查记录列表
     */
    @PreAuthorize("@ss.hasPermi('safety:record:export')")
    @Log(title = "洗眼器检查记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TSafetyRecord tSafetyRecord) {
        List<TSafetyRecord> list = tSafetyRecordService.selectTSafetyRecordList(tSafetyRecord);
        ExcelUtil<TSafetyRecord> util = new ExcelUtil<TSafetyRecord>(TSafetyRecord.class);
        return util.exportExcel(list, "洗眼器检查记录数据");
    }

    /**
     * 获取洗眼器检查记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('safety:record:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(tSafetyRecordService.selectTSafetyRecordById(id));
    }

    /**
     * 新增洗眼器检查记录
     */
    @PreAuthorize("@ss.hasPermi('safety:record:add')")
    @Log(title = "洗眼器检查记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TSafetyRecord tSafetyRecord) {
        //新增记录表
        tSafetyRecord.setCreatedate(new Date());
        tSafetyRecord.setCreaterCode(getUserId().toString());
        tSafetyRecordService.insertTSafetyRecord(tSafetyRecord);
        if ("1".equals(tSafetyRecord.getDevType())) {
            //修改洗眼器表数据为当前最新的检测记录
            TSafetyEyeWasher tSafetyEyeWasher = new TSafetyEyeWasher();
            tSafetyEyeWasher.setStatus(tSafetyRecord.getStatus());
            tSafetyEyeWasher.setFaultDesc(tSafetyRecord.getFaultDesc());
            tSafetyEyeWasher.setFaultSloveSign(tSafetyRecord.getFaultSloveSign());
            tSafetyEyeWasher.setInspector(tSafetyRecord.getInspector());
            tSafetyEyeWasher.setInspectdate(tSafetyRecord.getInspectdate());
            tSafetyEyeWasher.setInspectstatus(tSafetyRecord.getInspectstatus());
            tSafetyEyeWasher.setUpdaterCode(getUserId().toString());
            tSafetyEyeWasher.setUpdatedate(new Date());
            tSafetyEyeWasher.setFileIds(tSafetyRecord.getFileIds());
            tSafetyEyeWasher.setId(tSafetyRecord.getDevId());
            tSafetyEyeWasherService.updateTSafetyEyeWasher(tSafetyEyeWasher);
        }else if("2".equals(tSafetyRecord.getDevType())) {
            TSafetyFiredoor tSafetyFiredoor = new TSafetyFiredoor();
            tSafetyFiredoor.setInspector(tSafetyRecord.getInspector());
            tSafetyFiredoor.setInspectdate(tSafetyRecord.getInspectdate());
            tSafetyFiredoor.setStatus(tSafetyRecord.getStatus());
            tSafetyFiredoor.setMeasure(tSafetyRecord.getMeasure());
            tSafetyFiredoor.setFileIds(tSafetyRecord.getFileIds());
            tSafetyFiredoor.setUpdaterCode(getUserId().toString());
            tSafetyFiredoor.setUpdatedate(new Date());
            tSafetyFiredoor.setId(tSafetyRecord.getDevId());
            tSafetyFiredoorService.updateTSafetyFiredoor(tSafetyFiredoor);
        } else if ("3".equals(tSafetyRecord.getDevType())) {
            TSafetyFireExtinguisher tSafetyFireExtinguisher = new TSafetyFireExtinguisher();
            tSafetyFireExtinguisher.setInspector(tSafetyRecord.getInspector());
            tSafetyFireExtinguisher.setFillingDate(tSafetyRecord.getFillingDate());
            tSafetyFireExtinguisher.setInspectdate(tSafetyRecord.getInspectdate());
            tSafetyFireExtinguisher.setStatus(tSafetyRecord.getStatus());
            tSafetyFireExtinguisher.setMeasure(tSafetyRecord.getMeasure());
            tSafetyFireExtinguisher.setInspectstatus(tSafetyRecord.getInspectstatus());
            tSafetyFireExtinguisher.setFileIds(tSafetyRecord.getFileIds());
            tSafetyFireExtinguisher.setId(tSafetyRecord.getDevId());
            tSafetyFireExtinguisherService.updateTSafetyFireExtinguisher(tSafetyFireExtinguisher);
        }
        return AjaxResult.success();
    }

    /**
     * 修改洗眼器检查记录
     */
    @PreAuthorize("@ss.hasPermi('safety:record:edit')")
    @Log(title = "洗眼器检查记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TSafetyRecord tSafetyRecord) {
        tSafetyRecord.setUpdatedate(new Date());
        tSafetyRecord.setUpdaterCode(getUserId().toString());
        tSafetyRecordService.updateTSafetyRecord(tSafetyRecord);
        if ("1".equals(tSafetyRecord.getDevType())) {
            TSafetyRecord recordQuery = new TSafetyRecord();
            recordQuery.setDevId(tSafetyRecord.getDevId());
            recordQuery.setDevType("1");
            TSafetyRecord lastRecord = tSafetyRecordService.selectTSafetyRecordList(recordQuery).get(0);
            TSafetyEyeWasher tSafetyEyeWasher = new TSafetyEyeWasher();
            tSafetyEyeWasher.setStatus(lastRecord.getStatus());
            tSafetyEyeWasher.setFaultDesc(lastRecord.getFaultDesc());
            tSafetyEyeWasher.setFaultSloveSign(lastRecord.getFaultSloveSign());
            tSafetyEyeWasher.setInspector(lastRecord.getInspector());
            tSafetyEyeWasher.setInspectdate(lastRecord.getInspectdate());
            tSafetyEyeWasher.setInspectstatus(lastRecord.getInspectstatus());
            tSafetyEyeWasher.setUpdaterCode(getUserId().toString());
            tSafetyEyeWasher.setUpdatedate(new Date());
            tSafetyEyeWasher.setFileIds(tSafetyRecord.getFileIds());
            tSafetyEyeWasher.setId(lastRecord.getDevId());
            tSafetyEyeWasherService.updateTSafetyEyeWasher(tSafetyEyeWasher);
        }else if("2".equals(tSafetyRecord.getDevType())) {
            TSafetyRecord recordQuery = new TSafetyRecord();
            recordQuery.setDevId(tSafetyRecord.getDevId());
            recordQuery.setDevType("2");
            TSafetyRecord lastRecord = tSafetyRecordService.selectTSafetyRecordList(recordQuery).get(0);
            TSafetyFiredoor tSafetyFiredoor = new TSafetyFiredoor();
            tSafetyFiredoor.setInspector(lastRecord.getInspector());
            tSafetyFiredoor.setInspectdate(lastRecord.getInspectdate());
            tSafetyFiredoor.setStatus(lastRecord.getStatus());
            tSafetyFiredoor.setMeasure(lastRecord.getMeasure());
            tSafetyFiredoor.setFileIds(lastRecord.getFileIds());
            tSafetyFiredoor.setUpdaterCode(getUserId().toString());
            tSafetyFiredoor.setUpdatedate(new Date());
            tSafetyFiredoor.setId(tSafetyRecord.getDevId());
            tSafetyFiredoorService.updateTSafetyFiredoor(tSafetyFiredoor);
        } else if ("3".equals(tSafetyRecord.getDevType())) {
            TSafetyRecord recordQuery = new TSafetyRecord();
            recordQuery.setDevId(tSafetyRecord.getDevId());
            recordQuery.setDevType("3");
            TSafetyRecord lastRecord = tSafetyRecordService.selectTSafetyRecordList(recordQuery).get(0);
            TSafetyFireExtinguisher tSafetyFireExtinguisher = new TSafetyFireExtinguisher();
            tSafetyFireExtinguisher.setInspector(lastRecord.getInspector());
            tSafetyFireExtinguisher.setFillingDate(lastRecord.getFillingDate());
            tSafetyFireExtinguisher.setInspectdate(lastRecord.getInspectdate());
            tSafetyFireExtinguisher.setStatus(lastRecord.getStatus());
            tSafetyFireExtinguisher.setMeasure(lastRecord.getMeasure());
            tSafetyFireExtinguisher.setInspectstatus(lastRecord.getInspectstatus());
            tSafetyFireExtinguisher.setFileIds(lastRecord.getFileIds());
            tSafetyFireExtinguisher.setId(tSafetyRecord.getDevId());
            tSafetyFireExtinguisher.setUpdatedate(new Date());
            tSafetyFireExtinguisher.setUpdaterCode(getUserId().toString());
            tSafetyFireExtinguisherService.updateTSafetyFireExtinguisher(tSafetyFireExtinguisher);
        }
        return AjaxResult.success();
    }

    /**
     * 删除洗眼器检查记录
     */
    @PreAuthorize("@ss.hasPermi('safety:record:remove')")
    @Log(title = "洗眼器检查记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tSafetyRecordService.deleteTSafetyRecordByIds(ids));
    }
}
