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.entrustment.vo.MaterialListForBookVo;
import digital.laboratory.platform.sys.entity.Deliverer;
import digital.laboratory.platform.sys.entity.SysUser;
import digital.laboratory.platform.sys.entity.entrustment.Sample;
import digital.laboratory.platform.sys.vo.entrustment.MarkersVO;

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

/**
 * 委托服务类
 *
 * @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);

	public long countByCaseId(String caseId);
	void Entrustment2Word(EntrustmentVO ev) throws IOException;

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

	/**
	 * 构建鉴定要求
	 * @param materialList
	 * @return
	 */
	public String buildIdentfyReq(List<EntrustmentIdentificationMaterial> materialList);
	/**
	 * 构造委托书中的鉴定要求
	 *
	 * @param materialList
	 * @return
	 */
	public String buildEntrustReq(List<EntrustmentIdentificationMaterial> materialList);

	//构造鉴定事项确认书文书中的检材列表
	List<MaterialListForBookVo> getMaterialBookVoList(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);

	/**
	 * 委托创建人送检提交
	 * @param entrust
	 * @param opCode
	 * @param dlpUser
	 * @return
	 */
	Entrustment bizDeliver_Apply(Entrustment entrust, Integer opCode, DLPUser dlpUser);

	/**
	 * 下载鉴定委托书
	 * @param id
	 * @param theHttpServletRequest
	 * @param httpServletResponse
	 * @return
	 */
	String  bizGetPDFEntrustmentLetter(String id, HttpServletRequest theHttpServletRequest, HttpServletResponse httpServletResponse);

	/**
	 * 构建委托函数据映射
	 *
	 * @param ev 委托VO对象
	 * @return 数据映射
	 * @throws IllegalAccessException 如果访问对象的属性或方法时发生非法访问异常
	 */
	Map<String, Object> buildEntrustLetterDataMap(EntrustmentVO ev) throws IllegalAccessException;

	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);

	/**
	 * 根据委托时间去修改一些时间类型的字段信息，例如 submit_time、check_claim_time、check_time、approve_claim_time、approve_time、"
	 * deliver_confirm_time、accept_claim_time、accept_time、create_time以及process_info字段中的time类型的数据
	 * @param entrustment
	 * @return
	 */
    boolean alertTimeFiledInfoByEntrustDate(Entrustment entrustment);

	/**
	 * 判断当前保存的委托检材数量是否符合，决定是否可以提交委托审核
	 * @param entrustId 委托id
	 * @param materialCount 当前用户所在的检材页面检材数量
	 * @return
	 */
    Boolean isEligibleForSubmission(String entrustId, Integer materialCount);

	List<Deliverer> getDelivererList(DLPUser dlpUser);

	void previewEntrustLetterPDF(String entrustId, HttpServletResponse servletResponse) throws Exception;

	void previewEntrustPDF(String entrustId, HttpServletResponse servletResponse) throws Exception;
}
