using System;
using System.Collections.Generic;
using Edu.Model.Public;
using Edu.Repository.Public;
using VT.FW.DB;

namespace Edu.Module.Public
{
    public class PublicModule
    {
        private readonly RB_File_StoreRepository storeRepository = new RB_File_StoreRepository();


        #region 上传设置
        /// <summary>
        /// 获取上传存储信息
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <returns></returns>
        public List<RB_File_Store> GetFileStoreList(RB_File_Store query)
        {
            return storeRepository.GetListRepository(query);
        }


        /// <summary>
        /// 新增/修改存储位置
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool AddOrUpdateFileStore(RB_File_Store model)
        {
            bool result = false;

            int id = 0;
            if (model.ID == 0)
            {
                id = storeRepository.Insert(model);
                result = id > 0;
            }
            else
            {
                id = model.ID;
                result = storeRepository.Update(model);
            }
            //判断当前的是否设置成默认没有
            if (model.IsDefault == 1 && result)//将其他的默认赋值成0
            {
                Dictionary<string, object> fileds = new Dictionary<string, object>() { { nameof(RB_File_Store.IsDefault), 0 }, };
                List<WhereHelper> whereHelpers = new List<WhereHelper>()
                    {
                        new WhereHelper(){ FiledName=nameof(RB_File_Store.Group_Id),FiledValue=model.Group_Id ,OperatorEnum=OperatorEnum.Equal},
                         new WhereHelper(){ FiledName=nameof(RB_File_Store.School_Id),FiledValue=model.School_Id, OperatorEnum=OperatorEnum.Equal },
                         new WhereHelper(){ FiledName=nameof(RB_File_Store.ID),FiledValue=id, OperatorEnum=OperatorEnum.NotEqual },
                    };
                storeRepository.Update(fileds, whereHelpers);
            }
            return result;
        }
        #endregion
    }
}
