package jnpf.cultivate.v2.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jnpf.cultivate.mapper.FtbCultivatePromotionLogMapper;
import jnpf.cultivate.v2.service.FtbCultivatePromotionLogService;
import jnpf.model.cultivate.po.promotion.FtbCultivatePromotionLog;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class FtbCultivatePromotionLogServiceImpl
        extends ServiceImpl<FtbCultivatePromotionLogMapper, FtbCultivatePromotionLog>
        implements FtbCultivatePromotionLogService {

    /**
     * 完成晋升等级操作
     *
     * @param userId      用户ID，标识执行晋升操作的用户
     * @param promotionId 晋升记录ID，用于标识具体的晋升活动或任务
     * @param level       晋升等级，表示用户要完成的具体等级数值
     * @param tenantId    租户ID，用于多租户环境下的数据隔离
     */
    @Override
    public void completePromotionLevel(String userId, String promotionId, Integer level, String tenantId) {
        LambdaQueryWrapper<FtbCultivatePromotionLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FtbCultivatePromotionLog::getPromotionId, promotionId)
                .eq(FtbCultivatePromotionLog::getLevel, level)
                .eq(FtbCultivatePromotionLog::getUserId, userId)
                .eq(FtbCultivatePromotionLog::getEnabledMark, 0);
        List<FtbCultivatePromotionLog> logList = this.list(queryWrapper);

        if (CollUtil.isNotEmpty(logList)) {
            LambdaUpdateWrapper<FtbCultivatePromotionLog> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(FtbCultivatePromotionLog::getPromotionId, promotionId)
                    .eq(FtbCultivatePromotionLog::getLevel, level)
                    .eq(FtbCultivatePromotionLog::getUserId, userId)
                    .eq(FtbCultivatePromotionLog::getEnabledMark, 0)
                    .set(FtbCultivatePromotionLog::getState, 2);
            this.update(updateWrapper);
        } else {
            // 如果不存在，则新增
            FtbCultivatePromotionLog newLog = new FtbCultivatePromotionLog();
            newLog.setPromotionId(promotionId);
            newLog.setLevel(level);
            newLog.setUserId(userId);
            newLog.setState(2);
            newLog.setEnabledMark(0);
            this.save(newLog);
        }
    }

    /**
     * 查询用户的已完成晋升记录列表
     *
     * @param userId 用户ID，用于查询指定用户的晋升日志记录
     * @param state  地图状态,1-未完成 2-已完成 null-全部
     * @return List<FtbCultivatePromotionLog> 返回用户的所有已完成晋升记录列表
     */
    @Override
    public List<FtbCultivatePromotionLog> queryMyCompletePromotion(String userId, Integer state) {
        LambdaQueryWrapper<FtbCultivatePromotionLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(FtbCultivatePromotionLog::getUserId, userId)
                .eq(state != null, FtbCultivatePromotionLog::getState, state)
                .eq(FtbCultivatePromotionLog::getEnabledMark, 0).orderByAsc(FtbCultivatePromotionLog::getLevel);
        return this.list(queryWrapper);
    }
}