package com.srs.dormitory.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.srs.common.core.domain.AjaxResult;
import com.srs.dormitory.domain.DmsCampus;
import com.srs.dormitory.domain.DmsDormitoryBuilding;
import com.srs.dormitory.domain.DmsDormitoryPark;
import com.srs.dormitory.domain.Dto.ParkDto;
import com.srs.dormitory.mapper.DmsDormitoryParkMapper;
import com.srs.dormitory.service.IDmsCampusService;
import com.srs.dormitory.service.IDmsDormitoryBuildingInfoService;
import com.srs.dormitory.service.IDmsDormitoryBuildingService;
import com.srs.dormitory.service.IDmsDormitoryParkService;
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.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 园区表 服务实现类
 * </p>
 *
 * @author 猪油骨
 * @since 2023-07-14
 */
@Service
public class IDmsDormitoryParkServiceImpl extends ServiceImpl<DmsDormitoryParkMapper, DmsDormitoryPark> implements IDmsDormitoryParkService {
    @Autowired
    private IDmsDormitoryBuildingService dmsDormitoryBuildingService;
    @Autowired
    private IDmsCampusService dmsCampusService;
    /**
     * 添加园区
     * @param parkDto 请求参数
     * @param username 操作人
     * @return
     */
    @Override
    public AjaxResult addDmsDormitoryPark(ParkDto parkDto, String username) {
        if (parkDto.getName()==null){
            return AjaxResult.error("请输入园区名称");
        }
        if (parkDto.getCampusId() == null) {
            return AjaxResult.error("请输入校区ID");
        }
        DmsCampus one = dmsCampusService.lambdaQuery()
                .eq(DmsCampus::getId, parkDto.getCampusId())
                .one();
        if (one == null) return AjaxResult.error("校区ID不存在");
        DmsDormitoryPark park = new DmsDormitoryPark();
        BeanUtils.copyProperties(parkDto,park);
        park.setCreateBy(username);
        int insert = baseMapper.insert(park);
        if (insert>0) return AjaxResult.success();
        return AjaxResult.error();
    }

    /**
     * 查看园区列表
     * @param parkDto 请求参数
     * @return
     */
    @Override
    public List<Map<String,Object>> getDmsDormitoryParkList(ParkDto parkDto) {
        return baseMapper.getDormitoryParkList(parkDto);
    }

    /**
     * 修改园区信息
     * @param parkDto 请求参数
     * @param username
     * @return
     */
    @Override
    public AjaxResult updateDmsDormitoryPark(ParkDto parkDto, String username) {
        if (parkDto.getId()==null){
            return AjaxResult.error("请输入园区ID");
        }
        DmsDormitoryPark park = baseMapper.selectById(parkDto.getId());
        BeanUtils.copyProperties(parkDto,park);
        int i = baseMapper.updateById(park);
        if (i>0){
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    /**
     * 删除园区
     * @param ids 园区id列表
     * @return
     */
    @Override
    public AjaxResult deleteDmsDormitoryPark(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<DmsDormitoryBuilding> one = dmsDormitoryBuildingService.lambdaQuery()
                .eq(DmsDormitoryBuilding::getParkId, id)
                .list();
        return one.size()==0;
    }

    /**
     * 查看园区详细信息
     * @param id 园区ID
     * @return
     */
    @Override
    public AjaxResult getDmsDormitoryPark(Long id) {
        DmsDormitoryPark dormitoryPark = baseMapper.selectById(id);
        if (dormitoryPark==null) return AjaxResult.error("id不存在");
        Map<String, Object> map = BeanUtil.beanToMap(dormitoryPark);
        List<Long> buildingIds = new ArrayList<>();
        List<DmsDormitoryBuilding> list = dmsDormitoryBuildingService.lambdaQuery()
                .eq(DmsDormitoryBuilding::getParkId, id)
                .list();
        for (DmsDormitoryBuilding building : list){
            buildingIds.add(building.getId());
        }
        String createTime = map.get("createTime").toString();
        map.put("createTime",createTime.replace("T"," "));
        map.put("buildingList",list);
        Integer totalOccupancy = baseMapper.selectParkTotalOccupancy(id);
        map.put("occupancy",totalOccupancy);
        return AjaxResult.success("操作成功",map);
    }
}
