Browse Source

导入社区

qinchaoyue 1 tháng trước cách đây
mục cha
commit
c74a939c19

+ 49 - 0
SnapshotWinFormsApp/Application/CommunityInfoApplication.cs

@@ -0,0 +1,49 @@
+using Mapster;
+using SnapshotWinFormsApp.Entities.NewHotline;
+using SnapshotWinFormsApp.Entities.OldHotline;
+using SnapshotWinFormsApp.Repository;
+using SnapshotWinFormsApp.Repository.Interfaces;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace SnapshotWinFormsApp.Application;
+
+public class CommunityInfoApplication
+{
+    private readonly IBaseRepository<CommunityInfo> _communityInfoRepo;
+    private readonly IRepository<SSP_AreaEntity> _areaRepo;
+    public CommunityInfoApplication(DbSqlServer sqlServerDB)
+    {
+        _communityInfoRepo = new BaseRepository<CommunityInfo>(sqlServerDB, "自贡");
+        _areaRepo = new Repository<SSP_AreaEntity>(sqlServerDB, "自贡");
+    }
+
+    public async Task ImportCommunityInfoAsync(Action<string> log, CancellationToken token)
+    {
+        try
+        {
+            log($"正在查询旧数据...");
+            var items = await _areaRepo.GetAllAsync();
+
+            log($"共查询到{items.Count}条数据");
+            for (int i = 0;i < items.Count;i++)
+            {
+                var item = items[i];
+                var communitInfo = item.Adapt<CommunityInfo>();
+                var has = await _communityInfoRepo.Queryable()
+                    .AnyAsync(m => m.UniqueKey == communitInfo.UniqueKey, token);
+                if (has) continue;
+                await _communityInfoRepo.InsertAsync(communitInfo, token);
+                log($"{i}/{items.Count} 插入数据: {communitInfo.Id} {communitInfo.Name}");
+            }
+        }
+        catch (Exception e)
+        {
+            var msg = e.Message;
+            log($"导入数据异常: {e.Message}");
+        }
+    }
+}

+ 42 - 2
SnapshotWinFormsApp/Application/SnapshotUserInfoApplication.cs

@@ -4,12 +4,15 @@ using SnapshotWinFormsApp.Application.Dtos;
 using SnapshotWinFormsApp.Entities.NewHotline;
 using SnapshotWinFormsApp.Entities.OldHotline;
 using SnapshotWinFormsApp.Repository;
+using SnapshotWinFormsApp.Repository.Enum;
 using SnapshotWinFormsApp.Repository.Interfaces;
 using System;
 using System.Collections.Generic;
 using System.Linq;
+using System.Reflection;
 using System.Text;
 using System.Threading.Tasks;
+using System.Xml.Linq;
 
 namespace SnapshotWinFormsApp.Application;
 
@@ -19,6 +22,7 @@ public class SnapshotUserInfoApplication
     private readonly IRepository<WeChatUserEntity> _weChatRepo;
     private readonly IRepository<WeChatFollowUserEntity> _weChatFlowRepo;
     private readonly IBaseRepository<SnapshotUserInfo> _userRepo;
