package cn.iocoder.yudao.module.pms.controller.admin.qhse.hazard;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.pms.controller.admin.qhse.hazard.vo.IotHazardTypePageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.qhse.hazard.vo.IotHazardTypeRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.qhse.hazard.vo.IotHazardTypeSaveReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.vo.IotProductClassifySimpleRespVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.qhse.hazard.IotHazardTypeDO;
import cn.iocoder.yudao.module.pms.service.qhse.hazard.IotHazardTypeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


@Tag(name = "管理后台 - QHSE-隐患排查分类")
@RestController
@RequestMapping("/rq/iot-hazard-type")
@Validated
public class IotHazardTypeController {

    @Resource
    private IotHazardTypeService iotHazardTypeService;

    @PostMapping("/create")
    @Operation(summary = "创建QHSE-隐患排查分类")
    @PreAuthorize("@ss.hasPermission('rq:iot-hazard-type:create')")
    public CommonResult<Long> createIotHazardType(@Valid @RequestBody IotHazardTypeSaveReqVO createReqVO) {
        return success(iotHazardTypeService.createIotHazardType(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新QHSE-隐患排查分类")
    @PreAuthorize("@ss.hasPermission('rq:iot-hazard-type:update')")
    public CommonResult<Boolean> updateIotHazardType(@Valid @RequestBody IotHazardTypeSaveReqVO updateReqVO) {
        iotHazardTypeService.updateIotHazardType(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除QHSE-隐患排查分类")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('rq:iot-hazard-type:delete')")
    public CommonResult<Boolean> deleteIotHazardType(@RequestParam("id") Long id) {
        iotHazardTypeService.deleteIotHazardType(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得QHSE-隐患排查分类")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('rq:iot-hazard-type:query')")
    public CommonResult<IotHazardTypeRespVO> getIotHazardType(@RequestParam("id") Long id) {
        IotHazardTypeDO iotHazardType = iotHazardTypeService.getIotHazardType(id);
        return success(BeanUtils.toBean(iotHazardType, IotHazardTypeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得QHSE-隐患排查分类分页")
    @PreAuthorize("@ss.hasPermission('rq:iot-hazard-type:query')")
    public CommonResult<PageResult<IotHazardTypeRespVO>> getIotHazardTypePage(@Valid IotHazardTypePageReqVO pageReqVO) {
        PageResult<IotHazardTypeDO> pageResult = iotHazardTypeService.getIotHazardTypePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, IotHazardTypeRespVO.class));
    }
    @GetMapping("/list")
    @Operation(summary = "获取产品分类列表")
    @PreAuthorize("@ss.hasPermission('rq:iot-hazard-type:query')")
    public CommonResult<List<IotHazardTypeRespVO>> getHazardTypeList(IotHazardTypePageReqVO reqVO) {
        List<IotHazardTypeDO> list = iotHazardTypeService.getHazardTypeList(reqVO);
        return success(BeanUtils.toBean(list, IotHazardTypeRespVO.class));
    }
    @GetMapping("/export-excel")
    @Operation(summary = "导出QHSE-隐患排查分类 Excel")
    @PreAuthorize("@ss.hasPermission('rq:iot-hazard-type:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportIotHazardTypeExcel(@Valid IotHazardTypePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IotHazardTypeDO> list = iotHazardTypeService.getIotHazardTypePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "QHSE-隐患排查分类.xls", "数据", IotHazardTypeRespVO.class,
                        BeanUtils.toBean(list, IotHazardTypeRespVO.class));
    }

    @GetMapping(value = {"/list-all-simple", "/simple-list"})
    @Operation(summary = "获取SOC数据源精简信息列表")
    public CommonResult<List<IotProductClassifySimpleRespVO>> getSimpleIotProductClassifyList() {
        List<IotHazardTypeDO> list = iotHazardTypeService.getHazardTypeList(
                new IotHazardTypePageReqVO().setStatus(CommonStatusEnum.ENABLE.getStatus()));
        return success(BeanUtils.toBean(list, IotProductClassifySimpleRespVO.class));
    }
}