/*
 * 运营中心
 */

using System;
using System.Web;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using MySystem.OpModels;
using Library;
using LitJson;
using MySystemLib;

namespace MySystem.Areas.Admin.Controllers
{
    [Area("Admin")]
    [Route("Admin/[controller]/[action]")]
    public class SysAdminOperateController : BaseController
    {
        public SysAdminOperateController(IHttpContextAccessor accessor, ILogger<BaseController> logger, IOptions<Setting> setting) : base(accessor, logger, setting)
        {
        }

        #region 运营中心列表

        /// <summary>
        /// 根据条件查询运营中心列表
        /// </summary>
        /// <returns></returns>
        public IActionResult Index(SysAdmin data, string right)
        {
            ViewBag.RightInfo = RightInfo;
            ViewBag.right = right;


            string Condition = "";
            Condition += "AdminName:\"" + data.AdminName + "\",";
            Condition += "RealName:\"" + data.RealName + "\",";
            Condition += "Role:\"" + data.Role + "\",";

            if (!string.IsNullOrEmpty(Condition))
            {
                Condition = Condition.TrimEnd(',');
                Condition = ", where: {" + Condition + "}";
            }
            ViewBag.Condition = Condition;
            return View();
        }

        #endregion

        #region 根据条件查询运营中心列表

        /// <summary>
        /// 运营中心列表
        /// </summary>
        /// <returns></returns>
        public JsonResult IndexData(SysAdmin data, string OpCode, string RealName, string CreateDateData, int page = 1, int limit = 30)
        {

            Dictionary<string, string> Fields = new Dictionary<string, string>();

            Fields.Add("RealName", "2"); //名称

            string condition = " and Status>-1 and Sort>0 and QueryCount>0 and AdminName!='admin'";
            //运营中心编号
            if (!string.IsNullOrEmpty(OpCode))
            {
                condition += " and Id in (select OpId from OperateForCode where OpCode='" + OpCode + "')";
            }
            //创建时间
            if (!string.IsNullOrEmpty(CreateDateData))
            {
                string[] datelist = CreateDateData.Split(new string[] { " - " }, StringSplitOptions.None);
                string start = datelist[0];
                string end = datelist[1];
                condition += " and CreateDate>='" + start + " 00:00:00' and CreateDate<='" + end + " 23:59:59'";
            }

            Dictionary<string, object> obj = new AdminContentByConn(_accessor.HttpContext, PublicFunction.OpTables, OpConn).IndexData("SysAdmin", Fields, "Id desc", "0", page, limit, condition);
            List<Dictionary<string, object>> diclist = obj["data"] as List<Dictionary<string, object>>;
            foreach (Dictionary<string, object> dic in diclist)
            {
                //所属人信息
                int Id = int.Parse(dic["Id"].ToString());
                SysAdmin sysAdmin = opdb.SysAdmin.FirstOrDefault(m => m.Id == Id) ?? new SysAdmin();
                var userInfo = db.Users.FirstOrDefault(m => m.Id == sysAdmin.UserId) ?? new Models.Users();
                if (userInfo.UserType == 0) dic["Kind"] = "";
                if (userInfo.UserType == 1) dic["Kind"] = "运营中心";
                if (userInfo.UserType == 2) dic["Kind"] = "黄金合伙人";
                if (userInfo.UserType == 3) dic["Kind"] = "钻石合伙人";
                if (userInfo.UserType == 4) dic["Kind"] = "战略合伙人";
                dic["MakerCode"] = userInfo.MakerCode;
                dic["RealName"] = userInfo.RealName;

                //登陆地址
                dic["LoginUrl"] = OpHost + "Admin/" + dbconn.Encrypt3DES(sysAdmin.Sort.ToString(), "kxs12345").Replace("/", "%2F") + "/Login";

                //未使用用额度
                UserAccount userAccount = opdb.UserAccount.FirstOrDefault(m => m.Sort == Id) ?? new UserAccount();
                dic["TotalAmt"] = userAccount.TotalAmt;

                //可提现额度
                dic["ValidForGetAmount"] = userAccount.ValidForGetAmount;

                //关联分仓额度
                dic["ValidAmount"] = userAccount.ValidAmount;

                //已提现金额
                dic["WithdrawAmount"] = userAccount.WithdrawAmount;

                //提现中金额
                dic["WithdrawingAmount"] = userAccount.WithdrawingAmount;

            }
            return Json(obj);
        }

