package cn.iocoder.yudao.module.system.service.parkingspot;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.system.controller.admin.parkingspot.vo.ParkingSpotPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.parkingspot.vo.ParkingSpotSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.parkingspot.ParkingSpotDO;
import cn.iocoder.yudao.module.system.dal.mysql.parkingspot.ParkingSpotMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

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

import static cn.hutool.core.collection.CollUtil.isNotEmpty;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;

/**
 * 停车点 Service 实现类
 *
 * @author 艾楷
 */
@Service
@Validated
public class ParkingSpotServiceImpl extends ServiceImpl<ParkingSpotMapper, ParkingSpotDO> implements ParkingSpotService {

    @Resource
    private ParkingSpotMapper parkingSpotMapper;

    @Override
    public Long createParkingSpot(ParkingSpotSaveReqVO createReqVO) {
        // 插入
        ParkingSpotDO parkingSpot = BeanUtils.toBean(createReqVO, ParkingSpotDO.class);
        parkingSpotMapper.insert(parkingSpot);
        // 返回
        return parkingSpot.getId();
    }

    @Override
    public void updateParkingSpot(ParkingSpotSaveReqVO updateReqVO) {
        // 更新
        ParkingSpotDO updateObj = BeanUtils.toBean(updateReqVO, ParkingSpotDO.class);
        parkingSpotMapper.updateById(updateObj);
    }

    @Override
    public void deleteParkingSpot(Long id) {
        // 删除
        parkingSpotMapper.deleteById(id);
    }


    @Override
    public ParkingSpotDO getParkingSpot(Long id) {
        return parkingSpotMapper.selectById(id);
    }

    @Override
    public PageResult<ParkingSpotDO> getParkingSpotPage(ParkingSpotPageReqVO pageReqVO) {
        return parkingSpotMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ParkingSpotDO> getByMapId(Long positionMapId) {
        return parkingSpotMapper.selectList(new LambdaQueryWrapperX<ParkingSpotDO>()
                .eq(ParkingSpotDO::getPositionMapId, positionMapId));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchSaveOrEditOrDel(Long positionMapId, List<List<ParkingSpotDO>> list) {
        //批量添加、修改、删除
        if (isNotEmpty(list.get(0))) {
            parkingSpotMapper.insertBatch(list.get(0));
        }
        if (isNotEmpty(list.get(1))) {
            parkingSpotMapper.updateBatch(list.get(1));
        }
        if (isNotEmpty(list.get(2))) {
            parkingSpotMapper.deleteByIds(convertList(list.get(2), ParkingSpotDO::getId));
        }
    }

    @Override
    public void deleteParkingByMapId(Long mapId) {
        parkingSpotMapper.deleteParkingByMapId(mapId);
    }

}
