public bool IsDuplicated(Item item) { // DBへの問い合わせ var con = new MySqlConnection(ConnectionString); // 接続文字列は static で定義されているとして con.Open(); using (var command = con.CreateCommand()) { command.CommandText = "SELECT * FROM t_item WHERE skucode = @skucode"; command.Parameters.Add(new MySqlParameter("@skucode", item.Code.Value)); using (var reader = command.ExecuteReader()) { while (reader.Read()) { var fromDate = reader["fromdate"] as DateTime?; var toDate = reader["todate"] as DateTime?; var term = new ValidTerm((DateTime)fromDate, (DateTime)toDate); if (item.Term.IsOverrap(term)) { return(true); } } } } return(false); }
// 期間が同じか public override bool Equals(object obj) { if (obj.GetType() != this.GetType()) { return(false); } ValidTerm other = (ValidTerm)obj; return(this.FromDate == other.FromDate && this.ToDate == other.ToDate); }
// 期間が重なっているか public bool IsOverrap(ValidTerm other) { return(other.FromDate <= this.ToDate && other.ToDate >= this.FromDate); }
// 生存期間の変更 public void ChangeValidTerm(ValidTerm newTerm) { this.validTerm = newTerm; }
public Item(SkuCode code, ValidTerm validTerm) { this.id = Guid.NewGuid().ToString(); this.code = code; this.validTerm = validTerm; }