package com.zt.plat.module.qms.resource.material.controller.admin;

import com.zt.plat.framework.apilog.core.annotation.ApiAccessLog;
import com.zt.plat.framework.business.annotation.FileUploadController;
import com.zt.plat.framework.business.controller.AbstractFileUploadController;
import com.zt.plat.framework.business.interceptor.BusinessControllerMarker;
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.datapermission.core.annotation.DeptDataPermissionIgnore;
import com.zt.plat.framework.excel.core.util.ExcelUtils;
import com.zt.plat.module.qms.resource.material.controller.vo.MaterialUseEndReusePageReqVO;
import com.zt.plat.module.qms.resource.material.controller.vo.MaterialUseEndReuseRespVO;
import com.zt.plat.module.qms.resource.material.controller.vo.MaterialUseEndReuseSaveReqVO;
import com.zt.plat.module.qms.resource.material.dal.dataobject.MaterialUseEndReuseDO;
import com.zt.plat.module.qms.resource.material.service.MaterialUseEndReuseService;
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("/qms/resource/material-use-end-reuse")
@Validated
@FileUploadController(source = "qms.materialuseendreuse")
@DeptDataPermissionIgnore(enable = "true")
public class MaterialUseEndReuseController extends AbstractFileUploadController implements BusinessControllerMarker{

    static {
        FileUploadController annotation = MaterialUseEndReuseController.class.getAnnotation(FileUploadController.class);
        if (annotation != null) {
        setFileUploadInfo(annotation);
        }
    }

    @Resource
    private MaterialUseEndReuseService materialUseEndReuseService;

    @PostMapping("/add")
    @Operation(summary = "新增物料用完标记或清洗回收")
//    @PreAuthorize("@ss.hasPermission('qms:material-use-end-reuse:create')")
    public CommonResult<MaterialUseEndReuseRespVO> addMaterialUseEndReuse(@Valid @RequestBody MaterialUseEndReuseSaveReqVO createReqVO) {
        return success(materialUseEndReuseService.createMaterialUseEndReuse(createReqVO));
    }

//    @PutMapping("/update")
    @Operation(summary = "更新物料用完标记或清洗回收")
    @PreAuthorize("@ss.hasPermission('qms:material-use-end-reuse:update')")
    public CommonResult<Boolean> updateMaterialUseEndReuse(@Valid @RequestBody MaterialUseEndReuseSaveReqVO updateReqVO) {
        materialUseEndReuseService.updateMaterialUseEndReuse(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除物料用完标记或清洗回收")
    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('qms:material-use-end-reuse:delete')")
    public CommonResult<Boolean> deleteMaterialUseEndReuse(@RequestParam("id") Long id) {
        materialUseEndReuseService.deleteMaterialUseEndReuse(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除物料用完标记或清洗回收")
                @PreAuthorize("@ss.hasPermission('qms:material-use-end-reuse:delete')")
    public CommonResult<Boolean> deleteMaterialUseEndReuseList(@RequestBody BatchDeleteReqVO req) {
        materialUseEndReuseService.deleteMaterialUseEndReuseListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得物料用完标记或清洗回收")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('qms:material-use-end-reuse:query')")
    public CommonResult<MaterialUseEndReuseRespVO> getMaterialUseEndReuse(@RequestParam("id") Long id) {
        MaterialUseEndReuseDO materialUseEndReuse = materialUseEndReuseService.getMaterialUseEndReuse(id);
        return success(BeanUtils.toBean(materialUseEndReuse, MaterialUseEndReuseRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得物料用完标记或清洗回收分页")
//    @PreAuthorize("@ss.hasPermission('qms:material-use-end-reuse:query')")
    public CommonResult<PageResult<MaterialUseEndReuseRespVO>> getMaterialUseEndReusePage(@Valid MaterialUseEndReusePageReqVO pageReqVO) {
        PageResult<MaterialUseEndReuseRespVO> pageResult = materialUseEndReuseService.getPageWithDetail(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出物料用完标记或清洗回收 Excel")
    @PreAuthorize("@ss.hasPermission('qms:material-use-end-reuse:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMaterialUseEndReuseExcel(@Valid MaterialUseEndReusePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MaterialUseEndReuseDO> list = materialUseEndReuseService.getMaterialUseEndReusePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "物料用完标记或清洗回收.xls", "数据", MaterialUseEndReuseRespVO.class,
                        BeanUtils.toBean(list, MaterialUseEndReuseRespVO.class));
    }

    @PutMapping("/reuse")
    @Operation(summary = "物料回收")
    public CommonResult<Boolean> reuseMaterial(@RequestBody List<Long> detailIds) {
        materialUseEndReuseService.reuseMaterialsByDetailIds(detailIds);
        return success(true);
    }

}