package com.ruoyi.web.controller.doubleDefense;

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

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.doubledefense.dto.DangerDTO;
import com.ruoyi.doubledefense.dto.res.DangerResultDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
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.doubledefense.domain.Danger;
import com.ruoyi.doubledefense.service.IDangerService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 隐患信息Controller
 *
 * @author ruoyi
 * @date 2023-08-03
 */
@Api(tags = "PC隐患信息")
@RestController
@RequestMapping("/doubledefense/danger")
public class DangerController extends BaseController {
    @Autowired
    private IDangerService dangerService;


    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<Danger> util = new ExcelUtil<>(Danger.class);
        util.importTemplateExcel(response, "隐患信息");
    }

    @Log(title = "PC隐患信息", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<Danger> util = new ExcelUtil<>(Danger.class);
        List<Danger> dataList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        for (int i = 0; i < dataList.size(); i++) {
            Danger data = dataList.get(i);
            System.out.println(i + "、" + JSON.toJSONString(data));
        }
        return success();
    }

    /**
     * 查询隐患信息列表
     * 注意：当整改期限有值时，PC首页查询逾期数量列表
     */
    @ApiOperation("PC首页统计-双重防御机制：查询隐患列表")
    @GetMapping("/pcHomePageDangerList")
    public TableDataInfo pcHomePageDangerList(DangerDTO danger) {
        startPage();
        //前端如果传了过期时间，将时间修改为当前时间
        if(danger.getDangerManageDeaddline()!=null){
            danger.setDangerManageDeaddline(new Date());
        }
        List<DangerResultDTO> list = dangerService.pcHomePageDangerList(danger);
        return getDataTable(list);
    }

    /**
     * 查询隐患信息列表
     * 注意：当整改期限有值时，PC首页查询逾期数量列表
     */
    @ApiOperation("查询隐患信息列表")
    //TODO 20230920PreAuthorize("@ss.hasPermi('doubledefense:danger:list')")
    @GetMapping("/list")
    public TableDataInfo list(DangerDTO danger) {
        startPage();
        List<DangerResultDTO> list = dangerService.selectDangerList(danger);
        return getDataTable(list);
    }

    /**
     * 导出隐患信息列表
     */
    @ApiOperation("导出隐患信息列表")
    //TODO 20230920PreAuthorize("@ss.hasPermi('doubledefense:danger:export')")
    @Log(title = "隐患信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DangerDTO danger) {
        List<DangerResultDTO> list = dangerService.selectDangerList(danger);
        ExcelUtil<DangerResultDTO> util = new ExcelUtil<DangerResultDTO>(DangerResultDTO.class);
        util.exportExcel(response, list, "隐患信息数据");
    }

    /**
     * 获取隐患信息详细信息
     */
    @ApiOperation("获取隐患信息详细信息")
    //TODO 20230920PreAuthorize("@ss.hasPermi('doubledefense:danger:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(dangerService.selectDangerById(id));
    }

    /**
     * 新增隐患信息
     */
    @ApiOperation("新增隐患信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceId", value = "设备UUID", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "checkRecordId", value = "排查记录ID", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "dangerName", value = "隐患名称", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "dangerDesc", value = "隐患描述", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "registTime", value = "登记时间", dataType = "Date", dataTypeClass = Date.class),
            @ApiImplicitParam(name = "dangerLevel", value = "隐患等级：0一般隐患，1重大隐患", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "dangerImage", value = "隐患图片，多张以逗号分割", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "dangerSrc", value = "隐患来源：1日常排查，2综合排查，3专业性排查，4季节性排查，5重点时段及节假日前排查，6事故类比排查，7复产复工前排查，8外聘专家诊断式排查，9管控措施失效，10其它", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "hazardDangerType", value = "隐患类型：1安全，2工艺，3电气，4仪表，5消防，6总图，7设备，8其它", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "dangerManageType", value = "治理类型：0即查即改，1限期整改", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "cost", value = "整改资金", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "controlMeasures", value = "控制措施", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "suggest", value = "整改建议", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "deptId", value = "整改部门ID", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "areaId", value = "所属区域ID", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "manageUserId", value = "整改负责人ID", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "dangerManageDeaddline", value = "整改治理期限", dataType = "Date", dataTypeClass = Date.class)
    })
    //TODO 20230920PreAuthorize("@ss.hasPermi('doubledefense:danger:add')")
    @Log(title = "隐患信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DangerDTO danger) {
        return toAjax(dangerService.insertDanger(danger));
    }

    /**
     * 修改隐患信息
     */
    @ApiOperation("修改隐患信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键UUID", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "deviceId", value = "设备UUID", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "checkRecordId", value = "排查记录ID", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "dangerName", value = "隐患名称", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "dangerDesc", value = "隐患描述", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "registTime", value = "登记时间", dataType = "Date", dataTypeClass = Date.class),
            @ApiImplicitParam(name = "dangerLevel", value = "隐患等级：0一般隐患，1重大隐患", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "dangerImage", value = "隐患图片，多张以逗号分割", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "dangerSrc", value = "隐患来源：1日常排查，2综合排查，3专业性排查，4季节性排查，5重点时段及节假日前排查，6事故类比排查，7复产复工前排查，8外聘专家诊断式排查，9管控措施失效，10其它", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "hazardDangerType", value = "隐患描述", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "dangerManageType", value = "治理类型：0即查即改，1限期整改", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "cost", value = "整改资金", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "controlMeasures", value = "控制措施", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "suggest", value = "整改建议", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "deptId", value = "整改部门ID", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "areaId", value = "所属区域ID", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "manageUserId", value = "整改负责人ID", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "dangerManageDeaddline", value = "整改治理期限", dataType = "Date", dataTypeClass = Date.class)
    })
    //TODO 20230920PreAuthorize("@ss.hasPermi('doubledefense:danger:edit')")
    @Log(title = "隐患信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DangerDTO danger) {
        return toAjax(dangerService.updateDanger(danger));
    }

    /**
     * 删除隐患信息
     */
    @ApiOperation("删除隐患信息")
    //TODO 20230920PreAuthorize("@ss.hasPermi('doubledefense:danger:remove')")
    @Log(title = "隐患信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(dangerService.deleteDangerByIds(ids));
    }
}