        #endregion

        #region 增加运营中心

        /// <summary>
        /// 增加或修改运营中心信息
        /// </summary>
        /// <returns></returns>
        public IActionResult Add(string right)
        {
            ViewBag.RightInfo = RightInfo;
            ViewBag.right = right;


            return View();
        }

        #endregion

        #region 增加运营中心

        /// <summary>
        /// 增加或修改运营中心信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public string Add(SysAdmin data, UserAccount userAccount, string Areas, string Address, string Kind)
        {
            Dictionary<string, object> Fields = new Dictionary<string, object>();
            Dictionary<string, object> Field = new Dictionary<string, object>();

            var userForMakerCode = db.UserForMakerCode.FirstOrDefault(m => m.MakerCode == data.MakerCode) ?? new Models.UserForMakerCode();
            if (userForMakerCode.UserId > 0)
            {
                var userInfo = db.Users.FirstOrDefault(m => m.Id == userForMakerCode.UserId && m.Mobile == data.MakerMobile) ?? new Models.Users();
                if (userInfo.Id > 0)
                {
                    var check = opdb.SysAdmin.Any(m => m.AdminName == data.AdminName);
                    var checks = opdb.SysAdmin.Any(m => m.UserId == userInfo.Id);
                    if (check)
                    {
                        return "该用户名已存在，请重新输入";
                    }
                    if (checks)
                    {
                        return "该用户已存在运营中心，请重新输入";
                    }
                    Fields.Add("CreateMan", SysUserName + "_" + SysRealName); //创建人
                    Fields.Add("RealName", data.RealName); //运营中心名称
                    Fields.Add("MakerCode", data.MakerCode); //创客编号
                    Fields.Add("MakerMobile", data.MakerMobile); //手机号
                    Fields.Add("UserId", userInfo.Id); //创客Id
                    Fields.Add("MakerName", userInfo.RealName); //创客真实姓名
                    Fields.Add("AdminName", data.AdminName); //用户名
                    Fields.Add("Password", function.MD5_32(data.Password)); //密码
                    Fields.Add("FilePath", data.FilePath); //附件
                    Fields.Add("QueryCount", 1); //是否为主账号(1是，0否)

                    int Id = new AdminContentByConn(_accessor.HttpContext, PublicFunction.OpTables, OpConn).Add("SysAdmin", Fields, 0);
                    SysAdmin edit = opdb.SysAdmin.FirstOrDefault(m => m.Id == Id);
                    if (edit != null)
                    {
                        string No = Id.ToString();
                        for (int i = 0; i < 7 - Id.ToString().Length; i++)
                        {
                            No = "0" + No;
                        }
                        No = "YZ" + No;
                        edit.OpCode = No;
                        edit.Sort = Id;
                        AddSysLog(data.Id.ToString(), "SysAdmin", "add");
                        var query = opdb.OperateForCode.Add(new OperateForCode()
                        {
                            OpCode = No,
                            OpId = Id,

                        }).Entity;
                        opdb.SaveChanges();
                    }

                    var info = opdb.SysAdmin.FirstOrDefault(m => m.Id == Id);
                    if (info != null)
                    {
                        Field.Add("TotalAmt", userAccount.TotalAmt); //初始额度
                        Field.Add("Sort", Id); //运营中心Id
                        Field.Add("Id", edit.UserId); //创客Id
                        Field.Add("UserId", edit.UserId); //创客Id
                        int Ids = new AdminContentByConn(_accessor.HttpContext, PublicFunction.OpTables, OpConn).Add("UserAccount", Field, 0);
                        AddSysLog(userAccount.Id.ToString(), "UserAccount", "add");
                        var query = opdb.SysAdminRole.Add(new SysAdminRole()
                        {
                            CreateDate = DateTime.Now, //创建时间
                            Sort = edit.Id,//所属运营中心
                            CreateMan = SysUserName + "_" + SysRealName,
                            Details = "运营中心管理员，可以操作运营中心所有权限",
                            RightInfo = ",1_1,1_1_1,1_1_1_edit,1_2,1_2_1,1_2_1_export,1_2_2,1_3,1_3_1,1_3_1_import,1_4,1_4_1,1_4_1_add,1_4_2,1_4_2_add,1_4_3,1_4_3_edit,1_4_3_base,1_4_3_audit,1_6,1_6_1,1_6_1_add,1_6_1_delete,1_6_1_edit,1_6_2,1_6_2_add,1_6_2_delete,1_6_2_edit,",
                            Name = "运营中心管理员",

                        }).Entity;
                        opdb.SaveChanges();
                        var role = opdb.SysAdminRole.FirstOrDefault(m => m.Sort == edit.Id);
                        if (role != null)
                        {
                            edit.Role = role.Id.ToString();
                            opdb.SaveChanges();
                        }
                    }
                    userInfo.UserType = int.Parse(Kind);//设置创客为运营中心
                    //添加合伙人预设职级记录
                    var operatorRankWhite = db.OperatorRankWhite.FirstOrDefault(m => m.Id == userInfo.Id) ?? new Models.OperatorRankWhite();
                    if (operatorRankWhite.Id == 0)
                    {
                        operatorRankWhite = db.OperatorRankWhite.Add(new Models.OperatorRankWhite()
                        {
                            Id = userInfo.Id,
                            CreateDate = DateTime.Now,
                            UpdateDate = DateTime.Now.AddMonths(12),
                            Rank = 9,
                            UserId = userInfo.Id,
                        }).Entity;
                    }
                    db.SaveChanges();

                    var kqProduct = db.KqProducts.ToList();
                    foreach (var item in kqProduct)
                    {
                        string Nos = "YC000" + item.Id + info.Id;
                        var query = db.StoreHouse.Add(new Models.StoreHouse()
                        {
                            CreateDate = DateTime.Now, //创建时间
                            CreateMan = SysUserName + "_" + SysRealName,
                            ManageMobile = data.MakerMobile, //手机号
                            UserId = info.UserId, //运营中心创客Id
                            OpId = info.UserId, //运营中心创客Id
                            Address = Address,//仓库地址
                            Areas = Areas,//所属地区
                            ManageUserId = info.UserId,//仓库管理员
                            StoreName = item.Name + "运营分仓", //仓库名称
                            StoreNo = Nos, //仓库编号
                            Sort = info.Id,//运营中心Id
                            Status = 1,
                            BrandId = item.Id.ToString(),
                            StoreKind = 2,//运营仓

                        }).Entity;
                        db.SaveChanges();

                        var querys = opdb.StoreForOperate.Add(new OpModels.StoreForOperate()
                        {
                            CreateDate = DateTime.Now, //创建时间
                            CreateMan = SysUserName + "_" + SysRealName,
                            Sort = info.UserId,
                            OpId = info.UserId,
                            StoreId = query.Id,
                            SeoKeyword = query.StoreNo,
                            SeoDescription = query.StoreName,
                        }).Entity;
                        opdb.SaveChanges();
                    }

                    RedisDbconn.Instance.AddList("OperateAddServiceQueue", Id.ToString());
                }
                else
                {
                    return "创客编号和手机号不匹配";
                }
            }
            else
            {
                return data.MakerCode + "关联信息缺失";
            }

            return "success";
        }

