package com.ssj.service.course.service.impl;

import com.ssj.bean.course.domain.CourseSeries;
import com.ssj.bean.course.domain.CourseSeriesDetail;
import com.ssj.bean.course.domain.CourseSeriesDetailFeedback;
import com.ssj.bean.course.domain.CourseSeriesDetailFile;
import com.ssj.bean.weixin.libmy.domain.LibVip;
import com.ssj.bean.weixin.push.domain.Miniprogram;
import com.ssj.bean.weixin.push.domain.SendTemplateShort;
import com.ssj.bean.weixin.user.domain.User;
import com.ssj.dao.course.dao.CourseQueryDao;
import com.ssj.dao.course.dao.CourseSeriesDetailFeedbackDao;
import com.ssj.framework.core.common.service.BaseServiceImpl;
import com.ssj.framework.weixin.util.DateUtil;
import com.ssj.framework.weixin.util.WxUtil;
import com.ssj.service.course.req.TeacherFeedbackReq;
import com.ssj.service.course.service.CourseSeriesDetailFeedbackService;
import com.ssj.service.course.service.CourseSeriesJoinService;
import com.ssj.service.course.service.ICSDetailService;
import com.ssj.service.course.service.ICSService;
import com.ssj.service.weixin.library.service.ILibVipService;
import com.ssj.service.weixin.push.service.PushTemplateService;
import com.ssj.service.weixin.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;


/** 
 * @author  ZhangCaibao 
 * @version 2018年10月19日 上午10:01:20 
 * 类说明 
 */
@Transactional
@Service
public class CourseSeriesDetailFeedbackServiceImpl  extends BaseServiceImpl<CourseSeriesDetailFeedback, String> implements  CourseSeriesDetailFeedbackService{
	@Autowired
	private  CourseSeriesDetailFeedbackDao  dao;
	@Autowired
	private  CourseQueryDao  courseQueryDao;
	@Autowired
	private ILibVipService vipService;
	 @Autowired
	private PushTemplateService  pushTemplateService;
	@Autowired
	private UserService userService;
	
	@Autowired
	private ICSDetailService csDetailService;
	@Autowired
	private ICSService csService;
	
	@Autowired
	private CourseSeriesJoinService joinService;
	
	
	@Override
	public PagingAndSortingRepository<CourseSeriesDetailFeedback, String> getDao() {
		return dao;
	}
	@Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
	public List<Map<String, Object>> getCourseFeedback(String courseSeriesId) {
		return courseQueryDao.getCourseFeedback(courseSeriesId);
	}
	
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
	public List<Map<String, Object>> getCourseFeedback(String courseSeriesId,String libId) {
		return courseQueryDao.getCourseFeedback(courseSeriesId,libId);
	}
    
