M实现的增删改查代码生成器

对一些不复杂的维护界面,每次写维护页面要写他的增删改查M。基本是重复的体力劳动,写多了实在是写烦了,就实现一个方法,传入表名自动生成增删改查的M类代码和调用C#代码,开减少工作量。实际很多重复性的代码都能统一生成,分享给大家作为实现自动构造代码的示例。

实现类:

Class Demo.CodeGener Extends %RegisteredObject
{

/// 构造表的增删改查M类代码,以简化常规表M代码和C#调用代码工作量
/// 编码格式改WriteLineWithCode
/// w ##class(Demo.CodeGener).MakeTableCUIDQCode("BT_Bottle")
ClassMethod MakeTableCUIDQCode(TableName, PathAdd)
{
	s TableName=$g(TableName)
	s PathAdd=$g(PathAdd)
	s ClassName=$tr(TableName,"_")
	s rset = ##class(%ResultSet).%New()
	d rset.Prepare("select COLUMN_NAME,DATA_TYPE,CHARACTER_MAXIMUM_LENGTH,DESCRIPTION,IS_NULLABLE FROM information_schema.COLUMNS WHERE TABLE_SCHEMA='dbo'  and TABLE_NAME='"_TableName_"'")
	s exeret=rset.Execute()
	s colCount=rset.GetColumnCount()
	Set repid=$I(^CacheTemp)
	k ^TMP($zn,repid,$j)
	s IsLinux=1
	s logpath=""
	i ##class(%SYSTEM.Version).GetOS()="Windows" s IsLinux=0
	//默认路径
	i '$l(logpath) d
	.i IsLinux=0 d
	..s logpath="D:\"
	.e  d
	..s logpath="/"
	
	s logName=logpath_PathAdd_"LIS.WS.BLL.DHC"_$tr(TableName,"_")_".cls"
	//方法名,查询名
	s TableInnerName=$tr(TableName,"_")
	//不存在目录就创建
	i '##class(%File).DirectoryExists(logpath_PathAdd) d
	.d ##class(%File).CreateNewDir(logpath_PathAdd,"")
	
	s file=##class(%File).%New(logName)
	//存在就追加
	i ##class(%File).Exists(logName) d
	.d ##class(%File).Delete(logName)
	.d file.Open("WSN:/SHARED:/IOTABLE=""UTF8""")
	//不存在就新建
	e  d
	.d file.Open("WSN:/SHARED:/IOTABLE=""UTF8""")
	
	//写类头部
	d ..WriteLineWithCode(file,"///"_TableName_"操作类,由工具生成,返回空串保存成功,否则就是返回失败原因")
	d ..WriteLineWithCode(file,"Class LIS.WS.BLL.DHC"_$tr(TableName,"_")_" Extends %RegisteredObject")
	d ..WriteLineWithCode(file,"{")
	d ..WriteLineWithCode(file,"///"_$zd($h,8))
	d ..WriteLineWithCode(file,"///保存数据,多列以$CSP$分割")
	d ..WriteLineWithCode(file,"///"_TableInnerName)
	d ..WriteLineWithCode(file,"ClassMethod Save"_TableInnerName_"MTHD(SaveStr As %String(MAXLEN=99999999), P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, Sessions, Output RowCount As %String) As %String")
	d ..WriteLineWithCode(file,"{")
	d ..WriteLineWithCode(file,"	s SaveStr=$g(SaveStr)")
	d ..WriteLineWithCode(file,"	s Sessions=$g(Sessions)")
	d ..WriteLineWithCode(file,"	s UserDR=$p(Sessions,""^"",1)")
	d ..WriteLineWithCode(file,"	s WorkGroupDR=$p(Sessions,""^"",2)")
	d ..WriteLineWithCode(file,"	s HospitalDR=$p(Sessions,""^"",5)")
	d ..WriteLineWithCode(file,"	s sp=""$CSP$""")
	d ..WriteLineWithCode(file,"	//得到主键,为空就插入数据,否则就更新数据")
	d ..WriteLineWithCode(file,"	s RowID=$p(SaveStr,sp,1)")
	d ..WriteLineWithCode(file,"	s SaveObj=""""")
	d ..WriteLineWithCode(file,"	i $l(RowID) d")
	d ..WriteLineWithCode(file,"	.s SaveObj=##Class(dbo."_TableInnerName_").%New()")
	d ..WriteLineWithCode(file,"	e  d")
	d ..WriteLineWithCode(file,"	.s SaveObj=##Class(dbo."_TableInnerName_").%OpenId(RowID)")
	s Index=0
	s RowSpec="RowID"
    While(rset.Next())
    {
	    s outStr=""
        s colField=rset.GetColumnName(1)
        s colName=rset.GetDataByName(colField)
        s colField1=rset.GetColumnName(2)
        s colType=rset.GetDataByName(colField1)
        s colField2=rset.GetColumnName(3)
        s colLen=rset.GetDataByName(colField2)
        s colField3=rset.GetColumnName(4)
        s colDesc=rset.GetDataByName(colField3)
        s Index=Index+1
        i colName="RowID" continue
        s RowSpec=RowSpec_","_colName
        d ..WriteLineWithCode(file,"	s SaveObj."_colName_"=$p(SaveStr,sp,"_Index_")")
        
    }
	d ..WriteLineWithCode(file,"	s sc=SaveObj.%Save()")
    d ..WriteLineWithCode(file,"	i ('$SYSTEM.Status.IsOK(sc)) d")
    d ..WriteLineWithCode(file,"	.THROW ##class(%Exception.SystemException).%New(""事务委托"",""D"",,""-1^保存"_TableInnerName_"失败:""_$SYSTEM.Status.GetErrorText(sc))")
    d ..WriteLineWithCode(file,"	q """"")
    d ..WriteLineWithCode(file,"}")
    d ..WriteLineWithCode(file,"")
    d ..WriteLineWithCode(file,"")
    //构造删除数据代码
    d ..WriteLineWithCode(file,"///"_$zd($h,8))
	d ..WriteLineWithCode(file,"///删除数据,多个RowID以上尖号分割,返回空成功,非空为失败原因")
	d ..WriteLineWithCode(file,"///"_TableInnerName)
	d ..WriteLineWithCode(file,"ClassMethod Delete"_TableInnerName_"MTHD(RowIDS As %String(MAXLEN=99999999), P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, Sessions, Output RowCount As %String) As %String")
	d ..WriteLineWithCode(file,"{")
	d ..WriteLineWithCode(file,"	s RowIDS=$g(RowIDS)")
	d ..WriteLineWithCode(file,"	s Sessions=$g(Sessions)")
	d ..WriteLineWithCode(file,"	//托管事务删除数据")
	d ..WriteLineWithCode(file,"	q ##Class(LIS.WS.DHCLISServiceBase).DeclarativeTrans(""LIS.WS.BLL.DHC"_TableInnerName_""",""Delete"_TableInnerName_"Do"",RowIDS, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, Sessions)")
	d ..WriteLineWithCode(file,"}")
    d ..WriteLineWithCode(file,"")
    d ..WriteLineWithCode(file,"")
    
    d ..WriteLineWithCode(file,"///"_$zd($h,8))
	d ..WriteLineWithCode(file,"///删除数据,多个RowID以上尖号分割")
	d ..WriteLineWithCode(file,"///"_TableInnerName)
	d ..WriteLineWithCode(file,"ClassMethod Delete"_TableInnerName_"Do(RowIDS As %String(MAXLEN=99999999), P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, Sessions, Output RowCount As %String) As %String")
	d ..WriteLineWithCode(file,"{")
	d ..WriteLineWithCode(file,"	s RowIDS=$g(RowIDS)")
	d ..WriteLineWithCode(file,"	s Sessions=$g(Sessions)")
	d ..WriteLineWithCode(file,"	s Sessions=$g(Sessions)")
	d ..WriteLineWithCode(file,"	s UserDR=$p(Sessions,""^"",1)")
	d ..WriteLineWithCode(file,"	s WorkGroupDR=$p(Sessions,""^"",2)")
	d ..WriteLineWithCode(file,"	s HospitalDR=$p(Sessions,""^"",5)")
	d ..WriteLineWithCode(file,"	f i=1:1:$l(RowIDS,""^"") d")
	d ..WriteLineWithCode(file,"	.s RowID=$p(RowIDS,""^"",i)")
	d ..WriteLineWithCode(file,"	.s sc=##Class(dbo."_TableInnerName_").%DeleteId(RowID)")
	d ..WriteLineWithCode(file,"	.i ('$SYSTEM.Status.IsOK(sc)) d")
	d ..WriteLineWithCode(file,"	.THROW ##class(%Exception.SystemException).%New(""事务委托"",""D"",,""-1^删除"_TableInnerName_"失败:""_$SYSTEM.Status.GetErrorText(sc))")
	d ..WriteLineWithCode(file,"	q """"")
	d ..WriteLineWithCode(file,"}")
    d ..WriteLineWithCode(file,"")
    d ..WriteLineWithCode(file,"")
    
    
    //构造查询代码
    d ..WriteLineWithCode(file,"///"_$zd($h,8))
	d ..WriteLineWithCode(file,"///查询数据,查询条件自己改")
	d ..WriteLineWithCode(file,"///"_TableInnerName)
	d ..WriteLineWithCode(file,"Query Qry"_TableInnerName_"(P0, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, PageSize, PageIndex, Sessions, Output RowCount As %String) As %Query")
	d ..WriteLineWithCode(file,"{")
	d ..WriteLineWithCode(file,"}")
	d ..WriteLineWithCode(file,"")
    d ..WriteLineWithCode(file,"")
	d ..WriteLineWithCode(file,"/// 构造输出列")
	d ..WriteLineWithCode(file,"ClassMethod Qry"_TableInnerName_"(ByRef colinfo As %List, ByRef parminfo As %List, ByRef idinfo As %List, ByRef qHandle As %Binary, extoption As %Integer = 0, extinfo As %List) As %Status")
	d ..WriteLineWithCode(file,"{")
	d ..WriteLineWithCode(file,"	Set RowSpec=$LIST(qHandle,4)")
	d ..WriteLineWithCode(file,"	f i=1:1:$l(RowSpec,"","") d")
	d ..WriteLineWithCode(file,"	.s OneCol=$p(RowSpec,"","",i)")
	d ..WriteLineWithCode(file,"	.s OneColType=$p(OneCol,"":"",2)")
	d ..WriteLineWithCode(file,"	.s OneCol=$p(OneCol,"":"",1)")
	d ..WriteLineWithCode(file,"	.i i=1 s colinfo=$lb($lb(OneCol,OneColType))")
	d ..WriteLineWithCode(file,"	.e  s colinfo=colinfo_$lb($lb(OneCol,OneColType))")
	d ..WriteLineWithCode(file,"	s parminfo = """"")
	d ..WriteLineWithCode(file,"	s idinfo = """"")
	d ..WriteLineWithCode(file,"	q $$$OK")
	d ..WriteLineWithCode(file,"}")
	d ..WriteLineWithCode(file,"")
	d ..WriteLineWithCode(file,"")
	d ..WriteLineWithCode(file,"/// 在Execute控制RowSpec输出列")
	d ..WriteLineWithCode(file,"/// Query的执行方法")
	d ..WriteLineWithCode(file,"/// d ##class(%ResultSet).RunQuery(""Standard.StdQuery"",""QryTest"","""","""","""","""","""","""","""","""","""","""","""","""","""","""","""","""")")
	d ..WriteLineWithCode(file,"ClassMethod Qry"_TableInnerName_"Execute(ByRef qHandle As %Binary, P0, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, PageSize, PageIndex, Sessions, Output RowCount As %String) As %Status")
	d ..WriteLineWithCode(file,"{")
	d ..WriteLineWithCode(file,"	//指定输出列,和RowSpec保存一致,冒号指定类型")
	d ..WriteLineWithCode(file,"	s RowSpec="""_RowSpec_"""")
	d ..WriteLineWithCode(file,"	//得到repid和初始化ind")
	d ..WriteLineWithCode(file,"	Set ColFields = """_RowSpec_"""")
	d ..WriteLineWithCode(file," 	Set repid=$I(^CacheTemp)")
    d ..WriteLineWithCode(file,"	If $Get(ind)="""" Set ind=1")
	d ..WriteLineWithCode(file,"	//输出数据逻辑,这段逻辑可以动态改RowSpec控制输出列")
	d ..WriteLineWithCode(file,"	s RowID="""" f  s RowID=$o(^dbo."_TableInnerName_"I(""PK"_$zcvt(TableInnerName,"U")_""",RowID)) q:RowID=""""  d")
	d ..WriteLineWithCode(file,"	.d OutPutRow")
	d ..WriteLineWithCode(file,"")
	d ..WriteLineWithCode(file,"")
	d ..WriteLineWithCode(file,"OutPutRow")
	d ..WriteLineWithCode(file,"	s OneRowData=$g(^dbo."_TableInnerName_"D(RowID))")
	f i=2:1:$l(RowSpec,",") d
	.s OneName=$p(RowSpec,",",i)
	.d ..WriteLineWithCode(file,"	s "_OneName_"=$lg(OneRowData,"_i_")")
	d ..WriteLineWithCode(file,"	s Data = $lb("_RowSpec_")")
	d ..WriteLineWithCode(file,"	Set ^CacheTemp(repid,ind)=##Class(LIS.Util.Common).TransListNull(Data,ColFields)")
	d ..WriteLineWithCode(file,"	Set ind=ind+1")
	d ..WriteLineWithCode(file,"	q")
	d ..WriteLineWithCode(file,"}")
	d ..WriteLineWithCode(file,"")
	d ..WriteLineWithCode(file,"")
	d ..WriteLineWithCode(file,"ClassMethod Qry"_TableInnerName_"Close(ByRef qHandle As %Binary) As %Status [ PlaceAfter = Qry"_TableInnerName_"Execute ]")
	d ..WriteLineWithCode(file,"{")
	d ..WriteLineWithCode(file,"	Set repid=$LIST(qHandle,2)")
	d ..WriteLineWithCode(file," 	Kill ^CacheTemp(repid)")
	d ..WriteLineWithCode(file,"	Quit $$$OK")
	d ..WriteLineWithCode(file,"}")
	d ..WriteLineWithCode(file,"")
	d ..WriteLineWithCode(file,"")
	d ..WriteLineWithCode(file,"ClassMethod Qry"_TableInnerName_"Fetch(ByRef qHandle As %Binary, ByRef Row As %List, ByRef AtEnd As %Integer = 0) As %Status [ PlaceAfter = Qry"_TableInnerName_"Execute ]")
	d ..WriteLineWithCode(file,"{")
	d ..WriteLineWithCode(file," 	Set AtEnd=$LIST(qHandle,1)")
	d ..WriteLineWithCode(file," 	Set repid=$LIST(qHandle,2)")
	d ..WriteLineWithCode(file," 	Set ind=$LIST(qHandle,3)")
	d ..WriteLineWithCode(file," 	Set ind=$o(^CacheTemp(repid,ind))")
	d ..WriteLineWithCode(file," 	If ind="""" {	")
	d ..WriteLineWithCode(file," 		Set AtEnd=1")
	d ..WriteLineWithCode(file," 		Set Row=""""")
	d ..WriteLineWithCode(file," 	}")
	d ..WriteLineWithCode(file," 	Else      {	")
	d ..WriteLineWithCode(file," 		Set Row=^CacheTemp(repid,ind)")
	d ..WriteLineWithCode(file," 	}")
	d ..WriteLineWithCode(file," 	// Save QHandle")
	d ..WriteLineWithCode(file," 	s qHandle=$lb(AtEnd,repid,ind)")
	d ..WriteLineWithCode(file,"	Quit $$$OK")
	d ..WriteLineWithCode(file,"}")
	d ..WriteLineWithCode(file,"}")
    d file.Close()
    //构造C#代码
    d ..MakeTableCSharpCallCode(TableName,PathAdd)
    q "代码生成在:"_logName
}

/// 构造表的增删改查M类的C#调用代码,以简化常规表M代码和C#调用代码工作量
/// 编码格式改WriteLineWithCode
/// w ##class(Demo.CodeGener).MakeTableCSharpCallCode("BT_Bottle")
ClassMethod MakeTableCSharpCallCode(TableName, PathAdd)
{
	s TableName=$g(TableName)
	s PathAdd=$g(PathAdd)
	s ClassName=$tr(TableName,"_")
	s rset = ##class(%ResultSet).%New()
	d rset.Prepare("select COLUMN_NAME,DATA_TYPE,CHARACTER_MAXIMUM_LENGTH,DESCRIPTION,IS_NULLABLE FROM information_schema.COLUMNS WHERE TABLE_SCHEMA='dbo'  and TABLE_NAME='"_TableName_"'")
	s exeret=rset.Execute()
	s colCount=rset.GetColumnCount()
	Set repid=$I(^CacheTemp)
	k ^TMP($zn,repid,$j)
	s IsLinux=1
	s logpath=""
	i ##class(%SYSTEM.Version).GetOS()="Windows" s IsLinux=0
	//默认路径
	i '$l(logpath) d
	.i IsLinux=0 d
	..s logpath="D:\"
	.e  d
	..s logpath="/"
	
	s logName=logpath_PathAdd_"ash"_$tr(TableName,"_")_".cs"
	//方法名,查询名
	s TableInnerName=$tr(TableName,"_")
	//不存在目录就创建
	i '##class(%File).DirectoryExists(logpath_PathAdd) d
	.d ##class(%File).CreateNewDir(logpath_PathAdd,"")
	
	s file=##class(%File).%New(logName)
	//存在就追加
	i ##class(%File).Exists(logName) d
	.d ##class(%File).Delete(logName)
	.d file.Open("WSN:/SHARED:/IOTABLE=""UTF8""")
	//不存在就新建
	e  d
	.d file.Open("WSN:/SHARED:/IOTABLE=""UTF8""")
	
	//写类头部
	d ..WriteLineWithCode(file,"using System;")
	d ..WriteLineWithCode(file,"using System.Web;")
	d ..WriteLineWithCode(file,"using System.Reflection;")
    d ..WriteLineWithCode(file,"using System.Text;")
    d ..WriteLineWithCode(file,"using System.Data;")
    d ..WriteLineWithCode(file,"using System.Collections;")
    d ..WriteLineWithCode(file,"using System.Collections.Generic;")
    d ..WriteLineWithCode(file,"using LIS.Model.Entity;")
    d ..WriteLineWithCode(file,"using LIS.Model.Bussiness;")
    d ..WriteLineWithCode(file,"using Microsoft.AspNetCore.Http;")
    d ..WriteLineWithCode(file,"using iMedicalLIS;")
    d ..WriteLineWithCode(file,"")
    d ..WriteLineWithCode(file,"///"Class"">")
    d ..WriteLineWithCode(file,"/// [功能描述:"_TableInnerName_"调用类] ")
    d ..WriteLineWithCode(file,"/// [创建者:自动生成] ")
    d ..WriteLineWithCode(file,"/// [创建时间:"_$zd($h,8)_"] ")
    d ..WriteLineWithCode(file,"///<说明>")
    d ..WriteLineWithCode(file,"///  [说明:自定义操作自己改]")
    d ..WriteLineWithCode(file,"///")
    d ..WriteLineWithCode(file,"///<修改记录>")
    d ..WriteLineWithCode(file,"///    [修改时间:本次修改时间]")
    d ..WriteLineWithCode(file,"/// ")
    d ..WriteLineWithCode(file,"namespace UI.lis.ashx")
    d ..WriteLineWithCode(file,"{")
    //构造保存方法
    d ..WriteLineWithCode(file,"    public class ash"_TableInnerName_" : BaseHttpHandler")
    d ..WriteLineWithCode(file,"    {")
    d ..WriteLineWithCode(file,"        /// ")
    d ..WriteLineWithCode(file,"        /// 保存数据,多列以$CSP$分割")
    d ..WriteLineWithCode(file,"        /// ")
    d ..WriteLineWithCode(file,"        /// ")
    d ..WriteLineWithCode(file,"        public string Save"_TableInnerName_"()")
    d ..WriteLineWithCode(file,"        {")
    d ..WriteLineWithCode(file,"            //调用的后台类")
    d ..WriteLineWithCode(file,"            string className = ""LIS.WS.BLL.DHC"_TableInnerName_""";")
    d ..WriteLineWithCode(file,"            //调用的后台方法")
    d ..WriteLineWithCode(file,"            string funcName = ""Save"_TableInnerName_"MTHD"";")
    d ..WriteLineWithCode(file,"            string SaveStr=""""")
    d ..WriteLineWithCode(file,"            string sp=""$CSP$""")
    s Index=0
	s RowSpec="RowID"
	d ..WriteLineWithCode(file,"            string RowID=Helper.ValidParam(LIS.Core.MultiPlatform.LISContext.GetRequest(Request, ""RowID""), "");")
    While(rset.Next())
    {
	    s outStr=""
        s colField=rset.GetColumnName(1)
        s colName=rset.GetDataByName(colField)
        s colField1=rset.GetColumnName(2)
        s colType=rset.GetDataByName(colField1)
        s colField2=rset.GetColumnName(3)
        s colLen=rset.GetDataByName(colField2)
        s colField3=rset.GetColumnName(4)
        s colDesc=rset.GetDataByName(colField3)
        s Index=Index+1
        i colName="RowID" continue
        s RowSpec=RowSpec_","_colName
        d ..WriteLineWithCode(file,"            string "_colName_"=Helper.ValidParam(LIS.Core.MultiPlatform.LISContext.GetRequest(Request, """_colName_"""), "");")
    }
    s SaveCodeStr=$REPLACE(RowSpec,",","+sp+")
    d ..WriteLineWithCode(file,"            SaveStr="_SaveCodeStr_";")
    d ..WriteLineWithCode(file,"")
    d ..WriteLineWithCode(file,"")
    d ..WriteLineWithCode(file,"            Parameters param = new Parameters();")
    d ..WriteLineWithCode(file,"            param.P0=SaveStr")
    d ..WriteLineWithCode(file,"            string logInfo = this.UserLogin.UserDR + ""^"" + this.UserLogin.WorkGroupDR + ""^"" + this.UserLogin.LocationDR + ""^"" + this.UserLogin.WorkGroupDR + ""^"" + this.UserLogin.HospitalDR;")
    d ..WriteLineWithCode(file,"            int rowCount;")
    d ..WriteLineWithCode(file,"            string ObjStr = LIS.DAL.DataAccess.WebManager.GetDataJSON(className, funcName, param, logInfo, false, out rowCount, out this.Err);")
    d ..WriteLineWithCode(file,"            if(ObjStr=="""")")
    d ..WriteLineWithCode(file,"            {")
    d ..WriteLineWithCode(file,"            	return Helper.Success();")
    d ..WriteLineWithCode(file,"            }")
    d ..WriteLineWithCode(file,"            else")
	d ..WriteLineWithCode(file,"            {")
	d ..WriteLineWithCode(file,"            	return Helper.Error(ObjStr);")
	d ..WriteLineWithCode(file,"            }")
	d ..WriteLineWithCode(file,"    	}")
	d ..WriteLineWithCode(file,"")
    d ..WriteLineWithCode(file,"")    
	
	//构造删除方法
    d ..WriteLineWithCode(file,"        /// ")
    d ..WriteLineWithCode(file,"        /// 删除数据,多个RowID以上尖号分割")
    d ..WriteLineWithCode(file,"        /// ")
    d ..WriteLineWithCode(file,"        /// ")
    d ..WriteLineWithCode(file,"        public string Dalete"_TableInnerName_"()")
    d ..WriteLineWithCode(file,"        {")
    d ..WriteLineWithCode(file,"            //调用的后台类")
    d ..WriteLineWithCode(file,"            string className = ""LIS.WS.BLL.DHC"_TableInnerName_""";")
    d ..WriteLineWithCode(file,"            //调用的后台方法")
    d ..WriteLineWithCode(file,"            string funcName = ""Delete"_TableInnerName_"MTHD"";")
    d ..WriteLineWithCode(file,"")
    d ..WriteLineWithCode(file,"")
    d ..WriteLineWithCode(file,"            Parameters param = new Parameters();")
    d ..WriteLineWithCode(file,"            param.P0=Helper.ValidParam(LIS.Core.MultiPlatform.LISContext.GetRequest(Request, ""RowIDS""), "");")
    d ..WriteLineWithCode(file,"            string logInfo = this.UserLogin.UserDR + ""^"" + this.UserLogin.WorkGroupDR + ""^"" + this.UserLogin.LocationDR + ""^"" + this.UserLogin.WorkGroupDR + ""^"" + this.UserLogin.HospitalDR;")
    d ..WriteLineWithCode(file,"            int rowCount;")
    d ..WriteLineWithCode(file,"            string ObjStr = LIS.DAL.DataAccess.WebManager.GetDataJSON(className, funcName, param, logInfo, false, out rowCount, out this.Err);")
    d ..WriteLineWithCode(file,"            if(ObjStr=="""")")
    d ..WriteLineWithCode(file,"            {")
    d ..WriteLineWithCode(file,"            	return Helper.Success();")
    d ..WriteLineWithCode(file,"            }")
    d ..WriteLineWithCode(file,"            else")
	d ..WriteLineWithCode(file,"            {")
	d ..WriteLineWithCode(file,"            	return Helper.Error(ObjStr);")
	d ..WriteLineWithCode(file,"            }")
	d ..WriteLineWithCode(file,"    	}")
	d ..WriteLineWithCode(file,"")
    d ..WriteLineWithCode(file,"")  
    
    //构造查询代码
    d ..WriteLineWithCode(file,"        /// ")
    d ..WriteLineWithCode(file,"        /// 查询数据")
    d ..WriteLineWithCode(file,"        /// ")
    d ..WriteLineWithCode(file,"        /// ")
    d ..WriteLineWithCode(file,"        public string Qry"_TableInnerName_"()")
    d ..WriteLineWithCode(file,"        {")
    d ..WriteLineWithCode(file,"            //调用的后台类")
    d ..WriteLineWithCode(file,"            string className = ""LIS.WS.BLL.DHC"_TableInnerName_""";")
    d ..WriteLineWithCode(file,"            //调用的后台方法")
    d ..WriteLineWithCode(file,"            string funcName = ""Qry"_TableInnerName_"MTHD"";")
    d ..WriteLineWithCode(file,"")
    d ..WriteLineWithCode(file,"        	//预留的取前台参数代码")
    s Index=0
	d ..WriteLineWithCode(file,"            string RowID=Helper.ValidParam(LIS.Core.MultiPlatform.LISContext.GetRequest(Request, ""RowID""), "");")
    f i=1:1:$l(RowSpec,",") d
    .s colName=$p(RowSpec,",",i)
    .d ..WriteLineWithCode(file,"            string "_colName_"=Helper.ValidParam(LIS.Core.MultiPlatform.LISContext.GetRequest(Request, """_colName_"""), "");")
    
    d ..WriteLineWithCode(file,"")
    d ..WriteLineWithCode(file,"")
    d ..WriteLineWithCode(file,"            Parameters param = new Parameters();")
    d ..WriteLineWithCode(file,"            string logInfo = this.UserLogin.UserDR + ""^"" + this.UserLogin.WorkGroupDR + ""^"" + this.UserLogin.LocationDR + ""^"" + this.UserLogin.WorkGroupDR + ""^"" + this.UserLogin.HospitalDR;")
    d ..WriteLineWithCode(file,"            int rowCount;")
    d ..WriteLineWithCode(file,"            string ObjStr = LIS.DAL.DataAccess.WebManager.GetDataJSON(className, funcName, param, logInfo, false, out rowCount, out this.Err);")
    d ..WriteLineWithCode(file,"            return ObjStr;")
	d ..WriteLineWithCode(file,"    	}")
	d ..WriteLineWithCode(file,"")
    d ..WriteLineWithCode(file,"")  
    //收尾
    d ..WriteLineWithCode(file,"    }")
    d ..WriteLineWithCode(file,"}")
    d file.Close()
    q "代码生成在:"_logName
}

/// 带编码转换写字符
ClassMethod WriteLineWithCode(file, str)
{
	d file.WriteLine(str)
}

}

生成的M代码示例

///BT_Bottle操作类,由工具生成,返回空串保存成功,否则就是返回失败原因
Class LIS.WS.BLL.DHCBTBottle Extends %RegisteredObject
{
///20230807
///保存数据,多列以$CSP$分割
///BTBottle
ClassMethod SaveBTBottleMTHD(SaveStr As %String(MAXLEN=99999999), P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, Sessions, Output RowCount As %String) As %String
{
	s SaveStr=$g(SaveStr)
	s Sessions=$g(Sessions)
	s UserDR=$p(Sessions,"^",1)
	s WorkGroupDR=$p(Sessions,"^",2)
	s HospitalDR=$p(Sessions,"^",5)
	s sp="$CSP$"
	//得到主键,为空就插入数据,否则就更新数据
	s RowID=$p(SaveStr,sp,1)
	s SaveObj=""
	i $l(RowID) d
	.s SaveObj=##Class(dbo.BTBottle).%New()
	e  d
	.s SaveObj=##Class(dbo.BTBottle).%OpenId(RowID)
	s SaveObj.Code=$p(SaveStr,sp,2)
	s SaveObj.CName=$p(SaveStr,sp,3)
	s SaveObj.SetCapacity=$p(SaveStr,sp,4)
	s SaveObj.MinValue=$p(SaveStr,sp,5)
	s SaveObj.MaxValue=$p(SaveStr,sp,6)
	s SaveObj.CapColor=$p(SaveStr,sp,7)
	s SaveObj.BotFlag=$p(SaveStr,sp,8)
	s SaveObj.BotType=$p(SaveStr,sp,9)
	s SaveObj.Remark=$p(SaveStr,sp,10)
	s SaveObj.NullBotWeight=$p(SaveStr,sp,11)
	s SaveObj.Density=$p(SaveStr,sp,12)
	s SaveObj.Sequence=$p(SaveStr,sp,13)
	s SaveObj.Active=$p(SaveStr,sp,14)
	s sc=SaveObj.%Save()
	i ('$SYSTEM.Status.IsOK(sc)) d
	.THROW ##class(%Exception.SystemException).%New("事务委托","D",,"-1^保存BTBottle失败:"_$SYSTEM.Status.GetErrorText(sc))
	q ""
}


///20230807
///删除数据,多个RowID以上尖号分割,返回空成功,非空为失败原因
///BTBottle
ClassMethod DeleteBTBottleMTHD(RowIDS As %String(MAXLEN=99999999), P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, Sessions, Output RowCount As %String) As %String
{
	s RowIDS=$g(RowIDS)
	s Sessions=$g(Sessions)
	//托管事务删除数据
	q ##Class(LIS.WS.DHCLISServiceBase).DeclarativeTrans("LIS.WS.BLL.DHCBTBottle","DeleteBTBottleDo",RowIDS, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, Sessions)
}


///20230807
///删除数据,多个RowID以上尖号分割
///BTBottle
ClassMethod DeleteBTBottleDo(RowIDS As %String(MAXLEN=99999999), P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, Sessions, Output RowCount As %String) As %String
{
	s RowIDS=$g(RowIDS)
	s Sessions=$g(Sessions)
	s Sessions=$g(Sessions)
	s UserDR=$p(Sessions,"^",1)
	s WorkGroupDR=$p(Sessions,"^",2)
	s HospitalDR=$p(Sessions,"^",5)
	f i=1:1:$l(RowIDS,"^") d
	.s RowID=$p(RowIDS,"^",i)
	.s sc=##Class(dbo.BTBottle).%DeleteId(RowID)
	.i ('$SYSTEM.Status.IsOK(sc)) d
	.THROW ##class(%Exception.SystemException).%New("事务委托","D",,"-1^删除BTBottle失败:"_$SYSTEM.Status.GetErrorText(sc))
	q ""
}


///20230807
///查询数据,查询条件自己改
///BTBottle
Query QryBTBottle(P0, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, PageSize, PageIndex, Sessions, Output RowCount As %String) As %Query
{
}


/// 构造输出列
ClassMethod QryBTBottle(ByRef colinfo As %List, ByRef parminfo As %List, ByRef idinfo As %List, ByRef qHandle As %Binary, extoption As %Integer = 0, extinfo As %List) As %Status
{
	Set RowSpec=$LIST(qHandle,4)
	f i=1:1:$l(RowSpec,",") d
	.s OneCol=$p(RowSpec,",",i)
	.s OneColType=$p(OneCol,":",2)
	.s OneCol=$p(OneCol,":",1)
	.i i=1 s colinfo=$lb($lb(OneCol,OneColType))
	.e  s colinfo=colinfo_$lb($lb(OneCol,OneColType))
	s parminfo = ""
	s idinfo = ""
	q $$$OK
}


/// 在Execute控制RowSpec输出列
/// Query的执行方法
/// d ##class(%ResultSet).RunQuery("Standard.StdQuery","QryTest","","","","","","","","","","","","","","","","")
ClassMethod QryBTBottleExecute(ByRef qHandle As %Binary, P0, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, PageSize, PageIndex, Sessions, Output RowCount As %String) As %Status
{
	//指定输出列,和RowSpec保存一致,冒号指定类型
	s RowSpec="RowID,Code,CName,SetCapacity,MinValue,MaxValue,CapColor,BotFlag,BotType,Remark,NullBotWeight,Density,Sequence,Active"
	//得到repid和初始化ind
	Set ColFields = "RowID,Code,CName,SetCapacity,MinValue,MaxValue,CapColor,BotFlag,BotType,Remark,NullBotWeight,Density,Sequence,Active"
 	Set repid=$I(^CacheTemp)
	If $Get(ind)="" Set ind=1
	//输出数据逻辑,这段逻辑可以动态改RowSpec控制输出列
	s RowID="" f  s RowID=$o(^dbo.BTBottleI("PKBTBOTTLE",RowID)) q:RowID=""  d
	.d OutPutRow


OutPutRow
	s OneRowData=$g(^dbo.BTBottleD(RowID))
	s Code=$lg(OneRowData,2)
	s CName=$lg(OneRowData,3)
	s SetCapacity=$lg(OneRowData,4)
	s MinValue=$lg(OneRowData,5)
	s MaxValue=$lg(OneRowData,6)
	s CapColor=$lg(OneRowData,7)
	s BotFlag=$lg(OneRowData,8)
	s BotType=$lg(OneRowData,9)
	s Remark=$lg(OneRowData,10)
	s NullBotWeight=$lg(OneRowData,11)
	s Density=$lg(OneRowData,12)
	s Sequence=$lg(OneRowData,13)
	s Active=$lg(OneRowData,14)
	s Data = $lb(RowID,Code,CName,SetCapacity,MinValue,MaxValue,CapColor,BotFlag,BotType,Remark,NullBotWeight,Density,Sequence,Active)
	Set ^CacheTemp(repid,ind)=##Class(LIS.Util.Common).TransListNull(Data,ColFields)
	Set ind=ind+1
	q
}


ClassMethod QryBTBottleClose(ByRef qHandle As %Binary) As %Status [ PlaceAfter = QryBTBottleExecute ]
{
	Set repid=$LIST(qHandle,2)
 	Kill ^CacheTemp(repid)
	Quit $$$OK
}


ClassMethod QryBTBottleFetch(ByRef qHandle As %Binary, ByRef Row As %List, ByRef AtEnd As %Integer = 0) As %Status [ PlaceAfter = QryBTBottleExecute ]
{
 	Set AtEnd=$LIST(qHandle,1)
 	Set repid=$LIST(qHandle,2)
 	Set ind=$LIST(qHandle,3)
 	Set ind=$o(^CacheTemp(repid,ind))
 	If ind="" {	
 		Set AtEnd=1
 		Set Row=""
 	}
 	Else      {	
 		Set Row=^CacheTemp(repid,ind)
 	}
 	// Save QHandle
 	s qHandle=$lb(AtEnd,repid,ind)
	Quit $$$OK
}
}

生成的C#调用代码示例

using System;
using System.Web;
using System.Reflection;
using System.Text;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using LIS.Model.Entity;
using LIS.Model.Bussiness;
using Microsoft.AspNetCore.Http;
using iMedicalLIS;

///
/// [功能描述:BTBottle调用类] 
/// [创建者:自动生成] 
/// [创建时间:20230807] 
///<说明>
///  [说明:自定义操作自己改]
///
///<修改记录>
///    [修改时间:本次修改时间]
/// 
namespace UI.lis.ashx
{
    public class ashBTBottle : BaseHttpHandler
    {
        /// 
        /// 保存数据,多列以$CSP$分割
        /// 
        /// 
        public string SaveBTBottle()
        {
            //调用的后台类
            string className = "LIS.WS.BLL.DHCBTBottle";
            //调用的后台方法
            string funcName = "SaveBTBottleMTHD";
            string SaveStr=""
            string sp="$CSP$"
            string RowID=Helper.ValidParam(LIS.Core.MultiPlatform.LISContext.GetRequest(Request, "RowID"), ");
            string Code=Helper.ValidParam(LIS.Core.MultiPlatform.LISContext.GetRequest(Request, "Code"), ");
            string CName=Helper.ValidParam(LIS.Core.MultiPlatform.LISContext.GetRequest(Request, "CName"), ");
            string SetCapacity=Helper.ValidParam(LIS.Core.MultiPlatform.LISContext.GetRequest(Request, "SetCapacity"), ");
            string MinValue=Helper.ValidParam(LIS.Core.MultiPlatform.LISContext.GetRequest(Request, "MinValue"), ");
            string MaxValue=Helper.ValidParam(LIS.Core.MultiPlatform.LISContext.GetRequest(Request, "MaxValue"), ");
            string CapColor=Helper.ValidParam(LIS.Core.MultiPlatform.LISContext.GetRequest(Request, "CapColor"), ");
            string BotFlag=Helper.ValidParam(LIS.Core.MultiPlatform.LISContext.GetRequest(Request, "BotFlag"), ");
            string BotType=Helper.ValidParam(LIS.Core.MultiPlatform.LISContext.GetRequest(Request, "BotType"), ");
            string Remark=Helper.ValidParam(LIS.Core.MultiPlatform.LISContext.GetRequest(Request, "Remark"), ");
            string NullBotWeight=Helper.ValidParam(LIS.Core.MultiPlatform.LISContext.GetRequest(Request, "NullBotWeight"), ");
            string Density=Helper.ValidParam(LIS.Core.MultiPlatform.LISContext.GetRequest(Request, "Density"), ");
            string Sequence=Helper.ValidParam(LIS.Core.MultiPlatform.LISContext.GetRequest(Request, "Sequence"), ");
            string Active=Helper.ValidParam(LIS.Core.MultiPlatform.LISContext.GetRequest(Request, "Active"), ");
            SaveStr=RowID+sp+Code+sp+CName+sp+SetCapacity+sp+MinValue+sp+MaxValue+sp+CapColor+sp+BotFlag+sp+BotType+sp+Remark+sp+NullBotWeight+sp+Density+sp+Sequence+sp+Active;


            Parameters param = new Parameters();
            param.P0=SaveStr
            string logInfo = this.UserLogin.UserDR + "^" + this.UserLogin.WorkGroupDR + "^" + this.UserLogin.LocationDR + "^" + this.UserLogin.WorkGroupDR + "^" + this.UserLogin.HospitalDR;
            int rowCount;
            string ObjStr = LIS.DAL.DataAccess.WebManager.GetDataJSON(className, funcName, param, logInfo, false, out rowCount, out this.Err);
            if(ObjStr=="")
            {
            	return Helper.Success();
            }
            else
            {
            	return Helper.Error(ObjStr);
            }
    	}


        /// 
        /// 删除数据,多个RowID以上尖号分割
        /// 
        /// 
        public string DaleteBTBottle()
        {
            //调用的后台类
            string className = "LIS.WS.BLL.DHCBTBottle";
            //调用的后台方法
            string funcName = "DeleteBTBottleMTHD";


            Parameters param = new Parameters();
            param.P0=Helper.ValidParam(LIS.Core.MultiPlatform.LISContext.GetRequest(Request, "RowIDS"), ");
            string logInfo = this.UserLogin.UserDR + "^" + this.UserLogin.WorkGroupDR + "^" + this.UserLogin.LocationDR + "^" + this.UserLogin.WorkGroupDR + "^" + this.UserLogin.HospitalDR;
            int rowCount;
            string ObjStr = LIS.DAL.DataAccess.WebManager.GetDataJSON(className, funcName, param, logInfo, false, out rowCount, out this.Err);
            if(ObjStr=="")
            {
            	return Helper.Success();
            }
            else
            {
            	return Helper.Error(ObjStr);
            }
    	}


        /// 
        /// 查询数据
        /// 
        /// 
        public string QryBTBottle()
        {
            //调用的后台类
            string className = "LIS.WS.BLL.DHCBTBottle";
            //调用的后台方法
            string funcName = "QryBTBottleMTHD";

        	//预留的取前台参数代码
            string RowID=Helper.ValidParam(LIS.Core.MultiPlatform.LISContext.GetRequest(Request, "RowID"), ");
            string RowID=Helper.ValidParam(LIS.Core.MultiPlatform.LISContext.GetRequest(Request, "RowID"), ");
            string Code=Helper.ValidParam(LIS.Core.MultiPlatform.LISContext.GetRequest(Request, "Code"), ");
            string CName=Helper.ValidParam(LIS.Core.MultiPlatform.LISContext.GetRequest(Request, "CName"), ");
            string SetCapacity=Helper.ValidParam(LIS.Core.MultiPlatform.LISContext.GetRequest(Request, "SetCapacity"), ");
            string MinValue=Helper.ValidParam(LIS.Core.MultiPlatform.LISContext.GetRequest(Request, "MinValue"), ");
            string MaxValue=Helper.ValidParam(LIS.Core.MultiPlatform.LISContext.GetRequest(Request, "MaxValue"), ");
            string CapColor=Helper.ValidParam(LIS.Core.MultiPlatform.LISContext.GetRequest(Request, "CapColor"), ");
            string BotFlag=Helper.ValidParam(LIS.Core.MultiPlatform.LISContext.GetRequest(Request, "BotFlag"), ");
            string BotType=Helper.ValidParam(LIS.Core.MultiPlatform.LISContext.GetRequest(Request, "BotType"), ");
            string Remark=Helper.ValidParam(LIS.Core.MultiPlatform.LISContext.GetRequest(Request, "Remark"), ");
            string NullBotWeight=Helper.ValidParam(LIS.Core.MultiPlatform.LISContext.GetRequest(Request, "NullBotWeight"), ");
            string Density=Helper.ValidParam(LIS.Core.MultiPlatform.LISContext.GetRequest(Request, "Density"), ");
            string Sequence=Helper.ValidParam(LIS.Core.MultiPlatform.LISContext.GetRequest(Request, "Sequence"), ");
            string Active=Helper.ValidParam(LIS.Core.MultiPlatform.LISContext.GetRequest(Request, "Active"), ");


            Parameters param = new Parameters();
            string logInfo = this.UserLogin.UserDR + "^" + this.UserLogin.WorkGroupDR + "^" + this.UserLogin.LocationDR + "^" + this.UserLogin.WorkGroupDR + "^" + this.UserLogin.HospitalDR;
            int rowCount;
            string ObjStr = LIS.DAL.DataAccess.WebManager.GetDataJSON(className, funcName, param, logInfo, false, out rowCount, out this.Err);
            return ObjStr;
    	}


    }
}

你可能感兴趣的:(Caché,c#)