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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/***********************************************************************
 *            Project: baifenBinfa
 *        ProjectName: 百分兵法管理系统                               
 *                Web: http://chuanyin.com                     
 *             Author:                                        
 *              Email:                               
 *         CreateTime: 202403/02   
 *        Description: 暂无
 ***********************************************************************/
 
using System;
using System.Linq;
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 Microsoft.Extensions.DependencyInjection;
using SqlSugar;
 
 
namespace CoreCms.Net.Services
{
    /// <summary>
    /// 微信订阅消息存储表 接口实现
    /// </summary>
    public class CoreCmsUserWeChatMsgSubscriptionServices : BaseServices<CoreCmsUserWeChatMsgSubscription>, ICoreCmsUserWeChatMsgSubscriptionServices
    {
        private readonly ICoreCmsUserWeChatMsgSubscriptionRepository _dal;
        private readonly IServiceProvider _serviceProvider;
 
        private readonly IUnitOfWork _unitOfWork;
        public CoreCmsUserWeChatMsgSubscriptionServices(IUnitOfWork unitOfWork, ICoreCmsUserWeChatMsgSubscriptionRepository dal, IServiceProvider serviceProvider)
        {
            this._dal = dal;
            _serviceProvider = serviceProvider;
            base.BaseDal = dal;
            _unitOfWork = unitOfWork;
        }
 
 
        /// <summary>
        /// 获取模板信息
        /// </summary>
        /// <returns></returns>
        public async Task<WebApiCallBack> tmpl(int userId)
        {
            var jm = new WebApiCallBack();
 
            using var container = _serviceProvider.CreateScope();
 
            var templateServices = container.ServiceProvider.GetService<ICoreCmsUserWeChatMsgTemplateServices>();
 
            //支付通知|发货通知
            var arr = new string[] { "pay", "ship", "cancel" };
 
            var list = await templateServices.QueryListByClauseAsync(p => arr.Contains(p.templateTitle), p => p.id, OrderByType.Asc);
            var arrTitle = list.Select(p => p.templateId).ToList();
 
            jm.status = true;
            jm.data = arrTitle;
            jm.msg = "获取成功";
 
            return jm;
        }
 
 
        /// <summary>
        /// 设置订阅状态
        /// </summary>
        /// <returns></returns>
        public async Task<WebApiCallBack> SetTip(int userId, string templateId, string status)
        {
            var jm = new WebApiCallBack();
 
            using var container = _serviceProvider.CreateScope();
 
            var templateServices = container.ServiceProvider.GetService<ICoreCmsUserWeChatMsgTemplateServices>();
            var subscriptionServices = container.ServiceProvider.GetService<ICoreCmsUserWeChatMsgSubscriptionServices>();
 
            var setting = await templateServices.QueryAsync();
            var type = "";
 
            if (setting.Any())
            {
                foreach (var item in setting)
                {
                    if (item.templateId == templateId)
                    {
                        type = item.templateTitle;
                        break;
                    }
                }
            }
            var count = await subscriptionServices.GetCountAsync(p => p.userId == userId && p.type == type);
 
            if (status == "accept")
            {
                if (count < 1)
                {
                    var sub = new CoreCmsUserWeChatMsgSubscription();
                    sub.userId = userId;
                    sub.templateId = templateId;
                    sub.type = type;
                    await subscriptionServices.InsertAsync(sub);
                }
                else
                {
                    await subscriptionServices.UpdateAsync(
                        p => new CoreCmsUserWeChatMsgSubscription() { templateId = templateId },
                        p => p.userId == userId && p.type == type);
                }
            }
            else
            {
                if (count > 0)
                {
                    await subscriptionServices.DeleteAsync(p => p.userId == userId && p.type == type);
                }
            }
            jm.status = true;
            jm.msg = "设置订阅状态成功";
 
            return jm;
        }
 
    }
}