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

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.utils.DateUtils;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.iim.utils.DateUtil;
import com.jeeplus.modules.oa.dao.LeaveDao;
import com.jeeplus.modules.oa.entity.Leave;
import com.jeeplus.modules.oa.entity.OaAttendance;
import com.jeeplus.modules.oa.service.LeaveService;
import com.jeeplus.modules.oa.service.OaAttendanceService;
import com.jeeplus.modules.oa_evection.entity.oa_evection.OaEvection;
import com.jeeplus.modules.oa_evection.service.oa_evection.OaEvectionService;
import com.jeeplus.modules.sys.entity.User;
import com.jeeplus.modules.sys.utils.UserUtils;
import com.jeeplus.modules.work.entity.report.WorkReport;
import com.jeeplus.modules.work.service.report.WorkReportService;
import com.jeeplus.modules.workcalendar.entity.CalendarCountEntity;
import com.jeeplus.modules.workdailyroutine.entity.WorkDailyRoutine;
import com.jeeplus.modules.workdailyroutine.entity.WorkDailyRoutineDetail;
import com.jeeplus.modules.workdailyroutine.service.WorkDailyRoutineService;
import com.jeeplus.modules.workgoout.entity.GoOut;
import com.jeeplus.modules.workgoout.service.GoOutService;
import com.jeeplus.modules.workoutsignin.entity.WorkOutSignIn;
import com.jeeplus.modules.workoutsignin.service.WorkOutSignInService;
import com.jeeplus.modules.workovertimeform.entity.WorkOvertimeForm;
import com.jeeplus.modules.workovertimeform.service.WorkOvertimeFormService;
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.worksealform.entity.WorkSealForm;
import com.jeeplus.modules.worksealform.service.WorkSealFormService;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
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.workcalendar.entity.WorkCalendar;
import com.jeeplus.modules.workcalendar.dao.WorkCalendarDao;

/**
 * 工作日历Service
 * @author ssrh
 * @version 2018-07-17
 */
@Service
@Transactional(readOnly = true)
public class WorkCalendarService extends CrudService<WorkCalendarDao, WorkCalendar> {
    @Autowired
    private WorkDailyRoutineService workDailyRoutineService;
    @Autowired
    private WorkProjectNotifyService workProjectNotifyService;
    @Autowired
    private WorkReportService workReportService;
    @Autowired
    private WorkOutSignInService workOutSignInService;
    @Autowired
    private OaAttendanceService oaAttendanceService;
    @Autowired
    private com.jeeplus.modules.oa.dao.LeaveDao LeaveDao;
    @Autowired
    private LeaveService leaveService;
    @Autowired
    private WorkSealFormService workSealFormService;
    @Autowired
    private OaEvectionService oaEvectionService;
    @Autowired
    private WorkOvertimeFormService workOvertimeFormService;
    @Autowired
    private GoOutService goOutService;
	public WorkCalendar get(String id) {
		return super.get(id);
	}
	
	public List<WorkCalendar> findList(WorkCalendar workCalendar) {
        List<WorkCalendar> list = super.findList(workCalendar);
        if(list==null||list.size()<=0){
            return new ArrayList<>();
        }
        Date startDate = workCalendar.getStartDate();
        Iterator<WorkCalendar> iterator = list.iterator();
        while (iterator.hasNext()){
            WorkCalendar wCalendar = iterator.next();
            if("0".equals(wCalendar.getRepeatType())&&wCalendar.getStartDate().equals(startDate)){
                continue;
            }
            if("1".equals(wCalendar.getRepeatType())){
                String week = DateUtils.getWeek(startDate);
                week = week.replace("星期","周");
                if(wCalendar.getRepeatDate()!=null&&wCalendar.getRepeatDate().contains(week)){
                    continue;
                }
            }
            if("2".equals(wCalendar.getRepeatType())){
                String day = DateUtils.getDay(startDate);
                if(wCalendar.getRepeatDate()!=null&&wCalendar.getRepeatDate().contains(day)){
                    continue;
                }
            }
            iterator.remove();
        }
        return list;
	}
	
