using System;
using System.Data;
using System.Configuration;
using core.BusinessObject;
using core.DataAccessLayer;
using System.Collections;
using System.Collections.Generic;
using System.IO;

/// <summary>
/// PaperLogicManager 的摘要说明
/// </summary>
/// 
namespace core.BusinessLogicLayer
{
    public class PaperLogicManager
    {
        System.Collections.Generic.List<PaperTitleNum> list=new System.Collections.Generic.List<PaperTitleNum>();
        System.Data.DataSet ds = new DataSet();
        private System.Collections.ArrayList arrds = new System.Collections.ArrayList();
        core.DataAccessLayer.PaperLogicDataAccess paperLogicDataAccess = new PaperLogicDataAccess();
        public PaperLogicManager()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }

        public ArrayList getarr()
        {
            this.arrds = this.paperLogicDataAccess.getlist();
            return this.arrds;
        }

        #region 该方法用来将每次的题目信息进行获取，然后再从数据库中获取出对应的数据
        public string paperMainLogic(core.BusinessObject.PaperTitleNum paperTitleNum,PaperHeadInfo paperHeadInfo,ArrayList arr,bool flag)
        {
            int i = 0;
            this.ds = (DataSet)arr[1];
            if (paperTitleNum.courseChapterFrom >= paperTitleNum.courseChapterTo)
            {
                return "起始章节落后于末章节，不合理，请重新填充";
            }
            if (flag)
            {
                for (; i < this.ds.Tables.Count; i++)
                {
                    if (paperTitleNum.titleType.Equals(this.ds.Tables[i].TableName.ToString()))
                        return "该题型已存在，如果要在该题型上继续添加的话，请点击添加按钮，否则请重新选择题型！！！";
                }
            }
            
            if (i >= list.Count)
            {
                paperTitleNum = core.DataAccessLayer.PaperLogicDataAccess.getThemeTypeID(paperTitleNum);
                string str_re = paperLogicDataAccess.getTheme(paperTitleNum, paperHeadInfo,arr,flag);
                if (str_re.Equals("试题已经成功获取"))
                {
                    list.Add(paperTitleNum);
                }
                else
                {
                    return str_re;
                }
            }
            return "试题已经成功获取";

        }
        #endregion

        #region 将试卷的标头信息用该方法来处理
        public bool paperThemeLogic()
        {
            return false;
        }
        #endregion

        #region
        public string insertPaperInfo(PaperHeadInfo paperHeadInfo, PaperInfo paperInfo, List<PaperTitleNum> list)
        {

            string str_sql = "insert into paperInformation values(" + paperHeadInfo.paperID + ",'" + paperHeadInfo .teacherID+
                "','" + paperHeadInfo.courseID + "','" + paperInfo.suitSpecialty +
                "','" + paperInfo.testType + "','" + paperInfo .paperTime+ "')";
            string str_result=this.paperLogicDataAccess.setData(str_sql);
            for (int i = 0; i < list.Count; i++)
            {
                PaperTitleNum paperTitleNum = list[i];
                str_sql = "insert into paperTitleNumber values(" + paperHeadInfo.paperID + ",'" +
                    paperHeadInfo.teacherID + "','" + paperTitleNum.titleType + "','" + paperTitleNum.titleNumber + "','" + paperTitleNum.difficultFactor+
                    "'," + paperTitleNum.courseChapterFrom + "," + paperTitleNum .courseChapterTo+ ")";
                str_result = this.paperLogicDataAccess.setData(str_sql);
            }
            return str_result;
        }
        #endregion

        #region

        public PaperHeadInfo getPaperHeadInfoID(PaperHeadInfo paperHeadInfo)
        {
            return core.DataAccessLayer.PaperLogicDataAccess.getID(paperHeadInfo);
        }

        #endregion
        #region
        public byte[] get_Byte(string fileName)
        {
            FileInfo fi = new FileInfo(fileName);
            FileStream fs = fi.OpenRead();
            byte[] bytes = new byte[fs.Length];
            fs.Read(bytes, 0, Convert.ToInt32(fs.Length));
            return bytes;
        }
        #endregion

        #region
        public string set_Paper(PaperHeadInfo paperHeadInfo, PaperInfo paperInfo, byte[] bytes)
        {
            string str_sql = "insert into paperInformation values(" + paperHeadInfo.paperID + ",'" + paperHeadInfo.teacherID +
                "','" + paperHeadInfo.courseID + "','" + paperInfo.suitSpecialty +
                "','" + paperInfo.testType + "','" + paperInfo.paperTime + "')";
            string str_result = this.paperLogicDataAccess.setData(str_sql);
            str_sql = "insert into paper (paperID,teacherID,courseID,PaperContent) values(" + paperHeadInfo.paperID + ",'" + paperHeadInfo.teacherID + "','" + paperHeadInfo.courseID + "',@file)";
            str_result = this.paperLogicDataAccess.setData(bytes, str_sql);
            return str_result;
        }
        #endregion
    }
}
