package com.playlet.web.service.app.impl;

import com.playlet.system.domain.PlayletPublicDetail;
import com.playlet.system.domain.PlayletPublicUser;
import com.playlet.system.domain.PublicDetailShare;
import com.playlet.system.service.IPlayletPublicDetailService;
import com.playlet.system.service.IPlayletPublicUserService;
import com.playlet.system.service.IPublicDetailShareService;
import com.playlet.web.service.app.PublicDetailShareAppService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class PublicDetailShareAppServiceImpl implements PublicDetailShareAppService {

    private final IPublicDetailShareService iPublicDetailShareService;

    private final IPlayletPublicUserService iPlayletPublicUserService;

    private final IPlayletPublicDetailService iPlayletPublicDetailService;

    @Override
    public void addRecord(PublicDetailShare publicDetailShare) {
        // 翻译
        PlayletPublicUser share = iPlayletPublicUserService.selectPlayletPublicUserById(publicDetailShare.getAuthorId());
        PlayletPublicUser userOne = iPlayletPublicUserService.selectPlayletPublicUserById(publicDetailShare.getReaderOneId());
        PlayletPublicUser userTwo = iPlayletPublicUserService.selectPlayletPublicUserById(publicDetailShare.getReaderTwoId());
        publicDetailShare.setCreateTime(new Date());
        if(share != null){
            publicDetailShare.setAuthorName(share.getName());
        }
        if(userOne != null){
            publicDetailShare.setReaderOneName(userOne.getName());
        }
        if(userTwo != null){
            publicDetailShare.setReaderTwoName(userTwo.getName());
        }
        // 查询是否二次记录
        long readCount = iPublicDetailShareService.lambdaQuery()
                .eq(PublicDetailShare::getReaderOneId, publicDetailShare.getReaderOneId())
                .eq(PublicDetailShare::getReaderTwoId, publicDetailShare.getReaderTwoId())
                .count();

        long beReadCount = iPublicDetailShareService.lambdaQuery()
                .eq(PublicDetailShare::getReaderOneId, publicDetailShare.getReaderTwoId())
                .eq(PublicDetailShare::getReaderTwoId, publicDetailShare.getReaderOneId())
                .count();
        // 已经阅读或者查看过了
        boolean flag = readCount + beReadCount > 0;
        iPublicDetailShareService.insertPublicDetailShare(publicDetailShare);

        // 添加浏览记录时,同时需要增加一下文章表上面的阅读次数
        PlayletPublicDetail playletPublicDetail = iPlayletPublicDetailService.getById(publicDetailShare.getDetailId());
        if(playletPublicDetail != null && !flag){
            if(playletPublicDetail.getTransmitCount() == null){
                playletPublicDetail.setTransmitCount(1L);
            }else {
                playletPublicDetail.setTransmitCount(playletPublicDetail.getTransmitCount() + 1);
            }
            if(playletPublicDetail.getReadCount() == null){
                playletPublicDetail.setReadCount(1L);
            }else {
                playletPublicDetail.setReadCount(playletPublicDetail.getReadCount() + 1);
            }
            iPlayletPublicDetailService.updateById(playletPublicDetail);
        }
    }
}
