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

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

import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.framework.interceptor.annotation.RepeatSubmit;
import com.ruoyi.framework.security.service.SysPermissionService;
import com.ruoyi.project.plant.controller.TMtMeetingController;
import com.ruoyi.project.plant.domain.TMtKeymaintenance;
import com.ruoyi.project.plant.domain.TStaffmgr;
import com.ruoyi.project.plant.mapper.TStaffmgrMapper;
import com.ruoyi.project.system.domain.SysDictData;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.system.service.ISysDictTypeService;
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.TMtMessageattention;
import com.ruoyi.project.plant.service.ITMtMessageattentionService;
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-04
 */
@RestController
@RequestMapping("/plant/messageattention")
public class TMtMessageattentionController extends BaseController {
    @Autowired
    private ITMtMessageattentionService tMtMessageattentionService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private SysPermissionService permissionService;
    @Autowired
    private TMtMeetingController tMtMeetingController;
    @Autowired
    private ISysDictTypeService iSysDictTypeService;
    @Resource
    private TStaffmgrMapper tStaffmgrMapper;
    /**
     * 查询信息提醒列表
     */
    @PreAuthorize("@ss.hasPermi('plant:meeting:list')")
    @GetMapping("/list")
    public TableDataInfo list(TMtMessageattention tMtMessageattention) {
        // 权限集合
        SysUser s = sysUserService.selectUserById(getUserId());
        Set<String> permissions = permissionService.getMenuPermission(s);
        if (!permissions.contains("plant:meeting:allList") && !s.isAdmin()) {
            tMtMessageattention.setResponsible(s.getStaffId());
            tMtMessageattention.setCreaterCode(getUserId() + "");
        }
        startPage();
        List<TMtMessageattention> list = tMtMessageattentionService.selectTMtMessageattentionList(tMtMessageattention);
        List<SysDictData> dictList = iSysDictTypeService.selectDictDataByType("ACTUALPOST");

        for (TMtMessageattention t : list
        ) {
            //指定人
            if ("1".equals(t.getIsPerson())) {
                if (StringUtils.isNotEmpty(t.getResponsible())) {
                    t.setRespName(t.getResponsible());
                    SysUser user = sysUserService.selectUserByStaffId(t.getResponsible());
                    if (user != null) {
                        t.setRespName(user.getNickName());
                    }
                }
            }
            //指定岗位
            else if ("2".equals(t.getIsPerson())) {
                if (StringUtils.isNotEmpty(t.getPosition())) {
                    String[] arr = t.getPosition().split(",");
                    for (int i = 0; i < arr.length; i++) {
                        if (i == 0) {
                            t.setRespName(DictUtils.getDictVale(dictList, arr[i]));
                        } else {
                            t.setRespName(t.getRespName() + "," + DictUtils.getDictVale(dictList, arr[i]));
                        }
                    }
                }
            }
            if (t.getUpdaterCode() != null) {
                SysUser updater = sysUserService.selectUserById(Long.parseLong(t.getUpdaterCode()));
                t.setUpdaterCode(updater.getNickName());
            }
            if (t.getCreaterCode() != null) {
                SysUser creator = sysUserService.selectUserById(Long.parseLong(t.getCreaterCode()));
                t.setCreaterCode(creator.getNickName());
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出信息提醒列表
     */
    @PreAuthorize("@ss.hasPermi('plant:meeting:export')")
    @Log(title = "信息提醒", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TMtMessageattention tMtMessageattention) {
        List<TMtMessageattention> list = tMtMessageattentionService.selectTMtMessageattentionList(tMtMessageattention);
        ExcelUtil<TMtMessageattention> util = new ExcelUtil<TMtMessageattention>(TMtMessageattention.class);
        return util.exportExcel(list, "messageattention");
    }

    /**
     * 获取信息提醒详细信息
     */
    @PreAuthorize("@ss.hasPermi('plant:meeting:list')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(tMtMessageattentionService.selectTMtMessageattentionById(id));
    }

    /**
     * 新增信息提醒
     */
    @Log(title = "信息提醒", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping
    public AjaxResult add(@RequestBody TMtMessageattention tMtMessageattention) {
        tMtMessageattention.setUpdatedate(new Date());
        tMtMessageattention.setUpdaterCode(getUserId() + "");
        tMtMessageattention.setCreaterCode(getUserId() + "");
        if (tMtMessageattention.getIsHis() != null && tMtMessageattention.getIsHis() == 1) {
            tMtMeetingController.getReport(null, tMtMessageattention.getMeetingId());
        }
        //指定岗位
        if ("2".equals(tMtMessageattention.getIsPerson())) {
            if (StringUtils.isNotEmpty(tMtMessageattention.getPosition())) {
                TStaffmgr t = new TStaffmgr();
                t.setDeptId(103l);
                t.setActualposts(tMtMessageattention.getPosition());
                List<TStaffmgr> staffmgrList = tStaffmgrMapper.selectTStaffmgrListByDeptId(t);
                String respStr = "";
                for (TStaffmgr s : staffmgrList
                ) {
                    respStr = respStr + s.getStaffid() + ",";
                }
                tMtMessageattention.setResponsible(respStr);
            }
        }
        return toAjax(tMtMessageattentionService.insertTMtMessageattention(tMtMessageattention));
    }

    /**
     * 修改信息提醒
     */
    @Log(title = "信息提醒", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping
    public AjaxResult edit(@RequestBody TMtMessageattention tMtMessageattention) {
        tMtMessageattention.setUpdatedate(new Date());
        tMtMessageattention.setUpdaterCode(getUserId() + "");
        if (tMtMessageattention.getIsHis() != null && tMtMessageattention.getIsHis() == 1) {
            tMtMeetingController.getReport(null, tMtMessageattention.getMeetingId());
        }
        //指定岗位
        if ("2".equals(tMtMessageattention.getIsPerson())) {
            if (StringUtils.isNotEmpty(tMtMessageattention.getPosition())) {
                TStaffmgr t = new TStaffmgr();
                t.setDeptId(103l);
                t.setActualposts(tMtMessageattention.getPosition());
                List<TStaffmgr> staffmgrList = tStaffmgrMapper.selectTStaffmgrListByDeptId(t);
                String respStr = "";
                for (TStaffmgr s : staffmgrList
                ) {
                    respStr = respStr + s.getStaffid() + ",";
                }
                tMtMessageattention.setResponsible(respStr);
            }
        }
        return toAjax(tMtMessageattentionService.updateTMtMessageattention(tMtMessageattention));
    }

    /**
     * 删除信息提醒
     */
    @PreAuthorize("@ss.hasPermi('plant:meeting:remove')")
    @Log(title = "信息提醒", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tMtMessageattentionService.deleteTMtMessageattentionByIds(ids));
    }
}
