package com.ruoyi.project.plant.controller;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.system.domain.SysDictData;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.system.mapper.SysUserMapper;
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.TMtdConfig;
import com.ruoyi.project.plant.service.ITMtdConfigService;
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 ssy
 * @date 2023-11-16
 */
@RestController
@RequestMapping("/plant/dailyMeetingConfig")
public class TMtdConfigController extends BaseController
{
    @Autowired
    private ITMtdConfigService tMtdConfigService;
    @Resource
    private SysUserMapper userMapper;

    /**
     * 查询调度会议配置列表
     */
    @GetMapping("/list")
    public TableDataInfo list(TMtdConfig tMtdConfig)
    {
        startPage();
        List<TMtdConfig> list = tMtdConfigService.selectTMtdConfigList(tMtdConfig);
        for (TMtdConfig t :list
             ) {
            if (t.getConfigType() == 1 && StringUtils.isNotEmpty(t.getConfigValue())) {
                String[] arr = t.getConfigValue().split(",");
                t.setConfigValue("");
                for (int i = 0; i < arr.length; i++) {
                    SysUser s = userMapper.selectUserByStaffId(arr[i]);
                    t.setConfigValue(t.getConfigValue() + s.getNickName() + "_" + s.getDept().getDeptName() + ",");
                }
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出调度会议配置列表
     */
    @Log(title = "调度会议配置", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TMtdConfig tMtdConfig)
    {
        List<TMtdConfig> list = tMtdConfigService.selectTMtdConfigList(tMtdConfig);
        ExcelUtil<TMtdConfig> util = new ExcelUtil<TMtdConfig>(TMtdConfig.class);
        return util.exportExcel(list, "dailyMeetingConfig");
    }

    /**
     * 获取调度会议配置详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        TMtdConfig tMtdConfig = tMtdConfigService.selectTMtdConfigById(id);
        if (tMtdConfig.getConfigType() == 0) {
            String[] arr = tMtdConfig.getConfigValue().split(",");
            List<SysDictData> dictList = new ArrayList<>();
            for (int i = 0; i < arr.length; i++) {
                SysDictData dict = new SysDictData();
                dict.setDictLabel(arr[i]);
                dict.setDictValue(arr[i]);
                dict.setDictType(tMtdConfig.getConfigKey());
                dictList.add(dict);
            }
            tMtdConfig.setDictList(dictList);
        }
        if (tMtdConfig.getConfigType() == 1) {
            if (tMtdConfig.getConfigValue() != null) {
                String[] arr = tMtdConfig.getConfigValue().split(",");
                List<SysDictData> dictList = new ArrayList<>();
                for (int i = 0; i < arr.length; i++) {
                    SysUser sysUser =  userMapper.selectUserByStaffId(arr[i]);
                    SysDictData dict = new SysDictData();
                    dict.setDictLabel(sysUser.getNickName());
                    dict.setDictValue(sysUser.getStaffId());
                    dict.setDictType(tMtdConfig.getConfigKey());
                    dictList.add(dict);
                }
                tMtdConfig.setDictList(dictList);
            }
        }
        return AjaxResult.success(tMtdConfig);
    }

    /**
     * 新增调度会议配置
     */
    @Log(title = "调度会议配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TMtdConfig tMtdConfig)
    {
        return toAjax(tMtdConfigService.insertTMtdConfig(tMtdConfig));
    }

    /**
     * 修改调度会议配置
     */
    @Log(title = "调度会议配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TMtdConfig tMtdConfig)
    {
        return toAjax(tMtdConfigService.updateTMtdConfig(tMtdConfig));
    }

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