package com.jpsoft.shinestar.modules.base.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jpsoft.shinestar.modules.base.dao.HolidayInfoDAO;
import com.jpsoft.shinestar.modules.base.entity.HolidayInfo;
import com.jpsoft.shinestar.modules.base.service.HolidayInfoService;
import com.jpsoft.shinestar.modules.common.dto.Sort;
import org.joda.time.DateTime;
import org.joda.time.Days;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

@Transactional
@Component(value="holidayInfoService")
public class HolidayInfoServiceImpl implements HolidayInfoService {
	@Resource(name="holidayInfoDAO")
	private HolidayInfoDAO holidayInfoDAO;

	@Override
	public HolidayInfo get(String id) {
		// TODO Auto-generated method stub
		return holidayInfoDAO.get(id);
	}

	@Override
	public int insert(HolidayInfo model) {
		// TODO Auto-generated method stub
		//model.setId(UUID.randomUUID().toString());
		
		return holidayInfoDAO.insert(model);
	}

	@Override
	public int update(HolidayInfo model) {
		// TODO Auto-generated method stub
		return holidayInfoDAO.update(model);		
	}

	@Override
	public int delete(String id) {
		// TODO Auto-generated method stub
		return holidayInfoDAO.delete(id);
	}

	@Override
	public boolean exist(String id) {
		// TODO Auto-generated method stub
		int count = holidayInfoDAO.exist(id);
		
		return count > 0 ? true : false;
	}
	
	@Override
	public List<HolidayInfo> list() {
		// TODO Auto-generated method stub
		return holidayInfoDAO.list();
	}
		
	@Override
	public Page<HolidayInfo> pageSearch(Map<String, Object> searchParams, int pageNumber, int pageSize,boolean count,List<Sort> sortList) {
        Page<HolidayInfo> page = PageHelper.startPage(pageNumber,pageSize,count).doSelectPage(()->{
            holidayInfoDAO.search(searchParams,sortList);
        });
        
        return page;
	}


	@Override
	public Set<String> getHolidaySetByDate(String childrenCode, Date startDate, Date endDate, Boolean working) {
		List<HolidayInfo> list = holidayInfoDAO.findByCompanyCodeAndTimeRange(childrenCode,startDate,endDate);

		Set<String> holidaySet = new TreeSet<>();

		for (HolidayInfo holidayInfo : list) {
			if(holidayInfo.getWorking().equals(working)){
				DateTime beginTime = new DateTime(holidayInfo.getBeginDate());
				DateTime endTime = new DateTime(holidayInfo.getEndDate());

				int days = Days.daysBetween(beginTime,endTime).getDays();

				for (int i = 0; i <= days; i++) {
					holidaySet.add(beginTime.plusDays(i).toString("yyyy-MM-dd"));
				}
			}
		}

		return holidaySet;
	}

	@Override
	public Map<String,HolidayInfo> getHolidayMapByDate(String childrenCode, Date startDate, Date endDate, Boolean working) {
		List<HolidayInfo> list = holidayInfoDAO.findByCompanyCodeAndTimeRange(childrenCode,startDate,endDate);

		Map<String,HolidayInfo> holidayMap = new HashMap<>();

		for (HolidayInfo holidayInfo : list) {
			if(holidayInfo.getWorking().equals(working)){
				DateTime beginTime = new DateTime(holidayInfo.getBeginDate());
				DateTime endTime = new DateTime(holidayInfo.getEndDate());

				int days = Days.daysBetween(beginTime,endTime).getDays();

				for (int i = 0; i <= days; i++) {
					holidayMap.put(beginTime.plusDays(i).toString("yyyy-MM-dd"),holidayInfo);
				}
			}
		}

		return holidayMap;
	}

	@Override
	public boolean existByDateAndCompanyId(Date attendanceDate, String companyId) {
		HolidayInfo holidayInfo = holidayInfoDAO.existByDateAndCompanyId(attendanceDate,companyId);

		return holidayInfo!=null;
	}

	@Override
	public List<HolidayInfo> findByDate(Date date) {
		return holidayInfoDAO.findByDate(date);
	}
}