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

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import com.jpsoft.shinestar.modules.base.dao.HolidayCompanyDAO;
import com.jpsoft.shinestar.modules.base.entity.HolidayCompany;
import com.jpsoft.shinestar.modules.base.service.HolidayCompanyService;
import com.github.pagehelper.Page;
import com.jpsoft.shinestar.modules.common.dto.Sort;
import com.github.pagehelper.PageHelper;

@Transactional
@Component(value="holidayCompanyService")
public class HolidayCompanyServiceImpl implements HolidayCompanyService {
	@Resource(name="holidayCompanyDAO")
	private HolidayCompanyDAO holidayCompanyDAO;

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

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

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

	@Override
	public void updateAndInsert(List<HolidayCompany> list,String holidayId,String subject,Date now){
		List<HolidayCompany> oldList = holidayCompanyDAO.findByHolidayId(holidayId);
		for (HolidayCompany holidayCompany:oldList) {
			holidayCompany.setDelFlag(true);
			holidayCompany.setUpdateBy(subject);
			holidayCompany.setUpdateTime(now);
			holidayCompanyDAO.update(holidayCompany);
		}

		for (HolidayCompany holidayCompany:list) {
			holidayCompanyDAO.insert(holidayCompany);
		}
	}

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

	@Override
	public boolean exist(String id) {
		// TODO Auto-generated method stub
		int count = holidayCompanyDAO.exist(id);
		
		return count > 0 ? true : false;
	}
	
	@Override
	public List<HolidayCompany> list() {
		// TODO Auto-generated method stub
		return holidayCompanyDAO.list();
	}

	@Override
	public List<HolidayCompany> findByHolidayId(String holidayId){
		return holidayCompanyDAO.findByHolidayId(holidayId);
	}
		
	@Override
	public Page<HolidayCompany> pageSearch(Map<String, Object> searchParams, int pageNumber, int pageSize,List<Sort> sortList) {
        Page<HolidayCompany> page = PageHelper.startPage(1,10).doSelectPage(()->{
            holidayCompanyDAO.search(searchParams,sortList);
        });
        
        return page;
	}

	@Override
	public boolean existHoliday(String companyId, String dateStr) {
		int affectCount = holidayCompanyDAO.existHoliday(companyId, dateStr);

		return affectCount>0;
	}
}