持久层框架Clone

Clone框架:http://www.52chloe.com/Wiki/Document/3324802610879266816

Clone框架支持多种数据库,我用的是sql server

  基本查询:

1.取前几条数据

  sql server的sql 语句是:

1 UPDATE dbo.POM_Entry SET Status = 'Suspend' WHERE EntryCode IN 
2 (SELECT TOP 1 EntryCode FROM dbo.POM_Entry WHERE OrderCode = 'POM20170401151750740865' AND status = 'Issued' ORDER BY OrderIndex desc);

  用Clone框架.TakePage(1,20)

 

 1 //将该订单下相应个数的状态为下发的工单的状态改为暂停 
 2 public void SuspendAmount(string orderCode, int SuspendAmount)
 3         {
 4        //加事务
 5             this.DbContext.DoWithTransaction(() =>
 6             { 
 7           //查询出该订单下状态为下发的工单,并按排序号倒叙排序,取出SuspendAmount个工单,将其状态设为暂停
 8                 List<POM_Entry> Lq = this.DbContext.Query<POM_Entry>().FilterDeleted()
 9                 .Where(a => a.OrderCode == orderCode && a.Status == "Issued")
10                 .OrderByDesc(a => a.OrderIndex).TakePage(1, SuspendAmount).ToList();
11                 var entryAccount = Lq.Count;
12                 for (var i = 0;i < entryAccount; i++)
13                 {
14             //更新语句a => a.EntryCode == entryCode中entryCode不能用Lq[i].EntryCode代替,必须先取出来
15                     var entryCode = Lq[i].EntryCode;
16                     this.DbContext.Update<POM_Entry>(a => a.EntryCode == entryCode, a => new POM_Entry()
17                     {
18                         Status = "Suspend"
19                     });
20                 }
21             });
22         }

 

2.调用存储过程

  存储过程如下:

 1 SET ANSI_NULLS ON
 2 GO
 3 SET QUOTED_IDENTIFIER ON
 4 GO
 5 --创建生成工单的存储过程
 6 CREATE  PROCEDURE [dbo].[P_POM_CreateEntry]
 7     @OrderCode nvarchar(50),--输入参数订单号
 8     @CreateUserId NVARCHAR(50),--输入参数创建者ID
 9     @vMsg INT OUTPUT --输出参数
10 AS 
11 BEGIN    
12 SET NOCOUNT ON;
13 DECLARE --声明变量
14     @MaterielNo NVARCHAR(50), 
15     @NumberSegmentStart INT,
16     @NumberSegment INT,
17     @CellCode NVARCHAR(50),
18     @PPRCode NVARCHAR(50),
19     @Type NVARCHAR(50),
20     @OrderIndex INT = 1,
21     @Amount FLOAT=0
22 
23     --根据订单号查询 给变量赋值
24     SELECT
25     @MaterielNo = dbo.POM_Order.MaterielNo,
26     @NumberSegmentStart = dbo.POM_Order.NumberSegmentStart,
27     @CellCode = dbo.POM_Order.CellCode, 
28     @PPRCode = dbo.POM_Order.PPRCode,
29     @Type = dbo.POM_Order.[Type],
30     @Amount= dbo.POM_Order.Amount 
31     FROM dbo.POM_Order WHERE OrderCode = @OrderCode;
32     SET @NumberSegment = @NumberSegmentStart
33     --加入事务
34     begin tran;
35     begin TRY
36     --循环号段
37     DELETE  FROM dbo.POM_Entry WHERE OrderCode = @OrderCode;--如果存在删除
38     WHILE @NumberSegment < @NumberSegmentStart+CONVERT(INT,@Amount)--重点:int类型和float类型的直接相加 会出错,得做一下类型转换
39     BEGIN    
40     INSERT dbo.POM_Entry
41             ( Id ,
42               OrderCode ,
43               EntryCode ,
44               ProductUniqueCode ,
45               CellCode ,
46               PPRCode ,
47               Type ,
48               Status ,
49               PlanStartTime ,
50               PlanEndTime ,
51               ActualStartTime ,
52               ActualEndTime ,
53               OrderIndex,
54               CreationTime ,
55               CreateUserId ,
56               IsEnabled ,
57               IsDeleted ,
58               DeletionTime ,
59               DeleteUserId
60             )
61     VALUES  ( NEWID() , -- Id - nvarchar(50) --调用函数
62               @OrderCode , -- OrderCode - nvarchar(50)
63               [dbo].[f_GetNumber]('EN', floor(RAND()*1000)) , -- EntryCode - nvarchar(50) --调用函数f_GetNumber
64               [dbo].f_GetProductUniqueCode(@MaterielNo, @NumberSegment) , -- ProductUniqueCode - nvarchar(100)--调用函数f_GetProductUniqueCode
65               @CellCode , -- CellCode - nvarchar(50)
66               @PPRCode , -- PPRCode - nvarchar(50)
67               @Type , -- Type - nvarchar(50)
68               'New' , -- Status - nvarchar(50)
69               NULL , -- PlanStartTime - datetime
70               NULL , -- PlanEndTime - datetime
71               NULL , -- ActualStartTime - datetime
72               NULL , -- ActualEndTime - datetime
73               @OrderIndex , -- OrderIndex - int
74               GETDATE() , -- CreationTime - datetime
75               @CreateUserId , -- CreateUserId - varchar(50)
76               1 , -- IsEnabled - bit
77               0 , -- IsDeleted - bit
78               NULL , -- DeletionTime - datetime
79               NULL  -- DeleteUserId - varchar(50)
80             );
81     SET @NumberSegment += 1;
82     SET @OrderIndex += 1;
83     end
84     commit;--成功
85     SET @vMsg = 1;
86     end TRY
87     begin catch        
88         rollback tran;--失败回滚
89         SET @vMsg = 0;
90     end catch      
91 END

