using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XYY.Common.Standard;
using XYY.Data.Standard.SinoOrder;
using XYY.Model.Standard.SinoOrder;
using XYY.Service.Standard.Order.Interfaces;

namespace XYY.Service.Standard.Order
{
    public class SinoOrderService : ISinoOrderService
    {
        ISinoOrderRepository _sinoOrderRepository;

        public SinoOrderService(ISinoOrderRepository sinoOrderRepository)
        {
            _sinoOrderRepository = sinoOrderRepository;
        }

        public async Task<PageResult<View_SinoOrder>> QueryPager(QueryModel model)
        {
            if (model == null)
            {
                model = new QueryModel
                {
                    PageSize = 100,
                    PageIndex = 1
                };
            }
            if (model.QueryParamer == null || model.QueryParamer.Count == 0)
            {
                model.QueryParamer = new List<QueryParamer>();
            }

            var codeTypeParam = model.QueryParamer.FirstOrDefault(i => i.Filed == "CodeType");
            var codeParam = model.QueryParamer.FirstOrDefault(p => p.Filed == "Codes");
            if (codeTypeParam != null)
            {
                if (codeParam != null)
                {
                    string field = codeTypeParam.Value == "1" ? "TransferNumber" : (codeTypeParam.Value == "3" ? "CustomerOrderNo" : "TrackingNumber");
                    var newCodeParam = new QueryParamer { Filed = field, Method = "In", Value = codeParam.Value };

                    model.QueryParamer.Add(codeParam);
                }

                model.QueryParamer.Remove(codeTypeParam);
            }
            if (codeParam != null)
                model.QueryParamer.Remove(codeParam);

            var timeTypeParam = model.QueryParamer.FirstOrDefault(i => i.Filed == "TimeType");
            var timeParams = model.QueryParamer.Where(i => i.Filed == "Time")?.ToList();
            if (timeTypeParam != null)
            {
                if (timeParams?.Any() == true)
                {
                    string field = timeTypeParam.Value == "1" ? "OrderTime" :
                        (timeTypeParam.Value == "2" ? "ReceiveTime" : (timeTypeParam.Value == "3" ? "TransferTime" : (timeTypeParam.Value == "4" ? "ReturnTime" : "OrderTime")));
                    foreach (var tp in timeParams)
                    {
                        var newCodeParam = new QueryParamer { Filed = field, Method = tp.Method, Value = tp.Value };
                        model.QueryParamer.Add(newCodeParam);
                    }
                }
                model.QueryParamer.Remove(timeTypeParam);

            }
            if (timeParams?.Any() == true)
            {
                model.QueryParamer.RemoveAll(i => i.Filed == "Time");
            }

            //默认查一个星期数据
            if (model.QueryParamer == null || model.QueryParamer.Count == 0)
            {
                model.QueryParamer = new List<QueryParamer> {
                    new QueryParamer { Filed="OrderTime",Method="GrE",Value=DateTime.Now.AddDays(-7).ToString_HHmmss()} ,
                    new QueryParamer { Filed="OrderTime",Method="LeE",Value=DateTime.Now.ToString_HHmmss()}
                    };
            }


            return await _sinoOrderRepository.QueryPagerAsync(model);
        }
    }
}
