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

import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.jx.utils.DateUtils;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
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.extension.plugins.pagination.Page;
import com.thoughtworks.xstream.core.SecurityUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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


/**
 * 车辆信息Service业务层处理
 *
 * @author ruoyi
 * @date 2024-05-08
 */
@Service
public class DriveSchoolCarServiceImpl implements IDriveSchoolCarService
{
    @Autowired
    private DriveSchoolCarMapper driveSchoolCarMapper;

    @Resource
    private AdminUserApi userApi;

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

    /**
     * 查询车辆信息列表
     *
     * @param driveSchoolCar 车辆信息
     * @return 车辆信息
     */
    @Override
    public IPage<DriveSchoolCar> selectDriveSchoolCarList(DriveSchoolCar driveSchoolCar, Page<DriveSchoolCar> page)
    {
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        AdminUserRespDTO user = userApi.getUser(userId);
        Long deptId = user.getDeptId();
        driveSchoolCar.setDeptId(deptId);
        return driveSchoolCarMapper.selectDriveSchoolCarList(driveSchoolCar,page);
    }

    @Override
    public List<DriveSchoolCar> selectDriveSchoolCarListAll(DriveSchoolCar driveSchoolCar)
    {
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        AdminUserRespDTO user = userApi.getUser(userId);
        Long deptId = user.getDeptId();
        driveSchoolCar.setDeptId(deptId);
        return driveSchoolCarMapper.selectDriveSchoolCarListAll(driveSchoolCar);
    }

    /**
     * 新增车辆信息
     *
     * @param driveSchoolCar 车辆信息
     * @return 结果
     */
    @Override
    public int insertDriveSchoolCar(DriveSchoolCar driveSchoolCar)
    {
        DriveSchoolCar driveSchoolCar1 = driveSchoolCarMapper.selectByCarNo(driveSchoolCar.getCarNo());
        if (ObjectUtils.isNotEmpty(driveSchoolCar1)){
            throw new RuntimeException("该车牌的车辆已存在！");
        }
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        AdminUserRespDTO user = userApi.getUser(userId);
        Long deptId = user.getDeptId();
        driveSchoolCar.setDeptId(deptId);
        return driveSchoolCarMapper.insertDriveSchoolCar(driveSchoolCar);
    }

    /**
     * 修改车辆信息
     *
     * @param driveSchoolCar 车辆信息
     * @return 结果
     */
    @Override
    public int updateDriveSchoolCar(DriveSchoolCar driveSchoolCar)
    {
        //driveSchoolCar.setUpdateTime(DateUtils.getNowDate());
        return driveSchoolCarMapper.updateDriveSchoolCar(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.deleteDriveSchoolCarById(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.insertDriveSchoolCar(driveSchoolCar);
                successNum++;
            }else {
                failureNum++;
            }
        }
        return "数据已全部导入成功！";
    }
}