	public Page<WorkCalendar> findPage(Page<WorkCalendar> page, WorkCalendar workCalendar) {
		return super.findPage(page, workCalendar);
	}

	public void queryDetails(WorkCalendar workCalendar){
	    if(workCalendar!=null&&StringUtils.isNotBlank(workCalendar.getId())){
	        workCalendar.setUserList(dao.queryUserList(workCalendar.getId()));
        }
    }
	
	@Transactional(readOnly = false)
	public void save(WorkCalendar workCalendar) {
	    if(workCalendar.getOffice()==null){
	        workCalendar.setOffice(UserUtils.getSelectOffice());
        }
        if(workCalendar.getCompany()==null){
	        workCalendar.setCompany(UserUtils.getSelectCompany());
        }
		super.save(workCalendar);
	    this.saveShareUsers(workCalendar);
	    if(!"1".equals(workCalendar.getSendDaily())){
	        return;
        }
        if("0".equals(workCalendar.getRepeatType())&&!"1".equals(workCalendar.getRemind())){
	        this.createDailyRoutine(workCalendar);
        }
	}

	@Transactional(readOnly = false)
    public void createDailyRoutine(WorkCalendar workCalendar) {
        logger.info("开始发送日常事务，日历主题："+workCalendar.getSubject());
        WorkDailyRoutine workDailyRoutine = new WorkDailyRoutine();
        workDailyRoutine.setName(workCalendar.getSubject());
        workDailyRoutine.setSource("日历发送");
        workDailyRoutine.setStatus("未完成");
        workDailyRoutine.setCreateBy(UserUtils.getUser());
        workDailyRoutine.setCreateDate(new Date());
        workDailyRoutine.setRemarks(workCalendar.getContent());
        List<WorkDailyRoutineDetail> detailList = new ArrayList<>();
        List<User> userList = workCalendar.getUserList();
        WorkDailyRoutineDetail routineDetail = null;
        boolean flag = false;
        for (User user : userList) {
            routineDetail = new WorkDailyRoutineDetail();
            routineDetail.setOperator(user);
            detailList.add(routineDetail);
            if(workCalendar.getCreateBy().getId().equals(user.getId())){
                flag=true;
            }
        }
        if(!flag){
            routineDetail = new WorkDailyRoutineDetail();
            routineDetail.setOperator(workCalendar.getCreateBy());
            detailList.add(routineDetail);
        }
        workDailyRoutine.setDetailList(detailList);
        workDailyRoutine.setSourceId(workCalendar.getId());
        workDailyRoutineService.save(workDailyRoutine);
        logger.info("发送日常事务完成，日历主题："+workCalendar.getSubject());
    }

    private void saveShareUsers(WorkCalendar workCalendar) {
        if(workCalendar==null||workCalendar.getUserList()==null){
            return;
        }
        List<User> userList = workCalendar.getUserList();
        dao.deleteUsers(workCalendar.getId());
        Iterator<User> iterator = userList.iterator();
        while (iterator.hasNext()){
            User next = iterator.next();
            if (next==null|| StringUtils.isBlank(next.getId())){
                iterator.remove();
            }
        }
        if(userList ==null|| userList.size()<=0){
            return;
        }
        workCalendar.setUserList(userList);
        HashMap<String, Object> map = new HashMap<>(5);
        map.put("calId", workCalendar.getId());
        map.put("userList", userList);
        dao.batchInsert(map);
    }

    @Transactional(readOnly = false)
	public void delete(WorkCalendar workCalendar) {
        dao.deleteUsers(workCalendar.getId());
		super.delete(workCalendar);
        List<WorkDailyRoutine> dailyRoutineList = workDailyRoutineService.findBySourceId(workCalendar.getId());
        if(dailyRoutineList!=null&&dailyRoutineList.size()>0){
            for (WorkDailyRoutine workDailyRoutine : dailyRoutineList) {
                workDailyRoutineService.close(workDailyRoutine.getId());
            }
        }
    }


