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.device.controller.vo.DeviceProductPageReqVO;
import com.zt.plat.module.qms.resource.device.controller.vo.DeviceProductRespVO;
import com.zt.plat.module.qms.resource.device.dal.dataobject.DeviceProductDO;
import com.zt.plat.module.qms.resource.material.controller.vo.DeviceProductWithInfomationsRespVO;
import com.zt.plat.module.qms.resource.material.controller.vo.MaterialStandardSolutionPageReqVO;
import com.zt.plat.module.qms.resource.material.controller.vo.MaterialStandardSolutionRespVO;
import com.zt.plat.module.qms.resource.material.controller.vo.MaterialStandardSolutionSaveReqVO;
import com.zt.plat.module.qms.resource.material.dal.dataobject.MaterialStandardSolutionDO;
import com.zt.plat.module.qms.resource.material.service.MaterialStandardSolutionService;
import com.zt.plat.module.qms.resource.material.valid.AddGroup;
import com.zt.plat.module.qms.resource.material.valid.UpdateGroup;
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-standard-solution")
@Validated
@FileUploadController(source = "qms.materialstandardsolution")
@DeptDataPermissionIgnore(enable = "true")
public class MaterialStandardSolutionController extends AbstractFileUploadController implements BusinessControllerMarker{

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

    @Resource
    private MaterialStandardSolutionService materialStandardSolutionService;

    @PostMapping("/create")
    @Operation(summary = "新建配置")
//    @PreAuthorize("@ss.hasPermission('qms:material-standard-solution:create')")
    public CommonResult<MaterialStandardSolutionRespVO> createMaterialStandardSolution(@Validated(AddGroup.class) @RequestBody MaterialStandardSolutionSaveReqVO createReqVO) {
        return success(materialStandardSolutionService.createMaterialStandardSolution(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新标液,包含配置信息")
//    @PreAuthorize("@ss.hasPermission('qms:material-standard-solution:update')")
    public CommonResult<Boolean> updateMaterialStandardSolution(@Validated(UpdateGroup.class) @RequestBody MaterialStandardSolutionSaveReqVO updateReqVO) {
        materialStandardSolutionService.updateMaterialStandardSolution(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除标液,标准溶液，包含配置信息")
    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('qms:material-standard-solution:delete')")
    public CommonResult<Boolean> deleteMaterialStandardSolution(@RequestParam("id") Long id) {
        materialStandardSolutionService.deleteMaterialStandardSolution(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除标液,标准溶液，包含配置信息")
                @PreAuthorize("@ss.hasPermission('qms:material-standard-solution:delete')")
    public CommonResult<Boolean> deleteMaterialStandardSolutionList(@RequestBody BatchDeleteReqVO req) {
        materialStandardSolutionService.deleteMaterialStandardSolutionListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得标液,标准溶液，包含配置信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('qms:material-standard-solution:query')")
    public CommonResult<MaterialStandardSolutionRespVO> getStandardSolutionWithMaterialInfo(@RequestParam("id") Long id) {
        MaterialStandardSolutionRespVO solutionRespVO = materialStandardSolutionService.getStandardSolutionWithMaterialInfo(id);
        return success(solutionRespVO);
    }

    @GetMapping("/page")
    @Operation(summary = "获得标液,标准溶液，包含配置信息分页")
//    @PreAuthorize("@ss.hasPermission('qms:material-standard-solution:query')")
    public CommonResult<PageResult<MaterialStandardSolutionRespVO>> getStandardSolutionPageWithMaterialInfo(@Valid MaterialStandardSolutionPageReqVO pageReqVO) {
        PageResult<MaterialStandardSolutionRespVO> pageResult = materialStandardSolutionService.getStandardSolutionPageWithMaterialInfo(pageReqVO);
        return success(pageResult);
    }


    @GetMapping("/device-burette-list")
    @Operation(summary = "获取设备滴定管")
    public CommonResult<List<DeviceProductWithInfomationsRespVO>> getDeviceBuretteProductWithInfomationList() {
        List<DeviceProductWithInfomationsRespVO> deviceProductRespVOS = materialStandardSolutionService.getDeviceBuretteProductWithInfomationList();
        return success(deviceProductRespVOS);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出标液,标准溶液，包含配置信息 Excel")
    @PreAuthorize("@ss.hasPermission('qms:material-standard-solution:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMaterialStandardSolutionExcel(@Valid MaterialStandardSolutionPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MaterialStandardSolutionDO> list = materialStandardSolutionService.getMaterialStandardSolutionPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "标液,标准溶液，包含配置信息.xls", "数据", MaterialStandardSolutionRespVO.class,
                        BeanUtils.toBean(list, MaterialStandardSolutionRespVO.class));
    }

}