package com.gic.haoban.manage.service.service.notify.impl;

import com.gic.api.base.commons.BasePageInfo;
import com.gic.api.base.commons.Page;
import com.gic.commons.util.PageHelperUtils;
import com.gic.commons.util.UniqueIdUtils;
import com.gic.haoban.common.utils.EntityUtil;
import com.gic.haoban.manage.service.dao.mapper.NoticeMessageMapper;
import com.gic.haoban.manage.service.entity.TabNoticeMessage;
import com.gic.haoban.manage.service.pojo.bo.NoticeMessageBO;
import com.gic.haoban.manage.service.pojo.bo.PendingTaskBO;
import com.gic.haoban.manage.service.service.notify.NoticeMessageService;
import com.github.pagehelper.PageHelper;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created 2021/12/17.
 *
 * @author hua
 */
@Service
public class NoticeMessageServiceImpl implements NoticeMessageService {

    @Autowired
    private NoticeMessageMapper noticeMessageMapper;

    @Override
    public boolean addOrCreateNoticeMessage(NoticeMessageBO messageBO) {
        TabNoticeMessage tabNoticeMessage = EntityUtil.changeEntityByOrika(TabNoticeMessage.class, messageBO);
        tabNoticeMessage.setUpdateTime(new Date());
        if (tabNoticeMessage.getNoticeMessageId() == null) {
            tabNoticeMessage.setCreateTime(new Date());
            tabNoticeMessage.setNoticeMessageId(UniqueIdUtils.uniqueLong());
            noticeMessageMapper.insertSelective(tabNoticeMessage);
            return true;
        }
        noticeMessageMapper.updateByPrimaryKeySelective(tabNoticeMessage);
        return true;
    }

    @Override
    public void addNoticeMessageBatch(List<NoticeMessageBO> list) {
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        List<TabNoticeMessage> noticeMessageList = EntityUtil.changeEntityListByJSON(TabNoticeMessage.class, list);
        for (TabNoticeMessage noticeMessage : noticeMessageList) {
            noticeMessage.setNoticeMessageId(UniqueIdUtils.uniqueLong());
            noticeMessage.setCreateTime(new Date());
            noticeMessage.setUpdateTime(new Date());
        }
        noticeMessageMapper.insertBatch(noticeMessageList);
    }

    @Override
    public Page<NoticeMessageBO> pageNoticeMessage(String enterpriseId, String storeId, String clerkId,
            int categoryType, BasePageInfo pageInfo) {
        PageHelper.startPage(pageInfo);
        List<TabNoticeMessage> list = noticeMessageMapper.listNoticeMessage(enterpriseId, storeId, clerkId,
                categoryType);
        Page<NoticeMessageBO> retPage = PageHelperUtils.changePageHelperToCurrentPage(list, NoticeMessageBO.class);
        return retPage;
    }

    @Override
    public int countUnreadNoticeMessage(String enterpriseId, String storeId, String clerkId) {
        return noticeMessageMapper.countUnreadNoticeMessage(enterpriseId, storeId, clerkId, -1);
    }

    @Override
    public boolean updateNoticeMessageReadFlag(String enterpriseId, String clerkId, int categoryType) {
        noticeMessageMapper.updateNoticeMessageReadFlag(enterpriseId, clerkId, categoryType);
        return true;
    }
}
