package cn.iocoder.yudao.module.iot.controller.admin.iotorganization;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.iot.controller.admin.iotorganization.vo.IotOrganizationListReqVO;
import cn.iocoder.yudao.module.iot.controller.admin.iotorganization.vo.IotOrganizationRespVO;
import cn.iocoder.yudao.module.iot.controller.admin.iotorganization.vo.IotOrganizationSaveReqVO;
import cn.iocoder.yudao.module.iot.dal.dataobject.iotorganization.IotOrganizationDO;
import cn.iocoder.yudao.module.iot.service.iotorganization.IotOrganizationService;
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 = "管理后台 - IOT组织")
@RestController
@RequestMapping("/iot/organization")
@Validated
public class IotOrganizationController {

    @Resource
    private IotOrganizationService organizationService;

    @PostMapping("/create")
    @Operation(summary = "创建IOT组织")
    @PreAuthorize("@ss.hasPermission('iot:organization:create')")
    public CommonResult<Long> createOrganization(@Valid @RequestBody IotOrganizationSaveReqVO createReqVO) {
        return success(organizationService.createOrganization(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新IOT组织")
    @PreAuthorize("@ss.hasPermission('iot:organization:update')")
    public CommonResult<Boolean> updateOrganization(@Valid @RequestBody IotOrganizationSaveReqVO updateReqVO) {
        organizationService.updateOrganization(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除IOT组织")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('iot:organization:delete')")
    public CommonResult<Boolean> deleteOrganization(@RequestParam("id") Long id) {
        organizationService.deleteOrganization(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得IOT组织")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('iot:organization:query')")
    public CommonResult<IotOrganizationRespVO> getOrganization(@RequestParam("id") Long id) {
        IotOrganizationDO organization = organizationService.getOrganization(id);
        return success(BeanUtils.toBean(organization, IotOrganizationRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得IOT组织列表")
    @PreAuthorize("@ss.hasPermission('iot:organization:query')")
    public CommonResult<List<IotOrganizationRespVO>> getOrganizationList(@Valid IotOrganizationListReqVO listReqVO) {
        List<IotOrganizationDO> list = organizationService.getOrganizationList(listReqVO);
        return success(BeanUtils.toBean(list, IotOrganizationRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出IOT组织 Excel")
    @PreAuthorize("@ss.hasPermission('iot:organization:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportOrganizationExcel(@Valid IotOrganizationListReqVO listReqVO,
                                        HttpServletResponse response) throws IOException {
        List<IotOrganizationDO> list = organizationService.getOrganizationList(listReqVO);
        // 导出 Excel
        ExcelUtils.write(response, "IOT组织.xls", "数据", IotOrganizationRespVO.class,
                BeanUtils.toBean(list, IotOrganizationRespVO.class));
    }

}