|
@@ -0,0 +1,199 @@
|
|
|
+package com.hcloud.microserver.h5.service.impl;
|
|
|
+
|
|
|
+import com.github.pagehelper.PageHelper;
|
|
|
+import com.github.pagehelper.PageInfo;
|
|
|
+import com.hcloud.microserver.commoncore.exception.GlobalException;
|
|
|
+import com.hcloud.microserver.facade.carbon.forms.GoodsInfoForm;
|
|
|
+import com.hcloud.microserver.h5.bo.GoodsInfoBO;
|
|
|
+import com.hcloud.microserver.h5.dao.GoodsManageMapper;
|
|
|
+import com.hcloud.microserver.h5.forms.GoodsManageQueryForm;
|
|
|
+import com.hcloud.microserver.h5.service.GoodsManageService;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+@Service
|
|
|
+public class GoodsManageServiceImpl implements GoodsManageService {
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private GoodsManageMapper goodsManageMapper;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public PageInfo searchGoodsInfoByPage(GoodsManageQueryForm goodsManageQueryForm) {
|
|
|
+ PageHelper.startPage(goodsManageQueryForm.getPageNo(),goodsManageQueryForm.getPageSize());
|
|
|
+ List<GoodsInfoBO> list = goodsManageMapper.searchGoodsInfoByPage(goodsManageQueryForm);
|
|
|
+ return new PageInfo(list);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<GoodsInfoForm> getGoodsInfoByCarbon(Integer carbonNum) {
|
|
|
+ if (carbonNum == 0) return null;
|
|
|
+
|
|
|
+ //随机分配商品
|
|
|
+ Map<String,List<GoodsInfoForm>> helpMap = selectGoodsInfo(1,carbonNum);
|
|
|
+ List<GoodsInfoForm> resultList = new ArrayList<>();
|
|
|
+ Map<String,GoodsInfoForm> resultMap = new HashMap<>();
|
|
|
+ if (helpMap != null && helpMap.size() > 0){
|
|
|
+ resultList = getRandomPY(resultMap,helpMap,carbonNum,1);
|
|
|
+ }
|
|
|
+
|
|
|
+ return resultList;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询农户随机集
|
|
|
+ * lym
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private Map<String,List<GoodsInfoForm>> selectGoodsInfo(Integer pageNo, int buyNum){
|
|
|
+ Integer pageSize = 5;
|
|
|
+
|
|
|
+ String orderStr = null;
|
|
|
+ if (buyNum > 300){ //购买大于300kg多的排前
|
|
|
+ //PageHelper.orderBy("tmp.unsaledCarbonSkin DESC");
|
|
|
+ orderStr = "ORDER BY tmp.unsaledCarbonSkin DESC";
|
|
|
+ }else {
|
|
|
+ //PageHelper.orderBy("tmp.saledCarbonSkin ASC,tmp.unsaledCarbonSkin DESC");
|
|
|
+ orderStr = "ORDER BY tmp.saledCarbonSkin ASC,tmp.unsaledCarbonSkin DESC";
|
|
|
+ }
|
|
|
+
|
|
|
+ List<String> RandomStrings = getRandomString(pageSize); //开发主体首字母
|
|
|
+ Map<String,List<GoodsInfoForm>> helpMap = new HashMap();
|
|
|
+ for (String namePy:RandomStrings){ //循环获取
|
|
|
+ PageHelper.startPage(pageNo,pageSize);
|
|
|
+ List<GoodsInfoForm> list = goodsManageMapper.getBuyGoodsInfo(namePy,orderStr);
|
|
|
+ if (list != null && list.size() > 0)
|
|
|
+ helpMap.put(namePy,list);
|
|
|
+ }
|
|
|
+ return helpMap;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 随机生成字母
|
|
|
+ * lym
|
|
|
+ * @param length
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public List<String> getRandomString(int length) {
|
|
|
+ List<String> RandomString = new ArrayList<>();
|
|
|
+
|
|
|
+ String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
|
+ Random random = new Random();
|
|
|
+ for (int i = 0; i < length; ++i) {
|
|
|
+ int number = random.nextInt(26);// [0,26)
|
|
|
+ RandomString.add(String.valueOf(str.charAt(number)));
|
|
|
+ }
|
|
|
+ return RandomString;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * lym
|
|
|
+ * 主体首字母随机分购
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private List<GoodsInfoForm> getRandomPY(Map<String,GoodsInfoForm> resultMap,Map<String,List<GoodsInfoForm>> helpMap,int buyNum
|
|
|
+ ,Integer pageNo) {
|
|
|
+ Collection<String> resultKeys = resultMap.keySet();
|
|
|
+ //随机用户集
|
|
|
+ List<GoodsInfoForm> list = new ArrayList<>();
|
|
|
+ if (helpMap.size() <= 2){
|
|
|
+ Collection<List<GoodsInfoForm>> values = helpMap.values();
|
|
|
+ for (List<GoodsInfoForm> valueList:values) {
|
|
|
+ list.addAll(valueList);
|
|
|
+ }
|
|
|
+ }else {//获取随机农户集
|
|
|
+ list = getGoodsInfos(helpMap,resultKeys);
|
|
|
+ }
|
|
|
+ List<GoodsInfoForm> resultList = getRandom(resultMap,list,buyNum,pageNo);
|
|
|
+ return resultList;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取分组内的随机主体
|
|
|
+ * lym
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private List<GoodsInfoForm> getGoodsInfos(Map<String,List<GoodsInfoForm>> helpMap,Collection<String> resultKeys){
|
|
|
+ Collection<List<GoodsInfoForm>> values = helpMap.values();
|
|
|
+ List<GoodsInfoForm> list = new ArrayList<>();
|
|
|
+ for (List<GoodsInfoForm> valueList:values){ //获取随机主体集
|
|
|
+ int number = new Random().nextInt(valueList.size());
|
|
|
+ GoodsInfoForm info = valueList.get(number);
|
|
|
+ if (resultKeys != null && resultKeys.contains(info.getGuid())){
|
|
|
+ number += 1;
|
|
|
+ if (valueList.size() > 1 && number != valueList.size()){
|
|
|
+ info = valueList.get(valueList.size() -1);
|
|
|
+ }else {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ list.add(info);
|
|
|
+ }
|
|
|
+ return list;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * lym
|
|
|
+ * 随机分配主体
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private List<GoodsInfoForm> getRandom(Map<String,GoodsInfoForm> resultMap,List<GoodsInfoForm> list,int buyNum,Integer pageNo) {
|
|
|
+ Collection<String> resultKeys = resultMap.keySet();
|
|
|
+ List<GoodsInfoForm> resultList = new ArrayList<>();
|
|
|
+ int total = 0, unBuyNum = buyNum;
|
|
|
+
|
|
|
+ if (list.size() == 1) { //一个主体
|
|
|
+ GoodsInfoForm info = list.get(0);
|
|
|
+ int unsaledCarbonSkin = info.getUnsaledCarbonSkin().intValue();
|
|
|
+ if (unsaledCarbonSkin >= buyNum) { //大于购买量
|
|
|
+ info.setCarbonSkin(BigDecimal.valueOf(Double.valueOf(buyNum)));
|
|
|
+ resultList.add(info);
|
|
|
+ return resultList;
|
|
|
+ }
|
|
|
+ //只查询到一个随机主体,未达到四次追加,继续追加
|
|
|
+ info.setCarbonSkin(info.getUnsaledCarbonSkin());
|
|
|
+ resultList.add(info);
|
|
|
+ resultMap.put(info.getGuid(), info);
|
|
|
+ total += unsaledCarbonSkin;
|
|
|
+ unBuyNum = unBuyNum - unsaledCarbonSkin;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (list.size() > 1) { //多个主体
|
|
|
+ for (int i = 0; i < list.size(); i++) {
|
|
|
+ GoodsInfoForm info = list.get(i);
|
|
|
+ if (resultKeys.contains(info.getGuid())) continue; //主体已存在
|
|
|
+ int unsaledCarbonSkin = info.getUnsaledCarbonSkin().intValue();
|
|
|
+ if (unsaledCarbonSkin >= unBuyNum) { //大于购买量
|
|
|
+ info.setCarbonSkin(BigDecimal.valueOf(Double.valueOf(buyNum)));
|
|
|
+ total += unBuyNum;
|
|
|
+ unBuyNum = unBuyNum - unBuyNum;
|
|
|
+ } else {
|
|
|
+ info.setCarbonSkin(info.getUnsaledCarbonSkin());
|
|
|
+ total += unsaledCarbonSkin;
|
|
|
+ unBuyNum = unBuyNum - unsaledCarbonSkin;
|
|
|
+ }
|
|
|
+ resultMap.put(info.getGuid(), info);
|
|
|
+ if (total == buyNum) break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //列表循环完,分配不够,追加主体
|
|
|
+ if (total < buyNum){
|
|
|
+ if (pageNo == 4){
|
|
|
+ throw new GlobalException(1,"购买量过大");
|
|
|
+ }
|
|
|
+ pageNo = pageNo + 1;
|
|
|
+ Map<String,List<GoodsInfoForm>> farmList = selectGoodsInfo(pageNo,unBuyNum);
|
|
|
+ if (farmList != null && farmList.size() > 0) {
|
|
|
+ getRandomPY(resultMap, farmList, unBuyNum, pageNo);
|
|
|
+ }else {
|
|
|
+ throw new GlobalException(1,"碳汇量不够购买量");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ resultList = new ArrayList<>(resultMap.values());
|
|
|
+ return resultList;
|
|
|
+ }
|
|
|
+}
|