package com.cirle.scientific.service.impl;

import com.cirle.scientific.constants.DictConsts;
import com.cirle.scientific.dao.*;
import com.cirle.scientific.dto.HomeDataDTO;
import com.cirle.scientific.dto.QkjIntegralDetailDto;
import com.cirle.scientific.dto.QkjPositionsDTO;
import com.cirle.scientific.dto.QkjRecommendDTO;
import com.cirle.scientific.po.*;
import com.cirle.scientific.pojo.Result;
import com.cirle.scientific.service.*;
import com.cirle.scientific.utils.DateFormater;
import com.cirle.scientific.utils.DateUtil;
import com.cirle.scientific.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@Component
@Transactional
@Slf4j
public class BrandProductServiceImpl implements BrandProductService {

    @Autowired
    private QkjBrandProductDao brandProductDao;

    @Autowired
    private QkjBrandProductDetailDao brandProductDetailDao;

	@Autowired
	private RecommendService recommendService;

    @Autowired
    private LiveService liveService;

	@Autowired
	QkjTqcourseDao qkjTqcourseDao;

	@Autowired
	QkjArticleDao articleDao;

	@Autowired
	QkjConferenceDao conferenceDao;

	@Autowired
	QkjCourseWareDao courseWareDao;

	@Autowired
	QkjLiveDao liveDao;


	@Autowired
	DictDetailDao dictDetailDao;

