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

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

import com.alibaba.fastjson.JSON;
import com.ruoyi.framework.interceptor.annotation.RepeatSubmit;
import com.ruoyi.project.plant.controller.TMtMeetingController;
import com.ruoyi.project.plant.domain.TMtdEmail;
import com.ruoyi.project.plant.domain.TStaffmgr;
import com.ruoyi.project.plant.mapper.TStaffmgrMapper;
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.TMtPerson;
import com.ruoyi.project.plant.service.ITMtPersonService;
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;

import javax.annotation.Resource;

/**
 * 会议人员Controller
 *
 * @author ruoyi
 * @date 2021-01-14
 */
@RestController
@RequestMapping("/plant/person")
public class TMtPersonController extends BaseController
{
    @Autowired
    private ITMtPersonService tMtPersonService;
    @Resource
    private TStaffmgrMapper tStaffmgrMapper;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private TMtMeetingController tMtMeetingController;
    /**
     * 查询会议人员列表
     */
    @PreAuthorize("@ss.hasPermi('plant:meeting:list')")
    @GetMapping("/list")
    public TableDataInfo list(TMtPerson tMtPerson)
    {
        startPage();
        List<TMtPerson> list = tMtPersonService.selectTMtPersonList(tMtPerson);
        for (TMtPerson t: list
        ) {
            SysUser user = sysUserService.selectUserByStaffId(t.getStaffid());
            if (user != null) {
                t.setName(user.getNickName());
                t.setContact(user.getEmail());
                t.setUnit(user.getDept().getDeptName());
            }
        }

        return getDataTable(list);
    }

    /**
     * 导出会议人员列表
     */
    @PreAuthorize("@ss.hasPermi('plant:meeting:export')")
    @Log(title = "会议人员", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TMtPerson tMtPerson)
    {
        List<TMtPerson> list = tMtPersonService.selectTMtPersonList(tMtPerson);
        ExcelUtil<TMtPerson> util = new ExcelUtil<TMtPerson>(TMtPerson.class);
        return util.exportExcel(list, "person");
    }

    /**
     * 获取会议人员详细信息
     */
    @PreAuthorize("@ss.hasPermi('plant:meeting:list')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tMtPersonService.selectTMtPersonById(id));
    }

    /**
     * 新增会议人员
     */
    @PreAuthorize("@ss.hasPermi('plant:meeting:add')")
    @Log(title = "会议人员", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping
    public AjaxResult add(@RequestBody TMtPerson tMtPerson)
    {
        tMtPerson.setUpdatedate(new Date());
        tMtPerson.setUpdaterCode(getUserId() + "");
        tMtPerson.setCreaterCode(getUserId() + "");
        if (tMtPerson.getIsHis() != null && tMtPerson.getIsHis() == 1) {

            tMtMeetingController.getReport(null,tMtPerson.getMeetingId());
        }
        return toAjax(tMtPersonService.insertTMtPerson(tMtPerson));
    }

    /**
     * 修改会议人员
     */
    @PreAuthorize("@ss.hasPermi('plant:meeting:edit')")
    @Log(title = "会议人员", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping
    public AjaxResult edit(@RequestBody TMtPerson tMtPerson)
    {
        tMtPerson.setUpdatedate(new Date());
        tMtPerson.setUpdaterCode(getUserId() + "");
        if (tMtPerson.getIsHis() != null && tMtPerson.getIsHis() == 1) {
            tMtMeetingController.getReport(null,tMtPerson.getMeetingId());
        }
        return toAjax(tMtPersonService.updateTMtPerson(tMtPerson));
    }

    /**
     * 删除会议人员
     */
    @PreAuthorize("@ss.hasPermi('plant:meeting:remove')")
    @Log(title = "会议人员", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tMtPersonService.deleteTMtPersonByIds(ids));
    }
}
