package com.ruoyi.project.reliability.controller;

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

import com.ruoyi.project.common.domain.DataEntity;
import com.ruoyi.project.reliability.domain.TBamOriginal;
import com.ruoyi.project.reliability.mapper.TBamListMapper;
import com.ruoyi.project.reliability.service.ITBamOriginalService;
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.reliability.domain.TBamList;
import com.ruoyi.project.reliability.service.ITBamListService;
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-21
 */
@RestController
@RequestMapping("/reliability/list")
public class TBamListController extends BaseController
{
    @Autowired
    private ITBamListService tBamListService;

    @Autowired
    private ITBamOriginalService itBamOriginalService;
    @Autowired
    private ISysDictTypeService iSysDictTypeService;
    @Resource
    private TBamListMapper tBamListMapper;
    /**
     * 查询故障管理跟踪列表
     */
    @PreAuthorize("@ss.hasPermi('reliability:list:list')")
    @GetMapping("/list")
    public TableDataInfo list(TBamList tBamList)
    {
        startPage();
        List<TBamList> list = tBamListService.selectTBamListList(tBamList);
        return getDataTable(list);
    }

    //优先级统计
    @GetMapping("/priorityData")
    public List<DataEntity> priorityData(@RequestParam Map param)
    {
        param.put("params" , new HashMap<>());
        List<DataEntity> list = tBamListMapper.selectPriorityData(param);
        List<SysDictData> dictList = iSysDictTypeService.selectDictDataByType("PRIORITY");
        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;
    }
    //专业统计
    @GetMapping("/disciplineData")
    public List<DataEntity> disciplineData(@RequestParam Map param)
    {
        param.put("params" , new HashMap<>());
        List<DataEntity> list = tBamListMapper.selectDisciplineData(param);
        List<SysDictData> dictList = iSysDictTypeService.selectDictDataByType("DISCIPLINE");
        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('reliability:list:export')")
    @Log(title = "故障管理跟踪", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TBamList tBamList)
    {
        List<TBamList> list = tBamListService.selectTBamListList(tBamList);
        ExcelUtil<TBamList> util = new ExcelUtil<TBamList>(TBamList.class);
        return util.exportExcel(list, "list");
    }

    /**
     * 获取故障管理跟踪详细信息
     */
    @PreAuthorize("@ss.hasPermi('reliability:list:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tBamListService.selectTBamListById(id));
    }

    /**
     * 新增故障管理跟踪
     */
    @PreAuthorize("@ss.hasPermi('reliability:list:add')")
    @Log(title = "故障管理跟踪", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TBamList tBamList)
    {
        tBamList.setCreaterCode(getUserId().toString());
        return toAjax(tBamListService.insertTBamList(tBamList));
    }

    /**
     * 初选提交故障管理跟踪
     */
    @PreAuthorize("@ss.hasPermi('reliability:list:add')")
    @Log(title = "故障管理跟踪", businessType = BusinessType.INSERT)
    @GetMapping(value = "/insert/{id}")
    public  AjaxResult insert(@PathVariable("id") Long id)
    {
        TBamOriginal bamOriginal = itBamOriginalService.selectTBamOriginalById(id);
        bamOriginal.setOriginalId(id);
        bamOriginal.setId(null);
        bamOriginal.setCreaterCode(getUserId().toString());
        List<TBamList> tBamLists = tBamListService.selectTBamListList(new TBamList());
        for (TBamList t : tBamLists) {
            if (t.getOriginalId().equals(id) && t.getDelFlag() == 0) {
                return AjaxResult.error("此条初选数据已提交");
            }
        }
        return toAjax(tBamListService.insertTBamListFromOriginal(bamOriginal));
    }

    /**
     * 修改故障管理跟踪
     */
    @PreAuthorize("@ss.hasPermi('reliability:list:edit')")
    @Log(title = "故障管理跟踪", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TBamList tBamList)
    {
        tBamList.setUpdaterCode(getUserId().toString());
        tBamList.setUpdatedate(new Date());
        return toAjax(tBamListService.updateTBamList(tBamList));
    }

    /**
     * 删除故障管理跟踪
     */
    @PreAuthorize("@ss.hasPermi('reliability:list:remove')")
    @Log(title = "故障管理跟踪", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tBamListService.deleteTBamListByIds(ids));
    }
}
