username@email.com
2024-09-09 cc170291673472d3cda8d7ea77f6bd3a3b5dbb83
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/***********************************************************************
 *            Project: baifenBinfa
 *        ProjectName: 百分兵法管理系统                               
 *                Web: http://chuanyin.com                     
 *             Author:                                        
 *              Email:                               
 *         CreateTime: 202403/02   
 *        Description: 暂无
 ***********************************************************************/
 
using System;
using System.Threading.Tasks;
using CoreCms.Net.Configuration;
using CoreCms.Net.IRepository;
using CoreCms.Net.IRepository.UnitOfWork;
using CoreCms.Net.IServices;
using CoreCms.Net.Loging;
using CoreCms.Net.Model.Entities;
using CoreCms.Net.Model.ViewModels.UI;
using CoreCms.Net.Utility.Helper;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
 
 
namespace CoreCms.Net.Services
{
    /// <summary>
    /// 消息发送表 接口实现
    /// </summary>
    public class CoreCmsMessageServices : BaseServices<CoreCmsMessage>, ICoreCmsMessageServices
    {
        private readonly ICoreCmsMessageRepository _dal;
        private readonly IUnitOfWork _unitOfWork;
        public CoreCmsMessageServices(IUnitOfWork unitOfWork, ICoreCmsMessageRepository dal)
        {
            this._dal = dal;
            base.BaseDal = dal;
            _unitOfWork = unitOfWork;
        }
 
        /// <summary>
        /// 站内消息
        /// </summary>
        /// <param name="userId">接受者id</param>
        /// <param name="code">模板编码</param>
        /// <param name="parameters">参数</param>
        /// <returns></returns>
        public async Task<WebApiCallBack> Send(int userId, string code, JObject parameters)
        {
            var jm = new WebApiCallBack();
 
            var content = MessageHelper.GetTemp(code, parameters);
            if (string.IsNullOrEmpty(content))
            {
                jm.msg = GlobalErrorCodeVars.Code10009;
                return jm;
            }
 
            var msg = new CoreCmsMessage
            {
                userId = userId,
                code = code,
                parameters = JsonConvert.SerializeObject(parameters),
                contentBody = content,
                status = false,
                createTime = DateTime.Now
            };
 
            var bl = await _dal.InsertAsync(msg) > 0;
            jm.status = bl;
            jm.msg = bl ? "站内消息发布成功" : "站内消息发布失败";
 
            return jm;
        }
 
 
        /// <summary>
        /// 消息查看,更新已读状态
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<WebApiCallBack> info(int userId, int id)
        {
            var jm = new WebApiCallBack { status = true };
 
            var info = await _dal.QueryByClauseAsync(p => p.userId == userId && p.id == id);
            if (info != null)
            {
                await _dal.UpdateAsync(p => new CoreCmsMessage() { status = true }, p => p.id == info.id);
            }
 
            return jm;
        }
 
        /// <summary>
        /// 判断是否有新消息
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public async Task<bool> HasNew(int userId)
        {
            var bl = await _dal.ExistsAsync(p => p.userId == userId && p.status == false);
            return bl;
        }
 
 
    }
}