+    private readonly IRepository<Flow03_PushContent> _pushContentRepo;
 
     public SnapshotUserInfoApplication(DbSqlServer sqlServerDB)
     {
@@ -26,14 +30,50 @@ public class SnapshotUserInfoApplication
         _weChatRepo = new Repository<WeChatUserEntity>(sqlServerDB, "自贡");
         _weChatFlowRepo = new Repository<WeChatFollowUserEntity>(sqlServerDB, "自贡");
         _userRepo = new BaseRepository<SnapshotUserInfo>(sqlServerDB, "自贡");
+        _pushContentRepo = new Repository<Flow03_PushContent>(sqlServerDB, "自贡");
+    }
+
+    public async Task ImportGuiderInfoAsync(Action<string> log, CancellationToken token)
+    {
+        log($"正在查询旧数据...");
+        var guiderOldItems = await _pushContentRepo.Queryable()
+            .Where(m => m.MemberMobile != null && m.MemberName != null)
+            .GroupBy(m => new { m.MemberName, m.MemberMobile })
+            .Select(m => new { m.MemberName, m.MemberMobile })
+            .ToListAsync(token);
+
+        log($"共查询到{guiderOldItems.Count}条数据");
+        for (int i = 0;i < guiderOldItems.Count;i++)
+        {
+            var item = guiderOldItems[i];
+            if (token.IsCancellationRequested)
+            {
+                log("任务已取消");
+                return;
+            }
+            var userId = await _userRepo.Queryable()
+                .Where(m => m.PhoneNumber == item.MemberMobile)
+                .Select(m => m.Id)
+                .FirstAsync(token);
+            if (userId.IsNullOrEmpty())
+            {
+                var userInfo = new SnapshotUserInfo
+                {
+                    PhoneNumber = item.MemberMobile.Replace("\t", ""),
+                    Name = item.MemberName.Replace("\t", ""),
+                };
+                userInfo.CitizenType = EReadPackUserType.Guider;
+                userId = await _userRepo.InsertAsync(userInfo, token);
+                log($"{i}/{guiderOldItems.Count} 插入用户信息: {userId}, {userInfo.Name}, {userInfo.PhoneNumber}");
+            }
+        }
     }
 
     public async Task ImportSnapshotUserInfoAsync(Action<string> log, CancellationToken token)
     {
         log($"正在查询旧数据...");
         var items = await _weChatRepo.Queryable()
-            .LeftJoin<WeChatFollowUserEntity>((w, f) => w.WUR_unionid == f.unionid)
-            .Select((w, f) => new WeChatUserDto(), true)
+            .Select(m => new WeChatUserDto(), true)
             .ToListAsync(token);
         log($"共查询到{items.Count}条数据");
         var totalCount = items.Count;

+ 54 - 0
SnapshotWinFormsApp/Entities/NewHotline/CommunityInfo.cs

@@ -0,0 +1,54 @@
+using SnapshotWinFormsApp.Tools;
+using SqlSugar;
+using System;
+using System.Collections.Generic;
+using System.ComponentModel;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace SnapshotWinFormsApp.Entities.NewHotline;
+
+/// <summary>
+/// 社区信息
+/// 定期从网格员系统获取
+/// </summary>
+[SugarTable("community_info")]
+[Description("社区信息")]
+public class CommunityInfo : CreationSoftDeleteEntity
+{
+    /// <summary>
+    /// 社区名称
+    /// </summary>
+    [SugarColumn(ColumnDescription = "社区名称")]
+    public string Name { get; set; }
+
+    /// <summary>
+    /// 社区全称
+    /// </summary>
+    [SugarColumn(ColumnDescription = "社区全称")]
+    public string FullName { get; set; }
+
+    /// <summary>
+    /// 部门编号
+    /// </summary>
+    [SugarColumn(ColumnDescription = "部门编号")]
+    public string DepartmentNo { get; set; }
+
+    /// <summary>
+    /// 父社区Code
+    /// </summary>
+    [SugarColumn(ColumnDescription = "父社区Code")]
+    public string? ParentCode { get; set; }
+
+    /// <summary>
+    /// 社区唯一
+    /// </summary>
+    [SugarColumn(ColumnDescription = "社区唯一")]
+    public string UniqueKey => GetUniqueKey();
+
+    public string GetUniqueKey()
+    {
+        return $"{Id}{Name}{FullName}{ParentCode}".GetMD5();
+    }
+}

+ 3 - 1
SnapshotWinFormsApp/Entities/OldHotline/Flow03_PushContent.cs

@@ -1,4 +1,5 @@
-using System;
+using SqlSugar;
+using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
@@ -6,6 +7,7 @@ using System.Threading.Tasks;
 
 namespace SnapshotWinFormsApp.Entities.OldHotline;
 
+[SugarTable("ZG_CityHotline_Ver3.dbo.Flow03_PushContent")]
 public class Flow03_PushContent
 {
     public int FPC_ID { get; set; }

+ 20 - 0
SnapshotWinFormsApp/Entities/OldHotline/SSP_AreaEntity.cs

@@ -0,0 +1,20 @@
+using SqlSugar;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace SnapshotWinFormsApp.Entities.OldHotline;
+
+[SugarTable("ZG_CityHotline_Ver3.dbo.SSP_Area")]
+public class SSP_AreaEntity
+{
+    public int S_ID { get; set; }
+    public string Areaid { get; set; }
+    public int Areapid { get; set; }
+    public string? Areaname { get; set; }
+    public string? Areafullname { get; set; }
+    public string? Areadepartmentno { get; set; }
+    public string InsertTime { get; set; }
+}

+ 54 - 14
SnapshotWinFormsApp/MainForm.Designer.cs

@@ -28,11 +28,14 @@ partial class MainForm
     /// </summary>
     private void InitializeComponent()
     {
-        OkBtn = new Button();
         logTxt = new TextBox();
         CancelBtn = new Button();
         splitContainer1 = new SplitContainer();
         tableLayoutPanel1 = new TableLayoutPanel();
+        OkBtn = new Button();
+        guiderCBox = new CheckBox();
+        thirdAccountCBox = new CheckBox();
+        communityInfoCBox = new CheckBox();
         ((System.ComponentModel.ISupportInitialize)splitContainer1).BeginInit();
         splitContainer1.Panel1.SuspendLayout();
         splitContainer1.Panel2.SuspendLayout();
@@ -40,16 +43,6 @@ partial class MainForm
         tableLayoutPanel1.SuspendLayout();
         SuspendLayout();
         // 
-        // OkBtn
-        // 
-        OkBtn.Location = new Point(3, 3);
-        OkBtn.Name = "OkBtn";
-        OkBtn.Size = new Size(75, 23);
-        OkBtn.TabIndex = 0;
-        OkBtn.Text = "开始导入";
-        OkBtn.UseVisualStyleBackColor = true;
-        OkBtn.Click += OkBtn_Click;
-        // 
         // logTxt
         // 
         logTxt.BackColor = Color.Black;
@@ -99,17 +92,61 @@ partial class MainForm
         tableLayoutPanel1.ColumnCount = 2;
         tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50F));
         tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50F));
