/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.workattendance.service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.sys.entity.Office;
import com.jeeplus.modules.sys.entity.User;
import com.jeeplus.modules.sys.utils.UserUtils;
import com.jeeplus.modules.workattendance.dao.WorkAttendanceInfoDao;
import com.jeeplus.modules.workattendance.dao.WorkAttendanceMonthDao;
import com.jeeplus.modules.workattendance.entity.WorkAttendanceInfo;
import com.jeeplus.modules.workattendance.entity.WorkAttendanceMonth;
import com.jeeplus.modules.workprojectnotify.entity.WorkProjectNotify;
import com.jeeplus.modules.workprojectnotify.service.WorkProjectNotifyService;
import com.jeeplus.modules.workstaff.entity.WorkStaffBasicInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.common.persistence.Page;
import com.jeeplus.common.service.CrudService;
import com.jeeplus.modules.workattendance.entity.WorkAttendanceCount;
import com.jeeplus.modules.workattendance.dao.WorkAttendanceCountDao;

/**
 * 考勤统计Service
 * @author 杨帆
 * @version 2018-09-14
 */
@Service
@Transactional(readOnly = true)
public class WorkAttendanceCountService extends CrudService<WorkAttendanceCountDao, WorkAttendanceCount> {
	@Autowired
	private WorkAttendanceMonthDao workAttendanceMonthDao;
	@Autowired
	private WorkAttendanceInfoDao workAttendanceInfoDao;
	@Autowired
	private WorkProjectNotifyService workProjectNotifyService;
	public static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
	public WorkAttendanceCount get(String id) {
		WorkAttendanceCount workAttendanceCount = super.get(id);
		WorkAttendanceMonth workAttendanceMonth = new WorkAttendanceMonth();
		workAttendanceMonth.setAttendanceMonth(workAttendanceCount.getAttendanceMonth());
		workAttendanceMonth.setOffice(workAttendanceCount.getOffice());
		List<WorkAttendanceMonth> workAttendanceMonths = workAttendanceMonthDao.findList(workAttendanceMonth);
		if (workAttendanceMonths!=null && workAttendanceMonths.size()!=0){
			workAttendanceCount.setWorkAttendanceMonths(workAttendanceMonths);
		}
		return workAttendanceCount;
	}
	
	public List<WorkAttendanceCount> findList(WorkAttendanceCount workAttendanceCount) {
		return super.findList(workAttendanceCount);
	}
	public List<WorkAttendanceCount> findListByMonth(WorkAttendanceCount workAttendanceCount) {
		return dao.findListByMonth(workAttendanceCount);
	}

	public List<WorkAttendanceCount> groupByOfficeList(WorkStaffBasicInfo workStaffBasicInfo) {
		return dao.groupByOfficeList(workStaffBasicInfo);
	}

	public Page<WorkAttendanceCount> findPage(Page<WorkAttendanceCount> page, WorkAttendanceCount workAttendanceCount) {
		return super.findPage(page, workAttendanceCount);
	}
	
	@Transactional(readOnly = false)
	public void save(WorkAttendanceCount workAttendanceCount) {
		workAttendanceCount.setStatus("0");
		workAttendanceCount.setUser(UserUtils.getUser());
		workAttendanceCount.setAttendanceDate(new Date());
		WorkProjectNotify workProjectNotify = new WorkProjectNotify();
		workProjectNotify.setNotifyId(workAttendanceCount.getId());
		workProjectNotifyService.readByNotifyId(workProjectNotify);
		super.save(workAttendanceCount);
	}
	@Transactional(readOnly = false)
	public void store(WorkAttendanceCount workAttendanceCount) throws ParseException {
		Calendar now = Calendar.getInstance();
		String day = UserUtils.getSysParamByCompany("attendance_date",workAttendanceCount.getCompanyId());
		String firstDay = StringUtils.firstDay("", day, workAttendanceCount.getCompanyId());
		String lastDay = StringUtils.lastDay("", day, workAttendanceCount.getCompanyId());
		WorkAttendanceInfo workAttendanceInfo = new WorkAttendanceInfo();
		try {
			workAttendanceInfo.setStartDate(format.parse(firstDay));
			workAttendanceInfo.setEndDate(format.parse(lastDay));
			workAttendanceInfo.setCompanyId(workAttendanceCount.getCompanyId());
			workAttendanceInfo.setOffice(workAttendanceCount.getOffice());
		} catch (ParseException e) {
			logger.info("ParseException e" + e);
		}
		List<WorkAttendanceMonth> WorkAttendanceMonths = workAttendanceInfoDao.findListByTime(workAttendanceInfo);
		String month = lastDay.substring(0, 7);
		WorkAttendanceMonth attendanceMonth = new WorkAttendanceMonth();
		attendanceMonth.setAttendanceMonth(month);
		attendanceMonth.setOffice(workAttendanceCount.getOffice());
		attendanceMonth.setCompanyId(workAttendanceCount.getCompanyId());
		workAttendanceMonthDao.deleteByMonth(attendanceMonth);
		for (WorkAttendanceMonth workAttendanceMonth : WorkAttendanceMonths) {
			workAttendanceMonth.setAttendanceMonth(month);
			workAttendanceMonth.preInsert();
			workAttendanceMonth.setUpdateBy(new User("1"));
			workAttendanceMonth.setCreateBy(new User("1"));
			workAttendanceMonthDao.insert(workAttendanceMonth);
		}
	}

	@Transactional(readOnly = false)
	public void delete(WorkAttendanceCount workAttendanceCount) {
		super.delete(workAttendanceCount);
	}
	
	
	
	
}