package com.dcsoft.system.vehicle.service.impl;

import com.dcsoft.common.core.utils.CollUtil;
import com.dcsoft.common.core.utils.DateUtils;
import com.dcsoft.system.vehicle.domain.CarPark;
import com.dcsoft.system.vehicle.mapper.CarParkMapper;
import com.dcsoft.system.vehicle.service.ICarParkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 车场信息Service业务层处理
 *
 * @author nichun
 * @date 2023-12-27
 */
@Service
public class CarParkServiceImpl implements ICarParkService
{
    @Autowired
    private CarParkMapper carParkMapper;

    /**
     * 查询车场信息
     *
     * @param id 车场信息主键
     * @return 车场信息
     */
    @Override
    public CarPark selectCarParkById(Long id)
    {
        return carParkMapper.selectCarParkById(id);
    }

    /**
     * 查询车场信息列表
     *
     * @param carPark 车场信息
     * @return 车场信息
     */
    @Override
    public List<CarPark> selectCarParkList(CarPark carPark) {
        List<CarPark> carParks = carParkMapper.selectCarParkList(carPark);

        List<Long> collect = carParks.stream().map(CarPark::getParentId).filter(parentId -> parentId != 0).collect(Collectors.toList());
        if (CollUtil.isNotEmpty(collect)) {
            List<CarPark> parkList = carParkMapper.selectByIds(collect);
            for (CarPark park : parkList) {
                for (CarPark carPark1 : carParks) {
                    if (park.getId().equals(carPark1.getParentId())) {
                        carPark1.setParentIdName(park.getName());
                    }
                }
            }
        }

        return carParks;
    }

    /**
     * 新增车场信息
     *
     * @param carPark 车场信息
     * @return 结果
     */
    @Override
    public int insertCarPark(CarPark carPark)
    {
        carPark.setCreateTime(DateUtils.getNowDate());
        return carParkMapper.insertCarPark(carPark);
    }

    /**
     * 修改车场信息
     *
     * @param carPark 车场信息
     * @return 结果
     */
    @Override
    public int updateCarPark(CarPark carPark)
    {
        carPark.setUpdateTime(DateUtils.getNowDate());
        return carParkMapper.updateCarPark(carPark);
    }

    /**
     * 批量删除车场信息
     *
     * @param ids 需要删除的车场信息主键
     * @return 结果
     */
    @Override
    public int deleteCarParkByIds(Long[] ids)
    {
        return carParkMapper.deleteCarParkByIds(ids);
    }

    /**
     * 删除车场信息信息
     *
     * @param id 车场信息主键
     * @return 结果
     */
    @Override
    public int deleteCarParkById(Long id)
    {
        return carParkMapper.deleteCarParkById(id);
    }

    @Override
    public List<CarPark> getCarParkSelect(Long id) {
        return carParkMapper.getCarParkSelect(id);
    }
}