+        tableLayoutPanel1.Controls.Add(communityInfoCBox, 0, 2);
         tableLayoutPanel1.Controls.Add(OkBtn, 0, 0);
         tableLayoutPanel1.Controls.Add(CancelBtn, 1, 0);
+        tableLayoutPanel1.Controls.Add(guiderCBox, 0, 1);
+        tableLayoutPanel1.Controls.Add(thirdAccountCBox, 1, 1);
         tableLayoutPanel1.Dock = DockStyle.Fill;
         tableLayoutPanel1.Location = new Point(0, 0);
         tableLayoutPanel1.Name = "tableLayoutPanel1";
-        tableLayoutPanel1.RowCount = 2;
+        tableLayoutPanel1.RowCount = 3;
         tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Percent, 50F));
         tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Percent, 50F));
+        tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Absolute, 175F));
         tableLayoutPanel1.Size = new Size(200, 526);
         tableLayoutPanel1.TabIndex = 1;
         // 
+        // OkBtn
+        // 
+        OkBtn.Location = new Point(3, 3);
+        OkBtn.Name = "OkBtn";
+        OkBtn.Size = new Size(75, 23);
+        OkBtn.TabIndex = 0;
+        OkBtn.Text = "开始导入";
+        OkBtn.UseVisualStyleBackColor = true;
+        OkBtn.Click += OkBtn_Click;
+        // 
+        // guiderCBox
+        // 
+        guiderCBox.AutoSize = true;
+        guiderCBox.Location = new Point(3, 178);
+        guiderCBox.Name = "guiderCBox";
+        guiderCBox.Size = new Size(91, 19);
+        guiderCBox.TabIndex = 1;
+        guiderCBox.Text = "导入网格员";
+        guiderCBox.UseVisualStyleBackColor = true;
+        // 
+        // thirdAccountCBox
+        // 
+        thirdAccountCBox.AutoSize = true;
+        thirdAccountCBox.Location = new Point(103, 178);
+        thirdAccountCBox.Name = "thirdAccountCBox";
+        thirdAccountCBox.Size = new Size(94, 19);
+        thirdAccountCBox.TabIndex = 2;
+        thirdAccountCBox.Text = "导入微信账号";
+        thirdAccountCBox.UseVisualStyleBackColor = true;
+        // 
+        // communityInfoCBox
+        // 
+        communityInfoCBox.AutoSize = true;
+        communityInfoCBox.Location = new Point(3, 353);
+        communityInfoCBox.Name = "communityInfoCBox";
+        communityInfoCBox.Size = new Size(94, 19);
+        communityInfoCBox.TabIndex = 3;
+        communityInfoCBox.Text = "导入社区信息";
+        communityInfoCBox.UseVisualStyleBackColor = true;
+        // 
         // MainForm
         // 
         AcceptButton = OkBtn;
