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.ParkingLotDto;
import com.sptg.estate.api.entity.Agency;
import com.sptg.estate.api.entity.ParkingLot;
import com.sptg.estate.api.entity.ParkingLot;
import com.sptg.estate.api.entity.Residential;
import com.sptg.estate.biz.mapper.ParkingLotMapper;
import com.sptg.estate.biz.mapper.ParkingLotMapper;
import com.sptg.estate.biz.service.AgencyServiceInterface;
import com.sptg.estate.biz.service.ParkingLotServiceInterface;
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 ParkingLotServiceImpl extends ServiceImpl<ParkingLotMapper, ParkingLot> implements ParkingLotServiceInterface {

    private final AgencyServiceInterface agencyServiceInterface;
    private final ResidentialServiceInterface residentialServiceInterface;

    @Override
    public ResultBean<Page<ParkingLot>> page(ParkingLotDto dto) {
        Wrapper<ParkingLot> 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(ParkingLotDto dto) {
        //社区
        Agency agency = this.agencyServiceInterface.selectById(dto.getAgencyId());
        Assert.notNull(agency,"选择的社区不存在");
        dto.setAgencyName(agency.getAgencyName());
        //小区
        Residential residential = this.residentialServiceInterface.selectById(dto.getResidentialId());
        Assert.notNull(residential,"选择的小区不存在");
        dto.setResidentialName(residential.getName());
        //基本数据
        dto.setOrgPosition(residential.getOrgPosition());
        dto.setStatus(OperationConstant.ZERO);
        dto.setCreateTime(new Date());
        dto.setRemainSpace(dto.getSpaceTotal());
        return new ResultBean<>(ResultEnum.SUCCESS,this.insert(dto));
    }

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

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

    @Override
    public ParkingLot getById(Long id) {
        return this.selectById(id);
    }
}
