package com.jeeplus.business.drive.service;

import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeeplus.business.drive.domain.DriveInfo;
import com.jeeplus.business.drive.mapper.DriveInfoMapper;
import com.jeeplus.common.TokenProvider;
import com.jeeplus.core.query.QueryWrapperGenerator;
import com.jeeplus.sys.feign.IUserApi;
import com.jeeplus.sys.service.dto.UserDTO;
import com.jeeplus.utils.StringUtils;
import org.springframework.stereotype.Service;

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

@Service
public class DriveInfoService {

    @Resource
    private DriveInfoMapper driveInfoMapper;

    public IPage<DriveInfo> findPageList(DriveInfo driveInfo, Page<DriveInfo> page) throws Exception {
        QueryWrapper<DriveInfo> queryWrapper = QueryWrapperGenerator.buildQueryCondition(driveInfo, DriveInfo.class);
        queryWrapper.eq("del_flag",0);
        if (StringUtils.isNotBlank(driveInfo.getDriveNumber())){
            queryWrapper.like("drive_number",driveInfo.getDriveNumber());
        }
        if (StringUtils.isNotBlank(driveInfo.getDriveIdentifyNumber())){
            queryWrapper.like("drive_identify_number",driveInfo.getDriveIdentifyNumber());
        }
        IPage<DriveInfo> iPage=driveInfoMapper.findPageList(page,queryWrapper);
        return iPage;


    }

    /**}
     * 添加车辆信息
     * @param driveInfo
     */
    public void saveOrUpdate(DriveInfo driveInfo) {
        //获取当前登录人信息
        UserDTO userDTO = SpringUtil.getBean(IUserApi.class).getByToken(TokenProvider.getCurrentToken());

        DriveInfo info = new DriveInfo();
        info.setUpdateById(userDTO.getId());
        info.setUpdateTime(new Date());
        info.setDriveNumber(driveInfo.getDriveNumber());
        info.setDriveIdentifyNumber(driveInfo.getDriveIdentifyNumber());
        //判断id是否存在
        if (StringUtils.isNotBlank(driveInfo.getId())){
            info.setId(driveInfo.getId());
            driveInfoMapper.updateInfoById(info);
        }else {
            info.setCreateById(userDTO.getId());
            info.setCreateTime(new Date());
            driveInfoMapper.insert(info);
        }
    }

    /**
     * 根据id删除车辆信息
     * @param id
     */
    public void deleteById(String id) {
        driveInfoMapper.deleteById(id);
    }

    /**
     * 根据id查询车辆信息
     * @param id
     * @return
     */
    public DriveInfo findById(String id) {
        DriveInfo driveInfo=driveInfoMapper.findById(id);
        return driveInfo;
    }

    /**
     * 校验车辆
     * @param number
     * @return
     */
    public boolean checkNumber(String number) {
        DriveInfo driveInfo=driveInfoMapper.checkNumber(number);
        if (ObjectUtils.isNotEmpty(driveInfo)){
            return true;
        }
        return false;
    }
}
