package com.seer.teach.netty.im.handler.im.singlechat;

import cn.hutool.json.JSONUtil;
import com.seer.teach.netty.common.annotation.ImMessageContentType;
import com.seer.teach.netty.common.proto.MessageProtobuf;
import com.seer.teach.netty.im.handler.im.ImMessageContentHandler;
import com.seer.teach.teacher.api.LearningDurationServiceApi;
import io.netty.channel.ChannelHandlerContext;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Map;

/**
 *
 * <p>
 *
 * </p >
 *
 * @author Captain
 * @since 2025-09-20 15:33
 */
@Service
@Slf4j
@ImMessageContentType(135)
@RequiredArgsConstructor
public class VideoLearningDurationImpl implements ImMessageContentHandler {

    private final LearningDurationServiceApi learningDurationServiceApi;

    @Override
    public void handle(ChannelHandlerContext ctx, MessageProtobuf.Msg msg) {
        Map map = JSONUtil.toBean(msg.getBody(), Map.class);
        String device = msg.getHead().getDevice();
        Integer id = Integer.parseInt(map.get("id").toString());
        Integer type = Integer.parseInt(map.get("type").toString());
        BigDecimal time = new BigDecimal(map.get("time").toString())
                .divide(BigDecimal.valueOf(1000), 2, RoundingMode.HALF_UP);
        learningDurationServiceApi.saveLearningDuration(id,device,type,time);
    }
}
