package com.zyh.factory.controller;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.zyh.factory.controller.io.OrderIO;
import com.zyh.factory.entity.OrderEntity;
import com.zyh.factory.entity.ProductSourceEntity;
import com.zyh.factory.entity.WorkEntity;
import com.zyh.factory.repositories.OrderRepository;
import com.zyh.factory.repositories.ProductRepository;
import com.zyh.factory.service.OrderService;
import com.zyh.factory.transman.TransMessage;


@RestController
@RequestMapping(value = "/order")
public class OrderController {

    @Autowired
    OrderRepository orderRepository;
    @Autowired
    ProductRepository productRepository;
//    @Autowired
//    ProductSourceRepository productSourceRepository;
    @Autowired
    OrderService orderService;

    @RequestMapping(method = RequestMethod.POST)
    public OrderEntity create(@RequestBody OrderEntity order, HttpServletResponse res) {
    	OrderIO orderIO = new OrderIO();
    	System.out.println("create order ");
    	if (orderIO.getTransMessage() == null) {
    		orderIO.setTransMessage(new TransMessage());
    	}
//    	OrderEntity order = orderService.create(orderIO.getTransMessage(), orderIO.getOrder());
    	orderService.create(orderIO.getTransMessage(), order);
    	if(orderIO.getTransMessage().shouStop()) {
    		res.setStatus(HttpServletResponse.SC_BAD_REQUEST);
    		return order;
    	}
    	if (order.getOrderWorks() != null) {
    		for (WorkEntity work : order.getOrderWorks()) {
    			work.setOrder(order);
    		}
    	}
    	for (WorkEntity w : order.getOrderWorks()) {
    		if (w.getProduct() != null && w.getProduct().getProductId() == 0 ) {
    			if (w.getProduct().getProductSource() != null) {
//    				System.out.println("save producsource");
    				for (ProductSourceEntity e: w.getProduct().getProductSource()) {
    					e.setProduct(w.getProduct());
    				}    				
				}
//    			System.out.println("save product");
    			productRepository.save(w.getProduct());
    		}
    	}
    	orderRepository.save(order);
        return order;
    }
    
    @RequestMapping(value = "/{orderId}", method = RequestMethod.POST)
    public OrderEntity updateOrder(@PathVariable long orderId, @RequestBody OrderEntity order, 
    		HttpServletResponse res) {
    	System.out.println("update order "+orderId);
    	TransMessage message = new TransMessage();
        //OrderEntity orderOld = orderRepository.findOne(orderId);
    	orderService.update(message, order);
    	order.setOrderId(orderId);
    	if(message.shouStop()) {
    		res.setStatus(HttpServletResponse.SC_BAD_REQUEST);
    		return order;
    	}
    	orderRepository.save(order);
        return order;
    }
    
    @RequestMapping(method = RequestMethod.GET)
    public Iterable<OrderEntity> orders() {
        return orderRepository.findAll();
    }

    @RequestMapping(value = "/{orderId}", method = RequestMethod.GET)
    public OrderEntity order(@PathVariable long orderId) {
        return orderRepository.findOne(orderId);
    }
    
//    @RequestMapping(method = RequestMethod.GET)
//    public Iterable<OrderEntity> orderByStatus(@RequestParam String orderStatus) {
//        return orderRepository.findByOrderStatus(orderStatus);
//    }
}

@@ -26,24 +26,48 @@
 ontroller;%0A%0A
+import java.util.List;%0A%0A
 import javax
@@ -3219,16 +3219,462 @@
 %0A    %7D%0A%0A
+    @RequestMapping(value = %22/summary/%7BorderStatus%7D%22, method = RequestMethod.GET)%0A    public Long orderSummary(@PathVariable String orderStatus) %7B%0A        return orderRepository.countByOrderStatus(orderStatus);%0A    %7D%0A    %0A    @RequestMapping(value = %22/summary%22, method = RequestMethod.GET)%0A    public List%3C?%3E orderSummary() %7B%0A//        return orderRepository.countGroupByOrderStatus();%0A    %09return orderRepository.countGroupByMonth();%0A    %7D%0A    %0A
     @Req

Update OrderController.java