package com.fs.course.service.impl;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fs.common.exception.ServiceException;
import com.fs.common.utils.DateUtils;
import com.fs.common.utils.bean.BeanUtils;
import com.fs.course.domain.FsUserCoursePeriod;
import com.fs.course.domain.FsUserCoursePeriodDays;
import com.fs.course.dto.FsUserCourseTrainingCampDTO;
import com.fs.course.mapper.FsUserCoursePeriodDaysMapper;
import com.fs.course.mapper.FsUserCoursePeriodMapper;
import com.fs.course.service.IFsUserCoursePeriodDaysService;
import com.fs.course.vo.FsUserCourseTrainingCampVO;
import com.fs.store.service.IFsUserService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import com.fs.course.mapper.FsUserCourseTrainingCampMapper;
import com.fs.course.domain.FsUserCourseTrainingCamp;
import com.fs.course.service.IFsUserCourseTrainingCampService;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 会员训练营Service业务层处理
 *
 * @author fs
 * @date 2025-04-15
 */
@Service
@AllArgsConstructor
public class FsUserCourseTrainingCampServiceImpl extends ServiceImpl<FsUserCourseTrainingCampMapper, FsUserCourseTrainingCamp> implements IFsUserCourseTrainingCampService
{

    private final FsUserCoursePeriodMapper fsUserCoursePeriodMapper;
    private final IFsUserCoursePeriodDaysService fsUserCoursePeriodDaysService;

    /**
     * 查询训练营列表
     * @param params    参数
     * @return  list
     */
    @Override
    public List<FsUserCourseTrainingCampVO> selectFsUserCourseTrainingCampVOListByMap(Map<String, Object> params) {
        return baseMapper.selectFsUserCourseTrainingCampVOListByMap(params);
    }

    /**
     * 新增训练营
     * @param params    参数
     */
    @Override
    public void add(FsUserCourseTrainingCampDTO params) {
        FsUserCourseTrainingCamp trainingCamp = new FsUserCourseTrainingCamp();
        trainingCamp.setTrainingCampName(params.getTrainingCampName());
        trainingCamp.setOrderNumber(baseMapper.getOrderNumber());
        trainingCamp.setCreateTime(LocalDateTime.now());
        baseMapper.insert(trainingCamp);
    }

    /**
     * 删除训练营
     * @param ids   ids
     */
    @Override
    public void del(Long[] ids) {
        baseMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 复制训练营
     * @param id    id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void copy(Long id) {
        FsUserCourseTrainingCamp fsUserCourseTrainingCamp = baseMapper.selectById(id);
        if (Objects.isNull(fsUserCourseTrainingCamp)) {
            throw new ServiceException("训练营不存在");
        }

        // 复制训练营
        FsUserCourseTrainingCamp trainingCamp = new FsUserCourseTrainingCamp();
        trainingCamp.setTrainingCampName(fsUserCourseTrainingCamp.getTrainingCampName() + " - 副本");
        trainingCamp.setOrderNumber(baseMapper.getOrderNumber());
        trainingCamp.setCreateTime(LocalDateTime.now());
        baseMapper.insert(trainingCamp);

        // 复制营期-课程
        fsUserCoursePeriodMapper.selectFsUserCoursePeriodListByTrainingCampId(id).forEach(p -> {
            // 复制营期
            FsUserCoursePeriod copy = new FsUserCoursePeriod();
            BeanUtils.copyProperties(p, copy, "periodId");
            copy.setTrainingCampId(trainingCamp.getTrainingCampId());
            fsUserCoursePeriodMapper.insertFsUserCoursePeriod(copy);

            // 复制课程
            Wrapper<FsUserCoursePeriodDays> queryWrapper = Wrappers.<FsUserCoursePeriodDays>lambdaQuery().eq(FsUserCoursePeriodDays::getPeriodId, p.getPeriodId());
            List<FsUserCoursePeriodDays> collect = fsUserCoursePeriodDaysService.list(queryWrapper).stream().map(d -> {
                FsUserCoursePeriodDays copyD = new FsUserCoursePeriodDays();
                BeanUtils.copyProperties(d, copyD, "id");
                copyD.setPeriodId(copy.getPeriodId());
                copyD.setCreateTime(new Date());
                return copyD;
            }).collect(Collectors.toList());
            if (!collect.isEmpty()) {
                fsUserCoursePeriodDaysService.saveBatch(collect);
            }
        });
    }
}
