package com.seer.teach.mp.convert;

import com.seer.teach.mp.entity.MpUserSubscribeEntity;
import com.seer.teach.user.api.dto.UserInfoDTO;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;

@Mapper
public interface WxMpUserConvert {


    WxMpUserConvert INSTANCE = Mappers.getMapper(WxMpUserConvert.class);

    UserInfoDTO convertOne(WxMpUser wxMpUser);

    default LocalDateTime toLocalDateTime(Long timestamp) {
        if (timestamp == null) {
            return null;
        }
        return Instant.ofEpochMilli(timestamp)
                .atZone(ZoneId.systemDefault())
                .toLocalDateTime();
    }


    default MpUserSubscribeEntity convertOne(WxMpUser wxMpUser,Integer userId){
        return MpUserSubscribeEntity.builder()
                .appId(wxMpUser.getOpenId())
                .userId(userId)
                .subscribeTime(toLocalDateTime(wxMpUser.getSubscribeTime()))
                .subscribeStatus(wxMpUser.getSubscribe() ? 1 : 0)
                .build();
    };
}
