package com.huiju.foyo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huiju.foyo.dao.TaCaseMapper;
import com.huiju.foyo.dao.TaLikeLogMapper;
import com.huiju.foyo.model.TaCase;
import com.huiju.foyo.model.TaLikeLog;
import com.huiju.foyo.service.ITaLikeLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class TaLikeLogServiceImpl extends ServiceImpl<TaLikeLogMapper, TaLikeLog> implements ITaLikeLogService {

    @Autowired
    TaLikeLogMapper taLikeLogMapper;

    @Autowired
    TaCaseMapper taCaseMapper;

    @Override
    public Integer postCaseLike(TaCase taCase, TaLikeLog taLikeLog) {
        // 校验当前是否已经点过赞
        QueryWrapper<TaLikeLog> wrapper = new QueryWrapper<>();
        wrapper.eq("agent", taLikeLog.getAgent());
        wrapper.eq("target", taLikeLog.getTarget());
        wrapper.eq("target_type", taLikeLog.getTargetType());

        Integer count = taLikeLogMapper.selectCount(wrapper);
        if (count > 0) {
            return null != taCase.getLikeNum() ? taCase.getLikeNum() : 0;
        }

        taLikeLogMapper.insert(taLikeLog);
        taCaseMapper.increase(taCase.getId(), 1);
        taCase = taCaseMapper.selectById(taCase.getId());
        return taCase.getLikeNum();
    }
}
