package com.ym.mec.biz.service.impl;

import com.ym.mec.biz.dal.dao.VipGroupDefaultClassesCycleDao;
import com.ym.mec.biz.dal.entity.VipGroupDefaultClassesCycle;
import com.ym.mec.biz.service.VipGroupDefaultClassesCycleService;
import com.ym.mec.common.dal.BaseDAO;
import com.ym.mec.common.exception.BizException;
import com.ym.mec.common.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class VipGroupDefaultClassesCycleServiceImpl extends BaseServiceImpl<Integer, VipGroupDefaultClassesCycle>  implements VipGroupDefaultClassesCycleService {
	
	@Autowired
	private VipGroupDefaultClassesCycleDao vipGroupDefaultClassesCycleDao;

	@Override
	public BaseDAO<Integer, VipGroupDefaultClassesCycle> getDAO() {
		return vipGroupDefaultClassesCycleDao;
	}

	@Override
	public long insert(VipGroupDefaultClassesCycle bean) {

		if(vipGroupDefaultClassesCycleDao.countAll()<=0){
			bean.setMinClassTimes(0);
			return super.insert(bean);
		}else{
			VipGroupDefaultClassesCycle checkMaxClassTimes = vipGroupDefaultClassesCycleDao.checkMaxClassTimes(bean.getMaxClassTimes());
			if(null!=checkMaxClassTimes){
				throw new BizException("已存在包含该课次的设置!");
			}
			List<VipGroupDefaultClassesCycle> allRecord = vipGroupDefaultClassesCycleDao.findAllCycle();
			allRecord.add(bean);
			allRecord=createMinClassTimess(allRecord);
			vipGroupDefaultClassesCycleDao.deleteAll();
			return vipGroupDefaultClassesCycleDao.batchInstert(allRecord);
		}
	}

	@Override
	public List<VipGroupDefaultClassesCycle> findAllCycle() {
		return vipGroupDefaultClassesCycleDao.findAllCycle();
	}

	@Override
	public int update(VipGroupDefaultClassesCycle bean) {
		vipGroupDefaultClassesCycleDao.delete(bean.getId());
		bean.setId(null);
		return (int) insert(bean);
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public int delete(Integer id) {
		super.delete(id);
		List<VipGroupDefaultClassesCycle> allRecord = vipGroupDefaultClassesCycleDao.findAllCycle();
		allRecord=createMinClassTimess(allRecord);
		if (allRecord == null || allRecord.size() == 0) {
			return 1;
		}
		vipGroupDefaultClassesCycleDao.deleteAll();
		return vipGroupDefaultClassesCycleDao.batchInstert(allRecord);
	}

	private List<VipGroupDefaultClassesCycle> createMinClassTimess(List<VipGroupDefaultClassesCycle> vipGroupDefaultClassesCycles){
		vipGroupDefaultClassesCycles = vipGroupDefaultClassesCycles.stream().sorted(Comparator.comparing(VipGroupDefaultClassesCycle::getMaxClassTimes)).collect(Collectors.toList());
		for(int i=0;i<vipGroupDefaultClassesCycles.size();i++){
			if(i==0){
				vipGroupDefaultClassesCycles.get(i).setMinClassTimes(0);
			}else{
				vipGroupDefaultClassesCycles.get(i).setMinClassTimes(vipGroupDefaultClassesCycles.get(i-1).getMaxClassTimes()+1);
			}
			vipGroupDefaultClassesCycles.get(i).setId(null);
		}
		return vipGroupDefaultClassesCycles;
	}
}
