using JiepeiWMS.IServices; using JiepeiWMS.Model.Models; using JiepeiWMS.Services.BASE; using JiepeiWMS.IRepository.Base; using System.Threading.Tasks; using JiepeiWMS.IRepository.UnitOfWork; using JiepeiWMS.Common; using JiepeiWMS.Common.HttpRestSharp; using JiepeiWMS.Model.BllModels; using System; using JiepeiWMS.Extends; using System.Linq; namespace JiepeiWMS.Services { public class WMSupplierServices : BaseServices, IWMSupplierServices { private readonly IBaseRepository _dal; private readonly IBaseRepository _daltype; private readonly IUnitOfWork _DB; public WMSupplierServices(IBaseRepository dal, IBaseRepository daltype, IUnitOfWork DB) { this._dal = dal; this._daltype = daltype; base.BaseDal = dal; _DB = DB; } /// /// 供应商添加 /// /// /// /// public async Task AddSupplierInfo(WMSupplier Model, int UserId) { _DB.BeginTran(); try { if (Model == null) { throw new Exception("模型为空,无法添加数据!"); } //同步到OA系统 var apioahost = Appsettings.app("AppSettings", "ApiOAHost"); var result = HttpHelper.PostApi(apioahost + "/api/Supplier/SaveOrUpdateSupplier", new { appsecret = "kw3j5k32ur38rnerkKJHk83", timestamp = DateTime.Now._ToTimestamp(), SupName = Model.Name, SupShortName = Model.ShortName, OrgCode = Model.UnifiedCode, SupCharge = Model.LegalRepresentative, SupPhone = Model.Phone, SupEmail = Model.PostalCode, SupAddress = Model.Address, SupTypeId = Model.ArrayType }); if (result == null || result.Code != 1) { _DB.RollbackTran(); return result == null || string.IsNullOrEmpty(result.Message) ? "请求OA供应商接口失败" : result.Message; } //返回值OA的供应商id Model.OASupplierId = Convert.ToInt32(result.Data.OaSupId); Model.UnifiedCode = result.Data.OaSupCode; var Id = await _dal.Add(Model); //循环往供应商类型中间表插入数据 for (int i = 0, c = Model.ArrayType.Length; i < c; i++) { if (Id > 0) { var WMSupplierTypeId = Model.ArrayType[i]; WMSupplier_Type wt = new WMSupplier_Type(); wt.WMSupplierId = Id; wt.WMSupplierTypeId = WMSupplierTypeId; wt.IsDeleted = false; var TypeId = await _daltype.Add(wt); } } _DB.CommitTran(); } catch (Exception ex) { _DB.RollbackTran(); return ex.ToString(); } return string.Empty; } /// /// 更新供应商OASupplierId /// /// 模型 /// 操作人id /// public async Task EditSupplierInfo() { try { var SupplierIdList = (await _daltype.Query()).Select(x => x.WMSupplierId).Distinct(); var Listmodel = await BaseDal.Query(w => SupplierIdList.Contains(w.Id)); //var Listmodel = await BaseDal.Query(w => w.OASupplierId == 0); foreach (var item in Listmodel) { //将分类id转为数组 var supid = (await _daltype.Query(d => d.WMSupplierId == item.Id)).Select(d => d.Id.ToString()).ToArray(); //同步到OA系统 var apioahost = Appsettings.app("AppSettings", "ApiOAHost"); var result = HttpHelper.PostApi(apioahost + "/api/Supplier/SaveOrUpdateSupplier", new { appsecret = "kw3j5k32ur38rnerkKJHk83", timestamp = DateTime.Now._ToTimestamp(), OaSupId = item.OASupplierId, SupName = item.Name, SupShortName = item.ShortName, OrgCode = item.UnifiedCode, SupCharge = item.LegalRepresentative, SupPhone = item.Phone, SupEmail = item.PostalCode, SupAddress = item.Address, SupTypeId = supid, }); if (result == null || result.Code != 1) { throw new Exception("请求OA供应商接口失败,result值:" + result + ";"); } //返回值OA的参数并赋值 item.OASupplierId = Convert.ToInt32(result.Data.OaSupId); item.UnifiedCode = !string.IsNullOrWhiteSpace(result.Data.OaSupCode) ? result.Data.OaSupCode : item.UnifiedCode; item.Name = !string.IsNullOrWhiteSpace(result.Data.OaSupName) ? result.Data.OaSupName : item.Name; var OAId = BaseDal.ExecuteSqlCommand(@"update dbo.WMSupplier set OASupplierId=" + item.OASupplierId + ",UnifiedCode='" + item.UnifiedCode + "',Name='" + item.Name + "' where Id =" + item.Id + ""); } } catch (Exception ex) { _DB.RollbackTran(); return ex.ToString(); } return string.Empty; } } }