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

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import com.google.common.base.Strings;
import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.utils.GetLocation;
import com.jeeplus.common.utils.IdGen;
import com.jeeplus.common.utils.LocationUtils;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.leaveapply.entity.LeaveApply;
import com.jeeplus.modules.leaveapply.entity.LeaveDetail;
import com.jeeplus.modules.leaveapply.service.LeaveApplyService;
import com.jeeplus.modules.leavetype.dao.LeaveTypeDao;
import com.jeeplus.modules.leavetype.entity.LeaveType;
import com.jeeplus.modules.sys.dao.OfficeDao;
import com.jeeplus.modules.sys.dao.WorkattachmentDao;
import com.jeeplus.modules.sys.entity.Company;
import com.jeeplus.modules.sys.entity.Office;
import com.jeeplus.modules.sys.entity.User;
import com.jeeplus.modules.sys.entity.Workattachment;
import com.jeeplus.modules.sys.utils.DictUtils;
import com.jeeplus.modules.sys.utils.UserUtils;
import com.jeeplus.modules.utils.ErrorCode;
import com.jeeplus.modules.workattendance.dao.WorkAttendanceAuditDao;
import com.jeeplus.modules.workattendance.dao.WorkAttendanceMonthDao;
import com.jeeplus.modules.workattendance.entity.*;
import com.jeeplus.modules.workcalendar.service.WorkCalendarService;
import com.jeeplus.modules.workclientinfo.dao.WorkClientAttachmentDao;
import com.jeeplus.modules.workclientinfo.entity.WorkClientAttachment;
import com.jeeplus.modules.workfield.entity.WorkField;
import com.jeeplus.modules.workfield.service.WorkFieldService;
import com.jeeplus.modules.workout.entity.WorkOut;
import com.jeeplus.modules.workout.service.WorkOutService;
import com.jeeplus.modules.workovertime.entity.WorkOvertime;
import com.jeeplus.modules.workovertime.service.WorkOvertimeService;
import com.jeeplus.modules.workprojectnotify.entity.WorkProjectNotify;
import com.jeeplus.modules.workprojectnotify.service.WorkProjectNotifyService;
import com.jeeplus.modules.workprojectnotify.util.UtilNotify;
import com.jeeplus.modules.workstaff.dao.WorkStaffAchivesDao;
import com.jeeplus.modules.workstaff.entity.WorkStaffBasicInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
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.dao.WorkAttendanceInfoDao;

/**
 * 考勤详情Service
 * @author 杨帆
 * @version 2018-09-10
 */
@Service
@Transactional(readOnly = true)
public class WorkAttendanceInfoService extends CrudService<WorkAttendanceInfoDao, WorkAttendanceInfo> {
	@Autowired
	private WorkStaffAchivesDao workStaffAchivesDao;
	@Autowired
	private WorkAttendanceMonthDao workAttendanceMonthDao;
	@Autowired
	private WorkCalendarService workCalendarService;

	@Autowired
	private WorkOutService workOutService;
	@Autowired
	private WorkOvertimeService workOvertimeService;
	@Autowired
	private WorkFieldService workFieldService;
	@Autowired
	@Lazy
	private LeaveApplyService leaveApplyService;
	@Autowired
	private WorkAttendanceRuleService workAttendanceRuleService;
	@Autowired
    private WorkAttendanceAuditDao workAttendanceAuditDao;
	@Autowired
    private WorkProjectNotifyService workProjectNotifyService;
	@Autowired
	private WorkAttendanceCountService workAttendanceCountService;
	@Autowired
	private LeaveTypeDao leaveTypeDao;
	@Autowired
    private OfficeDao officeDao;
	@Autowired
	private WorkattachmentDao workattachmentDao;

	public static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
	public static SimpleDateFormat sdfs = new SimpleDateFormat("HHmm");

	public WorkAttendanceInfo get(String id) {
		WorkAttendanceInfo attendanceInfo = super.get(id);
		if (attendanceInfo!=null){
			if (attendanceInfo.getHolidayType()!=null){
				LeaveType leaveType = leaveTypeDao.get(attendanceInfo.getHolidayType());
				if (leaveType!=null){
					attendanceInfo.setLeaveTypeName(leaveType.getName());
				}
			}
			if (StringUtils.isNotBlank(attendanceInfo.getStaffId())){
				WorkStaffBasicInfo workStaffBasicInfo = workStaffAchivesDao.get(attendanceInfo.getStaffId());
				if (workStaffBasicInfo!=null && StringUtils.isNotBlank(workStaffBasicInfo.getId())){
					attendanceInfo.setWorkStaffBasicInfo(workStaffBasicInfo);
				}
			}
			List<Workattachment> workattachments = new ArrayList<>();
			if (attendanceInfo.getAttendanceDate()!=null && StringUtils.isNotBlank(attendanceInfo.getType())) {
				String time = format.format(attendanceInfo.getAttendanceDate());
				String cardType = "0";
				if ("2".equals(attendanceInfo.getType())) {
					cardType = "3";
				} else {
					cardType = attendanceInfo.getMorningAfternoon();
				}
				try {
					if (StringUtils.isNotBlank(attendanceInfo.getAttendanceType())) {
						if (attendanceInfo.getAttendanceType().equals("1")) {
							//附件
							Workattachment workattachment = new Workattachment();
							workattachment.setAttachmentId(attendanceInfo.getId());
							workattachment.setAttachmentFlag("115");
							workattachments = workattachmentDao.findList(workattachment);
						} else if (attendanceInfo.getAttendanceType().equals("2")) {
							WorkField workField = workFieldService.getByStaffIdAndTime(attendanceInfo.getStaffId(), time);
							if (workField != null && StringUtils.isNotBlank(workField.getId())) {
								String startDate = format.format(workField.getStartDate());
								String endDate = format.format(workField.getEndDate());
								String satrtType = workField.getStartType();
								String endType = workField.getEndType();
								if (startDate.equals(time) && satrtType.equals("2") && cardType.equals("1")) {
									logger.info("非外勤时间段");
								} else if (endDate.equals(time) && endType.equals("1") && cardType.equals("2")) {
									logger.info("非外勤时间段");
								}
								if (startDate.equals(time) && satrtType.equals("2") && cardType.equals("3")) {
									logger.info("非外勤时间段");
								} else if (endDate.equals(time) && endType.equals("1") && cardType.equals("3")) {
									logger.info("非外勤时间段");
								} else {
									//附件
									Workattachment workattachment = new Workattachment();
									workattachment.setAttachmentId(workField.getId());
									workattachment.setAttachmentFlag("75");
									workattachments = workattachmentDao.findList(workattachment);
								}
							}
						} else if (attendanceInfo.getAttendanceType().equals("3")) {
							WorkOut workOut = workOutService.getByStaffIdAndTime(attendanceInfo.getStaffId(), time);
							if (workOut != null && StringUtils.isNotBlank(workOut.getId())) {
								String startDate = format.format(workOut.getStartDate());
								String endDate = format.format(workOut.getEndDate());
								String satrtType = workOut.getStartType();
								String endType = workOut.getEndType();
								if (startDate.equals(time) && satrtType.equals("2") && cardType.equals("1")) {
									logger.info("非外出时间段");
								} else if (endDate.equals(time) && endType.equals("1") && cardType.equals("2")) {
									logger.info("非外出时间段");
								}
								if (startDate.equals(time) && satrtType.equals("2") && cardType.equals("3")) {
									logger.info("非外出时间段");
								} else if (endDate.equals(time) && endType.equals("1") && cardType.equals("3")) {
									logger.info("非外出时间段");
								} else {
									//附件
									Workattachment workattachment = new Workattachment();
									workattachment.setAttachmentId(workOut.getId());
									workattachment.setAttachmentFlag("73");
									workattachments = workattachmentDao.findList(workattachment);
								}
							}
						} else if (attendanceInfo.getAttendanceType().equals("4")) {
							LeaveApply leaveApply = leaveApplyService.getByStaffIdAndTime(attendanceInfo.getStaffId(), time);
							if (leaveApply != null && StringUtils.isNotBlank(leaveApply.getId())) {
								List<LeaveDetail> leaveDetails = leaveApply.getLeaveDetailList();
								boolean b = false;
								for (LeaveDetail leaveDetail:leaveDetails){
									String startDate = format.format(leaveDetail.getStartTime());
									String endDate = format.format(leaveDetail.getEndTime());
									String satrtType = leaveDetail.getStartType();
									String endType = leaveDetail.getEndType();
									if (startDate.equals(time) && satrtType.equals("2") && cardType.equals("1")) {
										logger.info("非外出时间段");
									} else if (endDate.equals(time) && endType.equals("1") && cardType.equals("2")) {
										logger.info("非外出时间段");
									}
									if (startDate.equals(time) && satrtType.equals("2") && cardType.equals("3")) {
										logger.info("非外出时间段");
									} else if (endDate.equals(time) && endType.equals("1") && cardType.equals("3")) {
										logger.info("非外出时间段");
									} else {
										b = true;
									}
								}
								if (b) {
									//附件
									Workattachment workattachment = new Workattachment();
									workattachment.setAttachmentId(leaveApply.getId());
									workattachment.setAttachmentFlag("114");
									workattachments = workattachmentDao.findList(workattachment);
								}
							}
						}
					}
					if (workattachments == null) {
						workattachments = new ArrayList<>();
					}
					if (StringUtils.isNotBlank(attendanceInfo.getIsOvertime()) && "1".equals(attendanceInfo.getIsOvertime())) {
						WorkOvertime workOvertime = workOvertimeService.getByStaffIdAndTime(attendanceInfo.getStaffId(), time);
						if (workOvertime != null && StringUtils.isNotBlank(workOvertime.getId())) {
							String startDate = format.format(workOvertime.getStartDate());
							String endDate = format.format(workOvertime.getEndDate());
							String satrtType = workOvertime.getStartType();
							String endType = workOvertime.getEndType();
							if (startDate.equals(time) && satrtType.equals("2") && cardType.equals("1")) {
								logger.info("非加班时间段");
							} else if (endDate.equals(time) && endType.equals("1") && cardType.equals("2")) {
								logger.info("非加班时间段");
							}
							if (startDate.equals(time) && satrtType.equals("2") && cardType.equals("3")) {
								logger.info("非加班时间段");
							} else if (endDate.equals(time) && endType.equals("1") && cardType.equals("3")) {
								logger.info("非加班时间段");
							} else {
								//附件
								Workattachment workattachment = new Workattachment();
								workattachment.setAttachmentId(workOvertime.getId());
								workattachment.setAttachmentFlag("72");
								List<Workattachment> workattachmentss = workattachmentDao.findList(workattachment);
								if (workattachmentss != null && workattachmentss.size() != 0) {
									workattachments.addAll(workattachments);
								}
							}
						}
					}
					if (workattachments != null && workattachments.size() != 0) {
						attendanceInfo.setWorkAttachments(workattachments);
					}
				}catch (Exception e){
					logger.info("workattachments Exception e:"+e);
				}
			}
		}else {
			attendanceInfo = getAudit(id);
		}
		return attendanceInfo;
	}
	public WorkAttendanceInfo getAudit(String id) {
		return workAttendanceAuditDao.get(id);
	}
	public Integer groupByOfficeStaffId(String month,String officeId) {
		return dao.groupByOfficeStaffId(month,officeId);
	}