    public List<WorkCalendar> findTaskList(WorkCalendar workCalendar) {
        return dao.findTaskList(workCalendar);
    }

    @Transactional(readOnly = false)
    public void notifyUsers(WorkCalendar workCalendar) {
        logger.info("开始发送日历提醒，日历主题："+workCalendar.getSubject());
        String contentStr = "日历信息 创建人：" + workCalendar.getCreateBy().getName() + "，日历主题：" + workCalendar.getSubject() + "待处理";
        String titleStr = "创建人：" + workCalendar.getCreateBy().getName() + "，日历主题：" + workCalendar.getSubject() + "待处理";
        String remark = "待通知";
        WorkProjectNotify workProjectNotify = UtilNotify.saveNotify(workCalendar.getId(), null, workCalendar.getCompany().getId(),
                titleStr, contentStr, "65", "0", remark, "");
        List<User> userList = workCalendar.getUserList();
        List<String> userIds = new ArrayList<>(userList.size());
        for (User u : userList) {
            userIds.add(u.getId());
        }
        for (User user : userList) {
            workProjectNotify.setUser(user);
            workProjectNotify.setId("");
            workProjectNotify.setNotifyRole("");
            workProjectNotifyService.save(workProjectNotify);
        }
        UserUtils.pushIm(userIds, contentStr);
        logger.info("发送日历提醒完成，日历主题："+workCalendar.getSubject());
    }

    public List<CalendarCountEntity> findCountList(WorkCalendar workCalendar) {
	    Calendar calendar = Calendar.getInstance();
	    calendar.setTime(workCalendar.getStartDate());
	    calendar.add(Calendar.MONTH,1);
	    calendar.add(Calendar.DAY_OF_MONTH,-1);
	    workCalendar.setEndDate(calendar.getTime());
	    calendar.setTime(new Date());
	    calendar.set(Calendar.MILLISECOND,0);
	    calendar.set(Calendar.SECOND,0);
	    calendar.set(Calendar.MINUTE,0);
	    calendar.set(Calendar.HOUR_OF_DAY,0);
        Date today = calendar.getTime();
        Date time=workCalendar.getStartDate();
        List<CalendarCountEntity> reportList=this.getManageCalendarByMonth(time);
        reportList.addAll(this.getManageCalendarByMonthAll(time));
        if(workCalendar.getEndDate().before(today)){
            return  reportList;
        }
        if(workCalendar.getStartDate().before(today)){
            workCalendar.setStartDate(today);
        }
        List<WorkCalendar> list = dao.findList(workCalendar);
        if(list==null||list.size()<=0){
            return reportList;
        }
        Date startDate = workCalendar.getStartDate();

        ArrayList<CalendarCountEntity> countList = new ArrayList<>();
        while (startDate.getTime()<=workCalendar.getEndDate().getTime()){
            CalendarCountEntity countEntity = new CalendarCountEntity();
            int count = 0;
            for (WorkCalendar wCalendar : list) {
                //计算每天工作日历总数
                if (wCalendar.getStartDate().after(startDate)){
                    continue;
                }
                if(wCalendar.getEndDate()!=null && wCalendar.getEndDate().before(startDate)){
                    continue;
                }
                if("0".equals(wCalendar.getRepeatType())&&wCalendar.getStartDate().equals(startDate)){
                    count++;
                    continue;
                }
                if("1".equals(wCalendar.getRepeatType())){
                    String week = DateUtils.getWeek(startDate);
                    week = week.replace("星期","周");
                    if(wCalendar.getRepeatDate()!=null&&wCalendar.getRepeatDate().contains(week)){
                        count++;
                        continue;
                    }
                }
                if("2".equals(wCalendar.getRepeatType())){
                    String day = DateUtils.getDay(startDate);
                    if(wCalendar.getRepeatDate()!=null&&wCalendar.getRepeatDate().contains(day)){
                        count++;
                        continue;
                    }
                }
            }
            if(count>0){
                countEntity.setCount(count);
                countEntity.setTitle("主题");
                countEntity.setStart(startDate);
                countEntity.setEnd(startDate);
                countList.add(countEntity);
            }
            startDate = DateUtils.addDays(startDate,1);
        }
        reportList.addAll(countList);
        //countList.addAll(reportList);
        return countList;
    }