        #endregion

        #region 修改运营中心

        /// <summary>
        /// 增加或修改运营中心信息
        /// </summary>
        /// <returns></returns>
        public IActionResult Edit(string right, int Id = 0)
        {
            ViewBag.RightInfo = RightInfo;
            ViewBag.right = right;


            SysAdmin editData = opdb.SysAdmin.FirstOrDefault(m => m.Id == Id) ?? new SysAdmin();
            UserAccount editDatas = opdb.UserAccount.FirstOrDefault(m => m.Sort == Id) ?? new UserAccount();
            ViewBag.data = editData;
            ViewBag.datas = editDatas;
            var userInfo = db.Users.FirstOrDefault(m => m.Id == editData.UserId) ?? new Models.Users();
            ViewBag.Kind = userInfo.UserType;
            return View();
        }

        #endregion

        #region 修改运营中心

        /// <summary>
        /// 增加或修改运营中心信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public string Edit(SysAdmin data, UserAccount userAccount, string Areas, string Address, string Kind)
        {
            Dictionary<string, object> Fields = new Dictionary<string, object>();
            Fields.Add("UpdateMan", SysUserName + "_" + SysRealName); //修改人
            Fields.Add("RealName", data.RealName); //运营中心名称
            Fields.Add("FilePath", data.FilePath); //附件
            Fields.Add("AdminName", data.AdminName); //用户名
            if (!string.IsNullOrEmpty(data.Password)) //密码不为空就修改密码，否则不操作
            {
                var sys = opdb.SysAdmin.FirstOrDefault(m => m.Id == data.Id);
                if (data.Password != sys.Password)
                {
                    Fields.Add("Password", function.MD5_32(data.Password)); //密码
                }
            }
            new AdminContentByConn(_accessor.HttpContext, PublicFunction.OpTables, OpConn).Edit("SysAdmin", Fields, data.Id);
            SysAdmin edit = opdb.SysAdmin.FirstOrDefault(m => m.Id == data.Id);
            edit.CreateDate = edit.CreateDate;
            var userInfo = db.Users.FirstOrDefault(m => m.Id == edit.UserId) ?? new Models.Users();
            userInfo.UserType = int.Parse(Kind);//设置创客为运营中心
            userInfo.UpdateDate = DateTime.Now;
            AddSysLog(data.Id.ToString(), "SysAdmin", "update");
            db.SaveChanges();

            return "success";
        }

