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.Main;
using LitJson;
using Library;
using System.Data;
using MySystem.Service.Main;

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

        #region 经营数据-经营数据记录(月)
        // [Authorize]
        public JsonResult BussinessRecordForMonth(string value)
        {
            value = DesDecrypt(value);
            value = value.Replace("null", "\"\"");
            JsonData data = JsonMapper.ToObject(value);
            List<Dictionary<string, object>> dataList = BussinessRecordForMonthDo(value);
            return Json(new AppResultJson() { Status = "1", Info = "", Data = dataList });
        }
        private List<Dictionary<string, object>> BussinessRecordForMonthDo(string value)
        {
            JsonData data = JsonMapper.ToObject(value);
            int MerchantId = int.Parse(function.CheckInt(data["MerchantId"].ToString())); //商户Id
            string TradeMonth = data["TradeMonth"].ToString(); //月份
            int pageSize = int.Parse(function.CheckInt(data["PageSize"].ToString()));
            int pageNum = int.Parse(function.CheckInt(data["PageNum"].ToString()));

            var dataList = BusinessUtil.BussinessRecordForMonth(MerchantId, TradeMonth, pageSize, pageNum);
            return dataList;
        }
        #endregion



        #region 经营数据-经营数据记录(半年)
        // [Authorize]
        public JsonResult BussinessRecordForHalfYear(string value)
        {
            value = DesDecrypt(value);
            value = value.Replace("null", "\"\"");
            JsonData data = JsonMapper.ToObject(value);
            List<Dictionary<string, object>> dataList = BussinessRecordForHalfYearDo(value);
            return Json(new AppResultJson() { Status = "1", Info = "", Data = dataList });
        }
        private List<Dictionary<string, object>> BussinessRecordForHalfYearDo(string value)
        {
            JsonData data = JsonMapper.ToObject(value);
            int MerchantId = int.Parse(function.CheckInt(data["MerchantId"].ToString())); //商户Id
            int pageSize = int.Parse(function.CheckInt(data["PageSize"].ToString()));
            int pageNum = int.Parse(function.CheckInt(data["PageNum"].ToString()));

            var dataList = BusinessUtil.BussinessRecordForHalfYear(MerchantId, pageSize, pageNum);
            return dataList;
        }
        #endregion



        #region 经营数据-经营数据记录-经营详情列表
        // [Authorize]
        public JsonResult DayDetailList(string value)
        {
            value = DesDecrypt(value);
            value = value.Replace("null", "\"\"");
            JsonData data = JsonMapper.ToObject(value);
            List<Dictionary<string, object>> dataList = DayDetailListDo(value);
            return Json(new AppResultJson() { Status = "1", Info = "", Data = dataList });
        }
        private List<Dictionary<string, object>> DayDetailListDo(string value)
        {
            JsonData data = JsonMapper.ToObject(value);
            int MerchantId = int.Parse(function.CheckInt(data["MerchantId"].ToString())); //商户Id
            string TradeDate = data["TradeDate"].ToString(); //日期
            int pageSize = int.Parse(function.CheckInt(data["PageSize"].ToString()));
            int pageNum = int.Parse(function.CheckInt(data["PageNum"].ToString()));
            string condition = " and Status>=1";
            if (!string.IsNullOrEmpty(data["MerchantId"].ToString()))
            {
                condition += " and MerchantId=" + MerchantId;
            }
            if (!string.IsNullOrEmpty(data["TradeDate"].ToString()))
            {
                var start = TradeDate.Substring(0, 4) + "-" + TradeDate.Substring(4, 2) + "-" + TradeDate.Substring(6, 2) + " 00:00:00";
                var end = TradeDate.Substring(0, 4) + "-" + TradeDate.Substring(4, 2) + "-" + TradeDate.Substring(6, 2) + " 23:59:59";
                condition += " and CreateDate>='" + start + "' and CreateDate<='" + end + "'";
            }

            var dataList = BusinessUtil.DayDetailList(condition, pageSize, pageNum);
            return dataList;
        }
        #endregion



        #region 经营数据-经营数据记录-交易详情
        // [Authorize]
        public JsonResult OrderDetail(string value)
        {
            value = DesDecrypt(value);
            value = value.Replace("null", "\"\"");
            JsonData data = JsonMapper.ToObject(value);
            Dictionary<string, object> dataList = OrderDetailDo(value);
            return Json(new AppResultJson() { Status = "1", Info = "", Data = dataList });
        }
        private Dictionary<string, object> OrderDetailDo(string value)
        {
            JsonData data = JsonMapper.ToObject(value);
            int MerchantId = int.Parse(function.CheckInt(data["MerchantId"].ToString())); //商户Id
            int OrderId = int.Parse(function.CheckInt(data["OrderId"].ToString())); //订单Id
            string condition = " and Status>=1";
            if (!string.IsNullOrEmpty(data["OrderId"].ToString()))
            {
                condition += " and Id=" + OrderId;
            }
            if (!string.IsNullOrEmpty(data["MerchantId"].ToString()))
            {
                condition += " and MerchantId=" + MerchantId;
            }

            var obj = BusinessUtil.OrderDetail(condition);
            return obj;
        }
        #endregion


    }
}
