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.DriveSchoolCarRepair;
import cn.iocoder.yudao.module.jx.mapper.DriveSchoolCarMapper;
import cn.iocoder.yudao.module.jx.mapper.DriveSchoolCarRepairMapper;
import cn.iocoder.yudao.module.jx.service.IDriveSchoolCarRepairService;
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.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-04-01
 */
@Service
public class DriveSchoolCarRepairServiceImpl implements IDriveSchoolCarRepairService
{
    @Autowired
    private DriveSchoolCarRepairMapper driveSchoolCarRepairMapper;

    @Resource
    private AdminUserApi userApi;

    /**
     * 查询车辆维修记录
     *
     * @param id 车辆维修记录主键
     * @return 车辆维修记录
     */
    @Override
    public DriveSchoolCarRepair selectDriveSchoolCarRepairById(Long id)
    {
        return driveSchoolCarRepairMapper.selectDriveSchoolCarRepairById(id);
    }

    @Resource
    private DriveSchoolCarMapper driveSchoolCarMapper;
    /**
     * 查询车辆维修记录列表
     *
     * @param driveSchoolCarRepair 车辆维修记录
     * @return 车辆维修记录
     */
    @Override
    public IPage<DriveSchoolCarRepair> selectDriveSchoolCarRepairList(DriveSchoolCarRepair driveSchoolCarRepair, Page<DriveSchoolCarRepair> page)
    {
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        AdminUserRespDTO user = userApi.getUser(userId);
        Long deptId = user.getDeptId();
        driveSchoolCarRepair.setDeptId(deptId);
        if (deptId == 100){
            driveSchoolCarRepair.setDeptId(null);
        }
        IPage<DriveSchoolCarRepair> driveSchoolCarRepairIPage = driveSchoolCarRepairMapper.selectDriveSchoolCarRepairList(driveSchoolCarRepair, page);
        List<DriveSchoolCarRepair> driveSchoolCarRepairs = driveSchoolCarRepairIPage.getRecords();
        for (DriveSchoolCarRepair schoolCarRepair : driveSchoolCarRepairs) {
            DriveSchoolCar driveSchoolCar = driveSchoolCarMapper.selectDriveSchoolCarById(schoolCarRepair.getCarId());
            if (ObjectUtils.isNotEmpty(driveSchoolCar)){
                schoolCarRepair.setCarName(driveSchoolCar.getCarModel());
            }
        }
        return driveSchoolCarRepairIPage;
    }

    @Override
    public List<DriveSchoolCarRepair> selectDriveSchoolCarRepairListAll(DriveSchoolCarRepair driveSchoolCarRepair)
    {
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        AdminUserRespDTO user = userApi.getUser(userId);
        Long deptId = user.getDeptId();
        driveSchoolCarRepair.setDeptId(deptId);
        List<DriveSchoolCarRepair> driveSchoolCarRepairs = driveSchoolCarRepairMapper.selectDriveSchoolCarRepairListAll(driveSchoolCarRepair);
        for (DriveSchoolCarRepair schoolCarRepair : driveSchoolCarRepairs) {
            DriveSchoolCar driveSchoolCar = driveSchoolCarMapper.selectDriveSchoolCarById(schoolCarRepair.getCarId());
            if (ObjectUtils.isNotEmpty(driveSchoolCar)){
                schoolCarRepair.setCarName(driveSchoolCar.getCarModel());
            }
        }
        return driveSchoolCarRepairs;
    }

    /**
     * 新增车辆维修记录
     *
     * @param driveSchoolCarRepair 车辆维修记录
     * @return 结果
     */
    @Override
    public int insertDriveSchoolCarRepair(DriveSchoolCarRepair driveSchoolCarRepair)
    {
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        AdminUserRespDTO user = userApi.getUser(userId);
        Long deptId = user.getDeptId();
        driveSchoolCarRepair.setDeptId(deptId);
        return driveSchoolCarRepairMapper.insertDriveSchoolCarRepair(driveSchoolCarRepair);
    }

    /**
     * 修改车辆维修记录
     *
     * @param driveSchoolCarRepair 车辆维修记录
     * @return 结果
     */
    @Override
    public int updateDriveSchoolCarRepair(DriveSchoolCarRepair driveSchoolCarRepair)
    {
        //driveSchoolCarRepair.setUpdateTime(DateUtils.getNowDate());
        return driveSchoolCarRepairMapper.updateDriveSchoolCarRepair(driveSchoolCarRepair);
    }

    /**
     * 批量删除车辆维修记录
     *
     * @param ids 需要删除的车辆维修记录主键
     * @return 结果
     */
    @Override
    public int deleteDriveSchoolCarRepairByIds(Long[] ids)
    {
        return driveSchoolCarRepairMapper.deleteDriveSchoolCarRepairByIds(ids);
    }

    /**
     * 删除车辆维修记录信息
     *
     * @param id 车辆维修记录主键
     * @return 结果
     */
    @Override
    public int deleteDriveSchoolCarRepairById(Long id)
    {
        return driveSchoolCarRepairMapper.deleteDriveSchoolCarRepairById(id);
    }
}
