package cn.iocoder.yudao.module.pms.controller.admin.depttype;

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.pms.controller.admin.depttype.vo.IotDeptTypePageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.depttype.vo.IotDeptTypeRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.depttype.vo.IotDeptTypeSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.depttype.IotDeptTypeDO;
import cn.iocoder.yudao.module.pms.service.depttype.IotDeptTypeService;
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("/pms/iot-dept-type")
@Validated
public class IotDeptTypeController {

    @Resource
    private IotDeptTypeService iotDeptTypeService;

    @PostMapping("/create")
    @Operation(summary = "创建部门类型")
    @PreAuthorize("@ss.hasPermission('pms:iot-dept-type:create')")
    public CommonResult<Long> createIotDeptType(@Valid @RequestBody IotDeptTypeSaveReqVO createReqVO) {
        return success(iotDeptTypeService.createIotDeptType(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新部门类型")
    @PreAuthorize("@ss.hasPermission('pms:iot-dept-type:update')")
    public CommonResult<Boolean> updateIotDeptType(@Valid @RequestBody IotDeptTypeSaveReqVO updateReqVO) {
        iotDeptTypeService.updateIotDeptType(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除部门类型")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('pms:iot-dept-type:delete')")
    public CommonResult<Boolean> deleteIotDeptType(@RequestParam("id") Long id) {
        iotDeptTypeService.deleteIotDeptType(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得部门类型")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('pms:iot-dept-type:query')")
    public CommonResult<IotDeptTypeRespVO> getIotDeptType(@RequestParam("id") Long id) {
        IotDeptTypeDO iotDeptType = iotDeptTypeService.getIotDeptType(id);
        return success(BeanUtils.toBean(iotDeptType, IotDeptTypeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得部门类型分页")
    @PreAuthorize("@ss.hasPermission('pms:iot-dept-type:query')")
    public CommonResult<PageResult<IotDeptTypeRespVO>> getIotDeptTypePage(@Valid IotDeptTypePageReqVO pageReqVO) {
        PageResult<IotDeptTypeDO> pageResult = iotDeptTypeService.getIotDeptTypePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, IotDeptTypeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出部门类型 Excel")
    @PreAuthorize("@ss.hasPermission('pms:iot-dept-type:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportIotDeptTypeExcel(@Valid IotDeptTypePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IotDeptTypeDO> list = iotDeptTypeService.getIotDeptTypePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "部门类型.xls", "数据", IotDeptTypeRespVO.class,
                        BeanUtils.toBean(list, IotDeptTypeRespVO.class));
    }

}