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.CarDto;
import com.sptg.estate.api.dto.UserDto;
import com.sptg.estate.api.entity.Agency;
import com.sptg.estate.api.entity.Car;
import com.sptg.estate.api.entity.Residential;
import com.sptg.estate.api.entity.User;
import com.sptg.estate.biz.mapper.CarMapper;
import com.sptg.estate.biz.service.AgencyServiceInterface;
import com.sptg.estate.biz.service.CarServiceInterface;
import com.sptg.estate.biz.service.ResidentialServiceInterface;
import com.sptg.estate.biz.service.UserServiceInterface;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@RequiredArgsConstructor
public class CarServiceImpl extends ServiceImpl<CarMapper, Car> implements CarServiceInterface {

    private final AgencyServiceInterface agencyServiceInterface;
    private final ResidentialServiceInterface residentialServiceInterface;
    private final UserServiceInterface userServiceInterface;

    @Override
    public ResultBean<Page<Car>> page(CarDto dto) {
        Wrapper<Car> wrapper = new EntityWrapper<>();
        //过滤删除
        wrapper.eq("i_status", OperationConstant.ZERO);
        //车牌号
        wrapper.like(dto.getNumber() != null,"s_number",dto.getNumber(), SqlLike.DEFAULT);
        //车辆类型
        wrapper.eq(dto.getType() != null,"i_type",dto.getType());
        //车主姓名
        wrapper.like(dto.getPersonName() != null,"s_person_name",dto.getPersonName(), SqlLike.DEFAULT);
        //车主姓名
        wrapper.like(dto.getPersonPhone() != null,"s_person_phone",dto.getPersonPhone(), SqlLike.DEFAULT);
        //审核状态
        wrapper.eq(dto.getAuditStatus() != null,"i_audit_stauts",dto.getAuditStatus());
        //根据时间日期
        wrapper.ge(dto.getBeginDate() != null,"t_create_time",dto.getBeginDate());
        wrapper.le(dto.getEndDate() !=null ,"t_create_time",dto.getEndDate());
        wrapper.orderBy("t_create_time",false);
        return new ResultBean<>(ResultEnum.SUCCESS,this.selectPage(dto.getPage(),wrapper));
    }

    @Override
    public ResultBean<Boolean> add(CarDto dto) {

        //小区
        Residential residential = this.residentialServiceInterface.findResidentialById(dto.getResidentialId());
        Assert.notNull(residential,"选择的小区不存在");
        dto.setResidentialName(residential.getName());
        dto.setAgencyName(residential.getAgencyName());
        dto.setAgencyId(residential.getAgencyId());

        //车主
        UserDto userDto = new UserDto();
        userDto.setResidentialId(residential.getId());
        userDto.setName(dto.getPersonName());
        userDto.setTel(dto.getPersonPhone());
        User user = this.userServiceInterface.byTelAndName(userDto);
        Assert.notNull(user,"车主不存在");
        dto.setPersonId(user.getId());
        dto.setOrgPosition(user.getOrgPosition());
        //创建时间
        dto.setCreateTime(new Date());
        return new ResultBean<>(ResultEnum.SUCCESS,this.insert(dto));
    }

    @Override
    public ResultBean<Boolean> addByApp(CarDto dto) {
        //小区
        Residential residential = this.residentialServiceInterface.findResidentialById(dto.getResidentialId());
        Assert.notNull(residential,"选择的小区不存在");
        dto.setResidentialName(residential.getName());
        dto.setAgencyName(residential.getAgencyName());
        dto.setAgencyId(residential.getAgencyId());

        //车主
        List<Long> ids = new ArrayList<>();
        ids.add(dto.getPersonId());

        List<User> user = this.userServiceInterface.getListByUserId(ids);
        if(user.size()>0){
            dto.setPersonName(user.get(0).getName());
            dto.setPersonPhone(user.get(0).getTel());
            dto.setOrgPosition(user.get(0).getOrgPosition());
        }

        //创建时间
        dto.setCreateTime(new Date());
        return new ResultBean<>(ResultEnum.SUCCESS,this.insert(dto));
    }

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

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