package com.cfg.shop.web;

import com.cfg.shop.domain.OrderItem;
import com.cfg.shop.domain.Product;
import com.cfg.shop.domain.ProductInfo;
import com.cfg.shop.service.ShopService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletResponse;
import java.security.Principal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by yhp on 2017/7/2.
 */
@RestController
@RequestMapping("/")
public class ShopController {

    @Autowired
    ShopService shopService;

    @Autowired
    UserDetailsService userDetailsService;

    private static final Logger logger = LoggerFactory.getLogger(ShopController.class);  //logger here can help us to fix and trace problems

    /**
     * to show the product information
     * in real world  we can not just show all data on one page
     * @return
     */
    @RequestMapping(value = "/show", method = RequestMethod.GET)
    public Map<String, Object> showProducts() {
        Map<String, Object> responseJson = new HashMap<String, Object>();
        List<ProductInfo> ProductList = shopService.getProductStoreList();   //show all stock products here no paging need as only 2 products and small amount
        responseJson.put("ProductInfos", ProductList);
        return responseJson;
    }

    /**
     * client order process
     *
     * @param OrderItems          orderitem list fromc client
     * @param httpServletResponse response need to transfer data to client
     * @return
     */
    @RequestMapping("/order")
    public Map<String, Object> Order(@RequestBody List<OrderItem> OrderItems, HttpServletResponse httpServletResponse) {
        Map<String, Object> responseJson = new HashMap<String, Object>();
        String error = "";
        String OrderNo = shopService.createOrderNo();
        List<Product> productList = new ArrayList<>();
        for (OrderItem orderItem : OrderItems) {
            Long amount = orderItem.getPurchasedNumber();
            if (amount <= 0) continue;  //amount ==0 ,no need to do next work for it
            Product product = shopService.getProductByNo(orderItem.getProductNo());
            if (product == null) {
                error = "Cannot find the product";
                logger.error(error);
                responseJson.put("error", error);
                break;

            }
            String result = shopService.checkStockProductAmount(product, orderItem.getPurchasedNumber());  //check the stock and to see if enough products exists
            if (result != null) {
                logger.error(result);
                responseJson.put("error", result);
                break;
            } else {
                result = shopService.buyProduct(OrderNo, product, orderItem.getPurchasedNumber());// process the buy product
                productList.add(product);
                if (result != null) {
                    logger.error(result);
                    responseJson.put("error", result);
                    result = shopService.recoverProduct(OrderNo, productList);  //when one products process failed here ,we need to recover all products belong to the order
                    break;
                }
            }
        }
        List<ProductInfo> ProductList = shopService.getProductStoreList();
        httpServletResponse.setStatus(HttpServletResponse.SC_OK);
        responseJson.put("ProductInfos", ProductList);
        responseJson.put("SummaryInfo", OrderItems);
        responseJson.put("OrderNo", OrderNo);
        return responseJson;
    }

    /**
     * for client to access and view the order information
     * here we need to check if the order no is invalid
     * @param orderNo
     * @param httpServletResponse
     * @return
     */
    @RequestMapping(value = "/queryOrder/{orderNo}", method = RequestMethod.GET)
    public Map<String, Object> QueryOrder(@PathVariable String orderNo, HttpServletResponse httpServletResponse) {
        Map<String, Object> responseJson = new HashMap<String, Object>();

        List<OrderItem> OrderItems = shopService.queryOrder(orderNo);
        if (OrderItems != null && OrderItems.isEmpty()) {
            responseJson.put("error", "invalid orderNo.");
        } else {
            responseJson.put("orderItems", OrderItems);
        }
        httpServletResponse.setStatus(HttpServletResponse.SC_OK);
        return responseJson;
    }

}

@@ -1979,16 +1979,244 @@
 r = %22%22;%0A
+        if(OrderItems==null)%0A        %7B%0A            error = %22input error,pleas contact the administrator%22;%0A            logger.error(error);%0A            responseJson.put(%22error%22, error);%0A            return responseJson;%0A        %7D%0A
         
@@ -4478,16 +4478,268 @@
 ject%3E();
+%0A        String error = %22%22;%0A        if(orderNo==null)%0A        %7B%0A            error = %22input error,pleas contact the administrator%22;%0A            logger.error(error);%0A            responseJson.put(%22error%22, error);%0A            return responseJson;%0A        %7D
 %0A%0A      

add some parameter check