package cn.iocoder.yudao.module.pms.controller.admin.yanfan.category;

import cn.iocoder.yudao.module.pms.controller.admin.yanfan.category.vo.IotYfCategoryPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.yanfan.category.vo.IotYfCategoryRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.yanfan.category.vo.IotYfCategorySaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.yanfan.category.IotYfCategoryDO;
import cn.iocoder.yudao.module.pms.service.yanfan.category.IotCategoryService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;


@Tag(name = "管理后台 - yf产品分类")
@RestController
@RequestMapping("/rq/iot-category")
@Validated
public class IotYfCategoryController {

    @Resource
    private IotCategoryService iotCategoryService;

    @PostMapping
    @Operation(summary = "创建yf产品分类")
    @PreAuthorize("@ss.hasPermission('rq:iot-category:create')")
    public CommonResult<Long> createIotCategory(@Valid @RequestBody IotYfCategorySaveReqVO createReqVO) {
        return success(iotCategoryService.createIotCategory(createReqVO));
    }

    @PutMapping
    @Operation(summary = "更新yf产品分类")
    @PreAuthorize("@ss.hasPermission('rq:iot-category:update')")
    public CommonResult<Boolean> updateIotCategory(@Valid @RequestBody IotYfCategorySaveReqVO updateReqVO) {
        iotCategoryService.updateIotCategory(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除yf产品分类")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('rq:iot-category:delete')")
    public CommonResult<Boolean> deleteIotCategory(@RequestParam("id") Long id) {
        iotCategoryService.deleteIotCategory(id);
        return success(true);
    }

    @GetMapping("/{categoryId}")
    @Operation(summary = "获得yf产品分类")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('rq:iot-category:query')")
    public CommonResult<IotYfCategoryRespVO> getIotCategory(@RequestParam("id") Long id) {
        IotYfCategoryDO iotCategory = iotCategoryService.getIotCategory(id);
        return success(BeanUtils.toBean(iotCategory, IotYfCategoryRespVO.class));
    }

    @GetMapping("/shortlist")
    @Operation(summary = "获得yf产品分类")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('rq:iot-category:query')")
    public CommonResult<List<Map<String, Object>>> getIotCategoryShort(@RequestParam("id") Long id) {
        IotYfCategoryPageReqVO pageReqVO = new IotYfCategoryPageReqVO();
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IotYfCategoryDO> iotCategory = iotCategoryService.getIotCategoryPage(pageReqVO).getList();
        List<Map<String, Object>> result = new ArrayList<>();
        iotCategory.forEach(e ->{
            Map<String, Object> map = new HashMap<>();
            map.put("id",e.getCategoryId());
            result.add(map);
        });
        return success(result);
    }

    @GetMapping("/list")
    @Operation(summary = "获得yf产品分类分页")
    @PreAuthorize("@ss.hasPermission('rq:iot-category:query')")
    public CommonResult<PageResult<IotYfCategoryRespVO>> getIotCategoryPage(@Valid IotYfCategoryPageReqVO pageReqVO) {
        PageResult<IotYfCategoryDO> pageResult = iotCategoryService.getIotCategoryPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, IotYfCategoryRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出yf产品分类 Excel")
    @PreAuthorize("@ss.hasPermission('rq:iot-category:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportIotCategoryExcel(@Valid IotYfCategoryPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IotYfCategoryDO> list = iotCategoryService.getIotCategoryPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "yf产品分类.xls", "数据", IotYfCategoryRespVO.class,
                        BeanUtils.toBean(list, IotYfCategoryRespVO.class));
    }

}