	public List<WorkAttendanceInfo> findList(WorkAttendanceInfo workAttendanceInfo) {
		return super.findList(workAttendanceInfo);
	}
	public List<WorkAttendanceInfo> findLists(WorkAttendanceInfo workAttendanceInfo) {
	    Office company = UserUtils.getSelectCompany();
		String month = workAttendanceInfo.getMonth();
        String day = UserUtils.getSysParamByCompany("attendance_date",company.getId());
		String firstDay = StringUtils.firstDay(month,day,company.getId());
		String lastDay = StringUtils.lastDay(month,day,company.getId());
		try {
			workAttendanceInfo.setStartDate(format.parse(firstDay));
			workAttendanceInfo.setEndDate(format.parse(lastDay));
		} catch (ParseException e) {
			logger.info("ParseException e"+e);
		}
		List<WorkAttendanceInfo> workAttendanceInfos = super.findList(workAttendanceInfo);
		for (WorkAttendanceInfo attendanceInfo:workAttendanceInfos){
			if (attendanceInfo.getHolidayType()!=null){
				LeaveType leaveType = leaveTypeDao.get(attendanceInfo.getHolidayType());
				if (leaveType!=null){
					attendanceInfo.setLeaveTypeName(leaveType.getName());
				}
			}
		}
		return workAttendanceInfos;
	}
	public WorkAttendanceRule findAttendanceRuleByCompany(String companyId,String ruleId,String officeId,User user) {
		WorkAttendanceRule workAttendanceRule = new WorkAttendanceRule();
		if (!companyId.equals("1")){
			if(ruleId != null && !ruleId.equals("")){
				workAttendanceRule =workAttendanceRuleService.get(ruleId);
			}else{
				workAttendanceRule = workAttendanceRuleService.findAttendanceRuleByCompany(companyId,user.getId(),"");
				if(workAttendanceRule==null || StringUtils.isBlank(workAttendanceRule.getId())){
					workAttendanceRule = workAttendanceRuleService.findAttendanceRuleByCompany(companyId,"",officeId);
				}
			}
		}
		return workAttendanceRule;
	}

	@Transactional(readOnly = false)
	public List<WorkAttendanceInfo> findListInfo(WorkAttendanceInfo workAttendanceInfo) throws ParseException {
		List<WorkAttendanceInfo> workAttendanceInfos = super.findList(workAttendanceInfo);
		if (workAttendanceInfos==null || workAttendanceInfos.size()!=2){
			User user = UserUtils.getUser();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			String time = sdf.format(new Date());
			Date date = sdf.parse(time);
			int holidays = this.isHolidays(time);
			WorkStaffBasicInfo workStaffBasicInfo = new WorkStaffBasicInfo();
			workStaffBasicInfo.setUserId(user.getId());
			workStaffBasicInfo.setCompany(user.getCompany());
			List<WorkStaffBasicInfo> workStaffBasicInfoList = workStaffAchivesDao.getByCompanyInfo(workStaffBasicInfo);
			WorkStaffBasicInfo workStaffAchive =  new WorkStaffBasicInfo();
			if (workStaffBasicInfoList!=null && workStaffBasicInfoList.size()!=0){
				workStaffAchive = workStaffBasicInfoList.get(0);
			}
			if (workStaffAchive == null || StringUtils.isBlank(workStaffAchive.getId())){
				workAttendanceInfos = null;
			}else {
				//依据公司和规则编号查询规则
				WorkAttendanceRule oaRule = workAttendanceRuleService.findAttendanceRuleByCompany(user.getCompany().getId(), workStaffAchive.getUserId(), "");
				if (oaRule == null || StringUtils.isBlank(oaRule.getId())) {
					oaRule = workAttendanceRuleService.findAttendanceRuleByCompany(user.getCompany().getId(), "", workStaffAchive.getOffice().getId());
				}
				if (StringUtils.isNotBlank(workStaffAchive.getId()) && oaRule != null) {
					if (workAttendanceInfos.size() == 0) {
						this.saveInfo(oaRule, "", workStaffAchive.getId(), user, date, time, null, "", "", "",
								"0", "0", "", holidays, null,
								"", "", "",
								"", "", null, "", 0, "", "");
					} else if (workAttendanceInfos.size() == 1) {
						String count = workAttendanceInfos.get(0).getMorningAfternoon().equals("1") ? "2" : "1";
						this.saveInfo(oaRule, "", workStaffAchive.getId(), user, date, time, null, "", "", "",
								"0", "0", "", holidays, null,
								"", "", "",
								"", "", null, "", Integer.parseInt(count), "", "");
					}
				}
			}
		}
		return workAttendanceInfos;
	}

	public Page<WorkAttendanceInfo> findPage(Page<WorkAttendanceInfo> page, WorkAttendanceInfo workAttendanceInfo) {
		return super.findPage(page, workAttendanceInfo);
	}

	@Transactional(readOnly = false)
	public void save(WorkAttendanceInfo workAttendanceInfo) {
		super.save(workAttendanceInfo);
	}
	@Transactional(readOnly = false)
	public void saveAudit(String notifyId,String ids,String falg) {
		if (StringUtils.isBlank(ids)){
			ids = notifyId;
		}
		String[] idArr = ids.split(",");
		for(String id : idArr) {
			WorkAttendanceInfo attendanceInfo = getAudit(id);
			if (falg.equals("yes")) {
				String wid = attendanceInfo.getWid();
				attendanceInfo.setStatus("1");
				workAttendanceAuditDao.update(attendanceInfo);
				attendanceInfo.setId(wid);
				dao.update(attendanceInfo);
			} else {
				attendanceInfo.setStatus("2");
				workAttendanceAuditDao.update(attendanceInfo);
			}
		}
		WorkProjectNotify notify = new WorkProjectNotify();
		notify.setNotifyId(notifyId);
		workProjectNotifyService.readByNotifyId(notify);
	}

