package com.zt.plat.module.qms.business.config.controller.admin;

import com.zt.plat.module.qms.business.config.controller.vo.ConfigWarehouseLocationInfomationPageReqVO;
import com.zt.plat.module.qms.business.config.controller.vo.ConfigWarehouseLocationInfomationRespVO;
import com.zt.plat.module.qms.business.config.controller.vo.ConfigWarehouseLocationInfomationSaveReqVO;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import com.zt.plat.framework.business.interceptor.BusinessControllerMarker;

import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.zt.plat.framework.common.pojo.vo.BatchDeleteReqVO;
import com.zt.plat.framework.common.pojo.PageParam;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.pojo.CommonResult;
import com.zt.plat.framework.common.util.object.BeanUtils;
import static com.zt.plat.framework.common.pojo.CommonResult.success;

import com.zt.plat.framework.excel.core.util.ExcelUtils;

import com.zt.plat.framework.apilog.core.annotation.ApiAccessLog;
import static com.zt.plat.framework.apilog.core.enums.OperateTypeEnum.*;

import com.zt.plat.module.qms.business.config.controller.vo.*;
import com.zt.plat.module.qms.business.config.dal.dataobject.ConfigWarehouseLocationInfomationDO;
import com.zt.plat.module.qms.business.config.service.ConfigWarehouseLocationInfomationService;

@Tag(name = "管理后台 - 样品库位信息")
@RestController
@RequestMapping("/qms/config-warehouse-location-infomation")
@Validated
public class ConfigWarehouseLocationInfomationController implements BusinessControllerMarker {


    @Resource
    private ConfigWarehouseLocationInfomationService configWarehouseLocationInfomationService;

    @PostMapping("/create")
    @Operation(summary = "创建样品库位信息")
    @PreAuthorize("@ss.hasPermission('qms:config-warehouse-location-infomation:create')")
    public CommonResult<ConfigWarehouseLocationInfomationRespVO> createConfigWarehouseLocationInfomation(@Valid @RequestBody ConfigWarehouseLocationInfomationSaveReqVO createReqVO) {
        return success(configWarehouseLocationInfomationService.createConfigWarehouseLocationInfomation(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新样品库位信息")
    @PreAuthorize("@ss.hasPermission('qms:config-warehouse-location-infomation:update')")
    public CommonResult<Boolean> updateConfigWarehouseLocationInfomation(@Valid @RequestBody ConfigWarehouseLocationInfomationSaveReqVO updateReqVO) {
        configWarehouseLocationInfomationService.updateConfigWarehouseLocationInfomation(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除样品库位信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('qms:config-warehouse-location-infomation:delete')")
    public CommonResult<Boolean> deleteConfigWarehouseLocationInfomation(@RequestParam("id") Long id) {
        configWarehouseLocationInfomationService.deleteConfigWarehouseLocationInfomation(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除样品库位信息")
                @PreAuthorize("@ss.hasPermission('qms:config-warehouse-location-infomation:delete')")
    public CommonResult<Boolean> deleteConfigWarehouseLocationInfomationList(@RequestBody BatchDeleteReqVO req) {
        configWarehouseLocationInfomationService.deleteConfigWarehouseLocationInfomationListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得样品库位信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('qms:config-warehouse-location-infomation:query')")
    public CommonResult<ConfigWarehouseLocationInfomationRespVO> getConfigWarehouseLocationInfomation(@RequestParam("id") Long id) {
        ConfigWarehouseLocationInfomationDO configWarehouseLocationInfomation = configWarehouseLocationInfomationService.getConfigWarehouseLocationInfomation(id);
        return success(BeanUtils.toBean(configWarehouseLocationInfomation, ConfigWarehouseLocationInfomationRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得样品库位信息分页")
    @PreAuthorize("@ss.hasPermission('qms:config-warehouse-location-infomation:query')")
    public CommonResult<PageResult<ConfigWarehouseLocationInfomationRespVO>> getConfigWarehouseLocationInfomationPage(@Valid ConfigWarehouseLocationInfomationPageReqVO pageReqVO) {
        PageResult<ConfigWarehouseLocationInfomationDO> pageResult = configWarehouseLocationInfomationService.getConfigWarehouseLocationInfomationPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ConfigWarehouseLocationInfomationRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出样品库位信息 Excel")
    @PreAuthorize("@ss.hasPermission('qms:config-warehouse-location-infomation:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportConfigWarehouseLocationInfomationExcel(@Valid ConfigWarehouseLocationInfomationPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ConfigWarehouseLocationInfomationDO> list = configWarehouseLocationInfomationService.getConfigWarehouseLocationInfomationPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "样品库位信息.xls", "数据", ConfigWarehouseLocationInfomationRespVO.class,
                        BeanUtils.toBean(list, ConfigWarehouseLocationInfomationRespVO.class));
    }

}