
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.ImMsgBizUserMapper;
import com.tmerclub.cloud.im.model.ImMsgBizUser;
import com.tmerclub.cloud.im.model.ImMsgBizUserHistory;
import com.tmerclub.cloud.im.service.ImMsgBizUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.ZoneId;
import java.util.Date;
import java.util.Objects;

/**
 * 用户最近聊天记录表
 *
 * @author tmerclub
 * @date 2021-08-25 13:58:56
 */
@Service
public class ImMsgBizUserServiceImpl implements ImMsgBizUserService {

    @Autowired
    private ImMsgBizUserMapper imMsgBizUserMapper;

    @Override
    public PageVO<ImMsgBizUser> pageByUserId(PageDTO pageDTO, Long userId) {
        return PageUtil.doPage(pageDTO, () -> imMsgBizUserMapper.listByUserId(userId));
    }

    @Override
    public ImMsgBizUser getById(Long id) {
        return imMsgBizUserMapper.getById(id);
    }

    @Override
    public ImMsgBizUser getBySendTypeBizIdUserId(Integer sendType, Long bizId, Long userId) {
        return imMsgBizUserMapper.getBySendTypeAndBizIdAndUserId(sendType, bizId, userId);
    }

    @Override
    public void save(ImMsgBizUser imMsgBizUser) {
        imMsgBizUserMapper.save(imMsgBizUser);
    }

    @Override
    public void update(ImMsgBizUser imMsgBizUser) {
        imMsgBizUserMapper.update(imMsgBizUser);
    }

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

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateBizUserLastMsg(ImMsgBizUserHistory imMsgBizUserHistory) {
        ImMsgBizUser imMsgBizUser = this.getBySendTypeBizIdUserId(imMsgBizUserHistory.getSendType(), imMsgBizUserHistory.getBizId(), imMsgBizUserHistory.getUserId());
        if (Objects.nonNull(imMsgBizUser)) {
            imMsgBizUser.setEmployeeId(imMsgBizUserHistory.getEmployeeId());
            if (Objects.equals(imMsgBizUserHistory.getWhoSend(), WhoSend.EMPLOYEE)) {
                imMsgBizUser.setUserUnreadNum(imMsgBizUser.getUserUnreadNum() + 1);
            } else if (Objects.equals(imMsgBizUserHistory.getWhoSend(), WhoSend.USER)) {
                imMsgBizUser.setBizUnreadNum(imMsgBizUser.getBizUnreadNum() + 1);
            }
            imMsgBizUser.setLastMsgId(imMsgBizUserHistory.getId());
            imMsgBizUser.setLastMsgType(imMsgBizUserHistory.getType());
            imMsgBizUser.setLastMsgContent(imMsgBizUserHistory.getContent());
            imMsgBizUser.setLastMsgWhoSend(imMsgBizUserHistory.getWhoSend());
            imMsgBizUser.setLastMsgTime(imMsgBizUserHistory.getCreateTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime());
            imMsgBizUser.setUpdateTime(new Date());
            this.update(imMsgBizUser);
        }
    }

    @Override
    public Integer getImUserUnreadNum(Long userId) {
        Integer num = imMsgBizUserMapper.getImUserUnreadNum(userId);
        return ((num == null) ? 0 : num);
    }

    @Override
    public void updateUserUnreadHistor(Long imMsgBizUserId, Integer userUnread, Integer whoSend) {
        imMsgBizUserMapper.updateUserUnreadHistor(imMsgBizUserId, userUnread, whoSend);
    }

    @Override
    public void updateShopUserUnreadMsgNum(Long id, Integer unread, Integer whoSend) {
        imMsgBizUserMapper.updateShopUserUnreadMsgNum(id, unread, whoSend);
    }
}
