package com.sptg.estate.biz.service.impl;

import com.baomidou.mybatisplus.enums.SqlLike;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.beans.ResultEnum;
import com.sptg.common.core.constant.OperationConstant;
import com.sptg.estate.api.dto.ParkingSpaceDto;
import com.sptg.estate.api.entity.ParkingSpace;
import com.sptg.estate.api.entity.ParkingSpace;
import com.sptg.estate.api.entity.Residential;
import com.sptg.estate.biz.mapper.ParkingSpaceMapper;
import com.sptg.estate.biz.mapper.ParkingSpaceMapper;
import com.sptg.estate.biz.service.AgencyServiceInterface;
import com.sptg.estate.biz.service.ParkingSpaceServiceInterface;
import com.sptg.estate.biz.service.ResidentialServiceInterface;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Date;

@Service
@RequiredArgsConstructor
public class ParkingSpaceServiceImpl extends ServiceImpl<ParkingSpaceMapper, ParkingSpace> implements ParkingSpaceServiceInterface {

    private final ResidentialServiceInterface residentialServiceInterface;

    @Override
    public ResultBean<Page<ParkingSpace>> page(ParkingSpaceDto dto) {
        Wrapper<ParkingSpace> wrapper = new EntityWrapper<>();
        //过滤删除
        wrapper.eq("i_status", OperationConstant.ZERO);
        //根据时间日期
        wrapper.ge(dto.getBeginDate() != null,"t_create_date",dto.getBeginDate());
        wrapper.le(dto.getEndDate() !=null ,"t_create_date",dto.getEndDate());

        return new ResultBean<>(ResultEnum.SUCCESS,this.selectPage(dto.getPage(),wrapper));
    }

    @Override
    public ResultBean<Boolean> add(ParkingSpaceDto dto) {
        //小区
        Residential residential = this.residentialServiceInterface.findResidentialById(dto.getResidentialId());
        Assert.notNull(residential,"选择的小区不存在");
        dto.setResidentialName(residential.getName());
        dto.setOrgPosition(residential.getOrgPosition());
        dto.setCreateTime(new Date());
        return new ResultBean<>(ResultEnum.SUCCESS,this.insert(dto));
    }

    @Override
    public ResultBean<Boolean> edit(ParkingSpaceDto dto) {
        return new ResultBean<>(ResultEnum.SUCCESS,this.updateById(dto));
    }

    @Override
    public ResultBean<Boolean> delete(ParkingSpaceDto dto) {
        ParkingSpace ParkingSpace = this.selectById(dto.getId());
        Assert.notNull(ParkingSpace,"选择的车位不存在");
        ParkingSpace.setStatus(OperationConstant.MINUS_ONE);
        ParkingSpace.setUpdateTime(new Date());
        return new ResultBean<>(ResultEnum.SUCCESS,this.updateById(ParkingSpace));
    }
}
