using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.AspNetCore.Authorization;
using System.Web;
using MySystem.Models.Main1;
using MySystem.Service.Main1;
using LitJson;
using Library;

namespace MySystem.Areas.Api.Controllers.v1.Main1
{
    [Area("Api")]
    [Route("/v1/QrCodePlateMain/[controller]/[action]")]
    public class MerchantInfoController : BaseController
    {
        public MerchantInfoController(IHttpContextAccessor accessor) : base(accessor)
        {
        }

        #region 商户激活-直连达标商户列表
        [Authorize]
        public JsonResult DirectQualifiedMerchants(string value)
        {
            value = PublicFunction.DesDecrypt(value);
            JsonData data = JsonMapper.ToObject(value);
            Dictionary<string, object> Other = new Dictionary<string, object>();
            List<Dictionary<string, object>> dataList = DirectQualifiedMerchantsDo(value, out Other);
            return Json(new AppResultJson() { Status = "1", Info = "", Data = dataList, Other = Other });
        }
        private List<Dictionary<string, object>> DirectQualifiedMerchantsDo(string value, out Dictionary<string, object> Other)
        {
            JsonData data = JsonMapper.ToObject(value);
            string MerchantName = data["MerchantName"].ToString(); //商户名称
            string AliPID = data["AliPID"].ToString(); //支付宝PID
            string WeChatNo = data["WeChatNo"].ToString(); //微信商户号
            int pageSize = int.Parse(function.CheckInt(data["page_size"].ToString()));
            int pageNum = int.Parse(function.CheckInt(data["page_num"].ToString()));
            string condition = " and IsAct=1 and ExamineStatus=1 and Months=10";
            if (!string.IsNullOrEmpty(MerchantName))
            {
                condition += " and Name like '%" + MerchantName + "%'";
            }
            if (!string.IsNullOrEmpty(AliPID))
            {
                var merAddInfo = MerchantAddInfoService.Query(" and AlipayPid=" + AliPID + "");
                condition += " and Id=" + merAddInfo.Id + "";
            }
            if (!string.IsNullOrEmpty(WeChatNo))
            {
                var merAddInfo = MerchantAddInfoService.Query(" and SubMchid=" + WeChatNo + "");
                condition += " and Id=" + merAddInfo.Id + "";
            }
            List<RelationData> relationData = new List<RelationData>();
            List<Dictionary<string, object>> dataList = new List<Dictionary<string, object>>();
            Other = new Dictionary<string, object>();
            int count = 0;
            List<Dictionary<string, object>> source = MerchantInfoService.List(relationData, condition, out count, pageNum, pageSize);
            foreach (Dictionary<string, object> subdata in source)
            {
                Dictionary<string, object> curData = new Dictionary<string, object>();
                var MerchantId = int.Parse(subdata["Id"].ToString());
                var merAddInfo = MerchantAddInfoService.Query(MerchantId);
                var Status = -1;
                var order = MerchantDepositOrderService.Query(" and Status>0 and MerchantId=" + MerchantId + "");
                if (order.Id > 0) Status = int.Parse(subdata["Status"].ToString());
                curData.Add("Id", MerchantId); //Id
                curData.Add("MerchantName", merAddInfo.CertMerchantName); //商户名称
                curData.Add("AliPID", merAddInfo.AlipayPid); //支付宝PID
                curData.Add("WeChatNo", merAddInfo.SubMchid); //微信商户号
                curData.Add("UpdateDate", order.UpdateDate.ToString() == "" ? "" : DateTime.Parse(order.UpdateDate.ToString()).ToString("yyyy-MM-dd HH:mm:ss")); //激活时间
                curData.Add("PayMode", order.Sort); //支付方式(1 支付宝 2 微信)
                curData.Add("Status", Status); //达标状态(-1 未参与 0 考核中 1 已通过 2 未通过 101 待领取 100 已领取 99 发放中)
                curData.Add("Months", subdata["Months"].ToString()); //达标月数
                dataList.Add(curData);
            }
            Other.Add("Count", count); //总数
            return dataList;
        }
        #endregion


        #region 商户管理-商户管理-直连重置登录密码
        [Authorize]
        public JsonResult DirectResetLoginPwd(string value)
        {
            value = PublicFunction.DesDecrypt(value);
            JsonData data = JsonMapper.ToObject(value);
            AppResultJson result = DirectResetLoginPwdDo(value);
            return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
        }
        private AppResultJson DirectResetLoginPwdDo(string value)
        {
            JsonData data = JsonMapper.ToObject(value);
            string MerchantId = data["MerchantId"].ToString(); //商户Id
            Dictionary<string, object> Obj = new Dictionary<string, object>();
            var Id = int.Parse(MerchantId);
            var query = main1db.MerchantInfo.FirstOrDefault(m => m.Id == Id) ?? new MerchantInfo();
            Dictionary<string, object> fields = new Dictionary<string, object>();
            if (query.Id > 0)
            {
                query.LoginPwd = function.MD532(query.Mobile.Substring(query.Mobile.Length - 6, 6)); //登录密码
            }
            main2db.SaveChanges();

            return new AppResultJson() { Status = "1", Info = "该商户的登录密码已重置为手机号后6位", Data = Obj };
            return new AppResultJson() { Status = "1", Info = "", Data = Obj };
        }
        #endregion


        #region 商户管理-商户管理-直连修改手机号
        [Authorize]
        public JsonResult DirectEditMobile(string value)
        {
            value = PublicFunction.DesDecrypt(value);
            JsonData data = JsonMapper.ToObject(value);
            AppResultJson result = DirectEditMobileDo(value);
            return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
        }
        private AppResultJson DirectEditMobileDo(string value)
        {
            JsonData data = JsonMapper.ToObject(value);
            string MerchantId = data["MerchantId"].ToString(); //商户Id
            string Mobile = data["Mobile"].ToString(); //新手机号
            Dictionary<string, object> Obj = new Dictionary<string, object>();
            if (Mobile.Length != 11 || !function.IsInt(Mobile) || Mobile.Substring(0, 1) != "1")
            {
                return new AppResultJson() { Status = "-1", Info = "手机号不正确" };
            }
            var Id = int.Parse(MerchantId);
            var query = main1db.MerchantInfo.FirstOrDefault(m => m.Id == Id) ?? new MerchantInfo();
            Dictionary<string, object> fields = new Dictionary<string, object>();
            if (query.Id > 0)
            {
                query.Mobile = Mobile;
                query.LoginPwd = function.MD532(Mobile.Substring(Mobile.Length - 6, 6)); //登录密码
            }
            main2db.SaveChanges();

            return new AppResultJson() { Status = "1", Info = "修改成功，新密码默认为该手机号后6位", Data = Obj };
        }
        #endregion


    }
}