	@Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
	public Map<String, Object> getCourseScore(String courseSeriesId) {
		Map<String, Object> map = new HashMap<String, Object>();
		List<Object[]>   list = dao.getCourseScore(courseSeriesId);
		Object score =0;
		Object total =0;
		if(list != null && !list.isEmpty()){
			Object[] tempObj = list.get(0);
			score = (Object)tempObj[0];
			total = (Object)tempObj[1];
		}
		map.put("score", score);
		map.put("total", total);
		return map;
	}
	@Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
	public Map<String, Object> getCourseScore(String courseSeriesId,String libId) {
		Map<String, Object> map = new HashMap<String, Object>();
		List<Object[]>   list = dao.getCourseScore(courseSeriesId,libId);
		Object score =0;
		Object total =0;
		if(list != null && !list.isEmpty()){
			Object[] tempObj = list.get(0);
			score = (Object)tempObj[0];
			total = (Object)tempObj[1];
		}
		map.put("score", score);
		map.put("total", total);
		return map;
	}
	@Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
	public CourseSeriesDetailFeedback getFeedbackByDetailsId(
			String courseDetailsId) {
		return dao.getFeedbackByDetailsId(courseDetailsId);
	}
	@Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
	public CourseSeriesDetailFeedback findyDetailIdAndVipId(String courseDetailId, String vipId) {
		
		return dao.findyDetailIdAndVipId(courseDetailId,vipId);
	}
	@Override
	public void courseFeedbackSend(TeacherFeedbackReq req) {
		LibVip vip = vipService.getById(req.getVipId());
		CourseSeriesDetailFeedback back = this.findyDetailIdAndVipId(req.getCourseDetailId(),req.getVipId());
		if(req.getSendType()==2){
			
			if(back == null){
				new CourseSeriesDetailFeedback();
			}
			back.setActiveScore(req.getActiveScore());
			back.setUserId(vip.getUserId());
			back.setVipId(req.getVipId());
			back.setCourseDetailsId(req.getCourseDetailId());
			back.setCourseSeriesId(req.getCourseSeriesId());
			back.setFocusScore(req.getFocusScore());
			back.setActiveScore(req.getActiveScore());
			back.setIsSend(1);
			back.setSaying(req.getSaying());
			back.setLibId(req.getLibId());
			back.setCreateTime(new Date());
			this.save(back);
		}else{
			
			back.setIsSend(1);
			this.save(back);
			
		}
		//发送报告
		User user = userService.getById(vip.getUserId());
		CourseSeriesDetail courseDetail = csDetailService.getById(back.getCourseDetailsId());
		CourseSeries courseSeries = csService.getById(courseDetail.getSeriesId());
		String parentContentMessage = "亲爱的"+ vip.getChildName()+"家长您好！老师已完成本节课的课后反馈，请及时查收，以便跟进孩子学习情况!";
		Map<String, String> data = new HashMap<String, String>();
		data.put("first", parentContentMessage+"\n");
		data.put("keyword1", courseSeries.getCourseName()+courseDetail.getCourseUnit());
		data.put("keyword2", DateUtil.dateToString(new Date()));
		String template_id = "OPENTM408875752";
		Miniprogram miniprogram = new Miniprogram();
		miniprogram.setAppid(WxUtil.map.get("courseAppId"));
		miniprogram.setPagepath("pages/feedback_report/feedback_report?type=11&feedbackId="+back.getId());
		SendTemplateShort sendTemplateShort = new SendTemplateShort(user.getAccount(), template_id, null,
				data, miniprogram);
		pushTemplateService.saveAndSendPushWxTemplate(sendTemplateShort);
		
		//讲join表的状态更新为已反馈
		joinService.updateStateSend(back.getCourseDetailsId(),back.getVipId(),back.getLibId());
	}
	@Override
	public Map<String, Object> courseFeedbackPre(TeacherFeedbackReq req) {
		LibVip vip = vipService.getById(req.getVipId());
		//先查询是否有数据
		CourseSeriesDetailFeedback back = this.findyDetailIdAndVipId(req.getCourseDetailId(),req.getVipId());
		if(back == null){
			back = new CourseSeriesDetailFeedback();
		}
		back.setActiveScore(req.getActiveScore());
		back.setUserId(vip.getUserId());
		back.setVipId(req.getVipId());
		back.setCourseDetailsId(req.getCourseDetailId());
		back.setCourseSeriesId(req.getCourseSeriesId());
		back.setFocusScore(req.getFocusScore());
		back.setActiveScore(req.getActiveScore());
		back.setIsSend(0);
		back.setSaying(req.getSaying());
		back.setLibId(req.getLibId());
		back.setCreateTime(new Date());
		this.save(back);
		
		Map<String, Object> feedBackInfo =  new HashMap<String, Object>();
		feedBackInfo.put("lib_id", req.getLibId());
		feedBackInfo.put("vip_id", req.getVipId());
		feedBackInfo.put("child_name", vip.getChildName());
		feedBackInfo.put("img_url", vip.getImgUrl());
		feedBackInfo.put("active_score", req.getActiveScore());
		feedBackInfo.put("saying", req.getSaying());
		feedBackInfo.put("focus_score", req.getFocusScore());
		feedBackInfo.put("course_details_id", req.getCourseDetailId());
		feedBackInfo.put("is_send", 0);
		feedBackInfo.put("is_save", back.getId());
		
		feedBackInfo.put("feedback_id", back.getId());
		feedBackInfo.put("is_evaluate", "0");
		feedBackInfo.put("course_time", DateUtil.dateToString(new Date()));
		CourseSeriesDetail detail = csDetailService.getById(req.getCourseDetailId());
		feedBackInfo.put("course_content", detail.getCourseContent()==null?new ArrayList<String>():Arrays.asList(detail.getCourseContent().replaceAll("\r","").replaceAll("\n", "").split("；")));
		feedBackInfo.put("contentImgList", new ArrayList<String>());
		//feedBackInfo.put("redFlowerList", backFileService.getRedFlowList3(req.getLibId(),req.getVipId(),req.getCourseDetailId()));
		feedBackInfo.put("redFlowerList", new ArrayList<String>());
		 //CourseSeriesDetail detail = csDetailService.getById(req.getCourseDetailId());
		 //查找附件
		 List<CourseSeriesDetailFile> fileList = detail.getFileList();
		 List<Map<String, Object>> mp4List = new ArrayList<Map<String, Object>>(); 
		 for (CourseSeriesDetailFile courseSeriesDetailFile : fileList) {
			 if(courseSeriesDetailFile.getFileType().equalsIgnoreCase("mp4")){
				 Map<String, Object> map2 = new HashMap<String, Object>();
				 map2.put("coverUrl", courseSeriesDetailFile.getCoverUrl());
				 map2.put("fileUrl", courseSeriesDetailFile.getFileUrl());
				 mp4List.add(map2);
			 }
			
			 
		}
		feedBackInfo.put("mp4List", mp4List);
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("feedBackInfo", feedBackInfo);
		return data;
	}
	@Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
	public CourseSeriesDetailFeedback findyDetailIdAndLibFeedbackId(
			String libFeedbackId, String vipId) {
		return dao.findyDetailIdAndLibFeedbackId(libFeedbackId, vipId);
	}
}
