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

import com.zt.plat.module.qms.business.bus.controller.vo.*;
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.BusinessAssayTaskDataDO;
import com.zt.plat.module.qms.business.bus.service.BusinessAssayTaskDataService;
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-assay-task-data")
@Validated
@DeptDataPermissionIgnore(enable = "true")
public class BusinessAssayTaskDataController implements BusinessControllerMarker {


    @Resource
    private BusinessAssayTaskDataService businessAssayTaskDataService;

    @PostMapping("/create")
    @Operation(summary = "创建子样检测任务业务")
    //@PreAuthorize("@ss.hasPermission('qms:business-assay-task-data:create')")
    public CommonResult<BusinessAssayTaskDataRespVO> createBusinessAssayTaskData(@Valid @RequestBody BusinessAssayTaskDataSaveReqVO createReqVO) {
        return success(businessAssayTaskDataService.createBusinessAssayTaskData(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新子样检测任务业务")
    //@PreAuthorize("@ss.hasPermission('qms:business-assay-task-data:update')")
    public CommonResult<Boolean> updateBusinessAssayTaskData(@Valid @RequestBody BusinessAssayTaskDataSaveReqVO updateReqVO) {
        businessAssayTaskDataService.updateBusinessAssayTaskData(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除子样检测任务业务")
    @Parameter(name = "id", description = "编号", required = true)
    //@PreAuthorize("@ss.hasPermission('qms:business-assay-task-data:delete')")
    public CommonResult<Boolean> deleteBusinessAssayTaskData(@RequestParam("id") Long id) {
        businessAssayTaskDataService.deleteBusinessAssayTaskData(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除子样检测任务业务")
                //@PreAuthorize("@ss.hasPermission('qms:business-assay-task-data:delete')")
    public CommonResult<Boolean> deleteBusinessAssayTaskDataList(@RequestBody BatchDeleteReqVO req) {
        businessAssayTaskDataService.deleteBusinessAssayTaskDataListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得子样检测任务业务")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    //@PreAuthorize("@ss.hasPermission('qms:business-assay-task-data:query')")
    public CommonResult<BusinessAssayTaskDataRespVO> getBusinessAssayTaskData(@RequestParam("id") Long id) {
        BusinessAssayTaskDataDO businessAssayTaskData = businessAssayTaskDataService.getBusinessAssayTaskData(id);
        return success(BeanUtils.toBean(businessAssayTaskData, BusinessAssayTaskDataRespVO.class));
    }
    
    @GetMapping("/getUnAssayTaskGroupList")
    @Operation(summary = "获得未指派的子样检测任务业务分组列表")
    //@PreAuthorize("@ss.hasPermission('qms:business-assay-task-data:query')")
    public CommonResult<?> getUnAssayTaskGroupList(BusinessAssayTaskDataReqVO reqVO) {
    	if (reqVO.getAssayDepartmentId() == null) {
    		LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
            reqVO.setAssayDepartmentId(loginUser.getVisitDeptId());
		}
    	List<BusinessAssayTaskDataGroupRespVO> list = businessAssayTaskDataService.getUnAssayTaskGroupList(reqVO);
		return success(list);
	}
    
    @GetMapping("/getUnAssayTaskGroupTypeList")
    @Operation(summary = "获得未指派的子样检测任务业务分组列表")
    //@PreAuthorize("@ss.hasPermission('qms:business-assay-task-data:query')")
    public CommonResult<?> getUnAssayTaskGroupTypeList(BusinessAssayTaskDataReqVO reqVO) {
    	if (reqVO.getAssayDepartmentId() == null) {
    		LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
            reqVO.setAssayDepartmentId(loginUser.getVisitDeptId());
		}
    	List<BusinessAssayTaskDataGroupRespVO> list = businessAssayTaskDataService.getUnAssayTaskGroupTypeList(reqVO);
		return success(list);
	}

    @GetMapping("/getUnAuditTaskGroupList")
    @Operation(summary = "获得未审核的子样检测任务业务分组列表")
    //@PreAuthorize("@ss.hasPermission('qms:business-assay-task-data:query')")
    public CommonResult<?> getUnAuditTaskGroupList(BusinessAssayTaskDataReqVO reqVO) {
    	if (reqVO.getAssayDepartmentId() == null) {
    		LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
            reqVO.setAssayDepartmentId(loginUser.getVisitDeptId());
		}
    	List<BusinessAssayTaskDataGroupRespVO> list = businessAssayTaskDataService.getUnAuditTaskGroupList(reqVO);
		return success(list);
    }

    @GetMapping("/list")
    @Operation(summary = "获得子样检测任务业务列表")
    //@PreAuthorize("@ss.hasPermission('qms:business-assay-task-data:query')")
    public CommonResult<List<BusinessAssayTaskDataExtendRespVO>> getBusinessAssayTaskDataPage(@Valid BusinessAssayTaskDataReqVO reqVO) {
        List<BusinessAssayTaskDataExtendRespVO> listResult = businessAssayTaskDataService.getBusinessAssayTaskDataList(reqVO);
        return success(listResult);
    }

    @GetMapping("/page")
    @Operation(summary = "获得子样检测任务业务分页")
    //@PreAuthorize("@ss.hasPermission('qms:business-assay-task-data:query')")
    public CommonResult<PageResult<BusinessAssayTaskDataExtendRespVO>> getBusinessAssayTaskDataPage(@Valid BusinessAssayTaskDataPageReqVO pageReqVO) {
        PageResult<BusinessAssayTaskDataExtendRespVO> pageResult = businessAssayTaskDataService.getBusinessAssayTaskDataPage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出子样检测任务业务 Excel")
    //@PreAuthorize("@ss.hasPermission('qms:business-assay-task-data:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportBusinessAssayTaskDataExcel(@Valid BusinessAssayTaskDataPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BusinessAssayTaskDataExtendRespVO> list = businessAssayTaskDataService.getBusinessAssayTaskDataPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "子样检测任务业务.xls", "数据", BusinessAssayTaskDataRespVO.class,
                        BeanUtils.toBean(list, BusinessAssayTaskDataRespVO.class));
    }

}