package com.ruoyi.project.plant.controller;

import java.util.List;

import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.system.domain.TAlarmPrincipal;
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.TMtdEmail;
import com.ruoyi.project.plant.service.ITMtdEmailService;
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 ssy
 * @date 2023-12-05
 */
@RestController
@RequestMapping("/plant/email")
public class TMtdEmailController extends BaseController
{
    @Autowired
    private ITMtdEmailService tMtdEmailService;
    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询会议邮件配置列表
     */
    @GetMapping("/list")
    public TableDataInfo list(TMtdEmail tMtdEmail)
    {
        startPage();
        List<TMtdEmail> list = tMtdEmailService.selectTMtdEmailList(tMtdEmail);
        for (TMtdEmail t: list
        ) {
            SysUser user = sysUserService.selectUserByStaffId(t.getStaffid());
            if (user != null) {
                t.setUserName(user.getNickName());
                t.setUserEmail(user.getEmail());
                t.setUserDept(user.getDept().getDeptName());
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出会议邮件配置列表
     */
    @Log(title = "会议邮件配置", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TMtdEmail tMtdEmail)
    {
        List<TMtdEmail> list = tMtdEmailService.selectTMtdEmailList(tMtdEmail);
        ExcelUtil<TMtdEmail> util = new ExcelUtil<TMtdEmail>(TMtdEmail.class);
        return util.exportExcel(list, "email");
    }

    /**
     * 获取会议邮件配置详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        TMtdEmail t = tMtdEmailService.selectTMtdEmailById(id);
        SysUser user = sysUserService.selectUserByStaffId(t.getStaffid());
        if (user != null) {
            t.setUserName(user.getNickName());
            t.setUserEmail(user.getEmail());
        }
        return AjaxResult.success(t);
    }

    /**
     * 新增会议邮件配置
     */
    @Log(title = "会议邮件配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TMtdEmail tMtdEmail)
    {
        return toAjax(tMtdEmailService.insertTMtdEmail(tMtdEmail));
    }

    /**
     * 修改会议邮件配置
     */
    @Log(title = "会议邮件配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TMtdEmail tMtdEmail)
    {
        return toAjax(tMtdEmailService.updateTMtdEmail(tMtdEmail));
    }

    /**
     * 删除会议邮件配置
     */
    @Log(title = "会议邮件配置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tMtdEmailService.deleteTMtdEmailByIds(ids));
    }
}
