using Masuit.Tools.Models;
using PVDEMCS.Common.DI;
using PVDEMCS.Common;
using PVDEMCS.Services.Models;
using PVDEMCS.Services.Repositories.Entities;
using PVDEMCS.Services.Repositories;
using PVDEMCS.Common.Tools;
using PVDEMCS.Services.Repositories.Impl;
using AngleSharp.Dom;

namespace PVDEMCS.Services.Impl
{
    /// <summary>
    /// 用户信息 服务层 实现
    /// </summary>
    public class SysUserService : ISysUserService
    {
        private ISysUserRepository _sysUserRepository;

        public SysUserService(ISysUserRepository sysUserRepository)
        {
            this._sysUserRepository = sysUserRepository;
        }
        /// <summary>
        /// 获取用户信息分页列表
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="activated">是否其哟个</param>
        /// <param name="page">当前页</param>
        /// <param name="size">页大小</param>
        /// <returns></returns>
        public Result<PagedList<SysUser>> GetSysUserPageList(string userName, bool? activated, int page, int size)
        {
            var pageList = _sysUserRepository.GetSysUserPageList(userName, activated, page, size);
            return pageList;
        }

        /// <summary>
        /// 获取用户明细
        /// </summary>
        /// <param name="id">id</param>
        /// <param name="userName">名称</param>
        /// <returns></returns>
        public Result<SysUser> GetSysUserDetail(string id, string userName)
        {
            var result = new Result<SysUser>();

            var value = _sysUserRepository.GetSysUserDetail(id, userName);
            if (!value.IsSuccess)
            {
                result.Message = value.Message;
                return result;
            }
            result.Content = ModelTools.PubClone<SysUserEntity, SysUser>.Trans(value.Content);

            return result;

        }

        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public Result AddSysUser(SysUser info)
        {
            var entity = ModelTools.PubClone<SysUser, SysUserEntity>.Trans(info);
            var result = _sysUserRepository.AddSysUser(entity);
            return result;
        }

        /// <summary>
        /// 更新用户
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public Result UpdateSysUser(SysUser info)
        {
            var entity = ModelTools.PubClone<SysUser, SysUserEntity>.Trans(info);
            var result = _sysUserRepository.UpdateSysUser(entity);
            return result;
        }

        /// <summary>
        /// 更新用户密码
        /// </summary>
        /// <param name="id">用户id</param>
        /// <param name="pwd">密码</param>
        /// <returns></returns>
        public Result UpdatePassword(string id, string pwd)
        {
            var result = _sysUserRepository.UpdatePassword(id, pwd);
            return result;
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Result DeleteUser(string id)
        {
            var result = _sysUserRepository.DeleteUser(id);
            return result;
        }
    }
}
