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 com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import cn.iocoder.yudao.module.jx.domain.DriveSchoolDeduct;
import cn.iocoder.yudao.module.jx.mapper.DriveSchoolDeductMapper;
import cn.iocoder.yudao.module.jx.service.IDriveSchoolDeductService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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-07
 */
@Service
public class DriveSchoolDeductServiceImpl implements IDriveSchoolDeductService
{
    @Autowired
    private DriveSchoolDeductMapper driveSchoolDeductMapper;

    @Resource
    private AdminUserApi userApi;

    /**
     * 查询提成配置
     *
     * @param id 提成配置主键
     * @return 提成配置
     */
    @Override
    public DriveSchoolDeduct selectDriveSchoolDeductById(Long id)
    {
        return driveSchoolDeductMapper.selectDriveSchoolDeductById(id);
    }

    /**
     * 查询提成配置列表
     *
     * @param driveSchoolDeduct 提成配置
     * @return 提成配置
     */
    @Override
    public IPage<DriveSchoolDeduct> selectDriveSchoolDeductList(DriveSchoolDeduct driveSchoolDeduct, Page<DriveSchoolDeduct> page)
    {
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        AdminUserRespDTO user = userApi.getUser(userId);
        Long deptId = user.getDeptId();
        driveSchoolDeduct.setDeptId(deptId);
        if (deptId == 100){
            driveSchoolDeduct.setDeptId(null);
        }
        return driveSchoolDeductMapper.selectDriveSchoolDeductList(driveSchoolDeduct,page);
    }

    /**
     * 新增提成配置
     *
     * @param driveSchoolDeduct 提成配置
     * @return 结果
     */
    @Override
    public int insertDriveSchoolDeduct(DriveSchoolDeduct driveSchoolDeduct) throws Exception {
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        AdminUserRespDTO user = userApi.getUser(userId);
        Long deptId = user.getDeptId();
        driveSchoolDeduct.setDeptId(deptId);
        String courseSubject = driveSchoolDeduct.getCourseSubject();
        DriveSchoolDeduct driveSchoolDeduct1 = new DriveSchoolDeduct();
        driveSchoolDeduct1.setCourseSubject(courseSubject);
        List<DriveSchoolDeduct> driveSchoolDeducts = driveSchoolDeductMapper.selectDriveSchoolDeductListAll(driveSchoolDeduct1);
        if (CollectionUtils.isNotEmpty(driveSchoolDeducts)){
            throw new Exception("该科目提成配置已存在！");
        }
        return driveSchoolDeductMapper.insertDriveSchoolDeduct(driveSchoolDeduct);
    }

    /**
     * 修改提成配置
     *
     * @param driveSchoolDeduct 提成配置
     * @return 结果
     */
    @Override
    public int updateDriveSchoolDeduct(DriveSchoolDeduct driveSchoolDeduct)
    {
        //driveSchoolDeduct.setUpdateTime(DateUtils.getNowDate());
        return driveSchoolDeductMapper.updateDriveSchoolDeduct(driveSchoolDeduct);
    }

    /**
     * 批量删除提成配置
     *
     * @param ids 需要删除的提成配置主键
     * @return 结果
     */
    @Override
    public int deleteDriveSchoolDeductByIds(Long[] ids)
    {
        return driveSchoolDeductMapper.deleteDriveSchoolDeductByIds(ids);
    }

    /**
     * 删除提成配置信息
     *
     * @param id 提成配置主键
     * @return 结果
     */
    @Override
    public int deleteDriveSchoolDeductById(Long id)
    {
        return driveSchoolDeductMapper.deleteDriveSchoolDeductById(id);
    }
}
