package digital.laboratory.platform.entrustment.service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import digital.laboratory.platform.common.core.util.R;
import digital.laboratory.platform.common.mybatis.security.service.DLPUser;
import digital.laboratory.platform.entrustment.dto.EntrustmentDTO;
import digital.laboratory.platform.entrustment.entity.Entrustment;
import digital.laboratory.platform.entrustment.entity.EntrustmentIdentificationMaterial;
import digital.laboratory.platform.entrustment.vo.EntrustmentVO;
import digital.laboratory.platform.sys.entity.SysUser;
import digital.laboratory.platform.sys.vo.entrustment.MarkersVO;
import digital.laboratory.platform.sys.entity.entrustment.Sample;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 委托服务类
 *
 * @author Zhang Xiaolong created at  2022-03-22
 * @describe 委托服务类
 */
public interface EntrustmentService extends IService<Entrustment> {

	/**
	 * ## 委托编码
	 * 用于标识每一次委托。
	 * 有可能一个案件会有多次委托鉴定, 所以在案件编码后加4位作为顺序号。
	 *
	 * ```
	 * WT <案件编码后 22 位> <顺序号4位>
	 * 例如:
	 * WT 5201010400002020030002 0002
	 * 合计 28 位
	 * ```
	 //* @param caseNo
	 * @return
	 */
//	String getNewEntrustmentNo(String caseNo);
	public String getNewEntrustmentNo();
	//生成受理编号
	public String getNewAcceptNo();
	List<EntrustmentVO> getEntrustmentVOList(QueryWrapper<Entrustment> qw);
	IPage<EntrustmentVO> getEntrustmentVOPage(IPage<Entrustment> page, QueryWrapper<Entrustment> qw);
	EntrustmentVO getEntrustmentVOById(String id);

	/**
	 * 查询任务
	 * @param userId
	 * @param group
	 * @param processInstanceId
	 * @param businessKey
	 * @return
	 */
	//List<Task> taskQuery(String userId, String group, String processInstanceId, String businessKey);

	/**
	 * 获取任务列表
	 * 查询条件可以是 userId, groupId, processInstanceId, busincessKey
	 */
	//List<TaskResponse> taskList(String entrustmentId, String userId);

	/**
	 * 认领任务
	 * 只有 Assignee 为空才能认领, unclaim 反认领可以置 Assignee 为空
	 * @param entrustmentId
	 * @return
	 */
	//HashMap<String, String> taskClaim(String entrustmentId);

	/**
	 * 反认领任务
	 * @param entrustmentId
	 * @return
	 */
	//HashMap<String, String> taskUnclaim(String entrustmentId);

	/**
	 * 委派任务
	 * 把任务指定给某个用户, 与 claim 不同之处于无论之前 assignee 是否为空都可以委派。
	 * 可以在某个用户长期不作为的情况下重新委派给其他人
	 * @param entrustmentId
	 * @param userId
	 * @return
	 */
	//HashMap<String, String> taskAssigneeSet(String entrustmentId, String userId);

	/**
	 * 提交任务
	 * @param entrustmentId 委托ID
	 * @param opCode 操作码: 1=通过, -1=拒绝(驳回), 2...=其他路径
	 */
	//HashMap<String, Object> taskApply(String entrustmentId, Integer opCode);


	/**
	 * 取指定环节可用的用户列表
	 * 当流程进行到某个环节的时候, 需要某个用户对这个环节进行处理(通过或不通过)。
	 * 这里取可用的用户列表
	 * <p>
	 * 涉及到的环境有以下几个: 
	 * 1、创建委托及提交
	 * 委托的提交者就是委托的创建者。不存在不通过的可能。
	 * 2、审核
	 * 审核者有几个条件: (1)必须是鉴定中心的工作人员 (2)必须拥有委托审核权限
	 * 3、审批
	 * 审核者有几个条件: (1)必须是鉴定中心的工作人员 (2)必须拥有委托审批权限
	 * 4、送检确认
	 * 送检确认者有几个条件: (1)必须与委托的创建者是同一个机构, 或上级机构的人 (2)必须拥有委托送检确认权限
	 * 5、受理
	 * 受理者有几个条件: (1)必须是鉴定中心的工作人员 (2)必须拥有委托受理权限
	 *
	 *
	 * @return
	 */
	List<SysUser> taskAvailableUserList_Check();
	List<SysUser> taskAvailableUserList_Approve();
	List<SysUser> taskAvailableUserList_Confirm(String clientOrgId);
	List<SysUser> taskAvailableUserList_Accept();
//	List<SysUser> taskAvailableUserList(String entrustmentId);
	public long countByCaseId(String caseId);
	void Entrustment2Word(EntrustmentVO ev) throws IOException;
//	void Entrustment2Word(EntrustmentVO ev, InputStream tempStream, OutputStream) throws IOException;

