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

1数据库创建:

csharp 复制代码
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数据:

csharp 复制代码
 /// <summary>
    /// 
    /// </summary>
    public class OrderSIndex
    {

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

DAL操作

csharp 复制代码
 public  class DALOrderSIndex
    {
        /// <summary>
        ///获取当前 0月累计,1日累计,2年累计,3,总共累计
        /// </summary>
        /// <param name="SetNoType"> 0月累计,1日累计,2年累计,3,总共累计</param>
        /// <param name="SetKeyword">当前关键字(需要的表单名子)</param>
        /// <param name="OutIndexLength">index编号长度 0001 就是4 </param>
        /// <returns></returns>
        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; 
        }

    }

使用方法:

csharp 复制代码
 string  innn =  new DAL.DALOrderSIndex().OutIndexStr("功能表数据",3,2);
相关推荐
CopyDragon4 小时前
设置域名跨越访问
数据库·sqlite
云空5 小时前
《Python 与 SQLite:强大的数据库组合》
数据库·python·sqlite
极客小张8 小时前
基于STM32的智能充电桩:集成RTOS、MQTT与SQLite的先进管理系统设计思路
stm32·单片机·嵌入式硬件·mqtt·sqlite·毕业设计·智能充电桩
YUJIANYUE14 小时前
PHP将指定文件夹下多csv文件[即多表]导入到sqlite单文件
jvm·sqlite·php
Bonne journée1 天前
SQLite数据库是什么?DB Browser for SQLite是什么?
数据库·sqlite
q567315232 天前
使用 Python 编辑 XML 文件中的文本字段
xml·java·数据库·python·sqlite
Mephisto.java2 天前
【大数据学习 | kafka】kafka的偏移量管理
大数据·sql·oracle·sqlite·json·hbase
doll ~CJ3 天前
SQLite的BLOB数据类型与C++二进制存储学习记录
c++·sqlite·blob·图像数据存取·bitset标准库
啧不应该啊4 天前
Django数据模型on_delete属性值
数据库·django·sqlite
荣世蓥5 天前
数据库_SQLite3
数据库·sqlite