using JiepeiWMS.IServices; using JiepeiWMS.Model.Models; using JiepeiWMS.Services.BASE; using JiepeiWMS.IRepository.Base; using JiepeiWMS.Model; using System.Threading.Tasks; using System.Linq.Expressions; using System; using JiepeiWMS.Extends; using SqlSugar; using JiepeiWMS.Common.HttpContextUser; using System.Linq; using JiepeiWMS.IRepository; using System.Security.Claims; using JiepeiWMS.Model.ViewModels; using System.Collections.Generic; using JiepeiWMS.Model.Result; using JiepeiWMS.Common.HttpRestSharp; using JiepeiWMS.Common; using System.Threading; using Microsoft.Extensions.Logging; using Newtonsoft.Json; using System.Net; using System.IO; using System.Text; using Newtonsoft.Json.Linq; using JiepeiWMS.Common.Helper; namespace JiepeiWMS.Services { public class WMProductListServices : BaseServices, IWMProductListServices { private readonly IBaseRepository _dal; private readonly IWMProductInfoRepository _wMProductInfoRepository; private readonly IWMWareHouseRepository _wMWareHouseRepository; private readonly IUser _user; private readonly IBaseRepository _sysUserInfo; private readonly IBaseRepository _userRoleServices; private readonly IBaseRepository _wMProductClassRepository; private readonly IBaseRepository _wMProductUnitRepository; private readonly IBaseRepository _wMProductSpecRepository; private readonly ILogger _logger; public WMProductListServices(IBaseRepository dal, IWMProductInfoRepository WMProductInfoRepository, IWMWareHouseRepository WMWareHouseRepository, IUser user, IBaseRepository sysUserInfoRepository, IBaseRepository UserRoleRepository, IBaseRepository WMProductClassRepository, IBaseRepository WMProductUnitRepository, IBaseRepository WMProductSpecRepository, ILogger logger) { this._dal = dal; base.BaseDal = dal; _wMProductInfoRepository = WMProductInfoRepository; _wMWareHouseRepository = WMWareHouseRepository; _user = user; _sysUserInfo = sysUserInfoRepository; _userRoleServices = UserRoleRepository; _wMProductClassRepository = WMProductClassRepository; _wMProductUnitRepository = WMProductUnitRepository; _wMProductSpecRepository = WMProductSpecRepository; _logger = logger; } private string materialUrl = "/uapws/rest/uapbd/manage/material/add"; /// /// 获取商品分页 /// /// public async Task> GetWMProductListPage(int page, int intPageSize, string key = "", string options = "", DateTime? startdate = null, DateTime? enddate = null, int wareHouseId = 0) { Expression> whereExpression = (ProductList, ProductClass, ProductSpec, ProductUnit, ProductInfo) => ProductList.Id > 0; //筛选主体Id //var userInfoModel = await _sysUserInfoRepository .QueryById(_user.ID); //if (userInfoModel != null) //{ // whereExpression = whereExpression.And(ProductList => ProductList.SysOrgId == userInfoModel.SysOrgId); //} //获取角色信息,判断是否数据展示 var role = string.Join(",", _user.GetClaimValueByType(ClaimTypes.Role)); if (role.Contains("仓库管理员")) { var sysUserInfoModel = await _sysUserInfo.QueryById(_user.ID); whereExpression = whereExpression.And((ProductList, ProductClass, ProductSpec, ProductUnit, ProductInfo) => ProductInfo.SysOrgId == sysUserInfoModel.SysOrgId); } if (!string.IsNullOrWhiteSpace(key)) { whereExpression = whereExpression.And((ProductList, ProductClass, ProductSpec, ProductUnit, ProductInfo) => ProductList.Name.Contains(key.Trim()) || ProductList.Code.Contains(key.Trim())); } if (!string.IsNullOrWhiteSpace(options)) { whereExpression = whereExpression.And((ProductList, ProductClass, ProductSpec, ProductUnit, ProductInfo) => ProductList.ClassId == options.ObjToInt()); } if (startdate != null) { whereExpression = whereExpression.And((ProductList, ProductClass, ProductSpec, ProductUnit, ProductInfo) => ProductList.CreateTime >= startdate); } if (enddate != null) { whereExpression = whereExpression.And((ProductList, ProductClass, ProductSpec, ProductUnit, ProductInfo) => ProductList.CreateTime <= enddate); } if (wareHouseId > 0) { whereExpression = whereExpression.And((ProductList, ProductClass, ProductSpec, ProductUnit, ProductInfo) => ProductInfo.WareHouseId == wareHouseId); } //获取角色信息,判断是否数据展示 if (role.Contains("超级管理员")) { } else { whereExpression = whereExpression.And((ProductList, ProductClass, ProductSpec, ProductUnit, ProductInfo) => ProductList.IsUse == true); } var PageList = await base.QueryPage( (ProductList, ProductClass, ProductSpec, ProductUnit, ProductInfo) => new object[] { JoinType.Left,ProductList.ClassId==ProductClass.Id, JoinType.Left,ProductList.SpecIds==ProductSpec.Id, JoinType.Left,ProductList.Unit==ProductUnit.Id, JoinType.Left,ProductList.Id==ProductInfo.ProductListId, }, (ProductList, ProductClass, ProductSpec, ProductUnit, ProductInfo) => new WMProductList() { Id = ProductList.Id, ClassId = ProductList.ClassId, Name = ProductList.Name, Code = ProductList.Code, BarCode = ProductList.BarCode, Unit = ProductList.Unit, Sort = ProductList.Sort, IsUse = ProductList.IsUse, IsChecked = ProductList.IsChecked, AdminId = ProductList.AdminId, CreateTime = ProductList.CreateTime, SpecIds = ProductList.SpecIds, Price = ProductList.Price, PurchasePrice = ProductList.PurchasePrice, OddDays = ProductList.OddDays, OddFloatNum = ProductList.OddFloatNum, StoreBaseline = ProductList.StoreBaseline, StoreLimit = ProductList.StoreLimit, SysOrgId = ProductInfo.SysOrgId, ClassName = ProductClass.ClassName, SpecName = ProductSpec.Name, UnitName = ProductUnit.Unit, WareHouseId = ProductInfo.WareHouseId ?? 0 }, whereExpression, page, intPageSize, " ProductList.Id desc" ); foreach (var item in PageList.data) { //if (productInfoList != null && productInfoList.Count > 1) //{ // //当前仓库的库存 // if (wareHouseId > 0) // { // productInfoModel = productInfoList.Where(x => x.WareHouseId == wareHouseId).FirstOrDefault(); // } //} //else //{ // //其它仓库的库存 // productInfoModel = productInfoList.FirstOrDefault(); //} var productInfoModel = await _wMProductInfoRepository.GetModel(x => x.ProductListId == item.Id && x.WareHouseId == item.WareHouseId); if (productInfoModel != null && productInfoModel.WareHouseId != null) { var wareHouseModel1 = await _wMWareHouseRepository.QueryById(productInfoModel.WareHouseId.Value); if (wareHouseModel1 != null) { item.WareHouseName = wareHouseModel1.Name; } item.WareHouseId = productInfoModel.WareHouseId.Value; item.Quantity = productInfoModel.Quantity; item.ProductInfoId = productInfoModel.Id; if (!string.IsNullOrWhiteSpace(item.SpecName)) item.SpecName = item.SpecName.TrimEnd(','); var sysUserInfoModel = await _sysUserInfo.QueryById(item.AdminId); if (sysUserInfoModel != null) item.AdminName = sysUserInfoModel.uRealName; } } return PageList; } /// /// 调用友新增物料接口 /// /// public async Task> BeginMaterialInNC(WMProductList listModel) { try { #region NCMaterialViewModels pos = new NCMaterialViewModels(); NCMaterialViewModels.Ufinterface uface = new NCMaterialViewModels.Ufinterface(); NCMaterialViewModels.Bill bill = new NCMaterialViewModels.Bill(); NCMaterialViewModels.Billhead head = new NCMaterialViewModels.Billhead(); NCMaterialViewModels.Materialconvert materialconvert = new NCMaterialViewModels.Materialconvert(); NCMaterialViewModels.Item item = new NCMaterialViewModels.Item(); if (listModel == null) { return new Tuple("用友新增物料接口失败", false, ""); } var ClassCode = ""; var UnitCode = ""; var SpecName = ""; var productClassModel = await _wMProductClassRepository.QueryById(listModel.ClassId); if (productClassModel != null) ClassCode = productClassModel.ClassCode; var productUnitModel = await _wMProductUnitRepository.QueryById(listModel.Unit); if (productUnitModel != null) UnitCode = productUnitModel.UnitCode; var productSpecModel = await _wMProductSpecRepository.QueryById(listModel.SpecIds); if (productSpecModel != null) SpecName = productSpecModel.Name; //赋uface uface.account = "JPKJ"; uface.billtype = "material";//新增物料 uface.isexchange = "Y"; uface.replace = "Y"; uface.sender = "JPKJ002"; uface.groupcode = "JPKJ"; var maxId = (await _dal.Query()).Max(x => x.Id); //赋bill bill.id = maxId + 1; //赋head head.pk_group = "JPKJ"; head.pk_org = "JPKJ"; head.code = !string.IsNullOrWhiteSpace(listModel.Code) ? listModel.Code : "material_temp_code"; head.name = listModel.Name; head.materialspec = SpecName; head.materialtype = "temp002"; head.pk_measdoc = UnitCode; head.pk_marbasclass = ClassCode; head.pk_prodline = ""; head.intolerance = "0";//入库容差(%) head.outtolerance = "0";//出库容差(%) head.outcloselowerlimit = "0";//出库关闭下容差(%) head.productfamily = "N"; head.pk_mattaxes = "CN1801"; head.enablestate = (listModel.IsUse.ObjToInt()).ObjToString(); //赋item item.pk_measdoc = UnitCode; item.measrate = "1/1"; //所有集合赋值 materialconvert.item = item; head.materialconvert = materialconvert; bill.billhead = head; uface.bill = bill; pos.ufinterface = uface; var apioahost = Appsettings.app("AppSettings", "ApiNCHost"); var serviceAddress = apioahost + materialUrl; var strContent = JsonConvert.SerializeObject(pos); #endregion string jsonresult = ""; var data = GetNetData.PostFunction(serviceAddress, strContent, ref jsonresult); var result = JsonConvert.DeserializeObject(data); if (result.data != null && result.data.ufinterface.successful == "Y") { return new Tuple("用友新增物料接口成功", true, result.data.ufinterface.sendresult[0].code); } else { return new Tuple(result.message, false, ""); } } catch (Exception ex) { _logger.LogError(ex, ex.ToString()); return new Tuple(ex.ToString(), false, ""); } } } }