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 cn.iocoder.yudao.module.jx.domain.DriveSchoolCar;
import cn.iocoder.yudao.module.jx.domain.DriveSchoolInsurance;
import cn.iocoder.yudao.module.jx.mapper.DriveSchoolCarMapper;
import cn.iocoder.yudao.module.jx.mapper.DriveSchoolInsuranceMapper;
import cn.iocoder.yudao.module.jx.service.IDriveSchoolInsuranceService;
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-06
 */
@Service
public class DriveSchoolInsuranceServiceImpl implements IDriveSchoolInsuranceService
{
    @Autowired
    private DriveSchoolInsuranceMapper driveSchoolInsuranceMapper;

    @Resource
    private  DriveSchoolCarMapper driveSchoolCarMapper;

    @Resource
    private AdminUserApi userApi;

    /**
     * 查询保险管理
     *
     * @param id 保险管理主键
     * @return 保险管理
     */
    @Override
    public DriveSchoolInsurance selectDriveSchoolInsuranceById(Long id)
    {
        return driveSchoolInsuranceMapper.selectDriveSchoolInsuranceById(id);
    }

    /**
     * 查询保险管理列表
     *
     * @param driveSchoolInsurance 保险管理
     * @return 保险管理
     */
    @Override
    public IPage<DriveSchoolInsurance> selectDriveSchoolInsuranceList(DriveSchoolInsurance driveSchoolInsurance, Page<DriveSchoolInsurance> page)
    {
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        AdminUserRespDTO user = userApi.getUser(userId);
        Long deptId = user.getDeptId();
        driveSchoolInsurance.setDeptId(deptId);
        if (deptId==100){
            driveSchoolInsurance.setDeptId(null);
        }
        IPage<DriveSchoolInsurance> driveSchoolInsuranceIPage = driveSchoolInsuranceMapper.selectDriveSchoolInsuranceList(driveSchoolInsurance, page);
        List<DriveSchoolInsurance> driveSchoolInsurances = driveSchoolInsuranceIPage.getRecords();

        for (DriveSchoolInsurance schoolInsurance : driveSchoolInsurances) {
            DriveSchoolCar driveSchoolCar = driveSchoolCarMapper.selectDriveSchoolCarById(schoolInsurance.getCarId());
            schoolInsurance.setCarName(driveSchoolCar.getCarModel());
        }

        return driveSchoolInsuranceIPage;
    }

    /**
     * 新增保险管理
     *
     * @param driveSchoolInsurance 保险管理
     * @return 结果
     */
    @Override
    public int insertDriveSchoolInsurance(DriveSchoolInsurance driveSchoolInsurance) throws Exception {
        DriveSchoolCar driveSchoolCar = driveSchoolCarMapper.selectByCarNo(driveSchoolInsurance.getCarNo());
        if (ObjectUtils.isEmpty(driveSchoolCar)){
            throw new Exception("车牌号不存在");
        }else {
            driveSchoolInsurance.setCarId(driveSchoolCar.getId());
        }
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        AdminUserRespDTO user = userApi.getUser(userId);
        Long deptId = user.getDeptId();
        driveSchoolInsurance.setDeptId(deptId);
        return driveSchoolInsuranceMapper.insertDriveSchoolInsurance(driveSchoolInsurance);
    }

    /**
     * 修改保险管理
     *
     * @param driveSchoolInsurance 保险管理
     * @return 结果
     */
    @Override
    public int updateDriveSchoolInsurance(DriveSchoolInsurance driveSchoolInsurance)
    {
        //driveSchoolInsurance.setUpdateTime(DateUtils.getNowDate());
        return driveSchoolInsuranceMapper.updateDriveSchoolInsurance(driveSchoolInsurance);
    }

    /**
     * 批量删除保险管理
     *
     * @param ids 需要删除的保险管理主键
     * @return 结果
     */
    @Override
    public int deleteDriveSchoolInsuranceByIds(Long[] ids)
    {
        return driveSchoolInsuranceMapper.deleteDriveSchoolInsuranceByIds(ids);
    }

    /**
     * 删除保险管理信息
     *
     * @param id 保险管理主键
     * @return 结果
     */
    @Override
    public int deleteDriveSchoolInsuranceById(Long id)
    {
        return driveSchoolInsuranceMapper.deleteDriveSchoolInsuranceById(id);
    }
}
