package com.yuchen.saas.park.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yuchen.saas.park.api.entity.park.ParkSpaceInfo;
import com.yuchen.saas.park.api.enums.SpaceLevelTypeEnum;
import com.yuchen.saas.park.api.enums.SpaceTypeEnum;
import com.yuchen.saas.park.api.request.SpaceInfoRequest;
import com.yuchen.saas.park.api.vo.park.ParkSpaceInfoTreeVO;
import com.yuchen.saas.park.service.mapper.ParkSpaceInfoMapper;
import com.yuchen.saas.park.service.service.ParkSpaceInfoService;
import org.nest.springwrap.core.mp.base.BaseServiceImpl;
import org.nest.springwrap.core.tool.api.R;
import org.nest.springwrap.core.tool.node.ForestNodeMerger;
import org.nest.springwrap.core.tool.utils.BeanUtil;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @ClassName ParkSpaceInfoServiceImpl
 * @Description 园区空间信息操作接口实现
 * @Author 张逸飞
 * @Date 2022/10/26 15:39
 */
@Service
public class ParkSpaceInfoServiceImpl extends BaseServiceImpl<ParkSpaceInfoMapper, ParkSpaceInfo> implements ParkSpaceInfoService {

    @Override
    public R<Boolean> saveSpaceInfo(ParkSpaceInfo parkSpaceInfo) {
        //1、层只能在地面层、建筑下创建；所属空间，根据创建类型过滤
        if(Objects.equals(parkSpaceInfo.getType(), SpaceTypeEnum.LEVEL.getCode())){
           ParkSpaceInfo spaceInfo = getById(parkSpaceInfo.getId());
           if(spaceInfo != null && !(Objects.equals(spaceInfo.getLevelType(), SpaceLevelTypeEnum.FLOOR_LAYER.getCode()) ||
                   Objects.equals(spaceInfo.getType(), SpaceTypeEnum.BUILDING.getCode()))){
             return R.fail("层只能在地面层、建筑下创建");
           }
        }
        save(parkSpaceInfo);
        return R.success("操作成功");
    }

    @Override
    public Boolean updateSpaceInfo(ParkSpaceInfo parkSpaceInfo) {
        updateById(parkSpaceInfo);
        return true;
    }

    @Override
    public List<ParkSpaceInfoTreeVO> getTree(SpaceInfoRequest spaceInfoRequest) {
        // 获取数据
        LambdaQueryWrapper<ParkSpaceInfo> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(Objects.nonNull(spaceInfoRequest.getParkId()),ParkSpaceInfo::getParkId,spaceInfoRequest.getParkId());
        queryWrapper.eq(Objects.nonNull(spaceInfoRequest.getType()),ParkSpaceInfo::getType,spaceInfoRequest.getType());
        queryWrapper.eq(Objects.nonNull(spaceInfoRequest.getTenantId()),ParkSpaceInfo::getTenantId,spaceInfoRequest.getTenantId());
        queryWrapper.orderByDesc(ParkSpaceInfo::getSort);
        List<ParkSpaceInfo> list = list(queryWrapper);
        // 进行类型转换
        List<ParkSpaceInfoTreeVO> collect = list.stream().map(dict -> BeanUtil.copy(dict, ParkSpaceInfoTreeVO.class)).collect(Collectors.toList());
        // 形成树结构并返回
        return ForestNodeMerger.merge(collect);
    }

    @Override
    public R delSpaceInfo(Long id) {
        // 判断是否有子节点
        LambdaQueryWrapper<ParkSpaceInfo> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ParkSpaceInfo::getParentId,id);
        Integer count = count(queryWrapper);
        if (count > 0){
            return R.error("请先删除子节点");
        }
        return R.status(removeById(id));
    }

    @Override
    public R batchSave(SpaceInfoRequest spaceInfoRequest) {
        if(spaceInfoRequest.getCount() == 0){
          return R.fail("创建数量必须大于等于1");
        }
        List<ParkSpaceInfo> spaceInfoList = new ArrayList<>();
        for (int i = 0; i < spaceInfoRequest.getCount(); i++) {
            ParkSpaceInfo parkSpaceInfo = new ParkSpaceInfo();
            parkSpaceInfo.setTenantId(spaceInfoRequest.getTenantId().toString());
            parkSpaceInfo.setParkId(spaceInfoRequest.getParkId());
            parkSpaceInfo.setType(spaceInfoRequest.getType());
            parkSpaceInfo.setParentId(spaceInfoRequest.getParentId());
            if(Objects.nonNull(spaceInfoRequest.getUseTypeId())){
                parkSpaceInfo.setUseTypeId(spaceInfoRequest.getUseTypeId());
            }
            if(Objects.nonNull(spaceInfoRequest.getLevelType())){
                parkSpaceInfo.setLevelType(spaceInfoRequest.getLevelType());
            }
            parkSpaceInfo.setSort(i);
            parkSpaceInfo.setName("未命名_"+i);
            spaceInfoList.add(parkSpaceInfo);
        }
        saveBatch(spaceInfoList);
        return R.success("操作成功");
    }
}
