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

import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.jx.domain.DriveSchoolCar;
import cn.iocoder.yudao.module.jx.domain.DriveSchoolCheck;
import cn.iocoder.yudao.module.jx.mapper.DriveSchoolCarMapper;
import cn.iocoder.yudao.module.jx.mapper.DriveSchoolCheckMapper;
import cn.iocoder.yudao.module.jx.service.IDriveSchoolCheckService;
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.extension.plugins.pagination.Page;
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-10
 */
@Service
public class DriveSchoolCheckServiceImpl implements IDriveSchoolCheckService
{
    @Autowired
    private DriveSchoolCheckMapper driveSchoolCheckMapper;

    @Resource
    private AdminUserApi userApi;

    /**
     * 查询车检管理
     *
     * @param id 车检管理主键
     * @return 车检管理
     */
    @Override
    public DriveSchoolCheck selectDriveSchoolCheckById(Long id)
    {
        return driveSchoolCheckMapper.selectDriveSchoolCheckById(id);
    }

    @Resource
    private DriveSchoolCarMapper driveSchoolCarMapper;

    /**
     * 查询车检管理列表
     *
     * @param driveSchoolCheck 车检管理
     * @return 车检管理
     */
    @Override
    public IPage<DriveSchoolCheck> selectDriveSchoolCheckList(DriveSchoolCheck driveSchoolCheck, Page<DriveSchoolCheck> page)
    {
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        AdminUserRespDTO user = userApi.getUser(userId);
        Long deptId = user.getDeptId();
        driveSchoolCheck.setDeptId(deptId);
        if (deptId == 100){
            driveSchoolCheck.setDeptId(null);
        }
        IPage<DriveSchoolCheck> driveSchoolCheckIPage = driveSchoolCheckMapper.selectDriveSchoolCheckList(driveSchoolCheck, page);
        List<DriveSchoolCheck> driveSchoolChecks = driveSchoolCheckIPage.getRecords();
        for (DriveSchoolCheck schoolCheck : driveSchoolChecks) {
            DriveSchoolCar driveSchoolCar = driveSchoolCarMapper.selectDriveSchoolCarById(schoolCheck.getCarId());
            if (ObjectUtils.isNotEmpty(driveSchoolCar) && ObjectUtils.isNotEmpty(driveSchoolCar.getCarModel()))
            {
                schoolCheck.setCarName(driveSchoolCar.getCarModel());
            }
        }
        return driveSchoolCheckIPage;
    }

    /**
     * 新增车检管理
     *
     * @param driveSchoolCheck 车检管理
     * @return 结果
     */
    @Override
    public int insertDriveSchoolCheck(DriveSchoolCheck driveSchoolCheck) throws Exception {
        DriveSchoolCar driveSchoolCar = driveSchoolCarMapper.selectByCarNo(driveSchoolCheck.getCarNo());
        if (ObjectUtils.isEmpty(driveSchoolCar)){
            throw new Exception("车牌号不存在");
        }else {
            driveSchoolCheck.setCarId(driveSchoolCar.getId());
        }

        Long userId = SecurityFrameworkUtils.getLoginUserId();
        AdminUserRespDTO user = userApi.getUser(userId);
        Long deptId = user.getDeptId();
        driveSchoolCheck.setDeptId(deptId);
        return driveSchoolCheckMapper.insertDriveSchoolCheck(driveSchoolCheck);
    }

    /**
     * 修改车检管理
     *
     * @param driveSchoolCheck 车检管理
     * @return 结果
     */
    @Override
    public int updateDriveSchoolCheck(DriveSchoolCheck driveSchoolCheck)
    {
        //driveSchoolCheck.setUpdateTime(DateUtils.getNowDate());
        return driveSchoolCheckMapper.updateDriveSchoolCheck(driveSchoolCheck);
    }

    /**
     * 批量删除车检管理
     *
     * @param ids 需要删除的车检管理主键
     * @return 结果
     */
    @Override
    public int deleteDriveSchoolCheckByIds(Long[] ids)
    {
        return driveSchoolCheckMapper.deleteDriveSchoolCheckByIds(ids);
    }

    /**
     * 删除车检管理信息
     *
     * @param id 车检管理主键
     * @return 结果
     */
    @Override
    public int deleteDriveSchoolCheckById(Long id)
    {
        return driveSchoolCheckMapper.deleteDriveSchoolCheckById(id);
    }
}
