using CommonUnit.Base; using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace CommonUnit.IDGenerator { /// /// 主键生成接口 /// public interface IIDGenerator { /// /// 生成新主键 /// /// 生成主键参数 /// 生成后新主键 string GeneratNewID(IDGenerateParm parm); } /// /// 主键生成参数 /// public class IDGenerateParm{ /// /// 主键基值(新主键在此基础上按规则增加) /// public string BaseValue { get; set; } /// /// 主键长度 /// public int IDLength { get; set; } } /// /// 简单主键生成器 /// public class SampleIDGenerator : IIDGenerator { /// /// 生成新主键 /// /// 生成主键参数 /// 生成后新主键 string IIDGenerator.GeneratNewID(IDGenerateParm parm) { var ret = ""; if (StringHelper.IsNullOrEmpty(parm.BaseValue)) parm.BaseValue = "0"; int li_bv; if (!int.TryParse(parm.BaseValue,out li_bv)) throw new Exception("参数错误,简单生成器仅能生成数字字符串!"); if (parm.IDLength == 0) parm.IDLength = 5; var li_id=(int)Math.Pow(10, parm.IDLength); li_id = li_id + li_bv + 1; return li_id.ToString().Substring(1); } } }