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.TMtActionlist;
import com.ruoyi.project.plant.domain.TStaffmgr;
import com.ruoyi.project.plant.mapper.TMtKeymaintenanceMapper;
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.TMtKeymaintenance;
import com.ruoyi.project.plant.service.ITMtKeymaintenanceService;
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/keymaintenance")
public class TMtKeymaintenanceController extends BaseController
{
    @Autowired
    private ITMtKeymaintenanceService tMtKeymaintenanceService;
    @Resource
    private TMtKeymaintenanceMapper tMtKeymaintenanceMapper;
    @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(TMtKeymaintenance tMtKeymaintenance)
    {
        // 权限集合
        SysUser s = sysUserService.selectUserById(getUserId());
        Set<String> permissions = permissionService.getMenuPermission(s);
        if (!permissions.contains("plant:meeting:allList") && !s.isAdmin()) {
            tMtKeymaintenance.setResponsible(s.getStaffId());
            tMtKeymaintenance.setCreaterCode(getUserId() + "");
        }
        startPage();
        List<TMtKeymaintenance> list = tMtKeymaintenanceService.selectTMtKeymaintenanceList(tMtKeymaintenance);
        List<SysDictData> dictList = iSysDictTypeService.selectDictDataByType("ACTUALPOST");

        for (TMtKeymaintenance 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);
    }

    //情况统计
    @GetMapping("/keyStatusData")
    public List<DataEntity> keyStatusData(@RequestParam Map param)
    {
        param.put("params" , new HashMap<>());
        logger.info(JSON.toJSONString(param));
        List<DataEntity> list = tMtKeymaintenanceMapper.selectKeyStatusData(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(TMtKeymaintenance tMtKeymaintenance)
    {
        List<TMtKeymaintenance> list = tMtKeymaintenanceService.selectTMtKeymaintenanceList(tMtKeymaintenance);
        ExcelUtil<TMtKeymaintenance> util = new ExcelUtil<TMtKeymaintenance>(TMtKeymaintenance.class);
        return util.exportExcel(list, "keymaintenance");
    }

    /**
     * 获取下周关键维修工作详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tMtKeymaintenanceService.selectTMtKeymaintenanceById(id));
    }

    /**
     * 新增下周关键维修工作
     */
    @Log(title = "下周关键维修工作", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping
    public AjaxResult add(@RequestBody TMtKeymaintenance tMtKeymaintenance)
    {
        tMtKeymaintenance.setUpdatedate(new Date());
        tMtKeymaintenance.setUpdaterCode(getUserId() + "");
        tMtKeymaintenance.setCreaterCode(getUserId() + "");
        if (tMtKeymaintenance.getIsHis() != null && tMtKeymaintenance.getIsHis() == 1) {
            tMtMeetingController.getReport(null,tMtKeymaintenance.getMeetingId());
        }
        //指定岗位
        if ("2".equals(tMtKeymaintenance.getIsPerson())) {
            if (StringUtils.isNotEmpty(tMtKeymaintenance.getPosition())) {
                TStaffmgr t = new TStaffmgr();
                t.setDeptId(103l);
                t.setActualposts(tMtKeymaintenance.getPosition());
                List<TStaffmgr> staffmgrList = tStaffmgrMapper.selectTStaffmgrListByDeptId(t);
                String respStr = "";
                for (TStaffmgr s : staffmgrList
                ) {
                    respStr = respStr + s.getStaffid() + ",";
                }
                tMtKeymaintenance.setResponsible(respStr);
            }
        }
        return toAjax(tMtKeymaintenanceService.insertTMtKeymaintenance(tMtKeymaintenance));
    }

    /**
     * 修改下周关键维修工作
     */
    @Log(title = "下周关键维修工作", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping
    public AjaxResult edit(@RequestBody TMtKeymaintenance tMtKeymaintenance)
    {
        tMtKeymaintenance.setUpdatedate(new Date());
        tMtKeymaintenance.setUpdaterCode(getUserId() + "");
        if (tMtKeymaintenance.getIsHis() != null && tMtKeymaintenance.getIsHis() == 1) {
            tMtMeetingController.getReport(null,tMtKeymaintenance.getMeetingId());
        }
        //指定岗位
        if ("2".equals(tMtKeymaintenance.getIsPerson())) {
            if (StringUtils.isNotEmpty(tMtKeymaintenance.getPosition())) {
                TStaffmgr t = new TStaffmgr();
                t.setDeptId(103l);
                t.setActualposts(tMtKeymaintenance.getPosition());
                List<TStaffmgr> staffmgrList = tStaffmgrMapper.selectTStaffmgrListByDeptId(t);
                String respStr = "";
                for (TStaffmgr s : staffmgrList
                ) {
                    respStr = respStr + s.getStaffid() + ",";
                }
                tMtKeymaintenance.setResponsible(respStr);
            }
        }
        return toAjax(tMtKeymaintenanceService.updateTMtKeymaintenance(tMtKeymaintenance));
    }

    /**
     * 删除下周关键维修工作
     */
    @Log(title = "下周关键维修工作", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tMtKeymaintenanceService.deleteTMtKeymaintenanceByIds(ids));
    }
}
