/// <summary>
 /// Kiểm tra và thêm mới Phat
 /// </summary>
 /// <param name="entity">Entity</param>
 /// <returns>Int32: ID của Phat Mới Thêm Vào</returns>
 public static Int32 Add(PhatEntity entity)
 {
     checkLogic(entity);
     checkDuplicate(entity, false);
     checkFK(entity);
     return PhatDAL.Add(entity);
 }
 /// <summary>
 /// Kiểm tra và chỉnh sửa Phat
 /// </summary>
 /// <param name="entity">PhatEntity</param>
 /// <returns>bool:kết quả thực hiện</returns>
 public static bool Edit(PhatEntity entity)
 {
     checkExist(entity.PK_iPhatDinhchiID);
     checkLogic(entity);
     checkDuplicate(entity, true);
     checkFK(entity);
     return PhatDAL.Edit(entity);
 }
 protected void btnXuly_Click(object sender, EventArgs e)
 {
     PhatEntity oPhat = new PhatEntity();
     oPhat.FK_iCosonuoiID = Convert.ToInt32(btnXuly.CommandArgument);
     oPhat.iMucdo = Convert.ToInt16(ddlHinhthucxuly.SelectedValue);
     oPhat.sLydo = txtLydo.Text;
     oPhat.dNgaythuchien = DateTime.Today;
     PhatBRL.Add(oPhat);
     if (Convert.ToInt64(Session["groupID"].ToString()) == 1)
         Response.Write("<script language=\"javascript\">alert('Xử lý thành công!');location='Default.aspx?page=Cosonuoitrong';</script>");
     else if (Convert.ToInt64(Session["groupID"].ToString()) == 4)
         Response.Write("<script language=\"javascript\">alert('Xử lý thành công!');location='Default.aspx?page=Cosonuoitrong&ctr=adm';</script>");
 }
 /// <summary>
 /// Kiểm tra logic Entity
 /// </summary>
 /// <param name="entity">PhatEntity: entity</param>
 private static void checkLogic(PhatEntity entity)
 {
     if (String.IsNullOrEmpty(entity.sLydo))
         throw new Exception(EX_SLYDO_EMPTY);
     if (entity.iMucdo < 0)
         throw new Exception(EX_IMUCDO_INVALID);
     if (entity.FK_iCosonuoiID < 0)
         throw new Exception(EX_FK_ICOSONUOIID_INVALID);
     if (DateTime.Parse("1753-01-01")>entity.dNgaythuchien)
         throw new Exception(EX_DNGAYTHUCHIEN_INVALID);
 }
 /// <summary>
 /// Kiểm tra tồn tại khóa ngoại
 /// </summary>
 /// <param name="entity">PhatEntity:entity</param>
 private static void checkFK(PhatEntity entity)
 {
     CosonuoitrongEntity oCosonuoitrong = CosonuoitrongDAL.GetOne(entity.FK_iCosonuoiID);
     if (oCosonuoitrong==null)
     {
         throw new Exception("Không tìm thấy :FK_iCosonuoiID");
     }
 }
 /// <summary>
 /// Kiểm tra trùng lặp bản ghi
 /// </summary>
 /// <param name="entity">PhatEntity: PhatEntity</param>
 private static void checkDuplicate(PhatEntity entity,bool checkPK)
 {
     /*
     Example
     List<PhatEntity> list = PhatDAL.GetAll();
     if (list.Exists(
         delegate(PhatEntity oldEntity)
         {
             bool result =oldEntity.FIELD.Equals(entity.FIELD, StringComparison.OrdinalIgnoreCase);
             if(checkPK)
                 result=result && oldEntity.PK_iPhatDinhchiID != entity.PK_iPhatDinhchiID;
             return result;
         }
     ))
     {
         list.Clear();
         throw new Exception(EX_FIELD_EXISTED);
     }
     */
 }