用Clone框架:

1  public int CreateEntry(string orderCode) {
2             DbParam[] pas = new DbParam[3];
3             pas[0] = new DbParam("@OrderCode", orderCode);
4             pas[1] = new DbParam("@CreateUserId",this.Session.UserId);
5             pas[2] = new DbParam("@vMsg", null, typeof(int)) { Direction = ParamDirection.Output };
6             DbContext.SqlQuery<int>("P_POM_CreateEntry", CommandType.StoredProcedure, pas).FirstOrDefault();
7             return (int)pas[2].Value;
8         }

 3.对时间类型的模糊查询

  sql server 的sql语句:

SELECT ISNULL(COUNT(1), 0) as amount FROM dbo.POM_Entry WHERE convert(varchar(10), ActualEndTime, 121) = '2017-04-01' AND Status = 'Complete'

用Clone框架:Clone框架有.Contains(),表示like模糊查询,但是不适用于Date类型的字段ActualEndTime,所以我只能写sql语句了

 1        /*
 2         平台首页:月产量统计图获取本月每日的产量
 3             */
 4         public string GetFinishAmountByMonth(string year,int month,int day)
 5         {
 6             string MonthStr = "";
 7             var dataStr = "";
 8             int vliday = 1;
 9             if (month < 10)
10             {
11                 MonthStr = "0" + month.ToString();
12             }
13             else
14             {
15                 MonthStr = month.ToString();
16             }
17             for(; vliday <=  day;vliday ++ )
18             {
19                 string NowDate = "";
20                
21                 if ( vliday < 10 )
22                 {
23                     NowDate = year + "-" + MonthStr + "-" + "0" + vliday.ToString();
24                 }
25                 else
26                 {
27                     NowDate = year + "-" + MonthStr + "-" + vliday.ToString();
28                 }
29                 //SELECT ISNULL(COUNT(1), 0) as amount FROM dbo.POM_Entry WHERE convert(varchar(10), ActualEndTime, 121) = '2017-04-01' AND Status = 'Complete'
30                 string sql = "SELECT ISNULL(COUNT(1), 0) as amount FROM dbo.POM_Entry WHERE convert(varchar(10), ActualEndTime, 121) = '{0}' AND Status = 'Complete'";
31                 POMDayProduct data = this.DbContext.SqlQuery<POMDayProduct>(string.Format(sql, NowDate),null).ToList()[0];
32                 if ( vliday != day)
33                 {
34                     dataStr = dataStr + data.amount + ",";
35                 }
36                 else
37                 {
38                     dataStr = dataStr + data.amount;
39                 }
40                 
41             }
42                 return dataStr;
43         }

注意:

1.这个方法得频繁的和数据库交互,不是很好;

2.对时间模糊查询时,时间字符串必须是'2017-04-09'这样的格式,也就是说'2017-4-9'这种格式是查不出来数据的,

所以我对月,日做了判断,小于10==>拼成'0x';

3.时间字符串类型的转换,充分利用convert(varchar(10), ActualEndTime, 121),这个函数是将ActualEndTime日期类型转化为10位的字符串时间:'2017-04-09';

4.查询某一天的记录直接利用convert()函数等值判断就可以了,不需要like了,代码中的"= '{0}'"就是了,而且" '{0}'"表示第一个参数NowDate,一定要加单引号,不然数据库也是查询不出来的;

5.SELECT ISNULL(COUNT(1), 0) as amount 也要注意, ISNULL(COUNT(1), 0)的数据类型是int类型的,所以POMDayProduct实体里的属性amount的类型(as amount) 也一定必须是int类型的;

6.最后说一下,为了避免和数据库多次交互,这个方法本来我是写成了存储过程,但无奈返回的是"1,2,3,4,,,,,"类型的字符串,而我用Clone框架的this.DbContext.SqlQuery<T>,T不能使用string,必须是一个实体,故放弃了存储过程

posted @ 2017-04-07 11:06  单纯的桃子  阅读(295)  评论(0编辑  收藏  举报