package com.yida.data.school.facility.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yida.data.common.core.common.ResultBean;
import com.yida.data.common.core.entity.school.EduDormitory;
import com.yida.data.common.core.entity.school.EduDormitoryMaster;
import com.yida.data.common.core.entity.school.EduDormitoryRoom;
import com.yida.data.school.dto.index.ListDormRoomDTO;
import com.yida.data.school.facility.mapper.EduDormitoryRoomMapper;
import com.yida.data.school.facility.service.EduDormitoryMasterService;
import com.yida.data.school.facility.service.EduDormitoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 宿舍楼 Controller(不鉴权)
 *
 * @author wjm
 * @date 2021-09-14 17:42:11
 */
@Api(tags = "宿舍-宿舍楼")
@Slf4j
@Validated
@RestController
@RequestMapping("/in/facility/dorm")
@RequiredArgsConstructor
public class InEduDormitoryController {

    private final EduDormitoryService eduDormitoryService;

    private final EduDormitoryRoomMapper eduDormitoryRoomMapper;

    private final EduDormitoryMasterService eduDormitoryMasterService;

    @ApiOperation("获取宿舍楼")
    @GetMapping("/getDorm")
    ResultBean<EduDormitory> getDorm(@RequestParam("id") Long id) {
        return ResultBean.buildSuccess(eduDormitoryService.getDorm(id));
    }

    @ApiOperation("根据学校ID获取宿舍楼List")
    @GetMapping("/getDormListBySchoolId")
    ResultBean<List<EduDormitory>> getDormListBySchoolId(@RequestParam("schoolId") Long schoolId) {
        List<EduDormitory> list = eduDormitoryService
            .list(Wrappers.lambdaQuery(new EduDormitory()).eq(EduDormitory::getSchoolId, schoolId));

        list.forEach(eduDormitory -> {
            ListDormRoomDTO dto = new ListDormRoomDTO();
            dto.setDormIds(Collections.singletonList(eduDormitory.getId()));
            List<EduDormitoryRoom> eduDormitoryRooms = eduDormitoryRoomMapper.listDormRoom(dto);
            eduDormitory.setDormRoomList(eduDormitoryRooms);
            //宿管
            List<EduDormitoryMaster> masterList = eduDormitoryMasterService
                .list(Wrappers.lambdaQuery(new EduDormitoryMaster()).eq(EduDormitoryMaster::getSchoolId, schoolId)
                    .eq(EduDormitoryMaster::getDormitoryId, eduDormitory.getId()));
            if (CollUtil.isNotEmpty(masterList)) {
                eduDormitory.setMasterIds(masterList.stream().map(EduDormitoryMaster::getMasterId).collect(Collectors.toList()));
            }
        });
        return ResultBean.buildSuccess(list);
    }
}
