package com.ruoyi.project.plant.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.ruoyi.project.common.domain.DataEntity;
import com.ruoyi.project.plant.mapper.TMtKeymaintenanceMapper;
import com.ruoyi.project.system.domain.SysDictData;
import com.ruoyi.project.system.service.ISysDictTypeService;
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;
    /**
     * 查询下周关键维修工作列表
     */
    @PreAuthorize("@ss.hasPermi('plant:keymaintenance:list')")
    @GetMapping("/list")
    public TableDataInfo list(TMtKeymaintenance tMtKeymaintenance)
    {
        startPage();
        List<TMtKeymaintenance> list = tMtKeymaintenanceService.selectTMtKeymaintenanceList(tMtKeymaintenance);
        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:keymaintenance: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");
    }

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

    /**
     * 新增下周关键维修工作
     */
    @PreAuthorize("@ss.hasPermi('plant:keymaintenance:add')")
    @Log(title = "下周关键维修工作", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TMtKeymaintenance tMtKeymaintenance)
    {
        return toAjax(tMtKeymaintenanceService.insertTMtKeymaintenance(tMtKeymaintenance));
    }

    /**
     * 修改下周关键维修工作
     */
    @PreAuthorize("@ss.hasPermi('plant:keymaintenance:edit')")
    @Log(title = "下周关键维修工作", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TMtKeymaintenance tMtKeymaintenance)
    {
        return toAjax(tMtKeymaintenanceService.updateTMtKeymaintenance(tMtKeymaintenance));
    }

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