/*
 * 创客账户
 */

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

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

        #region 结算卡变更记录

        /// <summary>
        /// 根据条件查询结算卡变更记录
        /// </summary>
        /// <returns></returns>
        public IActionResult Index(SettlementCardChangeRecord data, string right)
        {
            ViewBag.RightInfo = RightInfo;
            ViewBag.right = right;
            return View();
        }

        #endregion

        #region 根据条件查询结算卡变更记录

        /// <summary>
        /// 结算卡变更记录
        /// </summary>
        /// <returns></returns>
        public JsonResult IndexData(SettlementCardChangeRecord data, string CreateDateData, string MakerCode, string RealName, string BeforeCardNo, string AfterCardNo, int page = 1, int limit = 30)
        {

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

            string condition = " and Status>-1";
            //创建时间
            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'";
            }
            //创客编号
            if (!string.IsNullOrEmpty(MakerCode))
            {
                condition += " and Id in (select UserId from UserForMakerCode where MakerCode='" + MakerCode + "')";
            }
            //创客名称
            if (!string.IsNullOrEmpty(RealName))
            {
                condition += " and Id in (select Id from Users where RealName='" + RealName + "')";
            }
            //变更前卡号
            if (!string.IsNullOrEmpty(BeforeCardNo))
            {
                condition += " and BeforeCardNo=" + BeforeCardNo;
            }
            //变更后卡号
            if (!string.IsNullOrEmpty(AfterCardNo))
            {
                condition += " and AfterCardNo=" + AfterCardNo;
            }

            Dictionary<string, object> obj = new AdminContentOther(_accessor.HttpContext, PublicFunction.MainTables).IndexData("SettlementCardChangeRecord", 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());
                Users user = db.Users.FirstOrDefault(m => m.Id == UserId) ?? new Users();
                dic["MakerCode"] = user.MakerCode;
                dic["RealName"] = user.RealName;
            }
            return Json(obj);
        }

        #endregion

    }
}
