public int update(AnswerModels item)
 {
     StringBuilder sqlBuilder = new StringBuilder();
     sqlBuilder.Append("UPDATE ");
     sqlBuilder.Append("gov_answer ");
     sqlBuilder.Append("SET ");
     sqlBuilder.Append("question_id = @1, ");
     sqlBuilder.Append("answer_content = @2, ");
     sqlBuilder.Append("answer_username = @3, ");
     sqlBuilder.Append("update_username = @4, ");
     sqlBuilder.Append("answer_datetime = @5, ");
     sqlBuilder.Append("update_datetime = @6, ");
     sqlBuilder.Append("hidden_flg = @7 ");
     sqlBuilder.Append("WHERE ");
     sqlBuilder.Append("id = @8 ");
     this.Sql = sqlBuilder.ToString();
     MySqlCommand objCmd = new MySqlCommand(Sql, getConnection());
     objCmd.Parameters.AddWithValue("@1", item.QuestionId);
     objCmd.Parameters.AddWithValue("@2", item.AnswerContent);
     objCmd.Parameters.AddWithValue("@3", item.AnswerUsername);
     objCmd.Parameters.AddWithValue("@4", item.UpdateUsername);
     objCmd.Parameters.AddWithValue("@5", item.AnswerDatetime);
     objCmd.Parameters.AddWithValue("@6", item.UpdateDatetime);
     objCmd.Parameters.AddWithValue("@7", item.HiddenFlg);
     objCmd.Parameters.AddWithValue("@8", item.Id);
     int rs = objCmd.ExecuteNonQuery();
     return rs;
 }
 public int insert(AnswerModels item)
 {
     StringBuilder sqlBuilder = new StringBuilder();
     sqlBuilder.Append("INSERT ");
     sqlBuilder.Append("INTO ");
     sqlBuilder.Append("gov_answer ");
     sqlBuilder.Append("( ");
     sqlBuilder.Append("question_id, ");
     sqlBuilder.Append("answer_content, ");
     sqlBuilder.Append("answer_username, ");
     sqlBuilder.Append("update_username, ");
     sqlBuilder.Append("answer_datetime, ");
     sqlBuilder.Append("update_datetime, ");
     sqlBuilder.Append("hidden_flg ");
     sqlBuilder.Append(") ");
     sqlBuilder.Append("values(");
     sqlBuilder.Append("@1, @2, @3, @4, @5, @6, @7");
     sqlBuilder.Append(")");
     this.Sql = sqlBuilder.ToString();
     MySqlCommand objCmd = new MySqlCommand(Sql, getConnection());
     objCmd.Parameters.AddWithValue("@1", item.QuestionId);
     objCmd.Parameters.AddWithValue("@2", item.AnswerContent);
     objCmd.Parameters.AddWithValue("@3", item.AnswerUsername);
     objCmd.Parameters.AddWithValue("@4", item.UpdateUsername);
     objCmd.Parameters.AddWithValue("@5", item.AnswerDatetime);
     objCmd.Parameters.AddWithValue("@6", item.UpdateDatetime);
     objCmd.Parameters.AddWithValue("@7", item.HiddenFlg);
     int rs = objCmd.ExecuteNonQuery();
     return rs;
 }
        public ActionResult SaveRegist(AnswerModels item) {
            item.HiddenFlg = false;
            item.AnswerUsername = "******";
            item.AnswerDatetime = DateTime.Now;
            item.UpdateDatetime = DateTime.Now;
            item.UpdateUsername = "******";

            AnswerServices answerServices = new AnswerServices();
            answerServices.insert(item);

            QuestionServices questionServices = new QuestionServices();
            questionServices.Id = item.QuestionId.ToString();
            List<QuestionModels> lst = questionServices.select(-1, -1);
            if(lst != null && lst.Count > 0){
                QuestionModels questionInfo = new QuestionModels();
                questionInfo =  lst.First();
                questionInfo.HiddenFlg = false;
                questionInfo.QuestionDatetime = DateTime.Now;
                questionServices = new QuestionServices();
                questionServices.update(questionInfo);
            }

            return Redirect("/admin/question/answer?questionid=" + item.QuestionId.ToString());
        }
        public List<AnswerModels> select(int page, int limit)
        {
            int offset = (page - 1) * limit;
            StringBuilder sqlBuilder = new StringBuilder();
            sqlBuilder.Append("SELECT ");
            sqlBuilder.Append("* ");
            sqlBuilder.Append("FROM ");
            sqlBuilder.Append("gov_answer gaw");
            sqlBuilder.Append(getBaseSQL());
            if (page > 0 && limit > 0)
            {
                sqlBuilder.Append(" LIMIT @limit ");
                sqlBuilder.Append(" OFFSET @offset ");
            }
            this.Sql = sqlBuilder.ToString();

            MySqlCommand objCmd = new MySqlCommand(Sql, getConnection());
            if (page > 0 && limit > 0)
            {
                objCmd.Parameters.AddWithValue("@limit", limit);
                objCmd.Parameters.AddWithValue("@offset", offset);
            }

            MySqlDataReader dataReader = objCmd.ExecuteReader();
            List<AnswerModels> lstDocument = new List<AnswerModels>();
            AnswerModels item;
            while (dataReader.Read())
            {
                item = new AnswerModels();
                item.Id = Convert.ToInt32(dataReader["id"]);
                item.AnswerContent = dataReader["answer_content"].ToString();
                item.QuestionId = Convert.ToInt32(dataReader["question_id"]);
                item.UpdateUsername = dataReader["update_username"].ToString();
                item.UpdateDatetime = Convert.ToDateTime(dataReader["update_datetime"]);
                item.AnswerDatetime = Convert.ToDateTime(dataReader["answer_datetime"]);
                item.AnswerUsername = dataReader["answer_username"].ToString();
                item.HiddenFlg = Convert.ToBoolean(dataReader["hidden_flg"]);
                lstDocument.Add(item);
            }
            getConnection().Close();
            return lstDocument;
        }