 /*   public  List<CalendarCountEntity> findReportCount( Date time){
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM");
        String dateString = formatter.format(time);
        Map<String, Object> map=new HashMap();
        map.put("userId",UserUtils.getUser().getId());
        map.put("time",dateString);
        map.put("sqlStr",dataScopeFilter(UserUtils.getUser(), "o", "u","s"));
        List<CalendarCountEntity> reportCountList= dao.findReportCount(map);
        return reportCountList;
    }*/

    public List<CalendarCountEntity> getManageCalendarByMonth( Date time){
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM");
        String dateString = formatter.format(time);
        List workReportDateList = workReportService.getCreateDateList1(dateString);
        List workOutSignInDateList = workOutSignInService.getCreateDateList1(dateString);
        List oaAttendanceDateList = oaAttendanceService.getCreateDateList1(dateString);
        List workSealFormDateList = workSealFormService.getCreateDateList1(dateString);
        List leaveDateList = leaveService.getCreateDateList1(dateString);
        List oaEvectionDateList = oaEvectionService.getCreateDateList1(dateString);
        List workOvertimeDateList = workOvertimeFormService.getCreateDateList1(dateString);
        List goOutDateList = goOutService.getCreateDateList1(dateString);

        Set<String> set = new TreeSet<String>();
        for(int i=0;i<workOutSignInDateList.size();i++){
            String str = workOutSignInDateList.get(i).toString().substring(0,10);
            set.add(str);
        }
        for(int i=0;i<workReportDateList.size();i++){
            String str = workReportDateList.get(i).toString().substring(0,10);
            set.add(str);
        }
        for(int i=0;i<oaAttendanceDateList.size();i++){
            String str = oaAttendanceDateList.get(i).toString().substring(0,10);
            set.add(str);
        }
        for(int i=0;i<workSealFormDateList.size();i++){
            String str = workSealFormDateList.get(i).toString().substring(0,10);
            set.add(str);
        }
        for(int i=0;i<leaveDateList.size();i++){
            String str = leaveDateList.get(i).toString().substring(0,10);
            set.add(str);
        }
        for(int i=0;i<oaEvectionDateList.size();i++){
            String str = oaEvectionDateList.get(i).toString().substring(0,10);
            set.add(str);
        }
        for(int i=0;i<workOvertimeDateList.size();i++){
            String str = workOvertimeDateList.get(i).toString().substring(0,10);
            set.add(str);
        }
        for(int i=0;i<goOutDateList.size();i++){
            String str = goOutDateList.get(i).toString().substring(0,10);
            set.add(str);
        }
        //有效日期
        List<String> dateList = new ArrayList<String>(set);
        Collections.sort(dateList, Collections.reverseOrder());

        List<CalendarCountEntity> thisMonthRecordList = new ArrayList();

        List<WorkOutSignIn> workOutSignInList = workOutSignInService.findByCompany1(dateString);
        List<WorkReport> workReportList = workReportService.findByCompany1(dateString);
        List<OaAttendance> oaAttendanceList = oaAttendanceService.findByCompany1(dateString);
        List<WorkSealForm> workSealFormList = workSealFormService.findByCompany1(dateString);
        List<Leave> leaveList = leaveService.findByCompany1(dateString);
        List<OaEvection> oaEvectionList = oaEvectionService.findByCompany1(dateString);
        List<WorkOvertimeForm> workOvertimeFormList = workOvertimeFormService.findByCompany1(dateString);
        List<GoOut> goOutList = goOutService.findByCompany1(dateString);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        for(int i=0;i<dateList.size();i++){
            Map<String,Object> map = new HashMap<String,Object>();
            int num = 0 ;
            for(WorkOutSignIn sign : workOutSignInList){
                String createDate = sdf.format(sign.getCreateDate()).substring(0,10);
                if(createDate.equals(dateList.get(i))){
                    num ++ ;
                }
            }
            for(WorkReport report : workReportList){
                String createDate = sdf.format(report.getCreateDate()).substring(0,10);
                if(createDate.equals(dateList.get(i))){
                    num ++ ;
                }
            }
            for(OaAttendance attendance : oaAttendanceList){
                String createDate = sdf.format(attendance.getCreateDate()).substring(0,10);
                if(createDate.equals(dateList.get(i))){
                    if(attendance.getStartTime()!=null && !"".equals(attendance.getStartTime())){
                        num ++ ;
                    }
                    if(attendance.getEndTime() != null && !"".equals(attendance.getEndTime())){
                        num ++;
                    }
                }
            }
            for(WorkSealForm seal : workSealFormList){
                String createDate = sdf.format(seal.getCreateDate()).substring(0,10);
                if(createDate.equals(dateList.get(i))){
                    num ++ ;
                }
            }
            for(Leave lev : leaveList){
                String createDate = sdf.format(lev.getCreateDate()).substring(0,10);
                if(createDate.equals(dateList.get(i))){
                    num ++ ;
                }
            }
            for(OaEvection evection : oaEvectionList){
                String createDate = sdf.format(evection.getCreateDate()).substring(0,10);
                if(createDate.equals(dateList.get(i))){
                    num ++ ;
                }
            }
            for(WorkOvertimeForm workOvertime : workOvertimeFormList){
                String createDate = sdf.format(workOvertime.getCreateDate()).substring(0,10);
                if(createDate.equals(dateList.get(i))){
                    num ++ ;
                }
            }
            for(GoOut out : goOutList){
                String createDate = sdf.format(out.getCreateDate()).substring(0,10);
                if(createDate.equals(dateList.get(i))){
                    num ++ ;
                }
            }
            CalendarCountEntity calendarCountEntity=new CalendarCountEntity();
            calendarCountEntity.setCount(num);
            calendarCountEntity.setTitle("我的工作");
            try {
                calendarCountEntity.setStart(sdf.parse(dateList.get(i)));
                calendarCountEntity.setEnd(sdf.parse(dateList.get(i)));
            } catch (ParseException e) {
                e.printStackTrace();
            }

            thisMonthRecordList.add(calendarCountEntity);
        }
        return  thisMonthRecordList;
    }

