Commit 5cafdfaf authored by zhangyong's avatar zhangyong

1

parent a65c2b03
package com.egolm.shop.controller; package com.egolm.shop.controller;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
...@@ -25,6 +28,26 @@ public class CartController { ...@@ -25,6 +28,26 @@ public class CartController {
@Autowired @Autowired
private CartService cartService; private CartService cartService;
@ApiOperation("购物车信息")
@ApiImplicitParams({
@ApiImplicitParam(paramType = "query", name="shopNO", dataType = "String", required = true, value = "店铺编号", defaultValue = ""),
@ApiImplicitParam(paramType = "query", name = "terminal", dataType = "String", required = false, value = "终端标识", defaultValue = "wechat"),
@ApiImplicitParam(paramType = "query", name = "langID", dataType = "String", required = false, value = "语言", defaultValue = "936"),
})
@RequestMapping(value = "/cartQuery",method=RequestMethod.GET)
public String cartQuery(HttpServletRequest request, HttpServletResponse response) {
Map<String,Object> params = new HashMap<String,Object>();
params.put("shopNO",request.getParameter("shopNO"));
params.put("terminal",request.getParameter("terminal"));
params.put("langID",request.getParameter("langID"));
return cartService.query(params);
}
@ApiOperation("加入购物车(JSON格式)") @ApiOperation("加入购物车(JSON格式)")
@ApiImplicitParams({ @ApiImplicitParams({
@ApiImplicitParam(paramType = "query", dataType = "String", required = false, value = "JSON格式", defaultValue = "{\"shopNO\": \"00029975\",\"terminal\": \"wechat\",\"langID\": \"936\",\"cartList\": [{\"goodsID\": \"30771\",\"goodsTypeID\": 0,\"saleQty\": \"24\",\"agentContractNO\": \"600327002\",\"promoGroupNO\":\"\",\"promoPaperNO\":\"00001810100007\" }]}"), @ApiImplicitParam(paramType = "query", dataType = "String", required = false, value = "JSON格式", defaultValue = "{\"shopNO\": \"00029975\",\"terminal\": \"wechat\",\"langID\": \"936\",\"cartList\": [{\"goodsID\": \"30771\",\"goodsTypeID\": 0,\"saleQty\": \"24\",\"agentContractNO\": \"600327002\",\"promoGroupNO\":\"\",\"promoPaperNO\":\"00001810100007\" }]}"),
......
package com.egolm.shop.service;
import java.util.Map;
import com.alibaba.fastjson.JSONObject;
public interface CartRedisService {
public void reCache(String shopNo, String terminal);
public void changeQty(String shopNo, String terminal, Integer nIdx, Double nSaleQty);
public Map<String, Object> querySync(String shopNo, String terminal) ;
}
package com.egolm.shop.service; package com.egolm.shop.service;
import java.util.Map;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
public interface CartService { public interface CartService {
...@@ -9,4 +11,6 @@ public interface CartService { ...@@ -9,4 +11,6 @@ public interface CartService {
public String remove(JSONObject obj); public String remove(JSONObject obj);
public String updateQty (JSONObject obj); public String updateQty (JSONObject obj);
public String query (Map<String,Object> params);
} }
package com.egolm.shop.service.impl;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import com.egolm.common.GsonUtil;
import com.egolm.common.MathUtil;
import com.egolm.common.StringUtil;
import com.egolm.common.Util;
import com.egolm.common.bean.Rjx;
import com.egolm.common.exception.ReflectException;
import com.egolm.common.jdbc.JdbcTemplate;
import com.egolm.common.jdbc.ResultMutil;
import com.egolm.shop.service.CartRedisService;
/**
* 购物车数据缓存服务
* @ClassName: CartRedisServiceImpl
* @Description:TODO(这里用一句话描述这个类的作用)
* @author: zhang.yong
* @date: 2018年10月11日 上午10:41:30
*
*/
@Service
public class CartRedisServiceImpl implements CartRedisService {
private static final Log logger = LogFactory.getLog(CartRedisService.class);
private static boolean useCache = false;
@Autowired
private RedisTemplate<String, String> redisTemplate;
@Autowired
private JdbcTemplate jdbcTemplate;
@Async
public void changeQty(String shopNo, String terminal, Integer nIdx, Double nSaleQty) {
if (useCache) {
String json = (String) redisTemplate.opsForHash().get("CART_" + shopNo, terminal);
if (json == null) {
this.querySync(shopNo, terminal);
} else {
Rjx rjx = Rjx.json(GsonUtil.toMap(json));
Map<String, Object> shopGoodsObj = (Map) rjx.get("shopGoodsObj");
for (String key : shopGoodsObj.keySet()) {
Map<String, Object> obj = (Map) shopGoodsObj.get(key);
Integer nIdx_ = (Integer) obj.get("nIdx");
if (nIdx_.intValue() == nIdx.intValue()) {
obj.put("saleQty", nSaleQty);
this.processPayAmount(rjx);
this.cacheData(shopNo, terminal, rjx);
}
}
}
}
}
public void clearCache(String shopNo) {
redisTemplate.delete("CART_" + shopNo);
}
@Async
public void reCache(String shopNo, String terminal) {
if (useCache) {
redisTemplate.delete("CART_" + shopNo);
this.querySync(shopNo, terminal);
logger.info("刷新购物车缓存全量缓存:sShopNO=" + shopNo + ",sTerminalType=" + terminal);
}
}
@Async
public void queryAsync(String shopNo, String terminal) {
querySync(shopNo, terminal);
}
public Map<String, Object> querySync(String shopNo, String terminal) {
Integer queryCount = 0;
System.out.println("useCache: " + useCache);
logger.info("useCache: " + useCache);
while (useCache) {
queryCount++;
String json = (String) this.redisTemplate.opsForHash().get("CART_" + shopNo, terminal);
if (json == null) {
this.cacheTmpData(shopNo, terminal);
break;
} else {
Map<String, Object> cache = GsonUtil.toMap(json);
Double nCacheStatus = (Double) cache.get("nCacheStatus");
if (nCacheStatus == null) {
this.redisTemplate.expire("CART_" + shopNo, 5, TimeUnit.MINUTES);
logger.info("购物车查询缓存命中:sShopNO=" + shopNo + ",sTerminalType=" +terminal);
return GsonUtil.toMap(json);
} else if (nCacheStatus == 1D) {
if (queryCount >= 4) {
this.clearCache(shopNo);
break;
} else {
try {
Thread.sleep(500L);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
Rjx rjx = Rjx.json().set("IsValid", true);
if (!StringUtil.isNotUndefinedAndNull(shopNo, terminal)) {
throw new ReflectException("Query the shopping cart parameter error:"
+ Rjx.json().set("terminal", terminal).set("shopNo", shopNo).jsonValue());
}
List<Map<String, Object>> datas = this.getGoodsList(shopNo, terminal);
System.out.println("datas: " + GsonUtil.toJson(datas));
Set<String> chooseGroups = new HashSet<String>();
Set<String> chooseGifts = new HashSet<String>();
Set<String> promosNo = new HashSet<String>();
if (datas != null && datas.size() > 0) {
for (Map<String, Object> data : datas) {
Integer nTag_CG = (Integer) data.get("nTag_CG");
if (nTag_CG != null && (nTag_CG & 16) == 16) {
data.put("published", 1);
} else {
data.put("published", 0);
}
String sAgentContractNO = (String) data.get("sAgentContractNO");
Integer nGoodsID = (Integer) data.get("nGoodsID");
Integer nTag = (Integer) data.get("nTag");
String sPromoNo = (String) data.get("sPromoPaperNO");
String sGroupNo = (String) data.get("sPromoGroupNO");
promosNo.add(sPromoNo);
if (nTag == 1) {
chooseGifts.add(sAgentContractNO + "$" + nGoodsID);
}
if (StringUtil.isNotEmpty(sPromoNo, sGroupNo)) {
String groupKey = sPromoNo + "$" + sGroupNo;
chooseGroups.add(groupKey);
}
}
List<Map<String, Object>> wmsList = getWmsList(datas);
Util.replaceKey(datas, ":", false, "sShopNO:shopNo", "sAgentContractNO:contractNo", "nGoodsID:goodsId",
"sGoodsDesc:goodsName", "nSaleQty:saleQty", "sPromoPaperNO:promoNo",
"sPromoActionTypeID:promoTypeId", "nMinSaleQty:minOrderQty", "sUnit:priceUnit",
"nSaleUnits:orderUnit", "nRealSalePrice:price", "sSpec:basicInfoSpec", "sURL:imgPath",
"sCategoryNO:categoryNo", "sPromoName:promoMsg", "nAgentID:agentId", "sAgentName:agentName",
"dLastUpdateTime:lastUpdateTime", "sWarehouseNO:warehouseNo", "sBrandID:brandId",
"nStockQty:stockQty", "sBrand:brandName", "nTag:goodsType", "sPromoGroupNO:groupNo");
Util.removeKey(datas, "sShopNO", "sAgentContractNO", "nGoodsID", "nGoodsID_G", "nSaleQty", "sPromoPaperNO",
"sPromoGroupNO", "nTag", "dLastUpdateTime", "sURL", "sPromoActionTypeID", "sPromoName", "nAgentID",
"sGoodsDesc", "sSpec", "sUnit", "nMinSaleQty", "nSaleUnits", "nSalePrice", "nRealSalePrice",
"nTag_CG", "sBrand", "sBrandID", "sCategoryNO", "sGoodTypeID", "sGoodType", "sPromoPaperNos",
"sWarehouseNO", "sAgentName", "sWarehouseName", "sWarehouseNO", "nStockQty", "nMinDCAmount",
"nApID", "nAdID", "sIpInfo", "sDeviceInfo", "sUserBrower");
rjx.set("totalCount", datas.size());
rjx.set("wmsList", wmsList);
rjx.set("shopGoodsObj", Util.listToMM(datas, "$", "contractNo", "goodsId", "goodsType", "nIdx", "promoNo"));
}
if (!rjx.keySet().contains("shopGoodsObj")) {
rjx.set("shopGoodsObj", new HashMap<String, Object>());
rjx.set("totalCount", 0);
rjx.set("wmsList", new ArrayList<Object>());
}
List<Map<String, Object>> promos = this.processPromo(shopNo, promosNo, chooseGifts, chooseGroups);
this.processPromoGoods(promos, shopNo);
rjx.set("promoData", Util.listToMM(promos, "promoNo"));
this.processPayAmount(rjx);
this.cacheData(shopNo, terminal, rjx);
return rjx;
}
private void cacheTmpData(String shopNo, String terminal) {
this.redisTemplate.opsForHash().put("CART_" + shopNo, terminal,
Rjx.json().set("nCacheStatus", 1).toJson());
this.redisTemplate.expire("CART_" + shopNo, 5, TimeUnit.SECONDS);
}
private void cacheData(String shopNo, String terminal, Rjx rjx) {
if (useCache) {
this.redisTemplate.opsForHash().put("CART_" + shopNo, terminal, rjx);
this.redisTemplate.expire("CART_" + shopNo, 5, TimeUnit.MINUTES);
logger.info("购物车查询数据缓存已完成,缓存有效期5分钟:sShopNO=" + shopNo + ",sTerminalType=" + terminal);
}
}
private void processWmsPayAmount(String wmsNo, List<Map<String, Object>> wmsData, BigDecimal goodsAmount,
BigDecimal goodsPoint) {
if (goodsAmount == null) {
goodsAmount = BigDecimal.ZERO;
}
if (StringUtil.isNotBlank(wmsNo)) {
for (Map<String, Object> wmsMap : wmsData) {
if (wmsMap != null && wmsNo.equals(wmsMap.get("warehouseNo"))) {
BigDecimal discountAmount = Util.objTo(wmsMap.get("discountAmount"), BigDecimal.class,
BigDecimal.ZERO);
if (discountAmount != null) {
wmsMap.put("discountAmount", discountAmount.add(goodsAmount));
BigDecimal discountPoint = Util.objTo(wmsMap.get("discountPoint"), BigDecimal.class,
BigDecimal.ZERO);
wmsMap.put("discountPoint", discountPoint.add(goodsPoint));
}
break;
}
}
}
}
private void processPayAmount(Rjx rjx) {
BigDecimal nNeedPayAmount = BigDecimal.ZERO;
BigDecimal nNeedPayPoint = BigDecimal.ZERO;
BigDecimal nTotalAmount = BigDecimal.ZERO;
Map<String, Object> promoData = (Map<String, Object>) rjx.get("promoData");
Map<String, Object> goodsData = (Map<String, Object>) rjx.get("shopGoodsObj");
List<Map<String, Object>> wmsData = (List<Map<String, Object>>) rjx.get("wmsList");
for (Map<String, Object> wmsMap : wmsData) {
nTotalAmount = nTotalAmount.add((BigDecimal) wmsMap.get("totalAmount"));
}
rjx.set("totalAmount", nTotalAmount);
for (String goodsKey : goodsData.keySet()) {
Map<String, Object> goodsMap = (Map<String, Object>) goodsData.get(goodsKey);
String goodsPromoNo = (String) goodsMap.get("promoNo");
BigDecimal saleQty = Util.objTo(goodsMap.get("saleQty"), BigDecimal.class);
BigDecimal price = Util.objTo(goodsMap.get("price"), BigDecimal.class);
String wmsNo = (String) goodsMap.get("warehouseNo");
boolean boo = false;
if (StringUtil.isNotBlank(goodsPromoNo) && promoData.get(goodsPromoNo) == null) {
boo = true;
logger.error("活动不存在,或失效:" + goodsPromoNo);
}
if (StringUtil.isBlank(goodsPromoNo) || boo) {
BigDecimal goodsAmount = price.multiply(saleQty);
nNeedPayAmount = nNeedPayAmount.add(goodsAmount);
this.processWmsPayAmount(wmsNo, wmsData, goodsAmount, BigDecimal.ZERO);
System.out.println("1.nNeedPayAmount += " + goodsAmount);
}
}
for (String key : promoData.keySet()) {
Map<String, Object> promoMap = (Map<String, Object>) promoData.get(key);
Integer isCycle = Util.objTo(promoMap.get("isCycle"), Integer.class);
String promoNo = Util.objTo(promoMap.get("promoNo"), String.class);
String promoTypeId = Util.objTo(promoMap.get("promoTypeId"), String.class);
if (promoTypeId.equals("1")) {// 批量
for (String goodsKey : goodsData.keySet()) {
Map<String, Object> goodsMap = (Map<String, Object>) goodsData.get(goodsKey);
String goodsPromoNo = (String) goodsMap.get("promoNo");
if (goodsPromoNo != null && goodsPromoNo.equals(promoNo)) { // 匹配到活动,计算金额
BigDecimal saleQty = Util.objTo(goodsMap.get("saleQty"), BigDecimal.class);
BigDecimal price = Util.objTo(goodsMap.get("price"), BigDecimal.class);
BigDecimal discountPrice = price;
BigDecimal meetQty = null;
String wmsNo = (String) goodsMap.get("warehouseNo");
List<Map<String, Object>> promoGoods = (List<Map<String, Object>>) promoMap.get("productGroup");
for (Map<String, Object> map : promoGoods) {
if (map.get("contractNo").equals(goodsMap.get("contractNo"))
&& map.get("goodsId").equals(goodsMap.get("goodsId"))) {
meetQty = Util.objTo(map.get("meetQty"), BigDecimal.class);
if (saleQty.compareTo(meetQty) == 0 || saleQty.compareTo(meetQty) == 1) {// 满足批量活动条件
discountPrice = Util.objTo(map.get("realPrice"), BigDecimal.class);
}
break;
}
}
goodsMap.put("discountPrice", discountPrice);
BigDecimal goodsAmount = discountPrice.multiply(saleQty);
goodsMap.put("discountAmount", goodsAmount);
this.processWmsPayAmount(wmsNo, wmsData, goodsAmount, BigDecimal.ZERO);
nNeedPayAmount = nNeedPayAmount.add(goodsAmount);
System.out.println("2.nNeedPayAmount += " + goodsAmount);
}
}
} else if (promoTypeId.equals("2")) {// 包装
for (String goodsKey : goodsData.keySet()) {
Map<String, Object> goodsMap = (Map<String, Object>) goodsData.get(goodsKey);
String goodsPromoNo = (String) goodsMap.get("promoNo");
if (goodsPromoNo != null && goodsPromoNo.equals(promoNo)) { // 匹配到活动,计算金额
BigDecimal saleQty = Util.objTo(goodsMap.get("saleQty"), BigDecimal.class);
BigDecimal price = Util.objTo(goodsMap.get("price"), BigDecimal.class);
BigDecimal discountAmount = null;
BigDecimal meetQty = null;
String wmsNo = (String) goodsMap.get("warehouseNo");
List<Map<String, Object>> promoGoods = (List<Map<String, Object>>) promoMap.get("productGroup");
for (Map<String, Object> map : promoGoods) {
if (map.get("contractNo").equals(goodsMap.get("contractNo"))
&& map.get("goodsId").equals(goodsMap.get("goodsId"))) {
meetQty = Util.objTo(map.get("meetQty"), BigDecimal.class);
discountAmount = Util.objTo(map.get("realAmount"), BigDecimal.class);
break;
}
}
BigDecimal _count = new BigDecimal(saleQty.divide(meetQty, 0, RoundingMode.DOWN).intValue());
discountAmount = (_count.multiply(discountAmount))
.add(((saleQty.subtract(_count.multiply(meetQty))).multiply(price)));
goodsMap.put("discountAmount", discountAmount);
this.processWmsPayAmount(wmsNo, wmsData, discountAmount, BigDecimal.ZERO);
nNeedPayAmount = nNeedPayAmount.add(discountAmount);
System.out.println("3.nNeedPayAmount += " + (discountAmount));
}
}
} else if (promoTypeId.equals("3")) {// 组合
for (String goodsKey : goodsData.keySet()) {
Map<String, Object> goodsMap = (Map<String, Object>) goodsData.get(goodsKey);
String goodsPromoNo = (String) goodsMap.get("promoNo");
if (goodsPromoNo != null && goodsPromoNo.equals(promoNo)) { // 匹配到活动,计算金额
BigDecimal price = Util.objTo(goodsMap.get("price"), BigDecimal.class);
BigDecimal saleQty = Util.objTo(goodsMap.get("saleQty"), BigDecimal.class);
BigDecimal discountPrice = null;
String wmsNo = (String) goodsMap.get("warehouseNo");
if (goodsPromoNo != null && goodsPromoNo.equals(promoNo)) { // 匹配到活动,计算金额
String groupNo = (String) goodsMap.get("groupNo");
List<Map<String, Object>> combinList = (List<Map<String, Object>>) promoMap
.get("combinList");
for (Map<String, Object> combinMap : combinList) {
String gNo = Util.objTo(combinMap.get("groupNo"), String.class);
if (gNo != null && gNo.equals(groupNo)) {
BigDecimal groupCount = BigDecimal.ZERO;
BigDecimal groupPrice = BigDecimal.ZERO;
List<Map<String, Object>> combinGroup = (List<Map<String, Object>>) combinMap
.get("combinGroup");
for (Map<String, Object> map : combinGroup) {
BigDecimal meetQty = Util.objTo(map.get("meetQty"), BigDecimal.class);
groupPrice = groupPrice
.add(new BigDecimal(String.valueOf(map.get("groupPrice")))
.multiply(meetQty));
if (discountPrice == null
&& map.get("contractNo").equals(goodsMap.get("contractNo"))
&& map.get("goodsId").equals(goodsMap.get("goodsId"))) {
discountPrice = Util.objTo(map.get("groupPrice"), BigDecimal.class);
if (groupCount.compareTo(BigDecimal.ZERO) == 0) {
groupCount = new BigDecimal(Util.objTo(
saleQty.divide(meetQty, 0, RoundingMode.DOWN), Integer.class));
}
}
}
combinMap.put("groupCount", groupCount);
combinMap.put("groupPrice", groupPrice);
combinMap.put("groupAmount", groupPrice.multiply(groupCount));
break;
}
}
}
discountPrice = discountPrice == null ? price : discountPrice;
goodsMap.put("discountPrice", discountPrice);
BigDecimal discountAmount = discountPrice.multiply(saleQty);
goodsMap.put("discountAmount", discountAmount);
this.processWmsPayAmount(wmsNo, wmsData, discountAmount, BigDecimal.ZERO);
nNeedPayAmount = nNeedPayAmount.add(discountAmount);
System.out.println("4.nNeedPayAmount += " + (discountPrice.multiply(saleQty)));
}
}
} else if (promoTypeId.equals("4")) {// 满减
BigDecimal m = BigDecimal.ZERO;
String wmsNo = null;
BigDecimal discountAmount = BigDecimal.ZERO;
for (String goodsKey : goodsData.keySet()) {
Map<String, Object> goodsMap = (Map<String, Object>) goodsData.get(goodsKey);
String goodsPromoNo = (String) goodsMap.get("promoNo");
BigDecimal price = Util.objTo(goodsMap.get("price"), BigDecimal.class);
BigDecimal saleQty = Util.objTo(goodsMap.get("saleQty"), BigDecimal.class);
if (goodsPromoNo != null && goodsPromoNo.equals(promoNo)) {
if (StringUtil.isBlank(wmsNo)) {
wmsNo = (String) goodsMap.get("warehouseNo");
}
m = m.add(saleQty.multiply(price));
}
}
Map<String, Map<String, Object>> ruleGroup = (Map<String, Map<String, Object>>) promoMap
.get("ruleGroup");
for (String ruleKey : ruleGroup.keySet()) {
Map<String, Object> ruleMap = ruleGroup.get(ruleKey);
BigDecimal meetAmount = Util.objTo(ruleMap.get("meetAmount"), BigDecimal.class);
BigDecimal disAmount = Util.objTo(ruleMap.get("disAmount"), BigDecimal.class);
if (isCycle == 1) {
disAmount = BigDecimal.valueOf(m.divide(meetAmount, 0, RoundingMode.DOWN).intValue())
.multiply(disAmount);
}
if ((m.compareTo(meetAmount) == 0 || m.compareTo(meetAmount) == 1)
&& disAmount.compareTo(discountAmount) == 1) {
discountAmount = disAmount;
promoMap.put("ruleId", ruleKey);
}
}
BigDecimal goodsAmount = m.subtract(discountAmount);
this.processWmsPayAmount(wmsNo, wmsData, goodsAmount, BigDecimal.ZERO);
nNeedPayAmount = nNeedPayAmount.add(goodsAmount);
System.out.println("5.nNeedPayAmount += " + goodsAmount);
} else if (promoTypeId.equals("5")) {// 满折
BigDecimal m = BigDecimal.ZERO;
BigDecimal discountRate = new BigDecimal(1);
for (String goodsKey : goodsData.keySet()) {
Map<String, Object> goodsMap = (Map<String, Object>) goodsData.get(goodsKey);
String goodsPromoNo = (String) goodsMap.get("promoNo");
BigDecimal price = Util.objTo(goodsMap.get("price"), BigDecimal.class);
BigDecimal saleQty = Util.objTo(goodsMap.get("saleQty"), BigDecimal.class);
if (goodsPromoNo != null && goodsPromoNo.equals(promoNo)) {
m = m.add(saleQty.multiply(price));
}
}
Map<String, Map<String, Object>> ruleGroup = (Map<String, Map<String, Object>>) promoMap
.get("ruleGroup");
for (String ruleKey : ruleGroup.keySet()) {
Map<String, Object> ruleMap = ruleGroup.get(ruleKey);
BigDecimal meetAmount = Util.objTo(ruleMap.get("meetAmount"), BigDecimal.class);
BigDecimal disRate = Util.objTo(ruleMap.get("disRate"), BigDecimal.class);
if ((m.compareTo(meetAmount) == 0 || m.compareTo(meetAmount) == 1)
&& disRate.compareTo(discountRate) == -1) {
discountRate = disRate;
promoMap.put("ruleId", ruleKey);
}
}
for (String goodsKey : goodsData.keySet()) {
Map<String, Object> goodsMap = (Map<String, Object>) goodsData.get(goodsKey);
String goodsPromoNo = (String) goodsMap.get("promoNo");
BigDecimal price = Util.objTo(goodsMap.get("price"), BigDecimal.class);
BigDecimal saleQty = Util.objTo(goodsMap.get("saleQty"), BigDecimal.class);
if (goodsPromoNo != null && goodsPromoNo.equals(promoNo)) {
BigDecimal goodsAmount = price.multiply(saleQty).multiply(discountRate);
this.processWmsPayAmount((String) goodsMap.get("warehouseNo"), wmsData, goodsAmount,
BigDecimal.ZERO);
nNeedPayAmount = nNeedPayAmount.add(goodsAmount);
System.out.println("6.nNeedPayAmount += " + goodsAmount);
}
}
} else if (promoTypeId.equals("6")) {// 换购
// this.chooseGift(shopNo, promoNo);
List<Map<String, Object>> upgradeList = (List<Map<String, Object>>) promoMap.get("upgradeList");
BigDecimal upgradeAmount = BigDecimal.ZERO;
for (String goodsKey : goodsData.keySet()) {
Map<String, Object> goodsMap = (Map<String, Object>) goodsData.get(goodsKey);
String goodsPromoNo = (String) goodsMap.get("promoNo");
Integer goodsType = Util.objTo(goodsMap.get("goodsType"), Integer.class);
BigDecimal price = Util.objTo(goodsMap.get("price"), BigDecimal.class);
BigDecimal saleQty = Util.objTo(goodsMap.get("saleQty"), BigDecimal.class);
if (goodsType == 0 && goodsPromoNo != null && goodsPromoNo.equals(promoNo)) {
BigDecimal goodsAmount = saleQty.multiply(price);
this.processWmsPayAmount((String) goodsMap.get("warehouseNo"), wmsData, goodsAmount,
BigDecimal.ZERO);
nNeedPayAmount = nNeedPayAmount.add(goodsAmount);
System.out.println("7.nNeedPayAmount += " + goodsAmount);
upgradeAmount = upgradeAmount.add(goodsAmount);
}
}
promoMap.put("upgradeAmount", upgradeAmount);
List<Map<String, Object>> removeList = new ArrayList<Map<String, Object>>();
for (Map<String, Object> upgradeMap : upgradeList) {
BigDecimal meetAmount = Util.objTo(upgradeMap.get("meetAmount"), BigDecimal.class);
BigDecimal limitQty = Util.objTo(upgradeMap.get("limitQty"), BigDecimal.class);
BigDecimal maxQty = Util.objTo(upgradeMap.get("maxLimitQty"), BigDecimal.class);
BigDecimal exchangeCount = BigDecimal.ONE;
if (isCycle == 1) {
BigDecimal mCount = BigDecimal.valueOf(Double.MAX_VALUE);
if (meetAmount.compareTo(BigDecimal.ZERO) != 0) {
mCount = BigDecimal
.valueOf(upgradeAmount.divide(meetAmount, 0, RoundingMode.DOWN).intValue());
}
exchangeCount = mCount;
}
exchangeCount = MathUtil.min(new BigDecimal[] { maxQty, exchangeCount, limitQty });
upgradeMap.put("exchangeCount", exchangeCount);
if (exchangeCount.compareTo(BigDecimal.ZERO) <= 0) {
removeList.add(upgradeMap);
}
}
upgradeList.removeAll(removeList);
for (String goodsKey : goodsData.keySet()) {
Map<String, Object> goodsMap = (Map<String, Object>) goodsData.get(goodsKey);
String goodsPromoNo = (String) goodsMap.get("promoNo");
Integer goodsType = Util.objTo(goodsMap.get("goodsType"), Integer.class);
if (goodsType == 1 && goodsPromoNo != null && goodsPromoNo.equals(promoNo)) {
for (Map<String, Object> upgradeMap : upgradeList) {
if (upgradeMap.get("contractNo").equals(goodsMap.get("contractNo"))
&& upgradeMap.get("goodsId").equals(goodsMap.get("goodsId"))) {
BigDecimal exchangePoint = Util.objTo(upgradeMap.get("exchangePoint"), BigDecimal.class,
BigDecimal.ZERO);
BigDecimal exchangePrice = Util.objTo(upgradeMap.get("exchangePrice"), BigDecimal.class,
BigDecimal.ZERO);
BigDecimal exchangeCount = Util.objTo(upgradeMap.get("exchangeCount"), BigDecimal.class,
BigDecimal.ZERO);
this.processWmsPayAmount((String) goodsMap.get("warehouseNo"), wmsData, exchangePrice,
exchangePoint);
nNeedPayAmount = nNeedPayAmount.add(exchangePrice.multiply(exchangeCount));
System.out.println("8.nNeedPayAmount += " + exchangePrice.multiply(exchangeCount));
nNeedPayPoint = nNeedPayPoint.add(exchangePoint.multiply(exchangeCount));
break;
}
}
}
}
} else if (promoTypeId.equals("9")) {
for (String goodsKey : goodsData.keySet()) {
Map<String, Object> goodsMap = (Map<String, Object>) goodsData.get(goodsKey);
String goodsPromoNo = (String) goodsMap.get("promoNo");
if (goodsPromoNo != null && goodsPromoNo.equals(promoNo)) { // 匹配到活动,计算金额
BigDecimal saleQty = Util.objTo(goodsMap.get("saleQty"), BigDecimal.class);
BigDecimal price = Util.objTo(goodsMap.get("price"), BigDecimal.class);
BigDecimal discountPrice = price;
BigDecimal discountQty = BigDecimal.ZERO;
String wmsNo = (String) goodsMap.get("warehouseNo");
List<Map<String, Object>> promoGoods = (List<Map<String, Object>>) promoMap.get("productGroup");
for (Map<String, Object> map : promoGoods) {
if (map.get("contractNo").equals(goodsMap.get("contractNo"))
&& map.get("goodsId").equals(goodsMap.get("goodsId"))) {
BigDecimal limitPrice = (BigDecimal) map.get("limitPrice");
BigDecimal validQty = (BigDecimal) map.get("validQty");
BigDecimal limitQty = Util.objTo(map.get("limitQty"), BigDecimal.class);
BigDecimal maxLimitQty = Util.objTo(map.get("maxLimitQty"), BigDecimal.class);
maxLimitQty = maxLimitQty.compareTo(BigDecimal.ZERO) > 0
? (maxLimitQty.subtract(validQty == null ? BigDecimal.ZERO : validQty))
: new BigDecimal(9999999);
limitQty = limitQty.compareTo(BigDecimal.ZERO) > 0 ? limitQty : new BigDecimal(9999999);
discountQty = MathUtil.min(new BigDecimal[] { maxLimitQty, limitQty, saleQty });
discountQty = discountQty.compareTo(BigDecimal.ZERO) < 0 ? BigDecimal.ZERO
: discountQty;
goodsMap.put("discountPrice", limitPrice);
goodsMap.put("maxLimitQty", maxLimitQty);
goodsMap.put("limitQty", limitQty);
discountPrice = limitPrice;
BigDecimal oqty = saleQty.subtract(discountQty);
BigDecimal goodsAmount = discountPrice.multiply(discountQty).add(price.multiply(oqty));
goodsMap.put("discountQty", discountQty);
goodsMap.put("discountAmount", goodsAmount);
this.processWmsPayAmount(wmsNo, wmsData, goodsAmount, BigDecimal.ZERO);
nNeedPayAmount = nNeedPayAmount.add(goodsAmount);
System.out.println("9.nNeedPayAmount += " + goodsAmount);
}
}
}
}
}
}
rjx.set("payAmount", nNeedPayAmount);
rjx.set("payPoint", nNeedPayPoint);
}
private void processPromoGoods(List<Map<String, Object>> promos, String shopNo) {
Set<String> promoGoodsArgs = new HashSet<String>();
promoGoodsArgs.add("0 = 1");
for (Map<String, Object> promo : promos) {
List<Map<String, Object>> productList = (List) promo.get("productGroup");// 参与活动的商品的集合
List<Map<String, Object>> giftList = (List) promo.get("upgradeList");// 参与满赠活动的赠品集合
List<Map<String, Object>> groupList = (List) promo.get("combinList");// 参与组合促销的商品组合的集合
for (Map<String, Object> goods : productList) {
String contractNo = (String) goods.get("contractNo");
Integer goodsId = (Integer) goods.get("goodsId");
promoGoodsArgs.add("(sAgentContractNO = '" + contractNo + "' AND nGoodsID = " + goodsId + ")");
}
for (Map<String, Object> goods : giftList) {
String contractNo = (String) goods.get("contractNo");
Integer goodsId = (Integer) goods.get("goodsId");
promoGoodsArgs.add("(sAgentContractNO = '" + contractNo + "' AND nGoodsID = " + goodsId + ")");
}
for (Map<String, Object> groupInfo : groupList) {
List<Map<String, Object>> groupProductList = (List) groupInfo.get("combinGroup");
for (Map<String, Object> goods : groupProductList) {
String contractNo = (String) goods.get("contractNo");
Integer goodsId = (Integer) goods.get("goodsId");
promoGoodsArgs.add("(sAgentContractNO = '" + contractNo + "' AND nGoodsID = " + goodsId + ")");
}
}
}
String sqlGoods = "select distinct nGoodsID, sAgentContractNO, sGoodsDesc, nRealSalePrice, nTag, sSpec, sUnit, sBrandID, sCategoryNO, sURL, nStockQty, sWarehouseNO from V_CategoryGoods where sShopNO = '"
+ shopNo + "'" + StringUtil.join(" OR ", " AND (", ")", promoGoodsArgs);
List<Map<String, Object>> productInfo = jdbcTemplate.queryForList(sqlGoods);
Map<String, Map<String, Object>> pmm = Util.listToMM(productInfo, "$", "sAgentContractNO", "nGoodsID");
for (Map<String, Object> promo : promos) {
List<Map<String, Object>> productList = (List) promo.get("productGroup");// 参与活动的商品的集合
List<Map<String, Object>> giftList = (List) promo.get("upgradeList");// 参与满赠活动的赠品集合
List<Map<String, Object>> groupList = (List) promo.get("combinList");// 参与组合促销的商品组合的集合
for (Map<String, Object> product : productList) {
String contractNo = (String) product.get("contractNo");
Integer goodsId = (Integer) product.get("goodsId");
Map<String, Object> pm = pmm.get(contractNo + "$" + goodsId);
if (pm != null) {
product.put("imgPath", pm.get("sURL"));
product.put("goodsName", pm.get("sGoodsDesc"));
product.put("basicInfoSpec", pm.get("sSpec"));
product.put("priceUnit", pm.get("sUnit"));
product.put("stockQty", pm.get("nStockQty"));
product.put("warehouseNo", pm.get("sWarehouseNO"));
} else {
logger.error("商品资料不完整" + contractNo + "$" + goodsId);
}
}
for (Map<String, Object> product : giftList) {
String contractNo = (String) product.get("contractNo");
Integer goodsId = (Integer) product.get("goodsId");
Map<String, Object> pm = pmm.get(contractNo + "$" + goodsId);
if (pm != null) {
product.put("imgPath", pm.get("sURL"));
product.put("goodsName", pm.get("sGoodsDesc"));
product.put("price", pm.get("nRealSalePrice"));
product.put("basicInfoSpec", pm.get("sSpec"));
product.put("priceUnit", pm.get("sUnit"));
product.put("stockQty", pm.get("nStockQty"));
product.put("warehouseNo", pm.get("sWarehouseNO"));
product.put("nTag", pm.get("nTag"));
} else {
logger.error("商品资料不完整" + contractNo + "$" + goodsId);
}
}
for (Map<String, Object> groupInfo : groupList) {
List<Map<String, Object>> groupProductList = (List) groupInfo.get("combinGroup");
for (Map<String, Object> product : groupProductList) {
String contractNo = (String) product.get("contractNo");
Integer goodsId = (Integer) product.get("goodsId");
Map<String, Object> pm = pmm.get(contractNo + "$" + goodsId);
if (pm != null) {
product.put("imgPath", pm.get("sURL"));
product.put("goodsName", pm.get("sGoodsDesc"));
product.put("price", pm.get("nRealSalePrice"));
product.put("basicInfoSpec", pm.get("sSpec"));
product.put("priceUnit", pm.get("sUnit"));
product.put("stockQty", pm.get("nStockQty"));
product.put("warehouseNo", pm.get("sWarehouseNO"));
} else {
logger.error("商品资料不完整" + contractNo + "$" + goodsId);
}
}
}
}
}
private List<Map<String, Object>> processPromo(String shopNo, Set<String> promosNo, Set<String> chooseGifts,
Set<String> chooseGroups) {
String sqlPromo = "SELECT * FROM tYWPromoMain WHERE sPromoPaperNO in ('" + StringUtil.join("', '", promosNo)
+ "')";
String sqlPromoDtl = "SELECT DISTINCT a.* FROM ( SELECT dtl.*, pgc.nQty nGiftQty FROM tYWPromoDtl dtl LEFT JOIN tPromoGiftCount pgc ON pgc.sPromoPaperNO = dtl.sPromoPaperNO AND pgc.nGoodsID = dtl.nGoodsID ) a, tCart c WHERE a.sPromoPaperNO = c.sPromoPaperNO AND c.sShopNO = '"
+ shopNo
+ "' AND ( a.nTag = 1 OR ( a.sAgentContractNO = c.sAgentContractNO AND a.nGoodsID = c.nGoodsID AND a.nTag = c.nTag ) )";
String sqlPromoLimit = "SELECT sop.sPromoPaperNO, sop.sAgentContractNO, sop.nGoodsID, SUM(sop.nValidQty) nValidQty FROM tSalesOrderPromo sop WHERE sop.sPromoPaperNO IN ('"
+ StringUtil.join("', '", promosNo)
+ "') GROUP BY sop.sPromoPaperNO, sop.sAgentContractNO, sop.nGoodsID";
ResultMutil resultMutil = jdbcTemplate
.executeMutil(sqlPromo + Util.LINE_SEPARATOR + sqlPromoDtl + Util.LINE_SEPARATOR + sqlPromoLimit);
List<Map<String, Object>> promos = resultMutil.getDatas().get(0);
List<Map<String, Object>> promosDtl = resultMutil.getDatas().get(1);
List<Map<String, Object>> promosLimit = resultMutil.getDatas().get(2);
for (Map<String, Object> m1 : promosDtl) {
BigDecimal validQty = BigDecimal.ZERO;
for (Map<String, Object> m2 : promosLimit) {
String v1 = (String) m1.get("sPromoPaperNO");
String v2 = (String) m1.get("sAgentContractNO");
Integer v3 = (Integer) m1.get("nGoodsID");
String v1_ = (String) m2.get("sPromoPaperNO");
String v2_ = (String) m2.get("sAgentContractNO");
Integer v3_ = (Integer) m2.get("nGoodsID");
if (v1 != null && v1_ != null && v2 != null && v2_ != null && v3 != null && v3_ != null
&& v1.equals(v1_) && v2.equals(v2_) && v3.equals(v3_)) {
validQty = (BigDecimal) m2.get("nValidQty");
}
}
m1.put("nValidQty", validQty == null ? BigDecimal.ZERO : validQty);
}
for (Map<String, Object> promo : promos) {
Integer nProperty = (Integer) promo.get("nProperty");
String sPromoActionTypeID = (String) promo.get("sPromoActionTypeID");
String sPromoPaperNO = (String) promo.get("sPromoPaperNO");
List<String> cycleTypes = Arrays.asList("4", "5", "6");
List<String> cycleTypesDefault = Arrays.asList("1", "2", "3");
promo.put("isCycle", ((cycleTypes.contains(sPromoActionTypeID) && ((nProperty & 2) == 2))
|| (cycleTypesDefault.contains(sPromoActionTypeID))) ? 1 : 0);
promo.put("limitAmount", promo.get("nLimitAmount"));
List<Map<String, Object>> giftList = new ArrayList<Map<String, Object>>();// 参与满赠活动的赠品集合
List<Map<String, Object>> groupList = new ArrayList<Map<String, Object>>();// 参与组合促销的商品组合的集合
Map<String, Map<String, Object>> ruleGroup = new HashMap<String, Map<String, Object>>();// 满减满折规则
Map<String, Map<String, Object>> productGroup = new HashMap<String, Map<String, Object>>();// 参与活动的商品的集合
for (Map<String, Object> dtl : promosDtl) {
Map<String, Object> productMap = new HashMap<String, Object>();// 参与活动的商品
String sPromoPaperNODtl = (String) dtl.get("sPromoPaperNO");
Integer nTag = (Integer) dtl.get("nTag");
String sGroupNo = (String) dtl.get("sGroupNO");
String sAgentContractNO = (String) dtl.get("sAgentContractNO");
String sBrandID = (String) dtl.get("sBrandID");
String sCategoryNO = (String) dtl.get("sCategoryNO");
Integer nGoodsID = (Integer) dtl.get("nGoodsID");
BigDecimal nMeetQty = (BigDecimal) dtl.get("nMeetQty");
BigDecimal nMeetAmount = (BigDecimal) dtl.get("nMeetAmount");
BigDecimal nDisAmount = (BigDecimal) dtl.get("nDisAmount");
BigDecimal nDisRate = (BigDecimal) dtl.get("nDisRate");
BigDecimal nPrice = (BigDecimal) dtl.get("nPrice");
BigDecimal nAmount = (BigDecimal) dtl.get("nAmount");
BigDecimal nValidQty = (BigDecimal) dtl.get("nValidQty");
Integer nPoint = (Integer) dtl.get("nPoint");
Integer nLimitQty = (Integer) dtl.get("nLimitQty");
Integer nMaxLimitQty = (Integer) dtl.get("nMaxLimitQty");
Integer nGiftQty = Util.objTo(dtl.get("nGiftQty"), Integer.class, 0);
productMap.put("contractNo", sAgentContractNO);
productMap.put("goodsId", nGoodsID);
productMap.put("brandId", sBrandID);
productMap.put("categoryNo", sCategoryNO);
if (sPromoPaperNODtl.equals(sPromoPaperNO)) {
if ((sPromoActionTypeID.equals("6") && nTag != null && nTag == 0) || sPromoActionTypeID.equals("1")
|| sPromoActionTypeID.equals("2") || sPromoActionTypeID.equals("4")
|| sPromoActionTypeID.equals("5") || sPromoActionTypeID.equals("9")) {
String key = sAgentContractNO + "$" + sBrandID + "$" + sCategoryNO + "$" + nGoodsID;
productGroup.put(key, productMap);
}
if (sPromoActionTypeID.equals("1") || sPromoActionTypeID.equals("2")) {// 批量包装
productMap.put("meetQty", nMeetQty);
productMap.put("realPrice", nPrice);
productMap.put("realAmount", nAmount);
} else if (sPromoActionTypeID.equals("4") || sPromoActionTypeID.equals("5") && nTag == 0) {// 满减满折
String nRuleID = Util.objTo(dtl.get("nRuleID"), String.class, null);
Map<String, Object> map = new HashMap<String, Object>();
map.put("meetAmount", nMeetAmount);
map.put("disAmount", nDisAmount);
map.put("disRate", nDisRate);
ruleGroup.put(nRuleID, map);
} else if (sPromoActionTypeID.equals("6")) {// 换购
if (nTag != null && nTag == 1) {
productMap.put("meetAmount", nMeetAmount);
productMap.put("exchangePrice", nPrice);
productMap.put("exchangePoint", nPoint);
productMap.put("maxLimitQty", nMaxLimitQty == 0 ? 9999999 : (nMaxLimitQty - nGiftQty));
productMap.put("limitQty", nLimitQty);
giftList.add(productMap);
}
} else if (sPromoActionTypeID.equals("3")) {// 组合
Map<String, Object> groupInfo = null;
for (Map<String, Object> group : groupList) {
String groupNo = (String) group.get("groupNo");
if (groupNo.equals(sGroupNo)) {
groupInfo = group;
}
}
if (groupInfo == null) {
groupInfo = new HashMap<String, Object>();
groupInfo.put("groupNo", sGroupNo);
groupList.add(groupInfo);
}
List<Map<String, Object>> groupProductList = (List) groupInfo.get("combinGroup");
if (groupProductList == null) {
groupProductList = new ArrayList<Map<String, Object>>();
groupInfo.put("combinGroup", groupProductList);
}
productMap.put("groupPrice", nPrice);
productMap.put("meetQty", nMeetQty);
groupProductList.add(productMap);
} else if (sPromoActionTypeID.equals("9")) {
productMap.put("limitPrice", nPrice);
productMap.put("validQty", nValidQty);
productMap.put("limitQty", nLimitQty);
productMap.put("maxLimitQty", nMaxLimitQty);
}
}
}
promo.put("productGroup", Arrays.asList(productGroup.values().toArray()));
promo.put("upgradeList", giftList);
promo.put("combinList", groupList);
promo.put("ruleGroup", ruleGroup);
}
Util.replaceKey(promos, ":", false, "sPromoPaperNO:promoNo", "sPromoTheme:promoTheme",
"sPromoActionTypeID:promoTypeId", "sPromoActionType:promoType", "sPromoName:promoName",
"sPromoDesc:promoDesc", "nLimitAmount:limitAmount", "dPromoBeginDate:promoBeginDate",
"dPromoEndDate:promoEndDate", "sSettingModeID:settingModeID", "nPromoJoinType:promoJoinType",
"nUseCycle:useCycle", "nTerminalTypeID:terminalTypeID");
Util.removeKey(promos, "sPromoPaperNO", "sPromoTheme", "sPromoActionTypeID", "sPromoActionType", "sPromoName",
"sMemo", "nLimitAmount", "dPromoBeginDate", "dPromoEndDate", "dConfirmDate", "sConfirmUser",
"nUseCycle", "sSettingModeID", "nPromoJoinType", "sCreateUser", "dCreateDate", "nProperty",
"nTerminalTypeID", "dLastUpdateTime", "sOrgNO", "sPromoDesc");
return promos;
}
private List<Map<String, Object>> getWmsList(List<Map<String, Object>> datas) {
Integer TotalCount = 0;
List<Map<String, Object>> wmsList = new ArrayList<Map<String, Object>>();
Map<String, List<Map<String, Object>>> temp = Util.listToML(datas, "sWarehouseName");
for (String key : temp.keySet()) {
Set<String> pids = new HashSet<String>();
List<Map<String, Object>> GoodsList = temp.get(key);
for (Map<String, Object> g : GoodsList) {
List<String> list = new ArrayList<String>();
list.add((String) g.get("sAgentContractNO"));
list.add(Util.objTo(g.get("nGoodsID"), String.class));
list.add(Util.objTo(g.get("nTag"), String.class));
list.add(Util.objTo(g.get("nIdx"), String.class));
if ((String) g.get("sPromoPaperNO") != null) {
list.add((String) g.get("sPromoPaperNO"));
}
pids.add(StringUtil.join("$", list));
}
List<String> pidlist = new ArrayList<String>();
pidlist.addAll(pids);
pidlist.sort(new Comparator<String>() {
public int compare(String o1, String o2) {
String s1 = o1.split("\\$")[3];
String s2 = o2.split("\\$")[3];
Integer i1 = Integer.valueOf(s1);
Integer i2 = Integer.valueOf(s2);
return i1.compareTo(i2);
}
});
BigDecimal TotalAmount = BigDecimal.ZERO;
Map<String, Object> goodsFirst = GoodsList.get(0);
Double MinOrderAmt = Util.objTo(goodsFirst.get("nMinDCAmount"), Double.class); // 最小配送金额
String WMSInfo = (String) goodsFirst.get("sWarehouseName"); // 物流名称
String sWarehouseNO = (String) goodsFirst.get("sWarehouseNO"); // 物流名称
String sOrgNO = (String) goodsFirst.get("wOrgNO"); // 物流名称
for (Map<String, Object> goods : GoodsList) {
BigDecimal goodsAmount = (BigDecimal) goods.get("nRealSalePrice"); // 当前销售价格
BigDecimal nSaleQty = (BigDecimal) goods.get("nSaleQty"); // 购物车商品数量
if (goodsAmount == null) {
throw new ReflectException("The price of articles is empty");
} else {
TotalAmount = TotalAmount.add(goodsAmount.multiply(nSaleQty));
}
}
Map<String, Object> zoneGoods = new HashMap<String, Object>();
zoneGoods.put("wmsInfo", WMSInfo);
zoneGoods.put("warehouseNo", sWarehouseNO);
zoneGoods.put("minOrderAmt", MinOrderAmt);
zoneGoods.put("totalAmount", TotalAmount);
zoneGoods.put("orgNo", sOrgNO);
zoneGoods.put("goodsIdList", pidlist);
wmsList.add(zoneGoods);
TotalCount += GoodsList.size();
}
return wmsList;
}
private List<Map<String, Object>> getGoodsList(String shopNo, String terminal) {
String sqlPromoTag = "" + "SELECT " + "c.sPromoPaperNO, " + "p.nTag, " + "p.dPromoBeginDate, "
+ "p.dPromoEndDate " + "FROM " + "tCart c "
+ "LEFT JOIN tYWPromoMain p ON p.sPromoPaperNO = c.sPromoPaperNO " + "WHERE " + "c.sShopNO = '" + shopNo
+ "'";
String sqlClearPromoNo = "" + "SELECT " + "pn.sPromoPaperNO " + "FROM " + "(" + sqlPromoTag + ") pn " + "WHERE "
+ "pn.nTag&2 != 2 OR pn.nTag&1 = 1 OR GETDATE() < pn.dPromoBeginDate OR GETDATE() >= pn.dPromoEndDate";
String sqlClear = "" + "DELETE mmm FROM tCart mmm WHERE mmm.sShopNO = '" + shopNo
+ "' AND EXISTS ( SELECT NULL FROM tAgentContractGoods nnn WHERE nnn.nGoodsID = mmm.nGoodsID AND nnn.sAgentContractNO = mmm.sAgentContractNO AND (nnn.nTag & 16 = 0 or nnn.nTag & 1 = 1) AND mmm.sShopNO = '"
+ shopNo + "' )" + Util.LINE_SEPARATOR + "DELETE mmmm FROM tCart mmmm WHERE mmmm.sShopNO = '" + shopNo
+ "' AND NOT EXISTS ( SELECT NULL FROM tWarehouseDistrict wd, tShop s, tAgentContractWarehouse acw, tAgentContractGoods acg WHERE wd.sDistrictID = s.sDistrictID AND s.sShopNO = '"
+ shopNo
+ "' AND wd.nTag & 1 = 0 AND acw.sWarehouseNO = wd.sWarehouseNO AND acw.nTag & 1 = 0 AND acg.sAgentContractNO = acw.sAgentContractNO AND acg.nGoodsID = mmmm.nGoodsID AND acg.sAgentContractNO = mmmm.sAgentContractNO ) "
+ Util.LINE_SEPARATOR + "DELETE " + "tCart " + "WHERE " + "sShopNO = '" + shopNo + "' " + "AND nTag = 1 "
+ "AND sPromoPaperNO IN (" + sqlClearPromoNo + ")" + Util.LINE_SEPARATOR + "UPDATE tCart SET "
+ "sPromoGroupNO = NULL, " + "sPromoPaperNO = NULL " + "WHERE " + "sShopNO = '" + shopNo + "' "
+ "AND sPromoPaperNO IN (" + sqlClearPromoNo + ")";
String sqlCart = "" + "SELECT " + "distinct og.nGoodsID nGoodsID_G, " + "c.*, " + "p.sPromoActionTypeID, "
+ "p.sPromoName, " + "og.nTag gnTag, " + "og.sWarehouseNO, " + "og.nStockQty, " + "og.sURL, "
+ "og.nAgentID, " + "og.sGoodsDesc, " + "og.sSpec, " + "og.sUnit, " + "og.nMinSaleQty, "
+ "og.nSaleUnits, " + "og.nSalePrice, " + "og.nRealSalePrice, " + "og.nTag nTag_CG, " + "og.sBrand, "
+ "og.sBrandID, " + "og.sCategoryNO ," + "cust.sCustTypeID " + "FROM " + "tCart c "
+ "LEFT JOIN V_CategoryGoods og ON og.sShopNO = '" + shopNo
+ "' AND og.sAgentContractNO = c.sAgentContractNO AND og.nGoodsID = c.nGoodsID "
+ "LEFT JOIN tYWPromoMain p ON p.sPromoPaperNO = c.sPromoPaperNO "
+ "LEFT JOIN tCustomer cust ON cust.sCustNO = (SELECT shop.sCustNO FROM tShop shop WHERE shop.sShopNO = '"
+ shopNo + "')" + "WHERE " + "c.sShopNO = '" + shopNo + "'";
String sqlACart = "" + "SELECT " + "c.*, " + "m.nPartakeNum, " + "m.nPartakeCycle, " + "m.dPromoBeginDate, "
+ "agent.sAgentName, " + "w.sWarehouseName, "
+ "CASE WHEN wcts.nMinOrderAmount is not null THEN wcts.nMinOrderAmount ELSE w.nMinDCAmount END nMinDCAmount, "
+ "CASE w.sWarehouseTypeID WHEN 3 THEN 'china' ELSE '' END wOrgNO " + "FROM " + "(" + sqlCart + ") c "
+ "LEFT JOIN tWarehouse w ON w.sWarehouseNO = c.sWarehouseNO "
+ "LEFT JOIN tWarehouseCustTypeSet wcts ON wcts.nTag = 0 AND wcts.sWarehouseNO = c.sWarehouseNO AND wcts.sCustTypeID = c.sCustTypeID "
+ "LEFT JOIN tAgent agent ON agent.nAgentID = c.nAgentID "
+ "LEFT JOIN tYWPromoMain m ON m.sPromoPaperNO = c.sPromoPaperNO " + "WHERE "
+ "c.nGoodsID_G IS NOT NULL " + "and c.sWarehouseNO IS NOT NULL " + "ORDER BY c.nIdx";
String sqlp = ""
+ " SELECT "
+ " tt.sPromoPaperNO, "
+ " COUNT (sSalesOrderID) nCount "
+ " FROM "
+ " ( "
+ " SELECT "
+ " t.sSalesOrderID, "
+ " t.sPromoPaperNO "
+ " FROM "
+ " ( "
+ " SELECT "
+ " m.sPromoPaperNO, "
+ " m.dPromoBeginDate, "
+ " m.nPartakeCycle, "
+ " m.nPartakeNum, "
+ " DATEADD( "
+ " DAY, "
+ " CAST ( "
+ " datediff( "
+ " DAY, "
+ " m.dPromoBeginDate, "
+ " getdate() "
+ " ) / nPartakeCycle AS INT "
+ " ) * m.nPartakeCycle, "
+ " m.dPromoBeginDate "
+ " ) cycleBegin, "
+ " o.sSalesOrderID, "
+ " o.dOrderTime, "
+ " o.nOrderStatus "
+ " FROM "
+ " tYWPromoMain m "
+ " LEFT JOIN ( "
+ " SELECT "
+ " sod.sSalesOrderID, "
+ " sod.sPromoPaperNO, "
+ " so.dOrderTime, "
+ " so.nOrderStatus "
+ " FROM "
+ " tSalesOrderPromo sod "
+ " LEFT JOIN tSalesOrder so ON so.sSalesOrderID = sod.sSalesOrderID "
+ " AND so.sShopNO = '" + shopNo + "' "
+ " ) o ON o.sPromoPaperNO = m.sPromoPaperNO "
+ " AND o.dOrderTime IS NOT NULL "
+ " WHERE "
+ " m.nPartakeCycle IS NOT NULL "
+ " AND m.nPartakeCycle != 0 "
+ " AND m.nPartakeNum IS NOT NULL "
+ " AND m.nPartakeNum != 0 "
+ " AND m.sPromoPaperNO IN ( "
+ " SELECT "
+ " sPromoPaperNO "
+ " FROM "
+ " tCart "
+ " WHERE "
+ " sShopNO = '" + shopNo + "' "
+ " ) "
+ " ) t "
+ " WHERE "
+ " dOrderTime IS NOT NULL "
+ " AND t.dOrderTime >= t.cycleBegin "
+ " GROUP BY "
+ " t.sSalesOrderID, "
+ " t.sPromoPaperNO "
+ " ) tt "
+ " GROUP BY "
+ " sPromoPaperNO ";
ResultMutil resultMutil = jdbcTemplate
.executeMutil(sqlClear + Util.LINE_SEPARATOR + sqlACart + sqlp + Util.LINE_SEPARATOR);
List<Map<String, Object>> datas = resultMutil.getDatas().get(0);
List<Map<String, Object>> pds = resultMutil.getDatas().get(1);
for (Map<String, Object> map : datas) {
Integer nPartakeNum = Util.objTo(map.get("nPartakeNum"), Integer.class, 0);
String sPromoPaperNO = (String) map.get("sPromoPaperNO");
if (StringUtil.isNotBlank(sPromoPaperNO) && nPartakeNum > 0) {
for (Map<String, Object> pm : pds) {
String pno = (String) pm.get("sPromoPaperNO");
if (sPromoPaperNO.equals(pno)) {
Integer nCount = Util.objTo(pm.get("nCount"), Integer.class, 0);
if (nCount >= nPartakeNum) {
map.put("sPromoPaperNO", null);
}
}
}
}
}
return datas;
}
}
...@@ -15,6 +15,7 @@ import com.egolm.common.StringUtil; ...@@ -15,6 +15,7 @@ import com.egolm.common.StringUtil;
import com.egolm.common.Util; import com.egolm.common.Util;
import com.egolm.common.bean.Rjx; import com.egolm.common.bean.Rjx;
import com.egolm.common.jdbc.JdbcTemplate; import com.egolm.common.jdbc.JdbcTemplate;
import com.egolm.shop.service.CartRedisService;
import com.egolm.shop.service.CartService; import com.egolm.shop.service.CartService;
@Service @Service
...@@ -23,6 +24,8 @@ public class CartServiceImpl implements CartService { ...@@ -23,6 +24,8 @@ public class CartServiceImpl implements CartService {
private static final Log logger = LogFactory.getLog(CartServiceImpl.class); private static final Log logger = LogFactory.getLog(CartServiceImpl.class);
@Autowired @Autowired
private JdbcTemplate jdbcTemplate; private JdbcTemplate jdbcTemplate;
@Autowired
private CartRedisService cartRedisService;
/** /**
* { * {
...@@ -96,7 +99,7 @@ public class CartServiceImpl implements CartService { ...@@ -96,7 +99,7 @@ public class CartServiceImpl implements CartService {
} }
} }
} }
cartRedisService.reCache(shopNO, terminal);
return Rjx.jsonOk().set("count", count(shopNO)).toJson(); return Rjx.jsonOk().set("count", count(shopNO)).toJson();
} }
...@@ -143,6 +146,8 @@ public class CartServiceImpl implements CartService { ...@@ -143,6 +146,8 @@ public class CartServiceImpl implements CartService {
sql = "delete from tCart where sShopNO = '" + shopNO + "' and nTag = 1 and sPromoPaperNO in (" + sql + ")"; sql = "delete from tCart where sShopNO = '" + shopNO + "' and nTag = 1 and sPromoPaperNO in (" + sql + ")";
jdbcTemplate.executeUpdate(sql); jdbcTemplate.executeUpdate(sql);
jdbcTemplate.executeUpdate("delete from tCart where sShopNO = '" + shopNO + "'" + StringUtil.join(", ", " and nIdx in (", ")", nIdx)); jdbcTemplate.executeUpdate("delete from tCart where sShopNO = '" + shopNO + "'" + StringUtil.join(", ", " and nIdx in (", ")", nIdx));
cartRedisService.reCache(shopNO, terminal);
return Rjx.jsonOk().set("count", count(shopNO)).toJson(); return Rjx.jsonOk().set("count", count(shopNO)).toJson();
} }
...@@ -158,7 +163,7 @@ public class CartServiceImpl implements CartService { ...@@ -158,7 +163,7 @@ public class CartServiceImpl implements CartService {
*/ */
public String updateQty (JSONObject obj) { public String updateQty (JSONObject obj) {
String shopNO = obj.getString("shopNO"); String shopNO = obj.getString("shopNO");
String idx = obj.getString("idx"); Integer idx = obj.getInteger("idx");
Double saleQty = obj.getDouble("saleQty"); Double saleQty = obj.getDouble("saleQty");
String terminal = obj.getString("terminal"); String terminal = obj.getString("terminal");
String langID = obj.getString("langID"); String langID = obj.getString("langID");
...@@ -177,7 +182,25 @@ public class CartServiceImpl implements CartService { ...@@ -177,7 +182,25 @@ public class CartServiceImpl implements CartService {
} else { } else {
jdbcTemplate.executeUpdate("update tCart set nSaleQty = " + saleQty + " where sShopNO = '" + shopNO + "' and nIdx = " + idx); jdbcTemplate.executeUpdate("update tCart set nSaleQty = " + saleQty + " where sShopNO = '" + shopNO + "' and nIdx = " + idx);
} }
cartRedisService.changeQty(shopNO, terminal, idx, saleQty);
return Rjx.jsonOk().set("count", count(shopNO)).toJson(); return Rjx.jsonOk().set("count", count(shopNO)).toJson();
} }
/**
* 查询购物车
* <p>Title: query</p>
* <p>Description: </p>
* @param params
* @return
* @see com.egolm.shop.service.CartService#query(java.util.Map)
*/
@Override
public String query(Map<String, Object> params) {
String shopNO = params.get("shopNO")+"";
String terminal = params.get("terminal")+"";
Map<String, Object> queryMap = cartRedisService.querySync(shopNO, terminal);
return Rjx.jsonOk().setData(queryMap).toJson();
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment