package com.ruoyi.project.reliability.controller;

import java.util.*;

import com.ruoyi.project.common.domain.DataEntity;
import com.ruoyi.project.reliability.domain.TBamList;
import com.ruoyi.project.reliability.mapper.TBamFinishMapper;
import com.ruoyi.project.reliability.service.ITBamListService;
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.TBamFinish;
import com.ruoyi.project.reliability.service.ITBamFinishService;
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/finish")
public class TBamFinishController extends BaseController
{
    @Autowired
    private ITBamFinishService tBamFinishService;

    @Autowired
    private ITBamListService tBamListService;
    @Autowired
    private ISysDictTypeService iSysDictTypeService;
    @Resource
    private TBamFinishMapper tBamFinishMapper;

    /**
     * 查询结束的故障管理跟踪列表
     */
    @PreAuthorize("@ss.hasPermi('reliability:finish:list')")
    @GetMapping("/list")
    public TableDataInfo list(TBamFinish tBamFinish)
    {
        startPage();
        List<TBamFinish> list = tBamFinishService.selectTBamFinishList(tBamFinish);
        return getDataTable(list);
    }

    //优先级统计
    @GetMapping("/priorityData")
    public List<DataEntity> priorityData(@RequestParam Map param)
    {
        param.put("params" , new HashMap<>());
        List<DataEntity> list = tBamFinishMapper.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 = tBamFinishMapper.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;
    }

    //阶段统计
    @GetMapping("/stepData")
    public List<DataEntity> stepData(@RequestParam Map param)
    {
        param.put("params" , new HashMap<>());
        DataEntity data1 = tBamFinishMapper.selectStepFinishData(param);
        data1.setDataName("结束");
        DataEntity data2 = tBamFinishMapper.selectStepListData(param);
        data2.setDataName("跟踪");
        DataEntity data3 = tBamFinishMapper.selectStepOriginalData(param);
        data3.setDataName("初选");
        List<DataEntity> list = new ArrayList<>();
        list.add(data1);
        list.add(data2);
        list.add(data3);
        return list;
    }

    /**
     * 导出结束的故障管理跟踪列表
     */
    @PreAuthorize("@ss.hasPermi('reliability:finish:export')")
    @Log(title = "结束的故障管理跟踪", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TBamFinish tBamFinish)
    {
        List<TBamFinish> list = tBamFinishService.selectTBamFinishList(tBamFinish);
        ExcelUtil<TBamFinish> util = new ExcelUtil<TBamFinish>(TBamFinish.class);
        return util.exportExcel(list, "finish");
    }

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

    /**
     * 新增结束的故障管理跟踪
     */
    @PreAuthorize("@ss.hasPermi('reliability:finish:add')")
    @Log(title = "结束的故障管理跟踪", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TBamFinish tBamFinish)
    {
        tBamFinish.setCreaterCode(getUserId().toString());
        return toAjax(tBamFinishService.insertTBamFinish(tBamFinish));
    }

    /**
     * 跟踪提交故障管理跟踪
     */
    @PreAuthorize("@ss.hasPermi('reliability:finish:add')")
    @Log(title = "结束的故障管理跟踪", businessType = BusinessType.INSERT)
    @GetMapping(value = "/insert/{id}")
    public  AjaxResult insert(@PathVariable("id") Long id)
    {
        TBamList bamList = tBamListService.selectTBamListById(id);
        bamList.setBamId(id);
        bamList.setId(null);
        bamList.setCreaterCode(getUserId().toString());
        List<TBamFinish> tBamFinishes = tBamFinishService.selectTBamFinishList((new TBamFinish()));
        for (TBamFinish t : tBamFinishes) {
            if (t.getBamId().equals(id) && t.getDelFlag() == 0) {
                return AjaxResult.error("此条跟踪数据已提交");
            }
        }
        return toAjax(tBamFinishService.insertTBamFinishFromList(bamList));
    }

    /**
     * 修改结束的故障管理跟踪
     */
    @PreAuthorize("@ss.hasPermi('reliability:finish:edit')")
    @Log(title = "结束的故障管理跟踪", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TBamFinish tBamFinish)
    {
        tBamFinish.setUpdaterCode(getUserId().toString());
        tBamFinish.setUpdatedate(new Date());
        return toAjax(tBamFinishService.updateTBamFinish(tBamFinish));
    }

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