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.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.MaterialUseRecordDetailPageReqVO;
import com.zt.plat.module.qms.resource.material.controller.vo.MaterialUseRecordDetailRespVO;
import com.zt.plat.module.qms.resource.material.controller.vo.MaterialUseRecordDetailSaveReqVO;
import com.zt.plat.module.qms.resource.material.controller.vo.query.MaterialUseRecordWithMakeInfoQueryVO;
import com.zt.plat.module.qms.resource.material.controller.vo.resp.MaterialUseRecordWithMakeInfoRespVO;
import com.zt.plat.module.qms.resource.material.dal.dataobject.MaterialUseRecordDetailDO;
import com.zt.plat.module.qms.resource.material.service.MaterialUseRecordDetailService;
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-record-detail")
@Validated
@DeptDataPermissionIgnore(enable = "true")
public class MaterialUseRecordDetailController implements BusinessControllerMarker {


    @Resource
    private MaterialUseRecordDetailService materialUseRecordDetailService;

//    @PostMapping("/create")
    @Operation(summary = "创建使用记录配置关系,用于处理危险品溶液配置关系")
    @PreAuthorize("@ss.hasPermission('qms:material-use-record-detail:create')")
    public CommonResult<MaterialUseRecordDetailRespVO> createMaterialUseRecordDetail(@Valid @RequestBody MaterialUseRecordDetailSaveReqVO createReqVO) {
        return success(materialUseRecordDetailService.createMaterialUseRecordDetail(createReqVO));
    }

//    @PutMapping("/update")
    @Operation(summary = "更新使用记录配置关系,用于处理危险品溶液配置关系")
    @PreAuthorize("@ss.hasPermission('qms:material-use-record-detail:update')")
    public CommonResult<Boolean> updateMaterialUseRecordDetail(@Valid @RequestBody MaterialUseRecordDetailSaveReqVO updateReqVO) {
        materialUseRecordDetailService.updateMaterialUseRecordDetail(updateReqVO);
        return success(true);
    }

//    @DeleteMapping("/delete")
    @Operation(summary = "删除使用记录配置关系,用于处理危险品溶液配置关系")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('qms:material-use-record-detail:delete')")
    public CommonResult<Boolean> deleteMaterialUseRecordDetail(@RequestParam("id") Long id) {
        materialUseRecordDetailService.deleteMaterialUseRecordDetail(id);
        return success(true);
    }

//    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除使用记录配置关系,用于处理危险品溶液配置关系")
                @PreAuthorize("@ss.hasPermission('qms:material-use-record-detail:delete')")
    public CommonResult<Boolean> deleteMaterialUseRecordDetailList(@RequestBody BatchDeleteReqVO req) {
        materialUseRecordDetailService.deleteMaterialUseRecordDetailListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得使用记录配置关系,用于处理危险品溶液配置关系")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('qms:material-use-record-detail:query')")
    public CommonResult<MaterialUseRecordDetailRespVO> getMaterialUseRecordDetail(@RequestParam("id") Long id) {
        MaterialUseRecordDetailDO materialUseRecordDetail = materialUseRecordDetailService.getMaterialUseRecordDetail(id);
        return success(BeanUtils.toBean(materialUseRecordDetail, MaterialUseRecordDetailRespVO.class));
    }

    @GetMapping("/get-makeinfo")
    @Operation(summary = "获取危化品配置信息")
    public CommonResult<List<MaterialUseRecordWithMakeInfoRespVO>> getMaterialMakeInfoUseRecordDetail(@Valid MaterialUseRecordWithMakeInfoQueryVO queryVO) {
        List<MaterialUseRecordWithMakeInfoRespVO> makeInfoRespVOList = materialUseRecordDetailService.getMaterialMakeInfoUseRecordDetail(queryVO);
        return success(makeInfoRespVOList);
    }

    @GetMapping("/page")
    @Operation(summary = "获得使用记录配置关系,用于处理危险品溶液配置关系分页")
    @PreAuthorize("@ss.hasPermission('qms:material-use-record-detail:query')")
    public CommonResult<PageResult<MaterialUseRecordDetailRespVO>> getMaterialUseRecordDetailPage(@Valid MaterialUseRecordDetailPageReqVO pageReqVO) {
        PageResult<MaterialUseRecordDetailDO> pageResult = materialUseRecordDetailService.getMaterialUseRecordDetailPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MaterialUseRecordDetailRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出使用记录配置关系,用于处理危险品溶液配置关系 Excel")
    @PreAuthorize("@ss.hasPermission('qms:material-use-record-detail:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMaterialUseRecordDetailExcel(@Valid MaterialUseRecordDetailPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MaterialUseRecordDetailDO> list = materialUseRecordDetailService.getMaterialUseRecordDetailPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "使用记录配置关系,用于处理危险品溶液配置关系.xls", "数据", MaterialUseRecordDetailRespVO.class,
                        BeanUtils.toBean(list, MaterialUseRecordDetailRespVO.class));
    }

}