时间:2021-07-01 10:21:17 帮助过:4人阅读
定义classmethod名字是setdata,括号内为传入参数及类型,设置return初始值为0,然后调用routine里的SetData函数(规定调用成功返回1,在routine中设置),退出函数并返回Ret
.mac文件
#Include %occStatus
#Include Ps.Plan.Storage
#Include ChroDiseaConst
#Include Cm.RevisionInfo.Storage
SetData(PlanNo, PatientId, StartDate, EndDate, Module, Status, DoctorId, UserId, TerminalName, TerminalIP, DeviceType) Public
{
Set Ret = $$$SetDataFailed
Set key = PlanNo
Set oref = ##Class("Ps.Plan").%OpenId(key)
If ($IsObject(oref) = 0)
{
Set oref = ##Class("Ps.Plan").%New()
}
If ($IsObject(oref) = 1)
{
Set oref.PlanNo = PlanNo
Set oref.PatientId = PatientId
Set oref.StartDate = StartDate
Set oref.EndDate = EndDate
Set oref.Module = Module
Set oref.Status = Status
Set oref.DoctorId = DoctorId
Set RevisionInfo = $$GetNewRevisionInfo^Cm.RevisionInfo(UserId, TerminalName, TerminalIP, DeviceType)
Set oref.RevisionInfo.DateTime = $ListGet(RevisionInfo, $$$CmRevisionInfoDateTime)
Set oref.RevisionInfo.UserId = $ListGet(RevisionInfo, $$$CmRevisionInfoUserId)
Set oref.RevisionInfo.UserName = $ListGet(RevisionInfo, $$$CmRevisionInfoUserName)
Set oref.RevisionInfo.TerminalName = $ListGet(RevisionInfo, $$$CmRevisionInfoTerminalName)
Set oref.RevisionInfo.TerminalIP = $ListGet(RevisionInfo, $$$CmRevisionInfoTerminalIP)
Set oref.RevisionInfo.DeviceType = $ListGet(RevisionInfo, $$$CmRevisionInfoDeviceType)
Set sc = oref.%Save()
If $$$ISOK(sc)
{
Set Ret = $$$SetDataSuccess
}
else
{
Set ErrMsg = $System.Status.GetErrorText(sc,"")
Do WriteServerLog^Cm.CommonLibrary("","",ErrMsg)
}
Kill oref
}
Quit Ret
}
这里,ChroDiseaConst设置了工程中用到的所有状态变量,如代码中的ISOK等;Ps.Plan.Storage.inc文件设置了对应这张表的各个属性的宏文件,将调用数组元素时使用的数字设置为对应变量名,使代码易读;Cm.RevisionInfo.Storage也是相同的作用
首先设置返回初始值,设置主键,若已存在则打开这个主键对应的值,否则新建;设置各个属性的值为传入的参数;判断是否成功,否则报错;最后释放缓存并返回Ret退出。
.inc文件
#Define PsPlanD "^Ps.PlanD"
#Define PsPlanI "^Ps.PlanI"
#Define PsPlanPatientId 2
#Define PsPlanStartDate 3
#Define PsPlanEndDate 4
#Define PsPlanModule 5
#Define PsPlanStatus 6
#Define PsPlanDoctorId 7
#Define PsPlanRevisionInfo 8
定义宏
==================================================
添加方法(简单查表):
.cls:
ClassMethod GetPatientPlan(PlanNo As %String) As %List
{
Set Ret = ""
Set Ret = $$GetPatientPlan^Ps.Plan(PlanNo)
Quit Ret
}
输入一个参数(PlanNo)返回符合查找条件的一个列表。首先置初值为空,然后调用routine,最后返回退出。
.mac:
GetPatientPlan(PlanNo) Public
{
Set Ret = ""
Set Data = $Get(@$$$PsPlanD@(PlanNo))
If (Data ‘= "")
{
Set PatientId = $ListGet(Data,$$$PsPlanPatientId)
Set StartDate = $ListGet(Data,$$$PsPlanStartDate)
Set EndDate = $ListGet(Data,$$$PsPlanEndDate)
Set Module = $ListGet(Data,$$$PsPlanModule)
Set Status = $ListGet(Data,$$$PsPlanStatus)
Set DoctorId = $ListGet(Data,$$$PsPlanDoctorId)
Set RevisionInfo = $ListGet(Data,$$$PsPlanRevisionInfo)
Set DateTime = $ListGet(RevisionInfo, $$$CmRevisionInfoDateTime)
Set UserId = $ListGet(RevisionInfo, $$$CmRevisionInfoUserId)
Set UserName = $ListGet(RevisionInfo, $$$CmRevisionInfoUserName)
Set TerminalName = $ListGet(RevisionInfo, $$$CmRevisionInfoTerminalName)
Set TerminalIP = $ListGet(RevisionInfo, $$$CmRevisionInfoTerminalIP)
Set DeviceType = $ListGet(RevisionInfo, $$$CmRevisionInfoDeviceType)
Set RevisionInfoRet = $ListBuild(DateTime, UserId, UserName, TerminalName, TerminalIP, DeviceType)
Set Ret = $ListBuild(PatientId, StartDate, EndDate, Module, Status, DoctorId, RevisionInfoRet)
}
Quit Ret
}
同样的,先置初值,然后从D表中查找搜索条件(本例中因为用主键查找,因此不需要索引),然后设置返回的列表内容,最后build列表并返回。
=================================================
Terminal的使用:
命令行操作,用于对数据库进行指令操作或测试写过的方法。
如测试classmethod可以输入命令
w ##class(Ps.Plan).SetData(“xx”, “yy”, …)
测试query
do TestGetPlan^Ps.Plan()
(在routine中应有对应的测试方法TestGetPlan() )
注意,首次打开需要输入用户名密码,每次打开都要更改到对应命名空间:
zn “namespace”
Caché数据库学习笔记(2)
标签: