package com.ruoyi.project.configuration.controller;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.configuration.domain.TMatter;
import com.ruoyi.project.configuration.domain.TMatterProtect;
import com.ruoyi.project.configuration.service.ITMatterFeatureService;
import com.ruoyi.project.configuration.service.ITMatterProtectService;
import com.ruoyi.project.configuration.service.ITMatterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 危害物质数据关联Controller
 *
 * @author ruoyi
 * @date 2021-12-28
 */
@RestController
@RequestMapping("/configuration/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('configuration:matter:list')")
    @GetMapping("/list")
    public TableDataInfo list(TMatter tMatter) {
        startPage();
        List<TMatter> list = tMatterService.selectTMatterList(tMatter);
        for (TMatter ml : list) {
            StringBuilder mpName = new StringBuilder();
            for (String mp : ml.getMatterProtects().split(",")) {
                TMatterProtect tMatterProtect = itMatterProtectService.selectTMatterProtectById(Long.valueOf(mp));
                mpName.append(tMatterProtect.getConservatoryMeasureName()).append("、");
            }
            if (StringUtils.isNotEmpty(mpName.toString())) {
                ml.setMatterProtectsName(mpName.substring(0, mpName.length() - 1));
            }
        };
        return getDataTable(list);
    }

    /**
     * 导出危害物质数据关联列表
     */
    @PreAuthorize("@ss.hasPermi('configuration: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('configuration:matter:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(tMatterService.selectTMatterById(id));
    }

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

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

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