package selling.sunshine.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import selling.sunshine.form.TimeRangeForm;
import selling.sunshine.model.CustomerOrder;
import selling.sunshine.model.OrderItem;
import selling.sunshine.service.IndentService;
import selling.sunshine.service.OrderService;
import selling.sunshine.service.impl.IndentServiceImpl;
import selling.sunshine.utils.ResponseCode;
import selling.sunshine.utils.ResultData;

import javax.validation.Valid;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by sunshine on 7/7/16.
 */
@RestController
@RequestMapping("/indent")
public class IndentController {
    private Logger logger = LoggerFactory.getLogger(IndentController.class);

    @Autowired
    private IndentService indentService;

    @Autowired
    private OrderService orderService;

    @RequestMapping(method = RequestMethod.GET, value = "/overview")
    public ModelAndView indent() {
        ModelAndView view = new ModelAndView();
        view.setViewName("/backend/finance/indent");
        return view;
    }

    @RequestMapping(method = RequestMethod.POST, value = "/overview")
    public ResultData indent(@Valid TimeRangeForm form, BindingResult result) {
        ResultData data = new ResultData();
        if (result.hasErrors()) {
            data.setResponseCode(ResponseCode.RESPONSE_ERROR);
            return data;
        }
        Map<String, Object> condition = new HashMap<>();
        condition.put("start", form.getStart());
        condition.put("end", form.getEnd());
        condition.put("blockFlag", false);
        List<Integer> status = new ArrayList<>(Arrays.asList(1, 2, 3, 4));
        condition.put("statusList", status);
        ResultData queryResponse = orderService.fetchOrderItem(condition);
        if (queryResponse.getResponseCode() == ResponseCode.RESPONSE_OK) {
            List<OrderItem> list = (List<OrderItem>) queryResponse.getData();
            indentService.produce(list);
        }
        condition.remove("statusList");
        condition.put("status", status);
        queryResponse = orderService.fetchCustomerOrder(condition);
        if (queryResponse.getResponseCode() == ResponseCode.RESPONSE_OK) {
            List<CustomerOrder> list = (List<CustomerOrder>) queryResponse.getData();
            indentService.produce(list);
        }
        String path = IndentController.class.getResource("/").getPath();
        String os = System.getProperty("os.name").toLowerCase();
        if(os.indexOf("windows") >= 0){
        	path = path.substring(1);
        }
        
        int index = path.lastIndexOf("/WEB-INF/classes/");
        String parent = path.substring(0, index);
        String directory = "/material/journal/indent";
        StringBuffer sb = new StringBuffer(parent).append(directory).append("/").append(form.getStart().replaceAll("-", ""));
        System.err.println(sb.toString());
        return data;
    }
}

@@ -445,17 +445,55 @@
 ndView;%0A
+import selling.sunshine.form.SortRule;
 %0A
-
 import s
@@ -711,64 +711,8 @@
 ce;%0A
-import selling.sunshine.service.impl.IndentServiceImpl;%0A
 impo
@@ -825,17 +825,43 @@
 .Valid;%0A
+import java.sql.Timestamp;
 %0A
-
 import j
@@ -1363,24 +1363,1289 @@
 lAndView();%0A
+        Map%3CString, Object%3E condition = new HashMap%3C%3E();%0A        List%3CInteger%3E status = new ArrayList%3C%3E(Arrays.asList(1, 2, 3, 4));%0A        condition.put(%22statusList%22, status);%0A        condition.put(%22blockFalg%22, false);%0A        List%3CSortRule%3E rule = new ArrayList%3C%3E();%0A        rule.add(new SortRule(%22create_time%22, %22asc%22));%0A        condition.put(%22sort%22, rule);%0A        ResultData queryResponse = orderService.fetchOrderItem(condition);%0A        Timestamp createAt = null;%0A        if (queryResponse.getResponseCode() == ResponseCode.RESPONSE_OK) %7B%0A            OrderItem item = ((List%3COrderItem%3E) queryResponse.getData()).get(0);%0A            createAt = item.getCreateAt();%0A        %7D%0A        condition.remove(%22statusList%22);%0A        condition.put(%22status%22, status);%0A        queryResponse = orderService.fetchCustomerOrder(condition);%0A        if (queryResponse.getResponseCode() == ResponseCode.RESPONSE_OK) %7B%0A            CustomerOrder item = ((List%3CCustomerOrder%3E) queryResponse.getData()).get(0);%0A            if (createAt == null %7C%7C createAt.after(item.getCreateAt())) %7B%0A                createAt = item.getCreateAt();%0A            %7D%0A        %7D%0A        SimpleDateFormat format = new SimpleDateFormat(%22yyyy-MM-dd%22);%0A        view.addObject(%22start%22, format.format(createAt));%0A
         view
@@ -4141,16 +4141,17 @@
       if
+ 
 (os.inde
@@ -4170,16 +4170,17 @@
 %22) %3E= 0)
+ 
 %7B%0A      
@@ -4185,9 +4185,12 @@
     
-%09
+    
 path
@@ -4221,24 +4221,16 @@
       %7D%0A
-        
 %0A       

add time query in indent