        #endregion

        #region 删除运营中心信息

        /// <summary>
        /// 删除运营中心信息
        /// </summary>
        /// <returns></returns>
        public string Delete(string Id)
        {
            string[] idlist = Id.Split(new char[] { ',' });
            AddSysLog(Id, "SysAdmin", "del");
            foreach (string subid in idlist)
            {
                int id = int.Parse(subid);
                var sysAdmin = opdb.SysAdmin.FirstOrDefault(m => m.Id == id) ?? new OpModels.SysAdmin();
                if (sysAdmin.Id > 0)
                {
                    var userAccount = opdb.UserAccount.FirstOrDefault(m => m.Status > -1 && m.Sort == id && m.UserId == sysAdmin.UserId) ?? new OpModels.UserAccount();
                    if (userAccount.Id > 0)
                    {
                        userAccount.Status = -1;
                    }
                    var operateForCode = opdb.OperateForCode.FirstOrDefault(m => m.OpId == id) ?? new OpModels.OperateForCode();
                    if (operateForCode.OpId > 0)
                    {
                        new AdminContentByConn(_accessor.HttpContext, PublicFunction.OpTables, OpConn).Delete("UserAccount", operateForCode.OpId);
                    }
                    //修改运营中心仓
                    var storeHouse = db.StoreHouse.Where(m => m.Sort == id && m.Status == 1 && m.UserId == sysAdmin.UserId && m.OpId == sysAdmin.UserId).ToList();
                    foreach (var item in storeHouse)
                    {
                        var list = storeHouse.FirstOrDefault(m => m.Id == item.Id) ?? new Models.StoreHouse();
                        if (list.Id > 0)
                        {
                            list.Status = -1;
                            list.UpdateDate = DateTime.Now;
                            list.UpdateMan = SysUserName + "-" + SysRealName;
                            db.SaveChanges();
                        }
                    }
                    //修改运营中心关联分仓
                    var storeHouses = db.StoreHouse.Where(m => m.Sort == id && m.Status == 1).ToList();
                    foreach (var item in storeHouses)
                    {
                        var list = storeHouses.FirstOrDefault(m => m.Id == item.Id) ?? new Models.StoreHouse();
                        if (list.Id > 0)
                        {
                            list.Sort = 0;
                            list.OpId = 0;
                            list.UpdateDate = DateTime.Now;
                            list.UpdateMan = SysUserName + "-" + SysRealName;
                            db.SaveChanges();
                        }
                    }
                    //修改关联运营中心仓
                    var storeForOperate = opdb.StoreForOperate.Where(m => m.Status == 0 && m.OpId == sysAdmin.UserId && m.Sort == sysAdmin.UserId && m.QueryCount == 0).ToList();
                    foreach (var items in storeForOperate)
                    {
                        var lists = storeForOperate.FirstOrDefault(m => m.Id == items.Id) ?? new OpModels.StoreForOperate();
                        if (lists.Id > 0)
                        {
                            lists.Status = -1;
                            lists.UpdateDate = DateTime.Now;
                            lists.UpdateMan = SysUserName + "-" + SysRealName;
                            opdb.SaveChanges();
                        }
                    }
                    //修改运营中心关联分仓
                    var storeForOperates = opdb.StoreForOperate.Where(m => m.Status == 0 && m.OpId == sysAdmin.UserId && m.Sort == 0 && m.QueryCount == sysAdmin.UserId).ToList();
                    foreach (var items in storeForOperates)
                    {
                        var lists = storeForOperates.FirstOrDefault(m => m.Id == items.Id) ?? new OpModels.StoreForOperate();
                        if (lists.Id > 0)
                        {
                            lists.Status = -1;
                            lists.UpdateDate = DateTime.Now;
                            lists.UpdateMan = SysUserName + "-" + SysRealName;
                            opdb.SaveChanges();
                        }
                    }
                    opdb.SaveChanges();
                }
                Dictionary<string, object> Fields = new Dictionary<string, object>();
                Fields.Add("Status", -1);
                new AdminContentByConn(_accessor.HttpContext, PublicFunction.OpTables, OpConn).Edit("SysAdmin", Fields, id);
            }
            opdb.SaveChanges();

            return "success";
        }

