package com.cool.store.service.impl;

import com.cool.store.dao.LineFollowLogDAO;
import com.cool.store.dao.LineInfoDAO;
import com.cool.store.entity.LineFollowLogDO;
import com.cool.store.entity.LineInfoDO;
import com.cool.store.enums.ErrorCodeEnum;
import com.cool.store.exception.ServiceException;
import com.cool.store.request.LineFollowLogRequest;
import com.cool.store.service.LineFollowService;
import com.cool.store.vo.log.LineFollowLogVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @author zhangchenbiao
 * @FileName: LineFollowServiceImpl
 * @Description:
 * @date 2024-03-27 14:49
 */
@Service
public class LineFollowServiceImpl implements LineFollowService {

    @Resource
    private LineFollowLogDAO lineFollowLogDAO;
    @Resource
    private LineInfoDAO lineInfoDAO;

    @Override
    public PageInfo<LineFollowLogVO> getFollowLogPage(Long lineId, Integer pageNum, Integer pageSize) {
        Page<LineFollowLogDO> followLogPage = lineFollowLogDAO.getFollowLogPage(lineId, pageNum, pageSize);
        PageInfo resultPage = new PageInfo(followLogPage);
        List<LineFollowLogVO> resultList = LineFollowLogVO.convertList(followLogPage);
        resultPage.setList(resultList);
        return resultPage;
    }

    @Override
    public Long addFollowLog(LineFollowLogRequest followLog, String operateUserId, String operateUsername) {
        LineInfoDO lineInfo = lineInfoDAO.getLineInfo(followLog.getLineId());
        if(Objects.isNull(lineInfo)){
            throw new ServiceException(ErrorCodeEnum.LINE_ID_IS_NOT_EXIST);
        }
        return lineFollowLogDAO.addFollowLog(lineInfo, operateUserId, operateUsername, followLog.getMessage());
    }
}
