376 lines
18 KiB
Plaintext
Executable File
376 lines
18 KiB
Plaintext
Executable File
using GXWebApi.Common;
|
||
using Kingdee.BOS.WebApi.Client;
|
||
using log4net;
|
||
using Newtonsoft.Json;
|
||
using Newtonsoft.Json.Linq;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Configuration;
|
||
using System.Data;
|
||
using System.Web.Http;
|
||
using System.Xml.Linq;
|
||
|
||
namespace GXWebApi.Controllers
|
||
{
|
||
public class DifferentialController : ApiController
|
||
{
|
||
private static readonly ILog Logger = LogManager.GetLogger(typeof(DifferentialController));
|
||
|
||
// TODO: 在此添加金蝶云星空相关的 Action 方法
|
||
// 示例:
|
||
[HttpGet]
|
||
public IHttpActionResult Test()
|
||
{
|
||
return Ok("GXWebApi is running.");
|
||
}
|
||
|
||
#region 单独更新创建CRM产品
|
||
[HttpPost]
|
||
public IHttpActionResult CrUpCrmProduct([FromBody] JObject obj)
|
||
{
|
||
#region 定义参数
|
||
string FMaNumber = obj["FMaNumber"].ToString();
|
||
K3CloudApiClient client = null;
|
||
string acctID = "20191111204222978"; //登录帐套ID
|
||
string userID = "sysmes"; //用户账号
|
||
string password = "Admin@123"; //用户密码
|
||
string erpURL = "https://gaoxian.ik3cloud.com/k3cloud/"; //K3URL
|
||
int erpLaType = 2052; //语言种类4
|
||
|
||
#endregion
|
||
|
||
try
|
||
{
|
||
//第一步,登录金蝶系统
|
||
client = new K3CloudApiClient(erpURL);
|
||
var loginResult = client.ValidateLogin(acctID, userID, password, erpLaType);
|
||
var resultType = JObject.Parse(loginResult)["LoginResultType"].Value<int>();
|
||
if (resultType == 1)
|
||
{
|
||
//ERP接口Json
|
||
JObject QueryData = new JObject()
|
||
{
|
||
{ "FormId", "BD_MATERIAL" },
|
||
{ "FieldKeys", "" } ,
|
||
{ "OrderString", "" },
|
||
{ "FilterString", "" } ,
|
||
{ "TopRowCount", 0 },
|
||
{ "StartRow", 0 } ,
|
||
{ "Limit", 5000 }
|
||
};
|
||
QueryData["FieldKeys"] = "FMATERIALID,FNumber,FName,FUSEORGID.FNUMBER,FTaxRateId.FNumber,FSpecification,FForbidStatus,FIsEnable";
|
||
QueryData["OrderString"] = "FMATERIALID";
|
||
QueryData["FilterString"] = $" FDOCUMENTSTATUS = 'C' and FUSEORGID = '1' and FInvPtyId=10003 and FNumber = '{FMaNumber}' ";
|
||
var QueryList = client.ExecuteBillQuery(QueryData.ToString());
|
||
if (QueryList != null && QueryList.Count > 0)
|
||
{
|
||
|
||
string firstCellValue = QueryList[0][0].ToStringEx();
|
||
try
|
||
{
|
||
JToken returnToken = JToken.Parse(firstCellValue);
|
||
if (returnToken is JObject returnInfo && returnInfo.TryGetValue("Result", out JToken VALUE))
|
||
{
|
||
//LogHelper.WriteLog(returnInfo["Result"]["ResponseStatus"]["Errors"][0]["Message"].ToStringEx());//写入错误日志中(2021/10/12,经测试此代码暂时无法写入日志)
|
||
throw new Exception(returnInfo["Result"]["ResponseStatus"]["Errors"][0]["Message"].ToStringEx());//提示错误信息,并中止此次调用
|
||
}
|
||
}
|
||
catch (JsonReaderException)
|
||
{
|
||
// firstCellValue 不是 JSON 格式,说明是正常的查询结果数据,跳过错误检查
|
||
}
|
||
|
||
foreach (var sub in QueryList)
|
||
{
|
||
#region 获取金蝶字段信息
|
||
CrmApi crmApi = new CrmApi();
|
||
string MaterialID = sub[0].ToStringEx(); //物料内码
|
||
string MaterialNumber = sub[1].ToStringEx(); //物料内码
|
||
string MaterialName = sub[2].ToStringEx().Replace("\0", "").Replace(@"\u0000", ""); //物料名称
|
||
string OrgNumber = sub[3].ToStringEx(); //使用组织编码
|
||
string FTAXRATEID = sub[4].ToStringEx(); //税率
|
||
string FITEMMODEL = sub[5].ToStringEx(); //规格型号
|
||
string FForbidStatus = sub[6].ToStringEx().Equals("A") ? "1" : "2"; //禁用状态
|
||
bool FIsEnable = Convert.ToBoolean(sub[7]); //是否启用BOM版本
|
||
if (FITEMMODEL.Length > 200)
|
||
{
|
||
FITEMMODEL = FITEMMODEL.Substring(0, 199);
|
||
}
|
||
#endregion
|
||
|
||
#region 获取Token
|
||
string SelSQL = "select id from productCategory where code__c ='" + MaterialNumber + "' ";
|
||
string Token = crmApi.GetToken();
|
||
if (string.IsNullOrEmpty(Token))
|
||
{
|
||
throw new Exception("获取CRM Token失败!");
|
||
}
|
||
#endregion
|
||
|
||
#region 产品(物料)
|
||
|
||
string SelData = string.Empty;
|
||
|
||
#region 获取税率
|
||
SelSQL = "select id from taxRate__c where code__c = '" + FTAXRATEID + "'";
|
||
SelData = crmApi.SelectSQLApi(SelSQL, Token);
|
||
SelSQLData TAXRATEData = JsonConvert.DeserializeObject<SelSQLData>(SelData);
|
||
int TAXRATECount = TAXRATEData.result.count;
|
||
string TAXRATEID = string.Empty;
|
||
if (TAXRATECount > 0)
|
||
{
|
||
List<Dictionary<string, object>> keys = TAXRATEData.result.records;
|
||
TAXRATEID = keys[0]["id"].ToString();
|
||
}
|
||
#endregion
|
||
|
||
#region 判断是否已有
|
||
string ProductID = string.Empty;
|
||
SelSQL = "select id from product where productName ='" + MaterialNumber + "' ";
|
||
SelData = crmApi.SelectSQLApi(SelSQL, Token);
|
||
SelSQLData selSQLData2 = JsonConvert.DeserializeObject<SelSQLData>(SelData);
|
||
int count2 = selSQLData2.result.count;
|
||
|
||
ProductRoot productRoot = new ProductRoot();
|
||
ProductData productData = new ProductData();
|
||
productData.entityType = "11010000400001";
|
||
productData.customItem168__c = MaterialID;
|
||
productData.productName = MaterialNumber;
|
||
productData.customItem161__c = MaterialName;
|
||
productData.fscProductModel = FITEMMODEL;
|
||
productData.enableStatus = FForbidStatus;//禁用状态
|
||
productData.defaultTaxRate__c = TAXRATEID;
|
||
productData.baseUnitId = "4084356036024971"; //PCS
|
||
productData.parentId = "4084144016153157";//产品目录
|
||
productData.isActiveBom__c = FIsEnable;//是否启用BOM版本 1:是;0:否
|
||
productRoot.data = productData;
|
||
|
||
if (count2 <= 0)
|
||
{
|
||
#region 创建
|
||
string CproductReturnData = crmApi.CreateApi("product", Token, JsonConvert.SerializeObject(productRoot));
|
||
CreateReturnData createReturnData = JsonConvert.DeserializeObject<CreateReturnData>(CproductReturnData);
|
||
if (!createReturnData.code.Equals("200"))
|
||
{
|
||
throw new Exception("返回值:" + createReturnData.code + ",创建CRM产品失败:" + createReturnData.msg + "接口传入JSON:" + JsonConvert.SerializeObject(productRoot));
|
||
}
|
||
#endregion
|
||
}
|
||
else
|
||
{
|
||
#region 更新
|
||
string F_PATC_Text_qtr = selSQLData2.result.records[0]["id"].ToString();
|
||
string ReturnData = crmApi.UpdateApi("product", F_PATC_Text_qtr, Token, JsonConvert.SerializeObject(productRoot));
|
||
CreateReturnData createReturnData = JsonConvert.DeserializeObject<CreateReturnData>(ReturnData);
|
||
if (!createReturnData.code.Equals("200"))
|
||
{
|
||
throw new Exception("返回值:" + createReturnData.code + ",更新CRM产品失败:" + createReturnData.msg + "接口传入JSON:" + JsonConvert.SerializeObject(productRoot));
|
||
}
|
||
#endregion
|
||
}
|
||
#endregion
|
||
|
||
#endregion
|
||
}
|
||
}
|
||
else
|
||
{
|
||
throw new Exception("未找到当前物料信息!");
|
||
}
|
||
}
|
||
else
|
||
{
|
||
throw new Exception("登录云星空失败!");
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
return Ok(new
|
||
{
|
||
success = "false",
|
||
message = "同步失败:" + ex.Message
|
||
});
|
||
}
|
||
return Ok(new
|
||
{
|
||
success = "true",
|
||
message = "同步成功"
|
||
});
|
||
}
|
||
#endregion
|
||
|
||
#region 根据OA费用报销单更新金蝶付款申请单部门字段
|
||
[HttpPost]
|
||
public IHttpActionResult UpKingdeeDept([FromBody] JObject obj)
|
||
{
|
||
#region 变量
|
||
string RequestID = string.Empty;
|
||
MySqlUtils mySqlUtils = new MySqlUtils();
|
||
string SQL = string.Empty;
|
||
DataTable dt = new DataTable(), dt1 = new DataTable();
|
||
K3CloudApiClient client = null;
|
||
string FID = string.Empty;
|
||
#endregion
|
||
|
||
try
|
||
{
|
||
#region 检验参数
|
||
if (obj == null || obj.Property("RequestID") == null)
|
||
{
|
||
throw new Exception("请检查是否传参 RequestID!");
|
||
}
|
||
RequestID = obj["RequestID"].ToString();
|
||
if (RequestID.Equals(""))
|
||
{
|
||
throw new Exception("请检查传参是否正确!");
|
||
}
|
||
#endregion
|
||
|
||
#region 定义参数
|
||
string acctID = "20191111204222978"; //登录帐套ID
|
||
string userID = "sysmes"; //用户账号
|
||
string password = "Admin@123"; //用户密码
|
||
string erpURL = "https://gaoxian.ik3cloud.com/k3cloud/"; //K3URL
|
||
//string erpDB = PBCursor["K3DB"].ToString(); //K3 账套 -- 公有云,不再支持金蝶帐套
|
||
int erpLaType = 2052; //语言种类4
|
||
#endregion
|
||
|
||
#region 连接OA数据库
|
||
string OAConnection = ConfigurationManager.ConnectionStrings["OAConnection"].ConnectionString;
|
||
mySqlUtils.ConnectionString = OAConnection;
|
||
mySqlUtils.GetConnection();
|
||
#endregion
|
||
|
||
#region 获取费用报销数据
|
||
SQL = $@"select
|
||
S1.DEPARTMENTNAME
|
||
,S2.sqzz
|
||
,S.djbh
|
||
from formtable_main_24 S
|
||
LEFT JOIN hrmdepartment S1 ON S.szbm = S1.id
|
||
left join uf_cgsqzz S2 on S.sqzz = S2.id
|
||
where S.requestId ='{RequestID}'";
|
||
dt = mySqlUtils.ExecQuery(SQL);
|
||
if (dt == null || dt.Rows.Count == 0)
|
||
{
|
||
throw new Exception("【" + RequestID + "】查询费用报销数据为空!");
|
||
}
|
||
SQL = $@"select jdbmbh ,jdbmmc from uf_oa_kingdee_dept
|
||
where zzmc ='{dt.Rows[0]["sqzz"].ToString()}' and oabm = '{dt.Rows[0]["DEPARTMENTNAME"].ToString()}'";
|
||
dt1 = mySqlUtils.ExecQuery(SQL);
|
||
if (dt1 == null || dt1.Rows.Count == 0)
|
||
{
|
||
throw new Exception("【"+ RequestID + "】,【" + dt.Rows[0]["sqzz"].ToString() + "】,【" + dt.Rows[0]["DEPARTMENTNAME"].ToString() + "】查询部门映射关系为空!");
|
||
}
|
||
#endregion
|
||
|
||
#region 获取付款申请单FID
|
||
//第一步,登录金蝶系统
|
||
client = new K3CloudApiClient(erpURL);
|
||
var loginResult = client.ValidateLogin(acctID, userID, password, erpLaType);
|
||
var resultType = JObject.Parse(loginResult)["LoginResultType"].Value<int>();
|
||
|
||
//登录结果类型等于1,代表登录成功
|
||
if (resultType == 1) //登录成功
|
||
{
|
||
#region 查询条件
|
||
JObject QueryData = new JObject();
|
||
QueryData["FormId"] = "CN_PAYAPPLY";
|
||
QueryData["FieldKeys"] = "FID";
|
||
QueryData["OrderString"] = "FID";
|
||
QueryData["FilterString"] = "FBILLNO = '" + dt.Rows[0]["djbh"].ToString() + "'";
|
||
QueryData["StartRow"] = 0;
|
||
#endregion
|
||
|
||
#region 返回结果
|
||
var QueryList = client.ExecuteBillQuery(QueryData.ToString());
|
||
if (QueryList != null && QueryList.Count > 0)
|
||
{
|
||
FID = QueryList[0][0].ToString();
|
||
}
|
||
#endregion
|
||
|
||
#region 更新付款申请单部门字段
|
||
JArray NeedUpDateFields = new JArray();
|
||
NeedUpDateFields.Add("FDEPARTMENT");
|
||
JObject JsonData = new JObject();
|
||
JsonData.Add("NeedUpDateFields", NeedUpDateFields);
|
||
JsonData.Add("IsDeleteEntry", "false");
|
||
JObject Model = new JObject();
|
||
Model.Add("FID", FID);
|
||
Model.Add("FDEPARTMENT", new JObject() { { "FNUMBER", dt1.Rows[0]["jdbmbh"].ToString() } }); // 部门
|
||
JsonData.Add("Model", Model);
|
||
var data = JsonData.ToStringEx();
|
||
var ret = client.Save("CN_PAYAPPLY", data);
|
||
K3Response myK3Response = WmsCommonFunction.GetBodyData<K3Response>(ret);
|
||
if (!myK3Response.Result.ResponseStatus.IsSuccess)
|
||
{
|
||
foreach (K3Error sub1 in myK3Response.Result.ResponseStatus.Errors)
|
||
{
|
||
throw new Exception($"{sub1.FieldName}:{sub1.Message},修改付款申请单部门Json:{data}");
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
}
|
||
else
|
||
{
|
||
throw new Exception("登录新云星空失败,初始数据失败!");
|
||
}
|
||
#endregion
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LogHelper.WriteLog("更新金蝶付款申请单部门字段异常:" + ex.Message);
|
||
}
|
||
finally
|
||
{
|
||
#region 关闭连接
|
||
mySqlUtils.Dispose();
|
||
#endregion
|
||
}
|
||
LogHelper.WriteLog("【" + RequestID + "】更新金蝶付款申请单部门字段成功");
|
||
return Ok();
|
||
}
|
||
#endregion
|
||
|
||
#region 金蝶生产订单删除按钮删除WMS生产任务单
|
||
[HttpPost]
|
||
public IHttpActionResult RestoreMOInfo([FromBody] JObject obj)
|
||
{
|
||
#region 变量
|
||
string SQL = string.Empty,FMOID = string.Empty;
|
||
string FMOBillNO = obj["FMOBillNO"].ToString();
|
||
DBHelpe db = new DBHelpe();
|
||
#endregion
|
||
|
||
try
|
||
{
|
||
SQL = $@"SELECT FINTERID from T_PRD_MO
|
||
where FBILLNO = '{FMOBillNO}';";
|
||
DataTable dt = db.ExecuteTable(SQL);
|
||
if (dt != null && dt.Rows.Count > 0)
|
||
{
|
||
FMOID = dt.Rows[0]["FINTERID"].ToString();
|
||
SQL = $@"DELETE FROM T_PRD_MO WHERE FINTERID = '{FMOID}';
|
||
DELETE FROM T_PRD_MOENTRY WHERE FINTERID = '{FMOID}';
|
||
DELETE FROM T_PRD_MOENTRY1 WHERE FINTERID = '{FMOID}';";
|
||
db.ExecuteNonQuery(SQL);
|
||
}
|
||
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LogHelper.WriteLog("WMS删除生产任务单异常:" + ex.Message);
|
||
}
|
||
finally
|
||
{
|
||
LogHelper.WriteLog("金蝶生产任务单传入值:" + FMOBillNO);
|
||
}
|
||
return Ok(obj);
|
||
}
|
||
#endregion
|
||
|
||
}
|
||
}
|