package com.ssj.service.weixin.zuoyb.service.impl;

import com.ssj.bean.sys.sort.domain.Sort;
import com.ssj.bean.weixin.zyb.domain.ZuoybCourse;
import com.ssj.bean.weixin.zyb.domain.ZuoybWeek;
import com.ssj.dao.weixin.zuoyb.dao.ZuoybCourseDao;
import com.ssj.dao.weixin.zuoyb.dao.ZuoybQueryDao;
import com.ssj.framework.basic.utils.DateHelper;
import com.ssj.framework.core.common.service.BaseServiceImpl;
import com.ssj.service.sys.sort.service.SortService;
import com.ssj.service.weixin.zuoyb.service.ZuoybCourseService;
import com.ssj.service.weixin.zuoyb.service.ZuoybScoringContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


/** 
 * @author  ZhangCaibao 
 * @version 2017年7月4日 上午11:28:15 
 * 类说明 
 */
@Service
@Transactional
public class ZuoybCourseServiceImpl  extends BaseServiceImpl<ZuoybCourse, String> implements ZuoybCourseService{
    @Autowired
	private  ZuoybCourseDao  zuoybCourseDao;
    @Autowired
    private  ZuoybQueryDao  zuoybQueryDao;
    @Autowired
	 private SortService  sortService;
    
	 @Autowired
	 private  ZuoybScoringContentService  zuoybScoringContentService;
	@Override
	public PagingAndSortingRepository<ZuoybCourse, String> getDao() {
		return zuoybCourseDao;
	}
	@Override
	public Page<Map<String, Object>> getZuoybCourseist(
			Map<String, Object> searchParams, Pageable pageable) {
		return zuoybQueryDao.getZuoybCourseist(searchParams, pageable);
	}
	@Override
	public void insertZuoybWeekList(String zybCourseId, String libId) {
		List<ZuoybWeek> list  =new ArrayList<ZuoybWeek>();
		for(int i=1;i<8;i++){
			ZuoybWeek  zuoybWeek =new  ZuoybWeek();
			zuoybWeek.setCreateTime(new Date());
			zuoybWeek.setLibId(libId);
			zuoybWeek.setDays(i);
			zuoybWeek.setZybCourseId(zybCourseId);
			list.add(zuoybWeek);
		}
		zuoybQueryDao.insertZuoybWeekList(list);
		
	}
	@Override
	public Page<Map<String, Object>> getZybList(
			Map<String, Object> searchParams, Pageable pageable) {
		return zuoybQueryDao.getZybList(searchParams, pageable);
	}
	@Override
	public void updateCourseWeek(String zybCourseId,Integer days,String teacherId,String teacherName) {
		zuoybCourseDao.updateCourseWeek(zybCourseId, days, teacherId, teacherName);		
	}
	@Override
	public Integer getCourseNameAndHourAndLibId(String courseName,
			String courseHour, String libId) {
		return zuoybCourseDao.getCourseNameAndHourAndLibId(courseName, courseHour, libId);
	}
	@Override
	public void zuoybCourseDelete(String id) {
		zuoybCourseDao.deleteZybScoringContentV2(id);
		zuoybCourseDao.deleteZybScoringContentV1(id);
		zuoybCourseDao.deleteZybWorkByZybCourseId(id);
		this.delete(id);
	}
	@Override
	public List<Map<String, Object>> getTeacherIdList(String teacherId,Date date) {
		return zuoybQueryDao.getTeacherIdList(teacherId,DateHelper.getWeekDay(date));
	}
	@Override
	public Page<Map<String, Object>> findTeacherIdForPage(
			Map<String, Object> searchParams, Pageable pageable) {
		return zuoybQueryDao.findTeacherIdForPage(searchParams, pageable);
	}
	@Override
	public  List<Map<String,Object>> getLibIdAndVipIdList(String libId,String vipId) {
		return zuoybQueryDao.getLibIdAndVipIdList(libId, vipId);
	}
	@Override
	public List<ZuoybCourse> findByLibId(String libId) {
		return zuoybCourseDao.findByLibId(libId);
	}
	@Override
	 public List<ZuoybCourse> findByLibIdAndVipId(String libId ,String vipId){
		return zuoybCourseDao.findByLibIdAndVipId(libId, vipId);
	}
	@Override
	public void updateWorkImgByObjectId(String oldObjectId, String newObjectId) {
		zuoybCourseDao.updateWorkImgByObjectId(oldObjectId, newObjectId);
	}
	@Override
	public Integer getZybCourseIdAndLibIdAndNum(Integer num,
			String zybCourseId, String libId) {
		return zuoybCourseDao.getZybCourseIdAndLibIdAndNum(num, zybCourseId, libId);
	}
	@Override
	public Page<Map<String, Object>> zuoybSRList(
			Map<String, Object> searchParams, Pageable pageable) {
		return zuoybQueryDao.zuoybSRList(searchParams, pageable);
	}
	@Override
	public Page<Map<String, Object>> selectZybSRList(
			Map<String, Object> searchParams, Pageable pageable) {
		return zuoybQueryDao.selectZybSRList(searchParams, pageable);
	}
	@Override
	public List<ZuoybCourse> findByLibIdAndVipIdAndCourseHour(String libId,
			String vipId, String courseHour) {
		return zuoybCourseDao.findByLibIdAndVipIdAndCourseHour(libId, vipId, courseHour);
	}
	