@@ -126,14 +163,17 @@ partial class MainForm
         ((System.ComponentModel.ISupportInitialize)splitContainer1).EndInit();
         splitContainer1.ResumeLayout(false);
         tableLayoutPanel1.ResumeLayout(false);
+        tableLayoutPanel1.PerformLayout();
         ResumeLayout(false);
     }
 
     #endregion
-
-    private Button OkBtn;
     private TextBox logTxt;
     private Button CancelBtn;
     private SplitContainer splitContainer1;
     private TableLayoutPanel tableLayoutPanel1;
+    private Button OkBtn;
+    private CheckBox guiderCBox;
+    private CheckBox thirdAccountCBox;
+    private CheckBox communityInfoCBox;
 }

+ 9 - 2
SnapshotWinFormsApp/MainForm.cs

@@ -8,12 +8,14 @@ public partial class MainForm : Form
 {
     private readonly DbSqlServer _sqlServerDB;
     private readonly SnapshotUserInfoApplication _snapshotUserInfoApplication;
+    private readonly CommunityInfoApplication _communityInfoApplication;
     private CancellationTokenSource? _cts;
 
     public MainForm(DbSqlServer sqlServerDB)
     {
         _sqlServerDB = sqlServerDB;
         _snapshotUserInfoApplication = new SnapshotUserInfoApplication(sqlServerDB);
+        _communityInfoApplication = new CommunityInfoApplication(sqlServerDB);
 
         InitializeComponent();
         logTxt.AppendText("³õʼ»¯Íê³É\r\n");
@@ -25,12 +27,17 @@ public partial class MainForm : Form
         _cts?.Cancel();
         _cts = new CancellationTokenSource();
         var token = _cts.Token;
-        Task.Run(() => _snapshotUserInfoApplication.ImportSnapshotUserInfoAsync(AddLog, token));
+        if (guiderCBox.Checked)
+            Task.Run(() => _snapshotUserInfoApplication.ImportGuiderInfoAsync(AddLog, token));
+        if (thirdAccountCBox.Checked)
+            Task.Run(() => _snapshotUserInfoApplication.ImportSnapshotUserInfoAsync(AddLog, token));
+        if (communityInfoCBox.Checked)
+            Task.Run(() => _communityInfoApplication.ImportCommunityInfoAsync(AddLog, token));
     }
 
     private void AddLog(string msg)
     {
-        this.Invoke((EventHandler)delegate 
+        this.Invoke((EventHandler)delegate
         {
             this.logTxt.AppendText(msg + "\r\n");
             Logs.Note(msg);

+ 4 - 2
SnapshotWinFormsApp/Repository/BaseRepository.cs

@@ -1,4 +1,5 @@
-using SnapshotWinFormsApp.Entities.NewHotline;
+using Abp.Collections.Extensions;
+using SnapshotWinFormsApp.Entities.NewHotline;
 using SnapshotWinFormsApp.Repository.Interfaces;
 using SqlSugar;
 using System;
@@ -30,7 +31,8 @@ public class BaseRepository<T> : IBaseRepository<T> where T : Entity, new()
 
     public async Task<string> InsertAsync(T entity, CancellationToken token)
     {
-        entity.InitId();
+        if (entity.Id.IsNullOrEmpty())
+            entity.InitId();
         await _db.Insertable(entity).ExecuteCommandAsync(token);
         return entity.Id;
     }

+ 1 - 1
SnapshotWinFormsApp/Repository/Interfaces/IRepository.cs

@@ -12,7 +12,7 @@ public interface IRepository<T> where T : class, new()
 {
     ISugarQueryable<T> Queryable();
     T GetById(int id);
-    List<T> GetAll();
+    Task<List<T>> GetAllAsync();
 }
 
 public interface IBaseRepository<T> where T : Entity, new()

+ 15 - 4
SnapshotWinFormsApp/Repository/Repository.cs

@@ -14,7 +14,7 @@ public class Repository<T> : IRepository<T> where T : class, new()
 
     public Repository(DbSqlServer context, string key)
     {
-        _db = context.DbItems.GetValueOrDefault(key+ "SQLServerDB");
+        _db = context.DbItems.GetValueOrDefault(key + "SQLServerDB");
     }
 
     public T GetById(int id)
@@ -22,14 +22,25 @@ public class Repository<T> : IRepository<T> where T : class, new()
         return _db.Queryable<T>().InSingle(id);
     }
 
-    public List<T> GetAll()
+    public async Task<List<T>> GetAllAsync()
     {
-        return _db.Queryable<T>().ToList();
+        try
+        {
+            var query = _db.Queryable<T>();
+            var sql = query.ToSqlString();
+            return await query.ToListAsync();
+
+        }
+        catch (Exception e)
+        {
+            var msg = e.Message;
+            throw;
+        }
     }
 
 
     public ISugarQueryable<T> Queryable()
-    { 
+    {
         return _db.Queryable<T>();
     }
 }

+ 8 - 0
SnapshotWinFormsApp/Tools/MapsterConfig.cs

@@ -20,5 +20,13 @@ public static class MapsterConfig
             .Map(m => m.CreationTime, n => n.WUR_RegDate)
             .Map(m => m.OpenId, n => n.WUR_Openid)
             .Map(m => m.UnIonId, n => n.WUR_unionid);
+
+        TypeAdapterConfig<SSP_AreaEntity, CommunityInfo>.NewConfig()
+            .Map(m => m.CreationTime, n => n.InsertTime)
+            .Map(m => m.Name, n => n.Areaname)
+            .Map(m => m.Id, n => n.Areaid)
+            .Map(m => m.ParentCode, n => n.Areapid)
+            .Map(m => m.DepartmentNo, n => n.Areadepartmentno)
+            .Map(m => m.FullName, n => n.Areafullname);
     }
 }

+ 17 - 8
SnapshotWinFormsApp/Tools/MyExtensions.cs

@@ -1,17 +1,26 @@
-using Castle.Core.Internal;
-using System;
-using System.Collections.Generic;
-using System.ComponentModel.DataAnnotations.Schema;
+using System.ComponentModel.DataAnnotations.Schema;
 using System.Data;
-using System.Linq;
 using System.Reflection;
+using System.Security.Cryptography;
 using System.Text;
-using System.Threading.Tasks;
 
-namespace DataTransmission.Tools;
+namespace SnapshotWinFormsApp.Tools;
 
 public static class MyExtensions
 {
+    /// <summary>
+    /// 获取字符串的 md5 
+    /// </summary>
+    /// <param name="value"> 字符串 </param>
+    /// <returns> MD5 后的字符串<see cref="string"/>.  </returns>
+    public static string GetMD5(this string value)
+    {
+        using (var md5 = MD5.Create())
+        {
+            return BitConverter.ToString(md5.ComputeHash(Encoding.UTF8.GetBytes(value))).Replace("-", string.Empty);
+        }
+    }
+
     public static string GetTableName<T>(this T value) where T : class
     {
         var tableAttribute = typeof(T).GetCustomAttribute<TableAttribute>();
@@ -82,7 +91,7 @@ public static class MyExtensions
             {
                 value = $"'{(DateTime)value:yyyy-MM-dd hh:mm:ss.ffffff}'";
             }
-            else if (property.PropertyType.BaseType == typeof(System.Enum))
+            else if (property.PropertyType.BaseType == typeof(Enum))
             {
                 value = $"{(int)value}";
             }