diff --git a/ssjygl-xsx-common/src/main/java/com/cowr/ssjygl/supermarket/SupermarketService.java b/ssjygl-xsx-common/src/main/java/com/cowr/ssjygl/supermarket/SupermarketService.java index 31ff117..c3cbd40 100644 --- a/ssjygl-xsx-common/src/main/java/com/cowr/ssjygl/supermarket/SupermarketService.java +++ b/ssjygl-xsx-common/src/main/java/com/cowr/ssjygl/supermarket/SupermarketService.java @@ -118,7 +118,7 @@ public class SupermarketService extends BaseService { } Record out = supermarket.toRecord(); - out.set("products", Db.find("select t.supermarket_id, p.id, p.name, t.unit_price from supermarket_product t\n" + + out.set("products", Db.find("select t.supermarket_id, p.id, p.name, t.unit_price,t.product_id from supermarket_product t\n" + " left join product p on t.product_id = p.id \n" + " where t.supermarket_id = ?", id)); diff --git a/ssjygl-xsx-service/src/main/java/com/cowr/service/ssjygl/supermarket/SupEditProductValidator.java b/ssjygl-xsx-service/src/main/java/com/cowr/service/ssjygl/supermarket/SupEditProductValidator.java new file mode 100644 index 0000000..5d0394c --- /dev/null +++ b/ssjygl-xsx-service/src/main/java/com/cowr/service/ssjygl/supermarket/SupEditProductValidator.java @@ -0,0 +1,18 @@ +package com.cowr.service.ssjygl.supermarket; + +import com.cowr.common.validator.CrudParamValidator; +import com.cowr.common.view.Result; +import com.jfinal.core.Controller; + +public class SupEditProductValidator extends CrudParamValidator { + @Override + protected void validate(Controller c) { + validateRequired("supermarket_id", "supermarket_id", "supermarket_id 必填"); + validateRequired("product_id", "product_id", "product_id 必填"); + validateRequired("unit_price", "unit_price", "unit_price 必填"); + } + + protected void handleError(Controller c) { + c.renderJson(Result.failed(getErrmsg())); + } +} \ No newline at end of file diff --git a/ssjygl-xsx-service/src/main/java/com/cowr/service/ssjygl/supermarket/SupermarketController.java b/ssjygl-xsx-service/src/main/java/com/cowr/service/ssjygl/supermarket/SupermarketController.java index 84bc808..d0f5abe 100644 --- a/ssjygl-xsx-service/src/main/java/com/cowr/service/ssjygl/supermarket/SupermarketController.java +++ b/ssjygl-xsx-service/src/main/java/com/cowr/service/ssjygl/supermarket/SupermarketController.java @@ -125,4 +125,21 @@ public class SupermarketController extends BaseController { renderJson(SupermarketSyncService.me.removeProduct(supermarket_id, product_id, tokenuser)); } + + + + @Before(SupEditProductValidator.class) + public void editUnitPrice() { + Sysuser tokenuser = SysuserSyncService.me.getSysuserByToken(get("token")); + + if (tokenuser == null) { + renderJson(Result.noauth()); + return; + } + + int supermarket_id = getInt("supermarket_id"); + int product_id = getInt("product_id"); + Double unit_price = getParaToDouble("unit_price"); + renderJson(SupermarketSyncService.me.editUnitPrice(supermarket_id, product_id,unit_price, tokenuser)); + } } diff --git a/ssjygl-xsx-service/src/main/java/com/cowr/service/ssjygl/supermarket/SupermarketSyncService.java b/ssjygl-xsx-service/src/main/java/com/cowr/service/ssjygl/supermarket/SupermarketSyncService.java index f7f338c..eb1eab8 100644 --- a/ssjygl-xsx-service/src/main/java/com/cowr/service/ssjygl/supermarket/SupermarketSyncService.java +++ b/ssjygl-xsx-service/src/main/java/com/cowr/service/ssjygl/supermarket/SupermarketSyncService.java @@ -1,5 +1,6 @@ package com.cowr.service.ssjygl.supermarket; +import com.cowr.common.base.BaseModel; import com.cowr.common.enums.Enums; import com.cowr.common.view.Result; import com.cowr.model.*; @@ -14,6 +15,7 @@ import com.jfinal.plugin.activerecord.IAtom; import java.math.BigDecimal; import java.util.HashMap; import java.util.List; +import java.util.Map; public class SupermarketSyncService extends BaseSyncService { private static Log log = Log.getLog(SupermarketSyncService.class); @@ -150,4 +152,74 @@ public class SupermarketSyncService extends BaseSyncService { return Result.object(ret); } + + + public Result editUnitPrice(int supermarket_id, int product_id,Double unit_price, Sysuser sysuser) { + SupermarketProduct sp = SupermarketProduct.dao.findByIds(supermarket_id, product_id); + BaseModel saveold = (BaseModel) sp.clone(); + if (sp == null) { + return Result.failed("商品配置信息不存在"); + } + if(unit_price==null ||unit_price<=0){ + return Result.failed("商品单价不能小于0"); + } + sp.setUnitPrice(BigDecimal.valueOf(unit_price)); + String sql="select t.* from ordercluster t where t.state<5 and t.supermarket_id=? and product_id=? " + + "and t.id not in (select b.id from customer_supermarket_product a," + + "ordercluster b where a.customer_id=b.customer_id and a.supermarket_id=b.supermarket_id and a.product_id=b.product_id)"; + List orderclusterList=Ordercluster.dao.find(sql,supermarket_id, product_id); + Map map = new HashMap<>(); + if (orderclusterList!=null &&!orderclusterList.isEmpty()) { + for (Ordercluster ordercluster : orderclusterList) { + ordercluster.setUnitPrice(BigDecimal.valueOf(unit_price)); + if (!map.containsKey(ordercluster.getId())) { + map.put(ordercluster.getSupermarketId(), new SyncTask()); + } + map.get(ordercluster.getSupermarketId()).addUpdateData(ordercluster); + } + } + boolean ret = Db.tx(new IAtom() { + @Override + public boolean run() { + try { + boolean ret = true; + SyncTask synctask = new SyncTask(); + synctask.addUpdateData(sp); + + Stock stock = Stock.dao.findByIds(supermarket_id, product_id); + + if (stock != null) { + stock.setStockWeight(new BigDecimal(0)); + synctask.addUpdateData(stock); + + ret = stock.update(); + } + + if (orderclusterList!=null &&!orderclusterList.isEmpty()) { + int[] editret = Db.batchUpdate(orderclusterList, orderclusterList.size()); + for (int i : editret) { + // 必须是每条 sql 修改一条记录 + if (i != 1) { + return false; + } + } + // 将单价同步到不同的砂站 + for (Map.Entry entry : map.entrySet()) { + if (!SyncTaskService.me.save(entry.getValue(), entry.getKey())) { + return false; + } + } + } + return ret && sp.update() + && SyncTaskService.me.save(synctask) + && ModifyLogService.me.save(sp, saveold, Enums.DataOpType.UPDATE.getId(), sysuser); + } catch (Exception e) { + log.error(e.getMessage(), e); + return false; + } + } + }); + + return Result.object(ret); + } }