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

import com.zt.plat.framework.apilog.core.annotation.ApiAccessLog;
import com.zt.plat.framework.common.pojo.CommonResult;
import com.zt.plat.framework.common.pojo.PageParam;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.pojo.vo.BatchDeleteReqVO;
import com.zt.plat.framework.common.util.object.BeanUtils;
import com.zt.plat.framework.excel.core.util.ExcelUtils;
import com.zt.plat.module.base.controller.admin.base.vo.MaterialOtherPageReqVO;
import com.zt.plat.module.base.controller.admin.base.vo.MaterialOtherRespVO;
import com.zt.plat.module.base.controller.admin.base.vo.MaterialOtherSaveReqVO;
import com.zt.plat.module.base.dal.dataobject.base.MaterialOtherDO;
import com.zt.plat.module.base.service.base.MaterialOtherService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static com.zt.plat.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static com.zt.plat.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 物料拓展数据")
@RestController
@RequestMapping("/base/material-other")
@Validated
public class MaterialOtherController {


    @Resource
    private MaterialOtherService materialOtherService;

    @PostMapping("/create")
    @Operation(summary = "创建物料拓展数据")
    @PreAuthorize("@ss.hasPermission('base:material-other:create')")
    public CommonResult<MaterialOtherRespVO> createMaterialOther(@Valid @RequestBody MaterialOtherSaveReqVO createReqVO) {
        return success(materialOtherService.createMaterialOther(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新物料拓展数据")
    @PreAuthorize("@ss.hasPermission('base:material-other:update')")
    public CommonResult<Boolean> updateMaterialOther(@Valid @RequestBody MaterialOtherSaveReqVO updateReqVO) {
        materialOtherService.updateMaterialOther(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除物料拓展数据")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('base:material-other:delete')")
    public CommonResult<Boolean> deleteMaterialOther(@RequestParam("id") Long id) {
        materialOtherService.deleteMaterialOther(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除物料拓展数据")
                @PreAuthorize("@ss.hasPermission('base:material-other:delete')")
    public CommonResult<Boolean> deleteMaterialOtherList(@RequestBody BatchDeleteReqVO req) {
        materialOtherService.deleteMaterialOtherListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得物料拓展数据")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('base:material-other:query')")
    public CommonResult<MaterialOtherRespVO> getMaterialOther(@RequestParam("id") Long id) {
        MaterialOtherDO materialOther = materialOtherService.getMaterialOther(id);
        return success(BeanUtils.toBean(materialOther, MaterialOtherRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得物料拓展数据分页")
    @PreAuthorize("@ss.hasPermission('base:material-other:query')")
    public CommonResult<PageResult<MaterialOtherRespVO>> getMaterialOtherPage(@Valid MaterialOtherPageReqVO pageReqVO) {
        PageResult<MaterialOtherDO> pageResult = materialOtherService.getMaterialOtherPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MaterialOtherRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出物料拓展数据 Excel")
    @PreAuthorize("@ss.hasPermission('base:material-other:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMaterialOtherExcel(@Valid MaterialOtherPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MaterialOtherDO> list = materialOtherService.getMaterialOtherPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "物料拓展数据.xls", "数据", MaterialOtherRespVO.class,
                        BeanUtils.toBean(list, MaterialOtherRespVO.class));
    }

    @PutMapping("/enable-list")
    @Operation(summary = "批量更新")
    @PreAuthorize("@ss.hasPermission('base:material-other:update')")
    public CommonResult<Boolean> enableMaterialOtherList(@RequestBody List<MaterialOtherRespVO> saveReqVOS) {
        materialOtherService.enableMaterialOtherList(saveReqVOS);
        return success(true);
    }

}