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

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 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.materialproperties.vo.*;
import com.zt.plat.module.base.service.materialproperties.MaterialPropertiesService;

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


    @Resource
    private MaterialPropertiesService materialPropertiesService;

    @PostMapping("/create")
    @Operation(summary = "创建物料属性")
    @PreAuthorize("@ss.hasPermission('base:material-properties:create')")
    public CommonResult<MaterialPropertiesRespVO> createMaterialProperties(@Valid @RequestBody MaterialPropertiesSaveReqVO createReqVO) {
        createReqVO.setDeptId(null);
        return success(materialPropertiesService.createMaterialProperties(createReqVO));
    }

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

    @DeleteMapping("/delete")
    @Operation(summary = "删除物料属性")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('base:material-properties:delete')")
    public CommonResult<Boolean> deleteMaterialProperties(@RequestParam("id") Long id) {
        materialPropertiesService.deleteMaterialProperties(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> deleteMaterialPropertiesList(@RequestBody BatchDeleteReqVO req) {
        materialPropertiesService.deleteMaterialPropertiesListByIds(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<MaterialPropertiesRespVO> getMaterialProperties(@RequestParam("id") Long id) {
        MaterialPropertiesRespVO materialProperties = materialPropertiesService.getMaterialProperties(id);
        if (materialProperties.getDeptId() != null) {
            return CommonResult.error(401, "没有权限");
        }
        return success(materialProperties);
    }

    @GetMapping("/page")
    @Operation(summary = "获得物料属性分页")
    @PreAuthorize("@ss.hasPermission('base:material-properties:query')")
    public CommonResult<PageResult<MaterialPropertiesRespVO>> getMaterialPropertiesPage(@Valid MaterialPropertiesPageReqVO pageReqVO) {
        pageReqVO.setDeptId(null);
        PageResult<MaterialPropertiesRespVO> pageResult = materialPropertiesService.getMaterialPropertiesPage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/simple-page")
    @Operation(summary = "获得物料属性精简分页")
    @PreAuthorize("@ss.hasPermission('base:material-properties:query')")
    public CommonResult<PageResult<MaterialPropertiesSimpleRespVO>> getMaterialPropertiesSimplePage(@Valid MaterialPropertiesSimplePageReqVO pageReqVO) {
        pageReqVO.setDeptId(null);
        return success(materialPropertiesService.getMaterialPropertiesSimplePage(pageReqVO));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出物料属性 Excel")
    @PreAuthorize("@ss.hasPermission('base:material-properties:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMaterialPropertiesExcel(@Valid MaterialPropertiesPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        pageReqVO.setDeptId(null);
        List<MaterialPropertiesRespVO> list = materialPropertiesService.getMaterialPropertiesPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "物料属性.xls", "数据", MaterialPropertiesRespVO.class,
            list);
    }

}