
package com.tmerclub.cloud.im.service.impl;

import com.tmerclub.cloud.common.database.dto.PageDTO;
import com.tmerclub.cloud.common.database.util.PageUtil;
import com.tmerclub.cloud.common.database.vo.PageVO;
import com.tmerclub.cloud.im.constants.WhoSend;
import com.tmerclub.cloud.im.mapper.ImMsgBizEmployeeRecentMapper;
import com.tmerclub.cloud.im.model.ImMsgBizEmployeeRecent;
import com.tmerclub.cloud.im.model.ImMsgBizUserHistory;
import com.tmerclub.cloud.im.service.ImMsgBizEmployeeRecentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Objects;

/**
 * 员工最近聊天记录表
 *
 * @author tmerclub
 * @date 2021-08-25 13:58:56
 */
@Service
public class ImMsgBizEmployeeRecentServiceImpl implements ImMsgBizEmployeeRecentService {

    @Autowired
    private ImMsgBizEmployeeRecentMapper imMsgBizEmployeeRecentMapper;

    @Override
    public PageVO<ImMsgBizEmployeeRecent> pageBySendTypeAndBizIdAndEmployeeId(PageDTO pageDTO, Integer sendType, Long bizId, Long employeeId) {
        return PageUtil.doPage(pageDTO, () -> imMsgBizEmployeeRecentMapper.listBySendTypeAndBizIdAndEmployeeId(sendType, bizId, employeeId));
    }

    @Override
    public ImMsgBizEmployeeRecent getById(Long id) {
        return imMsgBizEmployeeRecentMapper.getById(id);
    }

    @Override
    public ImMsgBizEmployeeRecent getBySendTypeBizIdEmployeeIdUserId(Integer sendType, Long bizId, Long employeeId, Long userId) {
        return imMsgBizEmployeeRecentMapper.getBySendTypeBizIdEmployeeIdUserId(sendType, bizId, employeeId, userId);
    }

    @Override
    public void save(ImMsgBizEmployeeRecent imMsgBizEmployeeRecent) {
        imMsgBizEmployeeRecentMapper.save(imMsgBizEmployeeRecent);
    }

    @Override
    public void update(ImMsgBizEmployeeRecent imMsgBizEmployeeRecent) {
        imMsgBizEmployeeRecentMapper.update(imMsgBizEmployeeRecent);
    }

    @Override
    public void deleteById(Long id) {
        imMsgBizEmployeeRecentMapper.deleteById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateEmployeeRecent(ImMsgBizUserHistory imMsgBizUserHistory) {
        ImMsgBizEmployeeRecent imMsgBizEmployeeRecent = this.getBySendTypeBizIdEmployeeIdUserId(imMsgBizUserHistory.getSendType(), imMsgBizUserHistory.getBizId(), imMsgBizUserHistory.getEmployeeId(), imMsgBizUserHistory.getUserId());
        if (Objects.isNull(imMsgBizEmployeeRecent)) {
            imMsgBizEmployeeRecent = new ImMsgBizEmployeeRecent();
            imMsgBizEmployeeRecent.setSendType(imMsgBizUserHistory.getSendType());
            imMsgBizEmployeeRecent.setBizId(imMsgBizUserHistory.getBizId());
            imMsgBizEmployeeRecent.setUserId(imMsgBizUserHistory.getUserId());
            imMsgBizEmployeeRecent.setEmployeeId(imMsgBizUserHistory.getEmployeeId());
            imMsgBizEmployeeRecent.setEmployeeUnreadNum(0);
            imMsgBizEmployeeRecent.setCreateTime(new Date());
            imMsgBizEmployeeRecent.setUpdateTime(new Date());
            this.save(imMsgBizEmployeeRecent);
        } else {
            if (Objects.equals(imMsgBizUserHistory.getWhoSend(), WhoSend.USER)) {
                imMsgBizEmployeeRecent.setEmployeeUnreadNum(imMsgBizEmployeeRecent.getEmployeeUnreadNum() + 1);
            }
            imMsgBizEmployeeRecent.setUpdateTime(new Date());
            this.update(imMsgBizEmployeeRecent);
        }
    }

    @Override
    public void updateEmployeeUnreadMsgNum(Integer sendType, Long id, Long shopId, Long employeeId, Long userId) {
        imMsgBizEmployeeRecentMapper.updateEmployeeUnreadMsgNum(sendType, id, shopId, employeeId, userId);
    }
}