	@Override
	public List<ZuoybCourse> findBySiblings(String courseId) {
		return zuoybCourseDao.findBySiblings(courseId);
	}
	@Override
	public Integer fingByCourseHourAndLibIdCount(String courseHour, String libId) {
		return zuoybCourseDao.fingByCourseHourAndLibIdCount(courseHour, libId);
	}
	@Override
	public void updateCourseNum(String id, Integer num) {
		zuoybCourseDao.updateCourseNum(id, num);
	}
	@Override
	public ZuoybCourse findByLibIdAndCourseHour(String libId, String courseHour) {
		return zuoybCourseDao.findByLibIdAndCourseHour(libId, courseHour);
	}
	@Override
	public ZuoybCourse getCourseNameAndHour(String courseName,
			String courseHour, String libId) {
		return zuoybCourseDao.getCourseNameAndHour(courseName, courseHour, libId);
	}
	@Override
	public List<ZuoybCourse> getLibIdAndCourseHour(String libId,
			String courseHour) {
		return zuoybCourseDao.getLibIdAndCourseHour(libId, courseHour);
	}
	@Override
	public List<String> getCourseAndLibId(String libId) {
		return zuoybCourseDao.getCourseAndLibId(libId);
	}
	@Override
	public void zuoybCourseListSave(String libId) {
		int countlib=zuoybCourseDao.fingByLibIdCount(libId);
		if(countlib==0){
			List<Sort> zyb_course=	sortService.getParamValByCode("zyb_course");
			List<Sort> zyb_hour=sortService.getParamValByCode("zyb_hour");
			for (Sort sort : zyb_hour) {
				for (Sort sortname : zyb_course) {
					Integer count=this.getCourseNameAndHourAndLibId(sortname.getName(), sort.getName(), libId);
					if(count>0){
						continue;
					}
					ZuoybCourse   zuoybCourse  =new  ZuoybCourse();
					zuoybCourse.setCourseName(sortname.getName());
					zuoybCourse.setCourseHour(sort.getName());
					zuoybCourse.setLibId(libId);
					zuoybCourse.setDetails(sort.getAliases());
					zuoybCourse.setCreateTime(new Date());
					zuoybCourse.setIcon(sortname.getAliases());
					zuoybCourse.setNum(this.fingByCourseHourAndLibIdCount(sort.getName(), libId)+1);
					this.save(zuoybCourse);
					//zuoybScoringContentService.insertLibIdScoringContent(libId, zuoybCourse.getId(),sortname.getCode());
					this.insertZuoybWeekList(zuoybCourse.getId(), libId);
				}
			}
		}
		
			
	}

}