	@Transactional(readOnly = false)
	public AjaxJson addPunchCard(User user,String lnt,String lat,String wifi,String cardType,String ruleType,String atype,String palce,AjaxJson j) throws ParseException {
		if(StringUtils.isBlank(lat) || StringUtils.isBlank(lnt)){
			j.setSuccess(false);
			j.setErrorCode(ErrorCode.code_2005);
			j.setMsg("考勤定位失败，请重新定位");
			return j;
		}
		//依据公司和规则编号查询规则
		WorkAttendanceRule oaRule = workAttendanceRuleService.findAttendanceRuleByCompany(user.getCompany().getId(),user.getId(),"");
		if (oaRule==null || StringUtils.isBlank(oaRule.getId())){
			oaRule = workAttendanceRuleService.findAttendanceRuleByCompany(user.getCompany().getId(),"",user.getOffice().getId());
		}
		if(oaRule == null || StringUtils.isBlank(oaRule.getId())){
			j.setSuccess(false);
			j.setErrorCode(ErrorCode.code_2005);
			j.setMsg("当前用户没有考勤规则");
			return j;
		}
		if (oaRule.getType().equals("3")){
			j.setSuccess(false);
			j.setErrorCode(ErrorCode.code_2003);
			j.setMsg("当前考勤方式错误！");
			return j;
		}else if (oaRule.getType().equals("1") && !cardType.equals("3")){
			j.setSuccess(false);
			j.setErrorCode(ErrorCode.code_2003);
			j.setMsg("考勤方式错误,考勤规则为签到,不能进行打卡！");
			return j;
		}else if (oaRule.getType().equals("2") && cardType.equals("3")){
			j.setSuccess(false);
			j.setErrorCode(ErrorCode.code_2003);
			j.setMsg("考勤方式错误,考勤规则为打卡,不能进行签到！");
			return j;
		}
		List<WorkAttendancePlace> workAttendancePlaces = oaRule.getWorkAttendancePlaces();
		if(workAttendancePlaces==null || workAttendancePlaces.size()==0){
			j.setSuccess(false);
			j.setErrorCode(ErrorCode.code_2005);
			j.setMsg("没有考勤地点/wifi，无法进行考勤");
			return j;
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String time = sdf.format(new Date());
		Date date = sdf.parse(time);
		int holidays = this.isHolidays(time);
        WorkStaffBasicInfo workStaffBasicInfo = new WorkStaffBasicInfo();
        workStaffBasicInfo.setUserId(user.getId());
        workStaffBasicInfo.setCompany(user.getCompany());
        List<WorkStaffBasicInfo> workStaffBasicInfoList = workStaffAchivesDao.getByCompanyInfo(workStaffBasicInfo);
        String staffId = "";
        if (workStaffBasicInfoList!=null && workStaffBasicInfoList.size()!=0){
            staffId = workStaffBasicInfoList.get(0).getId();
        }
		Integer lateEarlyTime = 0;
		String lateEarly = "0";
		String type = "";
		String isValid = "0";
		String isOvertime = "0";
		int leaveType = 0;
		WorkAttendanceInfo workAttendanceInfo1 = null;
		WorkAttendanceInfo workAttendanceInfo2 = null;
		WorkAttendanceInfo workAttendanceInfo = new WorkAttendanceInfo();
		workAttendanceInfo.setStaffId(staffId);
		workAttendanceInfo.setAttendanceDate(date);
		List<WorkAttendanceInfo> workAttendanceInfos = this.findList(workAttendanceInfo);
		if (workAttendanceInfos == null || workAttendanceInfos.size() != 2) {
			j.setSuccess(false);
			j.setErrorCode(ErrorCode.code_2005);
			j.setMsg("考勤失败，当前考勤数据异常！");
			return j;
		}
		if (workAttendanceInfos.get(0).getMorningAfternoon().equals("1")) {
			workAttendanceInfo1 = workAttendanceInfos.get(0);
			workAttendanceInfo2 = workAttendanceInfos.get(1);
		} else {
			workAttendanceInfo1 = workAttendanceInfos.get(1);
			workAttendanceInfo2 = workAttendanceInfos.get(0);
		}
		if (workAttendanceInfo1.getType().equals("3") && cardType.equals("1")){
			j.setSuccess(false);
			j.setErrorCode(ErrorCode.code_2005);
			j.setMsg("考勤规则为不考勤，不能进行考勤！");
			return j;
		}
		if (workAttendanceInfo2.getType().equals("3") && cardType.equals("2")){
			j.setSuccess(false);
			j.setErrorCode(ErrorCode.code_2005);
			j.setMsg("考勤规则为不考勤，不能进行考勤！");
			return j;
		}
		if(!workAttendanceInfo1.getAttendanceType().equals("0") && cardType.equals("1")){
			j.setSuccess(false);
			j.setErrorCode(ErrorCode.code_2005);
			j.setMsg("上班已打卡！");
			return j;
		}else if(!workAttendanceInfo2.getAttendanceType().equals("0") && cardType.equals("2")){
			j.setSuccess(false);
			j.setErrorCode(ErrorCode.code_2005);
			j.setMsg("下班已打卡！");
			return j;
		}else if(((!workAttendanceInfo1.getAttendanceType().equals("0") && !workAttendanceInfo1.getAttendanceType().equals("4"))|| (!workAttendanceInfo2.getAttendanceType().equals("0")&& !workAttendanceInfo2.getAttendanceType().equals("4"))) && cardType.equals("3")){
			j.setSuccess(false);
			j.setErrorCode(ErrorCode.code_2005);
			j.setMsg("已签到,不能重新签到！");
			return j;
		}
		if (workAttendanceInfo1.getAttendanceType().equals("4") && !cardType.equals("2")){
			if (cardType.equals("1")) {
				type = "4";
			}else if(cardType.equals("3")){
				if (workAttendanceInfo2.getAttendanceType().equals("4")){
					type = "4";
				}else if (workAttendanceInfo2.getAttendanceType().equals("0")){
					leaveType = 2;
				}
			}
		}
		if (workAttendanceInfo2.getAttendanceType().equals("4")){
			if (cardType.equals("2")) {
				type = "4";
			}else if(cardType.equals("3")){
				if (workAttendanceInfo1.getAttendanceType().equals("4")){
					type = "4";
				}else if (workAttendanceInfo1.getAttendanceType().equals("0")){
					leaveType = 1;
				}
			}
		}

		/*if (leaveApply!=null && StringUtils.isNotBlank(leaveApply.getId())){
			String startDate = sdf.format(leaveApply.getStartTime());
			String endDate = sdf.format(leaveApply.getEndTime());
			String satrtType = leaveApply.getStartType();
			String endType = leaveApply.getEndType();
			if (startDate.equals(time) && satrtType.equals("2") && cardType.equals("1")){
				logger.info("非请假时间段");
				leaveType = 1;
			}else if (endDate.equals(time) && endType.equals("1") && cardType.equals("2")){
				logger.info("非请假时间段");
				leaveType = 2;
			}else if (startDate.equals(time) && satrtType.equals("2") && cardType.equals("3")){
				logger.info("非请假时间段");
				leaveType = 2;
			}else if (endDate.equals(time) && endType.equals("1") && cardType.equals("3")){
				logger.info("非请假时间段");
				leaveType = 1;
			}else {
				type = "4";
			}
		}*/
		if (!type.equals("4")) {
			if (holidays > 0 && oaRule.getNotWorkCount().equals("1")) {
				WorkOvertime workOvertime = workOvertimeService.getByStaffIdAndTime(staffId, time);
				if (workOvertime != null && StringUtils.isNotBlank(workOvertime.getId())) {
					String startDate = sdf.format(workOvertime.getStartDate());
					String endDate = sdf.format(workOvertime.getEndDate());
					String satrtType = workOvertime.getStartType();
					String endType = workOvertime.getEndType();
					if (startDate.equals(time) && satrtType.equals("2") && cardType.equals("1")) {
						logger.info("非加班时间段");
					} else if (endDate.equals(time) && endType.equals("1") && cardType.equals("2")) {
						logger.info("非加班时间段");
					}if (startDate.equals(time) && satrtType.equals("2") && cardType.equals("3")) {
						logger.info("非加班时间段");
					} else if (endDate.equals(time) && endType.equals("1") && cardType.equals("3")) {
						logger.info("非加班时间段");
					} else {
						isOvertime = "1";
					}
				}
			}
			WorkOut workOut = workOutService.getByStaffIdAndTime(staffId, time);
			WorkField workField = workFieldService.getByStaffIdAndTime(staffId, time);
			if (workOut != null && StringUtils.isNotBlank(workOut.getId())) {
				String startDate = sdf.format(workOut.getStartDate());
				String endDate = sdf.format(workOut.getEndDate());
				String satrtType = workOut.getStartType();
				String endType = workOut.getEndType();
				if (startDate.equals(time) && satrtType.equals("2") && cardType.equals("1")) {
					type = "1";
				} else if (endDate.equals(time) && endType.equals("1") && cardType.equals("2")) {
					type = "1";
				} else {
					isValid = "1";
					type = "3";
				}
			} else if (workField != null && StringUtils.isNotBlank(workField.getId())) {
				String startDate = sdf.format(workField.getStartDate());
				String satrtType = workField.getStartType();
				String endDate = sdf.format(workField.getEndDate());
				String endType = workField.getEndType();
				if (startDate.equals(time) && satrtType.equals("2") && cardType.equals("1")) {
					type = "1";
				} else if (endDate.equals(time) && endType.equals("1") && cardType.equals("2")) {
					type = "1";
				}if (startDate.equals(time) && satrtType.equals("2") && cardType.equals("3")) {
					type = "1";
				} else if (endDate.equals(time) && endType.equals("1") && cardType.equals("3")) {
					type = "1";
				} else {
					isValid = "1";
					type = "2";
				}
			} else {
				type = "1";
			}
			Boolean status = false;
			int i1 = 3;
			String city = GetLocation.getLogLatByCity(lnt, lat);
			if (type.equals("1")) {
				for (WorkAttendancePlace oap : workAttendancePlaces) {
					if (oap.getArea().getName().equals(city)) {
						i1 = 2;
					}
					if (StringUtils.isNotBlank(oap.getWifi()) && oap.getWifi().equals(wifi)) {
						status = true;
						break;
					} else {
						double placeScope = 0d;
						double longitude = 0d;
						if (oap.getPlaceScope() != null && oap.getPlaceScope() > 0) {
							placeScope = oap.getPlaceScope();
						} else {
							placeScope = 200;
						}
						if (!Strings.isNullOrEmpty(oap.getLongitude())) {
							longitude = Double.parseDouble(oap.getLongitude());
						}
						double latitude = 0d;
						if (!Strings.isNullOrEmpty(oap.getLatitude())) {
							latitude = Double.parseDouble(oap.getLatitude());
						}
						double lntd = 0d;
						double latd = 0d;
						if (!Strings.isNullOrEmpty(lnt)) {
							lntd = Double.parseDouble(lnt);
						}
						if (!Strings.isNullOrEmpty(lat)) {
							latd = Double.parseDouble(lat);
						}
						double distance = LocationUtils.getDistance(longitude, latitude, lntd, latd);
						Double d = new Double(distance);
						Double dp = new Double(placeScope);
						int i2 = d.compareTo(dp);
						if (i2 <= 0) {
							status = true;
							break;
						}
					}
				}
			}
			if (type.equals("1")) {
				if (status) {
					isValid = "1";
				} else {
					type = i1 + "";
				}
			}
			if (oaRule.getStartTime()!=null && oaRule.getEndTime()!=null) {
				lateEarlyTime = StringUtils.lateEarlyTime(cardType, oaRule.getStartTime(), oaRule.getEndTime(), new Date());
			}
			if(lateEarlyTime!=0 && !cardType.equals("3")){
				lateEarly = cardType;
			}
			if (StringUtils.isBlank(city)){
				type = ruleType;
			}
			if (oaRule.getType().equals(atype) && type.equals(ruleType)) {
				if (leaveType>0){
					this.saveInfo(oaRule,"", staffId, user, date, time,new Date(), lnt, lat, wifi,
							type, type, isOvertime, holidays, 0,
							"", "1", "0",
							palce, lateEarly, lateEarlyTime, isValid, leaveType, workAttendanceInfo1.getId(), workAttendanceInfo2.getId());
				}else{
					this.saveInfo(oaRule,"", staffId, user, date, time,new Date(), lnt, lat, wifi,
							type, type, isOvertime, holidays, 0,
							"", "1", "0",
							palce, lateEarly, lateEarlyTime, isValid, Integer.parseInt(cardType), workAttendanceInfo1.getId(), workAttendanceInfo2.getId());

				}
				j.setSuccess(true);
				j.setErrorCode(ErrorCode.code_1004);
				j.setMsg("考勤成功!");
			}else {
				j.setSuccess(false);
				j.setErrorCode(ErrorCode.code_2003);
				j.setMsg("考勤信息不一致,请重新考勤");
				return j;
			}
		}else {
			j.setSuccess(false);
			j.setErrorCode(ErrorCode.code_1004);
			j.setMsg("请假中不能进行考勤");
		}
		return j;
	}
	@Transactional(readOnly = false)
	public AjaxJson addPunchCardAudit(User user,String cardType,String ruleType,String addendanceDate,String reason,AjaxJson j) throws ParseException {
		//依据公司和规则编号查询规则
		WorkAttendanceRule oaRule = workAttendanceRuleService.findAttendanceRuleByCompany(user.getCompany().getId(),user.getId(),"");
		if (oaRule==null || StringUtils.isBlank(oaRule.getId())){
			oaRule = workAttendanceRuleService.findAttendanceRuleByCompany(user.getCompany().getId(),"",user.getOffice().getId());
		}
		if(oaRule == null || StringUtils.isBlank(oaRule.getId())){
			j.setSuccess(false);
			j.setErrorCode(ErrorCode.code_2005);
			j.setMsg("当前用户没有考勤规则");
			return j;
		}
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        SimpleDateFormat sdfDay = new SimpleDateFormat("yyyy-MM-dd");
        Date date = sdfDay.parse(addendanceDate);
        int holidays = this.isHolidays(addendanceDate);
        WorkStaffBasicInfo workStaffBasicInfo = new WorkStaffBasicInfo();
        workStaffBasicInfo.setUserId(user.getId());
        workStaffBasicInfo.setCompany(user.getCompany());
        List<WorkStaffBasicInfo> workStaffBasicInfoList = workStaffAchivesDao.getByCompanyInfo(workStaffBasicInfo);
        String staffId = "";
        if (workStaffBasicInfoList!=null && workStaffBasicInfoList.size()!=0){
            staffId = workStaffBasicInfoList.get(0).getId();
        }
        LeaveApply leaveApply = leaveApplyService.getByStaffIdAndTime(staffId,addendanceDate);
        Boolean status = false;
        int leaveType = 0;
		WorkAttendanceInfo workAttendanceInfo1 = null;
		WorkAttendanceInfo workAttendanceInfo2 = null;
        WorkAttendanceInfo workAttendanceInfo = new WorkAttendanceInfo();
        workAttendanceInfo.setStaffId(staffId);
        workAttendanceInfo.setAttendanceDate(date);
        List<WorkAttendanceInfo> workAttendanceInfos = this.findList(workAttendanceInfo);
        if (workAttendanceInfos == null || workAttendanceInfos.size() != 2) {
            j.setSuccess(false);
            j.setErrorCode(ErrorCode.code_2005);
            j.setMsg("失败，当前考勤数据异常！");
            return j;
        }
		if (workAttendanceInfos.get(0).getMorningAfternoon().equals("1")) {
			workAttendanceInfo1 = workAttendanceInfos.get(0);
			workAttendanceInfo2 = workAttendanceInfos.get(1);
		} else {
			workAttendanceInfo1 = workAttendanceInfos.get(1);
			workAttendanceInfo2 = workAttendanceInfos.get(0);
		}
        if (leaveApply!=null && StringUtils.isNotBlank(leaveApply.getId())){
            String startDate = sdf.format(leaveApply.getStartTime());
            String endDate = sdf.format(leaveApply.getEndTime());
            String satrtType = leaveApply.getStartType();
            String endType = leaveApply.getEndType();
            if (startDate.equals(addendanceDate) && satrtType.equals("2") && cardType.equals("1")){
                logger.info("非请假时间段");
                leaveType = 1;
            }else if (endDate.equals(addendanceDate) && endType.equals("1") && cardType.equals("2")){
                logger.info("非请假时间段");
                leaveType = 2;
            }else if (startDate.equals(addendanceDate) && satrtType.equals("2") && cardType.equals("3")){
                logger.info("非请假时间段");
                leaveType = 2;
            }else if (endDate.equals(addendanceDate) && endType.equals("1") && cardType.equals("3")){
                logger.info("非请假时间段");
                leaveType = 1;
            }else {
                status = true;
            }
        }
		if(!status){
            List<String> ids = new ArrayList<>();
            if (leaveType>0) {
				ids = this.saveInfoAudit(oaRule,"", staffId, user, date, addendanceDate, "", "", "",
						ruleType, ruleType,"", holidays, 0,
                        "", "1", "0",
                        "", "0", null, "1",reason, leaveType,workAttendanceInfo1.getId(),workAttendanceInfo2.getId());
            }else {
                ids = this.saveInfoAudit(oaRule,"", staffId, user, date,addendanceDate, "", "", "",
						ruleType, ruleType,"", holidays, 0,
                        "", "1", "0",
                        "", "0", null, "1",reason, Integer.parseInt(cardType),workAttendanceInfo1.getId(),workAttendanceInfo2.getId());
            }
            Office office = UserUtils.getSelectOffice();
            List<User> bmzrs = UserUtils.getByRoleActivityEnname("bmzr",2,office.getId(),"26",user);
			String title = "补卡类型:"+ DictUtils.getDictLabel(ruleType,"attendance_info_type","");
			String str = "补卡类型:"+ DictUtils.getDictLabel(ruleType,"attendance_info_type","")+",补卡时间:"+addendanceDate;
            WorkProjectNotify workProjectNotify = UtilNotify
                    .saveNotify(ids.get(0),
                            null,
                            user.getCompany().getId(),
                            title,
                            str,
                            "83",
                            "0",
                            "待审批",
                            ""
                    );
            if (bmzrs==null || bmzrs.size()==0){
				j.setSuccess(false);
				j.setErrorCode(ErrorCode.code_1004);
				j.setMsg("补卡失败,当前部门负责人为空,请联系管理员！");
				return j;
			}
            for (User u : bmzrs){
                workProjectNotify.setUser(u);
                workProjectNotify.setId("");
                workProjectNotify.setNotifyRole("部门负责人审批");
                workProjectNotifyService
                        .save(workProjectNotify);
                Map<String,Object> extras = new HashMap<>();
                extras.put("type","7002");
                extras.put("id",workProjectNotify.getId());
                extras.put("procDefKey","83");
                UserUtils.pushInfoToApp(title,str,extras,u.getId());
                UserUtils.pushIm(u.getId(),str);
            }
            Map<String,Object> map = new HashMap<>();
            map.put("id",ids.get(0));
            j.put("data",map);
            j.setMsg("补卡申请已发起");
        }else {
			j.put("data","");
			j.setSuccess(false);
			j.setErrorCode(ErrorCode.code_1004);
			j.setMsg("请假中不能进行补卡");
		}
		return j;
	}

	@Transactional(readOnly = false)
	public AjaxJson updatePunchCard(User user,String lnt,String lat,String wifi,String cardType,String ruleType,String atype,String palce,AjaxJson j) throws ParseException {
		//依据公司和规则编号查询规则
		WorkAttendanceRule oaRule = workAttendanceRuleService.findAttendanceRuleByCompany(user.getCompany().getId(),user.getId(),"");
		if (oaRule==null || StringUtils.isBlank(oaRule.getId())){
			oaRule = workAttendanceRuleService.findAttendanceRuleByCompany(user.getCompany().getId(),"",user.getOffice().getId());
		}
		if(oaRule == null || StringUtils.isBlank(oaRule.getId())){
			j.setErrorCode(ErrorCode.code_2005);
			j.setSuccess(false);
			j.setMsg("当前用户没有考勤规则");
			return j;
		}
		if (oaRule.getType().equals("3")){
			j.setErrorCode(ErrorCode.code_2003);
			j.setSuccess(false);
			j.setMsg("当前考勤方式错误！");
			return j;
		}else if (oaRule.getType().equals("1") && !cardType.equals("3")){
			j.setErrorCode(ErrorCode.code_2003);
			j.setSuccess(false);
			j.setMsg("考勤方式错误,考勤规则为签到,不能进行打卡！");
			return j;
		}else if (oaRule.getType().equals("2") && cardType.equals("3")){
			j.setErrorCode(ErrorCode.code_2003);
			j.setSuccess(false);
			j.setMsg("考勤方式错误,考勤规则为打卡,不能进行签到！");
			return j;
		}
		List<WorkAttendancePlace> workAttendancePlaces = oaRule.getWorkAttendancePlaces();
		if(workAttendancePlaces==null || workAttendancePlaces.size()==0){
			j.setErrorCode(ErrorCode.code_2005);
			j.setSuccess(false);
			j.setMsg("没有考勤地点/wifi，无法更新考勤");
			return j;
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String time = sdf.format(new Date());
		Date date = sdf.parse(time);
		int holidays = this.isHolidays(time);
		WorkStaffBasicInfo workStaffBasicInfo = new WorkStaffBasicInfo();
		workStaffBasicInfo.setUserId(user.getId());
		workStaffBasicInfo.setCompany(user.getCompany());
		List<WorkStaffBasicInfo> workStaffBasicInfoList = workStaffAchivesDao.getByCompanyInfo(workStaffBasicInfo);
		String staffId = "";
		if (workStaffBasicInfoList!=null && workStaffBasicInfoList.size()!=0){
			staffId = workStaffBasicInfoList.get(0).getId();
		}
		Integer lateEarlyTime = 0;
		String lateEarly = "";
		LeaveApply leaveApply = leaveApplyService.getByStaffIdAndTime(staffId,time);
		String type = "";
		String isValid = "0";
		String isOvertime = "";
		int leaveType = 0;
		WorkAttendanceInfo workAttendanceInfo1 = null;
		WorkAttendanceInfo workAttendanceInfo2 = null;
		WorkAttendanceInfo workAttendanceInfo = new WorkAttendanceInfo();
		workAttendanceInfo.setStaffId(staffId);
		workAttendanceInfo.setAttendanceDate(date);
		List<WorkAttendanceInfo> workAttendanceInfos = this.findList(workAttendanceInfo);
		if (workAttendanceInfos == null || workAttendanceInfos.size() != 2) {
			j.setSuccess(false);
			j.setErrorCode(ErrorCode.code_2005);
			j.setMsg("考勤失败，当前考勤数据异常！");
			return j;
		}
		if (workAttendanceInfos.get(0).getMorningAfternoon().equals("1")) {
			workAttendanceInfo1 = workAttendanceInfos.get(0);
			workAttendanceInfo2 = workAttendanceInfos.get(1);
		} else {
			workAttendanceInfo1 = workAttendanceInfos.get(1);
			workAttendanceInfo2 = workAttendanceInfos.get(0);
		}
		if (leaveApply!=null && StringUtils.isNotBlank(leaveApply.getId())){
			String startDate = sdf.format(leaveApply.getStartTime());
			String endDate = sdf.format(leaveApply.getEndTime());
			String satrtType = leaveApply.getStartType();
			String endType = leaveApply.getEndType();
			if (startDate.equals(time) && satrtType.equals("2") && cardType.equals("1")){
				logger.info("非请假时间段");
				leaveType = 1;
			}else if (endDate.equals(time) && endType.equals("1") && cardType.equals("2")){
				logger.info("非请假时间段");
				leaveType = 2;
			}else if (startDate.equals(time) && satrtType.equals("2") && cardType.equals("3")){
				logger.info("非请假时间段");
				leaveType = 2;
			}else if (endDate.equals(time) && endType.equals("1") && cardType.equals("3")){
				logger.info("非请假时间段");
				leaveType = 1;
			}else {
				type = "4";
			}
		}
		if (!type.equals("4")) {
			if (holidays > 0 && oaRule.getNotWorkCount().equals("1")) {
				WorkOvertime workOvertime = workOvertimeService.getByStaffIdAndTime(staffId, time);
				if (workOvertime != null && StringUtils.isNotBlank(workOvertime.getId())) {
					String startDate = sdf.format(workOvertime.getStartDate());
					String endDate = sdf.format(workOvertime.getEndDate());
					String satrtType = workOvertime.getStartType();
					String endType = workOvertime.getEndType();
					if (startDate.equals(time) && satrtType.equals("2") && cardType.equals("1")) {
						logger.info("非加班时间段");
					} else if (endDate.equals(time) && endType.equals("1") && cardType.equals("2")) {
						logger.info("非加班时间段");
					}if (startDate.equals(time) && satrtType.equals("2") && cardType.equals("3")) {
						logger.info("非加班时间段");
					} else if (endDate.equals(time) && endType.equals("1") && cardType.equals("3")) {
						logger.info("非加班时间段");
					} else {
						isOvertime = "0";
					}
				}
			}
			WorkOut workOut = workOutService.getByStaffIdAndTime(staffId, time);
			WorkField workField = workFieldService.getByStaffIdAndTime(staffId, time);
			if (workOut != null && StringUtils.isNotBlank(workOut.getId())) {
				String startDate = sdf.format(workOut.getStartDate());
				String endDate = sdf.format(workOut.getEndDate());
				String satrtType = workOut.getStartType();
				String endType = workOut.getEndType();
				if (startDate.equals(time) && satrtType.equals("2") && cardType.equals("1")) {
					type = "1";
				} else if (endDate.equals(time) && endType.equals("1") && cardType.equals("2")) {
					type = "1";
				} if (startDate.equals(time) && satrtType.equals("2") && cardType.equals("3")) {
					type = "1";
				} else if (endDate.equals(time) && endType.equals("1") && cardType.equals("3")) {
					type = "1";
				}else {
					isValid = "1";
					type = "3";
				}
			} else if (workField != null && StringUtils.isNotBlank(workField.getId())) {
				String startDate = sdf.format(workField.getStartDate());
				String satrtType = workField.getStartType();
				String endDate = sdf.format(workField.getEndDate());
				String endType = workField.getEndType();
				if (startDate.equals(time) && satrtType.equals("2") && cardType.equals("1")) {
					type = "1";
				} else if (endDate.equals(time) && endType.equals("1") && cardType.equals("2")) {
					type = "1";
				} else if (startDate.equals(time) && satrtType.equals("2") && cardType.equals("3")) {
					type = "1";
				} else if (endDate.equals(time) && endType.equals("1") && cardType.equals("3")) {
					type = "1";
				}else {
					isValid = "1";
					type = "2";
				}
			} else {
				type = "1";
			}
			Boolean status = false;
			int i1 = 3;
			String city = GetLocation.getLogLatByCity(lnt, lat);
			if (type.equals("1")) {
				for (WorkAttendancePlace oap : workAttendancePlaces) {
					if (oap.getArea().getName().equals(city)) {
						i1 = 2;
					}
					if (StringUtils.isNotBlank(oap.getWifi()) && oap.getWifi().equals(wifi)) {
						status = true;
						break;
					} else {
						double placeScope = 0d;
						double longitude = 0d;
						if (oap.getPlaceScope() != null && oap.getPlaceScope() > 0) {
							placeScope = oap.getPlaceScope();
						} else {
							placeScope = 200;
						}
						if (!Strings.isNullOrEmpty(oap.getLongitude())) {
							longitude = Double.parseDouble(oap.getLongitude());
						}
						double latitude = 0d;
						if (!Strings.isNullOrEmpty(oap.getLatitude())) {
							latitude = Double.parseDouble(oap.getLatitude());
						}
						double lntd = 0d;
						double latd = 0d;
						if (!Strings.isNullOrEmpty(lnt)) {
							lntd = Double.parseDouble(lnt);
						}
						if (!Strings.isNullOrEmpty(lat)) {
							latd = Double.parseDouble(lat);
						}
						double distance = LocationUtils.getDistance(longitude, latitude, lntd, latd);
						Double d = new Double(distance);
						Double dp = new Double(placeScope);
						int i2 = d.compareTo(dp);
						if (i2 <= 0) {
							status = true;
							break;
						}
					}
				}
			}
			if (type.equals("1")) {
				if (status) {
					isValid = "1";
				} else {
					type = i1 + "";
				}
			}
			lateEarly = "0";
			if (oaRule.getStartTime()!=null && oaRule.getEndTime()!=null){
				lateEarlyTime = StringUtils.lateEarlyTime(cardType,oaRule.getStartTime(),oaRule.getEndTime(),new Date());
			}
			if(lateEarlyTime!=0 && !cardType.equals("3")){
				lateEarly = cardType;
			}
			Boolean b = false;
			if(!workAttendanceInfo1.getType().equals("0") && cardType.equals("1")){
				b = true;
			}else if(!workAttendanceInfo2.getType().equals("0") && cardType.equals("2")){
				b = true;
			}else if((!workAttendanceInfo1.getType().equals("0") || !workAttendanceInfo2.getType().equals("0")) && cardType.equals("3")){
				b = true;
			}
			if (oaRule.getType().equals(atype) && type.equals(ruleType) && b) {
				if (leaveType>0){
					this.saveInfo(oaRule,"", staffId, user, date, time,new Date(), lnt, lat, wifi,
							type, type, isOvertime, holidays, 0,
							"", "1", "0",
							palce, lateEarly, lateEarlyTime, isValid, leaveType, workAttendanceInfo1.getId(), workAttendanceInfo2.getId());
				}else {
					this.saveInfo(oaRule,"", staffId, user, date, time,new Date(), lnt, lat, wifi,
							type, type, isOvertime, holidays, 0,
							"", "1", "0",
							palce, lateEarly, lateEarlyTime, isValid, Integer.parseInt(cardType), workAttendanceInfo1.getId(), workAttendanceInfo2.getId());
				}
				j.setSuccess(true);
				j.setErrorCode(ErrorCode.code_1004);
				j.setMsg("更新考勤成功!");
			}else {
				j.setSuccess(false);
				j.setErrorCode(ErrorCode.code_2003);
				j.setMsg("考勤信息不一致,请重新考勤");
				return j;
			}
		}else {
			j.setSuccess(false);
			j.setErrorCode(ErrorCode.code_1004);
			j.setMsg("请假中不能进行更新考勤");
		}
		return j;
	}

	@Transactional(readOnly = false)
	public void delete(WorkAttendanceInfo workAttendanceInfo) {
		super.delete(workAttendanceInfo);
	}

	@Transactional(readOnly = false)
	public void saveInfo(WorkAttendanceRule workAttendanceRule,String type,String staffId,User user,Date date,String time,Date attendanceTime,String lnt,String lat,String wifi,
						 String type1,String type2,
						 String isOvertime,Integer dateType,Integer overtimeTime,String holidayType,
						 String isCalculateAttendance,String isCalculateSubsidies,String attendancePlace,
						 String lateEarly,Integer lateEarlyTime,String isValid,int count,String id1,String id2) {
		if (StringUtils.isNotBlank(staffId) && workAttendanceRule!=null && StringUtils.isNotBlank(workAttendanceRule.getId())){
			String isHolidays = "0";
			if(isHolidays(time)!=0){
				isHolidays = "1";
			}
			if (StringUtils.isBlank(type)){
				type = workAttendanceRule.getType();
			}
			WorkAttendanceInfo attendanceInfo = new WorkAttendanceInfo();
			attendanceInfo.setStaffId(staffId);
			attendanceInfo.setCompanyId(user.getCompany().getId());
			attendanceInfo.setOffice(user.getOffice());
			attendanceInfo.setAttendanceDate(date);
			attendanceInfo.setMorningAfternoon("1");
			attendanceInfo.setWorkAttendanceRule(workAttendanceRule);
			attendanceInfo.setLat(lat);
			attendanceInfo.setLnt(lnt);
			attendanceInfo.setWifi(wifi);
			attendanceInfo.setIsOvertime(isOvertime);
			attendanceInfo.setDateType(dateType+"");
			attendanceInfo.setOvertimeTime(overtimeTime);
			attendanceInfo.setHolidayType(holidayType);
			attendanceInfo.setIsCalculateAttendance(isCalculateAttendance);
			attendanceInfo.setIsCalculateSubsidies(isCalculateSubsidies);
			attendanceInfo.setAttendanceType(type1);
			attendanceInfo.setAttendancePlace(attendancePlace);
			attendanceInfo.setLateEarly(lateEarly);
			attendanceInfo.setLateEarlyTime(lateEarlyTime);
			attendanceInfo.setIsValid(isValid);
			attendanceInfo.setIsHolidays(isHolidays);
			attendanceInfo.setType(StringUtils.isNotBlank(type)?type:workAttendanceRule.getType());
			attendanceInfo.setUserId(user.getId());
			attendanceInfo.setDelFlag("0");
			attendanceInfo.setAttendanceTime(attendanceTime);
			Date dateTime = new Date();
			attendanceInfo.setUpdateDate(dateTime);
			if(count == 0) {
				if(StringUtils.isBlank(id1)){
					attendanceInfo.setCreateDate(dateTime);
					attendanceInfo.setId(IdGen.uuid());
				}
				attendanceInfo.setUpdateBy(new User("1"));
				attendanceInfo.setCreateBy(new User("1"));
				if(StringUtils.isBlank(id1)){
					dao.insert(attendanceInfo);
				}else {
					dao.update(attendanceInfo);
				}
				WorkAttendanceInfo workAttendanceInfo = new WorkAttendanceInfo();
				workAttendanceInfo.setStaffId(staffId);
				workAttendanceInfo.setCompanyId(user.getCompany().getId());
				workAttendanceInfo.setOffice(user.getOffice());
				workAttendanceInfo.setAttendanceDate(date);
				workAttendanceInfo.preInsert();
				workAttendanceInfo.setMorningAfternoon("2");
				workAttendanceInfo.setCreateBy(new User("1"));
				workAttendanceInfo.setUpdateBy(new User("1"));
				workAttendanceInfo.setType(StringUtils.isNotBlank(type)?type:workAttendanceRule.getType());
				workAttendanceInfo.setIsHolidays(isHolidays);
				workAttendanceInfo.setUserId(user.getId());
				workAttendanceInfo.setDateType(dateType+"");
				workAttendanceInfo.setWorkAttendanceRule(workAttendanceRule);
				workAttendanceInfo.setAttendanceType(type2);
				workAttendanceInfo.setAttendanceTime(attendanceTime);
				workAttendanceInfo.setDelFlag("0");
				if(StringUtils.isBlank(id2)){
					dao.insert(workAttendanceInfo);
				}else {
					attendanceInfo.setUpdateDate(new Date());
					dao.update(workAttendanceInfo);
				}
			}else {
				attendanceInfo.setCreateBy(new User("1"));
				attendanceInfo.setUpdateBy(new User("1"));
				attendanceInfo.setMorningAfternoon(count + "");
				attendanceInfo.setIsHolidays(isHolidays);
				if (count == 1) {
					if (StringUtils.isBlank(id1)) {
						attendanceInfo.setCreateDate(dateTime);
						attendanceInfo.setId(IdGen.uuid());
						dao.insert(attendanceInfo);
					} else {
						attendanceInfo.setUpdateDate(new Date());
						attendanceInfo.setId(id1);
						dao.update(attendanceInfo);
					}
				} else if (count == 2) {
					attendanceInfo.setType(StringUtils.isNotBlank(type) ? type : workAttendanceRule.getType());
					if (StringUtils.isBlank(id2)) {
						attendanceInfo.setId(IdGen.uuid());
						attendanceInfo.setCreateDate(dateTime);
						dao.insert(attendanceInfo);
					} else {
						attendanceInfo.setId(id2);
						attendanceInfo.setUpdateDate(new Date());
						dao.update(attendanceInfo);
					}
				} else {
					attendanceInfo.setType(StringUtils.isNotBlank(type) ? type : workAttendanceRule.getType());
					attendanceInfo.setMorningAfternoon("1");
					if (StringUtils.isBlank(id1)) {
						attendanceInfo.setId(IdGen.uuid());
						attendanceInfo.setCreateDate(dateTime);
						dao.insert(attendanceInfo);
					} else {
						attendanceInfo.setId(id1);
						attendanceInfo.setUpdateDate(new Date());
						dao.update(attendanceInfo);
					}
					attendanceInfo.setMorningAfternoon("2");
					if (StringUtils.isBlank(id2)) {
						attendanceInfo.setId(IdGen.uuid());
						attendanceInfo.setCreateDate(dateTime);
						dao.insert(attendanceInfo);
					} else {
						attendanceInfo.setUpdateDate(new Date());
						attendanceInfo.setId(id2);
						dao.update(attendanceInfo);
					}
				}
			}
		}
	}

    @Transactional(readOnly = false)
    public List<String> saveInfoAudit(WorkAttendanceRule workAttendanceRule,String type,String staffId,User user,Date date,String time,String lnt,String lat,String wifi,
                         String type1,String type2,
                         String isOvertime,Integer dateType,Integer overtimeTime,String holidayType,
                         String isCalculateAttendance,String isCalculateSubsidies,String attendancePlace,
                         String lateEarly,Integer lateEarlyTime,String isValid,String reason,int count,String id1,String id2) {
		List<String> ids = new ArrayList<>();
		if (StringUtils.isNotBlank(staffId) && workAttendanceRule!=null && StringUtils.isNotBlank(workAttendanceRule.getId())){
			String isHolidays = "0";
			if (isHolidays(time) != 0) {
				isHolidays = "1";
			}
			WorkAttendanceInfo attendanceInfo = new WorkAttendanceInfo();
			attendanceInfo.setStaffId(staffId);
			attendanceInfo.setCompanyId(user.getCompany().getId());
			attendanceInfo.setOffice(user.getOffice());
			attendanceInfo.setAttendanceDate(date);
			attendanceInfo.setMorningAfternoon("1");
			attendanceInfo.setWorkAttendanceRule(workAttendanceRule);
			attendanceInfo.setLat(lat);
			attendanceInfo.setLnt(lnt);
			attendanceInfo.setIsOvertime(isOvertime);
			attendanceInfo.setWifi(wifi);
			attendanceInfo.setDateType(dateType + "");
			attendanceInfo.setOvertimeTime(overtimeTime);
			attendanceInfo.setHolidayType(holidayType);
			attendanceInfo.setIsCalculateAttendance(isCalculateAttendance);
			attendanceInfo.setIsCalculateSubsidies(isCalculateSubsidies);
			attendanceInfo.setAttendanceType(type1);
			attendanceInfo.setAttendancePlace(attendancePlace);
			attendanceInfo.setLateEarly(lateEarly);
			attendanceInfo.setLateEarlyTime(lateEarlyTime);
			attendanceInfo.setIsValid(isValid);
			attendanceInfo.setIsHolidays(isHolidays);
			attendanceInfo.setType(StringUtils.isNotBlank(type) ? type : workAttendanceRule.getType());
			attendanceInfo.setUserId(user.getId());
			Date dateTime = new Date();
			attendanceInfo.setUpdateDate(dateTime);
			attendanceInfo.setAttendanceTime(null);
			attendanceInfo.setStatus("0");
			attendanceInfo.setWid(id1);
			attendanceInfo.setRemarks(reason);
			if (count == 0) {
				attendanceInfo.setCreateDate(dateTime);
				attendanceInfo.setId(IdGen.uuid());
				attendanceInfo.setUpdateBy(new User("1"));
				attendanceInfo.setCreateBy(new User("1"));
				dao.insert(attendanceInfo);
				WorkAttendanceInfo workAttendanceInfo = new WorkAttendanceInfo();
				workAttendanceInfo.setStaffId(staffId);
				workAttendanceInfo.setCompanyId(user.getCompany().getId());
				workAttendanceInfo.setOffice(user.getOffice());
				workAttendanceInfo.setAttendanceDate(date);
				workAttendanceInfo.preInsert();
				workAttendanceInfo.setMorningAfternoon("2");
				workAttendanceInfo.setCreateBy(new User("1"));
				workAttendanceInfo.setUpdateBy(new User("1"));
				workAttendanceInfo.setType(StringUtils.isNotBlank(type) ? type : workAttendanceRule.getType());
				workAttendanceInfo.setIsHolidays(isHolidays);
				workAttendanceInfo.setUserId(user.getId());
				workAttendanceInfo.setDateType(dateType + "");
				workAttendanceInfo.setWorkAttendanceRule(workAttendanceRule);
				workAttendanceInfo.setRemarks(reason);
				workAttendanceInfo.setAttendanceTime(null);
				workAttendanceInfo.setAttendanceType(type2);
				workAttendanceInfo.setStatus("0");
				workAttendanceInfo.setWid(id2);
				workAttendanceAuditDao.insert(workAttendanceInfo);
				ids.add(workAttendanceInfo.getId());
			} else {
				attendanceInfo.setCreateBy(new User("1"));
				attendanceInfo.setUpdateBy(new User("1"));
				attendanceInfo.setMorningAfternoon(count + "");
				attendanceInfo.setIsHolidays(isHolidays);
				if (count == 1) {
					attendanceInfo.setType(StringUtils.isNotBlank(type) ? type : workAttendanceRule.getType());
					attendanceInfo.setCreateDate(dateTime);
					attendanceInfo.setId(IdGen.uuid());
					workAttendanceAuditDao.insert(attendanceInfo);
					ids.add(attendanceInfo.getId());
				} else if (count == 2) {
					attendanceInfo.setType(StringUtils.isNotBlank(type) ? type : workAttendanceRule.getType());
					attendanceInfo.setId(IdGen.uuid());
					attendanceInfo.setCreateDate(dateTime);
					attendanceInfo.setWid(id2);
					workAttendanceAuditDao.insert(attendanceInfo);
					ids.add(attendanceInfo.getId());
				} else {
					attendanceInfo.setType(StringUtils.isNotBlank(type) ? type : workAttendanceRule.getType());
					attendanceInfo.setMorningAfternoon("1");
					attendanceInfo.setId(IdGen.uuid());
					attendanceInfo.setCreateDate(dateTime);
					ids.add(attendanceInfo.getId());
					workAttendanceAuditDao.insert(attendanceInfo);
					attendanceInfo.setType(StringUtils.isNotBlank(type) ? type : workAttendanceRule.getType());
					attendanceInfo.setMorningAfternoon("2");
					attendanceInfo.setId(IdGen.uuid());
					attendanceInfo.setCreateDate(dateTime);
					attendanceInfo.setWid(id2);
					workAttendanceAuditDao.insert(attendanceInfo);
					ids.add(attendanceInfo.getId());
				}
			}
		}
        return ids;
    }

	@Transactional(readOnly = false)
	public void startTask() throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		SimpleDateFormat sdfDay = new SimpleDateFormat("yyyy-MM-dd");
		Date date = new Date();
		String day = sdfDay.format(date);
		String dayms = sdfs.format(date);
		if (dayms.equals("0120")){
			WorkStaffBasicInfo staffBasicInfo = new WorkStaffBasicInfo();
			List<WorkStaffBasicInfo> workStaffBasicInfos = workStaffAchivesDao.findInfoList(staffBasicInfo);
			if (workStaffBasicInfos != null && workStaffBasicInfos.size() != 0) {
				logger.info("当前考勤人数：" + workStaffBasicInfos.size());
				for (WorkStaffBasicInfo workStaffBasicInfo : workStaffBasicInfos) {
					String day1 = UserUtils.getSysParamByCompany("attendance_date",workStaffBasicInfo.getCompany().getId());
					String firstDay = StringUtils.firstDay("",day1,workStaffBasicInfo.getCompany().getId()).replace("-","");
					Boolean status = false;
					if (workStaffBasicInfo.getWorkEndDate()!=null){
						String end = sdf.format(workStaffBasicInfo.getWorkEndDate());
						if (Integer.parseInt(end)>=Integer.parseInt(firstDay)){
							status = true;
						}
					}else {
						status = true;
					}
					if (status) {
						//依据公司和规则编号查询规则
						WorkAttendanceRule oaRule = workAttendanceRuleService.findAttendanceRuleByCompany(workStaffBasicInfo.getCompany().getId(), workStaffBasicInfo.getUserId(), "");
						if (oaRule == null || StringUtils.isBlank(oaRule.getId())) {
							oaRule = workAttendanceRuleService.findAttendanceRuleByCompany(workStaffBasicInfo.getCompany().getId(), "", workStaffBasicInfo.getOffice().getId());
						}
						if (oaRule != null) {
							WorkAttendanceInfo attendanceInfo = new WorkAttendanceInfo();
							attendanceInfo.setStaffId(workStaffBasicInfo.getId());
							attendanceInfo.setAttendanceDate(date);
							List<WorkAttendanceInfo> workAttendanceInfos = this.findList(attendanceInfo);
							WorkAttendanceInfo workAttendanceInfo = new WorkAttendanceInfo();
							workAttendanceInfo.setStaffId(workStaffBasicInfo.getId());
							workAttendanceInfo.setCompanyId(workStaffBasicInfo.getCompany().getId());
							workAttendanceInfo.setOffice(workStaffBasicInfo.getOffice());
							workAttendanceInfo.setAttendanceDate(date);
							workAttendanceInfo.setAttendanceType("0");
							workAttendanceInfo.setType(oaRule.getType());
							workAttendanceInfo.setMorningAfternoon("1");
							workAttendanceInfo.setWorkAttendanceRule(oaRule);
							workAttendanceInfo.setCreateDate(date);
							workAttendanceInfo.setId(IdGen.uuid());
							workAttendanceInfo.setUpdateDate(date);
							workAttendanceInfo.setCreateBy(new User("1"));
							workAttendanceInfo.setUpdateBy(new User("1"));
							workAttendanceInfo.setDelFlag("0");
							workAttendanceInfo.setUserId(workStaffBasicInfo.getUserId());
							if (isHolidays(day) == 0) {
								workAttendanceInfo.setIsHolidays("0");
							} else {
								workAttendanceInfo.setIsHolidays("1");
							}
							if (workAttendanceInfos == null || workAttendanceInfos.size() == 0) {
								dao.insert(workAttendanceInfo);
								workAttendanceInfo.setMorningAfternoon("2");
								workAttendanceInfo.setId(IdGen.uuid());
								dao.insert(workAttendanceInfo);
							} else if (workAttendanceInfos.size() == 1) {
								String morningAfternoon = workAttendanceInfos.get(0).getMorningAfternoon();
								if ("1".equals(morningAfternoon)) {
									workAttendanceInfo.setMorningAfternoon("2");
									dao.insert(workAttendanceInfo);
								} else {
									workAttendanceInfo.setMorningAfternoon("1");
									dao.insert(workAttendanceInfo);
								}
							}
						}
					}
				}
			}
		}
	}

	@Transactional(readOnly = false)
	public void startMonthTask() throws ParseException {
	    Office office = new Office();
	    office.setType("1");
	    List<Office> companyList = officeDao.findListByCompanyName(office);
        Calendar now = Calendar.getInstance();
		Date date = new Date();
		String dayms = sdfs.format(date);
		if (dayms.equals("0140")) {
			int nowDay = now.get(Calendar.DAY_OF_MONTH);
			for (Office company : companyList) {
				String day = UserUtils.getSysParamByCompany("attendance_date", company.getId());
				int count = 0;
				if (StringUtils.isBlank(day)) {
					count = Integer.parseInt(StringUtils.lastDay("", "", company.getId()).substring(8, 10));
				} else {
					count = Integer.parseInt(day);
				}
				if (nowDay == count) {
					String firstDay = StringUtils.firstDay("", day, company.getId());
					String lastDay = StringUtils.lastDay("", day, company.getId());
					WorkAttendanceInfo workAttendanceInfo = new WorkAttendanceInfo();
					try {
						workAttendanceInfo.setStartDate(format.parse(firstDay));
						workAttendanceInfo.setEndDate(format.parse(lastDay));
						workAttendanceInfo.setCompanyId(company.getId());
					} catch (ParseException e) {
						logger.info("ParseException e" + e);
					}
					List<WorkAttendanceMonth> WorkAttendanceMonths = dao.findListByTime(workAttendanceInfo);
					String month = "";
					if (nowDay >= 15) {
						month = lastDay.substring(0, 7);
					} else {
						month = firstDay.substring(0, 7);
					}
					for (WorkAttendanceMonth workAttendanceMonth : WorkAttendanceMonths) {
						workAttendanceMonth.setAttendanceMonth(month);
						workAttendanceMonth.preInsert();
						workAttendanceMonth.setCreateBy(new User("1"));
						workAttendanceMonth.setUpdateBy(new User("1"));
						workAttendanceMonthDao.insert(workAttendanceMonth);
					}
				}
			}
		}

		/*SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
		SimpleDateFormat sdfMonth = new SimpleDateFormat("yyyyMM");
		SimpleDateFormat sdfDay = new SimpleDateFormat("yyyyMMdd");
		Date date = new Date();
		String year = sdf.format(date);
		AjaxJson ajaxJson = new AjaxJson();
		workCalendarService.queryHoliday(year, ajaxJson);
		Map<String, String> map = ajaxJson.getBody();
		String holidays = map.get("holidays");
		String workdays = map.get("workdays");
		int first = Integer.parseInt(firstDay.substring(8,10));
		int last = Integer.parseInt(lastDay.substring(8,10));
		String yearMonth = firstDay.substring(0,8);
		String yearMonth2 = lastDay.substring(0,8);
		int count = 0;
		//获取上月最后一天
		Calendar ca = Calendar.getInstance();
		ca.set(Calendar.DAY_OF_MONTH,0);
		String lastd = format.format(ca.getTime());
		int lastint = Integer.parseInt(lastd.substring(8,10));
		if (yearMonth.equals(yearMonth2)) {
			for (int i = first; i <= last; i++) {
				String day = yearMonth + i;
				if (i < 10) {
					day = yearMonth + "0" + i;
				}
				if (holidays.contains(day)) {
					continue;
				} else if (workdays.contains(day)) {
					count++;
				} else if (!isWeek(day)) {
					count++;
				}
			}
		}else {
			for (int i = first; i <= lastint; i++) {
				String day = yearMonth + i;
				if (i < 10) {
					day = yearMonth + "0" + i;
				}
				if (holidays.contains(day)) {
					continue;
				} else if (workdays.contains(day)) {
					count++;
				} else if (!isWeek(day)) {
					count++;
				}
			}
			for (int i = 1; i <= last; i++) {
				String day = yearMonth2 + i;
				if (i < 10) {
					day = yearMonth2 + "0" + i;
				}
				if (holidays.contains(day)) {
					continue;
				} else if (workdays.contains(day)) {
					count++;
				} else if (!isWeek(day)) {
					count++;
				}
			}
		}*/
		//Integer firstDate = Integer.parseInt(sdfDay.format(firstDay));
		//Integer lastDate = Integer.parseInt(sdfDay.format(lastDay));
		/*String month = lastDay.substring(0,7);;
		for (WorkAttendanceMonth workAttendanceMonth:WorkAttendanceMonths){
			Integer answerAttendanceDay = workAttendanceMonth.getAnswerAttendanceDay();
			int count1 = 0;
			if(workAttendanceMonth.getEntryDate()!=null && workAttendanceMonth.getDimissionDate()!=null) {
				String entry = format.format(workAttendanceMonth.getEntryDate());
				String dimission = format.format(workAttendanceMonth.getDimissionDate());
				Integer entryDate = Integer.parseInt(sdfDay.format(workAttendanceMonth.getEntryDate()));
				Integer dimissionDate = Integer.parseInt(sdfDay.format(workAttendanceMonth.getDimissionDate()));
				if (firstDate >= entryDate) {
					first = Integer.parseInt(entry.substring(8,10));
					yearMonth = entry.substring(0,8);
					if(lastDate >= dimissionDate){
						last = Integer.parseInt(dimission.substring(8,10));
						yearMonth2 = dimission.substring(0,8);
					}
				}else if(lastDate >= dimissionDate){
					last = Integer.parseInt(dimission.substring(8,10));
					yearMonth2 = dimission.substring(0,8);
				}
			}else {
				if (workAttendanceMonth.getEntryDate() != null) {
					String entry = format.format(workAttendanceMonth.getEntryDate());
					Integer entryDate = Integer.parseInt(sdfDay.format(workAttendanceMonth.getEntryDate()));
					if(firstDate >= entryDate) {
						first = Integer.parseInt(entry.substring(8, 10));
						yearMonth = entry.substring(0, 8);
					}
				} else if (workAttendanceMonth.getDimissionDate() != null) {
					String dimission = format.format(workAttendanceMonth.getDimissionDate());
					Integer dimissionDate = Integer.parseInt(sdfDay.format(workAttendanceMonth.getDimissionDate()));
					if (lastDate >= dimissionDate) {
						last = Integer.parseInt(dimission.substring(8,10));
						yearMonth2 = dimission.substring(0,8);
					}
				}
			}
			if (yearMonth.equals(yearMonth2)) {
				for (int i = first; i <= last; i++) {
					String day = yearMonth + i;
					if (i < 10) {
						day = yearMonth + "0" + i;
					}
					if (holidays.contains(day)) {
						continue;
					} else if (workdays.contains(day)) {
						count1++;
					} else if (!isWeek(day)) {
						count1++;
					}
				}
			}else {
				for (int i = first; i <= lastint; i++) {
					String day = yearMonth + i;
					if (i < 10) {
						day = yearMonth + "0" + i;
					}
					if (holidays.contains(day)) {
						continue;
					} else if (workdays.contains(day)) {
						count1++;
					} else if (!isWeek(day)) {
						count1++;
					}
				}
				for (int i = 1; i <= last; i++) {
					String day = yearMonth2 + i;
					if (i < 10) {
						day = yearMonth2 + "0" + i;
					}
					if (holidays.contains(day)) {
						continue;
					} else if (workdays.contains(day)) {
						count1++;
					} else if (!isWeek(day)) {
						count1++;
					}
				}
			}
			workAttendanceMonth.setAnswerAttendanceDay(count1==0?count:count1);
			workAttendanceMonth.setAttendanceMonth(month);
			workAttendanceMonth.preInsert();
			workAttendanceMonthDao.insert(workAttendanceMonth);
		}*/
	}
	@Transactional(readOnly = false)
	public void startMonthCountTask() throws ParseException {
        Office office = new Office();
        office.setType("1");
        List<Office> companyList = officeDao.findListByCompanyName(office);
        Calendar now = Calendar.getInstance();
        int nowDay = now.get(Calendar.DAY_OF_MONTH);
		Date date = new Date();
		String dayms = sdfs.format(date);
		if (dayms.equals("0150")) {
			for (Office company : companyList) {
				String day = UserUtils.getSysParamByCompany("attendance_date", company.getId());
				int count = 0;
				if (StringUtils.isBlank(day)) {
					count = Integer.parseInt(StringUtils.lastDay("", "", company.getId()).substring(8, 10));
				} else {
					count = Integer.parseInt(day);
				}
				if (nowDay == count) {
					String firstDay = StringUtils.firstDay("", day, company.getId());
					String lastDay = StringUtils.lastDay("", day, company.getId());
					WorkStaffBasicInfo staffBasicInfo = new WorkStaffBasicInfo();
					Date lastDate = format.parse(lastDay);
					String month = "";
					if (nowDay >= 15) {
						month = lastDay.substring(0, 7);
					} else {
						month = firstDay.substring(0, 7);
					}
					staffBasicInfo.setWorkEndDate(format.parse(firstDay));
					staffBasicInfo.setCompany(company);
					List<WorkAttendanceCount> workAttendanceCounts = workAttendanceCountService.groupByOfficeList(staffBasicInfo);
					for (WorkAttendanceCount workAttendanceCount : workAttendanceCounts) {
						Integer attendanceCount = groupByOfficeStaffId(month, workAttendanceCount.getOffice().getId());
						workAttendanceCount.setAttendanceCount(attendanceCount);
						workAttendanceCount.setAttendanceEndDate(lastDate);
						workAttendanceCount.setAttendanceMonth(month);
						workAttendanceCount.setStatus("1");
						workAttendanceCount.setCreateBy(new User("1"));
						workAttendanceCount.setUpdateBy(new User("1"));
						workAttendanceCountService.save(workAttendanceCount);
					}
				}
			}
		}

	}
	@Transactional(readOnly = false)
	public void startMonthNotifyTask() throws ParseException {
		Office com = new Office();
		com.setType("1");
		List<Office> companyList = officeDao.findListByCompanyName(com);
		Calendar now = Calendar.getInstance();
		int nowDay = now.get(Calendar.DAY_OF_MONTH);
		Date date = new Date();
		String dayms = sdfs.format(date);
		if (dayms.equals("0220")) {
			for (Office company : companyList) {
				String day = UserUtils.getSysParamByCompany("attendance_date", company.getId());
				String dayCount = UserUtils.getSysParamByCompany("attendance_count", company.getId());
				int dayCInt = StringUtils.isBlank(dayCount) ? 1 : Integer.parseInt(dayCount);
				if (nowDay == dayCInt) {
					int count1 = 0;
					int count2 = 0;
					String month = "";
					if (StringUtils.isBlank(day)) {
						count1 = Integer.parseInt(StringUtils.lastDay("", "", company.getId()).substring(8, 10));
					} else {
						count1 = Integer.parseInt(day);
					}
					if (StringUtils.isBlank(dayCount)) {
						count2 = 1;
					} else {
						count2 = Integer.parseInt(dayCount);
					}
					if (count1 >= count2 && nowDay == count2) {
						month = StringUtils.firstDay("", count1 + "", company.getId()).substring(0, 7);
					} else if (count1 < count2 && nowDay == count2) {
						month = StringUtils.lastDay("", count1 + "", company.getId()).substring(0, 7);
					}
					WorkAttendanceCount workAttendanceCount = new WorkAttendanceCount();
					workAttendanceCount.setCompanyId(company.getId());
					workAttendanceCount.setAttendanceMonth(month);
					List<WorkAttendanceCount> workAttendanceCounts = workAttendanceCountService.findListByMonth(workAttendanceCount);
					if (workAttendanceCounts != null && workAttendanceCounts.size() != 0) {
						for (WorkAttendanceCount attendanceCount : workAttendanceCounts) {
							Office office1 = attendanceCount.getOffice();
							List<User> bmzrs = UserUtils.getByRoleActivityEnname("bmzr", 2, office1.getId(), attendanceCount.getCompanyId(), "27");
							String title = "考勤审批月份:" + attendanceCount.getAttendanceMonth();
							String str = "考勤审批月份:" + attendanceCount.getAttendanceMonth() + ",考勤截止日期:" + format.format(attendanceCount.getAttendanceEndDate());
							WorkProjectNotify workProjectNotify = UtilNotify
									.saveNotify(attendanceCount.getId(),
											null,
											attendanceCount.getCompanyId(),
											title,
											str,
											"84",
											"0",
											"待审批",
											""
									);
							if (bmzrs != null && bmzrs.size() != 0) {
								for (User u : bmzrs) {
									workProjectNotify.setUser(u);
									workProjectNotify.setNotifyRole("部门负责人审批");
									workProjectNotify.setOfficeId(office1.getId());
									workProjectNotify.preInsert();
									workProjectNotify.setId(IdGen.uuid());
									workProjectNotify.setCreateBy(new User("1"));
									workProjectNotify.setUpdateBy(new User("1"));
									workProjectNotifyService
											.insert(workProjectNotify);
							/*Map<String,Object> extras = new HashMap<>();
							extras.put("type","7002");
							extras.put("id",workProjectNotify.getId());
							extras.put("procDefKey","84");
							UserUtils.pushInfoToApp(title,str,extras,u.getId());*/
									UserUtils.pushIm(u.getId(), str);
								}
							}
						}
					}
				}
			}
		}
	}

	public int isHolidays(String day) {
		String year = day.substring(0,4);
		AjaxJson ajaxJson = new AjaxJson();
		workCalendarService.queryHoliday(year, ajaxJson);
		Map<String, String> map = ajaxJson.getBody();
		String holidays = map.get("holidays");
		String workdays = map.get("workdays");
		String days = day.replace("-","");
		if (holidays.contains(days)){
			return 1;
		}else if (workdays.contains(days)){
			return 0;
		}else if(!StringUtils.isWeek(day)){
			return 0;
		}else {
			return 2;
		}
	}

}

