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.PmgBatParkFileRelevance;
import com.yuchen.saas.park.api.entity.park.PmgBatParkFloorInfo;
import com.yuchen.saas.park.api.entity.park.PmgBatParkRoomInfo;
import com.yuchen.saas.park.api.vo.park.PmgBatParkFloorInfoVo;
import com.yuchen.saas.park.api.vo.park.PmgbatParkRoomVo;
import com.yuchen.saas.park.api.vo.park.RoomVo;
import com.yuchen.saas.park.service.mapper.PmgBatParkFloorInfoMapper;
import com.yuchen.saas.park.service.service.PmgBatParkFloorInfoService;
import com.yuchen.saas.park.service.service.PmgBatParkFileInfoService;
import com.yuchen.saas.park.service.service.PmgBatParkFileRelevanceService;
import com.yuchen.saas.park.service.service.PmgBatParkRoomInfoService;
import org.nest.springwrap.core.mp.base.BaseServiceImpl;
import org.nest.springwrap.core.tool.api.R;
import org.nest.springwrap.core.tool.utils.Func;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 刘碧清
 * @date 2021/04/26 10:48
 */
@Service
public class PmgBatParkFloorInfoServiceImpl extends BaseServiceImpl<PmgBatParkFloorInfoMapper, PmgBatParkFloorInfo> implements PmgBatParkFloorInfoService {
    @Resource
    PmgBatParkRoomInfoService pmgBatParkRoomInfoService;
    @Resource
    PmgBatParkFileRelevanceService pmgBatParkFileRelevanceService;
    @Resource
    PmgBatParkFileInfoService pmgBatParkFileInfoService;


    /**
     * 获取数据
     *
     * @param
     * @return
     */
    @Override
    public List<PmgBatParkFloorInfo> getAll(RoomVo roomVo) {
        List<PmgBatParkFloorInfo> floorInfos = baseMapper.getAll(roomVo.getBuildingIds());
        if (floorInfos.size() > 0) {
            floorInfos.forEach(s -> {
                List<PmgbatParkRoomVo> roomList = pmgBatParkRoomInfoService.getList(s.getId(), roomVo);
                s.setList(roomList);
            });
        }
        return floorInfos;
    }

    /**
     * 楼层添加数据
     *
     * @param pmgBatParkFloorInfoEntities
     * @return
     */
    @Override
    public R submit(List<PmgBatParkFloorInfoVo> pmgBatParkFloorInfoEntities) {
        if (Func.isNotEmpty(pmgBatParkFloorInfoEntities)
                && pmgBatParkFloorInfoEntities.size() > 0) {
            for (PmgBatParkFloorInfoVo pmgBatParkFloorInfoEntity : pmgBatParkFloorInfoEntities) {
                PmgBatParkFloorInfo pmgBatParkFloorInfo = new PmgBatParkFloorInfo();
                pmgBatParkFloorInfo.setBuildingId(pmgBatParkFloorInfoEntity.getBuildingId());
                pmgBatParkFloorInfo.setFloorName(pmgBatParkFloorInfoEntity.getFloorName());
                pmgBatParkFloorInfo.setFloorNum(pmgBatParkFloorInfoEntity.getFloorName());
                pmgBatParkFloorInfo.setLMark(pmgBatParkFloorInfoEntity.getLMark());
                // 判断楼层名字和唯一标识是否重复
                LambdaQueryWrapper<PmgBatParkFloorInfo> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(PmgBatParkFloorInfo::getBuildingId,pmgBatParkFloorInfo.getBuildingId());
                wrapper.nested(qw -> qw.eq(PmgBatParkFloorInfo::getLMark,pmgBatParkFloorInfo.getLMark()).or().eq(PmgBatParkFloorInfo::getFloorName,
                        pmgBatParkFloorInfo.getFloorName()));
                if (count(wrapper) > 0){
                    return R.error("楼层名称或楼层标识重复！");
                }
                //新增楼层
                boolean savePmgBatParkFloorInfo = saveOrUpdate(pmgBatParkFloorInfo);
                if (!savePmgBatParkFloorInfo) {
                    return R.error("楼层保存失败！");
                }
                PmgBatParkFileRelevance pmgBatParkFileRelevance = new PmgBatParkFileRelevance();
                pmgBatParkFileRelevance.setFileId(pmgBatParkFloorInfoEntity.getFileId());
                pmgBatParkFileRelevance.setRelevanceId(pmgBatParkFloorInfo.getId());
                pmgBatParkFileRelevance.setParentId(pmgBatParkFloorInfoEntity.getParkId());
                pmgBatParkFileRelevance.setTypeId(2L);
                //新增文件关联
                boolean savePmgBatParkFileRelevance = pmgBatParkFileRelevanceService.saveOrUpdate(pmgBatParkFileRelevance);
                if (!savePmgBatParkFileRelevance) {
                    return R.error("文件关联失败！");
                }
            }
        }
        return R.data("保存成功！");
    }

    @Override
    public boolean del(List<Long> ids) {
        if (deleteLogic(ids)){
            pmgBatParkRoomInfoService.remove(Wrappers.<PmgBatParkRoomInfo>lambdaQuery().in(PmgBatParkRoomInfo::getFloorId, ids));
            return true;
        }
        return false;
    }
}
