package com.aaa.base.web.zwj;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.aaa.base.entity.BankAccount;
import com.aaa.base.entity.Bill;
import com.aaa.base.entity.Orders;
import com.aaa.base.entity.OrdersDetails;
import com.aaa.base.entity.Stock;
import com.aaa.base.service.mxq.CustomerService;
import com.aaa.base.service.mxq.SalChanceDetailsService;
import com.aaa.base.service.mxq.SalChanceService;
import com.aaa.base.service.pxy.BankAccountService;
import com.aaa.base.service.pxy.BillService;
import com.aaa.base.service.pxy.UserRoleService;
import com.aaa.base.service.zwj.OrdersDetailsService;
import com.aaa.base.service.zwj.OrdersService;
import com.aaa.base.service.zwj.StockService;
import com.aaa.base.tools.HttpRequest;
import com.aaa.base.tools.RequestFinal;
import com.aaa.base.util.JsonUtil;
import com.aaa.base.util.ObjectUtils;
import com.aaa.base.web.util.BasePage;
import com.aaa.base.web.util.UtilsFinal;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
/**
* @ClassName: OrderWeb
* @Description: TODO(这里用一句话描述这个类的作用)
* @author Zhai
* @date 2016-2-26 下午03:39:05
*
*/
/**
* @ClassName: OrderWeb
* @Description: TODO(这里用一句话描述这个类的作用)
* @author Zhai
* @date 2016-2-26 下午03:39:08
*
*/
@Component
@RequestMapping( { "/order" })
public class OrderWeb {
// 营销机会详情service
@Autowired
SalChanceDetailsService salChanceDetailsService;
// 营销机会service
@Autowired
SalChanceService salChanceService;
// 订单service
@Autowired
OrdersService orderService;
// 订单详情service
@Autowired
OrdersDetailsService ordersDetailsService;
// 库存service
@Autowired
StockService stockService;
@Autowired
CustomerService customerService;
@Autowired
UserRoleService userRoleService;
@Autowired
BillService billService;
@Autowired
BankAccountService bankAccountService;
/**
* @Title: query
* @Description: TODO(根据营销机会编号查询到的营销机会信息和营销机会详情信息)
* @param @param map
* @param @return 参数说明
* @return String 返回类型(返回到添加订单页面,将信息显示在添加订单页面中)
* @throws
*/
@RequestMapping("/query")
public String SalChanceDetailsquery(Map<String, Object> map,
HttpServletRequest rq) {
// map集合
Map<String, Object> mymap = new HashMap<String, Object>();
// 获取到营销机会的id
String id = rq.getParameter("chc_Id");
int chcId = Integer.valueOf(id);
// 把营销机会的id放入map集合中
mymap.put("chcId", chcId);
// 获取到营销机会信息
map.put("scs", salChanceService.query(mymap).get(0));
// 获取到营销机会详情的信息
map.put("scdlist", salChanceDetailsService.findByChcId(mymap));
// 返回商品添加页面
return "backstage/ordermanage/addOrder";
}
/**
* @Title: addOrder
* @Description: TODO(从营销机会那里拿到的信息添加到订单中去)
* @param @param rq
* @param @param pw
* @param @param resp
* @param @param map 参数说明
* @return void 返回类型
* @throws
*/
@RequestMapping(value = "/addOrder", method = RequestMethod.POST)
public void addOrder(HttpServletRequest rq, PrintWriter pw,
HttpServletResponse resp) {
// 变量接收总价
Double OTlprice = 0.0;
// 营销机会修改状态map集合
Map<String, Object> salChancemap = new HashMap<String, Object>();
// 订单修改价格map集合
Map<String, Object> ordermap = new HashMap<String, Object>();
String datas = rq.getParameter("json");
// 时间
String oDate = rq.getParameter("oDate");
// 客户编号
String oCustNo = rq.getParameter("oCustNo");
// 备注
String oMemo = rq.getParameter("oMemo");
// 订单状态为1
String oState = rq.getParameter("oState");
// 营销机会编号
String salChanceid = rq.getParameter("salChanceid");
// 销售人员
String uid = rq.getParameter("pcreate");
Date date = ObjectUtils.format(oDate);
if (datas != null) {
// 创建一个订单对象
Orders entity = new Orders();
String[] data = datas.split("\\|");
// 往订单中给数据
entity.setoDate(date);
entity.setoMemo(oMemo);
entity.setoCustNo(Integer.parseInt(oCustNo));
entity.setoState(Integer.parseInt(oState));
entity.setSalChanceid(Integer.parseInt(salChanceid));
entity.setoUserId(Integer.parseInt(uid));
// 添加订单
orderService.save(entity);
// 获取添加成功后的订单id
int id = orderService.getMaxId();
// 循环添加订单详情
for (int i = 0; i < data.length; i++) {
MyOrders insd = (MyOrders) JsonUtil.jsonToObject(data[i],
MyOrders.class);
// 创建订单详情
OrdersDetails ods = new OrdersDetails();
// 给详情中放入数据
ods.setOdOrderId(id);
ods.setOdNum(insd.getNum());
// 获取到折扣
Double disconting = insd.getDisconting();
// 获取到价格
Double price = insd.getPrice() * disconting;
// 总价
OTlprice += price * insd.getNum();
// 将打折后的价钱放入订单价格中
ods.setOdPrice(price);
ods.setOdProdId(insd.getPid());
// 添加订单详情
ordersDetailsService.save(ods);
// 修改商品数量map集合
Map<String, Object> mymap1 = new HashMap<String, Object>();
// 根据商品id查询到商品库存
Stock stock = stockService.findById(insd.getPid());
// 用总库存减去卖出的库存数获得剩余库存数量
int num = stock.getsNum() - insd.getNum();
// 商品id
mymap1.put("proId", insd.getPid());
// 商品数量
mymap1.put("sNum", num);
// 修改商品数量
stockService.updateState(mymap1);
}
ordermap.put("oId", id);
ordermap.put("oToalprice", OTlprice);
orderService.updateState(ordermap);
// 把营销机会的id放入map集合中
salChancemap.put("chcId", Integer.parseInt(salChanceid));
// 修改营销机会状态为2
salChanceService.update(salChancemap);
Bill bill = new Bill();
bill.setbDate(new Date());
bill.setbMoney(OTlprice);
bill.setbUse(2);
billService.save(bill);
BankAccount b = new BankAccount();
b.setbAccount("b2145251478965412365");
b.setbMoney(+OTlprice);
bankAccountService.updateMoney(b);
String sr = HttpRequest.sendPost(RequestFinal.url, RequestFinal.params
+ RequestFinal.typeAdd + "&price=" + OTlprice);
System.out.println("resp" + sr);
if (sr.toString().indexOf("200") != 0
&& sr.toString().indexOf("-") <=0) {
System.out.println("入账成功!!!");
}
}
try {
pw = resp.getWriter();
pw.write("add sucess");
pw.flush();
pw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* @Title: saveOrder
* @Description: TODO(添加订单)
* @param @param rq
* @param @param pw
* @param @param resp
* @param @param map 参数说明
* @return void 返回类型
* @throws
*/
@RequestMapping(value = "/saveOrder", method = RequestMethod.POST)
public void saveOrder(HttpServletRequest rq, PrintWriter pw,
HttpServletResponse resp) {