在 C# 中加入 Odoo rpc 查询
Odoo rpc query with join in C#
我有这种类型的代码可以从 Odoo
Rpc
:
中进行选择
XmlRpcClient client = new XmlRpcClient();
client.Url = LocalApplication.Url;
client.Path = "common";
// LOGIN BG
XmlRpcRequest requestLogin = new XmlRpcRequest("authenticate");
requestLogin.AddParams(LocalApplication.Db, LocalApplication.User, LocalApplication.Pass, XmlRpcParameter.EmptyStruct());
XmlRpcResponse responseLogin = client.Execute(requestLogin);
// READ
client.Path = "object";
// var x = client.Execute("select * from res_partner");
XmlRpcRequest requestSearch = new XmlRpcRequest("execute_kw");
requestSearch.AddParams(LocalApplication.Db, responseLogin.GetInt(),
LocalApplication.Pass, "res.partner", "search_read",
XmlRpcParameter.AsArray(
XmlRpcParameter.AsArray(
XmlRpcParameter.AsArray("is_agent", "=", "True".ToLower()))),
XmlRpcParameter.AsStruct(
XmlRpcParameter.AsMember("fields",
XmlRpcParameter.AsArray("name", "id", "phone", "email"))));
// SAVE
XmlRpcResponse responseSearch = client.Execute(requestSearch);
var agents = (responseSearch.GetObject() as List<object>);
Taswiq.Bussiness.Person p = new Person(LocalApplication.ConnectionString);
if (agents != null)
{
foreach (var a in agents)
{
Dictionary<string, object> agent = a as Dictionary<string, object>;
p.Name = agent["name"].ToString() == "False" ? "ND" : agent["name"].ToString();
p.Marca = Convert.ToInt32(agent["id"]);
p.Type = 1;
p.Phone = agent["phone"].ToString() == "False" ? "ND" : agent["phone"].ToString();
p.Email = agent["email"].ToString() == "False" ? "ND" : agent["email"].ToString();
MessageStruct result = p.Save();
if (result.HasErrors)
{
return result.Message;
}
}
return "Success";
}
一切正常,但现在我需要像这样从这 2 个表进行一些连接:
Select *
from product_template as d
inner join product_product as r on r.product_tmpl_id = d.id;
除了 Python 代码,我什么都找不到。
有人可以帮我吗?
我想我找到了解决办法。我不知道这是否是最好的方法,但就我而言,它解决了问题。
我已经为我需要加入的每个 table 制作了相同的列表:
public List<object> lstStock = new List<object>();
public void PopulateStockList()
{
XmlRpcClient stock = new XmlRpcClient();
stock.Url = LocalApplication.Url;
stock.Path = "common";
//LOGIN BG
XmlRpcRequest requestLogin = new XmlRpcRequest("authenticate");
requestLogin.AddParams(LocalApplication.Db, LocalApplication.User,
LocalApplication.Pass,
XmlRpcParameter.EmptyStruct());
XmlRpcResponse responseLogin = stock.Execute(requestLogin);
//READ
stock.Path = "object";
XmlRpcRequest requestSearch = new XmlRpcRequest("execute_kw");
requestSearch.AddParams(LocalApplication.Db, responseLogin.GetInt(),
LocalApplication.Pass, "stock.quant", "search_read",
XmlRpcParameter.AsArray(
XmlRpcParameter.AsArray(
XmlRpcParameter.AsArray("id", ">", 0)
)
),
XmlRpcParameter.AsStruct(
XmlRpcParameter.AsMember("fields",
XmlRpcParameter.AsArray("product_id", "quantity", "in_date",
"owner_id"))
)
);
XmlRpcResponse responseSearch = stock.Execute(requestSearch);
lstStock = (responseSearch.GetObject() as List<object>);
}
我需要加入的每个 table 都采用相同的方式。
最后,在 foreach
中,我做了一个连接,我需要填充我的 sql
table
public string DownloadRpcStock()
{
try
{
PopulateProdTemplate();
PopulateStockList();
PopulateProductList();
Stock s = new Stock(LocalApplication.ConnectionString);
foreach (var st in lstStock)
{
Dictionary<string, object> stc = st as Dictionary<string, object>;
var productId = stc["product_id"] as List<object>;
if (productId != null)
s.IdProduct = Convert.ToInt32(productId[0]); //Din lista care vine pe product_id imi iau valoarea dorita
s.IdStorage = 1;
s.StockQuantity = Convert.ToDouble(stc["quantity"]);
s.Rest = Convert.ToDouble(stc["quantity"]);
var prod = lstProduct.FirstOrDefault(
k => Convert.ToInt32((k as Dictionary<string, object>)["id"]) == Convert.ToInt32((stc["product_id"] as List<object>)[0])) as Dictionary<string, object>;
var prodTemplate =
lstProdTemplate.FirstOrDefault(
k => Convert.ToInt32((k as Dictionary<string, object>)["id"]) ==
Convert.ToInt32((prod["product_tmpl_id"] as List<object>)[0])) as Dictionary<string, object>;
if (prodTemplate != null)
s.Price = Convert.ToDouble(prodTemplate["list_price"]);
s.Batch = "NoBatch"; // Provizoriu
s.Date = Convert.ToDateTime(stc["in_date"]);
s.ExpireDate = Convert.ToDateTime(stc["in_date"]); // Provizoriu
s.IdCustomerSuplier = Convert.ToInt32(stc["owner_id"]);
s.Id = 0; //if (Id == 0) act = insert
MessageStruct result = s.Save();
if (result.HasErrors)
{
return result.Message;
}
}
return "Success";
}
catch (Exception ex)
{
return ex.Message;
}
}
在网上找了很多这样的问题都没有解决办法。
我希望我的解决方案将来能帮助到其他人。
我有这种类型的代码可以从 Odoo
Rpc
:
XmlRpcClient client = new XmlRpcClient();
client.Url = LocalApplication.Url;
client.Path = "common";
// LOGIN BG
XmlRpcRequest requestLogin = new XmlRpcRequest("authenticate");
requestLogin.AddParams(LocalApplication.Db, LocalApplication.User, LocalApplication.Pass, XmlRpcParameter.EmptyStruct());
XmlRpcResponse responseLogin = client.Execute(requestLogin);
// READ
client.Path = "object";
// var x = client.Execute("select * from res_partner");
XmlRpcRequest requestSearch = new XmlRpcRequest("execute_kw");
requestSearch.AddParams(LocalApplication.Db, responseLogin.GetInt(),
LocalApplication.Pass, "res.partner", "search_read",
XmlRpcParameter.AsArray(
XmlRpcParameter.AsArray(
XmlRpcParameter.AsArray("is_agent", "=", "True".ToLower()))),
XmlRpcParameter.AsStruct(
XmlRpcParameter.AsMember("fields",
XmlRpcParameter.AsArray("name", "id", "phone", "email"))));
// SAVE
XmlRpcResponse responseSearch = client.Execute(requestSearch);
var agents = (responseSearch.GetObject() as List<object>);
Taswiq.Bussiness.Person p = new Person(LocalApplication.ConnectionString);
if (agents != null)
{
foreach (var a in agents)
{
Dictionary<string, object> agent = a as Dictionary<string, object>;
p.Name = agent["name"].ToString() == "False" ? "ND" : agent["name"].ToString();
p.Marca = Convert.ToInt32(agent["id"]);
p.Type = 1;
p.Phone = agent["phone"].ToString() == "False" ? "ND" : agent["phone"].ToString();
p.Email = agent["email"].ToString() == "False" ? "ND" : agent["email"].ToString();
MessageStruct result = p.Save();
if (result.HasErrors)
{
return result.Message;
}
}
return "Success";
}
一切正常,但现在我需要像这样从这 2 个表进行一些连接:
Select *
from product_template as d
inner join product_product as r on r.product_tmpl_id = d.id;
除了 Python 代码,我什么都找不到。
有人可以帮我吗?
我想我找到了解决办法。我不知道这是否是最好的方法,但就我而言,它解决了问题。
我已经为我需要加入的每个 table 制作了相同的列表:
public List<object> lstStock = new List<object>();
public void PopulateStockList()
{
XmlRpcClient stock = new XmlRpcClient();
stock.Url = LocalApplication.Url;
stock.Path = "common";
//LOGIN BG
XmlRpcRequest requestLogin = new XmlRpcRequest("authenticate");
requestLogin.AddParams(LocalApplication.Db, LocalApplication.User,
LocalApplication.Pass,
XmlRpcParameter.EmptyStruct());
XmlRpcResponse responseLogin = stock.Execute(requestLogin);
//READ
stock.Path = "object";
XmlRpcRequest requestSearch = new XmlRpcRequest("execute_kw");
requestSearch.AddParams(LocalApplication.Db, responseLogin.GetInt(),
LocalApplication.Pass, "stock.quant", "search_read",
XmlRpcParameter.AsArray(
XmlRpcParameter.AsArray(
XmlRpcParameter.AsArray("id", ">", 0)
)
),
XmlRpcParameter.AsStruct(
XmlRpcParameter.AsMember("fields",
XmlRpcParameter.AsArray("product_id", "quantity", "in_date",
"owner_id"))
)
);
XmlRpcResponse responseSearch = stock.Execute(requestSearch);
lstStock = (responseSearch.GetObject() as List<object>);
}
我需要加入的每个 table 都采用相同的方式。
最后,在 foreach
中,我做了一个连接,我需要填充我的 sql
table
public string DownloadRpcStock()
{
try
{
PopulateProdTemplate();
PopulateStockList();
PopulateProductList();
Stock s = new Stock(LocalApplication.ConnectionString);
foreach (var st in lstStock)
{
Dictionary<string, object> stc = st as Dictionary<string, object>;
var productId = stc["product_id"] as List<object>;
if (productId != null)
s.IdProduct = Convert.ToInt32(productId[0]); //Din lista care vine pe product_id imi iau valoarea dorita
s.IdStorage = 1;
s.StockQuantity = Convert.ToDouble(stc["quantity"]);
s.Rest = Convert.ToDouble(stc["quantity"]);
var prod = lstProduct.FirstOrDefault(
k => Convert.ToInt32((k as Dictionary<string, object>)["id"]) == Convert.ToInt32((stc["product_id"] as List<object>)[0])) as Dictionary<string, object>;
var prodTemplate =
lstProdTemplate.FirstOrDefault(
k => Convert.ToInt32((k as Dictionary<string, object>)["id"]) ==
Convert.ToInt32((prod["product_tmpl_id"] as List<object>)[0])) as Dictionary<string, object>;
if (prodTemplate != null)
s.Price = Convert.ToDouble(prodTemplate["list_price"]);
s.Batch = "NoBatch"; // Provizoriu
s.Date = Convert.ToDateTime(stc["in_date"]);
s.ExpireDate = Convert.ToDateTime(stc["in_date"]); // Provizoriu
s.IdCustomerSuplier = Convert.ToInt32(stc["owner_id"]);
s.Id = 0; //if (Id == 0) act = insert
MessageStruct result = s.Save();
if (result.HasErrors)
{
return result.Message;
}
}
return "Success";
}
catch (Exception ex)
{
return ex.Message;
}
}
在网上找了很多这样的问题都没有解决办法。 我希望我的解决方案将来能帮助到其他人。