package com.qxueyou.scc.courseware.action; import java.math.BigDecimal; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import com.qxueyou.scc.admin.classes.model.ClsClass; import com.qxueyou.scc.admin.classes.service.IClassService; import com.qxueyou.scc.base.dao.CommonDAO; import com.qxueyou.scc.base.model.Constants; import com.qxueyou.scc.base.model.Pager; import com.qxueyou.scc.base.model.Result; import com.qxueyou.scc.base.service.ICommonService; import com.qxueyou.scc.base.util.ClientUtils; import com.qxueyou.scc.base.util.CollectionUtils; import com.qxueyou.scc.base.util.TraceUtils; import com.qxueyou.scc.base.util.WordProcessUtils; import com.qxueyou.scc.courseware.service.ICourceCategoryService; import com.qxueyou.scc.exercise.model.ExerciseReCourse; import com.qxueyou.scc.media.model.MediaVideoReCourse; import com.qxueyou.scc.org.model.OrgCategoryLevel; import com.qxueyou.scc.org.model.OrgCollegeCourse; import com.qxueyou.scc.org.model.OrgCourse; import com.qxueyou.scc.org.model.OrgCourseCategory; import com.qxueyou.scc.org.model.Organization; import com.qxueyou.scc.school.model.SchHandoutReCourse; /** * 科目类别管理controller * * @author 德虎 * @history 2014-11-25 新建 夏德虎 * */ @Controller @RequestMapping(value = "/org/courcecategory") public class CourceCategoryController { @Autowired private CommonDAO commonDAO; @Autowired @Qualifier("commonAppService") ICommonService commonService; @Autowired private ICourceCategoryService categoryService; @SuppressWarnings("unused") @Autowired private IClassService orgClassService; /*@Autowired ISysBusinessCacheService sysBusinessCacheService ;*/ /** * APP2.0: 课程超市 选课 获取所有分类
* URL /org/courcecategory/courseCatNew
* * @method GET * @return 返回值JSON串:
* *
	 * [{"index":1,"code":"1CKJR","categoryName":"财会金融","categoryId":"18","imgPath":"/web/res/img/app/coursemarket_ico_accounting.png"},
	 * ......
	 * {"index":8,"code":"9QB","categoryName":"全部","categoryId":"26","imgPath":"/web/res/img/app/coursemarket_ico_classify.png"}]
	 * 
* * 字段值说明: * *
	 * index:索引
	 * code:编码
	 * categoryName:名字
	 * categoryId:id
	 * imgPath:logo URL
	 * 
*/ @RequestMapping(value = "courseCatNew", method = RequestMethod.GET) public @ResponseBody List> getCourseCatData() { Pager page = new Pager(); page.setPageNum(1); page.setPageSize(Integer.MAX_VALUE); // 1. 查询该机构下所有的科目 String hql = "select c from OrgCourseCategory c where c.deleteFlag is false and c.categoryLevel = ? order by c.code asc "; List lstLastCategory = commonDAO.findList(hql, page, CollectionUtils.newList(OrgCourseCategory.CATEGORY_LEVEL_FIRST), OrgCourseCategory.class); if (lstLastCategory.isEmpty()) { return new ArrayList>(); } List> lstMap = new ArrayList>(lstLastCategory.size()); Map map; Map QBMap = new HashMap(5); int index = 0; for (OrgCourseCategory courseCat : lstLastCategory) { if ("0QB".equals(courseCat.getCode())) { QBMap.put("index", 8); QBMap.put("categoryId", courseCat.getCategoryId()); QBMap.put("categoryName", courseCat.getCategoryName()); QBMap.put("code", courseCat.getCode()); QBMap.put("imgPath", courseCat.getImgPath()); if (StringUtils.isNotBlank(courseCat.getImgPath()) && !"null".equals(courseCat.getImgPath())) { QBMap.put("imgPathShade", courseCat.getImgPath().split("\\.")[0].concat("-shade.png")); } else { QBMap.put("imgPathShade", courseCat.getImgPath()); } } else if (!"8QT".equals(courseCat.getCode())) { index = index + 1; map = new HashMap(5); map.put("index", index); map.put("categoryId", courseCat.getCategoryId()); map.put("categoryName", courseCat.getCategoryName()); map.put("code", courseCat.getCode()); map.put("imgPath", courseCat.getImgPath()); if (StringUtils.isNotBlank(courseCat.getImgPath()) && !"null".equals(courseCat.getImgPath())) { map.put("imgPathShade", courseCat.getImgPath().split("\\.")[0].concat("-shade.png")); } else { map.put("imgPathShade", courseCat.getImgPath()); } lstMap.add(map); } } lstMap.add(QBMap); return lstMap; } /** * APP2.0: 课程超市 所有查询条件
* URL /org/courcecategory/courseCatContitionNew
* * 返回值JSON串:
* *
	 * {
	 * "course":[
	 * 		{"code":"0QB","children":[],"index":1,"categoryName":"全部","categoryId":"26","parentId":null},
	 * 		{"code":"1CKJR","children":[],"index":2,"categoryName":"财会金融","categoryId":"18","parentId":null},
	 * 		{"code":"2YYKS","children":[],"index":3,"categoryName":"语言考试","categoryId":"19","parentId":null},
	 * 		{"code":"3YYL","children":[],"index":4,"categoryName":"医药类","categoryId":"20","parentId":null},
	 * 		{"code":"4JGL","children":[],"index":5,"categoryName":"建工类","categoryId":"21","parentId":null},
	 * 		{"code":"5ZYKZ","children":null,"index":6,"categoryName":"职业考证","categoryId":"22","parentId":null},
	 * 		{"code":"6XLJY","children":[
	 * 				{"code":null,"index":1,"categoryName":"经济学","categoryId":"5","parentId":"23"},
	 * 			{"code":null,"index":2,"categoryName":"法学","categoryId":"6","parentId":"23"}
	 * 			],"index":7,"categoryName":"学历教育","categoryId":"23","parentId":null},
	 * 		{"code":"7ITJN","children":[],"index":8,"categoryName":"IT技能","categoryId":"24","parentId":null},
	 * 		{"code":"8QT","children":[],"index":9,"categoryName":"其他","categoryId":"25","parentId":null}
	 * 	],
	 * "city":[{"name":"深圳","index":1,"code":"sz"},{"name":"广州","index":2,"code":"gz"}],
	 * "sort":[{"name":"价格由低到高","index":1,"code":"PRICE_ASC"},{"name":"价格由高到低","index":2,"code":"PRICE_DESC"}]
	 * }
	 * 
	 * 
	 * 
	 * @return
	 */
	@RequestMapping(value = "courseCatContitionNew", method = RequestMethod.GET)
	public @ResponseBody Map getCourseCatContitionData() {
		return categoryService.generateAllCourseCondition();
	}

	/**
	 * APP2.0: 该机构下所有课程的分类和科目 URL: /org/courcecategory/courseCategoryNew
	 * 
	 * @return
	 */
	@Deprecated
	@RequestMapping(value = "courseCategoryNew", method = RequestMethod.GET)
	public @ResponseBody List getCourseCategoryData() {

		// 1. 查询该机构下所有的科目
		String hql = "select courseId,name,price,imgPath,courseCategoryId " + " from OrgCourse c where c.deleteFlag is false";

		// 2. 查询该机构下所有的科目对应的类别
		String hql_type = "select distinct c.category.categoryId,c.category.categoryName from OrgCourse c where " + " c.deleteFlag is false order by c.createTime ";

		List args = new ArrayList(1);
		// 如果是游客班 查所有机构 1:表示游客班
//		if (!orgClassService.isCurrentVistorClass()) {
//			hql = hql.concat(" and c.org.organizationId=?");
//			hql_type = "select distinct c.category.categoryId,c.category.categoryName from OrgCourse c where " + " c.org.organizationId=? and c.deleteFlag is false order by c.createTime ";
//			args = CollectionUtils.newList(ClientUtils.getOrgId());
//		}

		// 1. 查询该机构下所有的科目
		List lstCourse = categoryService.queryOrgCourseListNew(hql, args);

		// 2. 查询该机构下所有的科目对应的类别
		List lstCategory = categoryService.queryOrgCourseCategoryList(hql_type, args);

		// 组装结构
		List newLstCourse = null;
		for (OrgCourseCategory category : lstCategory) {
			newLstCourse = new ArrayList();
			for (OrgCollegeCourse course : lstCourse) {
				if (category.getCategoryId().equals(course.getCourseCategoryId())) {
					newLstCourse.add(course);
				}
			}
			category.setCourseList(newLstCourse);
		}
		// 不限制值
		String not_limit = Constants.NOT_LIMIT_VALUE;
		// 重新组装类型 添加不限
		List lstLastCategory = new ArrayList();
		// 添加第一个不限
		OrgCourseCategory category = new OrgCourseCategory();
		category.setCategoryId(not_limit);
		category.setCategoryName("不限");

		List lstLastCourse = new ArrayList();
		OrgCollegeCourse course = new OrgCollegeCourse();
		course.setCourseId(not_limit);
		course.setName("不限");

		lstLastCategory.add(category);
		lstLastCategory.addAll(lstCategory);

		for (OrgCourseCategory cate : lstLastCategory) {
			lstLastCourse = new ArrayList();
			lstLastCourse.add(course);
			if (not_limit.equals(cate.getCategoryId())) {// 不限
				lstLastCourse.addAll(lstCourse);
			} else {
				lstLastCourse.addAll(cate.getCourseList());
			}

			cate.setCourseList(lstLastCourse);
		}

		return lstLastCategory;
	}

	/******************************************************************* 上面为App接口,下面为后台接口 **************************/

	/**
	 * 科目类别列表
	 * 
	 * @return
	 */
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public @ResponseBody List list() {
		Organization org = commonDAO.read(Organization.class, ClientUtils.getOrgId());

		return commonDAO.find("from OrgCourseCategory where deleteFlag is false and categoryLevel = ? and type = ?", CollectionUtils.newList(OrgCourseCategory.CATEGORY_LEVEL_SECOND, org.getType()), OrgCourseCategory.class);
	}

	/**
	 * 根据科目ID查询科目
	 * 
	 * @return
	 */
	@RequestMapping(value = "/{courceTypeId}", method = RequestMethod.GET)
	public String get(@PathVariable String courceTypeId) {
		// TODO
		return null;
	}

	/**
	 * 新增
	 * 
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST)
	public String add() {
		// TODO
		return null;
	}

	/**
	 * 删除
	 * 
	 * @return
	 */
	@RequestMapping(value = "/{courceTypeId}", method = RequestMethod.DELETE)
	public String delete(@PathVariable String courceTypeId) {
		// TODO
		return null;
	}

	/**
	 * 更新
	 * 
	 * @return
	 */
	@RequestMapping(value = "/{courceTypeId}", method = RequestMethod.POST)
	public String update(@PathVariable String courceTypeId) {
		// TODO
		return null;
	}

	/**
	 * 新增类别层级
	 * 
	 * @return
	 */
	
	
	
	@RequestMapping(value = "/addCategory", method = RequestMethod.POST)
	public @ResponseBody Result addCategory(String id, String name, String code, String type) {
		
		Result result = categoryService.addCategory(null, id, name, code, type);
		
		if(result.isSuccess()){
			return result;
		}
		
		return new Result(false,"参数错误");
		
	}

	/**
	 * 类别层级数据
	 * 
	 * @return
	 */
	@RequestMapping(value = "/allData", method = RequestMethod.POST)
	public @ResponseBody List allData() {
		String hql = " from OrgCategoryLevel where deleteFlag is false and topOrgId = ? order by orderNum desc, createTime asc";
		Organization org = commonDAO.read(Organization.class, ClientUtils.getOrgId());
		List lstCate = commonDAO.find(hql, CollectionUtils.newList(org.getTopOrganizationId()), OrgCategoryLevel.class);
		List levelIds = new ArrayList();
		for (OrgCategoryLevel orgCategoryLevel : lstCate) {
			levelIds.add(orgCategoryLevel.getCategoryLevelId());
		}
		if(levelIds.isEmpty()){
			return lstCate;
		}
		Map args = new HashMap();
		args.put("levelIds", levelIds.toArray());
		hql = "from OrgCollegeCourse where deleteFlag is false and courseCategoryId in (:levelIds) order by code asc";
		List courses = commonDAO.findByComplexHql(hql, args, OrgCollegeCourse.class);
		Map> map = new HashMap>();
		for (OrgCollegeCourse orgCollegeCourse : courses) {
			if(null == orgCollegeCourse){
				continue;
			}
			//去除掉不content内容
			orgCollegeCourse.setContent("");
	
			if(null == map.get(orgCollegeCourse.getCourseCategoryId())){
				List lstCourse = new ArrayList();
				lstCourse.add(orgCollegeCourse);
				map.put(orgCollegeCourse.getCourseCategoryId(), lstCourse);
			}else{
				map.get(orgCollegeCourse.getCourseCategoryId()).add(orgCollegeCourse);
			}
		}
		for (OrgCategoryLevel orgCategoryLevel : lstCate) {
			
			orgCategoryLevel.setCourses(map.get(orgCategoryLevel.getCategoryLevelId()));
		}
		return lstCate;
	}
	
	/**
	 * 视频类别层级数据
	 * 
	 * @return
	 */
	@RequestMapping(value = "/allCountData", method = RequestMethod.POST)
	public @ResponseBody List allCountData() {
		return setCount(1);
	}
	
	/**
	 * 讲义类别层级数据
	 * 
	 * @return
	 */
	@RequestMapping(value = "/allHandoutCountData", method = RequestMethod.POST)
	public @ResponseBody List allHandoutCountData() {
		return setCount(2);
	}
	
	/**
	 * 练习类别层级数据
	 * 
	 * @return
	 */
	@RequestMapping(value = "/allExerciseCountData", method = RequestMethod.POST)
	public @ResponseBody List allExersiseCountData() {
		return setCount(3);
	}
	
	/**
	 * 讲义类别层级数据
	 * 
	 * @return
	 */
	@RequestMapping(value = "/allArticleCountData", method = RequestMethod.POST)
	public @ResponseBody List allArticleCountData() {
		return setCount(4);
	}
	
	/**
	 * 查询视频讲义练习在各个科目下的数量
	 * @param type 1:视频 2:讲义 3:练习
	 * @return
	 */
	@SuppressWarnings("unchecked")
	private List setCount(int type){
		String hql = " from OrgCategoryLevel where deleteFlag is false and topOrgId = ? order by level asc, code asc";
		Organization org = commonDAO.read(Organization.class, ClientUtils.getOrgId());
		List lstCate = commonDAO.find(hql, CollectionUtils.newList(org.getTopOrganizationId()), OrgCategoryLevel.class);
		String sql = "";
		switch (type) {
		case 1:
			sql = "select r.college_course_id,count(1) from media_video_re_course r, media_video v where r.delete_flag is false and r.org_id = ? and v.video_id = r.video_id and v.delete_flag is false group by college_course_id " ;
			break;
		case 2:
			sql = "select r.college_course_id,count(1) from sch_handout_re_course r, sch_handout v  where r.delete_flag is false and r.org_id = ? and v.handout_id = r.handout_id and v.delete_flag is false  group by college_course_id" ;
			break;
		case 3:
			sql = "select r.college_course_id,count(1) from exercise_re_course r where r.delete_flag is false and r.org_id = ? group by college_course_id" ;
			break;
		case 4:
			sql = "select r.college_course_id,count(1) from sch_article_re_course r, sch_article v  where r.delete_flag is false and r.org_id = ? and v.ARTICLE_ID = r.ARTICLE_ID and v.delete_flag is false  group by college_course_id" ;
			break;
		default:
			break;
		}
		List lst = this.commonDAO.findBySql(sql, CollectionUtils.newList(ClientUtils.getOrgId()));
		
		Map map = new HashMap(5);
    	for (Object[] obj : lst) {
    		map.put(String.valueOf(obj[0]), Integer.parseInt(String.valueOf(obj[1])));
    	}
		
		for (OrgCategoryLevel orgCategoryLevel : lstCate) {
			hql = "from OrgCollegeCourse where deleteFlag is false and courseCategoryId = ? order by code asc";
			List courses = commonDAO.find(hql, CollectionUtils.newList(orgCategoryLevel.getCategoryLevelId()), OrgCollegeCourse.class);
			
			for(OrgCollegeCourse course : courses){
				if(type==1){
					course.setVideoCount(null != map.get(course.getCollegeCourseId()) ? map.get(course.getCollegeCourseId()) : 0);
				}else if(type==2){
					course.setHandoutCount(null != map.get(course.getCollegeCourseId()) ? map.get(course.getCollegeCourseId()) : 0);
				}else if(type==3){
					course.setGroupCount(null != map.get(course.getCollegeCourseId()) ? map.get(course.getCollegeCourseId()) : 0);
				}else if(type==4){
					course.setArticleCount(null != map.get(course.getCollegeCourseId()) ? map.get(course.getCollegeCourseId()) : 0);
				}
				course.setContent(null);
			}
			orgCategoryLevel.setCourses(courses);
		}
		
		return lstCate;
	}

	/**
	 * ztree层级数据
	 * 
	 * @return
	 *//*
	@RequestMapping(value = "/allHierarchyData", method = RequestMethod.POST)
	public @ResponseBody List allHierarchyData() {

		return sysBusinessCacheService.getOrgCourseCategory();
		
	}
	
	*//**
	 * ztree层级数据
	 * 
	 * @return
	 *//*
	@RequestMapping(value = "live/allHierarchyData", method = RequestMethod.POST)
	public @ResponseBody ResultJson allLiveHierarchyData() {

		return sysBusinessCacheService.getLiveOrgCourseCategory();
		
	}*/

	/**
	 * 查询
	 * 
	 * @return
	 */
	@RequestMapping(value = "/oneMsg", method = RequestMethod.POST)
	public @ResponseBody OrgCategoryLevel oneMsg(String id) {
		String hql = "from OrgCategoryLevel where deleteFlag is false and categoryLevelId = ?";

		return commonDAO.findUnique(hql, CollectionUtils.newList(id), OrgCategoryLevel.class);
	}
	
	/**
	 * 查询专业信息
	 * 
	 * @return
	 */
	@RequestMapping(value = "/oneCourse", method = RequestMethod.POST)
	public @ResponseBody Result oneCourse(String id) {
		String hql = "from OrgCollegeCourse where deleteFlag is false and collegeCourseId = ?";
		OrgCollegeCourse occ = commonDAO.findUnique(hql, CollectionUtils.newList(id), OrgCollegeCourse.class);
		
		Map data = new HashMap();
		data.put("name", occ.getName());
		data.put("content", occ.getContent());
		data.put("price", occ.getPrice());
		data.put("courseId", occ.getCourseId());
		data.put("collegeCourseId", occ.getCollegeCourseId());
		data.put("relation","");
		Result result = new Result(true,null,data);
		
		hql = " from OrgCourse where deleteFlag is false and courseId = ?";
		OrgCourse course = commonDAO.findUnique(hql, CollectionUtils.newList(occ.getCourseId()), OrgCourse.class);
		if(null == course){
			return result;
		}
		
		hql = " from OrgCourseCategory where deleteFlag is false and categoryId = ?";
		OrgCourseCategory cates = commonDAO.findUnique(hql, CollectionUtils.newList(course.getCourseCategoryId()), OrgCourseCategory.class);
		
		StringBuffer sb = new StringBuffer(course.getName()).append('/');
		sb.insert(0, cates.getCategoryName()+"/");
		int i = 0;
		while(cates.getCategoryLevel()==OrgCourseCategory.CATEGORY_LEVEL_FIRST){
			i++;
			hql = " from OrgCourseCategory where deleteFlag is false and categoryId = ?";
			cates = commonDAO.findUnique(hql, CollectionUtils.newList(cates.getParentCategoryId()),OrgCourseCategory.class);
			if(cates==null){
				break;
			}
			sb.insert(0,cates.getCategoryName()+"/");
			if(i==10){
				break;
			}
		}
		if(sb.length()!=0){
			data.put("relation",sb.deleteCharAt(sb.length()-1).toString() );
		}
		
		return result;
	}

	/**
	 * 获取机构名字
	 * 
	 * @return
	 */
	@RequestMapping(value = "/getOrgName", method = RequestMethod.POST)
	public @ResponseBody Result getOrgName() {
		Organization org = commonDAO.read(Organization.class, ClientUtils.getOrgId());

		return new Result(true, org.getName());
	}

	/**
	 * 删除科目
	 * 
	 * @return
	 */
	@RequestMapping(value = "/deleteCate", method = RequestMethod.POST)
	public @ResponseBody Result deleteCate(String id) {

		return categoryService.doDeleteCate(id);
	}

	/**
	 * 删除专业
	 * 
	 * @return
	 */
	@RequestMapping(value = "/deleteCourse", method = RequestMethod.POST)
	public @ResponseBody Result deleteCourse(String id) {

		return categoryService.doDeleteCourse(id);
	}

	/**
	 * 3初始化科目
	 * 
	 * @return
	 */
	@RequestMapping(value = "/initCourse", method = RequestMethod.POST)
	public @ResponseBody Result initCourse() {
		String hql = " from OrgCollegeCourse where deleteFlag is false ";
		List lstCourse = commonDAO.find(hql, OrgCollegeCourse.class);
		int i = 0;
		
		/*for (OrgCollegeCourse orgCollegeCourse : lstCourse) {
			Organization org = commonDAO.read(Organization.class, orgCollegeCourse.getOrganizationId());
			hql = " from OrgCourseCategory where deleteFlag is false and categoryLevel !=? and type = ? and categoryType = ?";
			OrgCourseCategory c = commonDAO.findUnique(hql, CollectionUtils.newList(OrgCourseCategory.CATEGORY_LEVEL_FIRST,org.getType(),orgCollegeCourse.getType()), OrgCourseCategory.class);
			if(c!=null){
				orgCollegeCourse.setCourseCategoryId(c.getCategoryId());
				orgCollegeCourse.setCourseCategoryName(c.getCategoryName());
			}
		
		}*/
		Map map = new HashMap();
		Map courseMap = new HashMap();
		for (OrgCollegeCourse orgCollegeCourse : lstCourse) {
			Organization org = commonDAO.read(Organization.class, orgCollegeCourse.getOrganizationId());
			hql = " from OrgCourseCategory where categoryId = ? and deleteFlag is false";
			OrgCourseCategory cate = commonDAO.findUnique(hql, CollectionUtils.newList(orgCollegeCourse.getCourseCategoryId()), OrgCourseCategory.class);
			if(cate==null){
				continue;
			}
			//hql = " from OrgCategoryLevel where topOrgId = ? and categoryName = ? and deleteFlag is false";
			//OrgCategoryLevel level = commonDAO.findUnique(hql,CollectionUtils.newList(orgCollegeCourse.getTopOrgId(),cate.getCategoryName()),OrgCategoryLevel.class);
			OrgCategoryLevel level = (OrgCategoryLevel) map.get(org.getTopOrganizationId()+"-"+cate.getCategoryName());
			if(level==null){
				level = new OrgCategoryLevel();
				level.setCategoryName(cate.getCategoryName());
				level.setCode(cate.getCode());
				level.setDeleteFlag(false);
				level.setFullName(cate.getFullName());
				level.setImgPath(cate.getImgPath());
				level.setLevel(BigDecimal.ONE.shortValue());
				level.setParentId(null);
				level.setOrgId(orgCollegeCourse.getOrganizationId());
				level.setTopOrgId(org.getTopOrganizationId());
				TraceUtils.setCreateTrace(level);
				commonService.save(level);
				map.put(org.getTopOrganizationId()+"-"+cate.getCategoryName(), level);
			}
			
			//hql = " from OrgCollegeCourse where deleteFlag is false and topOrgId = ? and courseId = ?";
			//OrgCollegeCourse oc = commonDAO.findUnique(hql, CollectionUtils.newList(org.getTopOrganizationId(),orgCollegeCourse.getCourseId()), OrgCollegeCourse.class);
			OrgCollegeCourse oc = (OrgCollegeCourse) courseMap.get(org.getTopOrganizationId()+"-"+orgCollegeCourse.getCourseId());
			if(oc==null){
				orgCollegeCourse.setTopOrgId(org.getTopOrganizationId());
				orgCollegeCourse.setCourseCategoryId(level.getCategoryLevelId());
				orgCollegeCourse.setCourseCategoryName(level.getCategoryName());
				commonService.save(orgCollegeCourse);
				courseMap.put(org.getTopOrganizationId()+"-"+orgCollegeCourse.getCourseId(), orgCollegeCourse);
			}
			
			i++;
		}
		return new Result(true,"总共有"+lstCourse.size()+",初始化了"+i+"条");
	}
	
	/**
	 * 2先初始化课件数据,再初始化上面的科目
	 * 
	 * @return
	 */
	@RequestMapping(value = "/initCourseWare", method = RequestMethod.POST)
	public @ResponseBody Result initCourseWare() {
		
		//视频
		int i = 0;
		String hql = " from MediaVideoReCourse where deleteFlag is false";
		List videos = commonDAO.find(hql, MediaVideoReCourse.class);
		for (MediaVideoReCourse course : videos) {
			hql = " from OrgCollegeCourse where collegeCourseId = ? and deleteFlag is false";
			OrgCollegeCourse oc = commonDAO.findUnique(hql, CollectionUtils.newList(course.getCollegeCourseId()), OrgCollegeCourse.class);
			if(oc==null){
				continue;
			}
			course.setOrgId(oc.getOrganizationId());
			TraceUtils.setUpdateTrace(course);
			commonService.save(course);
			i++;
		}
		//讲义
		int y = 0;
		hql = " from SchHandoutReCourse where deleteFlag is false";
		List handouts = commonDAO.find(hql, SchHandoutReCourse.class);
		for (SchHandoutReCourse course : handouts) {
			hql = " from OrgCollegeCourse where collegeCourseId = ? and deleteFlag is false";
			OrgCollegeCourse oc = commonDAO.findUnique(hql, CollectionUtils.newList(course.getCollegeCourseId()), OrgCollegeCourse.class);
			if(oc==null){
				continue;
			}
			course.setOrgId(oc.getOrganizationId());
			TraceUtils.setUpdateTrace(course);
			commonService.save(course);
			y++;
		}
		//练习
		int z = 0;
		hql = " from ExerciseReCourse where deleteFlag is false";
		List exers = commonDAO.find(hql, ExerciseReCourse.class);
		for (ExerciseReCourse course : exers) {
			hql = " from OrgCollegeCourse where collegeCourseId = ? and deleteFlag is false";
			OrgCollegeCourse oc = commonDAO.findUnique(hql, CollectionUtils.newList(course.getCollegeCourseId()), OrgCollegeCourse.class);
			if(oc==null){
				continue;
			}
			course.setOrgId(oc.getOrganizationId());
			TraceUtils.setUpdateTrace(course);
			commonService.save(course);
			z++;
		}
		return new Result(true,"视频总共"+videos.size()+"个,成功了"+i+"个"+"讲义总共"+handouts.size()+"个,成功了"+y+"个"+"练习总共"+exers.size()+"个,成功了"+z+"个");
	}
	
	/**
	 * 1初始化班级 机构ID
	 * 
	 * @return
	 */
	@RequestMapping(value = "/initClass", method = RequestMethod.POST)
	public @ResponseBody Result initClass() {
		String hql = " from ClsClass where deleteFlag is false ";
		List ocs = commonDAO.find(hql, ClsClass.class);
		int i = 0;
		for (ClsClass orgClass : ocs) {
			hql = " from OrgCollegeCourse where collegeCourseId = ? and deleteFlag is false";
			OrgCollegeCourse course = commonDAO.findUnique(hql,CollectionUtils.newList(orgClass.getCollegeCourseId()), OrgCollegeCourse.class);
			if(course==null){
				continue;
			}
			hql = " from Organization where organizationId = ? and deleteFlag is false";
			Organization org = commonDAO.findUnique(hql, CollectionUtils.newList(course.getOrganizationId()), Organization.class);
			if(org==null){
				continue;
			}
			orgClass.setOrgId(org.getOrganizationId());
			TraceUtils.setUpdateTrace(orgClass);
			commonService.save(orgClass);
			i++;
		}
		return new Result(true,"班级总共"+ocs.size()+",成功了"+i+"个");
	}
	
	/**
	 * 初始化levelCode
	 * 
	 * @return
	 */
	@SuppressWarnings("unused")
	@RequestMapping(value = "/initLevel", method = RequestMethod.GET)
	public @ResponseBody Result initLevel() {
		String hql = " from OrgCategoryLevel where deleteFlag is false and level = ?";
//		List parentsFirst = commonDAO.find(hql, CollectionUtils.newList(OrgCategoryLevel.CATEGORY_LEVEL_FIRST), OrgCategoryLevel.class);
//		for (OrgCategoryLevel orgCategoryLevel : parentsFirst) {
//			if(StringUtils.isBlank(orgCategoryLevel.getCategoryName())||StringUtils.isNotBlank(orgCategoryLevel.getLevelCode())){
//				continue;
//			}
//			String levelCode = generateLevelCode(orgCategoryLevel.getCategoryName());
//			String code = cacheService.get("course1_"+levelCode, String.class);
//			if(code==null){
//				orgCategoryLevel.setLevelCode(levelCode+"_00");
//				commonService.save(orgCategoryLevel);
//				cacheService.set("course1_"+levelCode, 24*60*60, orgCategoryLevel.getCategoryLevelId());
//				continue;
//			}else{
//				int i = 0;
//				String lc = "";
//				do{
//					i++;
//					lc = levelCode+i;
//				}while(null!=cacheService.get("course1_"+levelCode+i, String.class));
//				orgCategoryLevel.setLevelCode(lc+"_00");
//				commonService.save(orgCategoryLevel);
//				cacheService.set("course1_"+lc, 24*60*60, orgCategoryLevel.getCategoryLevelId());
//				continue;
//			}
//			
//		}
//		
		initNextLevel(Short.parseShort("2"));
		initNextLevel(Short.parseShort("3"));
		initNextLevel(Short.parseShort("4"));
		initNextLevel(Short.parseShort("5"));
		initNextLevel(Short.parseShort("6"));
		initNextLevel(Short.parseShort("7"));
		initNextLevel(Short.parseShort("8"));
		initNextLevel(Short.parseShort("9"));
		initNextLevel(Short.parseShort("10"));
		
		return new Result(true);
	}
	
	@SuppressWarnings("unused")
	private void initNextLevel(short level){
		String hql = " from OrgCategoryLevel where deleteFlag is false and level = ?";
		List parentsSecond = commonDAO.find(hql, CollectionUtils.newList(level), OrgCategoryLevel.class);
		for (OrgCategoryLevel orgCategoryLevel : parentsSecond) {
			if(StringUtils.isNotBlank(orgCategoryLevel.getLevelCode())){
				continue;
			}
			
			OrgCategoryLevel parent = commonDAO.read(OrgCategoryLevel.class, orgCategoryLevel.getParentId());
			if(null==parent||StringUtils.isBlank(parent.getLevelCode())){
				continue;
			}
			String levelCode = parent.getLevelCode()+"00";
//			String code = cacheService.get("course1_"+levelCode, String.class);
			String code = null;
			if(code==null){
				orgCategoryLevel.setLevelCode(levelCode);
				commonService.save(orgCategoryLevel);
//				cacheService.set("course1_"+levelCode, 24*60*60, orgCategoryLevel.getCategoryLevelId());
				continue;
			}else{
				int i = 0;
				String lc = "";
				do{
					i++;
					String h = String.valueOf(i);
					if(i<10){
						h = "0"+i;
					}
					lc = parent.getLevelCode()+h;
//				}while(null!=cacheService.get("course1_"+lc, String.class));
				}while(true);
//				orgCategoryLevel.setLevelCode(lc);
//				commonService.save(orgCategoryLevel);
//				cacheService.set("course1_"+lc, 24*60*60, orgCategoryLevel.getCategoryLevelId());
//				continue;
			}
			
		}
	}
	
	
	@SuppressWarnings("unused")
	private String generateLevelCode(String name) {

		// 转拼音
		//使用微软自带中文输入法用鼠标翻页,选择第一页之后的数据时,会生成(li'zhi.dr)这种账号
		String[] arrUnidecode = WordProcessUtils.toPinyin(name.replace(" ",""), " ").replaceAll("'", "").split(" ");
		
		String orgCode = "";
		if(null != arrUnidecode){
			for(String str : arrUnidecode){
				orgCode = orgCode.concat(str.substring(0,1));
			}
		}
		
		
		return orgCode;
	}
	
	public static void main(String[] args) {
		String[] arrUnidecode = WordProcessUtils.toPinyin("22".replace(" ","")).replaceAll("'", "").split(" ");
		String orgCode = "";
		if(null != arrUnidecode){
			for(String str : arrUnidecode){
				orgCode = orgCode.concat(str.substring(0,1));
			}
		}
		System.out.println(orgCode);
	}
	
}