Exemplo n.º 1
0
        /// <summary>
        /// 保存用户
        /// </summary>
        /// <param name="model">用户</param>
        /// <returns>主键ID</returns>
        public async Task <int> SaveUserAsync(User model)
        {
            using (IOSysContext db = new IOSysContext())
            {
                //更新
                if (model.ID > 0)
                {
                    db.Entry(model).State = EntityState.Modified;
                }
                //新增
                else
                {
                    await db.Users.AddAsync(model);
                }

                await db.SaveChangesAsync();
            }

            //移除缓存
            var key = string.Format(SysConst.Cache.UserList_FamilyID, model.FamilyID);

            CacheHelper.Remove(key);

            return(model.ID);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 保存支出类型
        /// </summary>
        /// <param name="list">支出类型</param>
        /// <returns>改动条数</returns>
        public async Task <int> SaveOutTypesAsync(List <OutType> list)
        {
            int retCount = 0;

            using (IOSysContext db = new IOSysContext())
            {
                foreach (var model in list)
                {
                    //更新
                    if (model.ID > 0)
                    {
                        db.Entry(model).State = EntityState.Modified;
                    }
                    //新增
                    else
                    {
                        await db.OutTypes.AddAsync(model);
                    }
                }

                retCount = await db.SaveChangesAsync();
            }

            //家庭ID
            var lstFamilyID = list.Select(m => m.FamilyID).Distinct().ToList();

            foreach (var familyID in lstFamilyID)
            {
                //移除缓存
                var key = string.Format(SysConst.Cache.OutTypeList_FamilyID, familyID);
                CacheHelper.Remove(key);
            }

            return(retCount);
        }
Exemplo n.º 3
0
        /// <summary>
        /// 修改使用的账户
        /// </summary>
        /// <param name="familyID">家庭ID</param>
        /// <param name="newAmountAccountID">新账户ID</param>
        /// <param name="newAmount">新金额</param>
        /// <param name="oldAmountAccountID">原账户ID</param>
        /// <param name="oldAmount">原金额</param>
        /// <param name="db">数据库上下文</param>
        public async Task ChangeAmountAccount(int familyID, int newAmountAccountID, decimal newAmount, int?oldAmountAccountID, decimal?oldAmount, IOSysContext db)
        {
            //新增使用账户
            if (oldAmountAccountID.HasValue == false)
            {
                await this.UseAmountAccount(familyID, newAmountAccountID, newAmount, db);
            }
            //只修改了金额
            else if (newAmountAccountID == oldAmountAccountID.Value)
            {
                var model = await this.GetAmountAccountAsync(familyID, newAmountAccountID);

                model.Amount         += (newAmount - oldAmount.Value);
                db.Entry(model).State = EntityState.Modified;
            }
            //修改了账户
            else
            {
                var taskUse   = this.UseAmountAccount(familyID, newAmountAccountID, newAmount, db);
                var taskUnuse = this.UnUseAmountAccount(familyID, oldAmountAccountID.Value, oldAmount.Value, db);

                await taskUse;
                await taskUnuse;
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// 保存更新账户排序权重字段
        /// </summary>
        /// <param name="list">账户</param>
        /// <returns>修改条数</returns>
        public async Task <int> UpdateAmountAccountSortWeightAsync(List <AmountAccount> list)
        {
            int retCount = 0;

            using (IOSysContext db = new IOSysContext())
            {
                foreach (var model in list)
                {
                    //更新
                    if (model.ID > 0)
                    {
                        db.Entry(model).Property(m => m.SortWeight).IsModified = true;
                    }
                }

                retCount = await db.SaveChangesAsync();
            }

            //家庭ID
            var lstFamilyID = list.Select(m => m.FamilyID).Distinct().ToList();

            foreach (var familyID in lstFamilyID)
            {
                //移除缓存
                var key = string.Format(SysConst.Cache.AmountAccountList_FamilyID, familyID);
                CacheHelper.Remove(key);
            }

            return(retCount);
        }
Exemplo n.º 5
0
        /// <summary>
        /// 反使用账户
        /// </summary>
        /// <param name="familyID">家庭ID</param>
        /// <param name="amountAccountID">账户ID</param>
        /// <param name="amount">金额</param>
        /// <param name="db">数据库上下文</param>
        public async Task UnUseAmountAccount(int familyID, int amountAccountID, decimal amount, IOSysContext db)
        {
            //获取账户
            var model = await this.GetAmountAccountAsync(familyID, amountAccountID);

            model.Amount -= amount;
            //model.SortWeight -= 1;
            db.Entry(model).State = EntityState.Modified;
        }
Exemplo n.º 6
0
        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="familyID">家庭ID</param>
        /// <param name="userID">用户ID</param>
        public async Task DeleteUserAsync(int familyID, int userID)
        {
            //获取原数据
            var model = await this.GetUserAsync(familyID, userID);

            if (model == null)
            {
                return;
            }

            //设删除标识
            model.IsDelete = true;

            using (IOSysContext db = new IOSysContext())
            {
                db.Entry(model).State = EntityState.Modified;
                await db.SaveChangesAsync();
            }

            //移除缓存
            var key = string.Format(SysConst.Cache.UserList_FamilyID, familyID);

            CacheHelper.Remove(key);
        }
Exemplo n.º 7
0
        /// <summary>
        /// 保存收入信息
        /// </summary>
        /// <param name="model">收入信息</param>
        /// <returns>主键ID</returns>
        public async Task <int> SaveInComeAsync(InCome model)
        {
            using (IOSysContext db = new IOSysContext())
            {
                using (var trans = db.Database.BeginTransaction())
                {
                    InCome oldModel = null;

                    //尝试查找原数据
                    if (model.ID > 0)
                    {
                        oldModel = db.InComes.AsNoTracking().Where(m => m.ID == model.ID && m.FamilyID == model.FamilyID).FirstOrDefault();

                        if (oldModel == null)
                        {
                            model.CreatorID  = model.UpdateID.Value;
                            model.CreateTime = model.UpdateTime.Value;
                        }
                    }

                    //新增
                    if (oldModel == null)
                    {
                        //新增
                        await db.InComes.AddAsync(model);

                        //修改账户信息
                        await BasicDAL.Inst.UseAmountAccount(model.FamilyID, model.AmountAccountID, model.Amount, db);

                        ////修改收入类型信息
                        //await BasicDAL.Inst.UseInType(model.FamilyID, model.InTypeID, db);
                    }
                    //更新
                    else
                    {
                        //设置更新者信息
                        model.UpdateID   = model.UpdateID;
                        model.UpdateTime = model.UpdateTime;
                        model.CreatorID  = oldModel.CreatorID;
                        model.CreateTime = oldModel.CreateTime;

                        //更新
                        db.Entry(model).State = EntityState.Modified;

                        //修改账户信息
                        await BasicDAL.Inst.ChangeAmountAccount(model.FamilyID, model.AmountAccountID, model.Amount, oldModel.AmountAccountID, oldModel.Amount, db);

                        ////修改收入类型信息
                        //await BasicDAL.Inst.ChangeInType(model.FamilyID, model.InTypeID, oldModel.InTypeID, db);
                    }

                    //保存修改
                    await db.SaveChangesAsync();

                    //提交事务
                    trans.Commit();
                }
            }

            //更新最小交易流水日期缓存
            this.UpdateMinTurnoverDate(model.FamilyID, model.InDate);

            return(model.ID);
        }
Exemplo n.º 8
0
        /// <summary>
        /// 保存借还信息
        /// </summary>
        /// <param name="model">借还信息</param>
        /// <returns>主键ID</returns>
        public async Task <int> SaveBorrowRepayAsync(BorrowRepay model)
        {
            using (IOSysContext db = new IOSysContext())
            {
                using (var trans = db.Database.BeginTransaction())
                {
                    BorrowRepay oldModel = null;

                    //尝试查找原数据
                    if (model.ID > 0)
                    {
                        oldModel = db.BorrowRepays.AsNoTracking().Where(m => m.ID == model.ID && m.FamilyID == model.FamilyID).FirstOrDefault();

                        if (oldModel == null)
                        {
                            model.CreatorID  = model.UpdateID.Value;
                            model.CreateTime = model.UpdateTime.Value;
                        }
                    }

                    //新增
                    if (oldModel == null)
                    {
                        //新增
                        await db.BorrowRepays.AddAsync(model);

                        //账户金额变动值
                        var amount = this.GetAmountDirectionForBorrowRepay(model.BRType, model.Amount);

                        //修改账户信息
                        await BasicDAL.Inst.UseAmountAccount(model.FamilyID, model.AmountAccountID, amount, db);
                    }
                    //更新
                    else
                    {
                        //设置更新者信息
                        model.UpdateID   = model.UpdateID;
                        model.UpdateTime = model.UpdateTime;
                        model.CreatorID  = oldModel.CreatorID;
                        model.CreateTime = oldModel.CreateTime;

                        //更新
                        db.Entry(model).State = EntityState.Modified;

                        //账户金额变动值
                        var newAmount = this.GetAmountDirectionForBorrowRepay(model.BRType, model.Amount);
                        var oldAmount = this.GetAmountDirectionForBorrowRepay(oldModel.BRType, oldModel.Amount);

                        //修改账户信息
                        await BasicDAL.Inst.ChangeAmountAccount(model.FamilyID, model.AmountAccountID, newAmount, oldModel.AmountAccountID, oldAmount, db);
                    }

                    //保存修改
                    await db.SaveChangesAsync();

                    //提交事务
                    trans.Commit();
                }
            }

            //更新最小交易流水日期缓存
            this.UpdateMinTurnoverDate(model.FamilyID, model.BRDate);

            return(model.ID);
        }