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

import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
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.DriveSchoolFile;
import cn.iocoder.yudao.module.jx.mapper.DriveSchoolFileMapper;
import cn.iocoder.yudao.module.jx.mapper.DriveSchoolInfoMapper;
import cn.iocoder.yudao.module.jx.service.IDriveSchoolFileService;
import cn.iocoder.yudao.module.jx.vo.DriveSchoolInfoVO;
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 DriveSchoolFileServiceImpl implements IDriveSchoolFileService
{
    @Autowired
    private DriveSchoolFileMapper driveSchoolFileMapper;

    @Resource
    private AdminUserApi userApi;


    /**
     * 查询文件类型
     *
     * @param id 文件类型主键
     * @return 文件类型
     */
    @Override
    public DriveSchoolFile selectDriveSchoolFileById(Long id)
    {
        return driveSchoolFileMapper.selectDriveSchoolFileById(id);
    }

    /**
     * 查询文件类型列表
     *
     * @param driveSchoolFile 文件类型
     * @return 文件类型
     */
    @Override
    public IPage<DriveSchoolFile> selectDriveSchoolFileList(DriveSchoolFile driveSchoolFile, Page<DriveSchoolFile> page)
    {
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        AdminUserRespDTO user = userApi.getUser(userId);
        Long deptId = user.getDeptId();
        driveSchoolFile.setDeptId(deptId);
        return driveSchoolFileMapper.selectDriveSchoolFileList(driveSchoolFile,page);
    }

    /**
     * 新增文件类型
     *
     * @param driveSchoolFile 文件类型
     * @return 结果
     */
    @Override
    public int insertDriveSchoolFile(DriveSchoolFile driveSchoolFile) throws Exception {
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        AdminUserRespDTO user = userApi.getUser(userId);
        Long deptId = user.getDeptId();
        driveSchoolFile.setDeptId(deptId);

        DriveSchoolFile driveSchoolFile1 = new DriveSchoolFile();
        driveSchoolFile1.setDeptId(deptId);
        driveSchoolFile1.setFileType(driveSchoolFile.getFileType());
        List<DriveSchoolFile> driveSchoolFiles = driveSchoolFileMapper.selectDriveSchoolFileListAll(driveSchoolFile1);
        if (ObjectUtils.isNotEmpty(driveSchoolFiles)){
            throw new Exception("驾校已存在文件类型，请勿重复添加!");
        }

        return driveSchoolFileMapper.insertDriveSchoolFile(driveSchoolFile);
    }

    /**
     * 修改文件类型
     *
     * @param driveSchoolFile 文件类型
     * @return 结果
     */
    @Override
    public int updateDriveSchoolFile(DriveSchoolFile driveSchoolFile)
    {
        //driveSchoolFile.setUpdateTime(DateUtils.getNowDate());
        return driveSchoolFileMapper.updateDriveSchoolFile(driveSchoolFile);
    }

    /**
     * 批量删除文件类型
     *
     * @param ids 需要删除的文件类型主键
     * @return 结果
     */
    @Override
    public int deleteDriveSchoolFileByIds(Long[] ids)
    {
        return driveSchoolFileMapper.deleteDriveSchoolFileByIds(ids);
    }

    /**
     * 删除文件类型信息
     *
     * @param id 文件类型主键
     * @return 结果
     */
    @Override
    public int deleteDriveSchoolFileById(Long id)
    {
        return driveSchoolFileMapper.deleteDriveSchoolFileById(id);
    }

    @Resource
    private DriveSchoolInfoMapper driveSchoolInfoMapper;
    @Override
    public DriveSchoolFile getOneFile(DriveSchoolFile driveSchoolFile) {
        DriveSchoolInfoVO schoolInfoByDeptId = driveSchoolInfoMapper.findInfoByJxId(driveSchoolFile.getDeptId());
        driveSchoolFile.setDeptId(schoolInfoByDeptId.getDeptId());
        DriveSchoolFile oneFile = driveSchoolFileMapper.getOneFile(driveSchoolFile);
        return oneFile;
    }
}
