package com.ylx.point.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ylx.point.domain.PointActivityTask;
import com.ylx.point.domain.dto.PointActivityTaskDTO;
import com.ylx.point.mapper.PointActivityTaskMapper;
import com.ylx.point.service.IPointActivityTaskService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;


/**
 * 积分活动任务规则Service业务层处理
 *
 * @author wzj
 * @date 2026-03-25
 */
@Service
public class PointActivityTaskServiceImpl extends ServiceImpl<PointActivityTaskMapper,PointActivityTask> implements IPointActivityTaskService {
    @Resource
    private PointActivityTaskMapper pointActivityTaskMapper;

    @Override
    public List<PointActivityTaskDTO> selectPointActivityTaskByActivityId(Long id) {
        // 查询活动任务列表
        List<PointActivityTask> taskList = pointActivityTaskMapper.selectPointActivityTaskByActivityId(id);
        
        if (CollectionUtil.isEmpty(taskList)) {
            return java.util.Collections.emptyList();
        }
        
        // 转换为DTO列表
        return taskList.stream().map(task -> {
            PointActivityTaskDTO dto = new PointActivityTaskDTO();
            BeanUtil.copyProperties(task, dto);
            return dto;
        }).collect(java.util.stream.Collectors.toList());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchInsertPointActivityTask(List<PointActivityTask> activityTasks) {
        if (CollectionUtil.isEmpty(activityTasks)) {
            return;
        }

        // 批量插入
        pointActivityTaskMapper.batchInsertPointActivityTask(activityTasks);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deletePointActivityTaskByActivityId(Long activityId) {
        pointActivityTaskMapper.deletePointActivityTaskByActivityId(activityId);
    }

    @Override
    public List<PointActivityTask> selectTasksByActivityType(Integer activityType) {
        return pointActivityTaskMapper.selectTasksByActivityType(activityType);
    }

}
