package org.jeecg.modules.system.app.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.system.app.dto.AppCoursesRuleDTO;
import org.jeecg.modules.system.app.entity.AppCourses;
import org.jeecg.modules.system.app.entity.AppCoursesPriceRules;
import org.jeecg.modules.system.app.mapper.AppCoursesMapper;
import org.jeecg.modules.system.app.mapper.AppCoursesPriceRulesMapper;
import org.jeecg.modules.system.app.service.IAppCoursesPriceRulesService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description: 课程价格规则表
 * @Author: jeecg-boot
 * @Date: 2025-07-03
 * @Version: V1.0
 */
@Service
public class AppCoursesPriceRulesServiceImpl extends ServiceImpl<AppCoursesPriceRulesMapper, AppCoursesPriceRules> implements IAppCoursesPriceRulesService {

    @Resource
    private AppCoursesMapper appCoursesMapper;

    @Override
    public List<AppCoursesRuleDTO> queryCoursesPriceRulesList(String courseId,Integer coursesType) {
        List<AppCoursesRuleDTO> ruleDTOS = new ArrayList<>();
        //查询补课类型的数据
        baseMapper.selectList(Wrappers.<AppCoursesPriceRules>lambdaQuery().eq(AppCoursesPriceRules::getCoursesId, courseId).eq(AppCoursesPriceRules::getCoursesType,coursesType))
                .forEach(appCourses -> {
                    AppCoursesRuleDTO appCoursesRuleDTO = new AppCoursesRuleDTO();
                    BeanUtils.copyProperties(appCourses, appCoursesRuleDTO);
                    ruleDTOS.add(appCoursesRuleDTO);
                });
        return ruleDTOS;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean editWitchPriceRules(List<AppCoursesRuleDTO> dtoList) {
        if (dtoList.isEmpty()) throw new JeecgBootException("课时列表不能为空");
        AppCourses appCourses = appCoursesMapper.selectById(dtoList.get(0).getCoursesId());
        List<AppCoursesPriceRules> priceRules = baseMapper.selectList(Wrappers.<AppCoursesPriceRules>lambdaQuery().eq(AppCoursesPriceRules::getCoursesId, appCourses.getId()).eq(AppCoursesPriceRules::getCoursesType,1));
        //删除的
        List<AppCoursesPriceRules> deletePriceRules = priceRules.stream()
                .filter(db -> dtoList.stream().noneMatch(nr -> nr.getId() != null && nr.getId().equals(db.getId())))
                .collect(Collectors.toList());
        deletePriceRules.forEach(
                db -> {
                    int deleteResult = baseMapper.deleteById(db.getId());
                    if (deleteResult < 1) {
                        throw new JeecgBootException("课程价格规则删除失败");
                    }
                }
        );
        //保存的
        List<AppCoursesPriceRules> savePriceRules = dtoList.stream()
                .filter(dto -> dto.getId() == null)
                .map(dto -> {
                    AppCoursesPriceRules appCoursesPriceRules = new AppCoursesPriceRules();
                    BeanUtils.copyProperties(dto, appCoursesPriceRules);
                    //标记为补课类型
                    appCoursesPriceRules.setCoursesType(CommonConstant.STATUS_1_INT);
                    return appCoursesPriceRules;
                })
                .collect(Collectors.toList());
        savePriceRules.forEach(rule -> {
            int savePriceResult = baseMapper.insert(rule);
            if (savePriceResult < 1) {
                throw new JeecgBootException("课程价格规则保存失败");
            }
        });
        //修改的
        List<AppCoursesRuleDTO> toUpdate = dtoList.stream()
                .filter(dto -> dto.getId() != null && priceRules.stream().anyMatch(db -> db.getId().equals(dto.getId())))
                .collect(Collectors.toList());
        for (AppCoursesRuleDTO dto : toUpdate) {
            AppCoursesPriceRules entity = new AppCoursesPriceRules();
            BeanUtils.copyProperties(dto,entity);
            int result = baseMapper.updateById(entity);
            if (result < 1) throw new JeecgBootException("课时更新失败");
        }
        return Boolean.TRUE;
    }

}
