package com.zt.plat.module.qms.thirdpartyapi.controller.admin;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.zt.plat.framework.common.pojo.CommonResult;
import com.zt.plat.framework.datapermission.core.annotation.DeptDataPermissionIgnore;
import com.zt.plat.module.qms.business.config.controller.vo.BaseSamplePageReqVO;
import com.zt.plat.module.qms.business.config.controller.vo.BaseSampleRespVO;
import com.zt.plat.module.qms.business.config.controller.vo.MaterialAssayStandardDetailPageReqVO;
import com.zt.plat.module.qms.business.config.controller.vo.MaterialAssayStandardDetailRespVO;
import com.zt.plat.module.qms.business.config.dal.dataobject.MaterialAssayStandardDO;
import com.zt.plat.module.qms.business.config.service.BaseSampleService;
import com.zt.plat.module.qms.business.config.service.MaterialAssayStandardDetailService;
import com.zt.plat.module.qms.business.config.service.MaterialAssayStandardService;
import com.zt.plat.module.qms.thirdpartyapi.controller.vo.ThirdpartySampleEntrustReqVO;
import com.zt.plat.module.qms.thirdpartyapi.service.ThirdpartySampleEntrustService;

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

import java.util.List;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;

/**
 * 样品委托
 */
@Tag(name = "通用样品委托接口")
@RestController
@RequestMapping("/qms/thirdpartyapi/sample-entrust")
@DeptDataPermissionIgnore(enable = "true")
public class ThirdpartySampleEntrustController {

    @Resource
    private BaseSampleService baseSampleService;
    
    @Resource 
    private MaterialAssayStandardService materialAssayStandardService;

    @Resource 
    private MaterialAssayStandardDetailService materialAssayStandardDetailService;
	
	@Resource
	private ThirdpartySampleEntrustService thirdpartySampleEntrustService;

    @GetMapping("/getBaseSampleList")
    @Operation(summary = "获得样品大类管理列表")
    public CommonResult<?> getBaseSampleList(BaseSamplePageReqVO reqVO) {
    	List<BaseSampleRespVO> list = baseSampleService.getBaseSampleList(reqVO);
    	return success(list);
    }
    
    @GetMapping("/getAssayStandardByBaseSampleId")
    @Operation(summary = "根据样品大类id，获取分析标准")
    public CommonResult<?> getAssayStandardByBaseSampleId(@RequestParam("baseSampleId") Long baseSampleId) {
    	List<MaterialAssayStandardDO> standardList = materialAssayStandardService.queryByBaseSampleID(baseSampleId);
        List<Long> standardIds = standardList.stream().map(MaterialAssayStandardDO::getId).toList();
        MaterialAssayStandardDetailPageReqVO params = new MaterialAssayStandardDetailPageReqVO();
        params.setStandardIdList(standardIds);
        List<MaterialAssayStandardDetailRespVO> list = materialAssayStandardDetailService.selectListWithExtend(params);
    	return success(list);
	}
	

    @PostMapping("/create")
    @Operation(summary = "创建委托登记")
    public CommonResult<?> create(@Valid @RequestBody List<ThirdpartySampleEntrustReqVO> sampleEntrustReqVOList) {
    	thirdpartySampleEntrustService.create(sampleEntrustReqVOList);
    	return success("");
    }
}
