package com.casic.missiles.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.casic.missiles.dto.ReturnDTO;
import com.casic.missiles.dto.ReturnUtil;
import com.casic.missiles.dto.workbench.CalendarScheduleDeleteRequest;
import com.casic.missiles.dto.workbench.CalendarScheduleListRequest;
import com.casic.missiles.enums.BusinessExceptionEnum;
import com.casic.missiles.exception.BusinessException;
import com.casic.missiles.mapper.workbench.WorkbenchCalendarScheduleMapper;
import com.casic.missiles.model.workbench.WorkbenchCalendarSchedule;
import com.casic.missiles.service.workbench.IWorkbenchCalendarScheduleService;
import com.casic.missiles.utils.DateUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 工作台-日历日程安排表 服务实现类
 * </p>
 *
 * @author wangpeng
 * @since 2023-06-16
 */
@Service
public class WorkbenchCalendarScheduleServiceImpl extends ServiceImpl<WorkbenchCalendarScheduleMapper, WorkbenchCalendarSchedule> implements IWorkbenchCalendarScheduleService {
    @Autowired
    private WorkbenchCalendarScheduleMapper calendarScheduleMapper;

    @Override
    public Map<String, List<WorkbenchCalendarSchedule>> calendarScheduleList(CalendarScheduleListRequest request) {
        String calendarMonth = request.getCalendarMonth();
        Long userId = request.getUserId();
        List<WorkbenchCalendarSchedule> list = calendarScheduleMapper.selectListByMonth(calendarMonth, userId);
        if(CollectionUtils.isEmpty(list)){
            return new HashMap<>();
        }
        Map<String, List<WorkbenchCalendarSchedule>> resultMap = list.stream().collect(
                Collectors.groupingBy(WorkbenchCalendarSchedule::getScheduleDate)
        );

        List<String> dayListOfMonth = DateUtils.getDayListOfMonth(calendarMonth);
        //LinkedHashMap保证插入顺序
        Map<String, List<WorkbenchCalendarSchedule>> finalMap = new LinkedHashMap<>();
        for (String day : dayListOfMonth) {
            if(CollectionUtils.isEmpty(resultMap.get(day))){
                finalMap.put(day, new ArrayList<>());
            }else{
                finalMap.put(day, resultMap.get(day));
            }
        }
        return finalMap;
    }

    @Override
    public ReturnDTO calendarScheduleAdd(List<WorkbenchCalendarSchedule> calendarScheduleList) {
        if (this.saveBatch(calendarScheduleList)) {
            return ReturnUtil.success();
        }
        throw new BusinessException(BusinessExceptionEnum.HANDLE_FAILED);
    }

    @Override
    public ReturnDTO updateCalendarSchedule(List<WorkbenchCalendarSchedule> calendarScheduleList) {
        Long userId = calendarScheduleList.get(0).getUserId();
        String scheduleDate = calendarScheduleList.get(0).getScheduleDate();
        QueryWrapper<WorkbenchCalendarSchedule> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        wrapper.eq("schedule_date", scheduleDate);
        calendarScheduleMapper.delete(wrapper);
        if(StringUtils.isEmpty(calendarScheduleList.get(0).getScheduleMatters())){
            return ReturnUtil.success();
        }
        if (this.saveBatch(calendarScheduleList)) {
            return ReturnUtil.success();
        }
        throw new BusinessException(BusinessExceptionEnum.HANDLE_FAILED);
    }

    @Override
    public ReturnDTO deleteCalendarSchedule(CalendarScheduleDeleteRequest request) {
        QueryWrapper<WorkbenchCalendarSchedule> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", request.getUserId());
        wrapper.eq("schedule_date", request.getScheduleDate());
        int delete = calendarScheduleMapper.delete(wrapper);
        if(delete > 0){
            return ReturnUtil.success();
        }
        throw new BusinessException(BusinessExceptionEnum.HANDLE_FAILED);
    }
}
