using CYQ.Data.Cache; using Newtonsoft.Json; using Song.ServiceInterfaces; using System; using System.Collections.Generic; using System.Configuration; using System.Dynamic; using System.Linq; using System.Runtime.Remoting.Contexts; using System.Text; using System.Threading.Tasks; using Taurus.Core; using Taurus.Mvc; using Taurus.Mvc.Attr; using WeiSha.Common; namespace PeiXun.Controllers.BasicInfo { public class CoursesAPIController : Controller { [Token] [HttpGet] public void CourseList() { int index = Query("index"); index = index == 0 ? 1 : index; int size = Query("size"); size = size == 0 ? 1 : size; int sbjid = Query("sbjid"); sbjid = sbjid == 0 ? -1 : sbjid; //专业id string search = Query("search"); string order = Query("order"); //排序,flux流量最大优先,def推荐、流量,tax排序号,new最新,rec推荐 Song.Entities.Organization org = Business.Do().OrganCurrent(); int sum = 0; List cour = Business.Do().CoursePager(org.Org_ID, sbjid, -1, true, search, order, size, index, out sum); dynamic obj = new ExpandoObject(); obj.sum = sum; obj.index = index; obj.size = size; obj.sbjid = sbjid; for (int i = 0; i < cour.Count; i++) { Song.Entities.Course c = cour[i]; c.Cou_LogoSmall = Upload.Get["Course"].Virtual + c.Cou_LogoSmall; c.Cou_Logo = Upload.Get["Course"].Virtual + c.Cou_Logo; //是否免费,或是限时免费 if (c.Cou_IsLimitFree) { DateTime freeEnd = c.Cou_FreeEnd.AddDays(1).Date; if (!(c.Cou_FreeStart <= DateTime.Now && freeEnd >= DateTime.Now)) c.Cou_IsLimitFree = false; } c.Cou_Intro = c.Cou_Target = c.Cou_Content = ""; c.Cou_Name = c.Cou_Name.Replace("\"", """); } obj.data = cour; Write(JsonConvert.SerializeObject(obj)); } [Token] [HttpGet] public void SelfCourse() { string action = Query("action"); if (string.IsNullOrEmpty(action)) action = "buycou"; string token = Query("token").TrimStart("xyl:".ToCharArray()); DistributedCache cache = DistributedCache.Instance; Song.Entities.Accounts emp = cache.Get(token); //学员登录id int stid = emp.Ac_ID; dynamic obj = new ExpandoObject(); obj.code = 1; obj.msg = "success"; switch (action) { case "buycou": List buyCou = Business.Do().CourseForStudent(stid, null, 1, false, -1); HandleCourseInfo(buyCou, action, stid); obj.data = buyCou; break; case "overcou": List overCou = Business.Do().CourseForStudent(stid, null, 2, false, -1); HandleCourseInfo(overCou, action, stid); obj.data = overCou; break; case "trycou": List trycou = Business.Do().CourseForStudent(stid, null, -1, true, -1); HandleCourseInfo(trycou, action, stid); obj.data = trycou; break; } Write(JsonConvert.SerializeObject(obj)); } [Token] [HttpGet] public void CourseInfo() { int couid = Query("couid"); string token = Query("token").TrimStart("xyl:".ToCharArray()); DistributedCache cache = DistributedCache.Instance; Song.Entities.Accounts emp = cache.Get(token); int accid = emp.Ac_ID; Song.Entities.Course course = Business.Do().CourseSingle(couid); if (course == null) return; //是否免费,或是限时免费 if (course.Cou_IsLimitFree) { DateTime freeEnd = course.Cou_FreeEnd.AddDays(1).Date; if (!(course.Cou_FreeStart <= DateTime.Now && freeEnd >= DateTime.Now)) course.Cou_IsLimitFree = false; } //课程资源路径 course.coupath = Upload.Get["Course"].Virtual; //学习该课程的总人数,包括已经过期的 int studyCount = Business.Do().CourseStudentSum(course.Cou_ID, null); course.studyCount = studyCount; //是否学习当前课程 //是否购买当前课程 bool isBuy = false; //是否购买 isBuy = Business.Do().StudyIsCourse(accid, course.Cou_ID); //没有购买,但处于限时免费中 if (!isBuy && course.Cou_IsLimitFree) { } course.isBuy = isBuy; //是否可以学习,如果是免费或已经选修便可以学习,否则当前课程允许试用且当前章节是免费的,也可以学习 bool canStudy = isBuy || course.Cou_IsFree || course.Cou_IsLimitFree || course.Cou_IsTry; course.canStudy = canStudy; //树形章节输出 Song.Entities.Outline[] outlines = Business.Do().OutlineAll(course.Cou_ID, true); if (outlines.Length > 0) course.olTree = Business.Do().OutlineTree(outlines); //课程公告 List guides = Business.Do().GuideCount(0, course.Cou_ID, 0, 0).ToList(); course.guides = guides; dynamic obj = new ExpandoObject(); obj.code = 1; obj.msg = "success"; obj.data = course; Write(JsonConvert.SerializeObject(obj)); } /// /// 根据课程ID获取课程信息 /// /// /// //[Cache(Expires = 60)] //public Song.Entities.Course ForID(int id) //{ // Song.Entities.Course cur = Business.Do().CourseSingle(id); // return _tran(cur); //} /// /// 生成json /// protected void HandleCourseInfo(List courses, string action, int stid) { //string tm = "{\"sum\":" + courses.Count + ",\"action\":\"" + action + "\",\"object\":["; for (int i = 0; i < courses.Count; i++) { Song.Entities.Course c = courses[i]; c.Cou_LogoSmall = Upload.Get["Course"].Virtual + c.Cou_LogoSmall; c.Cou_Logo = Upload.Get["Course"].Virtual + c.Cou_Logo; //是否免费,或是限时免费 if (c.Cou_IsLimitFree) { DateTime freeEnd = c.Cou_FreeEnd.AddDays(1).Date; if (!(c.Cou_FreeStart <= DateTime.Now && freeEnd >= DateTime.Now)) c.Cou_IsLimitFree = false; } c.Cou_Intro = c.Cou_Target = c.Cou_Content = ""; c.Cou_Name = c.Cou_Name.Replace("\"", """); c.olcount = Business.Do().OutlineOfCount(c.Cou_ID, -1, true); //当前课程里的章节数 c.quscount = Business.Do().QuesOfCount(c.Org_ID, c.Sbj_ID, c.Cou_ID, -1, 0, true);//当前课程的试题 //增加输出项 //Dictionary addParas = new Dictionary(); //addParas.Add("olcount", Business.Do().OutlineOfCount(c.Cou_ID, -1, true)); //当前课程里的章节数 //addParas.Add("quscount", Business.Do().QuesOfCount(c.Org_ID, c.Sbj_ID, c.Cou_ID, -1, 0, true)); //当前课程的试题 //获取课程的购买信息 Song.Entities.Student_Course sc = Business.Do().StudentCourse(stid, c.Cou_ID); if (sc != null) { //addParas.Add("starttime", sc.Stc_StartTime); //addParas.Add("endtime", sc.Stc_EndTime); c.starttime = sc.Stc_StartTime.ToString(); c.endtime = sc.Stc_EndTime.ToString(); } //tm += "" + c.ToJson(null, null, addParas); //if (i < courses.Count - 1) tm += ","; } //return tm; } } }