public void Create(Database db) { db.ExecNonQuery( string.Format( "insert into {0} ({1},{2},{3},{4},{5}) values(?strategy, ?stime, ?etime, ?mode, ?param)" , Mapper.TableName, Mapper.Strategy, Mapper.StartTime, Mapper.EndTime, Mapper.PickingMode, Mapper.Params ) , new string[] {"strategy","stime","etime", "mode", "param"} , new object[] {this.Strategy, this.StartTime, this.EndTime, this.PickingMode, this.Params}); this.PickId = Convert.ToInt32(db.ExecScalar("select last_insert_id()", null, null)); }
/// <summary> /// 创建股东数数据。前提条件:<br /> /// 1. 每次调用,<paramref name="entities"/>只能包含同一只股票的股东数数据;<br /> /// 2. 调用时,必须确保StockId, PublishDate, HolderCount, AverageStockNumber, Source属性值有效; /// </summary> /// <param name="db"></param> /// <param name="entities"></param> public static int Create(Database db, IList<ShareholdersNumEntity> entities) { if(entities==null || entities.Count<=0) return 0; DateTime minDate = DateTime.MaxValue, effectiveDate = new DateTime(1990, 1, 1); int stockId = 0, exists=0, insertedRows = 0; try{ db.BeginTransaction(); //添加数据 foreach(ShareholdersNumEntity entity in entities){ //数据校验 if(entity.StockId<=0 || entity.ReportDate<=effectiveDate // || entity.HolderCount<=0 || entity.AverageStockNumber<=0 || (entity.Source==null || entity.Source.Trim().Length<=0)) throw new EntityException("[holder-num] [create] 属性无效,无法更新数据库,[id:" + entity.StockId + ", date:" + entity.ReportDate.ToString("yyyyMMdd")); if(stockId==0) stockId = entity.StockId; if(stockId!=entity.StockId) throw new EntityException("[holder-num] [create] entities中包含了多只股票的股东数数据"); entity.CreateTime = DateTime.Now; entity.VarNum = 0; //插入数据 exists = Convert.ToInt32(db.ExecScalar( "select count(*) from sto_holder_num where sto_id=?id and report_date=?date", new string[] {"id", "date"}, new object[] { entity.StockId, entity.ReportDate} )); if(exists<=0){ insertedRows += db.ExecNonQuery(INSERT_SQL, new string[]{"id", "date", "holdersNum", "varNum", "avgShares", "totalShares", "transShares", "time", "source"}, new object[]{entity.StockId, entity.ReportDate, entity.HoldersNum, entity.VarNum , entity.AvgShares, entity.TotalShares, entity.TransShares , entity.CreateTime, entity.Source}); if(minDate>entity.ReportDate) minDate = entity.ReportDate; } } //更新股东数增长量 int prevCount = 0; exists = Convert.ToInt32(db.ExecScalar( "select count(*) from sto_holder_num where sto_id=?id and report_date<?date", new string[] {"id", "date"}, new object[] { stockId, minDate} )); if(exists>0){ prevCount = Convert.ToInt32(db.ExecScalar( "select holders_num from sto_holder_num where sto_id=?id and report_date<?date order by report_date desc limit 1", new string[] {"id", "date"}, new object[] { stockId, minDate} )); } DataSet ds = db.ExecDataSet( "select * from sto_holder_num where sto_id=?id and report_date>=?date order by report_date asc", new string[] { "id", "date" }, new object[] { stockId, minDate } ); foreach(DataRow row in ds.Tables[0].Rows){ if(prevCount==0){ prevCount = Convert.ToInt32(row["holders_num"]); continue; } int curCount = Convert.ToInt32(row["holders_num"]); db.ExecNonQuery( "update sto_holder_num set var_num=?varNum where sto_id=?id and report_date=?date", new string[] { "id", "date", "varNum" }, new object[] { stockId, row["report_date"], curCount-prevCount} ); prevCount = curCount; } db.CommitTransaction(); }catch(Exception ex){ db.RollbackTransaction(); throw ex; } return insertedRows; }