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.MaterialDestroyPageReqVO;
import com.zt.plat.module.base.controller.admin.base.vo.MaterialDestroyRespVO;
import com.zt.plat.module.base.controller.admin.base.vo.MaterialDestroySaveReqVO;
import com.zt.plat.module.base.dal.dataobject.base.MaterialDestroyDO;
import com.zt.plat.module.base.service.base.MaterialDestroyService;
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-destroy")
@Validated
public class MaterialDestroyController {


    @Resource
    private MaterialDestroyService materialDestroyService;

    @PostMapping("/create")
    @Operation(summary = "创建物料回收率")
    //@PreAuthorize("@ss.hasPermission('base:material-destroy:create')")
    public CommonResult<MaterialDestroyRespVO> createMaterialDestroy(@Valid @RequestBody MaterialDestroySaveReqVO createReqVO) {
        return success(materialDestroyService.createMaterialDestroy(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新物料回收率")
    //@PreAuthorize("@ss.hasPermission('base:material-destroy:update')")
    public CommonResult<Boolean> updateMaterialDestroy(@Valid @RequestBody MaterialDestroySaveReqVO updateReqVO) {
        materialDestroyService.updateMaterialDestroy(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除物料回收率")
    @Parameter(name = "id", description = "编号", required = true)
    //@PreAuthorize("@ss.hasPermission('base:material-destroy:delete')")
    public CommonResult<Boolean> deleteMaterialDestroy(@RequestParam("id") Long id) {
        materialDestroyService.deleteMaterialDestroy(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除物料回收率")
    //       @PreAuthorize("@ss.hasPermission('base:material-destroy:delete')")
    public CommonResult<Boolean> deleteMaterialDestroyList(@RequestBody BatchDeleteReqVO req) {
        materialDestroyService.deleteMaterialDestroyListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得物料回收率")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('base:material-destroy:query')")
    public CommonResult<MaterialDestroyRespVO> getMaterialDestroy(@RequestParam("id") Long id) {
        MaterialDestroyDO materialDestroy = materialDestroyService.getMaterialDestroy(id);
        return success(BeanUtils.toBean(materialDestroy, MaterialDestroyRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得物料回收率分页")
    @PreAuthorize("@ss.hasPermission('base:material-destroy:query')")
    public CommonResult<PageResult<MaterialDestroyRespVO>> getMaterialDestroyPage(@Valid MaterialDestroyPageReqVO pageReqVO) {
        PageResult<MaterialDestroyDO> pageResult = materialDestroyService.getMaterialDestroyPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MaterialDestroyRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出物料回收率 Excel")
    @PreAuthorize("@ss.hasPermission('base:material-destroy:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMaterialDestroyExcel(@Valid MaterialDestroyPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MaterialDestroyDO> list = materialDestroyService.getMaterialDestroyPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "物料回收率.xls", "数据", MaterialDestroyRespVO.class,
                        BeanUtils.toBean(list, MaterialDestroyRespVO.class));
    }

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