圣珀
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

591 lines
30 KiB

using ICS.WCF.Base;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
using UFSoft.UBF.Service;
using UFSoft.UBF.Util.Context;
using www.ufida.org.EntityData;
namespace ICS.ModifyU9Customer
{
public class ModifyCustomer
{
private string MessageLogName = "修改U9客户信息";
private string ERPConStr = Appconfig.GetU9ConnStr();
public ModifyCustomerResult CustModifyU9Customer(ModifyCustomerModel ModifyCustomer)
{
ModifyCustomerResult Return = new ModifyCustomerResult();//方法返回值
Return.IsSuccess = false;
Return.MESSAGE = "";
try
{
if (ModifyCustomer != null)
{
StringBuilder strHead = new StringBuilder();
strHead.AppendLine("-------------接口传入参数:");
if (string.IsNullOrEmpty(ModifyCustomer.Code))
{
ModifyCustomer.Code = "";
}
if (string.IsNullOrEmpty(ModifyCustomer.Name))
{
ModifyCustomer.Name = "";
}
if (string.IsNullOrEmpty(ModifyCustomer.ORGCode))
{
ModifyCustomer.ORGCode = "";
}
if (string.IsNullOrEmpty(ModifyCustomer.PrivateDescSeg2))
{
ModifyCustomer.PrivateDescSeg2 = "";
}
if (string.IsNullOrEmpty(ModifyCustomer.Department))
{
ModifyCustomer.Department = "";
}
if (string.IsNullOrEmpty(ModifyCustomer.Saleser))
{
ModifyCustomer.Saleser = "";
}
strHead.AppendLine("CustomerCode:" + ModifyCustomer.Code);
strHead.AppendLine("Name:" + ModifyCustomer.Name);
strHead.AppendLine("ORGCode:" + ModifyCustomer.ORGCode);
strHead.AppendLine("PrivateDescSeg1:" + ModifyCustomer.PrivateDescSeg2);
strHead.AppendLine("Department:" + ModifyCustomer.Department);
strHead.AppendLine("Saleser:" + ModifyCustomer.Saleser);
//strHead.AppendLine("IsEffective:" + ModifyCustomer.IsEffective );
strHead.AppendLine("***************接口传入参数End");
Appconfig.WriteLogFile(strHead.ToString(), MessageLogName);
Return.CustomerCode = ModifyCustomer.Code;
string QueryMessage = "";//修改前的错误信息
if (string.IsNullOrEmpty(ModifyCustomer.ORGCode))
{
ModifyCustomer.ORGCode = ConfigurationManager.AppSettings["orgCode"].ToString();
}
#region check
if (string.IsNullOrEmpty(ModifyCustomer.Code))
{
QueryMessage += "客户编码为空";
}
#endregion
if (string.IsNullOrEmpty(QueryMessage))
{
//上下文信息
object context;
context = CreateContextObj();
UFIDAU9CBOPubControllerContextDTOData contextDTO = new UFIDAU9CBOPubControllerContextDTOData();
contextDTO = CreateContextDto();
#region 修改服务调用框架结构
//修改SV
UFIDAU9ISVCustomerIBatchModifyCustomerByDTOSVClient client = new UFIDAU9ISVCustomerIBatchModifyCustomerByDTOSVClient();
//服务返回结果
UFIDAU9CBOSCMCustomerCustomerData[] returnCustomers;
UFSoft.UBF.Exceptions1.MessageBase[] returnMsg;//修改返回的报错
#endregion
#region 查询服务调用框架结构
//查询SV
UFIDAU9ISVCustomerIBatchQueryCustomerByDTOSRVClient Qclient = new global::UFIDAU9ISVCustomerIBatchQueryCustomerByDTOSRVClient();
//服务返回结果
UFIDAU9ISVCustomerCustomerDTOData[] QreturnCustomers;//查询返回的客户类
UFSoft.UBF.Exceptions1.MessageBase[] QreturnMsg;//查询返回的报错
#endregion
//先查询
List<UFIDAU9ISVCustomerQueryCustomerDTOData> QDTOS = new List<UFIDAU9ISVCustomerQueryCustomerDTOData>();
UFIDAU9ISVCustomerQueryCustomerDTOData CC = new UFIDAU9ISVCustomerQueryCustomerDTOData();
CC.m_org = new UFIDAU9CBOPubControllerCommonArchiveDataDTOData();
CC.m_org.m_code = ModifyCustomer.ORGCode;
CC.m_customer = new UFIDAU9CBOPubControllerCommonArchiveDataDTOData();
CC.m_customer.m_code = ModifyCustomer.Code;
QDTOS.Add(CC);
QreturnCustomers = Qclient.Do(out QreturnMsg, context, QDTOS.ToArray(), contextDTO);
if (QreturnCustomers == null || QreturnCustomers.Count() == 0)
{
throw new Exception("组织:" + ModifyCustomer.ORGCode + ",未找到客户编码:" + ModifyCustomer.Code);
}
//后修改
if (QreturnMsg != null && QreturnMsg.Length > 0)
{
foreach (UFSoft.UBF.Exceptions1.MessageBase MESS in QreturnMsg)
{
QueryMessage += MESS.innerMessages + ";";
}
}
if (string.IsNullOrEmpty(QueryMessage))
{
List<UFIDAU9ISVCustomerCustomerDTOData> OLDList = QreturnCustomers.ToList();
UFIDAU9ISVCustomerCustomerDTOData OldCus = OLDList.Find(a => a.m_code == ModifyCustomer.Code && a.m_org.m_code == ModifyCustomer.ORGCode);
if (OldCus != null)
{
Return.CustomerID = OldCus.m_iD;
Return.CustomerCode = OldCus.m_code;
UFIDAU9ISVCustomerCustomerDTOData cus = OldCus;
cus.Name = ModifyCustomer.Name;
cus.m_shortName = ModifyCustomer.Name;
if (cus.m_descFlexField != null)
{
cus.m_descFlexField.m_privateDescSeg2 = ModifyCustomer.PrivateDescSeg2;
}
else
{
UFIDAU9BaseFlexFieldDescFlexFieldDescFlexSegmentsData deflex = new UFIDAU9BaseFlexFieldDescFlexFieldDescFlexSegmentsData();
deflex.m_privateDescSeg2 = ModifyCustomer.PrivateDescSeg2;
cus.m_descFlexField = deflex;
}
if (cus.m_descFlexField != null)
{
cus.m_descFlexField.m_privateDescSeg7 = ModifyCustomer.PrivateDescSeg7;
}
else
{
UFIDAU9BaseFlexFieldDescFlexFieldDescFlexSegmentsData deflex = new UFIDAU9BaseFlexFieldDescFlexFieldDescFlexSegmentsData();
deflex.m_privateDescSeg7 = ModifyCustomer.PrivateDescSeg7;
cus.m_descFlexField = deflex;
}
if (cus.m_descFlexField != null)
{
cus.m_descFlexField.m_privateDescSeg8 = ModifyCustomer.PrivateDescSeg8;
}
else
{
UFIDAU9BaseFlexFieldDescFlexFieldDescFlexSegmentsData deflex = new UFIDAU9BaseFlexFieldDescFlexFieldDescFlexSegmentsData();
deflex.m_privateDescSeg8 = ModifyCustomer.PrivateDescSeg8;
cus.m_descFlexField = deflex;
}
if (cus.m_descFlexField != null)
{
cus.m_descFlexField.m_privateDescSeg11 = ModifyCustomer.PrivateDescSeg11;
}
else
{
UFIDAU9BaseFlexFieldDescFlexFieldDescFlexSegmentsData deflex = new UFIDAU9BaseFlexFieldDescFlexFieldDescFlexSegmentsData();
deflex.m_privateDescSeg11 = ModifyCustomer.PrivateDescSeg11;
cus.m_descFlexField = deflex;
}
if (!string.IsNullOrEmpty(ModifyCustomer.Department))
{
string DepID = GetDepartmentID(ModifyCustomer.Department, ModifyCustomer.ORGCode);
if (string.IsNullOrEmpty(DepID))
{
throw new Exception("部门编码:" + ModifyCustomer.Department + "在组织:" + ModifyCustomer.ORGCode + "中不存在");
}
else
{
UFIDAU9CBOPubControllerCommonArchiveDataDTOData dep = new UFIDAU9CBOPubControllerCommonArchiveDataDTOData();
dep.m_code = ModifyCustomer.Department;
dep.m_iD = Convert.ToInt64(DepID);
cus.m_department = dep;
}
}
if (!string.IsNullOrEmpty(ModifyCustomer.Saleser))
{
string SalerID = GetOperatorsID(ModifyCustomer.Saleser, ModifyCustomer.ORGCode);
if (string.IsNullOrEmpty(SalerID))
{
throw new Exception("业务员编码:" + ModifyCustomer.Department + "在组织:" + ModifyCustomer.ORGCode + "中不存在");
}
else
{
UFIDAU9CBOPubControllerCommonArchiveDataDTOData saleser = new UFIDAU9CBOPubControllerCommonArchiveDataDTOData();
saleser.m_code = ModifyCustomer.Saleser;
saleser.m_iD = Convert.ToInt64(SalerID);
cus.m_saleser = saleser;
}
}
if (cus.m_effective != null)
{
//cus.m_effective.m_isEffective = ModifyCustomer.IsEffective;
cus.m_effective.m_isEffective = true;
}
else
{
UFIDAU9BasePropertyTypesEffectiveData ee = new UFIDAU9BasePropertyTypesEffectiveData();
//cus.m_effective.m_isEffective = ModifyCustomer.IsEffective;
cus.m_effective.m_isEffective = true;
ee.m_effectiveDate = DateTime.Now;
cus.m_effective = ee;
}
if (cus.m_effective.m_isEffective)
{
cus.m_effective.m_disableDate = Convert.ToDateTime("9999.12.31");
}
else
{
cus.m_effective.m_disableDate = DateTime.Now;
}
#region 获取立账条件名称
string ARConfirmTermName = "";
string ARConfirmTermID = "";
string ctermrsql = @"select a.ID,a.Code,b.Name from CBO_ARConfirmTerm a
left join CBO_ARConfirmTerm_Trl b on a.id=b.id
left join Base_Organization j on a.Org=j.ID
where a.Code='{0}' AND j.Code='{1}'";
ctermrsql = string.Format(ctermrsql, ModifyCustomer.ARConfirmTerm, ModifyCustomer.ORGCode);
DataTable ctermdt2 = DBhlper.Query(ctermrsql, Appconfig.GetU9ConnStr());
if (ctermdt2.Rows.Count != 0)
{
ARConfirmTermName = ctermdt2.Rows[0]["Name"].ToString();
ARConfirmTermID = ctermdt2.Rows[0]["ID"].ToString();
}
else
{
throw new Exception("未获取到立账条件名称:" + ModifyCustomer.ARConfirmTerm + "对应ID");
}
#endregion
cus.m_aRConfirmTerm = new UFIDAU9CBOPubControllerCommonArchiveDataDTOData();//立账条件
cus.m_aRConfirmTerm.m_iD = long.Parse(ARConfirmTermID);
cus.m_aRConfirmTerm.m_code = ModifyCustomer.ARConfirmTerm;
cus.m_descFlexField.m_privateDescSeg1 = "0";//欠回单数
cus.m_descFlexField.m_privateDescSeg3 = "02";//是否逾期
cus.m_descFlexField.m_privateDescSeg4 = "01";//是否拆分
if (ARConfirmTermName == "月结30天")
{
cus.m_descFlexField.m_privateDescSeg6 = "30";//信用期限
}
else if (ARConfirmTermName == "月结60天")
{
cus.m_descFlexField.m_privateDescSeg6 = "60";
}
else if (ARConfirmTermName == "月结90天")
{
cus.m_descFlexField.m_privateDescSeg6 = "90";
}
else if (ARConfirmTermName == "月结360天")
{
cus.m_descFlexField.m_privateDescSeg6 = "360";
}
else
{
cus.m_descFlexField.m_privateDescSeg6 = "0";
}
cus.m_isCreditCheck = false;//信用检查
#region 客户位置
//UFIDAU9ISVCustomerCustomerSiteDTOData custSiteInfo = new UFIDAU9ISVCustomerCustomerSiteDTOData();
//List<UFIDAU9ISVCustomerCustomerSiteDTOData> custSiteList = new List<UFIDAU9ISVCustomerCustomerSiteDTOData>();
//UFIDAU9ISVCustomerCustomerSiteDTOData[] custSites;
//custSiteInfo.m_code = ModifyCustomer.SiteCode;
//custSiteInfo.Name = ModifyCustomer.SiteName;
//custSiteInfo.m_isDefaultBillTo = true;//默认开票位置
//custSiteInfo.m_customerState = 1;//目标客户
//custSiteInfo.m_isPriceListModify = true; // 价表可改
//custSiteInfo.m_isShippmentRuleEditable = true; // 出货原则可改
//custSiteInfo.m_isPaymentTermEditable = true; // 收款条件可改
//custSiteInfo.m_isSalesDiscount = true;
//custSiteInfo.m_shipmentMode = 1;
//custSiteInfo.m_isPostBankCheckList = true;
//custSiteInfo.m_bankCheckPeriod = 3;
//custSiteInfo.m_isPrePayment = false;
//custSiteInfo.m_holdDate = DateTime.Now;
//custSiteInfo.m_releaseDate = DateTime.Now;
//custSiteInfo.m_isHoldRelease = false;
//custSiteInfo.m_priceListID = -1;
//custSiteInfo.m_invoiceVerificationOrder = 1;
//custSiteInfo.m_supplySource = 3;
//custSiteInfo.m_demandTransType = 3;
//custSiteInfo.m_effective = new UFIDAU9BasePropertyTypesEffectiveData();
//custSiteInfo.m_effective.m_isEffective = true;
//custSiteInfo.m_effective.m_effectiveDate = DateTime.Parse("2021-11-01");
//custSiteInfo.m_effective.m_disableDate = DateTime.Parse("2099-01-01");
//custSiteInfo.m_supplyOrg = new UFIDAU9CBOPubControllerCommonArchiveDataDTOData();
//custSiteInfo.m_supplyOrg.m_iD = long.Parse(ConfigurationManager.AppSettings["orgID"].ToString());
//custSiteInfo.m_supplier = new UFIDAU9CBOPubControllerCommonArchiveDataDTOData();
//custSiteInfo.m_aRConfirmTerm = new UFIDAU9CBOPubControllerCommonArchiveDataDTOData();//立账条件
//custSiteInfo.m_aRConfirmTerm.m_iD = long.Parse(ARConfirmTermID);
//custSiteInfo.m_aRConfirmTerm.m_code = ModifyCustomer.ARConfirmTerm;
//custSiteList.Add(custSiteInfo);
//custSites = custSiteList.ToArray();
//cus.m_customerSiteDTOs = custSites;
#endregion
List<UFIDAU9ISVCustomerCustomerDTOData> NewList = new List<UFIDAU9ISVCustomerCustomerDTOData>();
NewList.Add(cus);
returnCustomers = client.Do(out returnMsg, context, NewList.ToArray(), contextDTO);
string errmessage = "";
if (returnMsg != null && returnMsg.Count() > 0)
{
foreach (UFSoft.UBF.Exceptions1.MessageBase ee in returnMsg)
{
errmessage += ee.innerMessages + ";";
}
Return.IsSuccess = false;
Return.MESSAGE = errmessage;
}
else
{
Return.IsSuccess = true;
Return.MESSAGE = "组织:" + ModifyCustomer.ORGCode + ",客户:" + ModifyCustomer.Code + "修改成功";
if (!string.IsNullOrWhiteSpace(ARConfirmTermID))
{
string SiteID = "";
long CusID = 0;
#region 获取组织ID
string OrgID = "";
string orgsql = @"select ID from Base_Organization
where Code='{0}'";
orgsql = string.Format(orgsql, ModifyCustomer.ORGCode);
DataTable orgdt = DBhlper.Query(orgsql, Appconfig.GetU9ConnStr());
if (orgdt.Rows.Count != 0)
{
OrgID = orgdt.Rows[0]["ID"].ToString();
}
#endregion
string cussql = @"select ID,State from CBO_Customer
where Code='{0}' AND Org='{1}'";
cussql = string.Format(cussql, ModifyCustomer.Code, OrgID);
DataTable cusdt = DBhlper.Query(cussql, Appconfig.GetU9ConnStr());
if (cusdt.Rows.Count > 0)
{
CusID = long.Parse(cusdt.Rows[0]["ID"].ToString());
#region 获取位置ID
string sitesql = @"select ID from CBO_CustomerSite
where Customer='{0}'";
sitesql = string.Format(sitesql, CusID);
DataTable sitedt = DBhlper.Query(sitesql, Appconfig.GetU9ConnStr());
if (sitedt.Rows.Count != 0)
{
SiteID = sitedt.Rows[0]["ID"].ToString();
}
#endregion
if (!string.IsNullOrWhiteSpace(SiteID))
{
string updatesql = @" update CBO_CustomerSite set ARConfirmTerm='{0}',Customer='{2}' where ID='{1}'
update CBO_CustomerSite_trl set name='{3}' where ID='{1}' ";
updatesql = string.Format(updatesql, ARConfirmTermID, SiteID, CusID, ModifyCustomer.Name);
DBhlper.Query(updatesql, Appconfig.GetU9ConnStr());
}
}
}
}
}
else
{
Return.IsSuccess = false;
Return.MESSAGE = "组织:" + ModifyCustomer.ORGCode + "下未找到客户:" + ModifyCustomer.Code;
}
}
else
{
Return.IsSuccess = false;
Return.MESSAGE = QueryMessage;
}
}
else
{
Return.IsSuccess = false;
Return.MESSAGE = QueryMessage;
}
}
else
{
Return.IsSuccess = false;
Return.MESSAGE = "传入的客户信息为空";
}
}
catch (Exception ex)
{
Return.IsSuccess = false;
Return.MESSAGE = GetExceptionMessage(ex);
}
StringBuilder strHeadr = new StringBuilder();
strHeadr.AppendLine("-------------接口传出参数:");
strHeadr.AppendLine("IsSuccess:" + Return.IsSuccess);
strHeadr.AppendLine("MESSAGE:" + Return.MESSAGE);
strHeadr.AppendLine("Code:" + Return.CustomerCode);
strHeadr.AppendLine("***************接口传出参数End");
Appconfig.WriteLogFile(strHeadr.ToString(), MessageLogName);
return Return;
}
#region 给上下文信息赋值1
/// <summary>
/// 给上下文信息赋值
/// </summary>
/// <returns></returns>
private ThreadContext CreateContextObj()
{
// 实例化应用上下文对象
ThreadContext thContext = new ThreadContext();
System.Collections.Generic.Dictionary<object, object> ns = new Dictionary<object, object>();
ns.Add("OrgID", ConfigurationManager.AppSettings["orgID"].ToString());
ns.Add("UserID", ConfigurationManager.AppSettings["userID"].ToString());
ns.Add("UserCode", ConfigurationManager.AppSettings["userCode"].ToString());
ns.Add("Datetime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
ns.Add("CultureName", ConfigurationManager.AppSettings["cultureName"].ToString());
ns.Add("EnterpriseID", ConfigurationManager.AppSettings["enterpriseID"].ToString());
ns.Add("DefaultCultureName", ConfigurationManager.AppSettings["cultureName"].ToString());
thContext.nameValueHas = ns;
return thContext;
}
#endregion
#region 给上下文信息赋值
/// <summary>
/// 给上下文信息赋值
/// </summary>
/// <returns></returns>
private UFIDAU9CBOPubControllerContextDTOData CreateContextDto()
{
// 实例化应用上下文对象
UFIDAU9CBOPubControllerContextDTOData thContext = new UFIDAU9CBOPubControllerContextDTOData();
thContext.m_orgID = long.Parse(ConfigurationManager.AppSettings["orgID"].ToString());
thContext.m_cultureName = ConfigurationManager.AppSettings["cultureName"].ToString();
thContext.m_entCode = ConfigurationManager.AppSettings["enterpriseID"].ToString();
thContext.m_orgCode = ConfigurationManager.AppSettings["orgCode"].ToString();
thContext.m_userCode = ConfigurationManager.AppSettings["userCode"].ToString();
thContext.m_userID = long.Parse(ConfigurationManager.AppSettings["userID"].ToString());
return thContext;
}
#endregion
private string GetDepartmentID(string Code, string ORGCode)
{
string sql = @"
select oo.ID from CBO_Department oo
inner join CBO_Department_Trl oot on oo.id=oot.id and oot.SysMLFlag='zh-CN'
inner join Base_Organization org on org.id=oo.org
where org.code='" + ORGCode + @"' and oo.Code='" + Code + @"'";
DataTable dt = DBhlper.Query(sql, ERPConStr);
if (dt != null && dt.Rows.Count > 0)
{
return dt.Rows[0][0].ToString();
}
return "";
}
private string GetOperatorsID(string Code, string ORGCode)
{
string sql = @"
select oo.ID from CBO_Operators oo
inner join CBO_Operators_Trl oot on oo.id=oot.id and oot.SysMLFlag='zh-CN'
inner join Base_Organization org on org.id=oo.org
where org.code='" + ORGCode + @"' and oo.Code='" + Code + @"'";
DataTable dt = DBhlper.Query(sql, ERPConStr);
if (dt != null && dt.Rows.Count > 0)
{
return dt.Rows[0][0].ToString();
}
return "";
}
#region 提取异常信息
/// <summary>
/// 提取异常信息
/// </summary>
/// <param name="ex"></param>
private string GetExceptionMessage(Exception ex)
{
string faultMessage = "未知错误,请查看ERP日志!";
System.TimeoutException timeoutEx = ex as System.TimeoutException;
if (timeoutEx != null)
{
faultMessage = "访问服务超时,请修改配置信息!";
}
else
{
FaultException<ServiceException> faultEx = ex as FaultException<ServiceException>;
if (faultEx == null)
{
faultMessage = ex.Message;
}
else
{
ServiceException serviceEx = faultEx.Detail;
if (serviceEx != null && !string.IsNullOrEmpty(serviceEx.Message)
&& !serviceEx.Message.Equals("fault", StringComparison.OrdinalIgnoreCase))
{
// 错误信息在faultEx.Message中,请提取,
// 格式为"Fault:料品不能为空,请录入\n 在....."
int startIndex = serviceEx.Message.IndexOf(":");
int endIndex = serviceEx.Message.IndexOf("\n");
if (endIndex == -1)
endIndex = serviceEx.Message.Length;
if (endIndex > 0 && endIndex > startIndex + 1)
{
faultMessage = serviceEx.Message.Substring(startIndex + 1, endIndex - startIndex - 1);
}
else
{
faultMessage = serviceEx.Message;
}
}
}
}
return faultMessage;
}
#endregion
}
}