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

import java.util.*;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.framework.interceptor.annotation.RepeatSubmit;
import com.ruoyi.framework.security.service.SysPermissionService;
import com.ruoyi.project.common.domain.DataEntity;
import com.ruoyi.project.plant.controller.TMtMeetingController;
import com.ruoyi.project.plant.domain.TStaffmgr;
import com.ruoyi.project.plant.mapper.TMtActionlistMapper;
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.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.plant.domain.TMtActionlist;
import com.ruoyi.project.plant.service.ITMtActionlistService;
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 2020-12-30
 */
@RestController
@RequestMapping("/plant/actionlist")
public class TMtActionlistController extends BaseController {
    @Autowired
    private ITMtActionlistService tMtActionlistService;
    @Resource
    private TMtActionlistMapper tMtActionlistMapper;
    @Autowired
    private ISysDictTypeService iSysDictTypeService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private SysPermissionService permissionService;
    @Autowired
    private TMtMeetingController tMtMeetingController;
    @Resource
    private TStaffmgrMapper tStaffmgrMapper;


    /**
     * 查询装置会议执行列表
     */
    @PreAuthorize("@ss.hasPermi('plant:meeting:list')")
    @GetMapping("/list")
    public TableDataInfo list(TMtActionlist tMtActionlist) {
        // 权限集合
        SysUser s = sysUserService.selectUserById(getUserId());
        Set<String> permissions = permissionService.getMenuPermission(s);
        if (!permissions.contains("plant:meeting:allList") && !s.isAdmin()) {
            tMtActionlist.setResponsible(s.getStaffId());
            tMtActionlist.setCreaterCode(getUserId() + "");
        }

        logger.info(JSON.toJSONString(tMtActionlist));
        startPage();
        List<TMtActionlist> list = tMtActionlistService.selectTMtActionlistList(tMtActionlist);
        List<SysDictData> dictList = iSysDictTypeService.selectDictDataByType("ACTUALPOST");

        for (TMtActionlist t : list
        ) {
            //指定人
            if ("1".equals(t.getIsPerson())) {
                if (StringUtils.isNotEmpty(t.getResponsible())) {
                    String[] respArr = t.getResponsible().split(",");
                    for (int i = 0; i < respArr.length; i++) {
                        SysUser user = sysUserService.selectUserByStaffId(respArr[i]);
                        if (user != null) {
                            if (i == 0) {
                                t.setRespName(user.getNickName());
                            } else {
                                t.setRespName(t.getRespName() + "," + 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);
    }

    //情况统计
    @GetMapping("/actionStatusData")
    public List<DataEntity> actionStatusData(@RequestParam Map param) {
        param.put("params", new HashMap<>());
        logger.info(JSON.toJSONString(param));
        List<DataEntity> list = tMtActionlistMapper.selectAcitonStatusData(param);
        List<SysDictData> dictList = iSysDictTypeService.selectDictDataByType("ACTION_STATUS");
        for (DataEntity d : list
        ) {
            for (SysDictData s : dictList
            ) {
                if (StringUtils.isBlank(d.getDataName())) {
                    d.setDataName("未知");
                    break;
                }
                if (s.getDictValue().equals(d.getDataName())) {
                    d.setDataName(s.getDictLabel());
                    break;
                }
            }
        }
        return list;
    }

    /**
     * 导出装置会议执行列表
     */
    @PreAuthorize("@ss.hasPermi('plant:meeting:export')")
    @Log(title = "装置会议执行", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TMtActionlist tMtActionlist) {
        List<TMtActionlist> list = tMtActionlistService.selectTMtActionlistList(tMtActionlist);
        ExcelUtil<TMtActionlist> util = new ExcelUtil<TMtActionlist>(TMtActionlist.class);
        return util.exportExcel(list, "actionlist");
    }

    /**
     * 获取装置会议执行详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(tMtActionlistService.selectTMtActionlistById(id));
    }

    /**
     * 新增装置会议执行
     */
    @Log(title = "装置会议执行", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping
    public AjaxResult add(@RequestBody TMtActionlist tMtActionlist) {
        tMtActionlist.setUpdatedate(new Date());
        tMtActionlist.setUpdaterCode(getUserId() + "");
        tMtActionlist.setCreaterCode(getUserId() + "");
        if (tMtActionlist.getIsHis() != null && tMtActionlist.getIsHis() == 1) {
            tMtMeetingController.getReport(null, tMtActionlist.getMeetingId());
        }

        //指定岗位
        if ("2".equals(tMtActionlist.getIsPerson())) {
            if (StringUtils.isNotEmpty(tMtActionlist.getPosition())) {
                TStaffmgr t = new TStaffmgr();
                t.setDeptId(103l);
                t.setActualposts(tMtActionlist.getPosition());
                List<TStaffmgr> staffmgrList = tStaffmgrMapper.selectTStaffmgrListByDeptId(t);
                String respStr = "";
                for (TStaffmgr s : staffmgrList
                     ) {
                    respStr = respStr + s.getStaffid() + ",";
                }
                tMtActionlist.setResponsible(respStr);
            }
        }

        return toAjax(tMtActionlistService.insertTMtActionlist(tMtActionlist));
    }

    /**
     * 修改装置会议执行
     */
    @Log(title = "装置会议执行", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping
    public AjaxResult edit(@RequestBody TMtActionlist tMtActionlist) {
        tMtActionlist.setUpdatedate(new Date());
        tMtActionlist.setUpdaterCode(getUserId() + "");
        if (tMtActionlist.getIsHis() != null && tMtActionlist.getIsHis() == 1) {
            tMtMeetingController.getReport(null, tMtActionlist.getMeetingId());
        }
        //指定岗位
        if ("2".equals(tMtActionlist.getIsPerson())) {
            if (StringUtils.isNotEmpty(tMtActionlist.getPosition())) {
                TStaffmgr t = new TStaffmgr();
                t.setDeptId(103l);
                t.setActualposts(tMtActionlist.getPosition());
                List<TStaffmgr> staffmgrList = tStaffmgrMapper.selectTStaffmgrListByDeptId(t);
                String respStr = "";
                for (TStaffmgr s : staffmgrList
                ) {
                    respStr = respStr + s.getStaffid() + ",";
                }
                tMtActionlist.setResponsible(respStr);
            }
        }
        return toAjax(tMtActionlistService.updateTMtActionlist(tMtActionlist));
    }

    /**
     * 删除装置会议执行
     */
    @Log(title = "装置会议执行", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tMtActionlistService.deleteTMtActionlistByIds(ids));
    }
}
