package com.ruoyi.project.plant.controller.weekMeeting;

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

import com.ruoyi.framework.interceptor.annotation.RepeatSubmit;
import com.ruoyi.project.plant.controller.TMtMeetingController;
import com.ruoyi.project.plant.domain.TMtEhsopenitem;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
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.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.plant.domain.TMtInterlock;
import com.ruoyi.project.plant.service.ITMtInterlockService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 关键机组联锁旁路Controller
 *
 * @author ruoyi
 * @date 2021-01-05
 */
@RestController
@RequestMapping("/plant/interlock")
public class TMtInterlockController extends BaseController
{
    @Autowired
    private ITMtInterlockService tMtInterlockService;
    @Autowired
    private TMtMeetingController tMtMeetingController;
    @Autowired
    private ISysUserService sysUserService;
    /**
     * 查询关键机组联锁旁路列表
     */
    @PreAuthorize("@ss.hasPermi('plant:meeting:list')")
    @GetMapping("/list")
    public TableDataInfo list(TMtInterlock tMtInterlock)
    {
        startPage();
        List<TMtInterlock> list = tMtInterlockService.selectTMtInterlockList(tMtInterlock);
        for (TMtInterlock t:list
        ) {
            if (t.getUpdaterCode() != null) {
                SysUser updater = sysUserService.selectUserById( Long.parseLong(t.getUpdaterCode()));
                t.setUpdaterCode(updater.getNickName());
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出关键机组联锁旁路列表
     */
    @PreAuthorize("@ss.hasPermi('plant:meeting:export')")
    @Log(title = "关键机组联锁旁路", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TMtInterlock tMtInterlock)
    {
        List<TMtInterlock> list = tMtInterlockService.selectTMtInterlockList(tMtInterlock);
        ExcelUtil<TMtInterlock> util = new ExcelUtil<TMtInterlock>(TMtInterlock.class);
        return util.exportExcel(list, "interlock");
    }

    /**
     * 获取关键机组联锁旁路详细信息
     */
    @PreAuthorize("@ss.hasPermi('plant:meeting:list')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tMtInterlockService.selectTMtInterlockById(id));
    }

    /**
     * 新增关键机组联锁旁路
     */
    @PreAuthorize("@ss.hasPermi('plant:interlock:edit')")
    @Log(title = "关键机组联锁旁路", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping
    public AjaxResult add(@RequestBody TMtInterlock tMtInterlock)
    {
        tMtInterlock.setUpdatedate(new Date());
        tMtInterlock.setUpdaterCode(getUserId() + "");
        tMtInterlock.setCreaterCode(getUserId() + "");
        if (tMtInterlock.getIsHis() != null && tMtInterlock.getIsHis() == 1) {

                tMtMeetingController.getReport(null,tMtInterlock.getMeetingId());
        }
        return toAjax(tMtInterlockService.insertTMtInterlock(tMtInterlock));
    }

    /**
     * 修改关键机组联锁旁路
     */
    @PreAuthorize("@ss.hasPermi('plant:interlock:edit')")
    @Log(title = "关键机组联锁旁路", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping
    public AjaxResult edit(@RequestBody TMtInterlock tMtInterlock)
    {
        tMtInterlock.setUpdatedate(new Date());
        tMtInterlock.setUpdaterCode(getUserId() + "");
        if (tMtInterlock.getIsHis() != null && tMtInterlock.getIsHis() == 1) {

            tMtMeetingController.getReport(null,tMtInterlock.getMeetingId());
        }
        return toAjax(tMtInterlockService.updateTMtInterlock(tMtInterlock));
    }

    /**
     * 删除关键机组联锁旁路
     */
    @PreAuthorize("@ss.hasPermi('plant:interlock:edit')")
    @Log(title = "关键机组联锁旁路", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tMtInterlockService.deleteTMtInterlockByIds(ids));
    }
}
