package com.zanxiang.game.module.sdk.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zanxiang.game.module.mybatis.entity.User;
import com.zanxiang.game.module.mybatis.entity.UserShare;
import com.zanxiang.game.module.mybatis.mapper.UserShareMapper;
import com.zanxiang.game.module.sdk.pojo.dto.GameAppletDTO;
import com.zanxiang.game.module.sdk.pojo.param.UserData;
import com.zanxiang.game.module.sdk.pojo.vo.GameShareVO;
import com.zanxiang.game.module.sdk.service.IGameAppletService;
import com.zanxiang.game.module.sdk.service.IUserShareService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @author : lingfeng
 * @time : 2023-01-16
 * @description : 用户分享
 */
@Slf4j
@Service
public class UserShareServiceImpl extends ServiceImpl<UserShareMapper, UserShare> implements IUserShareService {

    @Autowired
    private IGameAppletService gameAppletService;

    @Override
    public GameShareVO getGameShare(UserData userData) {
        GameAppletDTO gameAppletDTO = gameAppletService.getByGameId(userData.getGameId());
        String path = gameAppletDTO.getSharePath();
        if (userData.getUserId() != null) {
            path = path + "?shareUserId=" + userData.getUserId();
        }
        return GameShareVO.builder()
                .title(gameAppletDTO.getShareTitle())
                .shareImg(gameAppletDTO.getShareImg())
                .path(path)
                .build();
    }

    @Override
    public GameShareVO getShowCard(UserData userData) {
        GameAppletDTO gameAppletDTO = gameAppletService.getByGameId(userData.getGameId());
        return GameShareVO.builder()
                .title(gameAppletDTO.getShowCardTitle())
                .shareImg(gameAppletDTO.getShowCardImg())
                .path(gameAppletDTO.getSharePath())
                .build();
    }

    @Override
    public void createShareLog(User user, UserData userData) {
        super.save(UserShare.builder()
                .gameId(userData.getGameId())
                .fromUserId(user.getShareUserId())
                .toUserId(user.getId())
                .createTime(LocalDateTime.now())
                .updateTime(LocalDateTime.now())
                .build());
    }
}
