Bläddra i källkod

订单明细添加调整

eganwu 2 år sedan
förälder
incheckning
0c525f4583

+ 30 - 197
src/custom/restcontroller/webmanage/sale/order/OrderItems.java

@@ -58,7 +58,11 @@ public class OrderItems extends Controller {
     public String insertOrUpdate() throws YosException {
         Long sa_orderid = content.getLong("sa_orderid");
         Long sys_enterpriseid = content.getLong("sys_enterpriseid");
-//        Long sa_contractid = content.getLongValue("sa_contractid");
+        JSONArray items = content.getJSONArray("items");
+        //辅助类
+        OrderItemsHelper orderItemsHelper = new OrderItemsHelper(this);
+        ArrayList<String> sqlList = new ArrayList<>();
+
         Rows orderRows = getOrderRows(this, sa_orderid);
         if (orderRows.isEmpty()) {
             return getErrReturnObject().setErrMsg("数据不存在,无法添加").toString();
@@ -68,28 +72,16 @@ public class OrderItems extends Controller {
             }
         }
 
-//        sa_contractid = orderRows.get(0).getLong("sa_contractid");
+        //更新表头
+        sqlList.add(orderItemsHelper.getUpdateSql().getSQL());
 
         String type = orderRows.get(0).getString("type");
-        JSONArray items = content.getJSONArray("items");
         Long sa_promotionid = orderRows.get(0).getLong("sa_promotionid");
+        //查询活动
+        Rows promotionitems = dbConnect.runSqlQuery("select * from sa_promotion_items where siteid='" + siteid + "' and sa_promotionid=" + sa_promotionid);
+        //批量查询商品信息
+        RowsMap itemRowsMap = orderItemsHelper.getItemRowsMap(items);
 
-
-        ArrayList<String> sqlList = new ArrayList<>();
-        //查询erp库存
-//        Invbal.selectErpInvbal(this, items);
-
-        ArrayList<Long> itemids = new ArrayList<>();
-        for (Object obj : items) {
-            JSONObject item = (JSONObject) obj;
-            itemids.add(item.getLong("itemid"));
-        }
-
-        String sql = "select t1.*,t2.material from plm_item t1 left join plm_itemextend t2 ON t2.itemid = t1.itemid AND t2.siteid = t1.siteid where t1.siteid='" + siteid + "' and t1.itemid in  " + itemids;
-        sql = sql.replace("[", "(").replace("]", ")");
-        Rows itemRows = dbConnect.runSqlQuery(sql);
-
-        RowsMap itemRowsMap = itemRows.toRowsMap("itemid");
         BigDecimal sumqty = BigDecimal.ZERO;
         for (Object obj : items) {
             JSONObject item = (JSONObject) obj;
@@ -97,84 +89,20 @@ public class OrderItems extends Controller {
             Long sa_orderitemsid = item.getLong("sa_orderitemsid");
             BigDecimal qty = item.getBigDecimalValue("qty");
             sumqty = sumqty.add(qty);
-            if (!type.equals("促销订单")) {
-                if (itemRowsMap.containsKey(item.getString("itemid"))) {
-                    if (itemRowsMap.get(item.getString("itemid")).isNotEmpty()) {
-                        BigDecimal orderaddqty = itemRowsMap.get(item.getString("itemid")).get(0).getBigDecimal("orderaddqty");
-                        BigDecimal orderminqty = itemRowsMap.get(item.getString("itemid")).get(0).getBigDecimal("orderminqty");
-                        if (((qty.subtract(orderminqty)).remainder(orderaddqty)).compareTo(BigDecimal.ZERO) != 0) {
-                            return getErrReturnObject().setErrMsg("品号为" + itemRowsMap.get(item.getString("itemid")).get(0).getString("itemno") + "的商品数量不符合该商品的起订量和增量").toString();
-                        }
-                    }
-                }
-            }
             //商品信息
             Row itemRow = getItemRow(itemid);
-            Rows promotionitems = dbConnect.runSqlQuery("select * from sa_promotion_items where siteid='" + siteid + "' and sa_promotionid=" + sa_promotionid);
-            RowsMap promotionitemsRowsMap = promotionitems.toRowsMap("itemid");
-            if (type.equals("促销订单")) {
-                if (promotionitemsRowsMap.containsKey(item.getString("itemid"))) {
-                    if (promotionitemsRowsMap.get(item.getString("itemid")).isNotEmpty()) {
-                        BigDecimal saledqty = promotionitemsRowsMap.get(item.getString("itemid")).get(0).getBigDecimal("saledqty");
-                        BigDecimal groupqty = promotionitemsRowsMap.get(item.getString("itemid")).get(0).getBigDecimal("groupqty");
-                        BigDecimal orderaddqty = promotionitemsRowsMap.get(item.getString("itemid")).get(0).getBigDecimal("orderaddqty");
-                        BigDecimal orderminqty = promotionitemsRowsMap.get(item.getString("itemid")).get(0).getBigDecimal("orderminqty");
-                        boolean islimit = promotionitemsRowsMap.get(item.getString("itemid")).get(0).getBoolean("islimit");
-                        if (((qty.subtract(orderminqty)).remainder(orderaddqty)).compareTo(BigDecimal.ZERO) != 0) {
-                            return getErrReturnObject().setErrMsg("品号为" + itemRowsMap.get(item.getString("itemid")).get(0).getString("itemno") + "的商品数量不符合该商品的起订量和增量").toString();
-                        }
-                        if (islimit) {
-                            if (groupqty.compareTo(saledqty.add(qty)) < 0) {
-                                return getErrReturnObject().setErrMsg(itemRow.getString("itemno") + "商品数量已超过促销活动的限定数量【" + groupqty.subtract(saledqty) + "】,无法新建").toString();
-                            }
-                        }
-                    }
-                }
-            }
+            //是否符合商品起订量和增量判断
+            orderItemsHelper.compareQty(type, itemRowsMap, item, itemRow, promotionitems);
 
             BigDecimal price1 = new BigDecimal(0);
-            SQLFactory sqlFactory = new SQLFactory(this, "订单商品明细_新增");
+//            SQLFactory sqlFactory = new SQLFactory(this, "订单商品明细_新增");
+            Boolean isInsert = true;
             JSONArray customproperties = new JSONArray();
             if (sa_orderitemsid <= 0) {
-                Rows rows = dbConnect.runSqlQuery("SELECT sa_orderitemsid,price from sa_orderitems WHERE siteid='" + siteid + "' and itemid=" + itemid + " and sa_orderid=" + sa_orderid);
-
-                Rows customschemeRows = dbConnect.runSqlQuery("select * from sa_customscheme_items where siteid='" + siteid + "'");
-                RowsMap customschemeRowsMap = customschemeRows.toRowsMap("sa_customschemeid");
-
-                if (itemRowsMap.containsKey(item.getString("itemid"))) {
-                    if (itemRowsMap.get(item.getString("itemid")).isNotEmpty()) {
-                        if (itemRowsMap.get(item.getString("itemid")).get(0).getLong("sa_customschemeid") > 0 && !itemRowsMap.get(item.getString("itemid")).get(0).getBoolean("iscustomsize")) {
-                            System.out.println(0000);
-                            if (customschemeRowsMap.containsKey(itemRowsMap.get(item.getString("itemid")).get(0).getString("sa_customschemeid"))) {
-                                System.out.println(111111);
-                                for (Row customschemeItem : (customschemeRowsMap.get(itemRowsMap.get(item.getString("itemid")).get(0).getString("sa_customschemeid")))) {
-                                    System.out.println(customschemeItem.getString("description"));
-                                    System.out.println(2222);
-                                    customproperties.add(customschemeItem.getString("description") + ":" + itemRowsMap.get(item.getString("itemid")).get(0).getString(customschemeItem.getString("value")));
-                                }
-                            }
-                        } else if (itemRowsMap.get(item.getString("itemid")).get(0).getBoolean("iscustomsize")) {
-                            System.out.println(3333);
-                            if (customschemeRowsMap.containsKey(itemRowsMap.get(item.getString("itemid")).get(0).getString("sa_customschemeid"))) {
-                                System.out.println(4444);
-                                for (Row customschemeItem : (customschemeRowsMap.get(itemRowsMap.get(item.getString("itemid")).get(0).getString("sa_customschemeid")))) {
-                                    System.out.println(5555);
-                                    System.out.println((customschemeRowsMap.get(itemRowsMap.get(item.getString("itemid")).get(0).getString("sa_customschemeid"))).size());
-                                    if (!customschemeItem.getString("value").equals("spec")) {
-                                        customproperties.add(customschemeItem.getString("description") + ":" + itemRowsMap.get(item.getString("itemid")).get(0).getString(customschemeItem.getString("value")));
-                                    } else {
-                                        customproperties.add("尺寸:" + item.getString("length") + "*" + item.getString("width"));
-                                    }
-
-                                }
-                            } else {
-                                customproperties.add("尺寸:" + item.getString("length") + "*" + item.getString("width"));
-                            }
-                        }
-
-                    }
+                //获取定制属性
+                customproperties = orderItemsHelper.getCustomProperties(itemRowsMap, item);
 
-                }
+                Rows rows = dbConnect.runSqlQuery("SELECT sa_orderitemsid,price from sa_orderitems WHERE siteid='" + siteid + "' and itemid=" + itemid + " and sa_orderid=" + sa_orderid);
 
                 if (rows.isEmpty()) {
                     sa_orderitemsid = createTableID("sa_orderitems");
@@ -182,135 +110,40 @@ public class OrderItems extends Controller {
                     System.out.println(beans.order.Order.getDefaultIsRepeatValue(siteid, type));
                     if (beans.order.Order.getDefaultIsRepeatValue(siteid, type)) {
                         sa_orderitemsid = createTableID("sa_orderitems");
-                        sqlFactory = new SQLFactory(this, "订单商品明细_新增");
+                        isInsert = true;
                     } else {
                         price1 = rows.get(0).getBigDecimal("price");
                         sa_orderitemsid = rows.get(0).getLong("sa_orderitemsid");
-                        sqlFactory = new SQLFactory(this, "订单商品明细_更新");
+                        isInsert = false;
                     }
 
                 }
             } else {
-                sqlFactory = new SQLFactory(this, "订单商品明细_更新");
+                isInsert = false;
             }
-            sqlFactory.addParameter("customproperties", customproperties.toJSONString().replace("[", "").replace("]", "").replace("\"", ""));
-            sqlFactory.addParameter("siteid", siteid);
-            sqlFactory.addParameter("userid", userid);
-            sqlFactory.addParameter("username", username);
-            sqlFactory.addParameter("sa_orderitemsid", sa_orderitemsid);
-            sqlFactory.addParameter("sa_orderid", sa_orderid);
-            sqlFactory.addParameter("rowno", 1);
-            sqlFactory.addParameter("itemid", itemid);
-            sqlFactory.addParameter("remarks", item.getStringValue("remarks"));
-
-            BigDecimal conversionrate = itemRow.getBigDecimal("conversionrate");
-            if (conversionrate.compareTo(BigDecimal.ZERO) <= 0) {
-                conversionrate = BigDecimal.valueOf(1);
-            }
-            sqlFactory.addParameter("itemno", itemRow.getString("itemno"));
-            sqlFactory.addParameter("itemname", itemRow.getString("itemname"));
-            sqlFactory.addParameter("model", itemRow.getString("model"));
-            sqlFactory.addParameter("unit", itemRow.getString("unit"));
-            sqlFactory.addParameter("auxunit", itemRow.getString("auxunit"));
-            sqlFactory.addParameter("batchcontrol", itemRow.getLong("batchcontrol"));
-            sqlFactory.addParameter("delivery", itemRow.getLong("delivery"));
-
-            sqlFactory.addParameter("stockno", item.getOrDefault("stockno", "null"));
-            sqlFactory.addParameter("position", item.getOrDefault("position", "null"));
-            sqlFactory.addParameter("batchno", item.getOrDefault("batchno", "null"));
-
-            String deliverydate = item.getStringValue("deliverydate");
-            Rows rows = dbConnect.runSqlQuery("SELECT deliverydate,defaultprice from sa_orderitems WHERE sa_orderitemsid =" + sa_orderitemsid);
-            if (deliverydate.isEmpty()) {
-                if (rows.isNotEmpty()) {
-                    deliverydate = rows.get(0).getString("deliverydate");
-                } else {
-                    deliverydate = "null";
-                }
-            }
-            String needdate = item.getStringValue("needdate");
-            sqlFactory.addParameter("needdate", needdate.equals("") ? "null" : needdate);
-            sqlFactory.addParameter("deliverydate", deliverydate.equals("") ? "null" : deliverydate);
-            sqlFactory.addParameter("conversionrate", conversionrate);
-            //订购数量
-            sqlFactory.addParameter("qty", qty);
-            //辅助单位数量
-            sqlFactory.addParameter("auxqty", qty.divide(conversionrate));
+
             BigDecimal defaultprice;
             BigDecimal price;
             //价格
             ItemPrice itemPrice = ItemPrice.getItemPrice(this, sys_enterpriseid, itemid);
             if (!item.containsKey("defaultprice")) {
-                switch (type) {
-//                    case "项目订单":
-//                        defaultprice = itemPrice.getContractprice(sa_contractid);
-//                        break;
-//                    case "工具订单":
-//                        defaultprice = itemPrice.getMarketprice();
-//                        break;
-                    case "促销订单":
-                        defaultprice = itemPrice.getGraderateprice();
-                        price = itemPrice.getPromotionPrice(sa_promotionid);
-                        break;
-                    default:
-                        defaultprice = itemPrice.getGraderateprice();
-                        price = itemPrice.getGraderateprice();
-                        if (itemRowsMap.containsKey(item.getString("itemid"))) {
-                            if (itemRowsMap.get(item.getString("itemid")).isNotEmpty()) {
-                                if (itemRowsMap.get(item.getString("itemid")).get(0).getBoolean("iscustomsize")) {
-                                    System.out.println(price);
-                                    price = price.multiply(item.getBigDecimal("length").multiply(item.getBigDecimal("width")).divide(new BigDecimal("1000000"), 4, BigDecimal.ROUND_HALF_UP));
-                                    System.out.println(price);
-                                }
-
-                            }
-
-                        }
-                }
+                defaultprice = orderItemsHelper.getDefaultprice(itemPrice);
+                price = orderItemsHelper.getPrice(orderRows.get(0), itemPrice, item, itemRowsMap);
             } else {
+                defaultprice = item.getBigDecimalValue("defaultprice");
                 price = item.getBigDecimalValue("price");
                 if (price.compareTo(price1) != 0) {
                     sqlList.add(DataContrlLog.createLog(this, "sa_order", sa_orderid, "订单商品【" + itemRow.getString("itemname") + "】由单价:" + price1 + "修改为单价:" + price, "订单修改单价").getSQL());
                 }
-                defaultprice = item.getBigDecimalValue("defaultprice");
-//                if (!type.equals("特殊订单") && defaultprice.compareTo(rows.get(0).getBigDecimal("defaultprice")) != 0 && defaultprice.compareTo(itemPrice.getContractprice()) < 0) {
-//                    return getErrReturnObject().setErrMsg("调整价格不可小于协议价").toString();
-//                }
             }
-            if (type.equals("特殊订单")) {
-                if (item.containsKey("defaultprice")) {
-                    if (item.getBigDecimalValue("price").compareTo(BigDecimal.ZERO) > 0 && item.getBigDecimalValue("amount").compareTo(BigDecimal.ZERO) <= 0) {
-                        sqlFactory.addParameter("price", item.getBigDecimalValue("price"));
-                        //折前金额(元)
-                        sqlFactory.addParameter("amount", price.multiply(qty));
-                    } else if (item.getBigDecimalValue("price").compareTo(BigDecimal.ZERO) <= 0 && item.getBigDecimalValue("amount").compareTo(BigDecimal.ZERO) > 0) {
-                        sqlFactory.addParameter("price", item.getBigDecimalValue("amount").divide(qty, 6, BigDecimal.ROUND_HALF_UP));
-                        //折前金额(元)
-                        sqlFactory.addParameter("amount", item.getBigDecimalValue("amount"));
-                    } else {
-                        sqlFactory.addParameter("price", price);
-                        //折前金额(元)
-                        sqlFactory.addParameter("amount", price.multiply(qty));
-                    }
-                } else {
-                    sqlFactory.addParameter("price", price);
-                    //折前金额(元)
-                    sqlFactory.addParameter("amount", price.multiply(qty));
-                }
+
+            if (isInsert) {
+                sqlList.add(orderItemsHelper.getInsertSQL(customproperties, sa_orderitemsid, item, itemRow, type, defaultprice, price, itemPrice.getMarketprice()).getSQL());
             } else {
-                sqlFactory.addParameter("price", price);
-                //折前金额(元)
-                sqlFactory.addParameter("amount", price.multiply(qty));
+                sqlList.add(orderItemsHelper.getUpdateSQL(sa_orderitemsid, item, itemRow, type, defaultprice, price).getSQL());
             }
-            //单价,折后价(元),取合同价
-            sqlFactory.addParameter("defaultprice", defaultprice);
-            //金额,折后金额(元)
-            sqlFactory.addParameter("defaultamount", defaultprice.multiply(qty));
-            //牌价、市场价(元),标准订单牌价取商品价格,项目订单取合同里的牌价
-            sqlFactory.addParameter("marketprice", itemPrice.getMarketprice());
 
 
-            sqlList.add(sqlFactory.getSQL());
         }
 
         if (type.equals("促销订单")) {
@@ -715,7 +548,7 @@ public class OrderItems extends Controller {
         object.put("method", "getMsg");
 
         JSONObject content = new JSONObject();
-        content.put("fitemnos",  rows.toJsonArray("itemno"));
+        content.put("fitemnos", rows.toJsonArray("itemno"));
         object.put("content", content);
 
         WebRequest request = new WebRequest();

+ 344 - 0
src/custom/restcontroller/webmanage/sale/order/OrderItemsHelper.java

@@ -0,0 +1,344 @@
+package restcontroller.webmanage.sale.order;
+
+import beans.datacontrllog.DataContrlLog;
+import beans.itemprice.ItemPrice;
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
+import common.BaseClass;
+import common.Controller;
+import common.YosException;
+import common.data.*;
+
+import java.math.BigDecimal;
+import java.util.ArrayList;
+
+public class OrderItemsHelper extends BaseClass {
+
+    Controller controller;
+    JSONObject content;
+
+    public OrderItemsHelper(Controller controller) {
+        this.controller = controller;
+        this.content = controller.content;
+    }
+
+    /**
+     * 是否符合商品起订量和增量判断
+     *
+     * @param type
+     * @param itemRowsMap
+     * @param item
+     * @param itemRow
+     * @param promotionitems
+     * @throws YosException
+     */
+    public void compareQty(String type, RowsMap itemRowsMap, JSONObject item, Row itemRow, Rows promotionitems) throws YosException {
+        BigDecimal qty = item.getBigDecimalValue("qty");
+        String itemid = item.getString("itemid");
+        if (!type.equals("促销订单")) {
+            if (itemRowsMap.containsKey(itemid)) {
+                if (itemRowsMap.get(itemid).isNotEmpty()) {
+                    BigDecimal orderaddqty = itemRowsMap.get(itemid).get(0).getBigDecimal("orderaddqty");
+                    BigDecimal orderminqty = itemRowsMap.get(itemid).get(0).getBigDecimal("orderminqty");
+                    if (((qty.subtract(orderminqty)).remainder(orderaddqty)).compareTo(BigDecimal.ZERO) != 0) {
+                        throw new YosException("品号为" + itemRowsMap.get(itemid).get(0).getString("itemno") + "的商品数量不符合该商品的起订量和增量");
+                    }
+                }
+            }
+        }
+        RowsMap pRowsMap = promotionitems.toRowsMap("itemid");
+        if (type.equals("促销订单")) {
+            if (pRowsMap.containsKey(itemid)) {
+                if (pRowsMap.get(itemid).isNotEmpty()) {
+                    BigDecimal saledqty = pRowsMap.get(itemid).get(0).getBigDecimal("saledqty");
+                    BigDecimal groupqty = pRowsMap.get(itemid).get(0).getBigDecimal("groupqty");
+                    BigDecimal orderaddqty = pRowsMap.get(itemid).get(0).getBigDecimal("orderaddqty");
+                    BigDecimal orderminqty = pRowsMap.get(itemid).get(0).getBigDecimal("orderminqty");
+                    boolean islimit = pRowsMap.get(itemid).get(0).getBoolean("islimit");
+                    if (((qty.subtract(orderminqty)).remainder(orderaddqty)).compareTo(BigDecimal.ZERO) != 0) {
+                        throw new YosException("品号为" + itemRowsMap.get(itemid).get(0).getString("itemno") + "的商品数量不符合该商品的起订量和增量");
+                    }
+                    if (islimit) {
+                        if (groupqty.compareTo(saledqty.add(qty)) < 0) {
+                            throw new YosException(itemRow.getString("itemno") + "商品数量已超过促销活动的限定数量【" + groupqty.subtract(saledqty) + "】,无法新建");
+                        }
+                    }
+                }
+            }
+        }
+    }
+
+
+    /**
+     * 获取定制属性
+     *
+     * @param itemRowsMap
+     * @param item
+     * @return
+     * @throws YosException
+     */
+    public JSONArray getCustomProperties(RowsMap itemRowsMap, JSONObject item) throws YosException {
+
+        JSONArray customproperties = new JSONArray();
+
+        Rows customschemeRows = dbConnect.runSqlQuery("select * from sa_customscheme_items where siteid='" + controller.siteid + "'");
+        RowsMap customschemeRowsMap = customschemeRows.toRowsMap("sa_customschemeid");
+
+        String itemid = item.getString("itemid");
+        if (itemRowsMap.containsKey(itemid)) {
+            if (itemRowsMap.get(itemid).isNotEmpty()) {
+                if (itemRowsMap.get(itemid).get(0).getLong("sa_customschemeid") > 0 && !itemRowsMap.get(itemid).get(0).getBoolean("iscustomsize")) {
+//                    System.out.println(0000);
+                    if (customschemeRowsMap.containsKey(itemRowsMap.get(itemid).get(0).getString("sa_customschemeid"))) {
+//                        System.out.println(111111);
+                        for (Row customschemeItem : (customschemeRowsMap.get(itemRowsMap.get(itemid).get(0).getString("sa_customschemeid")))) {
+//                            System.out.println(customschemeItem.getString("description"));
+//                            System.out.println(2222);
+                            customproperties.add(customschemeItem.getString("description") + ":" + itemRowsMap.get(itemid).get(0).getString(customschemeItem.getString("value")));
+                        }
+                    }
+                } else if (itemRowsMap.get(itemid).get(0).getBoolean("iscustomsize")) {
+//                    System.out.println(3333);
+                    if (customschemeRowsMap.containsKey(itemRowsMap.get(itemid).get(0).getString("sa_customschemeid"))) {
+//                        System.out.println(4444);
+                        for (Row customschemeItem : (customschemeRowsMap.get(itemRowsMap.get(itemid).get(0).getString("sa_customschemeid")))) {
+//                            System.out.println(5555);
+//                            System.out.println((customschemeRowsMap.get(itemRowsMap.get(itemid).get(0).getString("sa_customschemeid"))).size());
+                            if (!customschemeItem.getString("value").equals("spec")) {
+                                customproperties.add(customschemeItem.getString("description") + ":" + itemRowsMap.get(itemid).get(0).getString(customschemeItem.getString("value")));
+                            } else {
+                                customproperties.add("尺寸:" + item.getString("length") + "*" + item.getString("width"));
+                            }
+
+                        }
+                    } else {
+                        customproperties.add("尺寸:" + item.getString("length") + "*" + item.getString("width"));
+                    }
+                }
+
+            }
+
+        }
+
+        return customproperties;
+    }
+
+    /**
+     * 批量查询商品信息
+     *
+     * @param items
+     * @return
+     * @throws YosException
+     */
+    public RowsMap getItemRowsMap(JSONArray items) throws YosException {
+        ArrayList<Long> itemids = new ArrayList<>();
+        for (Object obj : items) {
+            JSONObject item = (JSONObject) obj;
+            itemids.add(item.getLong("itemid"));
+        }
+
+        String sql = "select t1.*,t2.material from plm_item t1 left join plm_itemextend t2 ON t2.itemid = t1.itemid AND t2.siteid = t1.siteid where t1.siteid='" + controller.siteid + "' and t1.itemid in  " + itemids;
+        sql = sql.replace("[", "(").replace("]", ")");
+        return dbConnect.runSqlQuery(sql).toRowsMap("itemid");
+    }
+
+    public UpdateSQL getUpdateSql() throws YosException {
+        System.err.println("123");
+        UpdateSQL updateSQL = SQLFactory.createUpdateSQL(controller, "sa_order");
+        if (controller.content.containsKey("sa_order_v")) {
+            updateSQL.setDataVersion(content.getIntValue("sa_order_v"));
+        }
+        updateSQL.setValue("changeby",controller.username);
+        updateSQL.setWhere("sa_orderid", content.getLongValue("sa_orderid"));
+        updateSQL.setWhere("siteid", controller.siteid);
+
+        System.err.println(updateSQL.getSQL());
+
+        return updateSQL;
+
+    }
+
+
+    String sa_orderitems = "sa_orderitems";
+
+    public InsertSQL getInsertSQL(JSONArray customproperties, long sa_orderitemsid, JSONObject item, Row itemRow, String type, BigDecimal defaultprice, BigDecimal price, BigDecimal marketprice) throws YosException {
+        Long itemid = item.getLong("itemid");
+        BigDecimal qty = item.getBigDecimalValue("qty");
+
+        InsertSQL insertSQL = SQLFactory.createInsertSQL(controller, sa_orderitems);
+        insertSQL.setValue("customproperties", customproperties.toJSONString().replace("[", "").replace("]", "").replace("\"", ""));
+        insertSQL.setValue("siteid", controller.siteid);
+        insertSQL.setValue("sa_orderitemsid", sa_orderitemsid);
+        insertSQL.setValue("sa_orderid", content.getLongValue("sa_orderid"));
+        insertSQL.setValue("rowno", 1);
+        insertSQL.setValue("itemid", itemid);
+        insertSQL.setValue("remarks", item.getStringValue("remarks"));
+        insertSQL.setValue("itemno", itemRow.getString("itemno"));
+        insertSQL.setValue("itemname", itemRow.getString("itemname"));
+        insertSQL.setValue("model", itemRow.getString("model"));
+        insertSQL.setValue("unit", itemRow.getString("unit"));
+        insertSQL.setValue("auxunit", itemRow.getString("auxunit"));
+        insertSQL.setValue("batchcontrol", itemRow.getLong("batchcontrol"));
+        insertSQL.setValue("stockno", item.getOrDefault("stockno", "null"));
+        insertSQL.setValue("position", item.getOrDefault("position", "null"));
+        insertSQL.setValue("batchno", item.getOrDefault("batchno", "null"));
+
+        String deliverydate = item.getStringValue("deliverydate");
+        Rows rows = dbConnect.runSqlQuery("SELECT deliverydate,defaultprice from sa_orderitems WHERE sa_orderitemsid =" + sa_orderitemsid);
+        if (deliverydate.isEmpty()) {
+            if (rows.isNotEmpty()) {
+                deliverydate = rows.get(0).getString("deliverydate");
+            } else {
+                deliverydate = "null";
+            }
+        }
+        String needdate = item.getStringValue("needdate");
+        insertSQL.setValue("needdate", needdate.equals("") ? "null" : needdate);
+        insertSQL.setValue("deliverydate", deliverydate.equals("") ? "null" : deliverydate);
+        BigDecimal conversionrate = itemRow.getBigDecimal("conversionrate");
+        if (conversionrate.compareTo(BigDecimal.ZERO) <= 0) {
+            conversionrate = BigDecimal.valueOf(1);
+        }
+        insertSQL.setValue("conversionrate", conversionrate);
+        //订购数量
+        insertSQL.setValue("qty", qty);
+        //辅助单位数量
+        insertSQL.setValue("auxqty", qty.divide(conversionrate));
+        if (type.equals("特殊订单")) {
+            if (item.containsKey("defaultprice")) {
+                if (item.getBigDecimalValue("price").compareTo(BigDecimal.ZERO) > 0 && item.getBigDecimalValue("amount").compareTo(BigDecimal.ZERO) <= 0) {
+                    insertSQL.setValue("price", item.getBigDecimalValue("price"));
+                    //折前金额(元)
+                    insertSQL.setValue("amount", price.multiply(qty));
+                } else if (item.getBigDecimalValue("price").compareTo(BigDecimal.ZERO) <= 0 && item.getBigDecimalValue("amount").compareTo(BigDecimal.ZERO) > 0) {
+                    insertSQL.setValue("price", item.getBigDecimalValue("amount").divide(qty, 6, BigDecimal.ROUND_HALF_UP));
+                    //折前金额(元)
+                    insertSQL.setValue("amount", item.getBigDecimalValue("amount"));
+                } else {
+                    insertSQL.setValue("price", price);
+                    //折前金额(元)
+                    insertSQL.setValue("amount", price.multiply(qty));
+                }
+            } else {
+                insertSQL.setValue("price", price);
+                //折前金额(元)
+                insertSQL.setValue("amount", price.multiply(qty));
+            }
+        } else {
+            insertSQL.setValue("price", price);
+            //折前金额(元)
+            insertSQL.setValue("amount", price.multiply(qty));
+        }
+        //单价,折后价(元),取合同价
+        insertSQL.setValue("defaultprice", defaultprice);
+        //金额,折后金额(元)
+        insertSQL.setValue("defaultamount", defaultprice.multiply(qty));
+        //牌价、市场价(元),标准订单牌价取商品价格,项目订单取合同里的牌价
+        insertSQL.setValue("marketprice", marketprice);
+
+        return insertSQL;
+    }
+
+    public UpdateSQL getUpdateSQL(long sa_orderitemsid, JSONObject item, Row itemRow, String type, BigDecimal defaultprice, BigDecimal price) throws YosException {
+        BigDecimal qty = item.getBigDecimalValue("qty");
+
+        UpdateSQL updateSQL = SQLFactory.createUpdateSQL(controller, sa_orderitems);
+        //订购数量
+        updateSQL.setValue("qty", qty);
+        //辅助单位数量
+        BigDecimal conversionrate = itemRow.getBigDecimal("conversionrate");
+        if (conversionrate.compareTo(BigDecimal.ZERO) <= 0) {
+            conversionrate = BigDecimal.valueOf(1);
+        }
+        updateSQL.setValue("auxqty", qty.divide(conversionrate));
+        updateSQL.setValue("undeliqty", qty.subtract(itemRow.getBigDecimal("delivery")));
+        updateSQL.setValue("remarks", item.getStringValue("remarks"));
+        updateSQL.setValue("stockno", item.getOrDefault("stockno", "null"));
+        updateSQL.setValue("position", item.getOrDefault("position", "null"));
+        updateSQL.setValue("batchno", item.getOrDefault("batchno", "null"));
+
+        String deliverydate = item.getStringValue("deliverydate");
+        Rows rows = dbConnect.runSqlQuery("SELECT deliverydate,defaultprice from sa_orderitems WHERE sa_orderitemsid =" + sa_orderitemsid);
+        if (deliverydate.isEmpty()) {
+            if (rows.isNotEmpty()) {
+                deliverydate = rows.get(0).getString("deliverydate");
+            } else {
+                deliverydate = "null";
+            }
+        }
+        String needdate = item.getStringValue("needdate");
+        updateSQL.setValue("needdate", needdate.equals("") ? "null" : needdate);
+        updateSQL.setValue("deliverydate", deliverydate.equals("") ? "null" : deliverydate);
+
+
+        if (type.equals("特殊订单")) {
+            if (item.containsKey("defaultprice")) {
+                if (item.getBigDecimalValue("price").compareTo(BigDecimal.ZERO) > 0 && item.getBigDecimalValue("amount").compareTo(BigDecimal.ZERO) <= 0) {
+                    updateSQL.setValue("price", item.getBigDecimalValue("price"));
+                    //折前金额(元)
+                    updateSQL.setValue("amount", price.multiply(qty));
+                } else if (item.getBigDecimalValue("price").compareTo(BigDecimal.ZERO) <= 0 && item.getBigDecimalValue("amount").compareTo(BigDecimal.ZERO) > 0) {
+                    updateSQL.setValue("price", item.getBigDecimalValue("amount").divide(qty, 6, BigDecimal.ROUND_HALF_UP));
+                    //折前金额(元)
+                    updateSQL.setValue("amount", item.getBigDecimalValue("amount"));
+                } else {
+                    updateSQL.setValue("price", price);
+                    //折前金额(元)
+                    updateSQL.setValue("amount", price.multiply(qty));
+                }
+            } else {
+                updateSQL.setValue("price", price);
+                //折前金额(元)
+                updateSQL.setValue("amount", price.multiply(qty));
+            }
+        } else {
+            updateSQL.setValue("price", price);
+            //折前金额(元)
+            updateSQL.setValue("amount", price.multiply(qty));
+        }
+        //单价,折后价(元),取合同价
+        updateSQL.setValue("defaultprice", defaultprice);
+        //金额,折后金额(元)
+        updateSQL.setValue("defaultamount", defaultprice.multiply(qty));
+
+
+        updateSQL.setWhere("siteid", controller.siteid);
+        updateSQL.setWhere("sa_orderitemsid", sa_orderitemsid);
+        if (item.containsKey("sa_orderitems_v")) {
+            updateSQL.setDataVersion(item.getIntValue("sa_orderitems_v"));
+        }
+
+
+        return updateSQL;
+    }
+
+    public BigDecimal getDefaultprice(ItemPrice itemPrice) throws YosException {
+        return itemPrice.getGraderateprice();
+
+    }
+
+    public BigDecimal getPrice(Row orderRow, ItemPrice itemPrice, JSONObject item, RowsMap itemRowsMap) throws YosException {
+        String type = orderRow.getString("type");
+        Long sa_promotionid = orderRow.getLong("sa_promotionid");
+        BigDecimal price;
+        switch (type) {
+            case "促销订单":
+                price = itemPrice.getPromotionPrice(sa_promotionid);
+                break;
+            default:
+                price = itemPrice.getGraderateprice();
+                if (itemRowsMap.containsKey(item.getString("itemid"))) {
+                    if (itemRowsMap.get(item.getString("itemid")).isNotEmpty()) {
+                        if (itemRowsMap.get(item.getString("itemid")).get(0).getBoolean("iscustomsize")) {
+                            price = price.multiply(item.getBigDecimal("length").multiply(item.getBigDecimal("width")).divide(new BigDecimal("1000000"), 4, BigDecimal.ROUND_HALF_UP));
+                        }
+
+                    }
+                }
+        }
+
+        return price;
+    }
+
+
+}