package com.jeeplus.modules.oa.service;

import com.jeeplus.common.service.BaseService;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.common.utils.TimeUtils;
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_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.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.worksealform.entity.WorkSealForm;
import com.jeeplus.modules.worksealform.service.WorkSealFormService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by Meng on 2017/6/20.
 */
@Service
public class ManageCalendarService extends BaseService{

    @Autowired
    private WorkReportService workReportService;
    @Autowired
    private WorkOutSignInService workOutSignInService;
    @Autowired
    private OaAttendanceService oaAttendanceService;
    @Autowired
    private LeaveDao LeaveDao;
    @Autowired
    private LeaveService leaveService;
    @Autowired
    private WorkSealFormService workSealFormService;
    @Autowired
    private OaEvectionService oaEvectionService;
    @Autowired
    private WorkOvertimeFormService workOvertimeFormService;
    @Autowired
    private GoOutService goOutService;

    //优化版，供PC端使用
    public List<Map<String,Object>> getManageCalendarByMonth2(String companyId ,String date){

        WorkReport workReport = new WorkReport();
        WorkOutSignIn workOutSignIn = new WorkOutSignIn();
        OaAttendance oaAttendance = new OaAttendance();
        WorkSealForm workSealForm = new WorkSealForm();
        Leave leave = new Leave();
        OaEvection oaEvection = new OaEvection();
        WorkOvertimeForm workOvertimeForm = new WorkOvertimeForm();
        GoOut goOut = new GoOut();
        if (!UserUtils.getUser().isAdmin()){
            workReport.setCompanyId(companyId);
            workOutSignIn.setCompanyId(companyId);
            oaAttendance.setCompany(UserUtils.getSelectCompany());
            workSealForm.setCompanyId(companyId);
            leave.setCompanyId(companyId);
            oaEvection.setCompanyId(companyId);
            workOvertimeForm.setCompanyId(companyId);
            goOut.setCompanyId(companyId);
        }
        workReport.setDelFlag("0");
        workOutSignIn.setDelFlag("0");
        oaAttendance.setDelFlag("0");
        workSealForm.setDelFlag("0");
        leave.setDelFlag("0");
        oaEvection.setDelFlag("0");
        workOvertimeForm.setDelFlag("0");
        goOut.setDelFlag("0");
        List workReportDateList = workReportService.getCreateDateList(companyId,date);
        List workOutSignInDateList = workOutSignInService.getCreateDateList(companyId,date);
        List oaAttendanceDateList = oaAttendanceService.getCreateDateList(companyId,date);
        List workSealFormDateList = workSealFormService.getCreateDateList(companyId,date);
        List leaveDateList = leaveService.getCreateDateList(companyId,date);
        List oaEvectionDateList = oaEvectionService.getCreateDateList(companyId,date);
        List workOvertimeDateList = workOvertimeFormService.getCreateDateList(companyId,date);
        List goOutDateList = goOutService.getCreateDateList(companyId,date);

        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 thisMonthRecordList = new ArrayList();

        List<WorkOutSignIn> workOutSignInList = workOutSignInService.findByCompany2(workOutSignIn,date);
        List<WorkReport> workReportList = workReportService.findByCompany2(workReport,date);
        List<OaAttendance> oaAttendanceList = oaAttendanceService.findByCompany2(oaAttendance,date);
        List<WorkSealForm> workSealFormList = workSealFormService.findByCompany2(workSealForm,date);
        List<Leave> leaveList = leaveService.findByCompany2(leave,date);
        List<OaEvection> oaEvectionList = oaEvectionService.findByCompany2(oaEvection,date);
        List<WorkOvertimeForm> workOvertimeFormList = workOvertimeFormService.findByCompany2(workOvertimeForm,date);
        List<GoOut> goOutList = goOutService.findByCompany2(goOut,date);
        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 ++ ;
                }
            }
            map.put("date",dateList.get(i));  //指定月的某一天
            map.put("reportNum",String.valueOf(num));//当天的记录数

            thisMonthRecordList.add(map);
        }
        return  thisMonthRecordList;
    }
    //手机端使用
    public List getManageCalendarByMonth(String dateStr) {
        dateStr = dateStr + "-01";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date =null;
        try {
            date =sdf.parse(dateStr);
        }catch (ParseException e){

        }

        Calendar cal = Calendar.getInstance();
        cal.setTime(date);//month 为指定月份任意日期
        int year = cal.get(Calendar.YEAR);
        int m = cal.get(Calendar.MONTH) + 1;
        int dayNumOfMonth = TimeUtils.getDaysByYearMonth(year, m);
        cal.set(Calendar.DAY_OF_MONTH, 1);// 从一号开始
        List reportList =new ArrayList();
        for (int i = 0; i < dayNumOfMonth; i++, cal.add(Calendar.DATE, 1)) {
            Map calMap = new HashMap();
            Date d = cal.getTime();
            String dd = sdf.format(d);
            WorkReport workReport =new WorkReport();
            workReport.setCompanyId(UserUtils.getSelectCompany().getId());
            workReport.setCreateDate(d);
            List<WorkReport> workReportList = workReportService.findByCompany(workReport);
            calMap.put("date",dd);
            calMap.put("reportNum",workReportList.size());
            reportList.add(calMap);
        }
        return  reportList;
    }


    public Map<String,Object> getManageCalendarByDate(String dateStr) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date =null;
        try {
            date =sdf.parse(dateStr);
        }catch (ParseException e){

        }
        WorkReport workReport =new WorkReport();
        WorkOutSignIn workOutSignIn =new WorkOutSignIn();
        OaAttendance oaAttendance = new OaAttendance();
        Leave leave = new Leave();
        WorkSealForm workSealForm = new WorkSealForm();
        WorkOvertimeForm workOvertimeForm = new WorkOvertimeForm();
        OaEvection oaEvection = new OaEvection();
        GoOut goOut = new GoOut();
        if(! UserUtils.getUser().isAdmin()){
            workReport.setCompanyId(UserUtils.getSelectCompany().getId());
            workOutSignIn.setCompanyId(UserUtils.getSelectCompany().getId());
            leave.setCompanyId(UserUtils.getSelectCompany().getId());
            workSealForm.setCompanyId(UserUtils.getSelectCompany().getId());
            oaEvection.setCompanyId(UserUtils.getSelectCompany().getId());
            workOvertimeForm.setCompanyId(UserUtils.getSelectCompany().getId());
            goOut.setCompanyId(UserUtils.getSelectCompany().getId());
            oaAttendance.setCompany(UserUtils.getSelectCompany());
        }
        workReport.setCreateDate(date);
        workReport.setDelFlag("0");
        workOutSignIn.setCreateDate(date);
        workOutSignIn.setDelFlag("0");
        oaAttendance.setCreateDate(date);
        oaAttendance.setDelFlag("0");
        leave.setCreateDate(date);
        leave.setDelFlag("0");
        workSealForm.setCreateDate(date);
        workSealForm.setDelFlag("0");
        oaEvection.setCreateDate(date);
        oaEvection.setDelFlag("0");
        workOvertimeForm.setCreateDate(date);
        workOvertimeForm.setDelFlag("0");
        goOut.setCreateDate(date);
        goOut.setDelFlag("0");
        List<WorkReport> workReportList = workReportService.findByCompany(workReport);
        List<WorkOutSignIn> workOutList = workOutSignInService.findByCompany(workOutSignIn);
        List<OaAttendance> oaAttenList = oaAttendanceService.findByCompany(oaAttendance);
        List<WorkSealForm> workSealList = workSealFormService.findList(workSealForm);
        List<Leave> leaveList = LeaveDao.findByCompany(leave);
        List<OaEvection> oaEvectionList = oaEvectionService.findList(oaEvection);
        List<WorkOvertimeForm> workOvertimeFormList = workOvertimeFormService.findList(workOvertimeForm);
        List<GoOut> goOutList = goOutService.findList(goOut);

        Map<String,Object> map = new HashMap<String,Object>();
        map.put("workReportList",workReportList);
        map.put("workOutList",workOutList);
        map.put("oaAttenList",oaAttenList);
        map.put("workSealList",workSealList);
        map.put("leaveList",leaveList);
        map.put("oaEvectionList",oaEvectionList);
        map.put("workOvertimeFormList",workOvertimeFormList);
        map.put("goOutList",goOutList);
        return  map;
    }

    public List getReportUserList(String dateStr){
        Map<String,Object> maps = getManageCalendarByDate(dateStr);
        List reportList =new ArrayList();

        List<WorkReport> workReportList = (List<WorkReport>)maps.get("workReportList");
        List<WorkOutSignIn> workOutList = (List<WorkOutSignIn>)maps.get("workOutList");
        List<OaAttendance> oaAttenList = (List<OaAttendance>)maps.get("oaAttenList");
        List<WorkSealForm> workSealList = (List<WorkSealForm>)maps.get("workSealList");
        List<Leave> leaveList = (List<Leave>)maps.get("leaveList");
        List<OaEvection> oaEvectionList = (List<OaEvection>)maps.get("oaEvectionList");
        List<WorkOvertimeForm> workOvertimeFormList = (List<WorkOvertimeForm>)maps.get("workOvertimeFormList");
        List<GoOut> goOutList = (List<GoOut>)maps.get("goOutList");

        List<Map> mapList1 = new ArrayList<>();
        List<Map> mapList2 = new ArrayList<>();
        List<Map> mapList3 = new ArrayList<>();
        List<Map> mapList4 = new ArrayList<>();
        List<Map> mapList5 = new ArrayList<>();
        List<Map> mapList6 = new ArrayList<>();
        List<Map> mapList7 = new ArrayList<>();
        List<Map> mapList8 = new ArrayList<>();
        List<Map> mapList9 = new ArrayList<>();
        List<Map> mapList10 = new ArrayList<>();
        List<Map> mapList11 = new ArrayList<>();
        Map<String,Object> totalMap1 = new HashMap<String,Object>();
        Map<String,Object> totalMap2 = new HashMap<String,Object>();
        Map<String,Object> totalMap3 = new HashMap<String,Object>();
        Map<String,Object> totalMap4 = new HashMap<String,Object>();

        Map<String,Object> map3 = new HashMap<String,Object>();
        Map<String,Object> map4 = new HashMap<String,Object>();
        if(workReportList!=null && workReportList.size()!=0) {
            for (WorkReport w : workReportList) {
                Map map = new HashMap();
                User u = UserUtils.get(w.getCreateBy().getId());
                if (u!=null) {
                    map.put("userId", u.getId());
                    map.put("photo", u.getPhoto());
                    map.put("userName", u.getName());
                    map.put("createDate",w.getCreateDate());
                    map.put("updateDate",w.getUpdateDate());
                    map.put("workReportBean",w);
                    mapList1.add(map);
                }
            }
            map3.put("workReport",mapList1);
        }
        totalMap1.put("workReports",map3);
        if(workOutList!=null && workOutList.size()!=0) {
            for (WorkOutSignIn w : workOutList) {
                Map map = new HashMap();
                User u = UserUtils.get(w.getCreateBy().getId());
                if (u!=null) {
                    map.put("userId", u.getId());
                    map.put("photo", u.getPhoto());
                    map.put("userName", u.getName());
                    map.put("createDate",w.getCreateDate());
                    map.put("updateDate",w.getUpdateDate());
                    map.put("workOutSignInBean",w);
                    mapList2.add(map);
                }
            }
            map4.put("workOutSignIn", mapList2);
        }
        totalMap2.put("workOutSignIns",map4);

        Map<String,Object> map1 = new HashMap<String,Object>();
        if(oaAttenList!=null && oaAttenList.size()!=0) {
            SimpleDateFormat sdf3 = new SimpleDateFormat("HHmmss");
            for (OaAttendance o : oaAttenList) {
                Map map = new HashMap();
                User u = UserUtils.get(o.getCreateBy().getId());
                if (u!=null){
                    if (o.getStartTime() != null && !"".equals(o.getStartTime())) {
                        String ruleStart = o.getRule().getStartTime().replaceAll(":","")+"00";
                        Integer start = Integer.parseInt(sdf3.format(o.getStartTime()));
                        int morning = Integer.parseInt(ruleStart);
                        if (start > morning) {
                            map.put("userId", u.getId());
                            map.put("photo", u.getPhoto());
                            map.put("userName", u.getName());
                            map.put("createDate",o.getCreateDate());
                            map.put("updateDate",o.getUpdateDate());
                            map.put("oaAttendanceBean",o);
                            mapList3.add(map);
                        } else {
                                map.put("userId", u.getId());
                                map.put("photo", u.getPhoto());
                                map.put("userName", u.getName());
                                map.put("createDate",o.getCreateDate());
                                map.put("updateDate",o.getUpdateDate());
                                map.put("oaAttendanceBean",o);
                                mapList4.add(map);
                        }
                    }
                    if (o.getEndTime() != null && !"".equals(o.getEndTime())) {
                        String ruleEnd = o.getRule().getEndTime().replaceAll(":","")+"00";
                        Integer end = Integer.parseInt(sdf3.format(o.getEndTime()));
                        int night = Integer.parseInt(ruleEnd);
                        if (end >= night) {
                            map.put("userId", u.getId());
                            map.put("photo", u.getPhoto());
                            map.put("userName", u.getName());
                            map.put("createDate",o.getCreateDate());
                            map.put("updateDate",o.getUpdateDate());
                            map.put("oaAttendanceBean",o);
                            mapList5.add(map);
                        } else {
                                map.put("userId", u.getId());
                                map.put("photo", u.getPhoto());
                                map.put("userName", u.getName());
                                map.put("createDate",o.getCreateDate());
                                map.put("updateDate",o.getUpdateDate());
                                map.put("oaAttendanceBean",o);
                                mapList6.add(map);
                        }
                    }
                }
            }
            if(mapList3!=null && mapList3.size()!=0){ map1.put("late",mapList3); }

            if(mapList4!=null && mapList4.size()!=0){ map1.put("onDuty",mapList4);}
            if(mapList5!=null && mapList5.size()!=0){  map1.put("offDut",mapList5);}
            if(mapList6!=null && mapList6.size()!=0){ map1.put("leaveEarly",mapList6);}

        }
        totalMap3.put("attendances",map1);


        Map<String,Object> map2 = new HashMap<String,Object>();
        if(workSealList!=null && workSealList.size()!=0) {
            for (WorkSealForm w : workSealList) {
                Map map = new HashMap();
                User u = UserUtils.get(w.getCreateBy().getId());
                if (u!=null) {
                    map.put("userId", u.getId());
                    map.put("photo", u.getPhoto());
                    map.put("userName", u.getName());
                    map.put("createDate",w.getCreateDate());
                    map.put("updateDate",w.getUpdateDate());
                    map.put("workSealFormBean",w);
                    mapList7.add(map);
                }
            }
            map2.put("workSealFrom",mapList7);
        }
        if(leaveList!=null && leaveList.size()!=0) {
            for (Leave w : leaveList) {
                Map map = new HashMap();
                User u = UserUtils.get(w.getCreateBy().getId());
                if (u!=null) {
                    map.put("userId", u.getId());
                    map.put("photo", u.getPhoto());
                    map.put("userName", u.getName());
                    map.put("createDate",w.getCreateDate());
                    map.put("updateDate",w.getUpdateDate());
                    map.put("leaveBean",w);
                    mapList8.add(map);
                }
            }
            map2.put("leave",mapList8);
        }
        if(oaEvectionList!=null && oaEvectionList.size()!=0) {
            for (OaEvection w : oaEvectionList) {
                Map map = new HashMap();
                User u = UserUtils.get(w.getCreateBy().getId());
                if (u!=null) {
                    map.put("userId", u.getId());
                    map.put("photo", u.getPhoto());
                    map.put("userName", u.getName());
                    map.put("createDate",w.getCreateDate());
                    map.put("updateDate",w.getUpdateDate());
                    map.put("oaEvectionBean",w);
                    mapList9.add(map);
                }
            }
            map2.put("oaEvection",mapList9);
        }
        if(workOvertimeFormList!=null && workOvertimeFormList.size()!=0) {
            for (WorkOvertimeForm w : workOvertimeFormList) {
                Map map = new HashMap();
                User u = UserUtils.get(w.getCreateBy().getId());
                if (u!=null) {
                    map.put("userId", u.getId());
                    map.put("photo", u.getPhoto());
                    map.put("userName", u.getName());
                    map.put("createDate",w.getCreateDate());
                    map.put("updateDate",w.getUpdateDate());
                    map.put("workOvertimeFormBean",w);
                    mapList10.add(map);
                }
            }
            map2.put("workOvertimeForm",mapList10);
        }
        if(goOutList!=null && goOutList.size()!=0) {
            for (GoOut goOut : goOutList) {
                Map map = new HashMap();
                User u = UserUtils.get(goOut.getCreateBy().getId());
                if (u!=null) {
                    map.put("userId", u.getId());
                    map.put("photo", u.getPhoto());
                    map.put("userName", u.getName());
                    map.put("createDate",goOut.getCreateDate());
                    map.put("updateDate",goOut.getUpdateDate());
                    map.put("goOutBean",goOut);
                    mapList11.add(map);
                }
            }
            map2.put("workGoOut",mapList11);
        }
        totalMap4.put("audits",map2);
        reportList.add(totalMap1);
        reportList.add(totalMap2);
        reportList.add(totalMap3);
        reportList.add(totalMap4);
        return reportList;
    }

    public List getReportUserAppList(String dateStr){
        Map<String,Object> maps = getManageCalendarByDate(dateStr);
        List reportList =new ArrayList();

        List<WorkReport> workReportList = (List<WorkReport>)maps.get("workReportList");
        List<WorkOutSignIn> workOutList = (List<WorkOutSignIn>)maps.get("workOutList");
        List<OaAttendance> oaAttenList = (List<OaAttendance>)maps.get("oaAttenList");
        List<WorkSealForm> workSealList = (List<WorkSealForm>)maps.get("workSealList");
        List<Leave> leaveList = (List<Leave>)maps.get("leaveList");
        List<OaEvection> oaEvectionList = (List<OaEvection>)maps.get("oaEvectionList");
        List<WorkOvertimeForm> workOvertimeFormList = (List<WorkOvertimeForm>)maps.get("workOvertimeFormList");
        List<GoOut> goOutList = (List<GoOut>)maps.get("goOutList");

        List<Map> mapList1 = new ArrayList<>();
        List<Map> mapList2 = new ArrayList<>();
        List<Map> mapList3 = new ArrayList<>();
        List<Map> mapList4 = new ArrayList<>();
        List<Map> mapList5 = new ArrayList<>();
        List<Map> mapList6 = new ArrayList<>();
        List<Map> mapList7 = new ArrayList<>();
        List<Map> mapList8 = new ArrayList<>();
        List<Map> mapList9 = new ArrayList<>();
        List<Map> mapList10 = new ArrayList<>();
        List<Map> mapList11 = new ArrayList<>();
        Map<String,Object> totalMap1 = new HashMap<String,Object>();
        Map<String,Object> totalMap2 = new HashMap<String,Object>();
        Map<String,Object> totalMap3 = new HashMap<String,Object>();
        Map<String,Object> totalMap4 = new HashMap<String,Object>();

        Map<String,Object> map3 = new HashMap<String,Object>();
        Map<String,Object> map4 = new HashMap<String,Object>();
        if(workReportList!=null && workReportList.size()!=0) {
            Set<User> set = new HashSet<User>();
            for (WorkReport w : workReportList) {
                Map map = new HashMap();
                User u = UserUtils.get(w.getCreateBy().getId());
                if (u!=null && set.add(u)) {
                    map.put("userId", u.getId());
                    map.put("photo", u.getPhoto());
                    map.put("userName", u.getName());
                    mapList1.add(map);
                }
            }
            map3.put("workReport",mapList1);
        }
        totalMap1.put("workReports",map3);
        if(workOutList!=null && workOutList.size()!=0) {
            Set<User> set = new HashSet<User>();
            for (WorkOutSignIn w : workOutList) {
                Map map = new HashMap();
                User u = UserUtils.get(w.getCreateBy().getId());
                if (u!=null && set.add(u)) {
                    map.put("userId", u.getId());
                    map.put("photo", u.getPhoto());
                    map.put("userName", u.getName());
                    mapList2.add(map);
                }
            }
            map4.put("workOutSignIn", mapList2);
        }
        totalMap2.put("workOutSignIns",map4);
        Map<String,Object> map1 = new HashMap<String,Object>();
        if(oaAttenList!=null && oaAttenList.size()!=0) {
            SimpleDateFormat sdf3 = new SimpleDateFormat("HHmmss");
            for (OaAttendance o : oaAttenList) {
                Map map = new HashMap();
                Set<User> setmorning = new HashSet<User>();
                Set<User> setlate = new HashSet<User>();
                Set<User> setnight = new HashSet<User>();
                Set<User> setleaveEarly = new HashSet<User>();
                User u = UserUtils.get(o.getCreateBy().getId());
                if (u!=null){
                    if (o.getStartTime() != null && !o.getStartTime().equals("")) {
                        String arr[] = o.getRule().getStartTime().split(":");
                        String mornings =arr[0]+(arr[1].length()==1?"0"+arr[1]:arr[1])+"00";
                        int morning = Integer.parseInt(mornings);
                        Date startTime = o.getStartTime();
                        Integer start = Integer.parseInt(sdf3.format(startTime));
                        if (start > morning && setlate.add(u)) {
                            map.put("userId", u.getId());
                            map.put("photo", u.getPhoto());
                            map.put("userName", u.getName());
                            mapList3.add(map);
                        } else {
                            if(setmorning.add(u)) {
                                map.put("userId", u.getId());
                                map.put("photo", u.getPhoto());
                                map.put("userName", u.getName());
                                mapList4.add(map);
                            }
                        }
                    }
                    if (o.getEndTime() != null && !o.getEndTime().equals("")) {
                        String arr[] = o.getRule().getEndTime().split(":");
                        String nights =arr[0]+(arr[1].length()==1?"0"+arr[1]:arr[1])+"00";
                        int night = Integer.parseInt(nights);
                        Date endTime = o.getEndTime();
                        Integer end = Integer.parseInt(sdf3.format(endTime));
                        if (end > night && setnight.add(u)) {
                            map.put("userId", u.getId());
                            map.put("photo", u.getPhoto());
                            map.put("userName", u.getName());
                            mapList5.add(map);
                        } else {
                            if (setleaveEarly.add(u)) {
                                map.put("userId", u.getId());
                                map.put("photo", u.getPhoto());
                                map.put("userName", u.getName());
                                mapList6.add(map);
                            }
                        }
                    }
                }
            }
            if(mapList3!=null && mapList3.size()!=0){ map1.put("late",mapList3); }

            if(mapList4!=null && mapList4.size()!=0){ map1.put("onDuty",mapList4);}
            if(mapList5!=null && mapList5.size()!=0){  map1.put("offDut",mapList5);}
            if(mapList6!=null && mapList6.size()!=0){ map1.put("leaveEarly",mapList6);}

        }
        totalMap3.put("attendances",map1);
        Map<String,Object> map2 = new HashMap<String,Object>();
        if(workSealList!=null && workSealList.size()!=0) {
            Set<User> set = new HashSet<User>();
            for (WorkSealForm w : workSealList) {
                Map map = new HashMap();
                User u = UserUtils.get(w.getCreateBy().getId());
                if (u!=null && set.add(u)) {
                    map.put("userId", u.getId());
                    map.put("photo", u.getPhoto());
                    map.put("userName", u.getName());
                    mapList7.add(map);
                }
            }
            map2.put("workSealFrom",mapList7);
        }
        if(leaveList!=null && leaveList.size()!=0) {
            Set<User> set = new HashSet<User>();
            for (Leave w : leaveList) {
                Map map = new HashMap();
                User u = UserUtils.get(w.getCreateBy().getId());
                if (u!=null && set.add(u)) {
                    map.put("userId", u.getId());
                    map.put("photo", u.getPhoto());
                    map.put("userName", u.getName());
                    mapList8.add(map);
                }
            }
            map2.put("leave",mapList8);
        }
        if(oaEvectionList!=null && oaEvectionList.size()!=0) {
            Set<User> set = new HashSet<User>();
            for (OaEvection w : oaEvectionList) {
                Map map = new HashMap();
                User u = UserUtils.get(w.getCreateBy().getId());
                if (u!=null && set.add(u)) {
                    map.put("userId", u.getId());
                    map.put("photo", u.getPhoto());
                    map.put("userName", u.getName());
                    mapList9.add(map);
                }
            }
            map2.put("oaEvection",mapList9);
        }
        if(workOvertimeFormList!=null && workOvertimeFormList.size()!=0) {
            Set<User> set = new HashSet<User>();
            for (WorkOvertimeForm w : workOvertimeFormList) {
                Map map = new HashMap();
                User u = UserUtils.get(w.getCreateBy().getId());
                if (u!=null && set.add(u)) {
                    map.put("userId", u.getId());
                    map.put("photo", u.getPhoto());
                    map.put("userName", u.getName());
                    mapList10.add(map);
                }
            }
            map2.put("workOvertimeForm",mapList10);
        }
        if(goOutList!=null && goOutList.size()!=0) {
            Set<User> set = new HashSet<User>();
            for (GoOut goOut : goOutList) {
                Map map = new HashMap();
                User u = UserUtils.get(goOut.getCreateBy().getId());
                if (u!=null && set.add(u)) {
                    map.put("userId", u.getId());
                    map.put("photo", u.getPhoto());
                    map.put("userName", u.getName());
                    mapList11.add(map);
                }
            }
            map2.put("workGoOut",mapList11);
        }
        totalMap4.put("audits",map2);
        reportList.add(totalMap1);
        reportList.add(totalMap2);
        reportList.add(totalMap3);
        reportList.add(totalMap4);
        return reportList;
    }
}
