using System.Collections.Generic;
using AUTS_Server.Models;
using AUTS_Server.Service;
using Dm.filter;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using uts_manage;
using XAct.Users;

namespace AUTS_Server.Controllers
{
    [Route("IUser")]
    [ApiController]
    public class IUserController : ControllerBase
    {
        public readonly IEncryptionService encryptionService;
        private readonly ISqlSugarClient db;
        private readonly ILogHelperForService log;

        public IUserController(IEncryptionService encryptionService, ISqlSugarClient db, ILogHelperForService log)
        {
            this.encryptionService = encryptionService;
            this.db = db;
            this.log = log;
        }
        [HttpPost("AddOrUpdateUser")]
        public Returninfo AddOrUpdateUser([FromBody] uts_manage_userinfo user)
        {
            Returninfo rinfo = new Returninfo();
            try
            {
                if (user.ID == 0)
                {
                    //add
                    //添加用户默认密码是123456.后续用户自己修改自己的密码
                    tbl_uts_manage_user tbl =new tbl_uts_manage_user();
                    tbl.UserName = user.UserName;
                    tbl.Mobile = user.Mobile;
                    tbl.WeiXin = user.WeiXin;
                    tbl.Email = user.Email;
                    tbl.CompanyID = user.CompanyID;
                    tbl.IsValid = user.IsValid;
                    tbl.IsAdmin = user.IsAdmin;
                    tbl.CreateTime = DateTime.Now;
                    tbl.SetBarCode = user.SetBarCode;
                    tbl.AccountBill = user.AccountBill;
                    tbl.PlaintextPwd = "123456";
                    tbl.Password = encryptionService.Encrypt(encryptionService.Encrypt(tbl.PlaintextPwd));
                    int count = db.AsTenant().GetConnection(0).Insertable(tbl).ExecuteCommand();
                    if (count > 0)
                    {
                        rinfo.status = 200;
                        rinfo.message = "添加成功";
                        rinfo.isok = true;
                    }
                    else
                    {
                        rinfo.status = 300;
                        rinfo.message = "添加失败";
                        rinfo.isok = false;
                    }
                }
                else
                {
                    //修改不修改密码，
                    tbl_uts_manage_user list = db.AsTenant().GetConnection(0).Queryable<tbl_uts_manage_user>().First(x => x.ID == user.ID);
                    if (list != null)
                    {
                        list.UserName = user.UserName;
                        list.Mobile = user.Mobile;
                        list.WeiXin = user.WeiXin;
                        list.Email = user.Email;
                        list.CompanyID = user.CompanyID;
                        list.IsValid = user.IsValid;
                        list.IsAdmin = user.IsAdmin;
                        list.UpdateTime = DateTime.Now;
                        list.SetBarCode = user.SetBarCode;
                        list.AccountBill = user.AccountBill;
                        int count = db.AsTenant().GetConnection(0).Updateable(list).Where(x => x.ID == user.ID).ExecuteCommand();
                        if (count > 0)
                        {
                            rinfo.status = 200;
                            rinfo.message = "修改成功";
                            rinfo.isok = true;
                        }
                        else
                        {
                            rinfo.status = 300;
                            rinfo.message = "修改失败";
                            rinfo.isok = false;
                        }
                    }
                    else
                    {
                        rinfo.status = 404;
                    }
                }
            }
            catch (Exception ex)
            {

                rinfo.status = 500;
                rinfo.message = "系统异常";
                rinfo.isok = false;
                log.WriteLine("添加相关用户信息异常，异常信息是:" + ex.Message);

            }
            return rinfo;
        }

        /// <summary>
        ///查询所有用户
        /// </summary>
        [HttpGet("SelectUserAllinfo")]
        public void SelectUserAllinfo([FromBody] int page)
        {
            int totalCount = 0;
            try
            {
                var list = db.Queryable<tbl_uts_manage_user>()
     .LeftJoin<tbl_uts_manage_company>((u, c) => u.CompanyID == c.ID)
     .Select((u, c) => new
     {
         Id = u.ID,
         CustomerName = c.CustomerName,
         Username = u.UserName,
         CreateTime = u.CreateTime,
         WeiXin = u.WeiXin,
         Mobile = u.Mobile,
         Email = u.Email,
         IsValid = u.IsValid,
         //IsAdmin=u.IsAdmin,
         //BarCode=u.BarCode,
         //SetBarCode=u.SetBarCode,
     }).ToPageList(page, 12, ref totalCount);
            }
            catch (Exception ex)
            {

                //rinfo.status = 500;
                //rinfo.message = "系统异常";
                //rinfo.isok = false;
                log.WriteLine("查询用户异常，异常信息是:" + ex.Message);
            }

        }
        [HttpPost("DeleteUser")]
        public Returninfo DeleteUser([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)
                {
                    int count = db.AsTenant().GetConnection(0).Deleteable<tbl_uts_manage_user>().Where(x => x.ID == id).ExecuteCommand();
                    if (count > 0)
                    {
                        rinfo.status = 200;
                        rinfo.message = "删除成功";
                        rinfo.isok = true;
                    }
                    else
                    {
                        rinfo.status = 404;
                        rinfo.message = "删除失败";
                        rinfo.isok = false;
                    }
                }
                else
                {
                    rinfo.status = 404;
                    rinfo.message = "没有对应的数据";
                    rinfo.isok = false;
                }
            }
            catch (Exception ex)
            {
                rinfo.status = 500;
                rinfo.message = "系统异常";
                rinfo.isok = false;
                log.WriteLine("删除用户异常，异常信息是:" + ex.Message);
            }

            return rinfo;
        }
    }
    public class uts_manage_userinfo
    {
        public int ID { get; set; }
        public int  CompanyID { get; set; }
        public string UserName { get; set; }
        public string ?Password { get; set; }
        public DateTime? CreateTime { get; set; }
        public string? Mobile { get; set; }
        public string? WeiXin { get; set; }
        public string? Email { get; set; }
        public byte IsValid { get; set; }
        public byte IsAdmin { get; set; }
        public string? BarCode { get; set; }
        public byte SetBarCode { get; set; }
        public DateTime? UpdateTime { get; set; }
        public byte AccountBill { get; set; }
        public string? PlaintextPwd { get; set; }
    }
}


