using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using MESClassLibrary.BLL.Log;
using MESClassLibrary.EFModel;
using MESClassLibrary.Model;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace MESClassLibrary.BLL.Stock
{
    public class AppendColorBLL
    {
        BasicBLL<tb_StockInColor> db = new BasicBLL<tb_StockInColor>();
        public string SearchInfoAll(string page, string pageSize, string barCode, string startTime, string endTime)
        {
            try
            {
                string jsonStr = "[]";
                int total = 0;//总行数
                List<tb_StockInColor> list = db.SearchAllInfo();

                if (!string.IsNullOrWhiteSpace(barCode))
                {
                    list = list.Where(p => p.Barcode != null && p.Barcode.Contains(barCode)).ToList();
                }

                if (!string.IsNullOrWhiteSpace(startTime))
                {
                    list = list.Where(p => p.CreateTime >= Convert.ToDateTime(startTime) && p.CreateTime <= Convert.ToDateTime(endTime)).ToList();
                }

                list = list.OrderBy(p => p.CreateTime).ToList();

                if (list.Count > 0)
                {
                    total = list.Count;

                    int skipCount = (Convert.ToInt32(page) - 1) * Convert.ToInt32(pageSize);
                    list = list.Skip(skipCount).Take(Convert.ToInt32(pageSize)).ToList();

                    JsonDataModel<tb_StockInColor> md = new JsonDataModel<tb_StockInColor>();
                    md.total = total.ToString();
                    md.rows = list;
                    IsoDateTimeConverter timeConverter = new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" };
                    jsonStr = JsonConvert.SerializeObject(md, Formatting.Indented, timeConverter);
                }
                return jsonStr;
            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
                return "";
            }
        }

        public List<List<string>> SearchForExcel(string page, string pageSize, string barCode, string startTime, string endTime)
        {
            try
            {
                List<List<string>> list = new List<List<string>>();

                string jsonStr = "[]";
                int total = 0;//总行数
                List<tb_StockInColor> listProduct = db.SearchAllInfo().OrderBy(p => p.CreateTime).ToList();

                if (!String.IsNullOrEmpty(barCode))
                {
                    listProduct = listProduct.Where(p => p.Barcode != null && p.Barcode.Contains(barCode)).ToList();
                }

                if (!String.IsNullOrEmpty(startTime))
                {
                    listProduct = listProduct.Where(p => p.CreateTime >= Convert.ToDateTime(startTime) && p.CreateTime <= Convert.ToDateTime(endTime)).ToList();
                }

                if (listProduct != null && listProduct.Count() > 0)
                {
                    List<string> title_ = new List<string>();
                    title_.Add("条码");
                    title_.Add("颜色");
                    title_.Add("操作人");
                    title_.Add("操作时间");
                    //title_.Add("描述");
                    list.Add(title_);

                    foreach (var item in listProduct)
                    {
                        List<string> rowList = new List<string>();
                        rowList.Add(item.Barcode == null ? "" : item.Barcode);
                        rowList.Add(item.ColorInfo == null ? "" : item.ColorInfo);
                        rowList.Add(item.CreateBy == null ? "" : item.CreateBy);
                        rowList.Add(item.CreateTime.ToString());
                        //rowList.Add(item.SupplierDes == null ? "" : item.SupplierDes);
                        list.Add(rowList);
                    }
                }
                return list;
            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
                return null;
            }
        }
    }
}