예제 #1
0
        /// <summary>
        /// 根据用户账号和Token,保存所有提醒数据
        /// </summary>
        /// <param name="account">账号</param>
        /// <param name="token">token</param>
        /// <param name="notes">要保存的提醒数据</param>
        /// <returns>服务端版本号</returns>
        public int SaveNotesByAccount(string account, string token, List <Notes> notes)
        {
            var user = GetUser(account, token);

            // 注意,必须先删除Detail,否则会造成删除失败
            detailRepository.DeleteByUser(user.Id);
            notesRepository.DeleteByUser(user.Id);
            foreach (var note in notes.OrderBy(item => item.Id))
            {
                // 上传的Id是客户端标识,不能用
                note.ClientId = note.Id;
                note.Id       = 0;
                note.UserId   = user.Id;
                note.Title    = CutStr(note.Title, 200); // 汉字也按1计算
                note.Content  = CutStr(note.Content, 500);

                // note的id不会变化,还是0
                var savedRec = notesRepository.Save(note);

                foreach (var detail in note.Details)
                {
                    detail.Id     = 0;
                    detail.NoteId = savedRec.Id;
                    detailRepository.Save(detail);
                }
            }

            if (IncrVersion(user.Id, user.Version))
            {
                return(user.Version + 1);
            }

            throw new Exception("版本号加1出错:" + user.Id + ":" + account + " 当前版本:" + user.Version);
        }