关于计数以及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);
相关推荐
兵慌码乱5 天前
面向桌面端的资产管理系统分层架构设计与核心模块实现
python·系统架构·sqlite·pyqt5·数据库设计·桌面应用开发·mvc架构
兵慌码乱6 天前
基于Python+PyQt5+SQLite的药房管理系统实现:事务一致性与界面解耦全流程解析
python·sqlite·信号与槽·pyqt5·数据库设计·桌面应用开发·事务处理
伶俜6611 天前
鸿蒙原生应用实战(十八)ArkUI 记账本:SQLite 账单 + 图表统计 + 分类管理
jvm·sqlite·harmonyos
伶俜6611 天前
鸿蒙原生应用实战(二十)ArkUI 课程表 App:Grid 网格 + SQLite 存储 + 周次切换 + 上课提醒
华为·sqlite·harmonyos
Lanceli_van12 天前
SQLite 3.45.2(sqlite-autoconf-3450200)ARM 交叉编译完整步骤
arm开发·sqlite
王小王-12312 天前
基于电脑硬件市场数据分析与可视化系统
数据库·数据分析·django·sqlite·电脑·电脑硬件数据·电脑硬件市场分析
winfredzhang12 天前
给本地图库的“人“加上属性:DeepFace + MediaPipe Pose 联合分析(含 GitHub 镜像踩坑)
python·sqlite·mediapipe·打标签·场景和属性
zh_xuan13 天前
PC端操作SQLite数据库
数据库·c++·sqlite
winfredzhang13 天前
用 wxPython + 通义千问 VL 打造一款“批量人物图像识别“桌面应用
python·sqlite·wxpython·qwen 3.7max·分析照片
爱和冰阔落13 天前
【MCP实战】从0写一个本地工具服务器:文件搜索、SQLite查询与安全边界
服务器·安全·sqlite