	@Override
	public Result getDetailData(Long productId) {

		QkjBrandProduct brandProduct = brandProductDao.findById(productId).get();

		Map<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("id", brandProduct.getId());
		dataMap.put("coverImg", brandProduct.getCoverImg());
		dataMap.put("detail", brandProduct.getDetail());
		dataMap.put("isUpload", brandProduct.getIsUpload());
		List<DictDetail> dictDetails = dictDetailDao.listByNameAndLabel(DictConsts.BRAND_PRODUCT_TITLE_KEY, brandProduct.getId());
		String wareTitle = "学员实践报告上传";
		if(CollectionUtils.isNotEmpty(dictDetails)){
			DictDetail dictDetail = dictDetails.get(0);
			wareTitle =  Objects.isNull(dictDetail) ? "学员实践报告上传":dictDetail.getValue();
		}
		dataMap.put("wareTitle", wareTitle);

		//顶部推荐位
		dataMap.put("ads", recommendService.findByPostTypeAndObjId(5, productId.intValue(), 1, 100));

		//产品
		List<QkjBrandProductDetail> productDetailList = brandProductDetailDao.findByProductId(productId);
		List<Map<String, Object>> productList = productDetailList.stream().map(item ->{
			Map<String, Object> product = new HashMap<String, Object>();
			if(item.getObjType() == 2){
				//精品课
				QkjTqcourse tqcourse = qkjTqcourseDao.findById(item.getObjId().longValue()).get();
				product.put("objId", item.getObjId());
				product.put("objType", item.getObjType());
				product.put("objTitle", tqcourse.getTitle());
				product.put("coverImg", "http://files.qkyxzx.com/"+tqcourse.getCoverImg());
				product.put("ZqFlag", tqcourse.getZq());
				product.put("classHours", tqcourse.getTotalClassHours());
				product.put("price", tqcourse.getPrice());
				product.put("pointFlag", tqcourse.getPointFlag());
				product.put("point", tqcourse.getPoint());
				product.put("purchasedMembers", tqcourse.getPurchasedMembers());
				product.put("isSell", tqcourse.getIsSell());
				if(tqcourse.getZq() == 2){
					product.put("ZqFlag", 1);
					List<QkjTqcourse> tqcourses = qkjTqcourseDao.findTqListForZq(tqcourse.getId().intValue(),0,100);
					int purchasedMembers = 0;
					for(QkjTqcourse tq : tqcourses){
						purchasedMembers =purchasedMembers + tq.getPurchasedMembers()+tq.getStudyNum();
					}
					product.put("time", "共"+tqcourses.size()+"套 · "+ StringUtil.getTenThousandOfANumber(purchasedMembers)+"人次学习");
				}else{
					product.put("ZqFlag", 0);
					product.put("time", "共"+tqcourse.getTotalClassHours()+"讲 · "+StringUtil.getTenThousandOfANumber(tqcourse.getPurchasedMembers()+tqcourse.getStudyNum())+"人次学习");
				}
			}else if(item.getObjType() == 1){
				//直播
				QkjLive live = liveService.getLiveInfo(item.getObjId().longValue());
				product.put("objId", item.getObjId());
				product.put("objType", item.getObjType());
				product.put("objTitle", live.getLiveTitle());
				product.put("coverImg", "http://files.qkyxzx.com/"+live.getLiveCoverImg());
				product.put("classHours", live.getLiveSonCount());
				product.put("price", live.getPrice());
				product.put("pointFlag", live.getPointFlag());
				product.put("point", live.getPoint());
				product.put("purchasedMembers", live.getPurchasedMembers());
				product.put("startTime", DateFormater.formatDate(live.getLiveBeginTime(), "yyyy-MM-dd HH:mm"));
				product.put("remindMembers()", live.getRemindMembers());
				product.put("liveStatus", live.getLiveStatus());
				if(live.getLiveType() == 2){
					product.put("ZqFlag", 1);
					int joinMembers = 0;
					List<QkjLive> lives = liveDao.findSonList(live.getId().intValue(), 0, 100);
					for(QkjLive l : lives){
						joinMembers += l.getPurchasedMembers() + l.getJoinMembers();
					}
					product.put("time", "共"+lives.size()+"套 "+StringUtil.getTenThousandOfANumber(joinMembers)+"人次学习");
				}else{
					product.put("ZqFlag", 0);
					if(live.getLiveStatus() == 0){
						product.put("time", StringUtil.getTenThousandOfANumber(live.getRemindMembers())+"人预约");
					}else{
						product.put("time", StringUtil.getTenThousandOfANumber(live.getPurchasedMembers())+"人次学习");
					}
				}
			}else if(item.getObjType() == 3){
				//资讯文章
				QkjArticle live = articleDao.findById(item.getObjId().longValue()).get();
				product.put("objId", item.getObjId());
				product.put("objType", item.getObjType());
				product.put("objTitle", live.getTitle());
				product.put("coverImg", live.getCoverImg());
				product.put("purchasedMembers", live.getClick());
				product.put("author", live.getAuthor());
				product.put("isSell", 1);
				product.put("time", "作者："+live.getAuthor()+" ·"+StringUtil.getTenThousandOfANumber(live.getClick())+"次阅读");
			}else if(item.getObjType() == 4){
				//会议
				QkjConference live = conferenceDao.findById(item.getObjId().longValue()).get();
				product.put("objId", item.getObjId());
				product.put("objType", item.getObjType());
				product.put("objTitle", live.getTitle());
				product.put("coverImg", "http://files.qkyxzx.com/"+live.getCoverImg());
				product.put("classHours", live.getConferSonCount());
				product.put("price", live.getPrice());
				product.put("pointFlag", live.getPointFlag());
				product.put("point", live.getPoint());
				product.put("purchasedMembers", live.getPeopleCount());
				product.put("startTime", DateFormater.formatDate(live.getBigenTime(), "yyyy年MM月dd日"));
				product.put("liveStatus", live.getStatus());
				product.put("province", live.getProvince());
				product.put("city", live.getCity());
				product.put("isSell", live.getIsSell());
				if(live.getType() == 1){
					product.put("ZqFlag", 1);
					int peopleCount = 0;
					List<QkjConference> conferences = conferenceDao.findSonList(live.getId());
					for (QkjConference info : conferences){
						peopleCount+=info.getPeopleCount();
					}
					product.put("time1", live.getProvince()+"·"+live.getCity()+"   "+ (live.getBigenTime()==null?"":DateFormater.formatDate(live.getBigenTime(),"yyyy年MM月dd日")));
					product.put("time", "共"+live.getConferSonCount()+"场 · "+StringUtil.getTenThousandOfANumber(peopleCount)+"人次学习");
				}else{
					product.put("time1", live.getProvince()+"·"+live.getCity()+"   "+ (live.getBigenTime()==null?"":DateFormater.formatDate(live.getBigenTime(),"yyyy年MM月dd日")));
					product.put("time", StringUtil.getTenThousandOfANumber(live.getPeopleCount())+"人次学习");
				}
			}else if(item.getObjType() == 5){
				//课件
				QkjCourseWare live = courseWareDao.findById(item.getObjId().longValue()).get();
				product.put("objId", item.getObjId());
				product.put("objType", item.getObjType());
				product.put("objTitle", live.getTitle());
				if(null != live.getCoverImg()){
					product.put("coverImg", live.getCoverImg());
				}else{
					if(live.getFileType() == 3){
						product.put("coverImg", "http://files.qkyxzx.com/html5/pdf.png");
					}else if(live.getFileType() == 2){
						product.put("coverImg", "http://files.qkyxzx.com/html5/word.png");
					}if(live.getFileType() == 1){
						product.put("coverImg", "http://files.qkyxzx.com/html5/ppt.png");
					}if(live.getFileType() == 4){
						product.put("coverImg", "http://files.qkyxzx.com/html5/excel.png");
					}
				}
				product.put("price", live.getPrice());
				product.put("pointFlag", live.getPointFlag());
				product.put("point", live.getPoint());
				product.put("purchasedMembers", live.getDownloadMembers());
				product.put("isSell", 0);
				product.put("time", StringUtil.getTenThousandOfANumber(live.getDownloadMembers()) + "次下载");
			}
			return product;
		}).collect(Collectors.toList());
		dataMap.put("productList", productList);

		return Result.success(dataMap);
	}

}
