package com.ruoyi.project.system.controller;

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

import com.ruoyi.project.plant.domain.TStaffmgr;
import com.ruoyi.project.plant.service.ITStaffmgrService;
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.system.domain.TAlarmPrincipal;
import com.ruoyi.project.system.service.ITAlarmPrincipalService;
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 2020-12-30
 */
@RestController
@RequestMapping("/system/principal")
public class TAlarmPrincipalController extends BaseController
{
    @Autowired
    private ITAlarmPrincipalService tAlarmPrincipalService;

    @Autowired
    private ITStaffmgrService tStaffmgrService;
    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询预警管理负责人列表
     */
    @GetMapping("/list")
    public TableDataInfo list(TAlarmPrincipal tAlarmPrincipal)
    {
        startPage();
        List<TAlarmPrincipal> list = tAlarmPrincipalService.selectTAlarmPrincipalList(tAlarmPrincipal);
        for (TAlarmPrincipal t: list
             ) {
            SysUser user = sysUserService.selectUserByStaffId(t.getStaffid());
            if (user != null) {
                t.setPrincipalName(user.getNickName());
                t.setPrincipalEmail(user.getEmail());
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出预警管理负责人列表
     */
    @PreAuthorize("@ss.hasPermi('system:principal:export')")
    @Log(title = "预警管理负责人", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TAlarmPrincipal tAlarmPrincipal)
    {
        List<TAlarmPrincipal> list = tAlarmPrincipalService.selectTAlarmPrincipalList(tAlarmPrincipal);
        ExcelUtil<TAlarmPrincipal> util = new ExcelUtil<TAlarmPrincipal>(TAlarmPrincipal.class);
        return util.exportExcel(list, "principal");
    }

    /**
     * 获取预警管理负责人详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:principal:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tAlarmPrincipalService.selectTAlarmPrincipalById(id));
    }

    /**
     * 新增预警管理负责人
     */
    @PreAuthorize("@ss.hasPermi('system:principal:add')")
    @Log(title = "预警管理负责人", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TAlarmPrincipal tAlarmPrincipal)
    {
        tAlarmPrincipal.setCreaterCode(getUserId().toString());
        //人员信息对应
//        TStaffmgr staffmgr = tStaffmgrService.selectTStaffmgrByStaffId(tAlarmPrincipal.getStaffid());
//        tAlarmPrincipal.setPrincipalName(staffmgr.getName());
//        tAlarmPrincipal.setPrincipalEmail(staffmgr.getMail());
//        tAlarmPrincipal.setDeptId(staffmgr.getDeptId());
        return toAjax(tAlarmPrincipalService.insertTAlarmPrincipal(tAlarmPrincipal));
    }

    /**
     * 修改预警管理负责人
     */
    @PreAuthorize("@ss.hasPermi('system:principal:edit')")
    @Log(title = "预警管理负责人", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TAlarmPrincipal tAlarmPrincipal)
    {
        tAlarmPrincipal.setUpdaterCode(getUserId().toString());
        tAlarmPrincipal.setUpdatedate(new Date());
        //人员信息对应
        SysUser user = sysUserService.selectUserByStaffId(tAlarmPrincipal.getStaffid());
        if (user!=null) {
            tAlarmPrincipal.setPrincipalName(user.getNickName());
            tAlarmPrincipal.setPrincipalEmail(user.getEmail());
        }
        return toAjax(tAlarmPrincipalService.updateTAlarmPrincipal(tAlarmPrincipal));
    }

    /**
     * 删除预警管理负责人
     */
    @PreAuthorize("@ss.hasPermi('system:principal:remove')")
    @Log(title = "预警管理负责人", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tAlarmPrincipalService.deleteTAlarmPrincipalByIds(ids));
    }
}
