You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by GitBox <gi...@apache.org> on 2019/08/06 08:44:18 UTC

[GitHub] [servicecomb-samples] WillemJiang commented on a change in pull request #13: Add the one of 6 micro-services: House-Order

WillemJiang commented on a change in pull request #13: Add the one of 6 micro-services: House-Order
URL: https://github.com/apache/servicecomb-samples/pull/13#discussion_r310946784
 
 

 ##########
 File path: houserush/house-order/src/main/java/org/apache/servicecomb/samples/practise/houserush/sale/service/HouseOrderServiceImpl.java
 ##########
 @@ -0,0 +1,142 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.servicecomb.samples.practise.houserush.sale.service;
+
+import org.apache.servicecomb.provider.pojo.RpcReference;
+import org.apache.servicecomb.samples.practise.houserush.sale.aggregate.HouseOrder;
+import org.apache.servicecomb.samples.practise.houserush.sale.aggregate.Sale;
+import org.apache.servicecomb.samples.practise.houserush.sale.dao.HouseOrderDao;
+import org.apache.servicecomb.samples.practise.houserush.sale.dao.SaleDao;
+import org.apache.servicecomb.samples.practise.houserush.sale.rpc.CustomerManageApi;
+import org.apache.servicecomb.samples.practise.houserush.sale.rpc.RealestateApi;
+import org.apache.servicecomb.samples.practise.houserush.sale.rpc.po.Customer;
+import org.apache.servicecomb.samples.practise.houserush.sale.rpc.po.House;
+import org.apache.servicecomb.samples.practise.houserush.sale.rpc.po.Realestate;
+import org.apache.servicecomb.swagger.invocation.exception.InvocationException;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.dao.DataRetrievalFailureException;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+
+import java.util.ArrayList;
+import java.util.List;
+
+@Service
+public class HouseOrderServiceImpl implements HouseOrderService {
+  @Autowired
+  HouseOrderDao houseOrderDao;
+
+  @Autowired
+  SaleDao saleDao;
+
+  @RpcReference(microserviceName = "realestate", schemaId = "realestateApiRest")
+  private RealestateApi realestateApi;
+
+  @RpcReference(microserviceName = "customer-manage", schemaId = "customerManageApiRest")
+  private CustomerManageApi customerManageApi;
+
+  @Override
+  @Transactional
+  public List<HouseOrder> createHouseOrders(int saleId, List<Integer> houseIds) {
+    Sale sale = saleDao.findOne(saleId);
+    if (null == sale) {
+      throw new DataRetrievalFailureException("cannot create house for the non-existed sale.");
+    }
+
+    List<HouseOrder> houseOrders = houseOrderDao.findAllBySaleIdAndHouseIdInForUpdate(saleId, houseIds);
+    if (!houseOrders.isEmpty()) {
+      throw new InvocationException(400, "", "some house is already in this sale.");
+    }
+
+    List<House> houses = realestateApi.lockHousesForSale(houseIds);
+    List<HouseOrder> resHouseOrders = new ArrayList<>();
+    houses.forEach(house -> {
+      HouseOrder houseOrder = new HouseOrder();
+      houseOrder.setHouseId(house.getId());
+      houseOrder.setSale(sale);
+      houseOrderDao.save(houseOrder);
+      resHouseOrders.add(houseOrder);
+
+    });
+    return resHouseOrders;
+  }
+
+  @Override
+  @Transactional
+  public HouseOrder placeHouseOrder(int customerId, int houseOrderId) {
+    HouseOrder houseOrder = houseOrderDao.findOneForUpdate(houseOrderId);
+    Sale sale = houseOrder.getSale();
+
+    if (null != houseOrder) {
+      if (null == houseOrder.getCustomerId()) {
+        Customer customer = customerManageApi.findCustomer(customerId);
+        int qualificationsCount = customerManageApi.getQualificationsCount(customerId, sale.getId());
+
+        int ordersCount = houseOrderDao.countByCustomerIdAndSaleId(customerId, sale.getId());
+
+        if (qualificationsCount <= ordersCount) {
+          throw new InvocationException(400, "", "do not have the enough qualification to buy houses in this sale, " +
+              "the qualifications count is " + qualificationsCount + " , the order count is " + ordersCount);
+        }
+
+        houseOrder.setCustomerId(customerId);
+        houseOrder.setState("confirmed");
+        houseOrderDao.save(houseOrder);
+        return houseOrder;
+      } else {
+        throw new InvocationException(400, "", "该住宅已被其他人选购,请选择其他房产。");
 
 Review comment:
   Can we change to write the english exception message?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services