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

import org.springframework.validation.annotation.Validated;
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.RestController;

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

import java.util.Arrays;
import java.util.List;

import com.zt.plat.framework.business.interceptor.BusinessControllerMarker;
import com.zt.plat.framework.common.pojo.CommonResult;
import com.zt.plat.framework.common.pojo.vo.BatchDeleteReqVO;
import com.zt.plat.framework.datapermission.core.annotation.DeptDataPermissionIgnore;
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.liteflow.param.SampleTaskAssignManualParam;
import com.zt.plat.module.qms.business.bus.liteflow.param.SampleTaskAssignManualTypeParam;
import com.zt.plat.module.qms.business.bus.liteflow.param.SampleTaskAssignMethodParam;
import com.zt.plat.module.qms.business.bus.liteflow.param.SampleTaskAssignProjectMethodParam;
import com.zt.plat.module.qms.business.bus.liteflow.param.SampleTaskAssignProjectNoMethodParam;
import com.zt.plat.module.qms.business.bus.liteflow.param.SampleTaskAssignSampleParam;
import com.zt.plat.module.qms.business.bus.service.SampleTaskAssignService;
import com.zt.plat.module.system.api.user.dto.AdminUserRespDTO;

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

@Tag(name = "管理后台 - 样品任务分配")
@RestController
@RequestMapping("/qms/bus/sample/task-assign")
@Validated
@DeptDataPermissionIgnore(enable = "true")
public class SampleTaskAssignController implements BusinessControllerMarker {
	
	@Resource
	private SampleTaskAssignService sampleTaskAssignService;
	
	@Operation(description = "获取分配人员列表")
	@GetMapping("/getAssignUserList")
	public CommonResult<?> getAssignUserList() {
		List<AdminUserRespDTO> list = sampleTaskAssignService.getAssignUserList();
		return success(list);
	}

	@Operation(description = "根据用户id获取用户信息")
	@GetMapping("/getUserInfoById")
	public CommonResult<?> getUserInfoById(Long userId) {
		AdminUserRespDTO userRespDTO = sampleTaskAssignService.getUserInfoById(userId);
		return success(userRespDTO);
	}
	
	@GetMapping("/getAssaySampleList")
	public CommonResult<?> getAssaySampleList(BusinessAssayTaskDataReqVO reqVO) {
    	if (reqVO.getAssayDepartmentId() == null) {
    		LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
            reqVO.setAssayDepartmentId(loginUser.getVisitDeptId());
		}
		List<UnAssignTaskedSubSampleRespVO> list = sampleTaskAssignService.getAssaySampleList(reqVO);
		return success(list);
	} 
	
	@GetMapping("/getAssayMethodProjectList")
	public CommonResult<?> getAssayMethodProjectList(String businessSubSampleIds, Long configAssayMethodId) {
		List<Long> businessSubSampleIdList = Arrays.asList(businessSubSampleIds.split(",")).stream().map(Long::parseLong).toList();
		List<AssayMethodProjectRespVO> list = sampleTaskAssignService.getAssayMethodProjectList(businessSubSampleIdList, configAssayMethodId);
		return success(list);
	}
	
	@GetMapping("/getMaterialAssayStandardMethodProjectList")
	public CommonResult<?> getMaterialAssayStandardMethodProjectList(String businessSubSampleIds, Long baseSampleId, Long configAssayMethodId) {
		List<Long> businessSubSampleIdList = Arrays.asList(businessSubSampleIds.split(",")).stream().map(Long::parseLong).toList();
		List<AssayMethodProjectRespVO> list = sampleTaskAssignService.getMaterialAssayStandardMethodProjectList(businessSubSampleIdList, baseSampleId, configAssayMethodId);
		return success(list);
	}

    @PostMapping("/changeMethod")
    public CommonResult<?> changeMethod(@RequestBody ChangeAssayMethodReqVO req) {
    	sampleTaskAssignService.changeMethod(req);
    	return success("成功");
    }
	
    /**
     * 按方法分配
     * @param param
     * @return
     */
    @PostMapping("/methodAssign")
    public CommonResult<?> methodAssign(@RequestBody SampleTaskAssignMethodParam param) {
    	sampleTaskAssignService.methodAssign(param);
    	return success("成功");
    }

    /**
     * 手动分配
     * @param param
     * @return
     */
    @PostMapping("/manualAssign")
    public CommonResult<?> manualAssign(@RequestBody SampleTaskAssignManualParam param) {
    	sampleTaskAssignService.manualAssign(param);
    	return success("成功");
    }

    /**
     * 手动类型分配
     * @param param
     * @return
     */
    @PostMapping("/manualTypeAssign")
    public CommonResult<?> manualTypeAssign(@RequestBody SampleTaskAssignManualTypeParam param) {
    	sampleTaskAssignService.manualTypeAssign(param);
    	return success("成功");
    }

    /**
     * 按检测项目分配(已有方法)
     * @param param
     * @return
     */
    @PostMapping("/projectMethodAssign")
    public CommonResult<?> projectMethodAssign(@RequestBody SampleTaskAssignProjectMethodParam param) {
    	sampleTaskAssignService.projectMethodAssign(param);
    	return success("成功");
	}
    
    /**
     * 按检测项目分配（无方法）
     * @param param
     * @return
     */
    @PostMapping("/projectNoMethodAssign")
    public CommonResult<?> projectNoMethodAssign(@RequestBody SampleTaskAssignProjectNoMethodParam param) {
    	sampleTaskAssignService.projectNoMethodAssign(param);
    	return success("成功");
	}
	
    /**
     * 按样品分配
     * @param param
     * @return
     */
    @PostMapping("/sampleAssign")
    public CommonResult<?> sampleAssign(@RequestBody SampleTaskAssignSampleParam param) {
    	sampleTaskAssignService.sampleAssign(param);
    	return success("成功");
    }
    
    @PostMapping("/removeAssignTaskDetail")
    public CommonResult<?> removeAssignTaskDetail(@RequestBody BatchDeleteReqVO req) {
    	sampleTaskAssignService.removeAssignTaskDetail(req);
    	return success("成功");
    }

	@PostMapping("/createQcSample")
	public CommonResult<?> createQcSample(@RequestBody CreateQcSampleReqVO req) {
		sampleTaskAssignService.createQcSample(req);
		return success("成功");
	}
	
	@PostMapping("/deleteQcSample")
	public CommonResult<?> deleteQcSample(@RequestBody DeleteQcSampleReqVO req) {
		sampleTaskAssignService.deleteQcSample(req);
		return success("成功");
	}
    
    @PostMapping("/submitAssign")
    public CommonResult<?> submitAssign(Long id) {
    	sampleTaskAssignService.submitAssign(id);
    	return success("成功");
    }
    
    /**
     * 作废
     * @param id
     * @return
     */
    @PostMapping("/voidAssign")
    public CommonResult<?> voidAssign(Long id) {
    	sampleTaskAssignService.voidAssign(id);
    	return success("成功");
    }

}
