package com.ruoyi.project.check.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.check.domain.TCheckLawitems;
import com.ruoyi.project.check.service.ITCheckLawitemsService;
import com.ruoyi.system.service.ISysDictTypeService;
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.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.project.check.domain.TCheckLaws;
import com.ruoyi.project.check.service.ITCheckLawsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 法规Controller
 *
 * @author ruoyi
 * @date 2022-11-28
 */
@RestController
@RequestMapping("/check/laws")
public class TCheckLawsController extends BaseController {
    @Autowired
    private ITCheckLawsService tCheckLawsService;

    @Autowired
    private ISysDictTypeService isysDictTypeService;

    @Autowired
    private ITCheckLawitemsService tCheckLawitemsService;

    /**
     * 查询法规列表
     */
    @PreAuthorize("@ss.hasPermi('check:laws:list')")
    @GetMapping("/list")
    public TableDataInfo list(TCheckLaws tCheckLaws) {
        startPage();
        List<TCheckLaws> list = tCheckLawsService.selectTCheckLawsList(tCheckLaws);
        return getDataTable(list);
    }

    /**
     * 导出法规列表
     */
    @PreAuthorize("@ss.hasPermi('check:laws:export')")
    @Log(title = "法规导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TCheckLaws tCheckLaws) {
        List<TCheckLaws> list = tCheckLawsService.selectTCheckLawsList(tCheckLaws);
        ExcelUtil<TCheckLaws> util = new ExcelUtil<TCheckLaws>(TCheckLaws.class);
        util.exportExcel(response, list, "法规数据");
    }

    /**
     * 获取法规详细信息
     */
    @PreAuthorize("@ss.hasPermi('check:laws:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(tCheckLawsService.selectTCheckLawsById(id));
    }

    /**
     * 新增法规
     */
    @PreAuthorize("@ss.hasPermi('check:laws:add')")
    @Log(title = "法规新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TCheckLaws tCheckLaws) {
        tCheckLaws.setCreaterCode(getUserId());
        tCheckLaws.setUpdatedate(new Date());
        tCheckLaws.setUpdaterCode(getUserId());
        tCheckLawsService.insertTCheckLaws(tCheckLaws); // 新增法规表
        // 开始创建法规项内容
        List<SysDictData> pointTypes = isysDictTypeService.selectDictDataByType("point_type"); // 密封点类型
        List<SysDictData> plantTypes = isysDictTypeService.selectDictDataByType("plant_type"); // 装置类型
        List<SysDictData> mediumTypes = isysDictTypeService.selectDictDataByType("medium_type"); // 介质状态
        List<TCheckLawitems> tCheckLawitems = new ArrayList<>();
        for (SysDictData plantType : plantTypes) { // 遍历装置
            for (SysDictData pointType : pointTypes) { // 遍历密封点
                for (SysDictData mediumType : mediumTypes) { // 遍历介质状态
                    // 根据装置类型、密封点类型、介质状态生成法规项
                    TCheckLawitems tCheckLawitem = new TCheckLawitems();
                    tCheckLawitem.setLawId(tCheckLaws.getId());
                    tCheckLawitem.setPlantType(plantType.getDictValue());
                    tCheckLawitem.setPointType(pointType.getDictValue());
                    tCheckLawitem.setMediumType(mediumType.getDictValue());
                    // TODO 频率、泄露标准、维修天数需确认
                    tCheckLawitem.setDetectionFrequency("3");
                    tCheckLawitem.setGeneral("50");
                    tCheckLawitem.setSerious("200");
                    tCheckLawitem.setStratFix("15");
                    tCheckLawitem.setEndFix("15");
                    tCheckLawitem.setCreaterCode(getUserId());
                    tCheckLawitem.setCreatedate(new Date());
                    tCheckLawitem.setUpdatedate(new Date());
                    tCheckLawitem.setUpdaterCode(getUserId());
                    tCheckLawitems.add(tCheckLawitem);
                }
            }
        }
        // 新增法规项表
        return toAjax(tCheckLawitemsService.insertTCheckLawitemsByList(tCheckLawitems));

    }

    /**
     * 修改法规
     */
    @PreAuthorize("@ss.hasPermi('check:laws:edit')")
    @Log(title = "法规修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TCheckLaws tCheckLaws) {
        tCheckLaws.setUpdatedate(new Date());
        tCheckLaws.setUpdaterCode(getUserId());
        if (StringUtils.isNotEmpty(tCheckLaws.getStatus())) {
            tCheckLaws.setStarttime(new Date());
        }
        return toAjax(tCheckLawsService.updateTCheckLaws(tCheckLaws));
    }

    @PreAuthorize("@ss.hasPermi('check:laws:edit')")
    @Log(title = "法规匹配", businessType = BusinessType.UPDATE)
    @PutMapping("/matchLaws")
    public AjaxResult matchLaws(@RequestBody TCheckLaws tCheckLaws) {
        tCheckLaws.setStarttime(new Date());
        return toAjax(tCheckLawsService.updateTCheckLawsStatus(tCheckLaws));
    }

    /**
     * 删除法规
     */
    @PreAuthorize("@ss.hasPermi('check:laws:remove')")
    @Log(title = "法规删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tCheckLawsService.deleteTCheckLawsByIds(ids));
    }
}
