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

import cn.hutool.core.bean.BeanUtil;
import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
import cn.iocoder.yudao.module.base.entity.DriveSchoolCourseDeduct;
import cn.iocoder.yudao.module.base.mapper.DriveSchoolCourseDeductMapper;
import cn.iocoder.yudao.module.base.service.DlDriveSchoolCourseDeductService;
import cn.iocoder.yudao.module.base.vo.DriveSchoolCourseDeductVO;
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 cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import lombok.RequiredArgsConstructor;
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
@RequiredArgsConstructor
public class DlDriveSchoolCourseDeductServiceImpl extends ServiceImpl<DriveSchoolCourseDeductMapper, DriveSchoolCourseDeduct> implements DlDriveSchoolCourseDeductService {
    private final AdminUserApi userApi;

    private final IDriveSchoolDeductService deductService;

    /**
     * 分页查询提成配置
     *
     * @param page    分页对象
     * @param request 查询参数
     * @return 分页结果
     */
    @Override
    public IPage<DriveSchoolCourseDeductVO> queryPage(Page<DriveSchoolCourseDeductVO> page, DriveSchoolCourseDeductVO request) {
        return baseMapper.queryPage(page, request);
    }

    /**
     * 新增提成配置
     *
     * @param list 新增参数
     * @return 新增结果
     */
    @Override
    public void add(List<DriveSchoolCourseDeductVO> list) {
        List<DriveSchoolCourseDeduct> driveSchoolCourseDeducts = BeanUtil.copyToList(list, DriveSchoolCourseDeduct.class);
        saveBatch(driveSchoolCourseDeducts);
    }

    /**
     * 新增默认提成配置
     *
     * @param courseId 课程id
     */
    @Override
    public void defaultAdd(String courseId) {
        //查询默认提成配置
        List<DriveSchoolDeduct> defaultDeduct = deductService.list();
        if (CollectionUtils.isNotEmpty(defaultDeduct)) {
            List<DriveSchoolCourseDeduct> driveSchoolCourseDeducts = BeanUtil.copyToList(defaultDeduct, DriveSchoolCourseDeduct.class);
            driveSchoolCourseDeducts.stream().map(driveSchoolCourseDeduct -> driveSchoolCourseDeduct.setCourseId(courseId));
            saveBatch(driveSchoolCourseDeducts);
        }
    }

    @Override
    public DriveSchoolCourseDeduct getDeductByCourseIdAndSubject(String courseId, Integer subject) {
        return lambdaQuery()
                .eq(DriveSchoolCourseDeduct::getCourseId, courseId)
                .eq(DriveSchoolCourseDeduct::getCourseSubject, subject)
                .one();
    }
}
