package com.srs.dormitory.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.srs.common.core.domain.AjaxResult;
import com.srs.dormitory.domain.DmsDormitory;
import com.srs.dormitory.domain.DmsDormitoryUnit;
import com.srs.dormitory.domain.Dto.UnitDto;
import com.srs.dormitory.mapper.DmsDormitoryUnitMapper;
import com.srs.dormitory.service.IDmsDormitoryService;
import com.srs.dormitory.service.IDmsDormitoryUnitService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 单元表 服务实现类
 * </p>
 *
 * @author 猪油骨
 * @since 2023-07-14
 */
@Service
public class IDmsDormitoryUnitServiceImpl extends ServiceImpl<DmsDormitoryUnitMapper, DmsDormitoryUnit> implements IDmsDormitoryUnitService {
    @Autowired
    private IDmsDormitoryService dmsDormitoryService;

    /**
     * 添加单元
     *
     * @param unitDto  请求参数
     * @param username 操作人
     * @return
     */
    @Override
    public AjaxResult addDmsDormitoryUnit(UnitDto unitDto, String username) {
        if (unitDto.getName() == null) {
            return AjaxResult.error("请输入单元名称");
        }
        if (unitDto.getBuildingId() == null) {
            return AjaxResult.error("请输入宿舍楼id");
        }
        DmsDormitoryUnit dmsDormitoryUnit = new DmsDormitoryUnit();
        dmsDormitoryUnit.setCreateBy(username);
        BeanUtils.copyProperties(unitDto, dmsDormitoryUnit);
        int insert = baseMapper.insert(dmsDormitoryUnit);
        if (insert > 0) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    /**
     * 查看单元列表
     *
     * @param unitDto 请求参数
     * @return
     */
    @Override
    public List<Map<String, Object>> getDmsDormitoryUnitList(UnitDto unitDto) {
        return baseMapper.getList(unitDto);
    }

    /**
     * 修改宿舍单元
     *
     * @param unitDto 请求参数
     * @return
     */
    @Override
    public AjaxResult updateDmsDormitoryUnit(UnitDto unitDto, String username) {
        if (unitDto.getId() == null) {
            return AjaxResult.error("请输入单元ID");
        }
        DmsDormitoryUnit unit = baseMapper.selectById(unitDto.getId());
        BeanUtils.copyProperties(unitDto, unit);
        unit.setUpdateBy(username);
        int i = baseMapper.updateById(unit);
        if (i > 0) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    /**
     * 批量删除宿舍单元
     *
     * @param ids 单元id列表
     * @return
     */
    @Override
    public AjaxResult batchDeleteDmsDormitoryUnit(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();
        }
        return AjaxResult.error();
    }

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

    /**
     * 查看单元详情
     *
     * @param id 单元id
     * @return
     */
    @Override
    public AjaxResult getDmsDormitoryUnitInfo(Long id) {
        Map<String, Object> unit = baseMapper.selectUnitById(id);
        if (unit == null) return AjaxResult.error("id不存在");
        return AjaxResult.success(unit);
    }
}
