package com.srs.dormitory.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.srs.common.core.domain.AjaxResult;
import com.srs.dormitory.domain.*;
import com.srs.dormitory.domain.Dto.AddMajorDto;
import com.srs.dormitory.domain.Dto.EmptyDormitoryDto;
import com.srs.dormitory.domain.Vo.UnassignedVo;
import com.srs.dormitory.mapper.DmsMajorDormitoryMapper;
import com.srs.dormitory.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.srs.common.utils.PageUtils.startPage;

/**
 * <p>
 * 专业宿舍中间关联表 服务实现类
 * </p>
 *
 * @author 猪油骨
 * @since 2023-08-01
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class IDmsMajorDormitoryServiceImpl extends ServiceImpl<DmsMajorDormitoryMapper, DmsMajorDormitory> implements IDmsMajorDormitoryService {
    private final IDmsDormitoryBuildingInfoService dmsDormitoryBuildingInfoService;
    private final IDmsDormitoryUnitService unitService;
    private final IDmsDormitoryFloorService floorService;

    @Override
    public AjaxResult addMajor(AddMajorDto dto) {

        for (Long dormitoryId : dto.getDormitoryIds()) {
            DmsMajorDormitory majorDormitory = DmsMajorDormitory
                    .builder()
                    .majorId(dto.getMajorId())
                    .dormitoryId(dormitoryId)
                    .build();
            baseMapper.insert(majorDormitory);
        }
        return AjaxResult.success();
    }

    @Override
    public List<UnassignedVo> unassigned(Long deptId, Long buildingId, Long gradeId) {
        DmsDormitoryBuildingInfo building = dmsDormitoryBuildingInfoService
                .lambdaQuery()
                .eq(DmsDormitoryBuildingInfo::getBuildingId, buildingId)
                .one();
        return baseMapper.selectUnassigned(deptId, building.getGender(), gradeId);
    }

    @Override
    public List<Map<String, Object>> getUnit(Long buildingId) {
        List<DmsDormitoryUnit> units = unitService.lambdaQuery().eq(DmsDormitoryUnit::getBuildingId, buildingId).list();
        List<Map<String, Object>> maps = new ArrayList<>();
        for (DmsDormitoryUnit unit : units) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("unitId", unit.getId());
            map.put("name", unit.getName());
            maps.add(map);
        }
        return maps;
    }

    @Override
    public List<Map<String, Object>> getFloor(Long buildingId) {
        List<DmsDormitoryFloor> floors = floorService.lambdaQuery().eq(DmsDormitoryFloor::getBuildingId, buildingId).list();
        List<Map<String, Object>> maps = new ArrayList<>();
        for (DmsDormitoryFloor floor : floors) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("floorId", floor.getId());
            map.put("floor", floor.getFloor());
            maps.add(map);
        }
        return maps;
    }

    @Override
    public List<Map<String, Object>> getDormitory(EmptyDormitoryDto dto) {

        QueryWrapper<Map<String, Object>> wrapper = new QueryWrapper<>();
        wrapper
                .eq("b.id", dto.getBuildingId())
                .eq("occupancy", 0)
                .notInSql("d.id", "SELECT dormitory_id FROM dms_major_dormitory");
        if (dto.getUnitId() != null) {
            wrapper.eq("u.id", dto.getUnitId());
        }
        if (dto.getFloorId() != null) {
            wrapper.eq("f.id", dto.getFloorId());
        }

        startPage();
        return baseMapper.selectDormitory(wrapper);
    }

}