        #endregion

        #region 开启

        /// <summary>
        /// 开启
        /// </summary>
        /// <returns></returns>
        public string Open(string Id)
        {
            string[] idlist = Id.Split(new char[] { ',' });
            AddSysLog(Id, "SysAdmin", "open");
            foreach (string subid in idlist)
            {
                int id = int.Parse(subid);
                Dictionary<string, object> Fields = new Dictionary<string, object>();
                Fields.Add("Status", 1);
                new AdminContentByConn(_accessor.HttpContext, PublicFunction.OpTables, OpConn).Edit("SysAdmin", Fields, id);
            }
            opdb.SaveChanges();
            return "success";
        }

        #endregion

        #region 关闭

        /// <summary>
        /// 关闭
        /// </summary>
        /// <returns></returns>
        public string Close(string Id)
        {
            string[] idlist = Id.Split(new char[] { ',' });
            AddSysLog(Id, "SysAdmin", "close");
            foreach (string subid in idlist)
            {
                int id = int.Parse(subid);
                Dictionary<string, object> Fields = new Dictionary<string, object>();
                Fields.Add("Status", 0);
                new AdminContentByConn(_accessor.HttpContext, PublicFunction.OpTables, OpConn).Edit("SysAdmin", Fields, id);
            }
            opdb.SaveChanges();
            return "success";
        }

