//删卡之后获取到所有的赠送项目
List<CustomerGiveGood> customerGiveGoodList = new ArrayList<>();
CustomerClubCard customerClubCard = new CustomerClubCard();
//1有效 2无效
customerClubCard.setStatus(1);
CustomerDoc customerDoc = new CustomerDoc();
customerDoc.setId(customerDocId);
customerClubCard.setCustomerDoc(customerDoc);
List<CustomerClubCard> list = customerClubCardService.findList(customerClubCard);
for (CustomerClubCard card :list){
CustomerGiveGood customerGiveGood = new CustomerGiveGood();
customerGiveGood.setCustomerClubCardId(Long.parseLong(card.getId()));
//所有跟这个卡项关联的商品
List<CustomerGiveGood> giveGoods = customerGiveGoodService.findListHisGoodsNotNull(customerGiveGood);
if (giveGoods != null && giveGoods.size() > 0) {
customerGiveGoodList.addAll(giveGoods);
}
}
CustomerBill customerBill = new CustomerBill();
CustomerOrderDoc orderDoc = new CustomerOrderDoc();
orderDoc.setId(customerOrderDocId);
customerBill.setCustomerOrderDoc(orderDoc);
customerBill.setGiveStatus(1);
customerBill.setStatus(1);
//当前订单的有效的预存项
Response<List<CustomerBill>> bills = customerBillBusinessService.findList(customerBill);
HashMap<CustomerGiveGood,Integer> goodsMap = new HashMap<>();
if (bills.getData()!=null&&bills.getData().size()>0){
List<CustomerBill> haveIdBill = new ArrayList<>();
List<CustomerBill> notHaveIdBill = new ArrayList<>();
//将开单分成老数据和新数据 有id的为新数据
for (CustomerBill Bill :bills.getData()){
if (StringUtils.isNotEmpty(Bill.getGiveGoodsId())){
haveIdBill.add(Bill);
}else {
notHaveIdBill.add(Bill);
}
}
//分别处理
for (CustomerBill haveBill:haveIdBill){
CustomerGiveGood customerGiveGood = customerGiveGoodService.get(haveBill.getGiveGoodsId());
//卡被删除
if (customerGiveGood==null){
if (customerGiveGoodList.size()>0&&haveBill.getHisGoods()!=null){
for (CustomerGiveGood giveGood : customerGiveGoodList){
if (giveGood.getGoodId().equals(haveBill.getHisGoods().getId())&&giveGood.getNum()>0){
haveBill.setGiveGoodsId(giveGood.getId());
customerBillBusinessService.save(haveBill);
giveGood.setNum(giveGood.getNum()-1);
break;
}
if (giveGood==customerGiveGoodList.get(customerGiveGoodList.size()-1)){
Response<HisGoods> hisGoodsResponse = hisGoodsBusinessService.get(haveBill.getHisGoods().getId());
if (hisGoodsResponse.isSuccess()&&hisGoodsResponse.getData()!=null){
haveBill.setGiveStatus(0);
haveBill.setPrice(new BigDecimal(hisGoodsResponse.getData().getPrice()));
//加入金额
customerBillBusinessService.save(haveBill);
}
}
}
//没有预存项 将金额变回去
}else {
Response<HisGoods> hisGoodsResponse = hisGoodsBusinessService.get(haveBill.getHisGoods().getId());
if (hisGoodsResponse.isSuccess()&&hisGoodsResponse.getData()!=null){
haveBill.setGiveStatus(0);
haveBill.setPrice(new BigDecimal(hisGoodsResponse.getData().getPrice()));
//加入金额
customerBillBusinessService.save(haveBill);
}
}
}else {
//卡没删,但是得判断数量是否充足
}
}
for (CustomerBill notHaveBill:notHaveIdBill){
//老数据只有标记 用hisgoods
if (customerGiveGoodList.size()>0&¬HaveBill.getHisGoods()!=null){
for (CustomerGiveGood giveGood : customerGiveGoodList){
if (giveGood.getGoodId().equals(notHaveBill.getHisGoods().getId())&&giveGood.getNum()>0){
notHaveBill.setGiveGoodsId(giveGood.getId());
customerBillBusinessService.save(notHaveBill);
giveGood.setNum(giveGood.getNum()-1);
break;
}
if (giveGood==customerGiveGoodList.get(customerGiveGoodList.size()-1)){
Response<HisGoods> hisGoodsResponse = hisGoodsBusinessService.get(notHaveBill.getHisGoods().getId());
if (hisGoodsResponse.isSuccess()&&hisGoodsResponse.getData()!=null){
notHaveBill.setGiveStatus(0);
notHaveBill.setPrice(new BigDecimal(hisGoodsResponse.getData().getPrice()));
//加入金额
customerBillBusinessService.save(notHaveBill);
}
}
}
}
}
}