Пример #1
0
        private void Persistent(List <Posts> posts, List <DepartInfo> departs, List <PersonInfo> staff)
        {
            var staffBll    = new PersonInfoBll();
            var dbUpdateBll = new DbUpdateLogBll();

            Func <bool>[] delegates = new Func <bool> [3];
            delegates[0] = () =>
            {
                if (posts.Count > 0)
                {
                    var postBll = new PostsBll();
                    var maxId   = postBll.GetMaxId();
                    postBll.BulkInsert(posts);

                    var ids    = postBll.QueryList("Id>" + maxId, new[] { nameof(Posts.Id) }).Select(p => p.Id);
                    var dbLogs = ids.Select(id => new DbUpdateLog {
                        TableName = nameof(Posts), TargetId = id, UpdateType = 1, UpdateTime = DateTime.Now
                    });
                    dbUpdateBll.BulkInsert(dbLogs);
                }
                return(true);
            };
            delegates[1] = () =>
            {
                if (departs.Count > 0)
                {
                    var departBll = new DepartInfoBll();
                    var maxId     = departBll.GetMaxId();
                    departBll.BulkInsert(departs);

                    var ids    = departBll.QueryList("Id>" + maxId, new[] { nameof(DepartInfo.Id) }).Select(p => p.Id);
                    var dbLogs = ids.Select(id => new DbUpdateLog {
                        TableName = nameof(DepartInfo), TargetId = id, UpdateType = 1, UpdateTime = DateTime.Now
                    });
                    dbUpdateBll.BulkInsert(dbLogs);
                }
                return(true);
            };
            delegates[2] = () =>
            {
                List <Posts>      _posts   = new PostsBll().QueryAll().ToList();
                List <DepartInfo> _departs = new DepartInfoBll().QueryAll().ToList();
                staff.ForEach(s =>
                {
                    // 根据前面用 PersonId 存储的部门名称找到此员工对应的部门
                    // 根据前面用 PhotoPath 存储的职务名称找到此员工对应的职务
                    var depart     = _departs.Find(d => d.DepartmentName == s.PersonId);
                    var post       = _posts.Find(p => p.PostName == s.PhotoPath);
                    s.DepartmentId = depart?.Id ?? 0;
                    s.PostId       = post?.Id ?? 0;
                    s.PersonId     = string.Empty;
                    s.PhotoPath    = string.Empty;
                });

                var maxId = staffBll.GetMaxId();
                staffBll.BulkInsert(staff);

                var ids    = staffBll.QueryList("Id>" + maxId, new[] { nameof(PersonInfo.Id) }).Select(p => p.Id);
                var dbLogs = ids.Select(id => new DbUpdateLog {
                    TableName = nameof(PersonInfo), TargetId = (int)id, UpdateType = 1, UpdateTime = DateTime.Now
                });
                dbUpdateBll.BulkInsert(dbLogs);

                return(true);
            };

            // 若插入失败,则尝试五次
            for (var i = 0; i < 5; i++)
            {
                var success = staffBll.ExecuteTransation(delegates);
                if (success)
                {
                    return;
                }
            }
        }