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 ConsumerOrdersController : BaseController
    {
        public ConsumerOrdersController(IHttpContextAccessor accessor) : base(accessor)
        {
        }

        #region 交易查询-直连商户交易列表
        [Authorize]
        public JsonResult DirectConsumerOrdersList(string value)
        {
            value = PublicFunction.DesDecrypt(value);
            JsonData data = JsonMapper.ToObject(value);
            Dictionary<string, object> Other = new Dictionary<string, object>();
            List<Dictionary<string, object>> dataList = DirectConsumerOrdersListDo(value, out Other);
            return Json(new AppResultJson() { Status = "1", Info = "", Data = dataList, Other = Other });
        }
        private List<Dictionary<string, object>> DirectConsumerOrdersListDo(string value, out Dictionary<string, object> Other)
        {
            JsonData data = JsonMapper.ToObject(value);
            string MerchantName = data["MerchantName"].ToString(); //商户名称
            string MakerCode = data["MakerCode"].ToString(); //创客编号
            string OrderNo = data["OrderNo"].ToString(); //交易号
            string CreateDate = data["CreateDate"].ToString(); //交易时间
            string PayMode = data["PayMode"].ToString(); //交易平台(1 支付宝 2 微信)
            string IsAct = data["IsAct"].ToString(); //是否参与分账(0 否 1 是)
            string Status = data["Status"].ToString(); //交易状态(0 未支付 1 已支付)
            string SubjectType = data["SubjectType"].ToString(); //主体类型(1 企业 2 个体)
            string TradePayNo = data["TradePayNo"].ToString(); //第三方交易号
            string DivideFlag = data["DivideFlag"].ToString(); //分账状态(-1无 0 未分账 2 分账中 1 已完成)
            int pageSize = int.Parse(function.CheckInt(data["page_size"].ToString()));
            int pageNum = int.Parse(function.CheckInt(data["page_num"].ToString()));
            string condition = "";
            if (!string.IsNullOrEmpty(data["MerchantName"].ToString()))
            {
                var merAddInfo = MerchantAddInfoService.Query(" and CertMerchantName like '%" + MerchantName + "%'");
                condition += " and MerchantId='" + merAddInfo.Id + "'";
            }
            if (!string.IsNullOrEmpty(data["MakerCode"].ToString()))
            {
                var Ids = MerchantInfoUtil.QueryMerchantIdByMakerCode(MakerCode);
                condition += " and MerchantId in (" + Ids + ")";
            }
            if (!string.IsNullOrEmpty(data["OrderNo"].ToString()))
            {
                condition += " and OrderNo='" + OrderNo + "'";
            }
            if (!string.IsNullOrEmpty(data["CreateDate"].ToString()))
            {
                string[] datelist = CreateDate.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(data["PayMode"].ToString()))
            {
                condition += " and PayMode='" + PayMode + "'";
            }
            if (!string.IsNullOrEmpty(data["IsAct"].ToString()))
            {
                condition += " and IsAct='" + IsAct + "'";
            }
            if (!string.IsNullOrEmpty(data["Status"].ToString()))
            {
                if (Status == "0")
                {
                    condition += " and Status=0";
                }
                else
                {
                    condition += " and Status>0";
                }
            }
            if (!string.IsNullOrEmpty(data["SubjectType"].ToString()))
            {
                if (data["SubjectType"].ToString() == "1") SubjectType = "SUBJECT_TYPE_ENTERPRISE"; //企业公司
                if (data["SubjectType"].ToString() == "2") SubjectType = "SUBJECT_TYPE_INDIVIDUAL"; //个体工商户
                var Ids = MerchantInfoUtil.QueryMerchantIdBySubjectType(SubjectType);
                condition += " and MerchantId in (" + Ids + ")";
            }
            if (!string.IsNullOrEmpty(data["TradePayNo"].ToString()))
            {
                condition += " and TradePayNo='" + TradePayNo + "'";
            }
            if (!string.IsNullOrEmpty(data["DivideFlag"].ToString()))
            {
                var flag = int.Parse(data["DivideFlag"].ToString());
                if (flag == -1)
                {
                    condition += " and IsAct=0 and DivideFlag=0";
                }
                if (flag == 0)
                {
                    condition += " and IsAct=1 and DivideFlag=0";
                }
                if (flag == 2)
                {
                    condition += " and IsAct=1 and DivideFlag=1";
                }
                if (flag == 1)
                {
                    condition += " and IsAct=1 and DivideFlag=2";
                }
            }
            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 = ConsumerOrdersService.List(relationData, condition, out count, pageNum, pageSize);
            foreach (Dictionary<string, object> subdata in source)
            {
                Dictionary<string, object> curData = new Dictionary<string, object>();
                curData.Add("Id", subdata["Id"].ToString()); //Id
                var query = MerchantAddInfoService.Query(int.Parse(subdata["MerchantId"].ToString()));
                var subjectType = 0;
                curData.Add("MerchantName", query.CertMerchantName); //交易商户
                curData.Add("PayMoney", subdata["PayMoney"].ToString()); //交易金额
                var status = 0;
                if (subdata["Status"].ToString() != "0") status = 1;
                curData.Add("Status", status); //交易状态(0 未支付 1 已支付)
                curData.Add("PayMode", subdata["PayMode"].ToString()); //交易平台(1 支付宝 2 微信)
                curData.Add("OrderNo", subdata["OrderNo"].ToString()); //交易号
                curData.Add("CreateDate", subdata["CreateDate"].ToString() == "" ? "" : DateTime.Parse(subdata["CreateDate"].ToString()).ToString("yyyy-MM-dd HH:mm:ss")); //交易时间
                curData.Add("MaxDivi", ConsumerProfitService.Sum(" 1=1 and OrderId=" + subdata["Id"].ToString() + "","GetMoney").ToString("f2")); //分账金额
                if (query.SubjectType == "SUBJECT_TYPE_ENTERPRISE") subjectType = 1; //企业公司
                if (query.SubjectType == "SUBJECT_TYPE_INDIVIDUAL") subjectType = 2; //个体工商户
                curData.Add("SubjectType", subjectType); //主体类型(1 企业 2 个体)
                curData.Add("TradePayNo", TradePayNo); //第三方交易号
                var divideFlag = 0;
                var isAct = int.Parse(subdata["IsAct"].ToString());
                var dFlag = int.Parse(subdata["DivideFlag"].ToString());
                if (isAct == 0 && dFlag == 0)
                {
                    divideFlag = -1;
                }
                if (isAct == 1 && dFlag == 0)
                {
                    divideFlag = 0;
                }
                if (isAct == 1 && dFlag == 1)
                {
                    divideFlag = 2;
                }
                if (isAct == 1 && dFlag == 2)
                {
                    divideFlag = 1;
                }
                curData.Add("DivideFlag", divideFlag); //分账状态(-1无 0 未分账 2 分账中 1 已完成)
                dataList.Add(curData);
            }
            Other.Add("Count", count); //总数
            return dataList;
        }
        #endregion



        #region 交易查询-直连订单发起分红
        [Authorize]
        public JsonResult DirectQueryDivideAccounts(string value)
        {
            value = PublicFunction.DesDecrypt(value);
            JsonData data = JsonMapper.ToObject(value);
            AppResultJson result = DirectQueryDivideAccountsDo(value);
            return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
        }
        private AppResultJson DirectQueryDivideAccountsDo(string value)
        {
            JsonData data = JsonMapper.ToObject(value);
            string Id = data["Id"].ToString(); //订单Id
            return new AppResultJson() { Status = "1", Info = "成功" };
        }
        #endregion



    }
}
