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.MaterialCorrelationAssayPageReqVO;
import com.zt.plat.module.qms.resource.material.controller.vo.MaterialCorrelationAssayRespVO;
import com.zt.plat.module.qms.resource.material.controller.vo.MaterialCorrelationAssaySaveReqVO;
import com.zt.plat.module.qms.resource.material.dal.dataobject.MaterialCorrelationAssayDO;
import com.zt.plat.module.qms.resource.material.service.MaterialCorrelationAssayService;
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-correlation-assay")
@Validated
@DeptDataPermissionIgnore(enable = "true")
public class MaterialCorrelationAssayController implements BusinessControllerMarker {


    @Resource
    private MaterialCorrelationAssayService materialCorrelationAssayService;

//    @PostMapping("/create")
    @Operation(summary = "创建物料检化验关联信息")
    @PreAuthorize("@ss.hasPermission('qms:material-correlation-assay:create')")
    public CommonResult<MaterialCorrelationAssayRespVO> createMaterialCorrelationAssay(@Valid @RequestBody MaterialCorrelationAssaySaveReqVO createReqVO) {
        return success(materialCorrelationAssayService.createMaterialCorrelationAssay(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新物料检化验关联信息")
//    @PreAuthorize("@ss.hasPermission('qms:material-correlation-assay:update')")
    public CommonResult<Boolean> updateMaterialCorrelationAssay(@Valid @RequestBody MaterialCorrelationAssaySaveReqVO updateReqVO) {
        materialCorrelationAssayService.updateMaterialCorrelationAssay(updateReqVO);
        return success(true);
    }

//    @DeleteMapping("/delete")
    @Operation(summary = "删除物料检化验关联信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('qms:material-correlation-assay:delete')")
    public CommonResult<Boolean> deleteMaterialCorrelationAssay(@RequestParam("id") Long id) {
        materialCorrelationAssayService.deleteMaterialCorrelationAssay(id);
        return success(true);
    }

//    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除物料检化验关联信息")
                @PreAuthorize("@ss.hasPermission('qms:material-correlation-assay:delete')")
    public CommonResult<Boolean> deleteMaterialCorrelationAssayList(@RequestBody BatchDeleteReqVO req) {
        materialCorrelationAssayService.deleteMaterialCorrelationAssayListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得物料检化验关联信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('qms:material-correlation-assay:query')")
    public CommonResult<MaterialCorrelationAssayRespVO> getMaterialCorrelationAssay(@RequestParam("id") Long id) {
        MaterialCorrelationAssayDO materialCorrelationAssay = materialCorrelationAssayService.getMaterialCorrelationAssay(id);
        return success(BeanUtils.toBean(materialCorrelationAssay, MaterialCorrelationAssayRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得物料检化验关联信息分页")
    @PreAuthorize("@ss.hasPermission('qms:material-correlation-assay:query')")
    public CommonResult<PageResult<MaterialCorrelationAssayRespVO>> getMaterialCorrelationAssayPage(@Valid MaterialCorrelationAssayPageReqVO pageReqVO) {
        PageResult<MaterialCorrelationAssayDO> pageResult = materialCorrelationAssayService.getMaterialCorrelationAssayPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MaterialCorrelationAssayRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出物料检化验关联信息 Excel")
    @PreAuthorize("@ss.hasPermission('qms:material-correlation-assay:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMaterialCorrelationAssayExcel(@Valid MaterialCorrelationAssayPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MaterialCorrelationAssayDO> list = materialCorrelationAssayService.getMaterialCorrelationAssayPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "物料检化验关联信息.xls", "数据", MaterialCorrelationAssayRespVO.class,
                        BeanUtils.toBean(list, MaterialCorrelationAssayRespVO.class));
    }

}