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

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.materialhasproperties.vo.*;
import com.zt.plat.module.base.dal.dataobject.materialhasproperties.MaterialHasPropertiesDO;
import com.zt.plat.module.base.service.materialhasproperties.MaterialHasPropertiesService;

@Tag(name = "管理后台 - 物料持有属性")
@RestController
@RequestMapping("/base/material-has-properties")
@Validated
public class MaterialHasPropertiesController {


    @Resource
    private MaterialHasPropertiesService materialHasPropertiesService;

    @PostMapping("/create")
    @Operation(summary = "创建物料持有属性")
    @PreAuthorize("@ss.hasPermission('base:material-properties:create')")
    public CommonResult<MaterialHasPropertiesRespVO> createMaterialHasProperties(@Valid @RequestBody MaterialHasPropertiesSaveReqVO createReqVO) {
        return success(materialHasPropertiesService.createMaterialHasProperties(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新物料持有属性")
    @PreAuthorize("@ss.hasPermission('base:material-properties:update')")
    public CommonResult<Boolean> updateMaterialHasProperties(@Valid @RequestBody MaterialHasPropertiesSaveReqVO updateReqVO) {
        materialHasPropertiesService.updateMaterialHasProperties(updateReqVO);
        return success(true);
    }

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

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除物料持有属性")
                @PreAuthorize("@ss.hasPermission('base:material-properties:delete')")
    public CommonResult<Boolean> deleteMaterialHasPropertiesList(@RequestBody BatchDeleteReqVO req) {
        materialHasPropertiesService.deleteMaterialHasPropertiesListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得物料持有属性")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('base:material-properties:query')")
    public CommonResult<MaterialHasPropertiesRespVO> getMaterialHasProperties(@RequestParam("id") Long id) {
        MaterialHasPropertiesDO materialHasProperties = materialHasPropertiesService.getMaterialHasProperties(id);
        return success(BeanUtils.toBean(materialHasProperties, MaterialHasPropertiesRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得物料持有属性分页")
    @PreAuthorize("@ss.hasPermission('base:material-properties:query')")
    public CommonResult<PageResult<MaterialHasPropertiesRespVO>> getMaterialHasPropertiesPage(@Valid MaterialHasPropertiesPageReqVO pageReqVO) {
        pageReqVO.setDeptId(null);
        PageResult<MaterialHasPropertiesDO> pageResult = materialHasPropertiesService.getMaterialHasPropertiesPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MaterialHasPropertiesRespVO.class));
    }

    @PostMapping("/batch-save")
    @Operation(summary = "批量保存物料持有属性（全量替换）")
    @PreAuthorize("@ss.hasPermission('base:material-properties:update')")
    public CommonResult<MaterialHasPropertiesBatchSaveRespVO> batchSave(@Valid @RequestBody MaterialHasPropertiesBatchSaveReqVO reqVO) {
        MaterialHasPropertiesBatchSaveRespVO resp = materialHasPropertiesService.batchSave(reqVO, null);
        return success(resp);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出物料持有属性 Excel")
    @PreAuthorize("@ss.hasPermission('base:material-properties:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMaterialHasPropertiesExcel(@Valid MaterialHasPropertiesPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MaterialHasPropertiesDO> list = materialHasPropertiesService.getMaterialHasPropertiesPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "物料持有属性.xls", "数据", MaterialHasPropertiesRespVO.class,
                        BeanUtils.toBean(list, MaterialHasPropertiesRespVO.class));
    }

}