zoukankan      html  css  js  c++  java
  • 订单号生成规则,谁用谁知道

     1 class Program
     2     {
     3         //订单号数据源,实际项目中,这里是从数据库中读取过来的
     4         string[] strList = { "201512000001", "201512000002", "201512000003", "201512000004", 
     5                              "201512000008", "201512000007", "201512000006", "201512000005", 
     6                              "201512000009", "201512000010", "201512000011", "201512000012",
     7                              "201512000013","201512000014","201512000015" };        
     8         static void Main(string[] args)
     9         {
    10             Program p = new Program();
    11             Console.WriteLine(p.GetOrderNum());
    12             Console.ReadKey();
    13         }
    14 
    15         public string GetOrderNum()
    16         {
    17             string result = "";            
    18             string YearMonth = DateTime.Now.ToString("yyyyMM");
    19             var count = Count(YearMonth);//201512  5
    20             result = YearMonth + "0000001";
    21             var dd = count.ToString().Length;
    22             switch (dd)
    23             {
    24                 case 1:
    25                     result = YearMonth + "00000" + (count + 1);
    26                     break;
    27                 case 2:
    28                     result = YearMonth + "0000" + (count + 1);
    29                     break;
    30                 case 3:
    31                     result = YearMonth + "000" + (count + 1);
    32                     break;
    33                 case 4:
    34                     result = YearMonth + "00" + (count + 1);
    35                     break;
    36                 case 5:
    37                     result = YearMonth + "0" + (count + 1);
    38                     break;
    39                 default:
    40                     result = YearMonth + (count + 1);
    41                     break;
    42             }
    43             return result;
    44         }
    45         
    46         public int Count(string yearMonth) {
    47             int result = 0;
    48             result = strList.Count(item=>item.Contains(yearMonth));
    49             return result;
    50         }       
    51     }

    用string.PadLeft(n,'0')补零写出来的代码要比上面if的要好看很多,多谢园友提醒,最终代码请看下面

    public string GetOrderNum2()
    {
    string result = "";
    string YearMonth = DateTime.Now.ToString("yyyyMM");
    var count = Count(YearMonth);//201512 5 
    result= YearMonth+(count + 1).ToString().PadLeft(6,'0');
    return result;
    }
    

      

    谁有更好的生成规则,不妨一起讨论下

  • 相关阅读:
    dom4j解析带命名空间的xml文件
    Spring使用facotry-method创建单例Bean总结<转>
    代码审查工具StyleCop
    ReSharper 配置及用法(二)
    ReSharper 配置及用法(一)
    linqPad快速学习LINQ(含视频)
    评估期已过。有关如何升级的测试版软件的信息
    SQL批量更新数据库中所有用户数据表中字段类型为tinyint为int
    SQL SERVER获取数据库中所有表名 XTYPE类型
    sqlserver中创建链接服务器
  • 原文地址:https://www.cnblogs.com/axinno1/p/5025268.html
Copyright © 2011-2022 走看看