关于计数以及Index返回订单号升级版可以控制年月日累计(不重复)(sqlite)

1数据库创建:

RAGMA foreign_keys = false;

-- ----------------------------
-- Table structure for OrderSIndex
-- ----------------------------
DROP TABLE IF EXISTS "OrderSIndex";
CREATE TABLE "OrderSIndex" (
  "Id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  "OrderName" nvarchar(40),
  "OrderIndexInSigle" integer,
  "IndexType" integer,
  "CreateDate" datetime,
  "Updated" datetime,
  "ThisSigle" nvarchar(20)
);

-- ----------------------------
-- Auto increment value for OrderSIndex
-- ----------------------------
UPDATE "sqlite_sequence" SET seq = 4 WHERE name = 'OrderSIndex';

PRAGMA foreign_keys = true;

Model数据:

 /// 
    /// 
    /// 
    public class OrderSIndex
    {

        /// 
        /// 
        ///  
        [Description("")]
        public int Id { get; set; }
        /// 
        /// 
        ///  
        [Description("")]
        public string OrderName { get; set; }
        /// 
        /// 
        ///  
        [Description("")]
        public int OrderIndexInSigle { get; set; }
        /// 
        /// 
        ///  
        [Description("")]
        public int IndexType { get; set; }
        /// 
        /// 
        ///  
        [Description("")]
        public DateTime CreateDate { get; set; }
        /// 
        /// 
        ///  
        [Description("")]
        public DateTime Updated { get; set; }
        /// 
        /// 
        ///  
        [Description("")]
        public string ThisSigle { get; set; }
    }

DAL操作

 public  class DALOrderSIndex
    {
        /// 
        ///获取当前 0月累计,1日累计,2年累计,3,总共累计
        /// 
        ///  0月累计,1日累计,2年累计,3,总共累计
        /// 当前关键字(需要的表单名子)
        /// index编号长度 0001 就是4 
        /// 
        public string OutIndexStr(string SetKeyword, int SetNoType = 0, int OutIndexLength = 5)
        {
            string ThisSigle;
            switch (SetNoType)
            {
                case 0:
                    ThisSigle = $"{DateTime.Now.Year}{DateTime.Now.ToString("MM")}";
                    break;
                case 1:
                    ThisSigle = DateTime.Now.ToString("yyyyMMdd");
                    break;
                case 2:
                    ThisSigle = DateTime.Now.Year.ToString();
                    break;
                default://3
                    ThisSigle = string.Empty;//数据空
                    break;
            }
            string OutStr;
            string sqlQurey = $"select * from OrderSIndex where OrderName = '{SetKeyword}' and ThisSigle = '{ThisSigle}'";
            string SqlInsert = $"insert into  OrderSIndex (OrderName,OrderIndexInSigle,IndexType,CreateDate,Updated,ThisSigle) values('{SetKeyword}',1,{SetNoType},datetime('now','localtime'),datetime('now','localtime'),'{ThisSigle}')";
            string SqlUpdate = $"Update OrderSIndex set OrderIndexInSigle=OrderIndexInSigle +1 where OrderName = '{SetKeyword}' and ThisSigle = '{ThisSigle}'  ";
            using (IDbConnection Conn = new DALDBBase().GetOpenConn())
            {
                IEnumerable<OrderSIndex> list = Conn.Query<OrderSIndex>(sqlQurey);
                if (list == null || list.Count() == 0)
                {
                    Conn.Execute(SqlInsert);
                    OutStr = $"{ThisSigle}{1.ToString().PadLeft(OutIndexLength, '0')}";
                }
                else
                {
                    Conn.Execute(SqlUpdate);
                    OrderSIndex oneModel = list.FirstOrDefault();
                    OutStr = $"{ThisSigle}{(oneModel.OrderIndexInSigle + 1).ToString().PadLeft(OutIndexLength, '0')}";
                }
            }
            return OutStr; 
        }

    }

使用方法:

 string  innn =  new DAL.DALOrderSIndex().OutIndexStr("功能表数据",3,2);

你可能感兴趣的:(sqlite)