Skip to content

Instantly share code, notes, and snippets.

@boydaihungst
Last active April 27, 2020 16:44
Show Gist options
  • Save boydaihungst/f6d04ea6e234ae046d7b7431867d4324 to your computer and use it in GitHub Desktop.
Save boydaihungst/f6d04ea6e234ae046d7b7431867d4324 to your computer and use it in GitHub Desktop.
[When(@"create category has name ""(.*)"" and used in document")]
public void CreateCategoryWithDocument(string categoryName)
{
UnitOfWork unitOfWork = (UnitOfWork)dependencyResolver
.GetService(typeof(UnitOfWork));
var randomUser = unitOfWork.User.GetAll().FirstOrDefault();
using (var transaction = unitOfWork.BeginTransaction())
{
try
{
// remove old category
categoryName = categoryName.Trim().ToLower();
var loaiTaiLieu = unitOfWork.LoaiTaiLieu
.GetAll().Where(x => x.Name.ToLower() == categoryName).FirstOrDefault();
if (loaiTaiLieu != null)
{
var documents = loaiTaiLieu.DocumentCategories.Select(x => x.Document).ToList();
foreach (var item in documents)
{
documentId = item.Id;
RemoveGeneratedDocument();
}
unitOfWork.LoaiTaiLieu.Delete(loaiTaiLieu);
}
var dauBanHanh = unitOfWork.DauBanHanh
.FindAll(x => x.Name.ToLower() == categoryName).FirstOrDefault();
if (dauBanHanh != null)
{
var documents = unitOfWork.Document.GetAll().Where(x
=> x.DocumentAttributes.Any(item
=> item.Attrdef == 3 && item.Value == dauBanHanh.Id.ToString())).ToList();
foreach (var item in documents)
{
documentId = item.Id;
RemoveGeneratedDocument();
}
unitOfWork.DauBanHanhUser.DeleteRange(dauBanHanh.DauBanHanhUsers.ToList());
unitOfWork.SaveChanges();
unitOfWork.DauBanHanh.Delete(dauBanHanh);
}
var boPhanSoanThao = unitOfWork.BoPhanSoanThao
.FindAll(x => x.Name.ToLower() == categoryName).FirstOrDefault();
if (boPhanSoanThao != null)
{
var documents = unitOfWork.Document.GetAll().Where(x
=> x.DocumentAttributes.Any(item
=> item.Attrdef == 9 && item.Value == boPhanSoanThao.Id.ToString())).ToList();
foreach (var item in documents)
{
documentId = item.Id;
RemoveGeneratedDocument();
}
unitOfWork.BoPhanSoanThao.Delete(boPhanSoanThao);
}
var chucVu = unitOfWork.ChucVu
.FindAll(x => x.Name.ToLower() == categoryName).FirstOrDefault();
if (chucVu != null)
{
var documents = unitOfWork.Document.GetAll().Where(x
=> x.DocumentAttributes.Any(item
=> item.Attrdef == 15 && item.Value == chucVu.Id.ToString())).ToList();
foreach (var item in documents)
{
documentId = item.Id;
RemoveGeneratedDocument();
}
unitOfWork.ChucVu.Delete(chucVu);
}
var khoiSoanThao = unitOfWork.KhoiSoanThao
.FindAll(x => x.Name.ToLower() == categoryName).FirstOrDefault();
if (khoiSoanThao != null)
{
var documents = unitOfWork.Document.GetAll().Where(x
=> x.DocumentAttributes.Any(item
=> item.Attrdef == 14 && item.Value == khoiSoanThao.Id.ToString())).ToList();
foreach (var item in documents)
{
documentId = item.Id;
RemoveGeneratedDocument();
}
unitOfWork.KhoiSoanThao.Delete(khoiSoanThao);
}
var vanBanThongKe = unitOfWork.VanBanThongKe
.FindAll(x => x.Name.ToLower() == categoryName).FirstOrDefault();
if (vanBanThongKe != null)
{
var documents = unitOfWork.Document.GetAll().Where(x
=> x.DocumentAttributes.Any(item
=> item.Attrdef == 18 && item.Value == vanBanThongKe.Id.ToString())).ToList();
foreach (var item in documents)
{
documentId = item.Id;
RemoveGeneratedDocument();
}
unitOfWork.VanBanThongKe.Delete(vanBanThongKe);
}
unitOfWork.SaveChanges();
// Create new all category
DauBanHanh createdBoSo = unitOfWork.DauBanHanh.Insert(new DauBanHanh
{
Code = "test",
DauBanHanhUsers = new List<DauBanHanhUser>
{
new DauBanHanhUser
{
UserId = randomUser.Id,
Users = randomUser
}
},
Name = categoryName
});
BoPhanSoanThao createdBoPhanSoanThao = unitOfWork.BoPhanSoanThao.Insert(new BoPhanSoanThao
{
Name = categoryName,
});
ChucVu createdChucVu = unitOfWork.ChucVu.Insert(new ChucVu
{
Name = categoryName
});
KhoiSoanThao createdKhoiSoanThao = unitOfWork.KhoiSoanThao.Insert(new KhoiSoanThao
{
Name = categoryName
});
LoaiTaiLieu createdLoaiTaiLieu = unitOfWork.LoaiTaiLieu.Insert(new LoaiTaiLieu
{
Name = categoryName
});
VanBanThongKe createdVanBanThongKe = unitOfWork.VanBanThongKe.Insert(new VanBanThongKe
{
Name = categoryName
});
Document createdDocument = null;
unitOfWork.SaveChanges();
// Create document
createdDocument = unitOfWork.Document.Insert(new Document
{
Date = Utilities.DateToTimestamp(DateTime.Now),
Name = "test",
Owner = randomUser.Id,
Private = DocumentPrivateStatus.PUBLIC,
Published = DocumentPublishStatus.PUBLISH,
Statistic = 100,
Tomail = "[email protected]",
User = randomUser,
Relation = "",
ShareList = "",
Comment = "",
DocumentCategories = new List<DocumentCategory>
{
new DocumentCategory
{
CategoryID = createdLoaiTaiLieu.Id,
LoaiTaiLieu = createdLoaiTaiLieu
}
},
DocumentContents = new List<DocumentContent>
{
new DocumentContent
{
Checksum = "",
CreatedBy = randomUser.Id,
Date = Utilities.DateToTimestamp(DateTime.Now),
FileSize = 10000,
Dir = "1/",
FileType = ".pdf",
MimeType = "application/pdf",
Version = 1,
OrgFileName = "1p.pdf"
}
},
DocumentAttributes = new List<DocumentAttribute>
{
// nguoi ký duyet
new DocumentAttribute()
{
Attrdef = 1,
Value = randomUser.Id.ToString()
},
// nguoi ban hanh
new DocumentAttribute()
{
Attrdef = 2,
Value = randomUser.Id.ToString()
},
// dau ban hanh
new DocumentAttribute()
{
Attrdef = 3,
Value = createdBoSo.Id.ToString()
},
// ngay ban hanh
new DocumentAttribute()
{
Attrdef = 4,
Value = DateTime.Now.ToString("yyyy-mm-dd")
},
// ngay het hieu luc
new DocumentAttribute()
{
Attrdef = 5,
Value = DateTime.Now.ToString("yyyy-mm-dd")
},
// ngay bat dau hieu luc
new DocumentAttribute()
{
Attrdef = 6,
Value = DateTime.Now.ToString("yyyy-mm-dd")
},
// tinh hieu luc
new DocumentAttribute()
{
Attrdef = 7,
Value = "1"
},
// so van ban
new DocumentAttribute()
{
Attrdef = 8,
Value = "test"
},
// bo phan soan thao
new DocumentAttribute()
{
Attrdef = 9,
Value = createdBoPhanSoanThao.Id.ToString()
},
// nguoi soan thao
new DocumentAttribute()
{
Attrdef = 10,
Value = randomUser.Id.ToString()
},
// khoi soan thao
new DocumentAttribute()
{
Attrdef = 14,
Value = createdKhoiSoanThao.Id.ToString()
},
// chuc danh nguoi ky
new DocumentAttribute()
{
Attrdef = 15,
Value = createdChucVu.Id.ToString()
},
// noi nhan ben ngoai
new DocumentAttribute()
{
Attrdef = 16,
Value = ""
},
// noi gui
new DocumentAttribute()
{
Attrdef = 17,
Value = ""
},
//van ban thong ke
new DocumentAttribute()
{
Attrdef = 18,
Value = createdVanBanThongKe.Id.ToString()
}
}
});
unitOfWork.SaveChanges();
documentId = createdDocument.Id;
transaction.Commit();
}
catch (Exception)
{
transaction.Rollback();
throw;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment