using JiepeiWMS.IServices; using JiepeiWMS.Model.Models; using JiepeiWMS.Services.BASE; using JiepeiWMS.IRepository.Base; using System.Threading.Tasks; using JiepeiWMS.Common.HttpContextUser; using JiepeiWMS.Common.Enums; using JiepeiWMS.Common.Helper; using System; using JiepeiWMS.Model.ViewModels; using System.Linq; using System.Collections.Generic; using JiepeiWMS.Model; using System.Linq.Expressions; using System.Security.Claims; using JiepeiWMS.Extends; using SqlSugar; using JiepeiWMS.Common.Config; using JiepeiWMS.IRepository; using JiepeiWMS.Common; using JiepeiWMS.Common.HttpRestSharp; using JiepeiWMS.Model.Result; using System.Threading; using Microsoft.Extensions.Logging; using Newtonsoft.Json; namespace JiepeiWMS.Services { public class WMSaleInfoServices : BaseServices, IWMSaleInfoServices { private readonly IBaseRepository _dal; private readonly IWMSaleDetailRepository _wMSaleDetailRepository; private readonly IWMProductListRepository _wMProductListRepository; private readonly IWMWareHouseRepository _wMWareHouseRepository; private readonly IWMProductSpecRepository _wMProductSpecRepository; private readonly IWMProductUnitRepository _wMProductUnitRepository; private readonly IWMCheckInfoRepository _wMCheckInfo; private readonly IDepartmentRepository _departmentRepository; private readonly IBaseRepository _sysUserInfoRepository; private readonly IUser _user; private readonly IBaseRepository _userRoleRepository; private readonly IBaseRepository _roleRepository; private readonly ILogger _logger; public WMSaleInfoServices(IBaseRepository dal, IWMSaleDetailRepository WMSaleDetailRepository, IWMProductListRepository WMProductListRepository, IWMWareHouseRepository WMWareHouseRepository, IWMProductSpecRepository WMProductSpecRepository, IWMProductUnitRepository WMProductUnitRepository, IWMCheckInfoRepository WMCheckInfo, IDepartmentRepository DepartmentRepository, IBaseRepository SysUserInfoRepository, IUser user, IBaseRepository RoleRepository, IBaseRepository UserRoleRepository, ILogger logger) { this._dal = dal; base.BaseDal = dal; _wMSaleDetailRepository = WMSaleDetailRepository; _wMProductListRepository = WMProductListRepository; _wMWareHouseRepository = WMWareHouseRepository; _wMProductSpecRepository = WMProductSpecRepository; _wMProductUnitRepository = WMProductUnitRepository; _wMCheckInfo = WMCheckInfo; _departmentRepository = DepartmentRepository; _sysUserInfoRepository = SysUserInfoRepository; _user = user; _roleRepository = RoleRepository; _userRoleRepository = UserRoleRepository; _logger = logger; } private string materialoutUrl = "/uapws/rest/ic/manage/materialout/add"; /// /// 获取领料分页 /// /// public async Task> GetWMSaleInfoPage(int page, int intPageSize, string key = "", string options = "", string typeoptions = "", DateTime? startdate = null, DateTime? enddate = null, string statusoptions = "") { Expression> whereExpression = SaleInfo => SaleInfo.Status > 0; //获取角色信息,判断是否数据展示 var role = string.Join(",", _user.GetClaimValueByType(ClaimTypes.Role)); if (role.Contains("SuperAdmin") || role.Contains("超级管理员") || role.Contains("仓库管理员")) { } else { whereExpression = whereExpression.And(SaleInfo => SaleInfo.CheckAdminId == _user.ID || SaleInfo.EmployeeId == _user.ID); } //筛选主体Id var userInfoModel = await _sysUserInfoRepository.QueryById(_user.ID); if (userInfoModel != null) { whereExpression = whereExpression.And(SaleInfo => SaleInfo.SysOrgId == userInfoModel.SysOrgId); } if (!string.IsNullOrWhiteSpace(key)) { //筛选商品名称、商品编号 var ProductListList = (await _wMProductListRepository.Query(s => s.Name.Contains(key.Trim()) || s.Code.Contains(key.Trim()))).Select(c => c.Id); var SaleDetailList = (await _wMSaleDetailRepository.Query(x => ProductListList.Contains(x.ProductId))).Select(c => c.SaleId).Distinct(); whereExpression = whereExpression.And(SaleInfo => SaleInfo.SaleCode.Contains(key.Trim()) || SaleDetailList.Contains(SaleInfo.Id)); } if (!string.IsNullOrWhiteSpace(options)) { whereExpression = whereExpression.And(SaleInfo => SaleInfo.WareHouseId == options.ObjToInt()); } if (!string.IsNullOrWhiteSpace(typeoptions)) { whereExpression = whereExpression.And(SaleInfo => SaleInfo.PayType == typeoptions.ObjToInt()); } if (startdate != null) { whereExpression = whereExpression.And(SaleInfo => SaleInfo.CreateTime >= startdate); } if (enddate != null) { whereExpression = whereExpression.And(SaleInfo => SaleInfo.CreateTime <= enddate); } if (!string.IsNullOrWhiteSpace(statusoptions)) { whereExpression = whereExpression.And(SaleInfo => SaleInfo.Status == statusoptions.ObjToInt()); } var PageList = await base.QueryTabsPage( (SaleInfo, WareHouse, UserInfo, UserInfo2) => new object[] { JoinType.Left,SaleInfo.WareHouseId==WareHouse.Id, JoinType.Left,SaleInfo.EmployeeId==UserInfo.uID, JoinType.Left,SaleInfo.CheckAdminId==UserInfo2.uID }, (SaleInfo, WareHouse, UserInfo, UserInfo2) => new WMSaleInfo() { Id = SaleInfo.Id, Status = SaleInfo.Status, CheckKey = SaleInfo.CheckKey, EmployeeId = SaleInfo.EmployeeId, PayType = SaleInfo.PayType, Remark = SaleInfo.Remark, PayAmount = SaleInfo.PayAmount, CheckRank = SaleInfo.CheckRank, CreateTime = SaleInfo.CreateTime, SaleCode = SaleInfo.SaleCode, UpdateTime = SaleInfo.UpdateTime, WareHouseName = WareHouse.Name, EmployeeName = UserInfo.uRealName, CheckAdminName = UserInfo2.uRealName, DepartmentId = UserInfo.DepartmentId.Value }, whereExpression, page, intPageSize, " SaleInfo.Id desc" ); foreach (var item in PageList.data) { //领料状态 var dicStatus = typeof(EnumWMSaleStatus)._GetValueDescriptionDicFromEnumType(); string nameStatus; item.SaleStatus = dicStatus.TryGetValue((EnumWMSaleStatus)item.Status, out nameStatus) ? nameStatus : string.Empty; //领料类型 var dicType = typeof(EnumWMSaleType)._GetValueDescriptionDicFromEnumType(); string nameType; item.PayTypeName = dicType.TryGetValue((EnumWMSaleType)item.PayType, out nameType) ? nameType : string.Empty; //部门信息 if (item.DepartmentId > 0) { var DepartmentModel = await _departmentRepository.QueryById(item.DepartmentId.Value); if (DepartmentModel != null) item.DepartmentName = DepartmentModel.Name; } //商品信息 var SaleDetailModel = await _wMSaleDetailRepository.GetModel(x => x.SaleId == item.Id); if (SaleDetailModel != null) { var ProductListModel = await _wMProductListRepository.QueryById(SaleDetailModel.ProductId); if (ProductListModel != null) { item.ProductName = ProductListModel.Name; item.ProductCode = ProductListModel.Code; } } var userrolemodel = await _userRoleRepository.GetModel(r => r.UserId == _user.ID); if (userrolemodel != null) { var rolemodel = await _roleRepository.QueryById(userrolemodel.RoleId); if (rolemodel != null && rolemodel.Name == RoleConfig.SaleCangKuStaff) { item.MayOut = true;//显示出库按钮 } } } return PageList; } /// /// 获取领料或出库详情 /// /// /// public async Task GetWMSaleInfo(int Id) { var model = await base.GetModel(w => w.Id == Id); model.SaleDetailList = await _wMSaleDetailRepository.Query(w => w.SaleId == Id); //仓库名称 var WMWareHouseModel = await _wMWareHouseRepository.GetModel(w => w.Id == model.WareHouseId); if (WMWareHouseModel != null) model.WareHouseName = WMWareHouseModel.Name; //用户模型 var UserModel = await _sysUserInfoRepository.GetModel(u => u.uID == model.EmployeeId); if (UserModel != null) { model.DepartmentId = UserModel.DepartmentId.Value; model.EmployeeName = UserModel.uRealName; //部门名称 var DepartmentModel = await _departmentRepository.GetModel(d => d.Id == UserModel.DepartmentId); if (DepartmentModel != null) model.DepartmentName = DepartmentModel.Name; } //var UserModel1 = await _sysUserInfoRepository.GetModel(u => u.uID == model.CheckAdminId); //if (UserModel1 != null) // model.CheckAdminName = UserModel1.uRealName; var roleMdoel = await _roleRepository.GetModel(x => x.Name == RoleConfig.SaleCangKuStaff); if (roleMdoel != null && model.Status >= EnumWMSaleStatus.OutWareHouse.GetHashCode()) { var userRoleMdoel = await _userRoleRepository.GetModel(x => x.RoleId == roleMdoel.Id); if (userRoleMdoel != null) { var UserModel2 = await _sysUserInfoRepository.GetModel(u => u.uID == userRoleMdoel.UserId); if (UserModel2 != null) model.IssueName = UserModel2.uRealName; } } //领料状态 var dicSaleStatus = typeof(EnumWMSaleStatus)._GetValueDescriptionDicFromEnumType(); string nameSaleStatus; model.SaleStatus = dicSaleStatus.TryGetValue((EnumWMSaleStatus)model.Status, out nameSaleStatus) ? nameSaleStatus : string.Empty; //领料类型 var dicType = typeof(EnumWMSaleType)._GetValueDescriptionDicFromEnumType(); string nameType; model.PayTypeName = dicType.TryGetValue((EnumWMSaleType)model.PayType, out nameType) ? nameType : string.Empty; //获取审批流 var checkModel = await _wMCheckInfo.GetModel(w => w.SysOrgId == _user.SysOrgId && w.Key == model.CheckKey); if (checkModel != null) { var rankList = JsonHelper.DeserializeObject(checkModel.Json); var checkarr = rankList.checkRankList.Select(w => w.AdminName).ToArray(); for (int i = 0; i < checkarr.Length; i++) { model.CheckAdminName = checkarr[i] + " "; } model.CheckStrStep = checkarr.ToList(); //model.CheckRankStep = model.CheckRank == 0 ? (checkarr.Count() - 1) : (model.CheckRank - 2); model.CheckRankStep = model.CheckRank == 0 ? checkarr.Count() : (model.CheckRank - 1); } else { model.CheckStrStep = new List(); model.CheckRankStep = 0; } model.AdminId = _user.ID; foreach (var item in model.SaleDetailList) { //商品信息 var ProductListModel = await _wMProductListRepository.GetModel(w => w.Id == item.ProductId); if (ProductListModel != null) { item.ProductCode = ProductListModel.Code; item.ProductName = ProductListModel.Name; if (ProductListModel.SpecIds > 0) { //商品规格 var ProductSpecModel = await _wMProductSpecRepository.QueryById(ProductListModel.SpecIds); if (ProductSpecModel != null) { item.ProductSpec = ProductSpecModel.Name; } } if (ProductListModel.Unit > 0) { //商品单位 var ProductUnitModel = await _wMProductUnitRepository.QueryById(ProductListModel.Unit); if (ProductUnitModel != null) { item.ProductUnit = ProductUnitModel.Unit; } } } item.CheckNum = item.Quantity; item.GiveNum = item.Quantity; if (item.Checked <= 0) { item.Checked = 1; } //商品状态 var dicProductStatus = typeof(EnumWMSaleStatus)._GetValueDescriptionDicFromEnumType(); string nameProductStatus; item.ProductStatus = dicProductStatus.TryGetValue((EnumWMSaleStatus)item.Status, out nameProductStatus) ? nameProductStatus : string.Empty; } return model; } /// /// 获取领料最大的Id /// /// public async Task GetSaleInfoMaxId() { var db = await base.QueryTable("SELECT ISNULL(MAX(Id),0) FROM dbo.WMSaleInfo"); return db.Rows[0][0].ObjToInt(); } /// /// 调用友物料出库接口 /// /// public async Task> BeginMaterialOutInNC(WMSaleInfo infoModel, List detailList) { try { #region NCMaterialOutViewModels pos = new NCMaterialOutViewModels(); NCMaterialOutViewModels.Ufinterface uface = new NCMaterialOutViewModels.Ufinterface(); NCMaterialOutViewModels.Bill bill = new NCMaterialOutViewModels.Bill(); NCMaterialOutViewModels.Billhead head = new NCMaterialOutViewModels.Billhead(); NCMaterialOutViewModels.Cgeneralbid cgeneralbid = new NCMaterialOutViewModels.Cgeneralbid(); List item = new List(); var UnitCode = ""; var SupplierModel = new WMSupplier(); var ProductListModel = new WMProductList(); //赋uface uface.account = "01"; uface.billtype = "4D";//物料出库 uface.isexchange = "Y"; uface.replace = "Y"; uface.sender = "JPKJ001"; uface.groupcode = "JPKJ"; //赋head head.pk_group = "JPKJ"; head.pk_org = "1001"; head.corpoid = "1001"; head.corpvid = "1001"; head.pk_org = "1001"; head.pk_org_v = "1001"; head.cwarehouseid = "01"; head.vtrantypecode = "4D-01"; head.cdrawcalbodyoid = "1001"; head.cdrawcalbodyvid = "1001"; head.fbillflag = "2"; //赋item foreach (var detailItem in detailList) { if (detailItem.ProductId > 0) { ProductListModel = await _wMProductListRepository.QueryById(detailItem.ProductId); if (ProductListModel != null) { var qt = await QueryTable("select UnitCode from WMProductUnit where id=" + ProductListModel.Unit); UnitCode = qt.Rows._Select(t => t["UnitCode"]._ToDbStr()).FirstOrDefault(); } } var ItemModel = new NCMaterialOutViewModels.Item(); ItemModel.cmaterialoid = ProductListModel.Code; ItemModel.cmaterialvid = ProductListModel.Code; ItemModel.cunitid = UnitCode; ItemModel.castunitid = UnitCode; ItemModel.vchangerate = "1/1";//换算率 ItemModel.nshouldnum = detailItem.GiveNum.ToString(); ItemModel.nshouldassistnum = detailItem.GiveNum.ToString(); ItemModel.nnum = detailItem.GiveNum.ToString(); ItemModel.nassistnum = detailItem.GiveNum.ToString(); item.Add(ItemModel); } //所有集合赋值 cgeneralbid.item = item; head.cgeneralbid = cgeneralbid; bill.billhead = head; uface.bill = bill; pos.ufinterface = uface; var json = JsonConvert.SerializeObject(pos); //物料出库接口 var apinchost = Appsettings.app("AppSettings", "ApiNCHost"); NCJsonResult jsonResult = HttpHelper.PostApi(apinchost + materialoutUrl, pos); Thread.Sleep(300); #endregion var resultdescription = jsonResult.data.ufinterface.sendresult[0].resultdescription; if (jsonResult.data.ufinterface.successful == "Y") { return new Tuple("用友物料出库接口成功", true); } else { string[] strArray = resultdescription.Split("异常信息:"); return new Tuple("用友物料出库接口失败:"+strArray[1], false); } } catch (Exception ex) { _logger.LogError(ex, ex.ToString()); return new Tuple(ex.ToString(), false); } } } }