    public List<CalendarCountEntity> getManageCalendarByMonthAll( Date time){

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM");
        String dateString = formatter.format(time);

        //SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        List workReportDateList = workReportService.getCreateDateListAll(dateString);
        List workOutSignInDateList = workOutSignInService.getCreateDateListAll(dateString);
        List oaAttendanceDateList = oaAttendanceService.getCreateDateListAll(dateString);
        List workSealFormDateList = workSealFormService.getCreateDateListAll(dateString);
        List leaveDateList = leaveService.getCreateDateListAll(dateString);
        List oaEvectionDateList = oaEvectionService.getCreateDateListAll(dateString);
        List workOvertimeDateList = workOvertimeFormService.getCreateDateListAll(dateString);
        List goOutDateList = goOutService.getCreateDateListAll(dateString);

        Set<String> set = new TreeSet<String>();
        for(int i=0;i<workOutSignInDateList.size();i++){
            String str = workOutSignInDateList.get(i).toString().substring(0,10);
            set.add(str);
        }
        for(int i=0;i<workReportDateList.size();i++){
            String str = workReportDateList.get(i).toString().substring(0,10);
            set.add(str);
        }
        for(int i=0;i<oaAttendanceDateList.size();i++){
            String str = oaAttendanceDateList.get(i).toString().substring(0,10);
            set.add(str);
        }
        for(int i=0;i<workSealFormDateList.size();i++){
            String str = workSealFormDateList.get(i).toString().substring(0,10);
            set.add(str);
        }
        for(int i=0;i<leaveDateList.size();i++){
            String str = leaveDateList.get(i).toString().substring(0,10);
            set.add(str);
        }
        for(int i=0;i<oaEvectionDateList.size();i++){
            String str = oaEvectionDateList.get(i).toString().substring(0,10);
            set.add(str);
        }
        for(int i=0;i<workOvertimeDateList.size();i++){
            String str = workOvertimeDateList.get(i).toString().substring(0,10);
            set.add(str);
        }
        for(int i=0;i<goOutDateList.size();i++){
            String str = goOutDateList.get(i).toString().substring(0,10);
            set.add(str);
        }
        //有效日期
        List<String> dateList = new ArrayList<String>(set);
        Collections.sort(dateList, Collections.reverseOrder());

        List<CalendarCountEntity> thisMonthRecordList = new ArrayList();

        List<WorkOutSignIn> workOutSignInList = workOutSignInService.findByCompanyAll(dateString);
        List<WorkReport> workReportList = workReportService.findByCompanyAll(dateString);
        List<OaAttendance> oaAttendanceList = oaAttendanceService.findByCompanyAll(dateString);
        List<WorkSealForm> workSealFormList = workSealFormService.findByCompanyAll(dateString);
        List<Leave> leaveList = leaveService.findByCompanyAll(dateString);
        List<OaEvection> oaEvectionList = oaEvectionService.findByCompanyAll(dateString);
        List<WorkOvertimeForm> workOvertimeFormList = workOvertimeFormService.findByCompanyAll(dateString);
        List<GoOut> goOutList = goOutService.findByCompanyAll(dateString);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        for(int i=0;i<dateList.size();i++){
            Map<String,Object> map = new HashMap<String,Object>();
            int num = 0 ;
            for(WorkOutSignIn sign : workOutSignInList){
                String createDate = sdf.format(sign.getCreateDate()).substring(0,10);
                if(createDate.equals(dateList.get(i))){
                    num ++ ;
                }
            }
            for(WorkReport report : workReportList){
                String createDate = sdf.format(report.getCreateDate()).substring(0,10);
                if(createDate.equals(dateList.get(i))){
                    num ++ ;
                }
            }
            for(OaAttendance attendance : oaAttendanceList){
                String createDate = sdf.format(attendance.getCreateDate()).substring(0,10);
                if(createDate.equals(dateList.get(i))){
                    if(attendance.getStartTime()!=null && !"".equals(attendance.getStartTime())){
                        num ++ ;
                    }
                    if(attendance.getEndTime() != null && !"".equals(attendance.getEndTime())){
                        num ++;
                    }
                }
            }
            for(WorkSealForm seal : workSealFormList){
                String createDate = sdf.format(seal.getCreateDate()).substring(0,10);
                if(createDate.equals(dateList.get(i))){
                    num ++ ;
                }
            }
            for(Leave lev : leaveList){
                String createDate = sdf.format(lev.getCreateDate()).substring(0,10);
                if(createDate.equals(dateList.get(i))){
                    num ++ ;
                }
            }
            for(OaEvection evection : oaEvectionList){
                String createDate = sdf.format(evection.getCreateDate()).substring(0,10);
                if(createDate.equals(dateList.get(i))){
                    num ++ ;
                }
            }
            for(WorkOvertimeForm workOvertime : workOvertimeFormList){
                String createDate = sdf.format(workOvertime.getCreateDate()).substring(0,10);
                if(createDate.equals(dateList.get(i))){
                    num ++ ;
                }
            }
            for(GoOut out : goOutList){
                String createDate = sdf.format(out.getCreateDate()).substring(0,10);
                if(createDate.equals(dateList.get(i))){
                    num ++ ;
                }
            }
            CalendarCountEntity calendarCountEntity=new CalendarCountEntity();
            calendarCountEntity.setCount(num);
            calendarCountEntity.setTitle("我的管理");
            try {
                calendarCountEntity.setStart(sdf.parse(dateList.get(i)));
                calendarCountEntity.setEnd(sdf.parse(dateList.get(i)));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            thisMonthRecordList.add(calendarCountEntity);
        }
        return  thisMonthRecordList;
    }

    private static ConcurrentHashMap<String,String> holidayMap = new ConcurrentHashMap<>();
    private static ConcurrentHashMap<String,String> workdayMap = new ConcurrentHashMap<>();
    public void queryHoliday(String year, AjaxJson ajaxJson){
        ajaxJson.getBody().put("holidays","");
        ajaxJson.getBody().put("workdays","");
        if(Integer.valueOf(year)>Integer.valueOf(DateUtils.formatDate(new Date(),"yyyy"))){
            return ;
        }
        String holidayStr = holidayMap.get(year);
        String workdayStr = workdayMap.get(year);
        if(StringUtils.isBlank(holidayStr)){
            syncHolidays(year,year,true);
            holidayStr = holidayMap.get(year);
        }
        if(StringUtils.isBlank(workdayStr)){
            syncHolidays(year,this.createWorkStr(year),false);
            workdayStr = workdayMap.get(year);
        }
        ajaxJson.getBody().put("holidays",holidayStr);
        ajaxJson.getBody().put("workdays",workdayStr);
    }

    public void syncHolidays(String year,String param,boolean flag) {
        String holidayStr;HttpClient httpClient = new DefaultHttpClient();
        try {
            HttpGet httpGet = new HttpGet("http://tool.bitefu.net/jiari/?d="+param);
            HttpResponse response = httpClient.execute(httpGet);
            if (response.getStatusLine().getStatusCode() == 200) {
                HttpEntity resEntity = response.getEntity();
                String message = EntityUtils.toString(resEntity, "utf-8");
                JSONObject jsonObject = JSON.parseObject(message);
                StringBuilder builder = new StringBuilder();
                if(flag) {
                    JSONObject object = jsonObject.getJSONObject(year);
                    Set<Map.Entry<String, Object>> entrySet = object.entrySet();
                    for (Map.Entry<String, Object> objectEntry : entrySet) {
                        builder.append(year).append(objectEntry.getKey()).append(",");
                    }
                    if (builder.length() > 0) builder.deleteCharAt(builder.length() - 1);
                    holidayMap.put(year, builder.toString());
                }else {
                    Set<Map.Entry<String, Object>> entrySet = jsonObject.entrySet();
                    for (Map.Entry<String, Object> objectEntry : entrySet) {
                        if("0".equals(objectEntry.getValue().toString())){
                            builder.append(objectEntry.getKey()).append(",");
                        }
                    }
                    if (builder.length() > 0) builder.deleteCharAt(builder.length() - 1);
                    workdayMap.put(year,builder.toString());
                }
            } else {
                logger.info("请求法定节假日信息失败");
            }
        } catch (IOException e) {
            logger.error("请求法定节假日信息失败",e);
        }
    }

    private String createWorkStr(String year) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR,Integer.valueOf(year));
        calendar.set(Calendar.MONTH,0);
        calendar.set(Calendar.DAY_OF_MONTH,1);
        Date thisYear = calendar.getTime();
        calendar.add(Calendar.YEAR,1);
        Date nextYear = calendar.getTime();
        calendar.add(Calendar.YEAR,-1);
        StringBuilder builder = new StringBuilder();
        while (thisYear.before(nextYear)){
            if(thisYear.getDay()==0||thisYear.getDay()==6){
                builder.append(DateUtils.formatDate(thisYear,"yyyyMMdd")).append(",");
            }
            calendar.add(Calendar.DAY_OF_MONTH,1);
            thisYear=calendar.getTime();
        }
        if (builder.length() > 0) builder.deleteCharAt(builder.length()-1);
        return builder.toString();
    }

    public Page<WorkCalendar> findHomePage(Page<WorkCalendar> page, WorkCalendar workCalendar) {
        workCalendar.setPage(page);
        page.setList(dao.findHomeList(workCalendar));
        return page;
    }
}