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

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

import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.framework.interceptor.annotation.RepeatSubmit;
import com.ruoyi.project.plant.controller.TMtMeetingController;
import com.ruoyi.project.plant.domain.TMtKeymaintenance;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.system.service.ISysUserService;
import org.apache.commons.lang.StringUtils;
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.TMtEhsopenitem;
import com.ruoyi.project.plant.service.ITMtEhsopenitemService;
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;

/**
 * EHS开项跟踪Controller
 *
 * @author ruoyi
 * @date 2021-01-04
 */
@RestController
@RequestMapping("/plant/ehsopenitem")
public class TMtEhsopenitemController extends BaseController {
    @Autowired
    private ITMtEhsopenitemService tMtEhsopenitemService;
    @Autowired
    private TMtMeetingController tMtMeetingController;
    @Autowired
    private ISysUserService sysUserService;
    /**
     * 查询EHS开项跟踪列表
     */
    @PreAuthorize("@ss.hasPermi('plant:meeting:list')")
    @GetMapping("/list")
    public TableDataInfo list(TMtEhsopenitem tMtEhsopenitem) {
        startPage();
        List<TMtEhsopenitem> list = tMtEhsopenitemService.selectTMtEhsopenitemList(tMtEhsopenitem);
        for (TMtEhsopenitem t:list
        ) {
            if (t.getUpdaterCode() != null) {
                SysUser updater = sysUserService.selectUserById( Long.parseLong(t.getUpdaterCode()));
                t.setUpdaterCode(updater.getNickName());
            }
        }
        return getDataTable(list);
    }

    /**
     * 查询EHS开项跟踪列表
     */
    @PreAuthorize("@ss.hasPermi('plant:meeting:list')")
    @GetMapping("/ehsData")
    public List<TMtEhsopenitem> ehsData(TMtEhsopenitem tMtEhsopenitem) {
        List<TMtEhsopenitem> list = tMtEhsopenitemService.selectTMtEhsopenitemList(tMtEhsopenitem);
        return list;
    }


    /**
     * 导出EHS开项跟踪列表
     */
    @PreAuthorize("@ss.hasPermi('plant:meeting:export')")
    @Log(title = "EHS开项跟踪", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TMtEhsopenitem tMtEhsopenitem) {
        List<TMtEhsopenitem> list = tMtEhsopenitemService.selectTMtEhsopenitemList(tMtEhsopenitem);
        ExcelUtil<TMtEhsopenitem> util = new ExcelUtil<TMtEhsopenitem>(TMtEhsopenitem.class);
        return util.exportExcel(list, "ehsopenitem");
    }

    /**
     * 获取EHS开项跟踪详细信息
     */
    @PreAuthorize("@ss.hasPermi('plant:meeting:list')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(tMtEhsopenitemService.selectTMtEhsopenitemById(id));
    }

    /**
     * 新增EHS开项跟踪
     */
    @PreAuthorize("@ss.hasPermi('plant:ehsopenitem:edit')")
    @Log(title = "EHS开项跟踪", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping
    public AjaxResult add(@RequestBody TMtEhsopenitem tMtEhsopenitem) {
        tMtEhsopenitem.setUpdatedate(new Date());
        tMtEhsopenitem.setUpdaterCode(getUserId() + "");
        tMtEhsopenitem.setCreaterCode(getUserId() + "");
        if (tMtEhsopenitem.getIsHis() != null && tMtEhsopenitem.getIsHis() == 1) {

            tMtMeetingController.getReport(null, tMtEhsopenitem.getMeetingId());
        }
        return toAjax(tMtEhsopenitemService.insertTMtEhsopenitem(tMtEhsopenitem));
    }

    /**
     * 修改EHS开项跟踪
     */
    @PreAuthorize("@ss.hasPermi('plant:ehsopenitem:edit')")
    @Log(title = "EHS开项跟踪", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping
    public AjaxResult edit(@RequestBody TMtEhsopenitem tMtEhsopenitem) {
        tMtEhsopenitem.setUpdatedate(new Date());
        tMtEhsopenitem.setUpdaterCode(getUserId() + "");
        if (tMtEhsopenitem.getIsHis() != null && tMtEhsopenitem.getIsHis() == 1) {

            tMtMeetingController.getReport(null, tMtEhsopenitem.getMeetingId());
        }
        return toAjax(tMtEhsopenitemService.updateTMtEhsopenitem(tMtEhsopenitem));
    }

    /**
     * 删除EHS开项跟踪
     */
    @PreAuthorize("@ss.hasPermi('plant:ehsopenitem:edit')")
    @Log(title = "EHS开项跟踪", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tMtEhsopenitemService.deleteTMtEhsopenitemByIds(ids));
    }
}
