private bool DoAdd()
        {
            bool result = false;

            BLL.common_questions   bll   = new BLL.common_questions();
            Model.common_questions model = new Model.common_questions();

            model.group_id = Convert.ToInt32(this.rbtnGroup.SelectedValue);
            model.type     = Convert.ToInt32(this.rbtnType.SelectedValue);
            model.data_id  = this.chapter;
            model.number   = 0;
            model.title    = Convert.ToString(this.txttitle.Text.Trim());
            model.answer   = (model.type == (int)EnumCollection.questions_type.单选题 || model.type == (int)EnumCollection.questions_type.多选题 ||
                              model.type == (int)EnumCollection.questions_type.判断题) ? this.txtAnswers.Text.Trim().ToUpper() : this.txtAnswers.Text.Trim();
            model.score    = Convert.ToDecimal(this.txtscore.Text.Trim());
            model.analysis = Convert.ToString(this.txtanalysis.Text.Trim());
            model.add_time = System.DateTime.Now;

            int id = bll.Add(model);

            if (id > 0)
            {
                AddAdminLog(EnumCollection.ActionEnum.Add.ToString(), "添加试题信息,主键:" + id); //记录日志
                result = true;
            }
            return(result);
        }
Beispiel #2
0
        private void saveQuestionOptions()
        {
            int    id       = RequestHelper.GetFormInt("id");
            int    group    = RequestHelper.GetFormInt("group", 0);
            int    type     = RequestHelper.GetFormInt("type", 0);
            int    chapter  = RequestHelper.GetFormInt("chapter", 0);
            string title    = RequestHelper.GetFormString("title");
            int    score    = RequestHelper.GetFormInt("score", 0);
            string answers  = RequestHelper.GetFormString("answers");
            string analysis = RequestHelper.GetFormString("analysis");
            string options  = RequestHelper.GetFormString("options");

            Appoa.Web.UI.ManagePage mngPage = new Web.UI.ManagePage();
            BLL.common_questions    bll     = new BLL.common_questions();
            Model.common_questions  model   = bll.GetModel(id);

            if (model != null)
            {
                #region 修改题目信息
                model.group_id = group;
                model.type     = type;
                model.data_id  = chapter;
                model.number   = 0;
                model.title    = title;
                model.answer   = (model.type == (int)EnumCollection.questions_type.单选题 || model.type == (int)EnumCollection.questions_type.多选题 ||
                                  model.type == (int)EnumCollection.questions_type.判断题) ? answers.Trim().ToUpper() : answers.Trim();
                model.score    = score;
                model.analysis = HttpUtility.UrlDecode(analysis, System.Text.Encoding.UTF8);

                if (bll.Update(model))
                {
                    try
                    {
                        #region  择题设置选项
                        if (model.type == (int)EnumCollection.questions_type.单选题 || model.type == (int)EnumCollection.questions_type.多选题 || model.type == (int)EnumCollection.questions_type.判断题)
                        {
                            JArray               optionArr = JsonConvert.DeserializeObject <JArray>(options);
                            BLL.common_answers   anBll     = new BLL.common_answers();
                            Model.common_answers anModel   = null;

                            List <int> ids = new List <int>();
                            foreach (JObject obj in optionArr)
                            {
                                ids.Add(Convert.ToInt32(obj["options_id"].ToString()));
                            }

                            DataTable dt = anBll.GetList(" question_id = " + model.id);
                            foreach (DataRow item in dt.Rows)
                            {
                                int afid = Convert.ToInt32(item["id"]);
                                if (afid > 0)
                                {
                                    if (!ids.Contains(afid))
                                    {
                                        anBll.Delete(afid);
                                    }
                                }
                            }

                            foreach (JObject item in optionArr)
                            {
                                int options_id = Convert.ToInt32(item["options_id"]);
                                anModel = anBll.GetModel(options_id);
                                if (anModel != null)
                                {
                                    #region 修改选项
                                    anModel.question_id = model.id;
                                    anModel.options     = item["options"].ToString();
                                    anModel.contents    = item["options_contents"].ToString();
                                    anModel.score       = Convert.ToInt32(item["options_score"].ToString());
                                    if (anBll.Update(anModel))
                                    {
                                        mngPage.AddAdminLog(EnumCollection.ActionEnum.Modify.ToString(), "修改选项信息,主键:" + options_id); //记录日志
                                    }
                                    #endregion
                                }
                                else
                                {
                                    #region 添加选项
                                    anModel             = new Model.common_answers();
                                    anModel.question_id = model.id;
                                    anModel.options     = item["options"].ToString();
                                    anModel.contents    = item["options_contents"].ToString();
                                    anModel.score       = Convert.ToInt32(item["options_score"].ToString());
                                    anModel.add_time    = System.DateTime.Now;

                                    int anid = anBll.Add(anModel);
                                    if (anid > 0)
                                    {
                                        mngPage.AddAdminLog(EnumCollection.ActionEnum.Add.ToString(), "添加选项信息,主键:" + anid); //记录日志
                                    }
                                    #endregion
                                }
                            }
                        }
                        #endregion
                    }
                    catch (Exception e)
                    {
                        writeMsgError("系统错误:" + e.Message);
                    }

                    mngPage.AddAdminLog(EnumCollection.ActionEnum.Modify.ToString(), "修改试题信息,主键:" + model.id); //记录日志

                    writeMsgSuccess("修改题目信息成功");
                }
                else
                {
                    writeMsgError("修改题目信息失败");
                }
                #endregion
            }
            else
            {
                #region 添加题目信息
                model          = new Model.common_questions();
                model.group_id = group;
                model.type     = type;
                model.data_id  = chapter;
                model.number   = 0;
                model.title    = title;
                model.answer   = (model.type == (int)EnumCollection.questions_type.单选题 || model.type == (int)EnumCollection.questions_type.多选题 ||
                                  model.type == (int)EnumCollection.questions_type.判断题) ? answers.Trim().ToUpper() : answers.Trim();
                model.score    = score;
                model.analysis = HttpUtility.UrlDecode(analysis, System.Text.Encoding.UTF8);
                model.add_time = System.DateTime.Now;

                int qid = bll.Add(model);
                if (qid > 0)
                {
                    try
                    {
                        #region  择题设置选项
                        if (model.type == (int)EnumCollection.questions_type.单选题 || model.type == (int)EnumCollection.questions_type.多选题 || model.type == (int)EnumCollection.questions_type.判断题)
                        {
                            JArray               optionArr = JsonConvert.DeserializeObject <JArray>(options);
                            BLL.common_answers   anBll     = new BLL.common_answers();
                            Model.common_answers anModel   = null;

                            foreach (JObject item in optionArr)
                            {
                                int options_id = Convert.ToInt32(item["options_id"]);
                                if (options_id > 0)
                                {
                                    #region 修改选项
                                    anModel = anBll.GetModel(options_id);
                                    if (anModel != null)
                                    {
                                        anModel.question_id = qid;
                                        anModel.options     = item["options"].ToString();
                                        anModel.contents    = item["options_contents"].ToString();
                                        anModel.score       = Convert.ToInt32(item["options_score"].ToString());
                                        if (anBll.Update(anModel))
                                        {
                                            mngPage.AddAdminLog(EnumCollection.ActionEnum.Modify.ToString(), "修改选项信息,主键:" + options_id); //记录日志
                                        }
                                    }
                                    #endregion
                                }
                                else
                                {
                                    #region 添加选项
                                    anModel             = new Model.common_answers();
                                    anModel.question_id = qid;
                                    anModel.options     = item["options"].ToString();
                                    anModel.contents    = item["options_contents"].ToString();
                                    anModel.score       = Convert.ToInt32(item["options_score"].ToString());
                                    anModel.add_time    = System.DateTime.Now;

                                    int anid = anBll.Add(anModel);
                                    if (anid > 0)
                                    {
                                        mngPage.AddAdminLog(EnumCollection.ActionEnum.Add.ToString(), "添加选项信息,主键:" + anid); //记录日志
                                    }
                                    #endregion
                                }
                            }
                        }
                        #endregion
                    }
                    catch (Exception e)
                    {
                        writeMsgError("系统错误:" + e.Message);
                    }

                    mngPage.AddAdminLog(EnumCollection.ActionEnum.Add.ToString(), "添加试题信息,主键:" + qid); //记录日志

                    writeMsgSuccess("添加题目信息成功");
                }
                else
                {
                    writeMsgError("添加题目信息失败");
                }
                #endregion
            }
        }
        //保存
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            ChkAdminLevel("_ybd_common_questions", EnumCollection.ActionEnum.Add.ToString()); //检查权限

            #region
            string strError = string.Empty;
            if (txtgroup_id.Text.Trim() == "" || txtgroup_id.Text.Trim().Length > 4)
            {
                strError += "分组ID为空或超出长度![br]";
            }
            if (txttype.Text.Trim() == "" || txttype.Text.Trim().Length > 4)
            {
                strError += "题型为空或超出长度![br]";
            }
            if (txtdata_id.Text.Trim() == "" || txtdata_id.Text.Trim().Length > 4)
            {
                strError += "关联数据ID为空或超出长度![br]";
            }
            if (txtnumber.Text.Trim() == "" || txtnumber.Text.Trim().Length > 4)
            {
                strError += "序号为空或超出长度![br]";
            }
            if (txttitle.Text.Trim() == "" || txttitle.Text.Trim().Length > 200)
            {
                strError += "标题为空或超出长度![br]";
            }
            if (txtanswer.Text.Trim() == "" || txtanswer.Text.Trim().Length > 4000)
            {
                strError += "参考答案为空或超出长度![br]";
            }
            if (txtscore.Text.Trim() == "" || txtscore.Text.Trim().Length > 5)
            {
                strError += "分值为空或超出长度![br]";
            }
            if (txtanalysis.Text.Trim() == "" || txtanalysis.Text.Trim().Length > 2000)
            {
                strError += "解析为空或超出长度![br]";
            }
            if (txtadd_time.Text.Trim() == "" || txtadd_time.Text.Trim().Length > 8)
            {
                strError += "添加时间为空或超出长度![br]";
            }

            if (strError != string.Empty)
            {
                JscriptMsg(strError, "", "Error");
                return;
            }
            #endregion

            Model.common_questions model = new Model.common_questions();
            BLL.common_questions   bll   = new BLL.common_questions();

            model.group_id = Convert.ToInt32(txtgroup_id.Text);
            model.type     = Convert.ToInt32(txttype.Text);
            model.data_id  = Convert.ToInt32(txtdata_id.Text);
            model.number   = Convert.ToInt32(txtnumber.Text);
            model.title    = Convert.ToString(txttitle.Text);
            model.answer   = Convert.ToString(txtanswer.Text);
            model.score    = Convert.ToDecimal(txtscore.Text);
            model.analysis = Convert.ToString(txtanalysis.Text);
            model.add_time = Convert.ToDateTime(txtadd_time.Text);

            int id = bll.Add(model);
            if (id > 0)
            {
                AddAdminLog(EnumCollection.ActionEnum.Add.ToString(), "添加题目信息信息,主键:" + id); //记录日志
                JscriptMsg("添加题目信息信息成功!", "Manage.aspx", "");
            }
            else
            {
                JscriptMsg("保存过程中发生错误!", "");
            }
        }
        private DataTable ExcelToDataTable(string filePath, bool isColumnName)
        {
            int chapter = RequestHelper.GetQueryInt("chapter");

            DataTable  dataTable     = null;
            DataTable  dtOption      = null;
            FileStream fs            = null;
            DataColumn column        = null;
            DataRow    dataRow       = null;
            IWorkbook  workbook      = null;
            ISheet     sheetQuestion = null;
            ISheet     sheetOption   = null;
            IRow       row           = null;
            ICell      cell          = null;
            int        startRow      = 0;

            BLL.common_questions qbll = new BLL.common_questions();
            BLL.common_answers   obll = new BLL.common_answers();
            int group = Convert.ToInt32(this.rbtnGroup.SelectedValue);

            try
            {
                using (fs = File.OpenRead(filePath))
                {
                    // 2007版本
                    if (filePath.IndexOf(".xlsx") > 0)
                    {
                        workbook = new XSSFWorkbook(fs);
                    }
                    // 2003版本
                    else if (filePath.IndexOf(".xls") > 0)
                    {
                        workbook = new HSSFWorkbook(fs);
                    }

                    Dictionary <string, int> dic = GetQuestionDic();
                    if (workbook != null)
                    {
                        #region 问题表
                        sheetQuestion = workbook.GetSheetAt(0);//读取第一个sheetQuestion,当然也可以循环读取每个sheetQuestion
                        dataTable     = new DataTable();
                        if (sheetQuestion != null)
                        {
                            int rowCount = sheetQuestion.LastRowNum;//总行数
                            if (rowCount > 0)
                            {
                                IRow firstRow  = sheetQuestion.GetRow(0); //第一行
                                int  cellCount = firstRow.LastCellNum;    //列数

                                //构建datatable的列
                                if (isColumnName)
                                {
                                    startRow = 1;//如果第一行是列名,则从第二行开始读取
                                    for (int i = firstRow.FirstCellNum; i < cellCount; ++i)
                                    {
                                        cell = firstRow.GetCell(i);
                                        if (cell != null)
                                        {
                                            if (cell.StringCellValue != null)
                                            {
                                                column = new DataColumn(cell.StringCellValue);
                                                dataTable.Columns.Add(column);
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    for (int i = firstRow.FirstCellNum; i < cellCount; ++i)
                                    {
                                        column = new DataColumn("column" + (i + 1));
                                        dataTable.Columns.Add(column);
                                    }
                                }

                                //填充行
                                for (int i = startRow; i <= rowCount; ++i)
                                {
                                    row = sheetQuestion.GetRow(i);
                                    if (row == null)
                                    {
                                        continue;
                                    }

                                    dataRow = dataTable.NewRow();
                                    for (int j = row.FirstCellNum; j < cellCount; ++j)
                                    {
                                        cell = row.GetCell(j);
                                        if (cell == null)
                                        {
                                            dataRow[j] = "";
                                        }
                                        else
                                        {
                                            //CellType(Unknown = -1,Numeric = 0,String = 1,Formula = 2,Blank = 3,Boolean = 4,Error = 5,)
                                            switch (cell.CellType)
                                            {
                                            case CellType.Blank:
                                                dataRow[j] = "";
                                                break;

                                            case CellType.Numeric:
                                                short format = cell.CellStyle.DataFormat;
                                                //对时间格式(2015.12.5、2015/12/5、2015-12-5等)的处理
                                                if (format == 14 || format == 31 || format == 57 || format == 58)
                                                {
                                                    dataRow[j] = cell.DateCellValue;
                                                }
                                                else
                                                {
                                                    dataRow[j] = cell.NumericCellValue;
                                                }
                                                break;

                                            case CellType.String:
                                                dataRow[j] = cell.StringCellValue;
                                                break;
                                            }
                                        }
                                    }
                                    dataTable.Rows.Add(dataRow);
                                }
                            }
                        }
                        #endregion

                        Model.common_questions question = null;

                        foreach (DataRow item in dataTable.Rows)
                        {
                            question = qbll.GetModel(" group_id = " + group + " and title = '" + item[0].ToString() + "' ");
                            if (question == null)
                            {
                                question          = new Model.common_questions();
                                question.group_id = group;
                                question.type     = dic[item[1].ToString()];
                                question.data_id  = chapter;
                                question.number   = 0;
                                question.title    = item[0].ToString();
                                question.answer   = item[2].ToString();
                                question.score    = Convert.ToDecimal(item[3]);
                                question.analysis = item[4].ToString();
                                question.add_time = System.DateTime.Now;

                                qbll.Add(question);
                            }
                            else
                            {
                                question.group_id = group;
                                question.type     = dic[item[1].ToString()];
                                question.data_id  = chapter;
                                question.number   = 0;
                                question.answer   = item[2].ToString();
                                question.score    = Convert.ToDecimal(item[3]);
                                question.analysis = item[4].ToString();

                                qbll.Update(question);
                            }
                        }

                        #region  项表
                        sheetOption = workbook.GetSheetAt(1);//读取第一个sheetOption,当然也可以循环读取每个sheetOption
                        dtOption    = new DataTable();
                        if (sheetOption != null)
                        {
                            int rowCount = sheetOption.LastRowNum;//总行数
                            if (rowCount > 0)
                            {
                                IRow firstRow  = sheetOption.GetRow(0); //第一行
                                int  cellCount = firstRow.LastCellNum;  //列数

                                //构建datatable的列
                                if (isColumnName)
                                {
                                    startRow = 1;//如果第一行是列名,则从第二行开始读取
                                    for (int i = firstRow.FirstCellNum; i < cellCount; ++i)
                                    {
                                        cell = firstRow.GetCell(i);
                                        if (cell != null)
                                        {
                                            if (cell.StringCellValue != null)
                                            {
                                                column = new DataColumn(cell.StringCellValue);
                                                dtOption.Columns.Add(column);
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    for (int i = firstRow.FirstCellNum; i < cellCount; ++i)
                                    {
                                        column = new DataColumn("column" + (i + 1));
                                        dtOption.Columns.Add(column);
                                    }
                                }

                                //填充行
                                for (int i = startRow; i <= rowCount; ++i)
                                {
                                    row = sheetOption.GetRow(i);
                                    if (row == null)
                                    {
                                        continue;
                                    }

                                    dataRow = dtOption.NewRow();
                                    for (int j = row.FirstCellNum; j < cellCount; ++j)
                                    {
                                        cell = row.GetCell(j);
                                        if (cell == null)
                                        {
                                            dataRow[j] = "";
                                        }
                                        else
                                        {
                                            //CellType(Unknown = -1,Numeric = 0,String = 1,Formula = 2,Blank = 3,Boolean = 4,Error = 5,)
                                            switch (cell.CellType)
                                            {
                                            case CellType.Blank:
                                                dataRow[j] = "";
                                                break;

                                            case CellType.Numeric:
                                                short format = cell.CellStyle.DataFormat;
                                                //对时间格式(2015.12.5、2015/12/5、2015-12-5等)的处理
                                                if (format == 14 || format == 31 || format == 57 || format == 58)
                                                {
                                                    dataRow[j] = cell.DateCellValue;
                                                }
                                                else
                                                {
                                                    dataRow[j] = cell.NumericCellValue;
                                                }
                                                break;

                                            case CellType.String:
                                                dataRow[j] = cell.StringCellValue;
                                                break;
                                            }
                                        }
                                    }
                                    dtOption.Rows.Add(dataRow);
                                }
                            }
                        }
                        #endregion

                        Model.common_questions qModle = null;
                        Model.common_answers   option = null;

                        foreach (DataRow item in dtOption.Rows)
                        {
                            qModle = qbll.GetModel(" group_id = " + group + " and title = '" + item[0].ToString() + "' ");
                            if (qModle != null)
                            {
                                option = obll.GetModel(" question_id = " + qModle.id + " and contents = '" + item[2].ToString() + "' ");
                                if (option == null)
                                {
                                    option             = new Model.common_answers();
                                    option.question_id = qModle.id;
                                    option.options     = item[1].ToString();
                                    option.contents    = item[2].ToString();
                                    option.score       = dtOption.Columns.Count > 3 ? Convert.ToInt32(item[3].ToString()) : 0;
                                    option.add_time    = System.DateTime.Now;

                                    obll.Add(option);
                                }
                                else
                                {
                                    option.options = item[1].ToString();
                                    option.score   = dtOption.Columns.Count > 3 ? Convert.ToInt32(item[3].ToString()) : 0;

                                    obll.Update(option);
                                }
                            }
                        }
                    }
                }
                return(dataTable);
            }
            catch (Exception)
            {
                if (fs != null)
                {
                    fs.Close();
                }
                return(null);
            }
        }