移动系统liao
2025-05-01 a247547df86f0fad8f03aebb91de68d3f2bc7918
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
using Furion;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
 
namespace cylsg.Core
{
    /// <summary>
    /// 仓储类,使用方式只需要继承就行
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class BaseRepository<T> : SimpleClient<T> where T : class, new()
    {
 
 
        public BaseRepository(ISqlSugarClient context = null) : base(context)//默认值等于null不能少
        {
            base.Context = App.GetService<ISqlSugarClient>();//用手动获取方式支持切换仓储
 
        }
        /// <summary>
        /// 重写更新方法
        /// </summary>
        /// <param name="updateObj"></param>
        /// <returns></returns>
        public Task<bool> EzUpdateAsync(T updateObj)
        {
 
            var intbs = updateObj as BaseModelBase;
            if (intbs != null)
            {
                intbs.UpDataBy = GetJwtBy();
                intbs.UpDataTime = DateTime.Now;
            }
            return base.UpdateAsync(updateObj);
        }
        /// <summary>
        /// 异步插入
        /// </summary>
        /// <param name="insertObj"></param>
        /// <returns></returns>
        public Task<bool> EzInsertAsync(T insertObj)
        {
            var intbs = insertObj as BaseModelBase;
            if (intbs != null)
            {
                intbs.CreateBy = GetJwtBy();
                intbs.CreateTime = DateTime.Now;
            }
 
            return base.InsertAsync(insertObj);
        }
        /// <summary>
        /// 重写插入
        /// </summary>
        /// <param name="insertObj"></param>
        /// <returns></returns>
        public T EzInsertReturnEntity(T insertObj)
        {
            var intbs = insertObj as BaseModelBase;
            if (intbs != null)
            {
                intbs.CreateBy = GetJwtBy();
                intbs.CreateTime = DateTime.Now;
            }
            return base.InsertReturnEntity(insertObj);
        }
 
        /// <summary>
        /// 异步插入
        /// </summary>
        /// <param name="insertObj"></param>
        /// <returns></returns>
        public Task<int> EzInsertReturnIdentityAsync(T insertObj)
        {
 
            var intbs = insertObj as BaseModelBase;
            if (intbs != null)
            {
                intbs.CreateBy = GetJwtBy();
                intbs.CreateTime = DateTime.Now;
            }
            return base.InsertReturnIdentityAsync(insertObj);
        }
        /// <summary>
        /// 获取用户姓名和ID
        /// </summary>
        /// <returns></returns>
        private string GetJwtBy()
        {
            return (App.User?.FindFirstValue("UserID") ?? "") + "|" + (App.User?.FindFirstValue("NickName") ?? "系统自主处理");
        }
    }
    }