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

import com.alibaba.fastjson.JSONObject;
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.BusinessSubSampleDO;
import com.zt.plat.module.qms.business.bus.service.BusinessAssayTaskDataService;
import com.zt.plat.module.qms.business.bus.service.BusinessSubSampleService;
import com.zt.plat.module.qms.enums.QmsCommonConstant;
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-sub-sample")
@Validated
@DeptDataPermissionIgnore(enable = "true")
public class BusinessSubSampleController implements BusinessControllerMarker {


    @Resource
    private BusinessSubSampleService businessSubSampleService;
    
    @Resource
    private BusinessAssayTaskDataService businessAssayTaskDataService;

    @PostMapping("/create")
    @Operation(summary = "创建子样业务")
    //@PreAuthorize("@ss.hasPermission('qms:business-sub-sample:create')")
    public CommonResult<BusinessSubSampleRespVO> createBusinessSubSample(@Valid @RequestBody BusinessSubSampleSaveReqVO createReqVO) {
        return success(businessSubSampleService.createBusinessSubSample(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新子样业务")
    //@PreAuthorize("@ss.hasPermission('qms:business-sub-sample:update')")
    public CommonResult<Boolean> updateBusinessSubSample(@Valid @RequestBody BusinessSubSampleSaveReqVO updateReqVO) {
        businessSubSampleService.updateBusinessSubSample(updateReqVO);
        return success(true);
    }

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

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

    @GetMapping("/get")
    @Operation(summary = "获得子样业务")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    //@PreAuthorize("@ss.hasPermission('qms:business-sub-sample:query')")
    public CommonResult<BusinessSubSampleExtendRespVO> getBusinessSubSample(@RequestParam("id") Long id) {
//        BusinessSubSampleDO businessSubSample = businessSubSampleService.getBusinessSubSample(id);
//        return success(BeanUtils.toBean(businessSubSample, BusinessSubSampleRespVO.class));
    	BusinessSubSampleExtendRespVO result = businessSubSampleService.getBusinessSubSample(id);
    	return success(result);
    }

    @GetMapping("/getPrintSubSample")
    @Operation(summary = "获得打印子样业务")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<BusinessSubSampleExtendRespVO> getPrintSubSample(@RequestParam("id") Long id) {
    	BusinessSubSampleExtendRespVO result = businessSubSampleService.getPrintSubSample(id);
    	return success(result);
    }

    @GetMapping("/list")
    @Operation(summary = "获得子样业务列表")
    //@PreAuthorize("@ss.hasPermission('qms:business-sub-sample:query')")
    public CommonResult<?> getBusinessSubSampleList(@Valid BusinessSubSampleReqVO reqVO) {
		List<BusinessSubSampleExtendRespVO> listResult = businessSubSampleService.getBusinessSubSampleList(reqVO);
		return success(listResult);
	}

    @GetMapping("/page")
    @Operation(summary = "获得子样业务分页")
    //@PreAuthorize("@ss.hasPermission('qms:business-sub-sample:query')")
    public CommonResult<PageResult<BusinessSubSampleExtendRespVO>> getBusinessSubSamplePage(@Valid BusinessSubSamplePageReqVO pageReqVO) {
        PageResult<BusinessSubSampleExtendRespVO> pageResult = businessSubSampleService.getBusinessSubSamplePage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/page-analysis-group")
    @Operation(summary = "获得子样分析部门分页")
    //@PreAuthorize("@ss.hasPermission('qms:business-sub-sample:query')")
    public CommonResult<PageResult<BusinessSubSampleExtendRespVO>> getAnalysisGroupPage(@Valid BusinessSubSamplePageReqVO pageReqVO) {
    	if (pageReqVO.getAssayDepartmentId() == null) {
            LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
            pageReqVO.setAssayDepartmentId(loginUser.getVisitDeptId());
		}
        PageResult<BusinessSubSampleExtendRespVO> pageResult = businessSubSampleService.getAnalysisGroupPage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/page-stock")
    @Operation(summary = "查询样品库分页数据")
    public CommonResult<PageResult<BusinessSubSampleExtendRespVO>> pageStock(@Valid BusinessSubSamplePageReqVO pageReqVO) {
        PageResult<BusinessSubSampleExtendRespVO> pageResult = businessSubSampleService.getBusinessSubSamplePage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/selectPageForDispatch")
    @Operation(summary = "查询可申请调拨子样")
    public CommonResult<PageResult<BusinessSubSampleExtendRespVO>> selectPageForDispatch(@Valid BusinessSubSamplePageReqVO pageReqVO) {
        PageResult<BusinessSubSampleExtendRespVO> pageResult = businessSubSampleService.selectPageForDispatch(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/page-task")
    @Operation(summary = "获得子样业务分页")
    //@PreAuthorize("@ss.hasPermission('qms:business-sub-sample:query')")
    public CommonResult<PageResult<BusinessSubSampleExtendRespVO>> getBusinessSubSampleAndTaskPage(@Valid BusinessSubSamplePageReqVO pageReqVO) {
        PageResult<BusinessSubSampleExtendRespVO> pageResult = businessSubSampleService.getBusinessSubSamplePage(pageReqVO);
        List<BusinessSubSampleExtendRespVO> list = pageResult.getList();
        for (BusinessSubSampleExtendRespVO businessSubSampleExtendRespVO : list) {
        	BusinessAssayTaskDataReqVO search = new BusinessAssayTaskDataReqVO();
        	search.setBusinessSubSampleId(businessSubSampleExtendRespVO.getId());
        	search.setIsAssignTasked(QmsCommonConstant.NO);
        	List<BusinessAssayTaskDataExtendRespVO> businessAssayTaskDataList = businessAssayTaskDataService.getBusinessAssayTaskDataList(search);
        	businessSubSampleExtendRespVO.setBusinessAssayTaskDataList(businessAssayTaskDataList);
		}
        return success(pageResult);
    }
    
    @GetMapping("/getBySampleCodeAndFlowKey")
    @Operation(summary = "根据样品编号及样品流程节点key获取样品信息")
    public CommonResult<?> getBySampleCodeAndFlowKey(@Valid BusinessSubSampleReqVO reqVO) {
    	List<BusinessSubSampleExtendRespVO> resultList = businessSubSampleService.getBySampleCodeAndFlowKey(reqVO);
    	return success(resultList);
	}

    @GetMapping("/export-excel")
    @Operation(summary = "导出子样业务 Excel")
    //@PreAuthorize("@ss.hasPermission('qms:business-sub-sample:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportBusinessSubSampleExcel(@Valid BusinessSubSamplePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BusinessSubSampleExtendRespVO> list = businessSubSampleService.getBusinessSubSamplePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "子样业务.xls", "数据", BusinessSubSampleRespVO.class,
                        BeanUtils.toBean(list, BusinessSubSampleRespVO.class));
    }

    @PostMapping("/execReturnToStock")
    @Operation(summary = "样品归库")
    public CommonResult<BusinessSubSampleRespVO> execReturnToStock(@Valid @RequestBody BusinessSubSampleSaveReqVO reqVO) {
        return success(businessSubSampleService.execReturnToStock(reqVO));
    }

    @PostMapping("/execTakeOff")
    @Operation(summary = "样品下架")
    public CommonResult<Integer> execTakeOff(@RequestBody JSONObject reqVO) {
        Integer count = businessSubSampleService.execTakeOff(reqVO);
        return success(count);
    }

    @PostMapping("/execChangeLocation")
    @Operation(summary = "库位变更")
    public CommonResult<Integer> execChangeLocation(@RequestBody JSONObject reqVO) {
        Integer count = businessSubSampleService.execChangeLocation(reqVO);
        return success(count);
    }

}