

using dodohold.core;
using System.Text.Json;

namespace molilian.core
{
    public partial class ApiReportCore
    {
        public class RecordDTO
        {
            public int id { get; set; }
            public int data_type { get; set; }
            public DateTime report_date { get; set; }
            public int uclk_uv { get; set; } = 0;
            public int uclk_pv { get; set; } = 0;
            public int pay_ord_num { get; set; } = 0;
            public int order_ord_num { get; set; } = 0;
            public decimal pay_ord_amt { get; set; } = 0;
            public decimal pay_tk_disp_tfee { get; set; } = 0;
            public int pay_ord_uv { get; set; } = 0;
            public int eff_ord_num { get; set; } = 0;
            public decimal eff_ord_amt { get; set; } = 0;
            public decimal eff_tk_disp_tfee { get; set; } = 0;
            public int total_count { get; set; } = 0;
            public int abandon_count { get; set; } = 0;
            public int success_count { get; set; } = 0;
            public int req_other_aff_num { get; set; } = 0;
            public int req_goods_num { get; set; } = 0;
            public int req_live_num { get; set; } = 0;
            public int req_video_num { get; set; } = 0;
            public int req_shop_num { get; set; } = 0;
            public int req_base_num { get; set; } = 0;
            public int req_note_num { get; set; } = 0;
            public int resp_deeplink_num { get; set; } = 0;

        }



        public static async Task<List<dynamic>> ReportDailysAsync(JsonElement form, string clientIp, bool debug = false)
        {
            List<dynamic> list = [];
            //那就是入参日期和type
            //我们给出指定日期的：点击 付款 有效付款  转链API调用
            //type可以不入参，留空就两个场景的数据都显示就行了
            //先只要成功的就行了，，到时候他们要明细再加就行了

            int data_type = form.Read("type", 0);
            var report_date = form.PathReadArray<string>("query_date[]");

            DateTime stime = DateTime.Now.AddDays(-1).Date, etime = stime;
            if (report_date.Count == 2)
            {
                if (DateTime.TryParse(report_date[0], out stime) && DateTime.TryParse(report_date[1], out etime))
                {
                    stime = stime.Date;
                    etime = etime.Date;
                }
            }
            switch (data_type)
            {
                case 1:
                case 2:
                    {
                        var data = await DataTypeReportDailysAsync(data_type, stime, etime, clientIp, debug);
                        list.AddRange(data);

                    }
                    break;
                default:
                    {
                        int[] types = { 1, 2 };
                        foreach (var type in types)
                        {
                            var data = await DataTypeReportDailysAsync(type, stime, etime, clientIp, debug);
                            list.AddRange(data);
                        }
                    }
                    break;
            }
            return list;
        }

        public static async Task<List<dynamic>> DataTypeReportDailysAsync(int data_type, DateTime stime, DateTime etime, string clientIp, bool debug = false)
        {
            string filter = "`is_show`=@is_show AND data_type=@data_type AND report_date>=@stime AND report_date<=@etime";
            string orderBy = "report_date DESC";

            var list = new DBContext.Table("api_call_report_dailys")
                        .Where(filter, new { is_show = true, data_type, stime, etime })
                        .Order(orderBy)
                        .Select<RecordDTO>().ToList();

            if (!debug)
            {
                if (list.Count != 0)
                {
                    var ids = list.Select(o => o.id).ToList();

                    new DBContext.Table("api_call_report_dailys")
                        .Add("read_num:", "read_num+1")
                        .Where("id IN @ids", new { ids })
                        .Update();
                }
            }

            List<dynamic> data = list.Select(e => new
            {
                report_date = e.report_date.ToString("yyyy-MM-dd"),
                type = e.data_type,
                e.uclk_uv,
                e.uclk_pv,
                e.pay_ord_num,
                e.order_ord_num,
                e.pay_ord_amt,
                e.pay_tk_disp_tfee,
                e.pay_ord_uv,
                e.eff_ord_num,
                e.eff_ord_amt,
                e.eff_tk_disp_tfee,
                e.total_count,
                //e.abandon_count,
                e.success_count,
                e.req_other_aff_num,
                e.req_goods_num,
                e.req_live_num,
                e.req_video_num,
                e.req_shop_num,
                e.req_base_num,
                e.req_note_num,
                e.resp_deeplink_num
            }).ToList<dynamic>();
            return data;
        }

    }


}
