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

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.ObjectUtil;
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.room.dto.RoomStudentAppointPageDto;
import com.xjrsoft.module.room.entity.RoomBed;
import com.xjrsoft.module.room.entity.RoomStudentAppoint;
import com.xjrsoft.module.room.mapper.RoomBedMapper;
import com.xjrsoft.module.room.mapper.RoomStudentAppointMapper;
import com.xjrsoft.module.room.service.IRoomStudentAppointService;
import com.xjrsoft.module.room.vo.AppointPageRoomBedVo;
import com.xjrsoft.module.room.vo.HeadTeaRoomCadreAppointPageVo;
import com.xjrsoft.module.room.vo.RoomStudentAppointVo;
import com.xjrsoft.module.student.entity.BaseStudentPost;
import com.xjrsoft.module.teacher.entity.XjrUser;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @title: 寝室长任命
 * @Author dzx
 * @Date: 2023-12-30
 * @Version 1.0
 */
@Service
@AllArgsConstructor
public class RoomStudentAppointServiceImpl extends MPJBaseServiceImpl<RoomStudentAppointMapper, RoomStudentAppoint> implements IRoomStudentAppointService {

    private final RoomStudentAppointMapper roomStudentAppointMapper;

    private final RoomBedMapper roomBedMapper;

    @Override
    public IPage<HeadTeaRoomCadreAppointPageVo> getPage(RoomStudentAppointPageDto dto) {
        dto.setTeacherId(StpUtil.getLoginIdAsLong());
        IPage<HeadTeaRoomCadreAppointPageVo> headTeaRoomCadreAppointPageVoIPage = roomStudentAppointMapper.getPage(ConventPage.getPage(dto), dto);
        //找到每个寝室的每个床位的人
        for (HeadTeaRoomCadreAppointPageVo headTeaRoomCadreAppointPageVo : headTeaRoomCadreAppointPageVoIPage.getRecords()) {
            List<AppointPageRoomBedVo> appointPageRoomBedVoList = this.getStuByRoomId(Long.parseLong(headTeaRoomCadreAppointPageVo.getRoomId()));
            if (ObjectUtil.isNotNull(appointPageRoomBedVoList) && appointPageRoomBedVoList.size() > 0){
                //找到每个人的职位
                for (AppointPageRoomBedVo appointPageRoomBedVo : appointPageRoomBedVoList) {
                    MPJLambdaWrapper<RoomStudentAppoint> queryPost = new MPJLambdaWrapper<>();
                    queryPost
                            .selectAs(BaseStudentPost::getPost, RoomStudentAppointVo::getPostIdCN)
                            .select(RoomStudentAppoint.class, x -> VoToColumnUtil.fieldsToColumns(RoomStudentAppointVo.class).contains(x.getProperty()))
                            .leftJoin(BaseStudentPost.class,BaseStudentPost::getId,RoomStudentAppoint::getPostId)
                            .eq(RoomStudentAppoint::getRoomId,appointPageRoomBedVo.getRoomId())
                            .eq(RoomStudentAppoint::getRoomBedId,appointPageRoomBedVo.getRoomBedId())
                            .eq(RoomStudentAppoint::getStudentUserId,appointPageRoomBedVo.getStudentUserId());
                    List<RoomStudentAppointVo> roomStudentAppointList = this.selectJoinList(RoomStudentAppointVo.class,queryPost);
                    if (ObjectUtil.isNotNull(roomStudentAppointList) && roomStudentAppointList.size() > 0){
                        List<String> postList = new ArrayList<>();
                        for (RoomStudentAppointVo roomStudentAppointVo : roomStudentAppointList) {
                            postList.add(roomStudentAppointVo.getPostIdCN());
                        }
                        appointPageRoomBedVo.setPostList(postList);
                    }
                }
                headTeaRoomCadreAppointPageVo.setRoomBedList(appointPageRoomBedVoList);
            }
        }
        return headTeaRoomCadreAppointPageVoIPage;
    }

    @Override
    public List<AppointPageRoomBedVo> getStuByRoomId(RoomStudentAppointPageDto dto) {
        return this.getStuByRoomId(dto.getRoomId());
    }

    private List<AppointPageRoomBedVo> getStuByRoomId(Long roomId) {
        MPJLambdaWrapper<RoomBed> queryRoomBed = new MPJLambdaWrapper<>();
        queryRoomBed
                .selectAs(RoomBed::getId,AppointPageRoomBedVo::getRoomBedId)
                .selectAs(XjrUser::getName, AppointPageRoomBedVo::getStudentUserIdCN)
                .select(RoomBed.class, x -> VoToColumnUtil.fieldsToColumns(AppointPageRoomBedVo.class).contains(x.getProperty()))
                .leftJoin(XjrUser.class,XjrUser::getId,RoomBed::getStudentUserId)
                .eq(RoomBed::getRoomId,roomId)
                .eq(RoomBed::getIsCheckIn,1);
        List<AppointPageRoomBedVo> appointPageRoomBedVoList = roomBedMapper.selectJoinList(AppointPageRoomBedVo.class,queryRoomBed);
        return appointPageRoomBedVoList;
    }
}
