package restcontroller.sale.shoppingcart; import beans.datacontrllog.DataContrlLog; import beans.itemprice.ItemPrice; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import common.Controller; import common.YosException; import common.annotation.API; import common.annotation.CACHEING_CLEAN; import common.data.Row; import common.data.Rows; import common.data.RowsMap; import common.data.SQLFactory; import restcontroller.R; import java.io.IOException; import java.math.BigDecimal; import java.util.ArrayList; import java.util.HashMap; @API(title = "购物车管理") public class ShoppingCart extends Controller { /** * 构造函数 * * @param content */ public ShoppingCart(JSONObject content) throws YosException { super(content); } @API(title = "新增或更新", apiversion = R.ID20220924095102.v1.class) @CACHEING_CLEAN(apiversions = {R.ID20220924095302.class}) public String insertOrUpdate() throws YosException { Long sa_brandid = content.getLongValue("sa_brandid"); Long itemid = content.getLong("itemid"); BigDecimal qty = content.getBigDecimal("qty"); BigDecimal length = content.getBigDecimal("length"); BigDecimal width = content.getBigDecimal("width"); String itemno = content.getString("itemno"); String tradefield = content.getStringValue("tradefield"); SQLFactory sqlFactory = new SQLFactory(this, "购物车新增"); Long sa_shoppingcartid = 0L; Rows rows = dbConnect.runSqlQuery("SELECT sa_shoppingcartid FROM sa_shoppingcart WHERE siteid = '" + siteid + "' AND sa_brandid = " + sa_brandid + " and length="+length+" and width="+width+" AND itemid = " + itemid + " AND sys_enterpriseid = " + sys_enterpriseid + " AND tradefield = '" + tradefield + "' AND itemno = '" + itemno + "'"); if (rows.isNotEmpty()) { sa_shoppingcartid = rows.get(0).getLong("sa_shoppingcartid"); } ArrayList sqlList = new ArrayList<>(); if (sa_shoppingcartid <= 0) { sa_shoppingcartid = createTableID("sa_shoppingcart"); sqlList.add(DataContrlLog.createLog(this, "sa_shoppingcart", sa_shoppingcartid, "新增", "购物车新增" + sa_shoppingcartid).getSQL()); } else { sqlFactory = new SQLFactory(this, "购物车更新"); sqlList.add(DataContrlLog.createLog(this, "sa_shoppingcart", sa_shoppingcartid, "更新", "购物车更新" + sa_shoppingcartid).getSQL()); } sqlFactory.addParameter("siteid", siteid); sqlFactory.addParameter("itemid", itemid); sqlFactory.addParameter("username", username); sqlFactory.addParameter("userid", userid); sqlFactory.addParameter("itemno", itemno); sqlFactory.addParameter("length", length); sqlFactory.addParameter("width", width); sqlFactory.addParameter("tradefield", tradefield); sqlFactory.addParameter("sa_shoppingcartid", sa_shoppingcartid); sqlFactory.addParameter("sa_brandid", sa_brandid); sqlFactory.addParameter("qty", qty); sqlFactory.addParameter("sys_enterpriseid", sys_enterpriseid); sqlList.add(sqlFactory.getSQL()); dbConnect.runSqlUpdate(sqlList); content.put("sa_shoppingcartid", sa_shoppingcartid); return queryDetail(); } @API(title = "更新数量", apiversion = R.ID20220924104302.v1.class) @CACHEING_CLEAN(apiversions = {R.ID20220924095302.class}) public String updateQty() throws YosException { Long sa_shoppingcartid = content.getLong("sa_shoppingcartid"); BigDecimal qty = content.getBigDecimal("qty"); BigDecimal length = content.getBigDecimal("length"); BigDecimal width = content.getBigDecimal("width"); ArrayList sqlList = new ArrayList<>(); SQLFactory sqlFactory = new SQLFactory(this, "购物车更新数量"); sqlFactory.addParameter("siteid", siteid); sqlFactory.addParameter("username", username); sqlFactory.addParameter("sa_shoppingcartid", sa_shoppingcartid); sqlFactory.addParameter("qty", qty); sqlFactory.addParameter("length", length); sqlFactory.addParameter("width", width); sqlList.add(sqlFactory.getSQL()); //操作记录 sqlList.add(DataContrlLog.createLog(this, "sa_shoppingcart", sa_shoppingcartid, "更新数量", "更新数量" + qty).getSQL()); dbConnect.runSqlUpdate(sqlList); content.put("sa_shoppingcartid", sa_shoppingcartid); return queryDetail(); } @API(title = "删除", apiversion = R.ID20220924095202.v1.class) @CACHEING_CLEAN(apiversions = {R.ID20220924095302.class}) public String delete() throws YosException { JSONArray jsonArray = content.getJSONArray("sa_shoppingcartids"); ArrayList sqlList = new ArrayList<>(); SQLFactory sqlFactory = new SQLFactory(this, "购物车删除"); sqlFactory.addParameter_in("sa_shoppingcartid", jsonArray.toArray()); sqlFactory.addParameter("siteid", siteid); sqlList.add(sqlFactory.getSQL()); for (Object obj : jsonArray) { //操作记录 sqlList.add(DataContrlLog.createLog(this, "sa_shoppingcart", Long.valueOf(obj.toString()), "删除", "删除" + obj).getSQL()); } dbConnect.runSqlUpdate(sqlFactory); return getSucReturnObject().toString(); } @API(title = "购物车列表", apiversion = R.ID20220924095302.v1.class) public String queryList() throws YosException, IOException { StringBuffer where = new StringBuffer(" 1=1 "); if (content.containsKey("where")) { JSONObject whereObject = content.getJSONObject("where"); if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) { where.append(" and("); where.append("t1.itemno like'%").append(whereObject.getString("condition")).append("%' "); where.append("or t2.brandname like'%").append(whereObject.getString("condition")).append("%' "); where.append("or t3.itemname like'%").append(whereObject.getString("condition")).append("%' "); where.append("or t3.model like'%").append(whereObject.getString("condition")).append("%' "); where.append("or t3.spec like'%").append(whereObject.getString("condition")).append("%' "); where.append(")"); } } boolean isExport = content.getBooleanValue("isExport"); Long istool = content.getLongValue("istool"); SQLFactory sqlFactory = new SQLFactory(this, "购物车列表", pageSize, pageNumber, pageSorting); if (isExport) { sqlFactory = new SQLFactory(this, "购物车列表"); } sqlFactory.addParameter("siteid", siteid); sqlFactory.addParameter("sys_enterpriseid", sys_enterpriseid); sqlFactory.addParameter("istool", istool); sqlFactory.addParameter_SQL("where", where); String sql = sqlFactory.getSQL(); Rows rows = dbConnect.runSqlQuery(sql); ArrayList ids = rows.toArrayList("itemid", new ArrayList<>()); //默认商品图片 Rows defaultImageRows = getAttachmentUrl("system", (long) 1, "defaultImage"); //附件 RowsMap attRowsMap = getAttachmentUrl("plm_item", ids); //商品领域 RowsMap tradefieldRowsMap = beans.Item.Item.getTradefieldRowsMap(this, ids); for (Row row : rows) { if ((attRowsMap.getOrDefault(row.getString("itemid"), new Rows())).isEmpty()) { row.put("attinfos", defaultImageRows); } else { row.put("attinfos", attRowsMap.getOrDefault(row.getString("itemid"), new Rows())); } try { ItemPrice itemPrice = ItemPrice.getItemPrice(this, sys_enterpriseid, row.getLong("itemid")); row.put("oldprice", istool == 0 ?itemPrice.getContractprice():itemPrice.getMarketprice()); // row.put("gradeprice", istool == 0 ?itemPrice.getContractprice():itemPrice.getMarketprice()); } catch (Exception e) { e.printStackTrace(); row.put("oldprice", 0); // row.put("gradeprice", 0); } row.put("tradefield", tradefieldRowsMap.getOrDefault(row.getString("itemid"), new Rows())); } // if (isExport) { // //去除不需要导出项 // String[] removeFieldList = {"sa_shoppingcartid", "sa_brandid", "itemid", "unitgroupname", "orderminqty", "orderaddqty", "orderminqty_auxunit", "orderaddqty_auxunit"}; // for (String key : removeFieldList) { // rows.getFieldList().remove(key); // } // // Rows uploadRows = uploadExcelToObs("order", "订单商品明细_列表", rows, getTitleMap()); // return getSucReturnObject().setData(uploadRows).toString(); // } return getSucReturnObject().setData(rows).toString(); } //返回导出的标题 public HashMap getTitleMap() { HashMap titleMap = new HashMap<>(); titleMap.put("qty", "数量"); titleMap.put("itemno", "产品编号"); titleMap.put("tradefield", "领域"); titleMap.put("brandname", "品牌"); titleMap.put("itemname", "产品名称"); titleMap.put("model", "型号"); titleMap.put("spec", "规格尺寸"); titleMap.put("unitname", "主单位"); titleMap.put("auxunitname", "辅助单位"); return titleMap; } @API(title = "购物车数量", apiversion = R.ID20220927093202.v1.class) public String getNum() throws YosException { Rows rows = dbConnect.runSqlQuery("SELECT COUNT(0) num FROM sa_shoppingcart t1 WHERE t1.sys_enterpriseid=" + sys_enterpriseid + " AND t1.siteid= '" + siteid + "'"); return getSucReturnObject().setData(rows.get(0)).toString(); } @API(title = "详情", apiversion = R.ID20220924095402.v1.class) public String queryDetail() throws YosException { Long sa_shoppingcartid = content.getLong("sa_shoppingcartid"); SQLFactory sqlFactory = new SQLFactory(this, "购物车详情", pageSize, pageNumber, pageSorting); sqlFactory.addParameter("siteid", siteid); sqlFactory.addParameter("sys_enterpriseid", sys_enterpriseid); sqlFactory.addParameter("sa_shoppingcartid", sa_shoppingcartid); Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL()); ArrayList ids = rows.toArrayList("itemid", new ArrayList<>()); //附件 RowsMap attRowsMap = getAttachmentUrl("plm_item", ids); for (Row row : rows) { row.put("attinfos", attRowsMap.getOrDefault(row.getString("itemid"), new Rows())); try { ItemPrice itemPrice = ItemPrice.getItemPrice(this, sys_enterpriseid, row.getLong("itemid")); row.put("oldprice", itemPrice.getPrice()); row.put("gradeprice", itemPrice.getGraderateprice()); } catch (Exception e) { e.printStackTrace(); row.put("oldprice", 0); row.put("gradeprice", 0); } } return getSucReturnObject().setData(rows.isNotEmpty() ? rows.get(0) : new Row()).toString(); } }