using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Bailun.DC.Models;
using Bailun.DC.Services;
using Newtonsoft.Json;

namespace Bailun.DC.Web.Areas.Reports.Controllers
{
    [Area("Reports")]
    public class WarehouseController : Controller
    {

        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 逻辑仓库 库存
        /// </summary>
        /// <returns></returns>
        public ActionResult LogicWarehouse()
        {
            return View();
        }

        /// <summary>
        /// 逻辑仓库 库存列表
        /// </summary>
        /// <param name="parameter">分页信息</param>
        /// <param name="warehousetype">仓库类型</param>
        /// <param name="warehouse">仓库编码</param>
        /// <returns></returns>
        public string LogicWarehouseJson(BtTableParameter parameter,string warehousetype,string warehousecode)
        {
            var service = new Services.WareHouseServices();
            var total = 0;
            var obj = service.ListLogicWarehouseStock(parameter, warehousetype, warehousecode, ref total);

            var countM = service.LogicWarehouseStockCount(warehousetype, warehousecode);

            countM.warehouse_name = "总计";
            countM.amount_onedaysale = Math.Round(countM.amount_onedaysale??0, 2, MidpointRounding.AwayFromZero);
            countM.amount_stock = Math.Round(countM.amount_stock , 2, MidpointRounding.AwayFromZero);
            countM.amount_transit = Math.Round(countM.amount_transit??0, 2, MidpointRounding.AwayFromZero);
            countM.purchase_amount = Math.Round(countM.purchase_amount ?? 0, 2, MidpointRounding.AwayFromZero);

            var list = obj.Select(p => new {
                amount_onedaysale = (p.amount_onedaysale??0).ToString("N2"),
                amount_stock = p.amount_stock.ToString("N2"),
                amount_transit = (p.amount_transit??0.00M).ToString("N2"),
                fourteenday_total_sales = (p.fourteenday_total_sales??0).ToString("N2"),
                noshippingcount = (p.noshippingcount??0).ToString("N2"),
                nostockcount = (p.nostockcount??0).ToString("N2"),
                oneday_total_sales = (p.oneday_total_sales??0).ToString("N2"),
                purchase_amount = (p.purchase_amount??0).ToString("N2"),
                quantity_purchase = (p.quantity_purchase??0).ToString("N2"),
                quantity_transfer = (p.quantity_transfer??0).ToString("N2"),
                sevenday_total_sales = (p.sevenday_total_sales??0).ToString("N2"),
                thirtyday_total_sales = (p.thirtyday_total_sales??0).ToString("N2"),
                usable_stock = p.usable_stock.ToString("N2"),
                p.warehouse_code,
                p.warehouse_name,
            });
            
            return JsonConvert.SerializeObject(new { total = total, rows = list, count_row = new {
                countM.warehouse_name,
                amount_onedaysale = (countM.amount_onedaysale??0).ToString("N2"),
                amount_stock = countM.amount_stock.ToString("N2"),
                amount_transit = (countM.amount_transit??0).ToString("N2"),
                noshippingcount = (countM.noshippingcount??0).ToString("N2"),
                nostockcount = (countM.nostockcount??0).ToString("N2"),
                oneday_total_sales = (countM.oneday_total_sales??0).ToString("N2"),
                purchase_amount = (countM.purchase_amount??0).ToString("N2"),
                quantity_purchase = (countM.quantity_purchase??0).ToString("N2"),
                quantity_transfer = (countM.quantity_transfer??0).ToString("N2"),
                sevenday_total_sales = (countM.sevenday_total_sales??0).ToString("N2"),
                thirtyday_total_sales = (countM.thirtyday_total_sales??0).ToString("N2"),
                usable_stock = countM.usable_stock.ToString("N2"),
                fourteenday_total_sales = (countM.fourteenday_total_sales??0).ToString("N2")
            } }); 
        }

        /// <summary>
        /// Sku仓库
        /// </summary>
        /// <param name="code">仓库编码</param>
        /// <returns></returns>
        public ActionResult ListWareHouseSku(string code)
        {
            ViewBag.warehousecode = code;
            return View();
        }

        /// <summary>
        /// Sku仓库
        /// </summary>
        /// <param name="parameter">分页信息</param>
        /// <param name="code">仓库编码</param>
        /// <param name="categoryid">sku分类id</param>
        /// <param name="minordercount">14日均销量 最小值</param>
        /// <param name="maxordercount">14日均销量 最大值</param>
        /// <param name="stockmin">库存 最小值</param>
        /// <param name="stockmax">库存 最大值</param>
        /// <param name="stockamountmin">库存金额 最小值</param>
        /// <param name="stockamountmax">库存金额 最大值</param>
        /// <returns></returns>
        public string ListWareHouseSkuJson(BtTableParameter parameter, string code, int? push_status, int? minordercount, int? maxordercount, int? stockmin, int? stockmax, decimal? stockamountmin, decimal? stockamountmax,string skucategoryids)
        {
            var total = 0;
            var obj = new Services.WareHouseServices().ListWareHouseSku(parameter, code, null, minordercount, maxordercount, stockmin, stockmax, stockamountmin, stockamountmax, skucategoryids, push_status, ref total);

            var list = obj.Select(p => new {
                amount_onedaysale = p.amount_onedaysale??0,
                amount_stock = p.amount_stock??0,
                amount_transit = p.amount_transit??0,
                p.bailun_sku,
                p.category_name,
                noshippingcount = p.noshippingcount??0,
                nostockcount = p.nostockcount??0,
                oneday_total_sales = p.oneday_total_sales??0,
                purchase_amount = p.purchase_amount??0,
                quantity_purchase = p.quantity_purchase??0,
                quantity_transfer = p.quantity_transfer??0,
                sevenday_total_sales = p.sevenday_total_sales??0,

                push_status = p.push_status==1?"是":"否",
                p.sku_image,
                p.sku_title_cn,
                p.sku_title_en,
                thirtyday_total_sales = p.thirtyday_total_sales??0,
                usable_stock = p.usable_stock??0,
                p.warehouse_code,
                p.warehouse_name,
            });

            return JsonConvert.SerializeObject(new { total = total, rows = list });
        }

    }
}