package com.xjrsoft.module.teacher.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.utils.VoToColumnUtil;
import com.xjrsoft.module.teacher.dto.WfHeadTeacherLeavePageDto;
import com.xjrsoft.module.teacher.entity.WfHeadTeacherLeave;
import com.xjrsoft.module.teacher.entity.XjrUser;
import com.xjrsoft.module.teacher.mapper.WfHeadTeacherLeaveMapper;
import com.xjrsoft.module.teacher.service.IWfHeadTeacherLeaveService;
import com.xjrsoft.module.teacher.vo.WfHeadTeacherLeavePageVo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @title: 班主任事项请假
* @Author szs
* @Date: 2023-12-19
* @Version 1.0
*/
@Service
@AllArgsConstructor
public class WfHeadTeacherLeaveServiceImpl extends MPJBaseServiceImpl<WfHeadTeacherLeaveMapper, WfHeadTeacherLeave> implements IWfHeadTeacherLeaveService {

    private final WfHeadTeacherLeaveMapper wfHeadTeacherLeaveMapper;

    @Override
    public IPage<WfHeadTeacherLeavePageVo> getListPage(WfHeadTeacherLeavePageDto dto) {
        return wfHeadTeacherLeaveMapper.getListPage(ConventPage.getPage(dto),dto);
    }

    @Override
    public Map<Long, WfHeadTeacherLeave> getLeaveList(LocalDateTime startTime, LocalDateTime endTime) {
        List<WfHeadTeacherLeave> list = wfHeadTeacherLeaveMapper.selectJoinList(WfHeadTeacherLeave.class,
                new MPJLambdaWrapper<WfHeadTeacherLeave>()
                        .select(WfHeadTeacherLeave.class, x -> VoToColumnUtil.fieldsToColumns(WfHeadTeacherLeave.class).contains(x.getProperty()))
                        .innerJoin(XjrUser.class, XjrUser::getId, WfHeadTeacherLeave::getApplicantUserId)
                        .eq(WfHeadTeacherLeave::getStatus, 1)
                        .between(WfHeadTeacherLeave::getStartTime, startTime, endTime)
        );
        Map<Long, WfHeadTeacherLeave> result = new HashMap<>();
        for (WfHeadTeacherLeave leave : list) {
            result.put(leave.getApplicantUserId(), leave);
        }
        return result;
    }
}
