package com.srs.dormitory.service.impl;

import com.srs.common.core.domain.AjaxResult;
import com.srs.dormitory.domain.DmsDormitory;
import com.srs.dormitory.domain.DmsDormitoryFloor;
import com.srs.dormitory.domain.Dto.FloorDto;
import com.srs.dormitory.mapper.DmsDormitoryFloorMapper;
import com.srs.dormitory.service.IDmsDormitoryFloorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.srs.dormitory.service.IDmsDormitoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * <p>
 * 楼层表 服务实现类
 * </p>
 *
 * @author 猪油骨
 * @since 2023-07-14
 */
@Service
public class IDmsDormitoryFloorServiceImpl extends ServiceImpl<DmsDormitoryFloorMapper, DmsDormitoryFloor> implements IDmsDormitoryFloorService {
    @Autowired
    private IDmsDormitoryService dmsDormitoryService;
    /**
     * 添加宿舍楼层
     * @param floorDto 请求参数
     * @param username 操作人
     * @return
     */
    @Override
    public AjaxResult addDmsDormitoryFloor(FloorDto floorDto, String username) {
        if (floorDto.getFloor() == null) {
            return AjaxResult.error("请输入楼层号");
        }
        if (floorDto.getBuildingId() == null) {
            return AjaxResult.error("请输入宿舍楼ID");
        }
        DmsDormitoryFloor dormitoryFloor = new DmsDormitoryFloor();
        dormitoryFloor.setCreateBy(username);
        dormitoryFloor.setFloor(floorDto.getFloor());
        dormitoryFloor.setRemark(floorDto.remark);
        dormitoryFloor.setBuildingId(floorDto.getBuildingId());
        int insert = baseMapper.insert(dormitoryFloor);
        if (insert>0){
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    /**
     * 修改宿舍楼层信息
     * @param floorDto 请求参数
     * @param username 操作人
     * @return
     */
    @Override
    public AjaxResult updateDmsDormitoryFloor(FloorDto floorDto, String username) {
        if (floorDto.getId()==null){
            return AjaxResult.error("请输入楼层ID");
        }
        DmsDormitoryFloor dormitoryFloor = baseMapper.selectById(floorDto.getId());
        if (dormitoryFloor == null) {
            return AjaxResult.error("楼层ID不存在");
        }
        BeanUtils.copyProperties(floorDto,dormitoryFloor);
        dormitoryFloor.setUpdateBy(username);
        baseMapper.updateById(dormitoryFloor);
        return AjaxResult.success("操作成功",dormitoryFloor.getBuildingId());
    }

    /**
     * 批量删除宿舍楼层
     * @param ids 宿舍楼层id列表
     * @return
     */
    @Transactional
    @Override
    public AjaxResult batchDeleteDmsDormitoryFloor(List<Long> ids) {
        for (Long id : ids){
            if (!check(id)){
                return AjaxResult.error("请先删除id为"+id+"楼层对应的宿舍楼");
            }
        }
        int i = baseMapper.deleteBatchIds(ids);
        if (i>0){
            return AjaxResult.success();
        }
        throw new RuntimeException();
    }

    /**
     * 校验该楼层下是否还有宿舍
     * @param id 楼层id
     * @return
     */
    boolean check(Long id){
        List<DmsDormitory> dormitory = dmsDormitoryService.lambdaQuery()
                .eq(DmsDormitory::getFloorId, id)
                .list();
        return dormitory.size()==0;
    }

    /**
     * 查看楼层详细信息
     * @param id 楼层id
     * @return
     */
    @Override
    public AjaxResult getDmsDormitoryFloorInfo(Long id) {
        Map<String, Object> floor = baseMapper.selectFloorById(id);
        if (floor==null) return AjaxResult.error("id不存在");
        return AjaxResult.success(floor);
    }

    /**
     * 查看楼层列表
     * @param buildingId 宿舍楼id
     * @return
     */
    @Override
    public List<Map<String, Object>> getDormitoryFloorList(Long buildingId) {
        return baseMapper.getDormitoryFloorList(buildingId);
    }
}
