package com.fs.crm.mapper;

import com.fs.crm.domain.CrmMsg;
import com.fs.crm.param.CrmMsgListQueryParam;
import com.fs.crm.vo.CrmMsgListQueryVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 *  消息Mapper接口
 *
 * @author fs
 * @date 2022-12-21
 */
public interface CrmMsgMapper
{
    /**
     * 查询 消息
     *
     * @param msgId  消息ID
     * @return  消息
     */
    public CrmMsg selectCrmMsgById(Long msgId);

    /**
     * 查询 消息列表
     *
     * @param crmMsg  消息
     * @return  消息集合
     */
    public List<CrmMsg> selectCrmMsgList(CrmMsg crmMsg);

    /**
     * 新增 消息
     *
     * @param crmMsg  消息
     * @return 结果
     */
    public int insertCrmMsg(CrmMsg crmMsg);

    /**
     * 修改 消息
     *
     * @param crmMsg  消息
     * @return 结果
     */
    public int updateCrmMsg(CrmMsg crmMsg);

    /**
     * 删除 消息
     *
     * @param msgId  消息ID
     * @return 结果
     */
    public int deleteCrmMsgById(Long msgId);

    /**
     * 批量删除 消息
     *
     * @param msgIds 需要删除的数据ID
     * @return 结果
     */
    public int deleteCrmMsgByIds(Long[] msgIds);
    @Select({"<script> " +
            "select m.* from crm_msg m " +
            "where 1=1 " +
            "<if test = 'maps.companyId != null     '> " +
            "and m.company_id =#{maps.companyId} " +
            "</if>" +
            "<if test = 'maps.companyUserId != null     '> " +
            "and m.company_user_id =#{maps.companyUserId} " +
            "</if>" +
            "<if test = 'maps.msgType != null     '> " +
            "and m.msg_type =#{maps.msgType} " +
            "</if>" +
            "<if test = 'maps.isRead != null     '> " +
            "and m.is_read =#{maps.isRead} " +
            "</if>" +
            " order by m.msg_id desc "+
            "</script>"})
    List<CrmMsgListQueryVO> selectCrmMsgListQuery(@Param("maps") CrmMsgListQueryParam param);
    @Select("select IFNULL(count( 0),0) from crm_msg where company_user_id=#{userId} and is_read=0 ")
    Long selectCrmMsgCountByUserId(Long userId);
    @Update("update  crm_msg set is_read=1 where company_user_id=#{userId} ")
    int setAllRead(Long userId);
    @Select("select IFNULL(count( msg_type),0) from crm_msg where company_user_id=#{userId} and is_read=0 and msg_type=#{type}")
    Long selectCrmMsgCountByUserIdAndType(@Param("userId") Long userId, @Param("type") int type);
    @Select("select title from crm_msg where company_user_id=#{userId} and is_read=0 and msg_type=#{type} order by msg_id desc limit 1")
    String  selectCrmNewMsgByUserId(@Param("userId") Long userId, @Param("type") int type);
    @Update("update  crm_msg set is_read=1 where company_user_id=#{userId} and msg_type=#{type} ")
    int setReadByType(@Param("userId") Long userId, @Param("type") int type);
}
