//---------------------------------------------------------------------------
//注意：检查扩展脚本在OnAfterPost()事件中执行!
//---------------------------------------------------------------------------
// String _opacsconnection; 	约定的输入变量名
// String _modalitytype; 		约定的输入变量名
// int _number; 				约定的输入变量名：值1为测试模式，0为非测试。
// String _guid; 				约定的输出变量名
// TADOQuery _qryStudy; 		外部已定义的对象
// TADOQuery _qryCustom; 		外部已定义的对象

//---------------------------------------------------------------------------
String _opacsconnection;	
String _modalitytype;
int _number;
String _guid = "";

//---------------------------------------------------------------------------
void SetArrayItem(Variant& array4, int& i, String& first, String& second, String& third, String& fourth)
{
	array4[0][i] = first;
	array4[1][i] = second;
	array4[2][i] = third;
	array4[3][i] = fourth;
	i++;
}

//---------------------------------------------------------------------------
String AppendGeneralTask(String sTaskName, int iStudyState)
{
    // 添加常规任务
    int iStudyKey = _qryStudy.FieldByName("study_key").AsInteger;

    if (iStudyKey <= 0)
        return "无法定位当前检查！";
    ////
    String sQuery = "select Task.*, Study.* from Task inner join Study on " +
        "(Task.study_key = Study.study_key) where Task.study_key = " +
        IntToStr(iStudyKey) + " order by task_key";
	////
	_qryCustom.Close();
	_qryCustom.SQL.Text = sQuery;
	try
	{
		_qryCustom.Open();
		////
		Variant va[2];
		va[0] = "常规";
		va[1] = sTaskName;
		if (_qryCustom.Locate("task_kind;task_name", va, 0))
			return "常规项目已存在！";
		////
		String sLastDoctorName = GetConfigStr("SysLogon", "sLastDoctorName");
		////
		_qryCustom.Append();
		_qryCustom.FieldByName("study_key").Value = iStudyKey;
		_qryCustom.FieldByName("task_kind").Value = "常规";
		_qryCustom.FieldByName("task_name").Value = sTaskName;
		_qryCustom.FieldByName("task_relationkey").Value = 0;
		_qryCustom.FieldByName("task_sequence").Value = 1;
		_qryCustom.FieldByName("task_priority").Value = 3;
		_qryCustom.FieldByName("task_createuser").Value = sLastDoctorName;
		_qryCustom.FieldByName("task_createdate").Value = Now();
		_qryCustom.FieldByName("task_state").Value = iStudyState;
		_qryCustom.Post();
	}
	except
	{
	  	ShowMessage("常规任务添加失败！");
	  	return "";
	}      
	
	return "常规项目添加成功！";
}
    
//---------------------------------------------------------------------------
// 主程序
//---------------------------------------------------------------------------
{
	int iFieldCount = 0;
	String sFieldPairs[4][100];	
	
	// 参数依次为：	检查类别	检查项目	任务名称	起始状态
	// 任务名称：任务名称必须和任务设置里的任务名称保持一致!
	// 起始状态：0未开始，1已取材，2已包埋，3已制片，4已归档
	SetArrayItem(sFieldPairs, iFieldCount, "组织学",	"常规活检和冰冻", 	"冰冻",		"0");
	SetArrayItem(sFieldPairs, iFieldCount, "组织学",	"常规活检或冰冻", 	"冰冻",		"0");

	String sExamKind = _qryStudy.FieldByName("study_examkind").AsString;
	String sExamName = _qryStudy.FieldByName("study_examname").AsString;
	
	String	sTaskName = "正常";
	int		iStudyState = 0;
	for (int i = 0; i < iFieldCount; i++)
	{
		if ((Pos(sFieldPairs[0][i], sExamKind) > 0) && 
			(Pos(sFieldPairs[1][i], sExamName) > 0))
		{
			sTaskName = sFieldPairs[2][i];
			iStudyState = StrToInt(sFieldPairs[3][i]);
			break;
		}
	}
	/////   	
    _guid = AppendGeneralTask(sTaskName, iStudyState);
}
