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

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.framework.security.core.LoginUser;
import com.zt.plat.framework.security.core.util.SecurityFrameworkUtils;
import com.zt.plat.module.qms.business.bus.controller.vo.*;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessSampleEntrustUnStandardProjectDO;
import com.zt.plat.module.qms.business.bus.service.BusinessSampleEntrustUnStandardProjectService;
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-sample-entrust-un-standard-project")
@Validated
@DeptDataPermissionIgnore(enable = "true")
public class BusinessSampleEntrustUnStandardProjectController implements BusinessControllerMarker {


    @Resource
    private BusinessSampleEntrustUnStandardProjectService businessSampleEntrustUnStandardProjectService;

    @PostMapping("/create")
    @Operation(summary = "创建委检登记样品检测项目表(无标准)")
    //@PreAuthorize("@ss.hasPermission('qms:business-sample-entrust-un-standard-project:create')")
    public CommonResult<BusinessSampleEntrustUnStandardProjectRespVO> createBusinessSampleEntrustUnStandardProject(@Valid @RequestBody BusinessSampleEntrustUnStandardProjectSaveReqVO createReqVO) {
        return success(businessSampleEntrustUnStandardProjectService.createBusinessSampleEntrustUnStandardProject(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新委检登记样品检测项目表(无标准)")
    //@PreAuthorize("@ss.hasPermission('qms:business-sample-entrust-un-standard-project:update')")
    public CommonResult<Boolean> updateBusinessSampleEntrustUnStandardProject(@Valid @RequestBody BusinessSampleEntrustUnStandardProjectSaveReqVO updateReqVO) {
        businessSampleEntrustUnStandardProjectService.updateBusinessSampleEntrustUnStandardProject(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除委检登记样品检测项目表(无标准)")
    @Parameter(name = "id", description = "编号", required = true)
    //@PreAuthorize("@ss.hasPermission('qms:business-sample-entrust-un-standard-project:delete')")
    public CommonResult<Boolean> deleteBusinessSampleEntrustUnStandardProject(@RequestParam("id") Long id) {
        businessSampleEntrustUnStandardProjectService.deleteBusinessSampleEntrustUnStandardProject(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除委检登记样品检测项目表(无标准)")
    //@PreAuthorize("@ss.hasPermission('qms:business-sample-entrust-un-standard-project:delete')")
    public CommonResult<Boolean> deleteBusinessSampleEntrustUnStandardProjectList(@RequestBody BatchDeleteReqVO req) {
        businessSampleEntrustUnStandardProjectService.deleteBusinessSampleEntrustUnStandardProjectListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得委检登记样品检测项目表(无标准)")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    //@PreAuthorize("@ss.hasPermission('qms:business-sample-entrust-un-standard-project:query')")
    public CommonResult<BusinessSampleEntrustUnStandardProjectRespVO> getBusinessSampleEntrustUnStandardProject(@RequestParam("id") Long id) {
        BusinessSampleEntrustUnStandardProjectDO businessSampleEntrustUnStandardProject = businessSampleEntrustUnStandardProjectService.getBusinessSampleEntrustUnStandardProject(id);
        return success(BeanUtils.toBean(businessSampleEntrustUnStandardProject, BusinessSampleEntrustUnStandardProjectRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得委检登记样品检测项目表(无标准)分页")
    //@PreAuthorize("@ss.hasPermission('qms:business-sample-entrust-un-standard-project:query')")
    public CommonResult<PageResult<BusinessSampleEntrustUnStandardProjectRespVO>> getBusinessSampleEntrustUnStandardProjectPage(@Valid BusinessSampleEntrustUnStandardProjectPageReqVO pageReqVO) {
        PageResult<BusinessSampleEntrustUnStandardProjectDO> pageResult = businessSampleEntrustUnStandardProjectService.getBusinessSampleEntrustUnStandardProjectPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BusinessSampleEntrustUnStandardProjectRespVO.class));
    }
    
    @GetMapping("/list")
    @Operation(summary = "获得委检登记样品检测项目表(无标准)列表")
    public CommonResult<List<BusinessSampleEntrustUnStandardProjectExtendRespVO>> getBusinessSampleEntrustUnStandardProjectList(BusinessSampleEntrustUnStandardProjectPageReqVO reqVO) {
    	if (reqVO.getAssayDepartmentId() == null) {
    		LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
            reqVO.setAssayDepartmentId(loginUser.getVisitDeptId());
		}
    	List<BusinessSampleEntrustUnStandardProjectExtendRespVO> list = businessSampleEntrustUnStandardProjectService.getBusinessSampleEntrustUnStandardProjectList(reqVO);
    	return success(list);
    }

    @GetMapping("/listByBusinessBaseSampleId")
    @Operation(summary = "获得委检登记样品检测项目表(无标准)分页")
    //@PreAuthorize("@ss.hasPermission('qms:business-sample-entrust-un-standard-project:query')")
    public CommonResult<List<BusinessSampleEntrustUnStandardProjectExtendRespVO>> listByBusinessBaseSampleId(@RequestParam("businessBaseSampleId") Long businessBaseSampleId) {
        List<BusinessSampleEntrustUnStandardProjectExtendRespVO> listResult = businessSampleEntrustUnStandardProjectService.listByBusinessBaseSampleId(businessBaseSampleId);
        return success(listResult);
    }
    
    @PostMapping("/saveAssayDept")
    @Operation(summary = "保存指派的分析部门")
    public CommonResult<?> saveAssayDept(@RequestBody BusinessSampleEntrustUnStandardProjectAssayDeptSaveReqVO reqVO) {
    	businessSampleEntrustUnStandardProjectService.saveAssayDept(reqVO);
    	return success("成功");
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出委检登记样品检测项目表(无标准) Excel")
    //@PreAuthorize("@ss.hasPermission('qms:business-sample-entrust-un-standard-project:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportBusinessSampleEntrustUnStandardProjectExcel(@Valid BusinessSampleEntrustUnStandardProjectPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BusinessSampleEntrustUnStandardProjectDO> list = businessSampleEntrustUnStandardProjectService.getBusinessSampleEntrustUnStandardProjectPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "委检登记样品检测项目表(无标准).xls", "数据", BusinessSampleEntrustUnStandardProjectRespVO.class,
                        BeanUtils.toBean(list, BusinessSampleEntrustUnStandardProjectRespVO.class));
    }

}