package com.ruoyi.project.ehs.controller;

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

import com.ruoyi.project.ehs.domain.TMatterFeature;
import com.ruoyi.project.ehs.domain.TMatterProtect;
import com.ruoyi.project.ehs.service.ITMatterFeatureService;
import com.ruoyi.project.ehs.service.ITMatterProtectService;
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.ehs.domain.TMatter;
import com.ruoyi.project.ehs.service.ITMatterService;
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;

/**
 * 工作票-危害物质Controller
 *
 * @author ruoyi
 * @date 2021-09-22
 */
@RestController
@RequestMapping("/ehs/matter")
public class TMatterController extends BaseController {
    @Autowired
    private ITMatterService tMatterService;

    @Autowired
    private ITMatterFeatureService itMatterFeatureService;

    @Autowired
    private ITMatterProtectService itMatterProtectService;

    /**
     * 获取所有的危害物料
     *
     * @return
     */
    @GetMapping("/listMatters")
    public AjaxResult list() {
        List<TMatter> tMatters = this.tMatterService.selectTMatterList(new TMatter());
        for (TMatter tMatter : tMatters) {
            //获取危害分类
            String matterFeatures = tMatter.getMatterFeatures();
            String[] split = matterFeatures.split(",");
            List<String> matterFeatureList = this.itMatterFeatureService.queryByIds(split);
            tMatter.setMatterFeatureList(matterFeatureList);
            //获取危害防护
            String matterProtects = tMatter.getMatterProtects();
            String[] protectIds = matterProtects.split(",");
            List<TMatterProtect> tMatterProtects = this.itMatterProtectService.queryByIds(protectIds);
            tMatter.setProtects(tMatterProtects);
        }
        return AjaxResult.success(tMatters);
    }

    /**
     * 查询工作票-危害物质列表
     */
    @PreAuthorize("@ss.hasPermi('ehs:matter:list')")
    @GetMapping("/list")
    public TableDataInfo list(TMatter tMatter) {
        startPage();
        List<TMatter> list = tMatterService.selectTMatterList(tMatter);
        return getDataTable(list);
    }

    /**
     * 导出工作票-危害物质列表
     */
    @PreAuthorize("@ss.hasPermi('ehs:matter:export')")
    @Log(title = "工作票-危害物质", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TMatter tMatter) {
        List<TMatter> list = tMatterService.selectTMatterList(tMatter);
        ExcelUtil<TMatter> util = new ExcelUtil<TMatter>(TMatter.class);
        return util.exportExcel(list, "matter");
    }

    /**
     * 获取工作票-危害物质详细信息
     */
    @PreAuthorize("@ss.hasPermi('ehs:matter:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(tMatterService.selectTMatterById(id));
    }

    /**
     * 新增工作票-危害物质
     */
    @PreAuthorize("@ss.hasPermi('ehs:matter:add')")
    @Log(title = "工作票-危害物质", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TMatter tMatter) {
        return toAjax(tMatterService.insertTMatter(tMatter));
    }

    /**
     * 修改工作票-危害物质
     */
    @PreAuthorize("@ss.hasPermi('ehs:matter:edit')")
    @Log(title = "工作票-危害物质", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TMatter tMatter) {
        return toAjax(tMatterService.updateTMatter(tMatter));
    }

    /**
     * 删除工作票-危害物质
     */
    @PreAuthorize("@ss.hasPermi('ehs:matter:remove')")
    @Log(title = "工作票-危害物质", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tMatterService.deleteTMatterByIds(ids));
    }
}
