/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.estate.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import org.springblade.core.mp.support.Condition;
import org.springblade.estate.entity.ParkingArea;
import org.springblade.estate.entity.ParkingSpace;
import org.springblade.estate.service.IParkingSpaceService;
import org.springblade.estate.vo.ParkingAreaVO;
import org.springblade.estate.mapper.ParkingAreaMapper;
import org.springblade.estate.service.IParkingAreaService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.estate.vo.ParkingLayoutVO;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.transaction.annotation.Transactional;

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

/**
 *  服务实现类
 *
 * @author BladeX
 * @since 2022-01-11
 */
@Service
public class ParkingAreaServiceImpl extends BaseServiceImpl<ParkingAreaMapper, ParkingArea> implements IParkingAreaService {

	@Resource
	private IParkingSpaceService parkingSpaceService;

	@Override
	public IPage<ParkingAreaVO> selectParkingAreaPage(IPage<ParkingAreaVO> page, ParkingAreaVO parkingArea) {
		return page.setRecords(baseMapper.selectParkingAreaPage(page, parkingArea));
	}

    @Override
	@Transactional(rollbackFor = Exception.class)
    public void saveLayout(List<ParkingLayoutVO> parkingLayoutVOList) {
		for (ParkingLayoutVO parkingLayoutVO : parkingLayoutVOList) {
			ParkingArea parkingArea = new ParkingArea();
			BeanUtil.copyProperties(parkingLayoutVO,parkingArea);
			this.saveOrUpdate(parkingArea);
			List<ParkingSpace> parkingSpaceList = parkingLayoutVO.getParkingSpaceList();
			this.saveParkingSpace(parkingSpaceList,parkingArea.getId());
		}
    }

	@Override
	public List<ParkingLayoutVO> getLayout(Long parkingLotId) {
		List<ParkingLayoutVO> parkingLayoutVOList = new ArrayList<>();
		//根据车场id获取车场区域
		List<ParkingArea> parkingAreas = this.list(Condition.getQueryWrapper(new ParkingArea()).lambda().eq(ParkingArea::getParkingLotId, parkingLotId));
		if (CollUtil.isEmpty(parkingAreas)) {
			return parkingLayoutVOList;
		}
		//遍历区域获取车位列表
		for (ParkingArea parkingArea : parkingAreas) {
			ParkingLayoutVO parkingLayoutVO = new ParkingLayoutVO();
			BeanUtil.copyProperties(parkingArea,parkingLayoutVO);
			//获取车位列表
			List<ParkingSpace> parkingSpaceList = parkingSpaceService.list(Condition.getQueryWrapper(new ParkingSpace()).lambda()
				.eq(ParkingSpace::getParkingLotId, parkingLotId)
				.eq(ParkingSpace::getParkingAreaId, parkingArea.getId()));
			parkingLayoutVO.setParkingSpaceList(parkingSpaceList);
			parkingLayoutVOList.add(parkingLayoutVO);
		}
		return parkingLayoutVOList;
	}



	private void saveParkingSpace(List<ParkingSpace> parkingSpaceList,Long parkingAreaId) {
		if (CollUtil.isEmpty(parkingSpaceList)) {
			return;
		}
		parkingSpaceList.stream().forEach(item->item.setParkingAreaId(parkingAreaId));
		boolean b = parkingSpaceService.saveOrUpdateBatch(parkingSpaceList);
		if (!b) {
			throw new RuntimeException("批量保存车位信息失败");
		}
	}

}
