/*
         * 返回推荐文件的信息
         * userName:用户姓名
         * courseName:课程名
         * 推荐进一步学习时,值得一看的文件
         */
        public static List<CommandFile> commandAdvanced(string userName, string courseName)
        {
            DataTable commands = ExcelProcess.readExcel("CommandFiles\\" + courseName + "\\UK_command.csv").Tables[0];
            DataRow dr = null;
            for (int i = 0; i < commands.Rows.Count; i++)
            {
                if (commands.Rows[i]["User"].ToString().ToLower().Equals(userName.ToLower()))
                    dr = commands.Rows[i];
            }
            if (dr != null)
            {
                //利用pagerank算法,利用各知识点的推荐指数对包含该知识点的文件投票
                //改进公式:Rat(F1) = PR(K1)/C(K1) + PR(K2)/C(K2) + ... + PR(Kn)/C(Kn)
                //Rat(F1)为文件F1的得分,该文件包含知识点K1,K2,...,Kn
                //其中PR(K1)表示知识点K1的推荐指数,C(K1)为包含该知识点的文件个数
                DataTable files = GetData.getUnreadFile(userName, courseName);
                DataTable kSet = GetData.getKnowledge(courseName);
                List<CommandFile> commandFiles = new List<CommandFile>();
                List<Knowledge> Kn = new List<Knowledge>();
                for (int i = 0; i < kSet.Rows.Count; i++)
                {
                    Knowledge k = new Knowledge();
                    k.ID = Int16.Parse(kSet.Rows[i]["ID"].ToString());
                    k.Name = kSet.Rows[i]["Name"].ToString();
                    //此处模糊处理,不再以章知识点和节知识点分别处理
                    k.FileNum = GetData.getFileNumByKName(k.Name, courseName);
                    Kn.Add(k);
                }
                for (int i = 0; i < files.Rows.Count; i++)
                {
                    CommandFile f = new CommandFile();
                    f.ID = Int16.Parse(files.Rows[i]["ID"].ToString());
                    f.Name = files.Rows[i]["description"].ToString();
                    f.Url = files.Rows[i]["url"].ToString();
                    f.Cate = Int16.Parse(files.Rows[i]["cate"].ToString());
                    double expo = 0.0;
                    DataTable kInFile = GetData.getKnowledgeFromFile(Int16.Parse(files.Rows[i]["ID"].ToString()));
                    for (int j = 0; j < kInFile.Rows.Count; j++)
                    {
                        for (int k = 0; k < Kn.Count; k++)
                        {
                            if (Int16.Parse(kInFile.Rows[j]["ID"].ToString()) == Kn[k].ID)
                                expo += Double.Parse(dr[Kn[k].Name].ToString())/Kn[k].FileNum;
                        }
                    }
                    f.Expo = expo;

                    //按序插入
                    int index = 0;
                    for (; index < commandFiles.Count; index++)
                    {
                        if (f.Expo >= commandFiles[index].Expo)
                        {
                            commandFiles.Insert(index, f);
                            break;
                        }
                        else if (index < commandFiles.Count - 1 && f.Expo < commandFiles[index].Expo && f.Expo >= commandFiles[index + 1].Expo)
                        {
                            commandFiles.Insert(index + 1, f);
                            break;
                        }
                        else if (index == commandFiles.Count - 1 && f.Expo >= commandFiles[index].Expo)
                        {
                            commandFiles.Insert(index, f);
                            break;
                        }
                        else if (index == commandFiles.Count - 1 && f.Expo < commandFiles[index].Expo)
                        {
                            commandFiles.Add(f);
                            break;
                        }
                    }
                    if (index == 0)
                        commandFiles.Add(f);
                }

                //for (int showIndex = 0; showIndex < commandFiles.Count && showIndex < 10; showIndex++)
                //{
                //    Console.WriteLine(commandFiles[showIndex].Name + "-->" + commandFiles[showIndex].Url);
                //}
                return commandFiles;
            }

            return null;
        }
 /*
  * 返回推荐信息
  * userName:用户姓名
  * hasSeen:用户已经接触过的案例集,如第二日问题
  * 推荐那些用户还没看,但包含用户已经接触过的案例的文件
  */
 public static List<CommandFile> commandSameCase(string userName, DataTable hasSeen)
 {
     List<CommandFile> files = new List<CommandFile>();
     for (int i = 0; i < hasSeen.Rows.Count; i++)
     {
         DataTable dt = GetData.getUnreadFileByKID(Int16.Parse(hasSeen.Rows[i]["ID"].ToString()), userName);
         for (int j = 0; j < dt.Rows.Count; j++)
         {
             int k = 0;
             for (; k < files.Count; k++)
             {
                 if (Int16.Parse(dt.Rows[j]["ID"].ToString()) == files[k].ID)
                 {
                     files[k].caseKnowledge += hasSeen.Rows[i]["Name"].ToString() + " ";
                     break;
                 }
             }
             if (k >= files.Count)
             {
                 CommandFile f = new CommandFile();
                 f.ID = Int16.Parse(dt.Rows[j]["ID"].ToString());
                 f.Name = dt.Rows[j]["description"].ToString();
                 f.Url = dt.Rows[j]["url"].ToString();
                 f.Cate = Int16.Parse(dt.Rows[j]["cate"].ToString());
                 f.caseKnowledge = hasSeen.Rows[i]["Name"].ToString() + " ";
                 files.Add(f);
             }
         }
     }
     //for (int showIndex = 0; showIndex < files.Count && showIndex < 10; showIndex++)
     //{
     //    Console.WriteLine(files[showIndex].Name + "-->" + files[showIndex].Url + ":" + files[showIndex].caseKnowledge);
     //}
     return files;
 }
        /*
         * 返回推荐文件信息
         * userName:用户姓名
         * courseName:课程名
         * 推荐需要引起注意的知识点,即大家都看了,而自己没有看的文件
         */
        public static List<CommandFile> commandNecessary(string userName, string courseName, int cate)
        {
            string strCate = "";
            switch (cate)
            {
                case Category.COURSEWARE: strCate = "Courseware"; break;
                case Category.CASE: strCate = "Case"; break;
                case Category.VEDIO: strCate = "Vedio"; break;
            }
            DataTable commands = ExcelProcess.readExcel("CommandFiles\\" + courseName + "\\UF_" + strCate + ".csv").Tables[0];
            DataRow dr = null;
            for (int i = 0; i < commands.Rows.Count; i++)
            {
                if (commands.Rows[i]["User"].ToString().ToLower().Equals(userName.ToLower()))
                    dr = commands.Rows[i];
            }
            if (dr != null)
            {
                List<CommandFile> commandFiles = new List<CommandFile>();

                //其他用户对文件f的浏览情况
                //Pi(f) = Sum(U1(f)U2(f)...Ui-1(f)Ui+1(f)...Un(f))
                for (int i = 1; i < commands.Columns.Count; i++)
                {
                    //该用户没看过该文件
                    if (dr[commands.Columns[i].ColumnName].ToString().Length == 0)
                    {
                        CommandFile f = new CommandFile();
                        string[] info = commands.Columns[i].ColumnName.Split(new char[] { '/' });
                        f.ID = Int16.Parse(info[1]);
                        f.Name = info[0];
                        f.Url = GetData.getUrlByCaseID(f.ID);
                        f.Cate = GetData.getCateByCaseID(f.ID);
                        double expo = 0.0;
                        for (int j = 0; j < commands.Rows.Count; j++)
                        {
                            if ((!commands.Rows[j]["User"].ToString().Equals(userName)) && commands.Rows[j][commands.Columns[i].ColumnName].ToString().Length > 0)
                                expo += Double.Parse(commands.Rows[j][commands.Columns[i].ColumnName].ToString());
                        }
                        f.Expo = expo;

                        if (f.Expo > 10E-6)
                        {
                            //按序插入
                            int index = 0;
                            for (; index < commandFiles.Count; index++)
                            {
                                if (f.Expo > commandFiles[index].Expo)
                                {
                                    commandFiles.Insert(index, f);
                                    break;
                                }
                                else if (index < commandFiles.Count - 1 && f.Expo <= commandFiles[index].Expo && f.Expo > commandFiles[index + 1].Expo)
                                {
                                    commandFiles.Insert(index + 1, f);
                                    break;
                                }
                                else if (index == commandFiles.Count - 1 && f.Expo > commandFiles[index].Expo)
                                {
                                    commandFiles.Insert(index, f);
                                    break;
                                }
                                else if (index == commandFiles.Count - 1 && f.Expo <= commandFiles[index].Expo)
                                {
                                    commandFiles.Add(f);
                                    break;
                                }
                            }
                            if (index == 0)
                                commandFiles.Add(f);
                        }
                    }
                }
                //for (int showIndex = 0; showIndex < commandFiles.Count && showIndex < 10; showIndex++)
                //{
                //    Console.WriteLine(commandFiles[showIndex].Name + "-->" + commandFiles[showIndex].Url);
                //}
                return commandFiles;
            }

            return null;
        }