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

import com.ssj.bean.weixin.zyb.domain.ZuoybDay;
import com.ssj.dao.weixin.zuoyb.dao.ZuoybDayDao;
import com.ssj.framework.basic.utils.DateHelper;
import com.ssj.framework.core.common.service.BaseServiceImpl;
import com.ssj.service.weixin.zuoyb.service.ZuoybDayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


/** 
 * @author  ZhangCaibao 
 * @version 2018年5月6日 上午11:08:00 
 * 类说明 
 */
@Service
@Transactional
public class ZuoybDayServiceImpl  extends BaseServiceImpl<ZuoybDay, String> implements  ZuoybDayService{

	@Autowired
	private  ZuoybDayDao  zuoybDayDao;
	@Override
	public PagingAndSortingRepository<ZuoybDay, String> getDao() {
		return zuoybDayDao;
	}
	@Override
	public void saveToday(String vipId, int type) {
		ZuoybDay zuoybDay=	zuoybDayDao.getVipIdAndType(vipId, type);
		if(zuoybDay==null){
			zuoybDay =new  ZuoybDay();
			Date date =new Date();
			zuoybDay.setThatDay(date);
			zuoybDay.setCreateTime(date);
			zuoybDay.setVipId(vipId);
			zuoybDay.setType(type+"");
			zuoybDay.setNum(1);
			this.save(zuoybDay);
		}

	}
	@Override
	public void saveToday(String vipId, int type,String formdate) {
		ZuoybDay zuoybDay=	zuoybDayDao.getVipIdAndType(vipId, type,formdate);
		if(zuoybDay==null){
			zuoybDay =new  ZuoybDay();
			Date date=DateHelper.parseDate(formdate, "yyyy-MM-dd");
			zuoybDay.setThatDay(date);
			zuoybDay.setCreateTime(date);
			zuoybDay.setVipId(vipId);
			zuoybDay.setType(type+"");
			zuoybDay.setNum(1);
			this.save(zuoybDay);
		}else {
			//如果当天有相同的记录那么num数量累加
			if (zuoybDay.getNum() == null){
				zuoybDay.setNum(2);
			} else {
				zuoybDay.setNum(zuoybDay.getNum() + 1);
			}
			this.save(zuoybDay);
		}
	}
	@Override
	public List<ZuoybDay> getVipId(String vipId) {
		return zuoybDayDao.getVipId(vipId);
	}
	@Override
	public List<ZuoybDay> getVipIdAndStartDateAndEndDate(String vipId,
			String startDate, String endDate) {
		return zuoybDayDao.getVipIdAndStartDateAndEndDate(vipId, startDate, endDate);
	}
	@Override
	public void updatedVipIdDay(String vipId, String oldDate, String newDate,int type) {
		//如果原时间zyb_day中num=1则删除该条记录，否则num减1
		ZuoybDay zuoybDay = zuoybDayDao.getVipIdAndDateAndType(vipId, type, oldDate);
		if(zuoybDay!=null){
			if(zuoybDay.getNum() != null && zuoybDay.getNum() > 1) {
				zuoybDay.setNum(zuoybDay.getNum()-1);
				zuoybDay.setCreateTime(new Date());
				this.save(zuoybDay);
			}else {
				this.delete(zuoybDay);
				
			}
		}
		
		//如果新时间zyb_day中有记录num加1,否则新增一条记录
		zuoybDay = zuoybDayDao.getVipIdAndDateAndType(vipId, type, newDate);
		if(zuoybDay != null) {
			zuoybDay.setNum(zuoybDay.getNum()+1);
			zuoybDay.setCreateTime(new Date());
			this.save(zuoybDay);
		}else {
			this.saveToday(vipId, 3, newDate);
		}
	}
	@Override
	public void deleteByVipIdAndType(String vipId,String lessonServiceId,int type) {
		//先将所有一对一类型的任务日历num累计-1
		zuoybDayDao.updateByVipIdAndType(vipId, lessonServiceId, type);
		//#删除num=0的所有一对一类型任务日历记录
		zuoybDayDao.deleteByVipIdAndType(vipId,lessonServiceId,type);
	}

}
