package com.ruoyi.web.controller.doubleDefense;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.doubledefense.dto.HazardDTO;
import com.ruoyi.doubledefense.dto.res.HazardResultDTO;
import com.ruoyi.doubledefense.service.IHazardService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

/**
 * 风险分析对象Controller
 *
 * @author ruoyi
 * @date 2023-08-03
 */
@Api(tags = "风险分析对象")
@RestController
@RequestMapping("/doubledefense/hazard")
public class HazardController extends BaseController {
    @Autowired
    private IHazardService hazardService;

    /**
     * 查询风险分析对象列表
     */
    @ApiOperation("查询风险分析对象列表")
    //TODO 20230920PreAuthorize("@ss.hasPermi('doubledefense:hazard:list')")
    @GetMapping("/list")
    public TableDataInfo list(HazardDTO hazard) {
        startPage();
        List<HazardResultDTO> list = hazardService.selectHazardList(hazard);
        return getDataTable(list);
    }

    /**
     * 导出风险分析对象列表
     */
    @ApiOperation("导出风险分析对象列表")
    //TODO 20230920PreAuthorize("@ss.hasPermi('doubledefense:hazard:export')")
    @Log(title = "风险分析对象", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HazardDTO hazard) {
        List<HazardResultDTO> list = hazardService.selectHazardList(hazard);
        ExcelUtil<HazardResultDTO> util = new ExcelUtil<HazardResultDTO>(HazardResultDTO.class);
        util.exportExcel(response, list, "风险分析对象数据");
    }

    /**
     * 获取风险分析对象详细信息
     */
    @ApiOperation("获取风险分析对象详细信息")
    @ApiImplicitParam(name = "id", value = "分析对象ID", required = true, dataType = "String", paramType = "path", dataTypeClass = String.class)
    //TODO 20230920PreAuthorize("@ss.hasPermi('doubledefense:hazard:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {

        return success(hazardService.selectHazardById(id));
    }

    /**
     * 新增风险分析对象
     */
    @ApiOperation("新增风险分析对象")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "hazardName", value = "风险分析对象名称", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "hazardLevel", value = "风险等级：1低,2一般,3较大,4重大", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "areaId", value = "所属区域ID", dataType = "Long", dataTypeClass = Long.class)
    })
    //TODO 20230920PreAuthorize("@ss.hasPermi('doubledefense:hazard:add')")
    @Log(title = "风险分析对象", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HazardDTO hazard) {
        SysUser sysUser = getLoginUser().getUser();
        hazard.setCreateUserId(sysUser.getUserId());
        hazard.setUpdateUserId(sysUser.getUserId());
        hazard.setCompanyCode(sysUser.getCompanyCode());
//        if (StringUtils.isBlank(hazard.getDeviceId())) {
//            hazard.setDeviceId("4039d157-f56a-438e-a48c-3230155702f4");
//        }
        return toAjax(hazardService.insertHazard(hazard));
    }

    /**
     * 修改风险分析对象
     */
    @ApiOperation("修改风险分析对象")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "风险分析对象ID", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "hazardName", value = "风险分析对象名称", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "level", value = "风险等级：1低,2一般,3较大,4重大", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "areaId", value = "所属区域ID", dataType = "Long", dataTypeClass = Long.class)
    })
    //TODO 20230920PreAuthorize("@ss.hasPermi('doubledefense:hazard:edit')")
    @Log(title = "风险分析对象", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HazardDTO hazard) {
        SysUser sysUser = getLoginUser().getUser();
        hazard.setUpdateUserId(sysUser.getUserId());
//        if (StringUtils.isBlank(hazard.getDeviceId())) {
//            hazard.setDeviceId("4039d157-f56a-438e-a48c-3230155702f4");
//        }
        return toAjax(hazardService.updateHazard(hazard));
    }

    /**
     * 删除风险分析对象
     */
    @ApiOperation("删除风险分析对象")
    @ApiImplicitParam(name = "ids", value = "分析对象ID集合", required = true, dataType = "List", paramType = "path", dataTypeClass = List.class)
    //TODO 20230920PreAuthorize("@ss.hasPermi('doubledefense:hazard:remove')")
    @Log(title = "风险分析对象", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(hazardService.deleteHazardByIds(ids));
    }
}
