package com.xueyi.system.meeting.service.impl;

import com.xueyi.common.web.entity.service.impl.BaseServiceImpl;
import com.xueyi.common.web.utils.DateUtils;
import com.xueyi.system.api.meeting.domain.dto.DmMeetingOrdersDto;
import com.xueyi.system.api.meeting.domain.po.DmMeetingOrdersPo;
import com.xueyi.system.api.staff.domain.po.DmStaffPo;
import com.xueyi.system.meeting.domain.query.DmMeetingOrdersQuery;
import com.xueyi.system.meeting.manager.IDmMeetingOrdersManager;
import com.xueyi.system.meeting.mapper.DmMeetingOrdersMapper;
import com.xueyi.system.meeting.mapper.DmMeetingRoomsMapper;
import com.xueyi.system.meeting.service.IDmMeetingOrdersService;
import com.xueyi.system.staff.service.impl.DmStaffServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 会议室预约管理 服务层处理
 *
 * @author xueyi
 */
@Service
public class DmMeetingOrdersServiceImpl extends BaseServiceImpl<DmMeetingOrdersQuery, DmMeetingOrdersDto, IDmMeetingOrdersManager> implements IDmMeetingOrdersService {

    @Autowired
    private DmStaffServiceImpl dmStaffService;
    /**
     * 查询会议室预约对象列表 | 数据权限
     *
     * @param dmMeetingOrders 会议室预约对象
     * @return 会议室预约对象集合
     */
    @Override
    //@DataScope(userAlias = "createBy", mapperScope = {"DmMeetingOrdersMapper"})
    public List<DmMeetingOrdersDto> selectListScope(DmMeetingOrdersQuery dmMeetingOrders) {
        return baseManager.selectList(dmMeetingOrders);
    }

    @Override
    public List<DmMeetingOrdersPo> selectRoomList(DmMeetingOrdersPo query) {
        return null;
    }

    @Override
    public void fixColumns(DmMeetingOrdersDto dto) {
        DmStaffPo sm = dmStaffService.selectById(dto.getOrderBy());
        if (null != sm)
        dto.setOrderByEmp(sm.getUserName());

        dto.setStartTimeStr(DateUtils.formatDate(dto.getStartTime(), "HH:mm"));
        dto.setOrderDateStr(DateUtils.formatDate(dto.getOrderDate(), "YYYY-MM-dd"));
        dto.setEndTimeStr(DateUtils.formatDate(dto.getEndTime(), "HH:mm"));

    }

    @Autowired
    private DmMeetingRoomsMapper dmMeetingRoomsMapper;

    @Autowired
    private DmMeetingOrdersMapper dmMeetingOrdersMapper;

    @Override
    public Map<Long, List<DmMeetingOrdersPo>> selectOrderedList(Long deptId, String dateStr) {
        List<DmMeetingOrdersPo> list = new ArrayList<>();
        //当天
        String curDateStr = DateUtils.formatDate(new Date(), "yyyy-MM-dd");
        list = dmMeetingOrdersMapper.findListByDateStr(dateStr);

        //过滤掉今天的已经过去的预约记录，并按开始时间进行排序
        if (curDateStr.equals(dateStr)) {
            list = list.stream().filter(t->DateUtils.formatDate(t.getOrderDate(), "yyyy-MM-dd").compareTo(DateUtils.formatDate(new Date(), "yyyy-MM-dd"))>0 || (DateUtils.formatDate(t.getOrderDate(), "yyyy-MM-dd").compareTo(DateUtils.formatDate(new Date(), "yyyy-MM-dd"))==0 && DateUtils.formatDate(t.getStartTime(), "HH:mm").compareTo(DateUtils.formatDate(new Date(), "HH:mm"))>0)).sorted(Comparator.comparing(po -> DateUtils.formatDate(po.getStartTime(), "HH:mm"))).collect(Collectors.toList());
        } else {
            list = list.stream().sorted(Comparator.comparing(po -> DateUtils.formatDate(po.getStartTime(), "HH:mm"))).collect(Collectors.toList());
        }

        Map<Long, List<DmMeetingOrdersPo>> groupedByRoom = list.stream()
                .collect(Collectors.groupingBy(DmMeetingOrdersPo::getSpaceId));
        return groupedByRoom;
    }
}