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

import com.zt.plat.module.qms.business.bus.controller.vo.BusinessBaseSamplePageReqVO;
import com.zt.plat.module.qms.business.bus.controller.vo.BusinessBaseSampleRespVO;
import com.zt.plat.module.qms.business.bus.controller.vo.BusinessBaseSampleSaveReqVO;
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 com.zt.plat.framework.datapermission.core.annotation.DeptDataPermissionIgnore;

import static com.zt.plat.framework.common.pojo.CommonResult.success;

import com.zt.plat.framework.excel.core.util.ExcelUtils;
import com.zt.plat.module.qms.business.bus.controller.vo.*;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessBaseSampleDO;
import com.zt.plat.module.qms.business.bus.service.BusinessBaseSampleService;
import com.zt.plat.framework.apilog.core.annotation.ApiAccessLog;
import static com.zt.plat.framework.apilog.core.enums.OperateTypeEnum.*;

@Tag(name = "管理后台 - 主样业务")
@RestController
@RequestMapping("/qms/business-base-sample")
@Validated
@DeptDataPermissionIgnore(enable = "true")
public class BusinessBaseSampleController implements BusinessControllerMarker {


    @Resource
    private BusinessBaseSampleService businessBaseSampleService;

    @PostMapping("/create")
    @Operation(summary = "创建主样业务")
    //@PreAuthorize("@ss.hasPermission('qms:business-base-sample:create')")
    public CommonResult<BusinessBaseSampleRespVO> createBusinessBaseSample(@Valid @RequestBody BusinessBaseSampleSaveReqVO createReqVO) {
        return success(businessBaseSampleService.createBusinessBaseSample(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新主样业务")
    //@PreAuthorize("@ss.hasPermission('qms:business-base-sample:update')")
    public CommonResult<Boolean> updateBusinessBaseSample(@Valid @RequestBody BusinessBaseSampleSaveReqVO updateReqVO) {
        businessBaseSampleService.updateBusinessBaseSample(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除主样业务")
    @Parameter(name = "id", description = "编号", required = true)
    //@PreAuthorize("@ss.hasPermission('qms:business-base-sample:delete')")
    public CommonResult<Boolean> deleteBusinessBaseSample(@RequestParam("id") Long id) {
        businessBaseSampleService.deleteBusinessBaseSample(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除主样业务")
                //@PreAuthorize("@ss.hasPermission('qms:business-base-sample:delete')")
    public CommonResult<Boolean> deleteBusinessBaseSampleList(@RequestBody BatchDeleteReqVO req) {
        businessBaseSampleService.deleteBusinessBaseSampleListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得主样业务")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    //@PreAuthorize("@ss.hasPermission('qms:business-base-sample:query')")
    public CommonResult<BusinessBaseSampleRespVO> getBusinessBaseSample(@RequestParam("id") Long id) {
        BusinessBaseSampleDO businessBaseSample = businessBaseSampleService.getBusinessBaseSample(id);
        return success(BeanUtils.toBean(businessBaseSample, BusinessBaseSampleRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得主样业务分页")
    //@PreAuthorize("@ss.hasPermission('qms:business-base-sample:query')")
    public CommonResult<PageResult<BusinessBaseSampleRespVO>> getBusinessBaseSamplePage(@Valid BusinessBaseSamplePageReqVO pageReqVO) {
        PageResult<BusinessBaseSampleDO> pageResult = businessBaseSampleService.getBusinessBaseSamplePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BusinessBaseSampleRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出主样业务 Excel")
    //@PreAuthorize("@ss.hasPermission('qms:business-base-sample:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportBusinessBaseSampleExcel(@Valid BusinessBaseSamplePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BusinessBaseSampleDO> list = businessBaseSampleService.getBusinessBaseSamplePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "主样业务.xls", "数据", BusinessBaseSampleRespVO.class,
                        BeanUtils.toBean(list, BusinessBaseSampleRespVO.class));
    }

}