package com.sunxung.factoring.mapper.sys.systemMsg;

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

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import com.sunxung.factoring.entity.sys.systemMsg.SystemMsg;
import com.sunxung.factoring.entity.sys.systemMsg.SystemMsgUserRel;
import com.sunxung.factoring.entity.sys.systemMsg.vo.SearchSystemMsg;

/**
 * @description:消息Mapper
 * @author:吴端丽
 * @e-mail:1912942707@qq.com
 * @date:2018年3月20日 下午3:42:11
 */
@Mapper
public interface SystemMsgMapper {

    /**
     * 消息管理列表查询. <br/>
     * 
     * @param search
     * @return
     */
    List<SystemMsg> findBySearch(SearchSystemMsg search);

    Integer countBySearch(SearchSystemMsg search);

    /**
     * 已发送消息列表查询. <br/>
     * 
     * @param search
     * @return
     */
    List<SystemMsg> findSendMsgBySearch(SearchSystemMsg search);

    Integer countSendMsgBySearch(SearchSystemMsg search);

    void add(SystemMsg systemMsg);

    SystemMsg get(Long id);

    Integer delete(Long id);

    void addSystemMsgUser(@Param("systemMsgId") Long systemMsgId, @Param("receiverUserId") Long receiverUserId,
            @Param("readState") boolean readState, @Param("receiverRemove") boolean receiverRemove);

    /**
     * 根据消息id和接收人ID删除关联数据. <br/>
     * 
     * @param systemMsgId
     * @param receiverUserId
     * @return
     */
    Integer deleteBySystemMsgUserId(@Param("systemMsgId") Long systemMsgId,
            @Param("receiverUserId") Long receiverUserId);

    /**
     * 修改阅读状态. <br/>
     * 
     * @param id
     * @param readState
     */
    Integer updateReadState(@Param("id") Long id, @Param("userId") Long userId, @Param("readState") Boolean readState);

    /**
     * 根据消息ID，和是否已读查询关联表. <br/>
     * 
     * @param systemMsgId
     * @return
     */
    Integer countBySystemMsgId(@Param("systemMsgId") Long systemMsgId, @Param("readState") Boolean readState);

    /**
     * 根据消息ID查询消息接收人ID. <br/>
     * 
     * @param systemMsgId
     * @return
     */
    List<Long> findReceiverUserIdBysystemMsgId(@Param("systemMsgId") Long systemMsgId,
            @Param("readState") Boolean readState);

    /**
     * 根据消息ID删除关联表数据. <br/>
     * 
     * @param systemMsgId
     * @return
     */
    Integer deleteSystemMsgUserByMsgId(Long systemMsgId);

    /**
     * 根据已发消息ID逻辑删除消息. <br/>
     * 
     * @param id
     * @param remove
     */
    Integer updateSystemMsgRemove(@Param("id") Long id, @Param("remove") Boolean remove);

    /**
     * 查询所有待发送的定时消息. <br/>
     * 
     * @param sendType
     * @param pushTimeStart
     * @param pushTimeEnd
     * @return
     */
    List<Long> findTimingMessage(@Param("sendType") String sendType, @Param("pushTimeStart") Date pushTimeStart,
            @Param("pushTimeEnd") Date pushTimeEnd);

    /**
     * 根据消息ID修改消息状态. <br/>
     * 
     * @param id
     * @param pushState
     * @return
     */
    Integer updatePushStateById(@Param("id") Long id, @Param("pushState") Boolean pushState);

    /**
     * 根据消息ID修改用户消息中间表的接收人是否已删除. <br/>
     * 
     * @param systemMsgId
     * @param receiverRemove
     * @return
     */
    Integer updateReceiverRemoveByMsgId(@Param("systemMsgId") Long systemMsgId,
            @Param("receiverRemove") Boolean receiverRemove, @Param("userId") Long userId);

    /**
     * 根据消息ID查询消息用户关联中间表. <br/>
     * 
     * @param systemMsgId
     * @return
     */
    List<SystemMsgUserRel> findBySystemMsgId(Long systemMsgId);

    /**
     * 根据接收人、消息ID查询关联中间表数据条数. <br/>
     * 
     * @param systemMsgId
     * @param receiverUserId
     * @return
     */
    Integer countBySystemMsgIdAndReceiverUserId(@Param("systemMsgId") Long systemMsgId,
            @Param("receiverUserId") Long receiverUserId);

    /**
     * 获取指定业务数据流程处理过程中所产生的知晓者消息
     * 
     * @param businessId 业务Id
     * @param businessTypeId 业务类型（数据字典ID）
     * @param msgTypeId 消息类型，这里指知晓者消息类型
     * @return
     */
    List<SystemMsg> findKnownewsByBusiness(@Param("businessId") Long businessId,
            @Param("businessTypeId") Long businessTypeId, @Param("msgTypeId") Long msgTypeId);
}
