//package com.xunmei.system.service.impl;
//
//import cn.hutool.core.date.DateField;
//import cn.hutool.core.date.DateTime;
//import cn.hutool.core.date.DateUtil;
//import cn.hutool.core.util.ObjectUtil;
//import com.alibaba.fastjson.JSON;
//import com.isp.app.domain.*;
//import com.isp.app.dto.holiday.HolidayDto;
//import com.isp.app.dto.workTime.WorkTimeEditDto;
//import com.isp.app.dto.workTimeMonth.WorkTimeMonthFindMonthDto;
//import com.isp.app.repository.WorkTimeSetRepository;
//import com.isp.app.service.*;
//import com.isp.app.vo.workTime.WorkTimeNewVo;
//import com.isp.common.constant.SystemParameterConstant;
//import com.isp.common.context.LoginUser;
//import com.isp.common.context.RequestHolder;
//import com.isp.common.util.BeanHelper;
//import com.isp.common.util.DateHelper;
//import com.isp.common.util.StreamHelper;
//import com.isp.core.domain.Org;
//import com.isp.core.mapper.ObjectMapper;
//import com.isp.core.service.OrgService;
//import com.xunmei.system.api.domain.SysWorkTime;
//import com.xunmei.system.domain.SysWorkTimeMonth;
//import com.xunmei.system.domain.SysWorkTimeSet;
//import com.xunmei.system.service.ISysOrgService;
//import com.xunmei.system.service.ISysWorkTimeService;
//import com.xunmei.system.service.ISysWorkTimeSetService;
//import com.xunmei.system.util.BeanHelper;
//import com.xunmei.system.util.DateHelper;
//import com.xunmei.system.util.StreamHelper;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.collections4.CollectionUtils;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//import org.springframework.transaction.annotation.Transactional;
//
//import java.util.*;
//import java.util.stream.Collectors;
//
//import static com.isp.common.util.LogUtils.WORKTIMEBUSINESSLOG;
//
//@Slf4j
//@Component
//public class WorkTimeBusiness {
//
//    @Autowired
//    private ISysWorkTimeService workTimeMonthService;
//
//    @Autowired
//    private ISysWorkTimeService workTimeService;
//
//    @Autowired
//    ISysWorkTimeSetService workTimeSetService;
//
////    @Autowired
////    HolidayService holidayService;
//
////    @Autowired
////    private WorkTimeSetRepository workTimeSetRepository;
//
//    @Autowired
//    private ISysOrgService orgService;
//
////    @Autowired
////    SystemParameService systemParameService;
//
////    @Autowired
////    ObjectMapper objectMapper;
//
//    /**
//     * @param orgId
//     * @param month
//     * @param workTimeEditDtoList
//     */
//    @Transactional
//    public SysWorkTimeMonth batch(final Long orgId, final Date month, final List<SysWorkTime> workTimeEditDtoList) {
//        // 小于现在时间的不允许修改
//        final Date now = DateUtil.beginOfDay(new Date());
//        List<SysWorkTime> workTimeList = StreamHelper.to(workTimeEditDtoList, workTimeEditDto -> {
//            final Date workDate = workTimeEditDto.getDate();
//            if (workTimeEditDto.getIsEnable() == null || workDate.before(now)) {
////                WORKTIMEBUSINESSLOG.info("作息时间[ {} ]小于现在时间, 不允许改变", workDate);
//                return null;
//            }
//            final SysWorkTime workTime = SysWorkTime.of(orgId, workTimeEditDto.getDate());
//            BeanHelper.copyProperties(workTime, workTimeEditDto);
//            workTime.setIsManual(0L);
//            workTime.setIsEnable(workTimeEditDto.getIsEnable());
//            if (!(workTimeEditDto.getIsEnable() > 0)) {
//                workTime.setWorkTime(null);
//                workTime.setWorkOffTime(null);
//                workTime.setOpenTime(null);
//                workTime.setCloseTime(null);
//                workTime.setNoonbreakStart(null);
//                workTime.setNoonbreakEnd(null);
//            }
//            return workTime;
//        }, true);
//
//        //
//        for (SysWorkTime s :
//                workTimeList) {
//            workTimeService.save(s);
//            workTimeList.add(s);
//        }
//        WorkTimeMonthFindMonthDto request = new WorkTimeMonthFindMonthDto();
//        request.setMonth(month);
//        request.setOrgId(orgId);
//        WorkTimeMonth db = workTimeMonthService.findMonthTime(request);
//        if (db != null) {
//            int openCount = (int) workTimeEditDtoList.stream().filter(w -> w.getIsEnable() != null && w.getIsEnable() == true).count();
//            int closeCount = (int) workTimeEditDtoList.stream().filter(w -> w.getIsEnable() != null && w.getIsEnable() == false).count();
//            ;
////            for (final WorkTime workTime : workTimeList) {
////                if (workTime.getIsEnable()) {
////                    openCount++;
////                } else {
////                    closeCount++;
////                }
////            }
//            db.setUpdateTime(new Date());
//            db.setCloseCount(closeCount);
//            db.setOpenCount(openCount);
//            return this.workTimeMonthService.save(db);
//        } else {
//            return this.workTimeMonthService.save(WorkTimeMonth.of(orgId, month, workTimeList));
//        }
//    }
//
//    /**
//     * 生成下个月的作息
//     */
//    @Transactional
//    public void generateNextMonth() {
//        Calendar calendar = Calendar.getInstance();
//        calendar.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
//        System.out.println("============================ work_time_job 1================== : " + calendar.getTime());
//
//        calendar.set(Calendar.DAY_OF_MONTH, 1);
//        calendar.add(Calendar.MONTH, 1);
//        Integer year = calendar.get(Calendar.YEAR);
//        generateWorkTime(year, calendar.get(Calendar.MONTH));
////        List<WorkTimeSet> workTimes = workTimeSetService.find("year", year);
////
////        final Date date = DateHelper.getDate(calendar.getTime());
////        System.out.println("============================ work_time_job 2================== : " + date);
////        final DateHelper dateTime = new DateHelper(date);
////        final Date start = dateTime.monthStart();
////        final Date end = dateTime.monthEnd();
////        final List<DateTime> dateTimeList = DateUtil.rangeToList(start, end, DateField.DAY_OF_MONTH);
////        HolidayDto holidayDto = new HolidayDto();
////        holidayDto.setYear(year);
////        List<Holiday> holidayList = this.holidayService.all(holidayDto);
////        List<Date> holidayDates = null;
////        if (CollectionUtils.isNotEmpty(holidayList)) {
////            holidayDates = holidayList.stream().map(p -> p.getYmdDate()).collect(Collectors.toList());
////        }
////        for (WorkTimeSet workTime : workTimes) {
////            if(ObjectUtil.isNull(workTime.getOrgId())){
////                continue;
////            }
////
////            List<WorkTimeEditDto> workTimeEditDtoList = new ArrayList<>();
////            List<Holiday> holidays = workTime.getHolidayList();
////            List<Date> need = null;
////            if (CollectionUtils.isNotEmpty(holidays)) {
////                need = holidays.stream().map(p -> p.getYmdDate()).collect(Collectors.toList());
////            }
////            for (DateTime time : dateTimeList) {
////                WorkTimeEditDto dto = new WorkTimeEditDto();
////                dto.setDate(time);
////                dto.setWorkTime(workTime.getWorkTime());
////                dto.setWorkOffTime(workTime.getWorkOffTime());
////                dto.setOpenTime(workTime.getOpenTime());
////                dto.setCloseTime(workTime.getCloseTime());
////                dto.setNoonbreakStart(workTime.getNoonbreakStart());
////                dto.setNoonbreakEnd(workTime.getNoonbreakEnd());
////                dto.setIsEnable(false);
////                if (holidayDates != null && (!holidayDates.contains(time.toJdkDate()))) {
////                    if (workTime.getSunday() && time.dayOfWeek() == 1) {
////                        dto.setIsEnable(true);
////                    }
////                    if (workTime.getMonday() && time.dayOfWeek() == 2) {
////                        dto.setIsEnable(true);
////                    }
////                    if (workTime.getTuesday() && time.dayOfWeek() == 3) {
////                        dto.setIsEnable(true);
////                    }
////                    if (workTime.getWednesday() && time.dayOfWeek() == 4) {
////                        dto.setIsEnable(true);
////                    }
////                    if (workTime.getThursday() && time.dayOfWeek() == 5) {
////                        dto.setIsEnable(true);
////                    }
////                    if (workTime.getFriday() && time.dayOfWeek() == 6) {
////                        dto.setIsEnable(true);
////                    }
////                    if (workTime.getSaturday() && time.dayOfWeek() == 7) {
////                        dto.setIsEnable(true);
////                    }
////                }
////                if (need != null && need.contains(time.toJdkDate())) {
////                    dto.setIsEnable(true);
////                }
////                workTimeEditDtoList.add(dto);
////            }
////            batch(workTime.getOrgId(), date, workTimeEditDtoList);
////        }
//    }
//
//    @Transactional
//    public void generateWorkTime(int year, int month) {
//        Calendar calendar = Calendar.getInstance();
//        calendar.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
//        calendar.set(Calendar.DAY_OF_MONTH, 1);
//        calendar.set(Calendar.MONTH, month);
//        calendar.set(Calendar.YEAR, year);
//        System.out.println("============================ work_time_job 1================== : " + calendar.getTime());
//        List<SysWorkTimeSet> workTimes = workTimeSetService.find("year", year);
//        Map<Long, List<SysWorkTimeSet>> group = workTimes.stream()
//                .sorted(Comparator.comparing(SysWorkTimeSet::getEffectiveDate).reversed())
//                .collect(Collectors.groupingBy(w -> w.getOrgId()));
//
//        final Date date = DateHelper.getDate(calendar.getTime());
//        System.out.println("============================ work_time_job 2================== : " + date);
//        final DateHelper dateTime = new DateHelper(date);
//        final Date start = dateTime.monthStart();
//        final Date end = dateTime.monthEnd();
//        final List<DateTime> dateTimeList = DateUtil.rangeToList(start, end, DateField.DAY_OF_MONTH);
//        System.out.println(JSON.toJSONString(dateTimeList));
//        HolidayDto holidayDto = new HolidayDto();
//        holidayDto.setYear(year);
//        List<Holiday> holidayList = this.holidayService.all(holidayDto);
//        List<Date> holidayDates = null;
//        if (CollectionUtils.isNotEmpty(holidayList)) {
//            holidayDates = holidayList.stream().map(p -> p.getYmdDate()).collect(Collectors.toList());
//        }
//        List<WorkTimeEditDto> workTimeEditDtoList = new ArrayList<>();
//        for (DateTime time : dateTimeList) {
//            Date date1 = time.toJdkDate();
//            System.out.println("date1:" + date1);
//            for (Long orgId : group.keySet()) {
//                Optional<WorkTimeSet> ws = group.get(orgId).stream().filter(w -> date1.after(w.getEffectiveDate()) || DateUtil.isSameDay(date1, w.getEffectiveDate())).findFirst();
//                if (!ws.isPresent()) {
//                    continue;
//                }
//                WorkTimeSet set = ws.get();
//                WorkTimeSetDayOfWeek dayOfWeekSet = ws.get().getWeekDaySet(time.dayOfWeek());
//                boolean isEnable = ObjectUtil.isNotNull(dayOfWeekSet) && dayOfWeekSet.getIsWorkday();
//                WorkTimeEditDto dto = new WorkTimeEditDto();
//                dto.setDate(time);
//                dto.setOrgId(orgId);
//                dto.setIsEnable(isEnable);
//                if (ObjectUtil.isNull(dayOfWeekSet) || !dayOfWeekSet.getIsWorkday()) {
//                    Optional<WorkTimeSetDayOfWeek> firstWorkDay = set.getDayOfWeeks().stream().filter(f -> f.getIsWorkday()).findFirst();
//                    if (firstWorkDay.isPresent()) {
//                        dayOfWeekSet = firstWorkDay.get();
//                    }
//                }
//                //dto.setIsEnable(dayOfWeekSet.getIsWorkday());
//                workTimeEditDtoList.add(dto);
//                dto.setWorkTime(dayOfWeekSet.getWorkTime());
//                dto.setWorkOffTime(dayOfWeekSet.getWorkOffTime());
//                dto.setOpenTime(dayOfWeekSet.getOpenTime());
//                dto.setCloseTime(dayOfWeekSet.getCloseTime());
//                dto.setNoonbreakStart(dayOfWeekSet.getNoonbreakStart());
//                dto.setNoonbreakEnd(dayOfWeekSet.getNoonbreakEnd());
//                workTimeEditDtoList.add(dto);
//            }
//        }
//
//        Map<Long, List<WorkTimeEditDto>> groupDay = workTimeEditDtoList.stream().collect(Collectors.groupingBy(w -> w.getOrgId()));
//        //获取所有机构下个月的按日配置的作息
//        List<WorkTimeNewVo> list = new ArrayList<>();
//        list = objectMapper.getNextMonthWorkTime(DateUtil.format(start, "yyyy-MM"));
//        for (Long orgId : groupDay.keySet()) {
//            List<WorkTimeEditDto> workTimeEditDtos = groupDay.get(orgId);
//            List<WorkTimeNewVo> workTimeNewVos = list.stream().filter(w -> w.getOrgId().equals(orgId)).collect(Collectors.toList());
//            //去掉workTimeEditDtos中机构与日期在workTimeNewVos中存在的数据
//            List<WorkTimeEditDto> list1 = null;
//            if (CollectionUtils.isNotEmpty(workTimeNewVos)) {
//                list1 = workTimeEditDtos.stream().filter(s -> {
//                    return workTimeNewVos.stream().noneMatch(f -> f.getOrgId().equals(s.getOrgId()) && f.getYmdDate().equals(s.getDate()));
//                }).collect(Collectors.toList());
//            } else {
//                list1 = workTimeEditDtos;
//            }
//
//            batch(orgId, date, list1);
//        }
////        for (WorkTimeSet workTime : workTimes) {
////            if (ObjectUtil.isNull(workTime.getOrgId())) {
////                continue;
////            }
////
////            List<WorkTimeEditDto> workTimeEditDtoList = new ArrayList<>();
////            List<Holiday> holidays = workTime.getHolidayList();
////            List<Date> need = new ArrayList<>();
////            if (CollectionUtils.isNotEmpty(holidays)) {
////                need = holidays.stream().map(p -> p.getYmdDate()).collect(Collectors.toList());
////            }
////            for (DateTime time : dateTimeList) {
////
////
////
////                boolean isEnable = ObjectUtil.isNotNull(dayOfWeekSet) && dayOfWeekSet.getIsWorkday();
////                if (ObjectUtil.isNotNull(holidayDates) && holidayDates.contains(time.toJdkDate())) {
////                    isEnable = false;
////                }
////                if (CollectionUtils.isNotEmpty(need) && need.contains(time.toJdkDate())) {
////                    isEnable = true;
////                }
////
////
////                if (!isEnable) {
////                    continue;
////                }
////
////
////
////
//////                if (holidayDates != null && (!holidayDates.contains(time.toJdkDate()))) {
//////                    if (workTime.getSunday() && time.dayOfWeek() == 1) {
//////                        dto.setIsEnable(true);
//////                    }
//////                    if (workTime.getMonday() && time.dayOfWeek() == 2) {
//////                        dto.setIsEnable(true);
//////                    }
//////                    if (workTime.getTuesday() && time.dayOfWeek() == 3) {
//////                        dto.setIsEnable(true);
//////                    }
//////                    if (workTime.getWednesday() && time.dayOfWeek() == 4) {
//////                        dto.setIsEnable(true);
//////                    }
//////                    if (workTime.getThursday() && time.dayOfWeek() == 5) {
//////                        dto.setIsEnable(true);
//////                    }
//////                    if (workTime.getFriday() && time.dayOfWeek() == 6) {
//////                        dto.setIsEnable(true);
//////                    }
//////                    if (workTime.getSaturday() && time.dayOfWeek() == 7) {
//////                        dto.setIsEnable(true);
//////                    }
//////                }
//////                if (need != null && need.contains(time.toJdkDate())) {
//////                    dto.setIsEnable(true);
//////                }
////
////            }
////            batch(workTime.getOrgId(), date, workTimeEditDtoList);
////        }
//    }
//
//    public List<WorkTime> batchnew(Long orgId, List<WorkTimeEditDto> workTimeDtoList) {
//        // 小于现在时间的不允许修改
//        final Date now = DateUtil.beginOfDay(new Date());
//        Date updateTime = new Date();
//        List<WorkTime> workTimeList = StreamHelper.to(workTimeDtoList, workTimeEditDto -> {
//            final Date workDate = workTimeEditDto.getDate();
//            if (workTimeEditDto.getIsEnable() == null || workDate.before(now)) {
//                WORKTIMEBUSINESSLOG.info("作息时间[ {} ]小于现在时间, 不允许改变", workDate);
//                return null;
//            }
//            final WorkTime workTime = WorkTime.of(orgId, workTimeEditDto.getDate());
//            BeanHelper.copyProperties(workTime, workTimeEditDto);
//            workTime.setIsManual(true);
//            workTime.setUpdateTime(updateTime);
//            return workTime;
//        }, true);
//        //
//        workTimeList = this.workTimeService.save(workTimeList);
//        return workTimeList;
//    }
//
//    public boolean checkTimeSet() {
//        boolean flag = true;
//        LoginUser loginUser = RequestHolder.get().getLoginUser();
//        Org org = this.orgService.find(loginUser.getOrgId());
//        boolean isCheck = true;
//        //非营业网点、离行自助银行、金库和办公大楼不需要提示
//        if (org.getType() != null && (org.getType().ordinal() != 3 && org.getType().ordinal() != 4 && org.getType().ordinal() != 5 && org.getType().ordinal() != 6)) {
//            isCheck = false;
//            //如果是办公大楼
//        } else {
//            if (org.getType().ordinal() == 6) {
//                SystemParame systemParame = systemParameService.findFirstByCode(SystemParameterConstant.OFFICEBUILDING_OPEN_ALLHOURS);
//                //如果是全天开放
//                if (systemParame != null && systemParame.getValue().equals("1")) {
//                    isCheck = false;
//                }
//            }
//        }
//        if (isCheck) {
//            List<WorkTimeSet> workTimes = this.workTimeSetService.find("orgId", loginUser.getOrgId());
//            if (!CollectionUtils.isNotEmpty(workTimes)) {
//                flag = false;
//            }
//        }
//        return flag;
//    }
//}
