/*
 * 提现记录
 */

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.CashModels;
using Library;
using LitJson;
using MySystemLib;

namespace MySystem.Areas.Admin.Controllers
{
    [Area("Admin")]
    [Route("Admin/[controller]/[action]")]
    public class WithdrawRecordController : BaseController
    {
        public WithdrawRecordController(IHttpContextAccessor accessor, ILogger<BaseController> logger, IOptions<Setting> setting) : base(accessor, logger, setting)
        {
            OtherMySqlConn.connstr = ConfigurationManager.AppSettings["CashSqlConnStr"].ToString();
        }

        #region 提现记录列表

        /// <summary>
        /// 根据条件查询提现记录列表
        /// </summary>
        /// <returns></returns>
        public IActionResult Index(WithdrawRecord data, string right)
        {
            ViewBag.RightInfo = RightInfo;
            ViewBag.right = right;


            return View();
        }

        #endregion

        #region 根据条件查询提现记录列表

        /// <summary>
        /// 提现记录列表
        /// </summary>
        /// <returns></returns>
        public JsonResult IndexData(WithdrawRecord data, string TradeStatusSelect, string CashChannelSelect, int page = 1, int limit = 30)
        {

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

            Fields.Add("CashRecNo", "1"); //提现单号
            Fields.Add("PartnerTradeNo", "1"); //业务单号
            Fields.Add("UserMobile", "1"); //用户手机号
            Fields.Add("BizTime", "3"); //业务时间
            Fields.Add("ChannelCode", "1"); //渠道编号
            Fields.Add("TransCompleteTime", "3"); //交易完成时间


            string condition = " and Status>-1";
            //订单状态
            if (!string.IsNullOrEmpty(TradeStatusSelect))
            {
                condition += " and TradeStatus=" + TradeStatusSelect;
            }
            //代付渠道
            if (!string.IsNullOrEmpty(CashChannelSelect))
            {
                condition += " and CashChannel=" + CashChannelSelect;
            }


            Dictionary<string, object> obj = new AdminContentOther(_accessor.HttpContext, PublicFunction.CashTables).IndexData("WithdrawRecord", 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 TradeStatus = int.Parse(dic["TradeStatus"].ToString());
                string CashChannel = dic["CashChannel"].ToString();
                if(CashChannel == "云汇算")
                {
                    if (TradeStatus == 0) dic["TradeStatusName"] = "初始化";
                    if (TradeStatus == 1) dic["TradeStatusName"] = "未审核,待打款";
                    if (TradeStatus == 2) dic["TradeStatusName"] = "审核通过,待打款";
                    if (TradeStatus == 3) dic["TradeStatusName"] = "审核不通过,已解冻";
                    if (TradeStatus == 4) dic["TradeStatusName"] = "冻结失败(预留)";
                    if (TradeStatus == 5) dic["TradeStatusName"] = "代付成功";
                    if (TradeStatus == 6) dic["TradeStatusName"] = "代付失败,待核实";
                    if (TradeStatus == 7) dic["TradeStatusName"] = "代付失败,已解冻";
                    if (TradeStatus == 8) dic["TradeStatusName"] = "代付处理中";
                }
                else if(CashChannel == "金控")
                {
                    if (TradeStatus == 0) dic["TradeStatusName"] = "进行中";
                    if (TradeStatus == 1) dic["TradeStatusName"] = "处理中";
                    if (TradeStatus == 2) dic["TradeStatusName"] = "成功";
                    if (TradeStatus == 3) dic["TradeStatusName"] = "失败";
                    if (TradeStatus == 4) dic["TradeStatusName"] = "取消";
                    if (TradeStatus == 10) dic["TradeStatusName"] = "可疑";
                }

            }
            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(WithdrawRecord data)
        {
            Dictionary<string, object> Fields = new Dictionary<string, object>();

            Fields.Add("AuditResult", data.AuditResult); //审核结果
            Fields.Add("AuditRemark", data.AuditRemark); //审核备注

            Fields.Add("SeoTitle", data.SeoTitle);
            Fields.Add("SeoKeyword", data.SeoKeyword);
            Fields.Add("SeoDescription", data.SeoDescription);
            int Id = new AdminContentOther(_accessor.HttpContext, PublicFunction.CashTables).Add("WithdrawRecord", Fields, 0);
            AddSysLog(data.Id.ToString(), "WithdrawRecord", "add");
            cashdb.SaveChanges();

            return "success";
        }

        #endregion

        #region 修改提现记录

        /// <summary>
        /// 增加或修改提现记录信息
        /// </summary>
        /// <returns></returns>
        public IActionResult Edit(string right, int Id = 0)
        {
            ViewBag.RightInfo = RightInfo;
            ViewBag.right = right;


            WithdrawRecord editData = cashdb.WithdrawRecord.FirstOrDefault(m => m.Id == Id) ?? new WithdrawRecord();
            ViewBag.data = editData;
            return View();
        }

        #endregion

        #region 修改提现记录

        /// <summary>
        /// 增加或修改提现记录信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public string Edit(WithdrawRecord data)
        {
            Dictionary<string, object> Fields = new Dictionary<string, object>();

            Fields.Add("AuditResult", data.AuditResult); //审核结果
            Fields.Add("AuditRemark", data.AuditRemark); //审核备注

            Fields.Add("SeoTitle", data.SeoTitle);
            Fields.Add("SeoKeyword", data.SeoKeyword);
            Fields.Add("SeoDescription", data.SeoDescription);
            new AdminContentOther(_accessor.HttpContext, PublicFunction.CashTables).Edit("WithdrawRecord", Fields, data.Id);
            AddSysLog(data.Id.ToString(), "WithdrawRecord", "update");
            cashdb.SaveChanges();

            return "success";
        }

        #endregion

        #region 删除提现记录信息

        /// <summary>
        /// 删除提现记录信息
        /// </summary>
        /// <returns></returns>
        public string Delete(string Id)
        {
            string[] idlist = Id.Split(new char[] { ',' });
            AddSysLog(Id, "WithdrawRecord", "del");
            foreach (string subid in idlist)
            {
                int id = int.Parse(subid);
                Dictionary<string, object> Fields = new Dictionary<string, object>();
                Fields.Add("Status", -1);
                new AdminContentOther(_accessor.HttpContext, PublicFunction.CashTables).Edit("WithdrawRecord", Fields, id);
            }
            cashdb.SaveChanges();

            return "success";
        }

        #endregion

        #region 开启

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

        #endregion

        #region 关闭

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

        #endregion

        #region 排序
        /// <summary>
        /// 排序
        /// </summary>
        /// <param name="Id"></param>
        public string Sort(int Id, int Sort)
        {
            new AdminContentOther(_accessor.HttpContext, PublicFunction.CashTables).Sort("WithdrawRecord", Sort, Id);

            AddSysLog(Id.ToString(), "WithdrawRecord", "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];

                cashdb.WithdrawRecord.Add(new WithdrawRecord()
                {
                    CreateDate = DateTime.Now,
                    UpdateDate = DateTime.Now,

                });
                cashdb.SaveChanges();
            }
            AddSysLog("0", "WithdrawRecord", "Import");
            return "success";
        }
        #endregion

        #region 导出Excel

        /// <summary>
        /// 导出Excel
        /// </summary>
        /// <returns></returns>
        public JsonResult ExportExcel(WithdrawRecord data, string TradeStatusSelect, string CashChannelSelect)
        {
            Dictionary<string, string> Fields = new Dictionary<string, string>();
            Fields.Add("CashRecNo", "1"); //提现单号
            Fields.Add("PartnerTradeNo", "1"); //业务单号
            Fields.Add("UserMobile", "1"); //用户手机号
            Fields.Add("BizTime", "3"); //业务时间
            Fields.Add("ChannelCode", "1"); //渠道编号
            Fields.Add("TransCompleteTime", "3"); //交易完成时间


            string condition = " and Status>-1";
            //订单状态
            if (!string.IsNullOrEmpty(TradeStatusSelect))
            {
                condition += " and TradeStatus=" + TradeStatusSelect;
            }
            //代付渠道
            if (!string.IsNullOrEmpty(CashChannelSelect))
            {
                condition += " and CashChannel=" + CashChannelSelect;
            }


            Dictionary<string, object> obj = new AdminContentOther(_accessor.HttpContext, PublicFunction.CashTables).IndexData("WithdrawRecord", Fields, "Id desc", "0", 1, 20000, condition, "CashRecNo,UserId,UserMobile,UserName,IdcardNo,BankCardNo,ChannelCode,ChannelName,TradeAmount,TradeFee,ManageFee,TradeStatus,TransCompleteTime,CashChannel", false);
            List<Dictionary<string, object>> diclist = obj["data"] as List<Dictionary<string, object>>;
            foreach (Dictionary<string, object> dic in diclist)
            {
                //订单状态
                int TradeStatus = int.Parse(dic["TradeStatus"].ToString());
                string CashChannel = dic["CashChannel"].ToString();
                if(CashChannel == "云汇算")
                {
                    if (TradeStatus == 0) dic["TradeStatus"] = "初始化";
                    if (TradeStatus == 1) dic["TradeStatus"] = "未审核,待打款";
                    if (TradeStatus == 2) dic["TradeStatus"] = "审核通过,待打款";
                    if (TradeStatus == 3) dic["TradeStatus"] = "审核不通过,已解冻";
                    if (TradeStatus == 4) dic["TradeStatus"] = "冻结失败(预留)";
                    if (TradeStatus == 5) dic["TradeStatus"] = "代付成功";
                    if (TradeStatus == 6) dic["TradeStatus"] = "代付失败,待核实";
                    if (TradeStatus == 7) dic["TradeStatus"] = "代付失败,已解冻";
                    if (TradeStatus == 8) dic["TradeStatus"] = "代付处理中";
                }
                else if(CashChannel == "金控")
                {
                    if (TradeStatus == 0) dic["TradeStatus"] = "进行中";
                    if (TradeStatus == 1) dic["TradeStatus"] = "处理中";
                    if (TradeStatus == 2) dic["TradeStatus"] = "成功";
                    if (TradeStatus == 3) dic["TradeStatus"] = "失败";
                    if (TradeStatus == 4) dic["TradeStatus"] = "取消";
                    if (TradeStatus == 10) dic["TradeStatus"] = "可疑";
                }

            }

            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>();
            ReturnFields.Add("CashRecNo", "提现单号");
            ReturnFields.Add("UserId", "创客");
            ReturnFields.Add("UserMobile", "用户手机号");
            ReturnFields.Add("UserName", "用户姓名");
            ReturnFields.Add("IdcardNo", "身份证号");
            ReturnFields.Add("BankCardNo", "提现卡号");
            ReturnFields.Add("ChannelCode", "渠道编号");
            ReturnFields.Add("ChannelName", "渠道名称");
            ReturnFields.Add("TradeAmount", "交易金额");
            ReturnFields.Add("TradeFee", "交易渠道手续费");
            ReturnFields.Add("ManageFee", "交易平台管理");
            ReturnFields.Add("TradeStatus", "订单状态");
            ReturnFields.Add("TransCompleteTime", "交易完成时间");
            ReturnFields.Add("CashChannel", "代付渠道");

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

        #endregion


    }
}
