|
@@ -1,15 +1,16 @@
|
|
|
package restcontroller.webmanage.saletool.custorder;
|
|
|
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import common.Controller;
|
|
|
import common.YosException;
|
|
|
import common.annotation.API;
|
|
|
-import common.data.InsertSQL;
|
|
|
-import common.data.Row;
|
|
|
-import common.data.Rows;
|
|
|
-import common.data.SQLFactory;
|
|
|
+import common.data.*;
|
|
|
import restcontroller.R;
|
|
|
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.util.ArrayList;
|
|
|
+
|
|
|
|
|
|
/**
|
|
|
* C端订单
|
|
@@ -29,36 +30,55 @@ public class CustOrder extends Controller {
|
|
|
public String insertOrUpdate() throws YosException {
|
|
|
|
|
|
Long sa_custorderid = content.getLongValue("sa_custorderid");
|
|
|
- Long sa_fadid = content.getLongValue("sa_fadid");
|
|
|
+ JSONArray items = content.getJSONArray("items");
|
|
|
|
|
|
- Rows fadRows = dbConnect.runSqlQuery("SELECT * from sa_fad WHERE sa_fadid=" + sa_fadid + " and siteid='" + siteid + "'");
|
|
|
- if (fadRows.isEmpty()) {
|
|
|
- return getErrReturnObject().setErrMsg("商品不存在").toString();
|
|
|
- }
|
|
|
- Row fadRow = fadRows.get(0);
|
|
|
+ ArrayList<String> sqlList = new ArrayList<>();
|
|
|
|
|
|
if (sa_custorderid <= 0) {
|
|
|
sa_custorderid = createTableID("sa_custorder");
|
|
|
- InsertSQL insertSQL = SQLFactory.createInsertSQL(this, "sa_custorder");
|
|
|
- insertSQL.setUniqueid(sa_custorderid);
|
|
|
- insertSQL.setSiteid(siteid);
|
|
|
- insertSQL.setValue("", "");
|
|
|
- insertSQL.insert();
|
|
|
+ InsertSQL insertSQL = CustOrderHelper.getOrderInsertSQL(this, sa_custorderid);
|
|
|
+ sqlList.add(insertSQL.getSQL());
|
|
|
+ content.put("sa_custorderid", sa_custorderid);
|
|
|
+ }
|
|
|
|
|
|
+ ArrayList<Long> sa_fadids = new ArrayList<>();
|
|
|
+
|
|
|
+ for (Object object : items) {
|
|
|
+ JSONObject jsonObject = (JSONObject) object;
|
|
|
+ Long sa_fadid = jsonObject.getLongValue("sa_fadid");
|
|
|
+ sa_fadids.add(sa_fadid);
|
|
|
+ Rows rows = dbConnect.runSqlQuery("SELECT * from sa_custorderitems WHERE sa_custorderid=" + sa_custorderid + " and sa_fadid=" + sa_fadid + " and siteid='" + siteid + "'");
|
|
|
+ if (rows.isNotEmpty()) {
|
|
|
+ UpdateSQL updateSQL = SQLFactory.createUpdateSQL(this, "sa_custorderitems");
|
|
|
+ updateSQL.setSiteid(siteid);
|
|
|
+ updateSQL.setUniqueid(rows.get(0).getLong("sa_custorderitemsid"));
|
|
|
+ updateSQL.setValue("qty", jsonObject.getBigDecimalValue("qty"));
|
|
|
+ updateSQL.setValue("amount", jsonObject.getBigDecimalValue("qty").multiply(rows.get(0).getBigDecimal("price")));
|
|
|
+ sqlList.add(updateSQL.getSQL());
|
|
|
+ } else {
|
|
|
+ InsertSQL itemsInsertSQL = CustOrderHelper.getOrderItemsInsertSQL(this, sa_custorderid, jsonObject);
|
|
|
+ sqlList.add(itemsInsertSQL.getSQL());
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- insertSQL = SQLFactory.createInsertSQL(this, "sa_custorderitems");
|
|
|
- insertSQL.setUniqueid(createTableID("sa_custorderitems"));
|
|
|
- insertSQL.setSiteid(siteid);
|
|
|
- insertSQL.setValue("sa_custorderid", sa_custorderid);
|
|
|
- insertSQL.setValue("sa_fadid", content.getLongValue("sa_fadid"));
|
|
|
- insertSQL.setValue("pricetype", content.getLongValue("sa_fadid"));
|
|
|
- insertSQL.insert();
|
|
|
+ DeleteSQL deleteSQL = SQLFactory.createDeleteSQL(this, "sa_custorderitems");
|
|
|
+ deleteSQL.setSiteid(siteid);
|
|
|
+ deleteSQL.setWhere("sa_custorderid", sa_custorderid);
|
|
|
+ deleteSQL.setWhere("sa_fadid not in" + sa_fadids.toString().replace("[", "(").replace("]", ")") + "");
|
|
|
+ deleteSQL.delete();
|
|
|
|
|
|
- content.put("sa_custorderid", sa_custorderid);
|
|
|
- } else {
|
|
|
+ dbConnect.runSqlUpdate(sqlList);
|
|
|
+
|
|
|
+ Rows amountRows = dbConnect.runSqlQuery("SELECT sum(amount) amount from sa_custorderitems WHERE sa_custorderid=" + sa_custorderid + " and siteid='" + siteid + "'");
|
|
|
|
|
|
- }
|
|
|
|
|
|
+ UpdateSQL updateSQL = SQLFactory.createUpdateSQL(this, "sa_custorder");
|
|
|
+ updateSQL.setSiteid(siteid);
|
|
|
+ updateSQL.setUniqueid(sa_custorderid);
|
|
|
+ updateSQL.setValue("remarks", content.getStringValue("remarks"));
|
|
|
+ updateSQL.setValue("rec_contactsid", content.getLongValue("rec_contactsid"));
|
|
|
+ updateSQL.setValue("amount", amountRows.get(0).getBigDecimal("amount"));
|
|
|
+ updateSQL.update();
|
|
|
|
|
|
return getSucReturnObject().toString();
|
|
|
}
|