/*
 * 设置商户型创客记录
 */

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

namespace MySystem.Areas.Admin.Controllers
{
    [Area("Admin")]
    [Route("Admin/[controller]/[action]")]
    public class SetMerchantTypeRecordController : BaseController
    {
        public SetMerchantTypeRecordController(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(StoreSwapSn data, string right)
        {
            ViewBag.RightInfo = RightInfo;
            ViewBag.right = right;

            return View();
        }

        #endregion

        #region 根据条件查询设置商户型创客记录

        /// <summary>
        /// 设置商户型创客记录
        /// </summary>
        /// <returns></returns>
        public JsonResult IndexData(SetMerchantTypeRecord data, string PosSn, string MerUserTypeMakerCode, string PosFromMakerCode, string OperateTypeSelect, string CreateDateData, int page = 1, int limit = 30)
        {

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

            Fields.Add("PosSn", "1"); //SN号

            string condition = " and Status>-1";
            //商户型创客编号
            if (!string.IsNullOrEmpty(MerUserTypeMakerCode))
            {
                condition += " and UserId in (select UserId from UserForMakerCode where MakerCode='" + MerUserTypeMakerCode + "')";
            }
            //机具所属创客编号
            if (!string.IsNullOrEmpty(PosFromMakerCode))
            {
                condition += " and UserId in (select UserId from UserForRealName where RealName='" + PosFromMakerCode + "')";
            }
            //操作类型
            if(!string.IsNullOrEmpty(OperateTypeSelect))
            {
                condition += " and OperateType=" + OperateTypeSelect;
            }
            //操作时间
            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 AdminContentOther(_accessor.HttpContext, PublicFunction.MainTables).IndexData("SetMerchantTypeRecord", 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)
            {
                // dic["BrandName"] = RelationClass.GetKqProductBrandInfo(int.Parse(dic["BrandId"].ToString()));

                //商户型创客信息
                int ToUserId = int.Parse(dic["ToUserId"].ToString());
                Users tuser = db.Users.FirstOrDefault(m => m.Id == ToUserId) ?? new Users();
                dic["MerUserTypeMakerCode"] = tuser.MakerCode;
                dic["MerUserTypeName"] = tuser.RealName;

                //机具所属创客信息
                int FromUserId = int.Parse(dic["FromUserId"].ToString());
                Users fuser = db.Users.FirstOrDefault(m => m.Id == FromUserId) ?? new Users();
                dic["PosFromMakerCode"] = fuser.MakerCode;
                dic["PosFromName"] = fuser.RealName;
                
                //业务状态
                int OperateType = int.Parse(dic["OperateType"].ToString());
                if(OperateType == 1) dic["OperateTypeName"] = "设置";
                if(OperateType == 2) dic["OperateTypeName"] = "取消";

            }
            return Json(obj);
        }

        #endregion

    }
}
