package com.seer.teach.mp.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.seer.teach.common.enums.ResultCodeEnum;
import com.seer.teach.common.utils.AssertUtils;
import com.seer.teach.mp.entity.MpActivityEntity;
import com.seer.teach.mp.entity.MpAgentActivityParticipantEntity;
import com.seer.teach.mp.mapper.MpAgentActivityMapper;
import com.seer.teach.mp.service.IMpActivityService;
import com.seer.teach.mp.service.IMpAgentActivityLogService;
import com.seer.teach.mp.service.IMpAgentActivityParticipantService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 代理商活动表 服务实现类
 * </p>
 *
 * @author Lingma
 * @since 2025-12-29
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class MpActivityServiceImpl extends ServiceImpl<MpAgentActivityMapper, MpActivityEntity> implements IMpActivityService {

    private final IMpAgentActivityParticipantService agentActivityParticipantService;
    private final IMpAgentActivityLogService agentActivityLogService;

    @Override
    public boolean saveOrUpdateActivity(MpActivityEntity entity) {
        MpActivityEntity oldEntity = null;
        String operationType = "创建";

        if (entity.getId() != null) {
            // 更新操作，先获取旧数据用于日志记录
            oldEntity = super.getById(entity.getId());
            operationType = "更新";
        }

        if (entity.getId() == null) {
            // 新增时设置创建时间
            entity.setCreateTime(LocalDateTime.now());
        }
        entity.setUpdateTime(LocalDateTime.now());

        boolean result = this.saveOrUpdate(entity);

        // 记录操作日志
        if (result) {
            String beforeData = oldEntity != null ? JSONUtil.toJsonStr(oldEntity) : "";
            String afterData = JSONUtil.toJsonStr(entity);
            String description = operationType + "代理商活动: " + entity.getActivityName();

            agentActivityLogService.logOperation(
                    entity.getId(),
                    entity.getCreateBy(),
                    operationType,
                    description,
                    beforeData,
                    afterData
            );
        }

        return result;
    }

    @Override
    public boolean deleteActivity(Integer id) {
        // 检查活动是否存在
        MpActivityEntity existing = super.getById(id);
        AssertUtils.notNull(existing, ResultCodeEnum.ACTIVITY_NOT_ACTIVE);

        // 删除活动前先删除相关的参与记录
        LambdaQueryWrapper<MpAgentActivityParticipantEntity> participantWrapper =
                new LambdaQueryWrapper<MpAgentActivityParticipantEntity>()
                        .eq(MpAgentActivityParticipantEntity::getActivityId, id);
        agentActivityParticipantService.remove(participantWrapper);

        boolean result = this.removeById(id);

        // 记录操作日志
        if (result) {
            String beforeData = JSONUtil.toJsonStr(existing);
            String afterData = "";
            String description = "删除代理商活动: " + existing.getActivityName();

            agentActivityLogService.logOperation(
                    id,
                    existing.getCreateBy(),
                    "删除",
                    description,
                    beforeData,
                    afterData
            );
        }

        return result;
    }

    @Override
    public List<String> getActivityName() {
        return this.list().stream()
                .map(MpActivityEntity::getActivityName).toList();
    }

    @Override
    public boolean saveOrUpdate(MpActivityEntity entity) {
        boolean result = super.saveOrUpdate(entity);
        return result;
    }
}