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

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

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.dubbo.config.annotation.Service;
import com.ssj.bean.weixin.libmy.domain.TbLibJoin;
import com.ssj.bean.weixin.sales.domain.WorkDay;
import com.ssj.dao.weixin.sales.dao.WorkDayDao;
import com.ssj.framework.core.common.service.BaseServiceImpl;
import com.ssj.service.weixin.sales.service.WorkDayService;


@Service(version="1.0")
@Transactional
public class WorkDayServiceImpl extends BaseServiceImpl<WorkDay, String>  implements WorkDayService{

	private   Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private WorkDayDao  workDayDao;
	
	@Override
	public WorkDay getEndWorkDay(Date curDate, Integer day,String libId) {
		libId=getWorkDayLibId(libId);
		return workDayDao.getEndWorkDay(curDate, day,libId);
	}

	@Override
	public List<WorkDay> getMonthWorkDay(Date curDate,String libId) {
		libId=getWorkDayLibId(libId);
		return workDayDao.getMonthWorkDay(curDate,libId);
	}

	@Override
	public PagingAndSortingRepository<WorkDay, String> getDao() {
		return workDayDao;
	}

	@Override
	public WorkDay getNextWorkDay(Date curDate,String libId) {
		libId=getWorkDayLibId(libId);
		return workDayDao.getNextWorkDay(curDate,libId);
	}
	
	private String getWorkDayLibId(String libId){
		if(StringUtils.isNotEmpty(libId)){
			Integer libIdCount=workDayDao.countWorkDayByLibId(libId);
			if(libIdCount<=0){
				libId="SSJ";
			}
		}else{
			libId="SSJ";
		}
		return libId;
	}

	@Override
	public void insertWorkDayByLibId(TbLibJoin lib) {
		if(lib!=null && StringUtils.isNotEmpty(lib.getLeagueCode())){
			Integer libIdCount=workDayDao.countWorkDayByLibId(lib.getId());
			if(libIdCount<=0){
				workDayDao.insertWorkDayByLibId(lib.getId());
			}
		}
	}

	@Override
	public List<WorkDay> findWorkDayByLibId(String libId) {
		return workDayDao.findWorkDayByLibId(libId);
	}

	@Override
	public List<WorkDay> findWorkDayByDate(String libId, String datestart,String dateend) {
		return workDayDao.findWorkDayByDate(libId, datestart, dateend);
	}

}