Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2674 vikas 1
package in.shop2020.serving.controllers;
2
 
3
import in.shop2020.model.v1.order.Order;
4
import in.shop2020.model.v1.order.OrderStatus;
2714 vikas 5
import in.shop2020.model.v1.user.Cart;
2674 vikas 6
import in.shop2020.model.v1.user.Line;
7
import in.shop2020.model.v1.user.User;
8
import in.shop2020.thrift.clients.TransactionServiceClient;
9
import in.shop2020.thrift.clients.UserContextServiceClient;
10
 
11
import java.text.SimpleDateFormat;
2714 vikas 12
import java.util.Arrays;
2674 vikas 13
import java.util.Date;
14
import java.util.List;
15
 
16
import org.apache.log4j.Logger;
17
import org.apache.struts2.convention.annotation.Result;
18
import org.apache.struts2.convention.annotation.Results;
19
 
20
/**
21
 * @author vikas
22
 *
23
 */
24
@Results({
25
    @Result(name = "input", location = "/", type = "redirect")
26
})
27
 
28
@SuppressWarnings("serial")
29
public class OrderCustomerInputController extends BaseController {
30
    private static Logger log = Logger.getLogger(Class.class);
31
    private String email;
32
    private long orderId;
33
    private User user;
34
    private long orderCount;
35
    private long completedOrderCount;
36
    private long openOrderCount;
37
    private long failedOrderCount;
38
    private String lastLogin;
39
    private double cartItems;
2714 vikas 40
    private String couponCode = "";
2674 vikas 41
 
42
    public OrderCustomerInputController(){
43
        super();
44
    }
45
 
46
    public String create() throws Exception {
47
        UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
48
        in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
49
        TransactionServiceClient transactionServiceClient = new TransactionServiceClient();
50
        if (email == null || email.isEmpty()) {
51
            if (orderId != 0) {
52
                Order order = transactionServiceClient.getClient().getOrder(orderId);
53
                user = userClient.getUserById(order.getCustomer_id());
54
            }
55
        }
56
        else {
57
            try {
58
                user = userClient.getUserByEmail(email);
59
            } catch (Exception e) {
60
                log.warn("Unable to get user service client", e);
61
                return "input";
62
            }
63
        }
64
        if (user == null || user.getUserId() <= 0) {
65
            addActionError("Invalid Input");
66
            return "input";
67
        }
68
 
69
        List<Order> allOrders = transactionServiceClient.getClient().getOrdersForCustomer(user.getUserId(), 0, (new Date()).getTime(), null);
70
        orderCount = allOrders.size();
71
 
2714 vikas 72
        List<OrderStatus> failedStatusList = Arrays.asList(new OrderStatus[] {
73
                OrderStatus.SALES_RET_RESHIPPED,
74
                OrderStatus.SALES_RET_REFUNDED,
75
                OrderStatus.DOA_INVALID_REFUNDED,
76
                OrderStatus.DOA_INVALID_RESHIPPED,
77
                OrderStatus.DOA_VALID_REFUNDED,
78
                OrderStatus.DOA_RESHIPPED,
79
                OrderStatus.REFUNDED,
80
                OrderStatus.REJECTED,
81
                OrderStatus.FAILED});
82
 
2674 vikas 83
        for (Order o : allOrders) {
2714 vikas 84
            if (o.getStatus() == OrderStatus.DELIVERY_SUCCESS) {
2674 vikas 85
                completedOrderCount++;
86
            }
2714 vikas 87
            else if (failedStatusList.contains(o.getStatus())) {
2674 vikas 88
                failedOrderCount++;
89
            }
90
            else {
91
                openOrderCount++;
92
            }
93
        }
94
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
95
        lastLogin = sdf.format(new Date(userClient.getUserState(user.getUserId()).getLastLogin()));
96
        try {
2714 vikas 97
            Cart cart = userClient.getCurrentCart(user.getUserId());
98
            if (cart.getCouponCode() != null) {
99
                couponCode = cart.getCouponCode();
100
            }
101
            for (Line line : cart.getLines()) {
2674 vikas 102
                cartItems += line.getQuantity();
103
            }
104
        } catch (Exception e) {
105
            log.warn("No cart assigned for this user", e);
106
        }
107
        return "index";
108
    }
109
 
110
    public void setEmail(String email) {
111
        this.email = email;
112
    }
113
 
114
    public String getEmail() {
115
        return email;
116
    }
117
 
118
    public void setOrderId(String orderId) {
119
        try {
120
            this.orderId = Long.parseLong(orderId);
121
        }
122
        catch (NumberFormatException e) {
123
            log.error(e);
124
        }
125
    }
126
 
127
    public Long getOrderId() {
128
        return orderId;
129
    }
130
 
131
    public User getUser() {
132
        return user;
133
    }
134
 
135
    public long getOrderCount() {
136
        return orderCount;
137
    }
138
 
139
    public long getCompletedOrderCount() {
140
        return completedOrderCount;
141
    }
142
 
143
    public long getOpenOrderCount() {
144
        return openOrderCount;
145
    }
146
 
147
    public long getFailedOrderCount() {
148
        return failedOrderCount;
149
    }
150
 
151
    public double getCartItems() {
152
        return cartItems;
153
    }
154
 
155
    public String getLastLogin() {
156
        return lastLogin;
157
    }
2714 vikas 158
 
159
    public String getCouponCode() {
160
        return couponCode;
161
    }
2674 vikas 162
}