using JiepeiWMS.Common.Enums;
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.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using JiepeiWMS.Common.Helper;
using JiepeiWMS.Model.ViewModels;
using JiepeiWMS.Common.HttpContextUser;
using JiepeiWMS.Api.Models;
namespace JiepeiWMS.Api.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
[Authorize(Permissions.Name)]
public class WMPurchaseQuoteDetailController : ControllerBase
{
///
/// 服务器接口,因为是模板生成,所以首字母是大写的,自己可以重构下
///
private readonly IWMPurchaseQuoteDetailServices _BllWMPurchaseQuoteDetail;
private readonly IDepartmentServices _BllDepartment;
private readonly IWMProductUnitServices _BllWMProductUnit;
private readonly ISysOrgServices _BllSysOrg;
private readonly IWMSupplierServices _BllWMSupplier;
private readonly IWMQuoteInfoServices _BllWMQuoteInfo;
private readonly IWMQuoteInfoLogServices _BllWMQuoteInfoLog;
private readonly IWMCheckInfoServices _BLLWMCheckInfo;
private readonly IUser _User;
private readonly IWMWareHouseServices _wMWareHouseServices;
private readonly ISysUserInfoServices _sysUserInfoServices;
private readonly IWMSupplierServices _wMSupplierServices;
public WMPurchaseQuoteDetailController(
IWMPurchaseQuoteDetailServices BllWMPurchaseQuoteDetail,
IDepartmentServices BllDepartment,
IWMProductUnitServices BllWMProductUnit,
ISysOrgServices BllSysOrg,
IWMSupplierServices BllWMSupplier,
IWMQuoteInfoServices BllWMQuoteInfo,
IWMQuoteInfoLogServices BllWMQuoteInfoLog,
IWMCheckInfoServices BLLWMCheckInfo,
IUser User,
IWMWareHouseServices WMWareHouseServices,
ISysUserInfoServices sysUserInfoServices,
IWMSupplierServices WMSupplierServices
)
{
_BllWMPurchaseQuoteDetail = BllWMPurchaseQuoteDetail;
_BllDepartment = BllDepartment;
_BllWMProductUnit = BllWMProductUnit;
_BllSysOrg = BllSysOrg;
_BllWMSupplier = BllWMSupplier;
_BllWMQuoteInfo = BllWMQuoteInfo;
_BllWMQuoteInfoLog = BllWMQuoteInfoLog;
_BLLWMCheckInfo = BLLWMCheckInfo;
_User = User;
_wMWareHouseServices = WMWareHouseServices;
_sysUserInfoServices = sysUserInfoServices;
_wMSupplierServices = WMSupplierServices;
}
[HttpGet]
public async Task>> GetList(
int? Id = null, string ProductCode = null, string PurchaseCode = null,
string QuoteCode = null, int? Status = null, DateTime? StartTime = null, DateTime? EndTime = null,
int PageIndex = 1, int PageSize = 20)
{
Expression> whereExpression =
(pg, pl, pi, qi, dp, ps) => pg.SysOrgId == _User.SysOrgId;
if (Id != null)
{
whereExpression = whereExpression.And((pg, pl, pi, qi, dp, ps) => pg.Id == Id);
}
if (!string.IsNullOrEmpty(ProductCode))
{
ProductCode = ProductCode.Trim();
whereExpression = whereExpression.And((pg, pl, pi, qi, dp, ps) => pl.Code == ProductCode);
}
if (!string.IsNullOrEmpty(PurchaseCode))
{
PurchaseCode = PurchaseCode.Trim();
whereExpression = whereExpression.And((pg, pl, pi, qi, dp, ps) => pi.PurchaseCode == PurchaseCode);
}
if (!string.IsNullOrEmpty(QuoteCode))
{
QuoteCode = QuoteCode.Trim();
whereExpression = whereExpression.And((pg, pl, pi, qi, dp, ps) => qi.QuoteCode == QuoteCode);
}
if (Status == 30)//审核通过
{
whereExpression = whereExpression.And((pg, pl, pi, qi, dp, ps) => pg.Status == 30);
}
else if (Status == 80)//采购中
{
whereExpression = whereExpression.And((pg, pl, pi, qi, dp, ps) => pg.Status == 80 || pg.Status == 90);
}
if (StartTime.HasValue)
{
whereExpression = whereExpression.And((pg, pl, pi, qi, dp, ps) => pg.CreateTime >= StartTime.Value);
}
if (EndTime.HasValue)
{
EndTime = EndTime.Value.AddSeconds(1);
whereExpression = whereExpression.And((pg, pl, pi, qi, dp, ps) => pg.CreateTime < EndTime.Value);
}
var plst = await _BllWMPurchaseQuoteDetail.QueryPage(
(pg, pl, pi, qi, dp, ps) => new object[] {
JoinType.Left, pg.ProductId == pl.Id,
JoinType.Left, pg.PurchaseId == pi.Id,
JoinType.Left, pg.QuoteId == qi.Id,
JoinType.Left, pg.DepartmentId == dp.Id,
JoinType.Left, pl.SpecIds==ps.Id
}, (pg, pl, pi, qi, dp, ps) => new WMPurchaseQuoteDetail
{
Id = pg.Id,
Amount = pg.Amount,
CheckNum = pg.CheckNum,
CreateTime = pg.CreateTime,
DepartmentId = pg.DepartmentId,
DepartmentName = dp.Name,
DisAmount = pg.DisAmount,
Discount = pg.Discount,
DiscountPrice = pg.DiscountPrice,
Price = pg.Price,
ProductCode = pl.Code,
ProductId = pg.ProductId,
ProductName = pl.Name,
ProductSpec = ps.Name,
PuchaseOrderNo = pi.PurchaseCode,
PurchaseId = pg.PurchaseId,
PurchaseNum = pg.PurchaseNum,
PurchaseUnit = pg.PurchaseUnit,
Quantity = pg.Quantity,
QuoteId = pg.QuoteId,
QuoteOrderNo = qi.QuoteCode,
Remark = pg.Remark,
Status = pg.Status,
StorageNum = pg.StorageNum,
SysOrgId = pg.SysOrgId,
TypeId = pg.TypeId,
UpdateTime = pg.UpdateTime,
WareHouseId = pi.WareHouseId,
EmployeeId = pi.EmployeeId,
SupplierId = qi.SupplierId.Value,
}, whereExpression, PageIndex, PageSize, OrderByFileds: " pg.Status, pg.id desc");
var dicstatus = typeof(EnumWMPurchaseDetailStatus)._GetValueDescriptionDicFromEnumType();
foreach (var i in plst.data)
{
string name;
i.StatusName = dicstatus.TryGetValue((EnumWMPurchaseDetailStatus)i.Status, out name) ? name : string.Empty;
//仓库信息
if (i.WareHouseId > 0)
{
var WareHouseModel = await _wMWareHouseServices.QueryById(i.WareHouseId.Value);
if (WareHouseModel != null)
i.WareHouseName = WareHouseModel.Name;
}
//采购员
if (i.EmployeeId > 0)
{
var UserInfoModel = await _sysUserInfoServices.QueryById(i.EmployeeId);
if (UserInfoModel != null)
i.EmployeeName = UserInfoModel.uRealName;
}
//供应商
if (i.SupplierId > 0)
{
var SupplierModel = await _wMSupplierServices.QueryById(i.SupplierId);
if (SupplierModel != null)
i.SupplierName = SupplierModel.Name;
}
//主体
if (i.SysOrgId > 0)
{
var _SysOrgModel = await _BllSysOrg.QueryById(i.SysOrgId);
if (_SysOrgModel != null)
i.CompanyName = _SysOrgModel.CompanyName;
}
i.OutInType = 1;
i.InTime = DateTime.Now;
//没有入库数就默认入全部
if (i.StorageNum == 0 || i.StorageNum == null)
{
i.inputNum = (int)(i.CheckNum ?? 0m);
}
else
{
i.inputNum = (int)(i.CheckNum ?? 0m) - (int)(i.StorageNum ?? 0m);
if (i.inputNum < 0)
i.inputNum = 1;
}
}
return new MessageModel>()
{
msg = "获取成功",
success = true,
response = plst
};
}
[HttpGet]
public async Task> GetParams()
{
var dicDepartmentId = await _BllDepartment.Query(t => t.Status > 0);
var dicStatus = typeof(EnumWMPurchaseDetailStatus)._GetValueDescriptionDicFromEnumType().Select(t => new { Key = t.Key.GetHashCode(), t.Value });
var dicPurchaseUnit = await _BllWMProductUnit.Query();
var dicSysOrgId = await _BllSysOrg.Query();
var dicTypeId = typeof(EnumWMPurchaseType)._GetValueDescriptionDicFromEnumType().Select(t => new { Key = t.Key.GetHashCode(), t.Value });
var dicPayType = typeof(EnumPayType)._GetValueDescriptionDicFromEnumType().Select(t => new { Key = t.Key.GetHashCode(), t.Value });
var dicSupplierId = await _BllWMSupplier.Query();
var dicWMPurchaseStatus = typeof(EnumWMPurchaseStatus)._GetValueDescriptionDicFromEnumType().Select(t => new { Key = t.Key.GetHashCode(), t.Value });
return new MessageModel