        #endregion

        #region 排序
        /// <summary>
        /// 排序
        /// </summary>
        /// <param name="Id"></param>
        public string Sort(int Id, int Sort)
        {
            new AdminContentByConn(_accessor.HttpContext, PublicFunction.OpTables, OpConn).Sort("SysAdmin", Sort, Id);

            AddSysLog(Id.ToString(), "SysAdmin", "sort");
            return "success";
        }
        #endregion

        #region 导入数据
        /// <summary>
        /// 导入数据
        /// </summary>
        /// <param name="ExcelData"></param>
        public string Import(string ExcelData)
        {
            ExcelData = HttpUtility.UrlDecode(ExcelData);
            JsonData list = JsonMapper.ToObject(ExcelData);
            for (int i = 1; i < list.Count; i++)
            {
                JsonData dr = list[i];

                opdb.SysAdmin.Add(new SysAdmin()
                {
                    CreateDate = DateTime.Now,
                    UpdateDate = DateTime.Now,

                });
                opdb.SaveChanges();
            }
            AddSysLog("0", "SysAdmin", "Import");
            return "success";
        }
        #endregion

        #region 导出Excel

        /// <summary>
        /// 导出Excel
        /// </summary>
        /// <returns></returns>
        public JsonResult ExportExcel(SysAdmin data)
        {
            Dictionary<string, string> Fields = new Dictionary<string, string>();
            Fields.Add("AdminName", "2"); //用户名
            Fields.Add("RealName", "2"); //名称
            Fields.Add("Role", "1"); //角色
            Fields.Add("LastLoginDate", "3"); //最后登录时间

            Dictionary<string, object> obj = new AdminContentByConn(_accessor.HttpContext, PublicFunction.OpTables, OpConn).IndexData("SysAdmin", Fields, "Id desc", "0", 1, 20000, "", "", false);
            List<Dictionary<string, object>> diclist = obj["data"] as List<Dictionary<string, object>>;
            foreach (Dictionary<string, object> dic in diclist)
            {

            }

            Dictionary<string, object> result = new Dictionary<string, object>();
            result.Add("Status", "1");
            result.Add("Info", "Excel报表-" + DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss") + ".xlsx");
            result.Add("Obj", diclist);
            Dictionary<string, object> ReturnFields = new Dictionary<string, object>();

            result.Add("Fields", ReturnFields);
            AddSysLog("0", "SysAdmin", "ExportExcel");
            return Json(result);
        }

        #endregion


        #region 修改运营中心未使用额度

        public IActionResult EditAmt(string right, int Id = 0)
        {
            ViewBag.RightInfo = RightInfo;
            ViewBag.right = right;
            SysAdmin editData = opdb.SysAdmin.FirstOrDefault(m => m.Id == Id) ?? new SysAdmin();
            ViewBag.data = editData;
            return View();
        }

        #endregion


        #region 修改运营中心未使用额度

        [HttpPost]
        public string EditAmt(SysAdmin data, string OperationAmount, string Note, int Kind)
        {
            var sysAdmin = opdb.SysAdmin.FirstOrDefault(m => m.Id == data.Id);
            var account = opdb.UserAccount.FirstOrDefault(m => m.UserId == sysAdmin.UserId);
            decimal BeforeAmount = account.TotalAmt + account.ValidAmount + account.ValidForGetAmount; //变更前总金额

            int OperateType = 0;
            var description = "";
            if (Kind == 1)
            {
                account.TotalAmt -= decimal.Parse(OperationAmount);
                OperateType = 2;//系统扣减
                description = "系统扣减";
            }

            //增加
            else if (Kind == 2)
            {
                account.TotalAmt += decimal.Parse(OperationAmount);
                OperateType = 1;//系统增加
                description = "系统增加";
            }
            decimal AfterAmount = account.TotalAmt + account.ValidAmount + account.ValidForGetAmount; //变更后总金额

            AmountRecord amountRecord = opdb.AmountRecord.Add(new AmountRecord()
            {
                CreateDate = DateTime.Now,
                SeoKeyword = Note,
                SeoDescription = description,
                UserId = sysAdmin.UserId, //运营中心Id
                CreateMan = SysUserName + "-" + SysRealName,
                UseAmount = decimal.Parse(OperationAmount),//操作金额
                BeforeAmount = BeforeAmount,//使用前剩余额度
                AfterAmount = AfterAmount,//使用后剩余额度
                AfterTotalAmt = account.TotalAmt,
                AfterValidForGetAmount = account.ValidForGetAmount,
                AfterValidAmount = account.ValidAmount,
                OperateType = OperateType,//操作类别
            }).Entity;
            opdb.SaveChanges();
            AddSysLog(data.Id.ToString(), "SysAdmin", "EditAmt");
            opdb.SaveChanges();
            return "success";
        }

        #endregion


        #region 修改运营中心可提现额度

        public IActionResult EditBalance(string right, int Id = 0)
        {
            ViewBag.RightInfo = RightInfo;
            ViewBag.right = right;
            SysAdmin editData = opdb.SysAdmin.FirstOrDefault(m => m.Id == Id) ?? new SysAdmin();
            ViewBag.data = editData;
            return View();
        }

        #endregion

        #region 修改运营中心可提现额度

        [HttpPost]
        public string EditBalance(SysAdmin data, string OperationAmount, string Note, int Kind)
        {
            var sysAdmin = opdb.SysAdmin.FirstOrDefault(m => m.Id == data.Id);
            var account = opdb.UserAccount.FirstOrDefault(m => m.UserId == sysAdmin.UserId);
            if ((Kind == 1) && decimal.Parse(OperationAmount) > account.ValidForGetAmount)
            {
                return "账号余额不足";
            }
            decimal BeforeAmount = account.TotalAmt + account.ValidAmount + account.ValidForGetAmount; //变更前总金额
            int OperateType = 0;
            var description = "";
            if (Kind == 1 && decimal.Parse(OperationAmount) <= account.ValidForGetAmount)
            {
                account.ValidForGetAmount -= decimal.Parse(OperationAmount);
                OperateType = 2;
                description = "系统扣减";
            }
            else if (Kind == 1 && decimal.Parse(OperationAmount) > account.ValidForGetAmount)
            {
                return "扣减金额大于可提现额度";
            }
            else if (Kind == 2)
            {
                account.ValidForGetAmount += decimal.Parse(OperationAmount);
                OperateType = 1;
                description = "系统增加";
            }
            decimal AfterAmount = account.TotalAmt + account.ValidAmount + account.ValidForGetAmount; //变更后总金额
            AmountRecord amountRecord = opdb.AmountRecord.Add(new AmountRecord()
            {
                CreateDate = DateTime.Now,
                SeoKeyword = Note,
                SeoDescription = description,
                UserId = sysAdmin.UserId, //运营中心Id
                CreateMan = SysUserName + "-" + SysRealName,
                UseAmount = decimal.Parse(OperationAmount),//操作金额
                BeforeAmount = BeforeAmount,//使用前剩余额度
                AfterAmount = AfterAmount,//使用后剩余额度
                AfterTotalAmt = account.TotalAmt,
                AfterValidForGetAmount = account.ValidForGetAmount,
                AfterValidAmount = account.ValidAmount,
                OperateType = OperateType,//操作类别
            }).Entity;
            opdb.SaveChanges();
            AddSysLog(data.Id.ToString(), "SysAdmin", "EditBalance");
            opdb.SaveChanges();
            return "success";
        }

        #endregion


    }
}
