using JiepeiWMS.Common.Enums;
using JiepeiWMS.Common.Helper;
using JiepeiWMS.Extends;
using JiepeiWMS.IServices;
using JiepeiWMS.Model;
using JiepeiWMS.Model.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
namespace JiepeiWMS.Api.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
[Authorize(Permissions.Name)]
public class WMStockLogController : ControllerBase
{
///
/// 库存流水日志
///
private readonly IWMStockLogServices _wMStockLogServices;
private readonly IWMProductListServices _wMProductListServices;
private readonly IWMProductClassServices _wMProductClassServices;
private readonly IWMMaterialCostServices _wMMaterialCostServices;
public WMStockLogController(IWMStockLogServices WMStockLogServices,
IWMProductListServices WMProductListServices,
IWMProductClassServices WMProductClassServices,
IWMMaterialCostServices WMMaterialCostServices)
{
_wMStockLogServices = WMStockLogServices;
_wMProductListServices = WMProductListServices;
_wMProductClassServices = WMProductClassServices;
_wMMaterialCostServices = WMMaterialCostServices;
}
///
/// 查询列表
///
/// 页数
/// 台账类型
/// 所属仓库
/// 开始时间
/// 结束时间
/// 关键字
/// 条数
///
[HttpGet]
public async Task>> Index(string key = "", string typeoptions = "", string warehouseoptions = "", DateTime? startdate = null, DateTime? enddate = null, int page = 1, int intPageSize = 20)
{
if (string.IsNullOrWhiteSpace(key))
key = "";
if (string.IsNullOrWhiteSpace(typeoptions))
typeoptions = "";
if (string.IsNullOrWhiteSpace(warehouseoptions))
warehouseoptions = "";
var PageList = await _wMStockLogServices.GetWMStockLogPage(page, intPageSize, key, typeoptions, warehouseoptions, startdate, enddate);
return new MessageModel>()
{
msg = "获取成功",
success = PageList.dataCount >= 0,
response = PageList
};
}
///
/// 导出物料初期、初末实况表格
///
[HttpGet]
public async Task ExportStockLog(int type)
{
var stockLogList = await _wMStockLogServices.QuerySql("select sl.Id,case when sl.Type=1 then (select PurchaseCode from WMPurchaseInfo where Id=OrderId) when sl.Type=2 then (select SaleCode from WMSaleInfo where Id=OrderId) when sl.Type=4 then (select QuoteCode from WMQuoteInfo where Id=OrderId) end CodeNo,Code,pl.Name ProductName,ps.Name ProductSpec,wh.Name WareHouseName,case when sl.Type=0 then '初始化库存' when sl.Type=1 then '采购入库' when sl.Type=2 then '销售出库' when sl.Type=3 then '库存盘点' when sl.Type=4 then '报价入库' end TypeName,case when sl.Quantity > 0 then case when sl.Operation=1 then '+' else '-' end + CAST(sl.Quantity AS varchar) else '' end QuantitySign,pl.Price,TotalPrice,RemainQuantity,RemainPrice,TotalRemainPrice,sl.CreateTime,ui.uRealName AdminName from WMStockLog sl left join WMProductList pl on sl.ProductListId = pl.id left join WMProductSpec ps on sl.ProductSpecId = ps.id left join WMWareHouse wh on sl.WareHouseId = wh.id left join sysUserInfo ui on sl.AdminId = ui.uID");
if (stockLogList != null && stockLogList.Any())
{
var classId = 0;
var date = DateTime.Now;
var StartCreateTime = date.ToString("yyyy-MM-01").ObjToDate();//当前月份得第一天
var EndCreateTime = StartCreateTime.AddMonths(1).AddSeconds(-1);//当月月份得最后一天
var LList = stockLogList.Where(x => x.CreateTime >= StartCreateTime && x.CreateTime < EndCreateTime).Select(c => new { c.Id, c.CodeNo, c.Code, c.ProductName, c.ProductSpec, c.WareHouseName, c.TypeName, c.QuantitySign, c.Price, c.TotalPrice, c.RemainQuantity, c.RemainPrice, c.TotalRemainPrice, c.CreateTime, c.AdminName }).ToList();
var heads = new List() { "编号", "订单编号", "商品编号", "商品名称", "商品规格", "仓库名称", "台账类型", "台账数量", "台账单价", "台账总价", "库存数量", "库存均价", "库存总价", "创建时间", "创建人" };
var stream = ExcelHelper.CreateExcelStreamFromList(LList, heads);
foreach (var item in await _wMStockLogServices.Query(x => x.CreateTime >= StartCreateTime && x.CreateTime < EndCreateTime))
{
var DepId = item.DepId.Value;
var ProductListId = item.ProductListId;
var Year = item.CreateTime.Year;
var Month = item.CreateTime.Month;
var ProductListModel = await _wMProductListServices.QueryById(item.ProductListId);
if (ProductListModel != null)
{
var ProductClassModel = await _wMProductClassServices.QueryById(ProductListModel.ClassId);
if (ProductClassModel != null)
classId = ProductClassModel.Id;
}
var PeriodEndCost = item.RemainQuantity * item.RemainPrice;//库存数量 * 库存均价
Expression> whereExpression = x => x.Id > 0;
if (DepId > 0)
whereExpression = whereExpression.And(x => x.DepId == DepId);
if (classId > 0)
whereExpression = whereExpression.And(x => x.ClassId == classId);
if (ProductListId > 0)
whereExpression = whereExpression.And(x => x.ProductId == ProductListId);
if (Year > 0)
whereExpression = whereExpression.And(x => x.Years == Year);
if (Month > 0)
whereExpression = whereExpression.And(x => x.Months == Month);
var MaterialCostModel = await _wMMaterialCostServices.GetModel(whereExpression);
if (MaterialCostModel != null)
{
MaterialCostModel.UpdateTime = DateTime.Now;
MaterialCostModel.PeriodEndCost = PeriodEndCost;
var result = await _wMMaterialCostServices.Update(MaterialCostModel);
}
else
{
WMMaterialCost model = new WMMaterialCost()
{
DepId = DepId,
ClassId = classId,
ProductId = ProductListId,
Years = Year,
Months = Month,
CreateTime = DateTime.Now,
PeriodEndCost = PeriodEndCost
};
var result1 = await _wMMaterialCostServices.Add(model);
}
}
return File(stream, "application/octet-stream", type == 1 ? "物料初期实况" + StartCreateTime.ToString("yyyy-MM-dd") + "~" + EndCreateTime.ToString("yyyy-MM-dd") + ".xlsx" : type == 2 ? "物料初末实况.xlsx" : "");
}
return null;
}
}
}