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

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuchen.saas.park.api.entity.park.ParkingSpaceEntity;
import com.yuchen.saas.park.api.request.ParkingSpaceRequest;
import com.yuchen.saas.park.api.response.ParkingSpaceResponse;
import com.yuchen.saas.park.api.response.SpaceStaticResponse;
import com.yuchen.saas.park.service.mapper.ParkingSpaceMapper;
import com.yuchen.saas.park.service.service.ParkingSpaceService;
import com.yuchen.saas.park.service.service.PmgBatParkInfoService;
import org.nest.springwrap.core.mp.base.BaseServiceImpl;
import org.nest.springwrap.core.mp.support.Query;
import org.nest.springwrap.core.secure.utils.AuthUtil;
import org.nest.springwrap.core.tool.api.R;
import org.nest.springwrap.core.tool.utils.Func;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author 肖祥
 * @create 2022/11/29 14:49
 */
@Service
public class ParkingSpaceServiceImpl extends BaseServiceImpl<ParkingSpaceMapper, ParkingSpaceEntity> implements ParkingSpaceService {
    @Resource
    private PmgBatParkInfoService pmgBatParkInfoService;

    @Override
    public R<IPage<ParkingSpaceResponse>> list(ParkingSpaceRequest parkingSpaceRequest, Query query) {
        IPage<ParkingSpaceResponse> iPage = new Page<>(query.getCurrent(),query.getSize());
        String tenantId= AuthUtil.getUser().getTenantId();
        IPage<ParkingSpaceResponse> pageVo = baseMapper.listPage(iPage,parkingSpaceRequest,tenantId,parkingSpaceRequest.getParkId());
        return R.data(pageVo);
    }

    @Override
    public R<SpaceStaticResponse> spaceStatics(ParkingSpaceRequest parkingSpaceRequest) {
        String tenantId= AuthUtil.getUser().getTenantId();
        SpaceStaticResponse spaceStaticResponse = baseMapper.spaceStatics(parkingSpaceRequest);
        return R.data(spaceStaticResponse);
    }

    @Override
    public R save(ParkingSpaceRequest parkingSpaceRequest) {
        ParkingSpaceEntity parkingSpaceEntity = new ParkingSpaceEntity();
        BeanUtils.copyProperties(parkingSpaceRequest,parkingSpaceEntity);
        save(parkingSpaceEntity);
        return R.success("操作成功");
    }

    @Override
    public R update(ParkingSpaceRequest parkingSpaceRequest) {
        ParkingSpaceEntity parkingSpaceEntity = new ParkingSpaceEntity();
        BeanUtils.copyProperties(parkingSpaceRequest,parkingSpaceEntity);
        updateById(parkingSpaceEntity);
        return R.success("操作成功");
    }

    @Override
    public R remove(ParkingSpaceRequest parkingSpaceRequest) {
        deleteLogic(Func.toLongList(parkingSpaceRequest.getId().toString()));
        return R.success("操作成功");
    }

    @Override
    public R<ParkingSpaceResponse> detail(Long id) {
        ParkingSpaceEntity parkingSpaceEntity =  getById(id);
        ParkingSpaceResponse parkingSpaceResponse = new ParkingSpaceResponse();
        BeanUtils.copyProperties(parkingSpaceEntity,parkingSpaceResponse);
        return R.data(parkingSpaceResponse);
    }
}
