package com.qmth.sop.business.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import com.qmth.sop.business.bean.params.*;
import com.qmth.sop.business.bean.result.*;
import com.qmth.sop.business.entity.*;
import com.qmth.sop.common.enums.ServiceScopeEnum;
import com.qmth.sop.common.enums.SopStatusEnum;
import com.qmth.sop.common.enums.TFCustomTypeEnum;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * sop表 服务类
 * </p>
 *
 * @author wangliang
 * @since 2023-08-01
 */
public interface TBSopInfoService extends IService<TBSopInfo> {

    /**
     * 查询动态sop表名是否存在
     *
     * @param db
     * @param tableName
     * @return
     */
    int selectSopDynamicExist(String db, String tableName);

    /**
     * 保存动态sop信息
     *
     * @param tfCustomFlow
     * @param code
     * @param flowId
     * @param flowTaskResult
     * @return
     */
    Boolean saveDynamicSop(TFCustomFlow tfCustomFlow, String code, Long flowId, FlowTaskResult flowTaskResult);

    /**
     * 查询动态sop数据
     *
     * @param tableName
     * @param flowDeploymentId
     * @param flowId
     * @return
     */
    int selectDynamicSop(String tableName, String flowDeploymentId, Long flowId);

    /**
     * sop发布信息
     *
     * @param sopPublishParam
     * @return
     */
    String sopPublish(SopPublishParam sopPublishParam) throws Exception;

    /**
     * sop填报信息
     *
     * @param flowApproveParam
     * @return
     */
    Boolean sopApprove(FlowApproveParam flowApproveParam) throws InterruptedException;

    /**
     * 系统自动审批
     *
     * @param tfCustomFlow
     * @param tfCustomFlowEntity
     * @param tfFlowApprove
     * @param crmProjectResult
     * @param crmNo
     * @param crmDetailId
     * @throws InterruptedException
     */
    public void sopSystemApprove(TFCustomFlow tfCustomFlow, TFCustomFlowEntity tfCustomFlowEntity,
            TFFlowApprove tfFlowApprove, CrmProjectResult crmProjectResult, String crmNo, Long crmDetailId)
            throws InterruptedException;

    /**
     * 代理自动审批
     *
     * @param flowId
     * @param sopNo
     */
    public void sopProxySystemApprove(Long flowId, String sopNo) throws InterruptedException;

    /**
     * sop管理列表
     *
     * @param sopInfoListParam
     * @return
     */
    IPage<Map> list(SopInfoListParam sopInfoListParam);

    /**
     * 根据crmNo查询在途的sop信息
     *
     * @param crmNo
     * @return
     */
    List<String> findFlowPassageByCrmNo(String crmNo);

    /**
     * 根据serviceId查询所有sop信息
     *
     * @param serviceId
     * @return
     */
    List<SopInfoResult> findFlowByServiceId(Long serviceId);

    /**
     * 保存提醒/延时任务
     *
     * @param map
     */
    public void saveJobRemind(Map<String, Object> map) throws InterruptedException;

    /**
     * 更新sop计划进场开始时间和结束时间
     *
     * @param flowTaskResult
     * @param sopId
     * @param flowId
     * @param userId
     * @param type
     * @throws InterruptedException
     */
    public void saveSopPlanDate(FlowTaskResult flowTaskResult, Long sopId, Long flowId, Long userId,
            TFCustomTypeEnum type) throws InterruptedException;

    /**
     * sop编辑
     *
     * @param id
     * @return
     */
    public FlowResult sopEdit(Long id) throws InterruptedException;

    /**
     * sop保存
     *
     * @param sopSaveParam
     * @return
     */
    public Boolean sopSave(SopSaveParam sopSaveParam) throws InterruptedException;

    /**
     * 获取表单内容
     *
     * @param tfCustomFlowEntity
     * @param setup
     * @return
     */
    public FlowTaskResult getFormProperties(TFCustomFlowEntity tfCustomFlowEntity, Integer setup);

    /**
     * 设置流程第一步值
     *
     * @param sopPublishParam
     * @param tfCustomFlow
     * @param crmProjectResult
     * @return
     * @throws Exception
     */
    public SopPublishParam setSetupOne(SopPublishParam sopPublishParam, TFCustomFlow tfCustomFlow,
            CrmProjectResult crmProjectResult) throws Exception;

    /**
     * 修改流程第一步值
     *
     * @param flowTaskResult
     * @param tfCustomFlow
     * @param processVar
     * @param tbCrmDetail
     * @param crmProjectResult
     * @return
     */
    public FlowTaskResult updateSetupOne(FlowTaskResult flowTaskResult, TFCustomFlow tfCustomFlow, String processVar,
            TBCrmDetail tbCrmDetail, CrmProjectResult crmProjectResult);

    /**
     * sop转审
     *
     * @param crmDetailId
     * @param sopAllocationParam
     * @return
     */
    Boolean sopApproverExchange(Long crmDetailId, SopAllocationParam sopAllocationParam) throws InterruptedException;

    /**
     * 查询sop状态
     *
     * @param sopNo
     * @return
     */
    SopStatusEnum findBySopNo(String sopNo);

    /**
     * 查询sop信息
     *
     * @param sopNo
     * @return
     */
    TBSopInfo findByCode(String sopNo);

    /**
     * sop人员配置
     *
     * @param sopAllocationParam
     * @return
     */
    Boolean sopAllocation(SopAllocationParam sopAllocationParam) throws InterruptedException;

    /**
     * 修改流程第一步
     *
     * @param flowId
     * @param crmDetailId
     * @param engineerUserIdList
     * @param regionCoordinatorUserId
     * @param projectManagerUserId
     * @return
     */
    Boolean updateFlowSetupOne(Long flowId, Long crmDetailId, List<String> engineerUserIdList,
            String regionCoordinatorUserId, String projectManagerUserId);

    /**
     * 更新流程信息
     *
     * @param allocationParamList
     * @param tfCustomFlow
     * @param tfCustomFlowEntity
     * @param tfFlowApprove
     * @param tbCrmDetail
     * @return
     * @throws InterruptedException
     */
    Boolean updateFlowInfo(List<AllocationParam> allocationParamList, TFCustomFlow tfCustomFlow,
            TFCustomFlowEntity tfCustomFlowEntity, TFFlowApprove tfFlowApprove, TBCrmDetail tbCrmDetail)
            throws InterruptedException;

    /**
     * 查询服务范围
     *
     * @param type
     * @param flowTaskResult
     * @return
     */
    ServiceScopeEnum findServiceScope(TFCustomTypeEnum type, FlowTaskResult flowTaskResult);

    /**
     * 根据sop单号查询sop计划时间
     *
     * @param sopNo
     * @return
     */
    SopPlanTimeResult querySopPlanTime(String sopNo);

    /**
     * 根据sopId查询用户
     *
     * @param id
     * @return
     */
    List<SysUser> userList(Long id);
}
