package com.zt.plat.module.base.controller.admin.materialhasclasses;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
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 jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.zt.plat.framework.common.pojo.vo.BatchDeleteReqVO;
import com.zt.plat.framework.common.pojo.PageParam;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.pojo.CommonResult;
import com.zt.plat.framework.common.util.object.BeanUtils;
import static com.zt.plat.framework.common.pojo.CommonResult.success;

import com.zt.plat.framework.excel.core.util.ExcelUtils;

import com.zt.plat.framework.apilog.core.annotation.ApiAccessLog;
import static com.zt.plat.framework.apilog.core.enums.OperateTypeEnum.*;

import com.zt.plat.module.base.controller.admin.materialhasclasses.vo.*;
import com.zt.plat.module.base.dal.dataobject.materialhasclasses.MaterialHasClassesDO;
import com.zt.plat.module.base.service.materialhasclasses.MaterialHasClassesService;

@Tag(name = "管理后台 - 物料持有分类")
@RestController
@RequestMapping("/base/material-has-classes")
@Validated
public class MaterialHasClassesController {


    @Resource
    private MaterialHasClassesService materialHasClassesService;

    @PostMapping("/create")
    @Operation(summary = "创建物料持有分类")
    @PreAuthorize("@ss.hasPermission('base:material-has-classes:create')")
    public CommonResult<MaterialHasClassesRespVO> createMaterialHasClasses(@Valid @RequestBody MaterialHasClassesSaveReqVO createReqVO) {
        return success(materialHasClassesService.createMaterialHasClasses(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新物料持有分类")
    @PreAuthorize("@ss.hasPermission('base:material-has-classes:update')")
    public CommonResult<Boolean> updateMaterialHasClasses(@Valid @RequestBody MaterialHasClassesSaveReqVO updateReqVO) {
        materialHasClassesService.updateMaterialHasClasses(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除物料持有分类")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('base:material-has-classes:delete')")
    public CommonResult<Boolean> deleteMaterialHasClasses(@RequestParam("id") Long id) {
        materialHasClassesService.deleteMaterialHasClasses(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除物料持有分类")
                @PreAuthorize("@ss.hasPermission('base:material-has-classes:delete')")
    public CommonResult<Boolean> deleteMaterialHasClassesList(@RequestBody BatchDeleteReqVO req) {
        materialHasClassesService.deleteMaterialHasClassesListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得物料持有分类")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('base:material-has-classes:query')")
    public CommonResult<MaterialHasClassesRespVO> getMaterialHasClasses(@RequestParam("id") Long id) {
        MaterialHasClassesDO materialHasClasses = materialHasClassesService.getMaterialHasClasses(id);
        return success(BeanUtils.toBean(materialHasClasses, MaterialHasClassesRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得物料持有分类分页")
    @PreAuthorize("@ss.hasPermission('base:material-has-classes:query')")
    public CommonResult<PageResult<MaterialHasClassesRespVO>> getMaterialHasClassesPage(@Valid MaterialHasClassesPageReqVO pageReqVO) {
        PageResult<MaterialHasClassesDO> pageResult = materialHasClassesService.getMaterialHasClassesPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MaterialHasClassesRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出物料持有分类 Excel")
    @PreAuthorize("@ss.hasPermission('base:material-has-classes:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMaterialHasClassesExcel(@Valid MaterialHasClassesPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MaterialHasClassesDO> list = materialHasClassesService.getMaterialHasClassesPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "物料持有分类.xls", "数据", MaterialHasClassesRespVO.class,
                        BeanUtils.toBean(list, MaterialHasClassesRespVO.class));
    }

}