package cn.iocoder.yudao.module.system.controller.admin.informationmapassociation;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
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.system.controller.admin.informationmapassociation.vo.InformationMapAssociationPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.informationmapassociation.vo.InformationMapAssociationRespVO;
import cn.iocoder.yudao.module.system.controller.admin.informationmapassociation.vo.InformationMapAssociationSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.informationmapassociation.InformationMapAssociationDO;
import cn.iocoder.yudao.module.system.service.informationmapassociation.InformationMapAssociationService;
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 = "管理后台 - 设备地图关联")
@RestController
@RequestMapping("/system/information-map-association")
@Validated
public class InformationMapAssociationController {

    @Resource
    private InformationMapAssociationService informationMapAssociationService;

    @PostMapping("/create")
    @Operation(summary = "创建设备地图关联")
    @PreAuthorize("@ss.hasPermission('system:information-map-association:create')")
    public CommonResult<Long> createInformationMapAssociation(@Valid @RequestBody InformationMapAssociationSaveReqVO createReqVO) {
        return success(informationMapAssociationService.createInformationMapAssociation(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新设备地图关联")
    @PreAuthorize("@ss.hasPermission('system:information-map-association:update')")
    public CommonResult<Boolean> updateInformationMapAssociation(@Valid @RequestBody InformationMapAssociationSaveReqVO updateReqVO) {
        informationMapAssociationService.updateInformationMapAssociation(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除设备地图关联")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:information-map-association:delete')")
    public CommonResult<Boolean> deleteInformationMapAssociation(@RequestParam("id") Long id) {
        informationMapAssociationService.deleteInformationMapAssociation(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得设备地图关联")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:information-map-association:query')")
    public CommonResult<InformationMapAssociationRespVO> getInformationMapAssociation(@RequestParam("id") Long id) {
        InformationMapAssociationDO informationMapAssociation = informationMapAssociationService.getInformationMapAssociation(id);
        return success(BeanUtils.toBean(informationMapAssociation, InformationMapAssociationRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得设备地图关联分页")
    @PreAuthorize("@ss.hasPermission('system:information-map-association:query')")
    public CommonResult<PageResult<InformationMapAssociationRespVO>> getInformationMapAssociationPage(@Valid InformationMapAssociationPageReqVO pageReqVO) {
        PageResult<InformationMapAssociationDO> pageResult = informationMapAssociationService.getInformationMapAssociationPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, InformationMapAssociationRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出设备地图关联 Excel")
    @PreAuthorize("@ss.hasPermission('system:information-map-association:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportInformationMapAssociationExcel(@Valid InformationMapAssociationPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<InformationMapAssociationDO> list = informationMapAssociationService.getInformationMapAssociationPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "设备地图关联.xls", "数据", InformationMapAssociationRespVO.class,
                        BeanUtils.toBean(list, InformationMapAssociationRespVO.class));
    }

}
