using AUTS_Server.Models;
using AUTS_Server.Service;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using uts_manage;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace AUTS_Server.Controllers
{
    [Route("ILogin")]
    [ApiController]
    public class ILonginController : ControllerBase
    {
      
        private readonly ISqlSugarClient db;
        private readonly IEncryptionService encryptionService;
        public readonly IUserOperationLog userOperationLog;
        private readonly ILogHelperForService log;
        public ILonginController(ISqlSugarClient db, IEncryptionService encryptionService, IUserOperationLog userOperationLog, ILogHelperForService log)
        {
            this.db = db;
            this.encryptionService = encryptionService;
            this.userOperationLog = userOperationLog;
            this.log = log;
        }
        /// <summary>
        /// 登录接口
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("Login")]
        public Returninfo Longin([FromBody] uts_manage_user user)
        {
            Returninfo rinfo = new Returninfo();
            try
            {
                //密码加密
                user.Password = encryptionService.Encrypt(encryptionService.Encrypt(user.Password));
                tbl_uts_manage_user list = db.AsTenant().GetConnection(0).Queryable<tbl_uts_manage_user>().Where(it => it.UserName == user.UserName && it.Password == user.Password).First();
                if (list != null)
                {
                    if (list.IsValid == 1)
                    {

                        //获取用户权限
                        //List<tbl_uts_manage_userauth_operation> listauth = db.AsTenant().GetConnection(0).Queryable<tbl_uts_manage_userauth_operation>().Where(it => it.UserID == list.ID).ToList();
                        #region  记录登录信息
                        string userAgent = Request.Headers["User-Agent"].ToString();
                        string Device;
                        //var userAgent = Request.UserAgent;
                        bool
                            _windows = userAgent.Contains("Windows NT"),
                            _mac = userAgent.Contains("Macintosh"),
                            _iphone = userAgent.Contains("iPhone"),
                            _android = userAgent.Contains("Android")
                            ;
                        if (_windows)
                        {
                            Device = "windows";
                        }
                        else if (_mac)
                        {
                            Device = "Mac";
                        }
                        else if (_iphone)
                        {
                            Device = "ios";
                        }
                        else if (_android)
                        {
                            Device = "Android";
                        }
                        else
                        {
                            Device = "未知";
                        }
                        userOperationLog.UserLog("登录", Device, user.UserName);
                        #endregion
                        //return true;
                        rinfo.status = 200;
                        rinfo.message = "登录成功";
                        rinfo.isok = true;
                        //rinfo.data = list;
                    }
                    else
                    {
                        rinfo.status = 600;
                        rinfo.message = "用户被禁用";
                        rinfo.isok = false;
                    }

                }
                else
                {
                    rinfo.status = 600;
                    rinfo.message = "用户名或密码错误";
                    rinfo.isok = false;
                }
            }
            catch (Exception ex)
            {

                rinfo.status = 500;
                rinfo.message = "系统异常";
                rinfo.isok = false;
                log.WriteLine("登录接口异常：" + ex.Message);
            }
           
            return rinfo;
        }
       /// <summary>
       /// 修改密码接口
       /// </summary>
       /// <param name="upwd"></param>
        [HttpPost]
        [Route("UpdataPassword")]
        public Returninfo UpdataPassword([FromBody] uppwd upwd)
        {

            Returninfo rinfo = new Returninfo();
            try
            {
                upwd.oldpassword = encryptionService.Encrypt(encryptionService.Encrypt(upwd.oldpassword));
                tbl_uts_manage_user list = db.AsTenant().GetConnection(0).Queryable<tbl_uts_manage_user>().First(x => x.UserName == upwd.username && x.Password == upwd.oldpassword);
                if (list != null)
                {
                    list.Password = encryptionService.Encrypt(encryptionService.Encrypt(upwd.newpassword));
                    list.PlaintextPwd = upwd.newpassword;
                    int count = db.AsTenant().GetConnection(0).Updateable(list).Where(x => x.ID == list.ID).ExecuteCommand();
                    if (count > 0)
                    {
                        rinfo.message = "修改密码成功";
                        rinfo.isok = true;
                        rinfo.status = 200;
                    }
                    else
                    {
                        rinfo.message = "修改密码息失败";
                        rinfo.isok = false;
                        rinfo.status = 100;
                    }
                }
                else
                {
                    rinfo.message = "没有查找到这个用户的信息";
                    rinfo.isok = false;
                    rinfo.status = 404;
                }
            }
            catch (Exception ex)
            {

                rinfo.message = ex.Message;
                rinfo.isok = false;
                rinfo.status = 500;
                log.WriteLine("修改密码接口异常:"+ex.Message);
            }
            return rinfo;
        }
        /// <summary>
        /// 重置密码接口
        /// </summary>
        /// <param name="id"></param>
        [HttpPost]
        [Route("ResetPassword")]
        public Returninfo ResetPassword([FromBody] int id)
        {
            Returninfo rinfo = new Returninfo();
            try
            {

                tbl_uts_manage_user list = db.AsTenant().GetConnection(0).Queryable<tbl_uts_manage_user>().First(x => x.ID == id);
                if (list != null)
                {
                    list.Password = encryptionService.Encrypt(encryptionService.Encrypt("123456")); 
                    list.PlaintextPwd="123456";
                    int count = db.AsTenant().GetConnection(0).Updateable(list).Where(x => x.ID == list.ID).ExecuteCommand();
                    if (count > 0)
                    {
                        rinfo.message = "重置密码成功";
                        rinfo.isok = true;
                        rinfo.status = 200;
                    }
                    else
                    {
                        rinfo.message = "重置密码息失败";
                        rinfo.isok = false;
                        rinfo.status = 100;
                    }
                }
                else
                {
                    rinfo.message = "没有查找到这个用户的信息";
                    rinfo.isok = false;
                    rinfo.status = 404;
                }
            }
            catch (Exception ex)
            {

                rinfo.message = ex.Message;
                rinfo.isok = false;
                rinfo.status = 500;
                log.WriteLine("重置密码接口异常:" + ex.Message);
            }
            return rinfo;
        }
    }
    public class uts_manage_user
    {
        public string UserName { get; set; }
        public string Password { get; set; }

    }
    public class uppwd
    {
        public string newpassword { get; set; }
        public string oldpassword { get; set; }
        //public int id { get; set; }

        public string username { get; set; }
    }
}
