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

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.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.pms.controller.admin.qhse.soc.vo.IotSocSourcePageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.qhse.soc.vo.IotSocSourceRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.qhse.soc.vo.IotSocSourceSaveReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.vo.IotProductClassifySimpleRespVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.qhse.soc.IotSocSourceDO;
import cn.iocoder.yudao.module.pms.service.qhse.soc.IotSocSourceService;
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 = "管理后台 - SOC卡分析数据源")
@RestController
@RequestMapping("/rq/iot-soc-source")
@Validated
public class IotSocSourceController {

    @Resource
    private IotSocSourceService iotSocSourceService;

    @PostMapping("/create")
    @Operation(summary = "创建SOC卡分析数据源")
    @PreAuthorize("@ss.hasPermission('rq:iot-soc-source:create')")
    public CommonResult<Long> createIotSocSource(@Valid @RequestBody IotSocSourceSaveReqVO createReqVO) {
        return success(iotSocSourceService.createIotSocSource(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新SOC卡分析数据源")
    @PreAuthorize("@ss.hasPermission('rq:iot-soc-source:update')")
    public CommonResult<Boolean> updateIotSocSource(@Valid @RequestBody IotSocSourceSaveReqVO updateReqVO) {
        iotSocSourceService.updateIotSocSource(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除SOC卡分析数据源")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('rq:iot-soc-source:delete')")
    public CommonResult<Boolean> deleteIotSocSource(@RequestParam("id") Long id) {
        iotSocSourceService.deleteIotSocSource(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得SOC卡分析数据源")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('rq:iot-soc-source:query')")
    public CommonResult<IotSocSourceRespVO> getIotSocSource(@RequestParam("id") Long id) {
        IotSocSourceDO iotSocSource = iotSocSourceService.getIotSocSource(id);
        return success(BeanUtils.toBean(iotSocSource, IotSocSourceRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得SOC卡分析数据源分页")
    @PreAuthorize("@ss.hasPermission('rq:iot-soc-source:query')")
    public CommonResult<List<IotSocSourceRespVO>> getIotSocSourcePage(@Valid IotSocSourcePageReqVO pageReqVO) {
        List<IotSocSourceDO> list = iotSocSourceService.getSocList(pageReqVO);
        return success(BeanUtils.toBean(list, IotSocSourceRespVO.class));
    }

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

    @GetMapping("/export-excel")
    @Operation(summary = "导出SOC卡分析数据源 Excel")
    @PreAuthorize("@ss.hasPermission('rq:iot-soc-source:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportIotSocSourceExcel(@Valid IotSocSourcePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IotSocSourceDO> list = iotSocSourceService.getIotSocSourcePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "SOC卡分析数据源.xls", "数据", IotSocSourceRespVO.class,
                        BeanUtils.toBean(list, IotSocSourceRespVO.class));
    }

}