	//初始化送检受理系统数据
	void initCaseSendAccept();
	//生成鉴定事项确认书
	Boolean generateIdentifyItemsBook(String entrustId);
	/**
	 *获取文书名称
	 * @param entrustId
	 * @param whatBook  entrust-委托书  identItemBook-鉴定事项确认书  identfyBook-鉴定书
	 * @return
	 */
	String getBookNameByEntrustId(String entrustId,String whatBook);

	/**
	 * 构建鉴定要求
	 * @param materialList
	 * @return
	 */
	public String buildIdentfyReq(List<EntrustmentIdentificationMaterial> materialList);
	/**
	 * 构造委托书中的鉴定要求
	 *
	 * @param materialList
	 * @return
	 */
	public String buildEntrustReq(List<EntrustmentIdentificationMaterial> materialList);
	/**
	 * 构建检材描述
	 * @param materialList
	 * @return
	 */
	public String buildMaterialDes(List<EntrustmentIdentificationMaterial> materialList);
	//检验完成
	public boolean testsFinish(String entrustId);
	//查询委托各个状态的数量
	public  Integer getEntrustStatusCount(List<Integer> statues, DLPUser dlpUser);
	//获取案前委托的委托编号
	String getNewEntrustAcceptNoForBeforeCase();
	//根据年份获取当年的最大号
	String getNewEntrustNoByYear(String year);
	//合并送检人员的证件照
	Boolean getDeliverMergePhoto(String entrustId,String acceptNo,String caseName,String deliverer1Name,String deliverer2Name);
	//检查送检人的证件照片是否上传
	Boolean checkDeliverPhoto(String entrustId);
	//获取委托下的
//	String  getMaterialMergePhoto(String entrustId, Integer  groupCount);
	//处理历史数据到流转系统中
	Boolean historyDataToTransSys(String status,List<String> entrustIds);
	//检查检材是否已经复秤
	boolean checkRepeatWeigh(Entrustment entrustment);
	//同步的历史数据补充缺失的字段
	Boolean updateImportData(String flag);

    Entrustment bizSubmitter_Save(Entrustment entrust, DLPUser dlpUser);

	Entrustment bizSubmitter_Apply(Entrustment entrust, DLPUser dlpUser);

	Entrustment bizChecker_Claim( Entrustment entrust,DLPUser dlpUser);

	Entrustment bizChecker_Apply(Entrustment entrust, Integer opCode, DLPUser dlpUser);

	Entrustment bizApprover_Claim(Entrustment entrust, DLPUser dlpUser);

	Entrustment bizApprover_Apply(Entrustment entrust, Integer opCode, DLPUser dlpUser);

	Entrustment bizConfirm_Apply(Entrustment entrust, Integer opCode, DLPUser dlpUser);

	Entrustment bizDeliver_Save(Entrustment entrust, DLPUser dlpUser);

	Entrustment bizDeliver_Apply(Entrustment entrust, Integer opCode, DLPUser dlpUser);

	String  bizGetPDFEntrustmentLetter(String id, HttpServletRequest theHttpServletRequest, HttpServletResponse httpServletResponse);

	Entrustment bizAccept_Save(Entrustment entrust, DLPUser dlpUser);

	Entrustment bizAccept_Apply(Entrustment entrust, Integer opCode, DLPUser dlpUser);

	List<Sample> getTransferMaterialList(DLPUser dlpUser, Entrustment entrustment);

	void bizGetPDFIdentifyItemsConfirmLetter(String id, String pdfOrWord, HttpServletRequest theHttpServletRequest, HttpServletResponse httpServletResponse);

	void GenerateIdentifyItemsConfirmLetterPDF(EntrustmentVO ev);

	Entrustment bizSubmitter_AddEntrustment(Entrustment entrustment, DLPUser dlpUser);

	R deleteById(String id, DLPUser dlpUser);

	R uploadAttachmentObj_Base64(String entrustmentId, JSONObject jsonParam) throws Exception;

	R uploadDeliverPhoto_Base64(String entrustmentId, JSONObject jsonParam);

	R uploadAcceptAttachmentObj_Base64(String entrustmentId, JSONObject jsonParam) throws Exception;

	R<List<SysUser>> bizGetAvailableUserList(String entrustmentId);

	List<MarkersVO>  getImQuantityForInspection(DLPUser dlpUser);

	Entrustment addNewEmt(EntrustmentDTO entrustmentDTO, DLPUser dlpUser);

	List<MarkersVO>  getInspectMarkers(DLPUser dlpUser);

	List<MarkersVO> getImQuantityForAccept();

	List<MarkersVO> getMarkersForOther(DLPUser dlpUser);

	List<MarkersVO> getMarkersForEntrustment();

    void printSampleFile(String entrustmentId) throws Exception;

	boolean synchronizationByMaterialType();

    IPage getEntrustmentAndMaterial(Page page, Integer status,String keywords);

	boolean isMaterialPhoto(String entrustmentId);
}
