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

import com.playlet.system.domain.PlayletPublicDetail;
import com.playlet.system.domain.PublicUserRecord;
import com.playlet.system.service.IPlayletPublicDetailService;
import com.playlet.system.service.IPublicUserRecordService;
import com.playlet.web.service.app.PublicUserRecordAppService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class PublicUserRecordAppServiceImpl implements PublicUserRecordAppService {

    private final IPublicUserRecordService iPublicUserRecordService;

    private final IPlayletPublicDetailService iPlayletPublicDetailService;

    @Override
    public void addRecord(PublicUserRecord userRecord) {
        iPublicUserRecordService.insertPublicUserRecord(userRecord);
        // 添加浏览记录时,同时需要增加一下文章表上面的阅读次数
        PlayletPublicDetail playletPublicDetail = iPlayletPublicDetailService.getById(Long.valueOf(userRecord.getRemark()));
        if(playletPublicDetail != null){
            if(playletPublicDetail.getReadCount() == null){
                playletPublicDetail.setReadCount(1L);
            }else {
                // 实时统计阅读数
                long count = iPublicUserRecordService.lambdaQuery().eq(PublicUserRecord::getRemark, userRecord.getRemark()).count();
                playletPublicDetail.setReadCount(count);
            }
            iPlayletPublicDetailService.updateById(playletPublicDetail);
        }
    }

    @Override
    public void updateRecord(PublicUserRecord userRecord) {
        iPublicUserRecordService.updateById(userRecord);
    }
}
