/*
 * 创客账户变动记录
 */

using System;
using System.Web;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Data;
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 AmountRecordOperateController : BaseController
    {
        public AmountRecordOperateController(IHttpContextAccessor accessor, ILogger<BaseController> logger, IOptions<Setting> setting) : base(accessor, logger, setting)
        {
            OtherMySqlConn.connstr = ConfigurationManager.AppSettings["OpSqlConnStr"].ToString();
        }

        #region 运营中心额度变动明细记录列表

        /// <summary>
        /// 根据条件查询运营中心额度变动明细记录列表
        /// </summary>
        /// <returns></returns>
        public IActionResult Index(AmountRecord data, string right, int UserId = 0)
        {
            ViewBag.RightInfo = RightInfo;
            ViewBag.right = right;

            return View();
        }

        #endregion

        #region 根据条件查询运营中心额度变动明细记录列表

        /// <summary>
        /// 运营中心额度变动明细记录列表
        /// </summary>
        /// <returns></returns>
        public JsonResult IndexData(AmountRecord data, string MakerCode, string OpCode, string CreateDateData, int page = 1, int limit = 30)
        {

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

            string condition = " and Status>-1";
            //创客编号
            if (!string.IsNullOrEmpty(MakerCode))
            {
                condition += " and UserId in (select UserId from SysAdmin where MakerCode='" + MakerCode + "')";
            }
            //运营中心编号
            if (!string.IsNullOrEmpty(OpCode))
            {
                condition += " and UserId in (select UserId from SysAdmin 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'";
                var start = Convert.ToDateTime(Convert.ToDateTime(datelist[0]).ToString("yyyy-MM-dd"));
                var end = Convert.ToDateTime(Convert.ToDateTime(datelist[1]).ToString("yyyy-MM-dd") + " 23:59:59");
                var check = opdb.AmountRecord.Any(m => m.CreateDate >= start);
                var checks = opdb.AmountRecord.Any(m => m.CreateDate <= end);
                if (check)
                {
                    var sId = opdb.AmountRecord.Where(m => m.CreateDate >= start).Min(m => m.Id);
                    condition += "  and Id >=" + sId;
                }
                if (checks)
                {
                    var eId = opdb.AmountRecord.Where(m => m.CreateDate <= end).Max(m => m.Id);
                    condition += " and Id <=" + eId;
                }
            }
            else
            {
                var start = Convert.ToDateTime(DateTime.Now.AddMonths(-1).ToString("yyyy-MM") + "-01");
                var check = opdb.AmountRecord.Any(m => m.CreateDate >= start);
                if (check)
                {
                    var minId = opdb.AmountRecord.Where(m => m.CreateDate >= start).Min(m => m.Id);
                    var Info = function.ReadInstance("/WritePage/AmountRecordOperate/AmountRecordOperate.txt");
                    if (string.IsNullOrEmpty(Info.ToString()))
                    {
                        function.WritePage("/WritePage/AmountRecordOperate/", "AmountRecordOperate.txt", minId.ToString());
                        condition += " and Id >=" + minId;
                    }
                    else if (minId != int.Parse(Info))
                    {
                        function.WritePage("/WritePage/AmountRecordOperate/", "AmountRecordOperate.txt", minId.ToString());
                        condition += " and Id >=" + minId;
                    }
                    else
                    {
                        condition += " and Id >=" + Convert.ToInt32(Info);
                    }
                }
                else
                {
                    condition += " and Id =0";
                }
            }

            Dictionary<string, object> obj = new AdminContentByConn(_accessor.HttpContext, PublicFunction.OpTables, OpConn).IndexData("AmountRecord", 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 UserId = int.Parse(dic["UserId"].ToString());
                SysAdmin sysAdmin = opdb.SysAdmin.FirstOrDefault(m => m.UserId == UserId) ?? new SysAdmin();
                dic["OpCode"] = sysAdmin.OpCode;
                dic["MakerCode"] = sysAdmin.MakerCode;
                dic["MakerName"] = sysAdmin.MakerName;
                int OperateType = int.Parse(dic["OperateType"].ToString());
                dic["OperateTypeName"] = "";
                if (OperateType == 1) dic["OperateTypeName"] = "收入";
                if (OperateType == 2) dic["OperateTypeName"] = "支出";
            }
            return Json(obj);
        }

        #endregion

    }
}
