package com.srs.dormitory.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.srs.common.core.domain.AjaxResult;
import com.srs.common.core.domain.entity.SysDept;
import com.srs.dormitory.domain.DmsBuildingDept;
import com.srs.dormitory.domain.DmsDormitoryBuilding;
import com.srs.dormitory.domain.Dto.DeptBuildingDto;
import com.srs.dormitory.domain.Vo.DeptVo;
import com.srs.dormitory.domain.Vo.EmptyDormitoryVo;
import com.srs.dormitory.domain.Vo.GenderVo;
import com.srs.dormitory.domain.Vo.GendersVo;
import com.srs.dormitory.mapper.DmsBuildingDeptMapper;
import com.srs.dormitory.service.IDmsBuildingDeptService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.srs.dormitory.service.IDmsDormitoryBuildingService;
import com.srs.dormitory.utils.BeanCopyUtils;
import com.srs.system.service.ISysDeptService;
import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * <p>
 * 宿舍楼和部门关系中间表 服务实现类
 * </p>
 *
 * @author 猪油骨
 * @since 2023-07-31
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class IDmsBuildingDeptServiceImpl extends ServiceImpl<DmsBuildingDeptMapper, DmsBuildingDept> implements IDmsBuildingDeptService {

    private final IDmsDormitoryBuildingService dmsDormitoryBuildingService;
    private final ISysDeptService deptService;

    @Override
    @Transactional
    public AjaxResult addDept(DeptBuildingDto dto) {
        dto.getBuildingIds().forEach(b -> {
            DmsBuildingDept dmsBuildingDept = new DmsBuildingDept();
            dmsBuildingDept.setBuildingId(b);
            dmsBuildingDept.setDeptId(dto.getDeptId());
            baseMapper.insert(dmsBuildingDept);
        });
        return AjaxResult.success();
    }

    @Override
    public List<GendersVo> genderCount(Long gradeId) {
        List<GendersVo> deptIds = baseMapper.selectDeptId();
        for (GendersVo vo : deptIds) {
            List<GenderVo> genderVos = baseMapper.selectGenderCount(vo.getDeptId(), gradeId);
            vo.setGender(genderVos);
        }
        return deptIds;
    }

    @Override
    public List<EmptyDormitoryVo> emptyDormitory() {

        List<DmsDormitoryBuilding> list = dmsDormitoryBuildingService.list();
        List<EmptyDormitoryVo> voList = new ArrayList<>();
        for (DmsDormitoryBuilding building : list) {
            EmptyDormitoryVo vo = baseMapper.selectEmptyDormitory(building.getId());
            voList.add(vo);
        }
        return voList;
    }

    @Override
    public List<DeptVo> buildingAllocation() {
        List<SysDept> depts = deptService.list(new LambdaQueryWrapper<SysDept>().select(SysDept::getDeptId, SysDept::getDeptName));
        List<DeptVo> vos = BeanCopyUtils.copyBeanList(depts, DeptVo.class);
        for (DeptVo vo : vos) {
            List<Map<String, Object>> maps = baseMapper.selectBuilding(vo.getDeptId());
            vo.setBuildings(maps);
        }
        return vos;
    }

}
