package com.wangdong.controller;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.wangdong.dto.Good;
import com.wangdong.dto.Order;
import com.wangdong.dto.PayType;
import com.wangdong.dto.Status;
import com.wangdong.dto.User;
import com.wangdong.service.OrderService;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
@Controller
public class OrderController {
@Autowired
private JedisPool jedisPool;
@Autowired
private OrderService orderService;
@RequestMapping("login")
public String login(String uid,HttpServletRequest request) {
User user = orderService.getUserByUid(uid);
request.getSession().setAttribute("user", user);
return "success";
}
@RequestMapping("/")
public String toindex(HttpServletRequest request,Order order,Integer tid,Integer sid) {
User user = (User) request.getSession().getAttribute("user");
List<Order> olists=orderService.getOrdersList(order,tid,sid);
List<PayType> plist=orderService.getPayTypesList();
List<Status> slist=orderService.getStatusList();
Jedis jedis = jedisPool.getResource();
String oid = jedis.get("user"+user.getUid());
if(oid==null) {
jedis.close();
request.setAttribute("olists", olists);
request.setAttribute("order", order);
request.setAttribute("plists", plist);
request.setAttribute("slists", slist);
request.setAttribute("tid", tid);
request.setAttribute("sid", sid);
return "index";
}
Map<String, String> map=null;
if(tid==null&&sid==null) {
if(order.getOid()==""||order.getOid()==null) {
if(jedis.hgetAll(oid).isEmpty()) {
}else {
map = jedis.hgetAll(oid);
}
}
if(order.getOid()==oid) {
if(jedis.hgetAll(oid).isEmpty()) {
}else {
map = jedis.hgetAll(oid);
}
}
}
jedis.close();
request.setAttribute("olists", olists);
request.setAttribute("plists", plist);
request.setAttribute("slists", slist);
request.setAttribute("tid", tid);
request.setAttribute("sid", sid);
request.setAttribute("map", map);
request.setAttribute("order", order);
return "index";
}
@RequestMapping("toaddOrder")
public String toaddOrder(HttpServletRequest request) {
Jedis jedis = jedisPool.getResource();
User user = (User) request.getSession().getAttribute("user");
//生成订单号
Date data = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmm");
String format = dateFormat.format(data);
String oid=format+user.getUid();
//
jedis.set(user.getUid(), oid);
jedis.close();
//查询所有商品
List<Good> glist=orderService.getGoodsList();
//
request.setAttribute("glist", glist);
request.setAttribute("oid", oid);
request.setAttribute("user", user);
return "addOrder";
}
@RequestMapping("Recalculate_sumprice")
@ResponseBody
public double Recalculate_sumprice(String gids){
double sumprice=0.00;
if("".equals(gids)) {
return 0;
}
String[] gidss = gids.split(",");
for (String gid : gidss) {
Map<String, String> map=getGoodByGid(Integer.valueOf(gid));
sumprice+=Double.valueOf(map.get("gprice"));
}
return sumprice;
}
private Map<String, String> getGoodByGid(Integer gid) {
Jedis jedis = jedisPool.getResource();
Map<String, String> map = jedis.hgetAll("good"+gid);
if(map.isEmpty()) {
//如果map为代表redis没有储存此件商品信息,去数据库查询即可
Good good = orderService.getGoodsByGid(gid);
map = good.toMap();
//将结果存入redis
jedis.hmset("good"+gid, map);
}
jedis.close();
return map;
}
@RequestMapping("addOrder")
@ResponseBody
public Integer addOrder(Integer[] gids,HttpServletRequest request) {
double sumprice=0.00;
List<Good> glists=new ArrayList<>();
for (Integer gid : gids) {
Map<String, String> map=getGoodByGid(gid);
sumprice+=Double.valueOf(map.get("gprice"));
Good good = new Good();
good.setGid(gid);
glists.add(good);
}
Jedis jedis = jedisPool.getResource();
Order order = new Order();
User user = (User) request.getSession().getAttribute("user");
String oid = jedis.get(user.getUid()+"");
order.setOid(oid);
order.setSumprice(sumprice);
Date data = new Date();
order.setCreatetime(data);
Status status = new Status();
status.setSid(2);
order.setStatus(status);
order.setUser(user);
order.setGlists(glists);
try {
orderService.addOrder(order);
} catch (Exception e) {
return 0;
}
jedis.del(oid);
jedis.del("user"+user.getUid());
jedis.close();
return 1;
}
@RequestMapping("editOrder")
@ResponseBody
public Integer editOrder(String gids,HttpServletRequest request) {
User user = (User) request.getSession().getAttribute("user");
Map<String,String> map = new HashMap<>();
Jedis jedis = jedisPool.getResource();
String oid = jedis.get(user.getUid()+"");
jedis.set("user"+user.getUid(), oid);
map.put("oid", oid);
map.put("uid", user.getUid());
map.put("name", user.getName());
map.put("gids", gids);
Date data = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String format = dateFormat.format(data);
map.put("data", format);
map.put("pay_type", "待提交");
jedis.hmset(oid, map);
jedis.close();
return 1;
}
@RequestMapping("getOrderByOid")
@ResponseBody
public Order getOrderByOid(String oid) {
return orderService.getOrderByOid(oid);
}
@RequestMapping("pay")
public String pay(Order order,Integer tid,HttpServletRequest request) {
orderService.updateOrder(order,tid);
return "redirect:/";
}
@RequestMapping("deleteOrderByoid")
@ResponseBody
public Integer deleteOrderByoid(String oid) {
try {
orderService.deleteOrderByOid(oid);
} catch (Exception e) {
return 0;
}
return 1;
}
@RequestMapping("addRedisOrder")
@ResponseBody
public Integer addRedisOrder(String oid) throws Exception {
double sumprice=0.00;
List<Good> glists=new ArrayList<>();
Jedis jedis = jedisPool.getResource();
Map<String, String> map = jedis.hgetAll(oid);
String uid = map.get("uid");
User user = new User();
user.setUid(uid);
String data = map.get("data");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = sdf.parse(data);
String gids = map.get("gids");
String[] gidss = gids.split(",");
for (String gid : gidss) {
Map<String, String> map1=getGoodByGid(Integer.valueOf(gid));
sumprice+=Double.valueOf(map1.get("gprice"));
Good good = new Good();
good.setGid(Integer.valueOf(gid));
glists.add(good);
}
Order order = new Order();
order.setOid(oid);
order.setCreatetime(date);
order.setGlists(glists);
Status status = new Status();
status.setSid(2);
order.setStatus(status);
order.setSumprice(sumprice);
order.setUser(user);
try {
orderService.addOrder(order);
} catch (Exception e) {
return 0;
}
jedis.del(oid);
jedis.del("user"+uid);
jedis.close();
return 1;
}
@RequestMapping("toeditOrder")
public String toeditOrder(HttpServletRequest request) throws Exception {
List<Good> glist=orderService.getGoodsList();
User user = (User) request.getSession().getAttribute("user");
Jedis jedis = jedisPool.getResource();
String oid = jedis.get("user"+user.getUid());
Map<String, String> map = jedis.hgetAll(oid);
String data = map.get("data");
SimpleDateFormat sdf = new SimpleDateFor
评论1
最新资源