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

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

import com.jpsoft.shinestar.modules.base.dao.WorkPersonSchedulingDAO;
import com.jpsoft.shinestar.modules.base.dao.WorkShiftInfoDAO;
import com.jpsoft.shinestar.modules.base.entity.WorkPersonScheduling;
import com.jpsoft.shinestar.modules.base.entity.WorkShiftInfo;
import com.jpsoft.shinestar.modules.base.service.WorkPersonSchedulingService;
import com.jpsoft.shinestar.modules.common.utils.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import com.github.pagehelper.Page;
import com.jpsoft.shinestar.modules.common.dto.Sort;
import com.github.pagehelper.PageHelper;

@Transactional
@Component(value="workPersonSchedulingService")
public class WorkPersonSchedulingServiceImpl implements WorkPersonSchedulingService {
	@Resource(name="workPersonSchedulingDAO")
	private WorkPersonSchedulingDAO workPersonSchedulingDAO;
	@Resource(name="workShiftInfoDAO")
	private WorkShiftInfoDAO workShiftInfoDAO;

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

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

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

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

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

        return page;
	}

	@Override
	public WorkPersonScheduling findByYearMonthDayPersonId(int year, int month, int day, Long personId) {
		return workPersonSchedulingDAO.findByYearMonthDayPersonId(year,month,day,personId);
	}

	@Override
	public int deleteByPersonIdSchedulingDay(int year, int month, int day, Long personId){
		return workPersonSchedulingDAO.deleteByPersonIdSchedulingDay(year,month,day,personId);
	}

	@Override
	public WorkPersonScheduling findByPersonIdSchedulingDay(Long personId, Date dateTime) {
		List<WorkPersonScheduling> list = workPersonSchedulingDAO.findByPersonIdSchedulingDay(personId,dateTime);

		if(list.size()>0){
			return list.get(0);
		}
		else{
			return null;
		}
	}

	@Override
	public List<WorkPersonScheduling> findByPersonIdYearMonth(Long personId,int year,int month){
		return workPersonSchedulingDAO.findByPersonIdYearMonth(personId,year,month);
	}

	/***
	 *
	 * @param personId
	 * @param year
	 * @param months
	 * @param isQueryRest 查询是否有休息
	 * @return
	 */
	@Override
	public List<WorkPersonScheduling> findByPersonIdYearMonths(Long personId,int year,List<Integer> months,boolean isQueryRest){
		return workPersonSchedulingDAO.findByPersonIdYearMonths(personId,year,months,isQueryRest);
	}

	@Override
	public List<WorkPersonScheduling> findByPersonIdSDateAndEDate(Long personId,Date startDate,Date endDate,boolean isQueryRest){
		return workPersonSchedulingDAO.findByPersonIdSDateAndEDate(personId,startDate,endDate,isQueryRest);
	}

	@Override
	public BigDecimal findAttendanceDaysByPersonIdSDateAndEDate(Long personId, Date startDate, Date endDate, boolean isQueryRest){
		BigDecimal attendancedays = BigDecimal.ZERO;

		List<WorkPersonScheduling>  wpsList = workPersonSchedulingDAO.findByPersonIdSDateAndEDate(personId,startDate,endDate,isQueryRest);
		for(WorkPersonScheduling wps : wpsList){
			String shiftIds = wps.getShiftIds();
			if(StringUtils.isNotEmpty(shiftIds)){
				List<String> shiftList = Arrays.asList(shiftIds.split(","));
				for(String shiftId : shiftList){
					WorkShiftInfo wsi = workShiftInfoDAO.get(shiftId);

					if(wsi != null && wsi.getDays()!=null){
						attendancedays = attendancedays.add(wsi.getDays());
					}
				}
			}
		}

		return attendancedays;
	}

	@Override
	public List<Date> findSchedulingDayByPersonIdSDateAndEDate(Long personId,Date startDate,Date endDate,boolean isQueryRest){
		return workPersonSchedulingDAO.findSchedulingDayByPersonIdSDateAndEDate(personId,startDate,endDate,isQueryRest);
	}

	@Override
	public int findRepeatSchedulingDayByPersonId(Long personId){
		List<Date> schedulingDays =  workPersonSchedulingDAO.findRepeatSchedulingDay(personId);
		int count = 0;
		for(Date schedulingDay :schedulingDays) {
			List<WorkPersonScheduling> list = workPersonSchedulingDAO.findByPersonIdSchedulingDay(personId,schedulingDay);

			if(list.size()>0){
				WorkPersonScheduling wps = list.get(0);

				wps.setDelFlag(true);
				wps.setUpdateBy("批量操作");
				wps.setUpdateTime(new Date());
				int a = workPersonSchedulingDAO.update(wps);
				count = count + a;
			}
		}
		return count;
	}
}