package com.qxueyou.scc.courseware.service.impl; 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.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service; import com.qxueyou.scc.admin.classes.model.ClsClass; import com.qxueyou.scc.base.model.Result; import com.qxueyou.scc.base.service.impl.CommonAppService; 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.UUIDUtils; import com.qxueyou.scc.base.util.WordProcessUtils; import com.qxueyou.scc.exercise.model.ExerciseGroup; import com.qxueyou.scc.media.model.MediaVideo; import com.qxueyou.scc.media.service.IMediaVideoService; import com.qxueyou.scc.org.dao.OrgDAO; import com.qxueyou.scc.org.model.OrgCategoryLevel; import com.qxueyou.scc.org.model.OrgCollegeCourse; import com.qxueyou.scc.org.model.OrgCourseCategory; import com.qxueyou.scc.org.model.Organization; import com.qxueyou.scc.courseware.service.ICourceCategoryService; import com.qxueyou.scc.school.model.SchHandout; import com.qxueyou.scc.school.service.IHandoutService; import com.qxueyou.scc.sys.model.SysCondition; @Service(value = "courceCategoryAppService") public class CourceCategoryAppService extends CommonAppService implements ICourceCategoryService { private OrgDAO orgDAO; /** * IHandoutService服务 */ @Autowired IHandoutService handoutService; @Autowired private IMediaVideoService mediaVideoService; public OrgDAO getOrgDAO() { return orgDAO; } /** * 依赖注入 * * @param commonDAO */ @Autowired(required = false) public void setOrgDAO(@Qualifier("orgDAO") OrgDAO orgDAO) { this.orgDAO = orgDAO; } @Override public List queryOrgCourseList(String hql, List args) { return orgDAO.queryOrgCourseList(hql, args); } @Override public List queryOrgCourseCategoryList(String hql, List args) { return orgDAO.queryOrgCourseCategoryList(hql, args); } @Override public List queryOrgCourseListNew(String hql, List args) { return orgDAO.queryOrgCourseListNew(hql, args); } /** * 组装 全部课程 所有查询条件 * * @return * @version 2.0 */ public Map generateAllCourseCondition(){ Map result = new HashMap(3); // 1.课程分类 String firstHql = "select c from OrgCourseCategory c where c.deleteFlag is false and c.categoryLevel = ? order by c.code asc "; List lstFirstCategory = this.find(firstHql, CollectionUtils.newList(OrgCourseCategory.CATEGORY_LEVEL_FIRST) , OrgCourseCategory.class); String secondHql = "select c from OrgCourseCategory c where c.deleteFlag is false and c.categoryLevel = ? and type <> ? order by c.code asc "; List lstSecondCategory = this.find(secondHql, CollectionUtils.newList(OrgCourseCategory.CATEGORY_LEVEL_SECOND,Organization.ORG_TYPE_DEGREE_EDUCATION) , OrgCourseCategory.class); // 1.1 第二层 Map> secondMap = new HashMap>(lstFirstCategory.size()); for(OrgCourseCategory second : lstSecondCategory ){ if(StringUtils.isNotBlank(second.getParentCategoryId()) && null == secondMap.get(second.getParentCategoryId())){ secondMap.put(second.getParentCategoryId(), CollectionUtils.newList(OrgCourseCategory.class, second)); }else if(StringUtils.isNotBlank(second.getParentCategoryId())){ secondMap.get(second.getParentCategoryId()).add(second); } } // 组装的数据格式 Map sMap; List> lstS ; Map fMap; List> lstF = new ArrayList>(); int fIndex = 0; int sIndex = 0; // 循环 for(OrgCourseCategory first : lstFirstCategory){ fIndex = fIndex + 1 ; sIndex = 0; fMap = new HashMap(); lstS = new ArrayList>(); fMap.put("index", fIndex ); fMap.put("categoryId", first.getCategoryId()); fMap.put("categoryName", first.getCategoryName()); fMap.put("code", first.getCode()); fMap.put("parentId", null); // 第二层 if(null != secondMap.get(first.getCategoryId()) ){ for(OrgCourseCategory second : secondMap.get(first.getCategoryId())){ sIndex = sIndex + 1 ; sMap = new HashMap(); sMap.put("index", sIndex ); sMap.put("categoryId", second.getCategoryId()); sMap.put("categoryName", second.getCategoryName()); sMap.put("code", second.getCode()); sMap.put("parentId", second.getParentCategoryId()); lstS.add(sMap); } } fMap.put("children", lstS); lstF.add(fMap); } result.put("course", lstF); // 2.排序 List> lstSort = new ArrayList>(5); Map sort; String sortHql = "from SysCondition s where s.deleteFlag is false and s.module = ? order by s.order asc "; List lstSorts = this.find(sortHql, CollectionUtils.newList(SysCondition.MODULE_COURSE_MARKET_SORT), SysCondition.class); for(SysCondition s : lstSorts ){ sort = new HashMap(3); sort.put("index", s.getOrder()); sort.put("code", s.getCode()); sort.put("name", s.getName()); lstSort.add(sort); } result.put("sort", lstSort); // 3.地区 List> lstCity = new ArrayList>(6); Map city; String cityHql = "from SysCondition s where s.deleteFlag is false and s.module = ? order by s.order asc "; List lstCitys = this.find(cityHql, CollectionUtils.newList(SysCondition.MODULE_COURSE_MARKET_CITY), SysCondition.class); for(SysCondition s : lstCitys ){ city = new HashMap(3); city.put("index", s.getOrder()); city.put("code", s.getCode()); city.put("name", s.getName()); lstCity.add(city); } result.put("city", lstCity); return result ; } public Result insertLevelCategory(String id,String name,String code,String type){ Organization org = read(Organization.class, ClientUtils.getOrgId()); Result result = new Result(true); //保存 if("save".equals(type)){ OrgCategoryLevel ocl = read(OrgCategoryLevel.class, id); ocl.setCategoryName(name); ocl.setCode(code); save(ocl); result.setData(ocl); return result; } //增加同级 if("add".equals(type)){ OrgCategoryLevel ocl0 = read(OrgCategoryLevel.class, id); OrgCategoryLevel ocl = new OrgCategoryLevel(); ocl.setCategoryName(name); ocl.setCode(code); ocl.setDeleteFlag(false); ocl.setFullName(name); ocl.setImgPath(null); ocl.setLevel(ocl0.getLevel()); ocl.setParentId(ocl0.getParentId()); OrgCategoryLevel parent = read(OrgCategoryLevel.class, ocl0.getParentId()); ocl.setOrgId(ClientUtils.getOrgId()); ocl.setTopOrgId(org.getTopOrganizationId()); TraceUtils.setCreateTrace(ocl); result.setData(insertOcl(ocl,parent,name)); return result; } //增加下级 if("addNext".equals(type)){ OrgCategoryLevel parent = read(OrgCategoryLevel.class, id); OrgCategoryLevel ocl = new OrgCategoryLevel(); ocl.setCategoryName(name); ocl.setCode(code); ocl.setDeleteFlag(false); ocl.setFullName(name); ocl.setImgPath(null); ocl.setParentId(id); ocl.setOrgId(ClientUtils.getOrgId()); ocl.setTopOrgId(org.getTopOrganizationId()); TraceUtils.setCreateTrace(ocl); result.setData(insertOcl(ocl,parent,name)); return result; } return new Result(false,"参数错误"); } private OrgCategoryLevel insertOcl(OrgCategoryLevel ocl,OrgCategoryLevel parent,String name){ int loop = 0; String levelCode = ""; String origLevelCode = ""; OrgCategoryLevel origCateLevel ; //BeanUtils.copyProperties(ocl, origCateLevel); if(parent==null){ // 首层:直接生成 levelCode = UUIDUtils.generateUUID(); origLevelCode = levelCode; }else{ // 下层:根据parent生成 levelCode = parent.getLevelCode()+"00"; } do { try { origCateLevel = new OrgCategoryLevel(); BeanUtils.copyProperties(ocl , origCateLevel); if(parent==null){ // 首层:直接生成 origCateLevel.setLevel(BigDecimal.ONE.shortValue()); origCateLevel.setLevelCode(levelCode+"_00"); }else{ // 下层:根据parent生成 origCateLevel.setLevel(new BigDecimal(parent.getLevel()).add(BigDecimal.ONE).shortValue()); origCateLevel.setLevelCode(levelCode); } save(origCateLevel); flush(); return origCateLevel ; // break; } catch (Exception e) { clearCache(); loop++; if(parent==null){ // 首层:直接生成 levelCode = origLevelCode.concat(String.valueOf(loop)); }else{ // 下层:根据parent生成 String h = String.valueOf(loop); if(loop<10){ h = "0"+loop; } levelCode = parent.getLevelCode().concat(h); } } } while (loop < 1000); return null ; } @SuppressWarnings("unused") private String generateLevelCode(String name) { // 转拼音 //使用微软自带中文输入法用鼠标翻页,选择第一页之后的数据时,会生成(li'zhi.dr)这种账号 String[] arrUnidecode = WordProcessUtils.toPinyin(name," ").replaceAll("'", "").split(" "); String orgCode = ""; if(null != arrUnidecode){ for(String str : arrUnidecode){ orgCode = orgCode.concat(str.substring(0,1)); } } return orgCode; } public Result doDeleteCate(String id){ boolean flag = false; String hql = " from OrgCategoryLevel where deleteFlag is false and (categoryLevelId = ? or parentId = ?)"; List lstCate = find(hql, CollectionUtils.newList(id,id), OrgCategoryLevel.class); for (OrgCategoryLevel orgCategoryLevel : lstCate) { hql = " from OrgCollegeCourse where deleteFlag is false and courseCategoryId = ?"; List course = find(hql, CollectionUtils.newList(orgCategoryLevel.getCategoryLevelId()), OrgCollegeCourse.class); for (OrgCollegeCourse orgCollegeCourse : course) { hql = " from ClsClass where collegeCourseId = ? and deleteFlag is false"; List classes = find(hql, CollectionUtils.newList(orgCollegeCourse.getCollegeCourseId()), ClsClass.class); if(!classes.isEmpty()){ flag = true; break; } } if(flag){ break; } } if(flag){ return new Result(false,"该分类下的科目下存在班级,请先删除班级"); } for (OrgCategoryLevel orgCategoryLevel : lstCate) { hql = " from OrgCollegeCourse where deleteFlag is false and courseCategoryId = ?"; List course = find(hql, CollectionUtils.newList(orgCategoryLevel.getCategoryLevelId()), OrgCollegeCourse.class); for (OrgCollegeCourse orgCollegeCourse : course) { doDeleteCourse(orgCollegeCourse.getCollegeCourseId()); } orgCategoryLevel.setDeleteFlag(true); TraceUtils.setUpdateTrace(orgCategoryLevel); save(orgCategoryLevel); } return new Result(true); } public Result doDeleteCourse(String id){ String hql = " from ClsClass where deleteFlag is false and collegeCourseId = ?"; ClsClass cls = findUnique(hql, CollectionUtils.newList(id), ClsClass.class); if(cls!=null){ return new Result(false,"该科目下存在班级,请先删除班级"); } //删除所有视频 doDeleteVideo(id); //删除所有练习 doDeleteExe(id); //删除所有讲义 doDeleteHandout(id); hql = " from OrgCollegeCourse where deleteFlag is false and collegeCourseId = ?"; OrgCollegeCourse course = findUnique(hql, CollectionUtils.newList(id), OrgCollegeCourse.class); course.setDeleteFlag(true); TraceUtils.setUpdateTrace(course); return save(course); } private void doDeleteVideo(String id){ String hql = " from MediaVideo where (videoId in " + " (select videoId from MediaVideoReCourse where collegeCourseId = ? and deleteFlag is false)" + " or originVideoId in (select videoId from MediaVideoReCourse where collegeCourseId = ? and deleteFlag is false))" + " and deleteFlag is false"; List lstVideo = find(hql, CollectionUtils.newList(id,id), MediaVideo.class); for (MediaVideo mv : lstVideo) { mv.setDeleteFlag(true); TraceUtils.setUpdateTrace(mv); this.mediaVideoService.saveVideo(mv); } } private void doDeleteExe(String id){ String hql = " from ExerciseGroup where (groupId in (select groupId from ExerciseReCourse where collegeCourseId = ? and deleteFlag is false)" + " or originExerciseId in (select groupId from ExerciseReCourse where collegeCourseId = ? and deleteFlag is false))" + " and deleteFlag is false"; List groups = find(hql,CollectionUtils.newList(id,id),ExerciseGroup.class); for (ExerciseGroup g : groups){ g.setDeleteFlag(true); TraceUtils.setUpdateTrace(g); save(g); } } private void doDeleteHandout(String id){ String hql = " from SchHandout where (handoutId in " + "(select handoutId from SchHandoutReCourse where collegeCourseId = ? and deleteFlag is false" + ") or originHandoutId in (select handoutId from SchHandoutReCourse where collegeCourseId = ? and deleteFlag is false ))" + "and deleteFlag is false"; List handouts = find(hql,CollectionUtils.newList(id,id),SchHandout.class); for(SchHandout handout : handouts){ handout.setDeleteFlag(true); TraceUtils.setUpdateTrace(handout); this.handoutService.saveHandout(handout); } } /** * 新增科目 * @param id * @param name * @param code * @param type * @return */ @Override public Result addCategory(Organization org, String id, String name, String code, String type) { Result result = new Result(true); //保存 if("save".equals(type)){ OrgCategoryLevel ocl = this.read(OrgCategoryLevel.class, id); ocl.setCategoryName(name); ocl.setCode(code); this.save(ocl); result.setData(ocl); return result; } //增加同级 if("add".equals(type)){ OrgCategoryLevel ocl0 = this.read(OrgCategoryLevel.class, id); OrgCategoryLevel ocl = new OrgCategoryLevel(); ocl.setCategoryName(name); ocl.setCode(code); ocl.setDeleteFlag(false); ocl.setFullName(name); ocl.setImgPath(null); ocl.setLevel(ocl0.getLevel()); ocl.setParentId(ocl0.getParentId()); OrgCategoryLevel parent = this.read(OrgCategoryLevel.class, ocl0.getParentId()); TraceUtils.setCreateTrace(ocl); result.setData(insertOcl(ocl,parent,name));//插入ocl return result; } //增加下级 if("addNext".equals(type)){ OrgCategoryLevel parent = this.read(OrgCategoryLevel.class, id); OrgCategoryLevel ocl = new OrgCategoryLevel(); ocl.setCategoryName(name); ocl.setCode(code); ocl.setDeleteFlag(false); ocl.setFullName(name); ocl.setImgPath(null); ocl.setParentId(id); TraceUtils.setCreateTrace(ocl); result.setData(insertOcl(ocl,parent,name)); return result; } return new Result(false); } }