package cn.iocoder.yudao.module.jx.service.impl;

import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
import cn.iocoder.yudao.module.base.service.DlDriveSchoolCoachService;
import cn.iocoder.yudao.module.jx.domain.DriveSchoolCar;
import cn.iocoder.yudao.module.jx.mapper.DriveSchoolCarMapper;
import cn.iocoder.yudao.module.jx.service.IDriveSchoolCarService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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


/**
 * 车辆信息Service业务层处理
 *
 * @author ruoyi
 * @date 2024-05-08
 */
@Service
public class DriveSchoolCarServiceImpl extends ServiceImpl<DriveSchoolCarMapper,DriveSchoolCar> implements IDriveSchoolCarService
{
    @Autowired
    private DriveSchoolCarMapper driveSchoolCarMapper;
    @Resource
    private DlDriveSchoolCoachService coachService;

    /**
     * 查询车辆信息
     *
     * @param id 车辆信息主键
     * @return 车辆信息
     */
    @Override
    public DriveSchoolCar selectDriveSchoolCarById(Long id)
    {
        return driveSchoolCarMapper.selectDriveSchoolCarById(id);
    }

    /**
     * 查询车辆信息列表
     *
     * @param driveSchoolCar 车辆信息
     * @return 车辆信息
     */
    @Override
    public IPage<DriveSchoolCar> selectDriveSchoolCarList(Page<DriveSchoolCar> page,DriveSchoolCar driveSchoolCar)
    {
        return driveSchoolCarMapper.selectDriveSchoolCarList(driveSchoolCar,page);
    }

    @Override
    public List<DriveSchoolCar> selectDriveSchoolCarListAll(DriveSchoolCar driveSchoolCar)
    {
        return driveSchoolCarMapper.selectDriveSchoolCarListAll(driveSchoolCar);
    }

    /**
     * 新增车辆信息
     *
     * @param driveSchoolCar 车辆信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertDriveSchoolCar(DriveSchoolCar driveSchoolCar)
    {
        DriveSchoolCar driveSchoolCar1 = driveSchoolCarMapper.selectByCarNo(driveSchoolCar.getCarNo());
        if (ObjectUtils.isNotEmpty(driveSchoolCar1)){
            throw new RuntimeException("该车牌的车辆已存在！");
        }
        if (StringUtils.isNotEmpty(driveSchoolCar.getCoachId())){
            coachService.updateSchoolCoachCarId(driveSchoolCar.getCoachId(), driveSchoolCar.getCarNo());
        }
        return driveSchoolCarMapper.insert(driveSchoolCar);
    }

    /**
     * 修改车辆信息
     *
     * @param driveSchoolCar 车辆信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateDriveSchoolCar(DriveSchoolCar driveSchoolCar)
    {
        if (StringUtils.isNotEmpty(driveSchoolCar.getCoachId())){
            coachService.updateSchoolCoachCarId(driveSchoolCar.getCoachId(), driveSchoolCar.getCarNo());
        }
        return driveSchoolCarMapper.updateById(driveSchoolCar);
    }

    /**
     * 批量删除车辆信息
     *
     * @param ids 需要删除的车辆信息主键
     * @return 结果
     */
    @Override
    public int deleteDriveSchoolCarByIds(Long[] ids)
    {
        return driveSchoolCarMapper.deleteDriveSchoolCarByIds(ids);
    }

    /**
     * 删除车辆信息信息
     *
     * @param id 车辆信息主键
     * @return 结果
     */
    @Override
    public int deleteDriveSchoolCarById(Long id)
    {
        return driveSchoolCarMapper.deleteById(id);
    }

    @Override
    public String importCars(List<DriveSchoolCar> carsList, boolean updateSupport, String operName) throws Exception {
        if (CollectionUtils.isEmpty(carsList)){
            throw new Exception("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        for (DriveSchoolCar driveSchoolCar : carsList) {
            DriveSchoolCar driveSchoolCar1 = driveSchoolCarMapper.selectByCarNo(driveSchoolCar.getCarNo());
            if (ObjectUtils.isEmpty(driveSchoolCar1)){
                driveSchoolCarMapper.insert(driveSchoolCar);
                successNum++;
            }else {
                failureNum++;
            }
        }
        return "数据已全部导入成功！";
    }

    @Override
    public DriveSchoolCar selectByCoachId(Long coachId) {
        LambdaQueryWrapper<DriveSchoolCar> queryWrapper = new LambdaQueryWrapper<DriveSchoolCar>()
                .eq(DriveSchoolCar::getUserId,coachId)
                .orderByDesc(BaseDO::getCreateTime);
        List<DriveSchoolCar> list = this.list(queryWrapper);
        return list.isEmpty()?null:list.get(0);
    }
}
