using System.Collections.Generic;
using System.Threading.Tasks;
using GxPress.Repository.Interface;
using GxPress.Repository.Interface.Organization;
using GxPress.Service.Interface.OrganizationUser;
using System.Transactions;
namespace GxPress.Service.Implement.OrganizationUser
{
    public class OrganizationUserService : IOrganizationUserService
    {
        private readonly IOrganizationUserRepository organizationUserRepository;
        private readonly IUserRepository userRepository;
        public OrganizationUserService(IOrganizationUserRepository organizationUserRepository, IUserRepository userRepository)
        {
            this.organizationUserRepository = organizationUserRepository;
            this.userRepository = userRepository;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<bool> InsertAsync(Entity.tede2.Organization.OrganizationUser model)
        {
            var user = new Entity.User();
            user.LoginAccountNumber = model.LoginAccountNumber;
            user.Name = model.UserName;
            user.Phone = model.Phone;
            user.OrganizationId = model.OrganizationId;
            var userId = await userRepository.InsertAsync(user);
            model.UserId = userId;
            return await organizationUserRepository.InsertAsync(model);
        }
        public async Task<bool> DeleteAsync(List<int> ids)
        {
            try
            {
                using (var transactionScope = new TransactionScope())
                {
                    //查询用户
                    var list = await organizationUserRepository.GetAllAsync(ids);
                    //删除用户
                    await userRepository.DeleteAsync(ids);
                    await organizationUserRepository.DeleteAsync(ids);
                    transactionScope.Complete();
                }

            }
            catch (System.Exception ex)
            {
                throw new Common.Exceptions.BusinessException(ex.Message);
            }
            return true;
        }
    }
}