307 lines
12 KiB
Plaintext
307 lines
12 KiB
Plaintext
package com.nationalelectric.greenH5;
|
||
|
||
import java.math.BigDecimal;
|
||
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 java.util.concurrent.locks.ReentrantLock;
|
||
|
||
import javax.annotation.Resource;
|
||
|
||
import org.springframework.beans.BeanUtils;
|
||
import org.springframework.jdbc.core.BeanPropertyRowMapper;
|
||
import org.springframework.stereotype.Controller;
|
||
import org.springframework.transaction.annotation.Transactional;
|
||
import org.springframework.transaction.interceptor.TransactionAspectSupport;
|
||
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.ResponseBody;
|
||
|
||
import com.jysoft.unipush.AliasManage;
|
||
import com.nationalelectirc.Constant.Constant;
|
||
import com.nationalelectirc.utils.RestResult;
|
||
import com.nationalelectric.greenH5.DTO.GreenFoodOrderDetailDTO;
|
||
import com.nationalelectric.greenH5.DTO.GreenFoodOrderListDTO;
|
||
import com.nationalelectric.greenH5.bizc.IGreenFoodOrderDetailBizc;
|
||
import com.nationalelectric.greenH5.bizc.IGreenOperateLogBizc;
|
||
import com.nationalelectric.greenH5.enums.OrderStatus;
|
||
import com.nationalelectric.greenH5.enums.PayStatus;
|
||
import com.nationalelectric.greenH5.enums.TakeStatus;
|
||
import com.nationalelectric.greenH5.po.GreenDiningRoom;
|
||
import com.nationalelectric.greenH5.po.GreenFoodOrderDetail;
|
||
import com.nationalelectric.greenH5.po.GreenFoodOrderList;
|
||
import com.nationalelectric.greenH5.po.GreenUserInfo;
|
||
import com.nationalelectric.greenH5.utils.DateTime;
|
||
import com.nationalelectric.greenH5.utils.DateUtil;
|
||
import com.sgcc.uap.persistence.IHibernateDao;
|
||
|
||
import com.sgcc.uap.service.validator.ServiceValidatorBaseException;
|
||
|
||
|
||
|
||
|
||
/**
|
||
* <b>概述</b>:<br>
|
||
* <p>
|
||
* <b>功能</b>:<br>
|
||
*
|
||
* @author chenweikang
|
||
*/
|
||
@Controller
|
||
@RequestMapping("/greenFoodOrderList")
|
||
public class GreenFoodOrderListController extends GreenBaseController {
|
||
|
||
|
||
@Resource
|
||
private IHibernateDao hibernateDao;
|
||
/**
|
||
* greenfoodorderdetail GreenFoodOrderDetail逻辑构件
|
||
*/
|
||
@Resource
|
||
private IGreenFoodOrderDetailBizc greenfoodorderdetailBizc;
|
||
@Resource
|
||
private IGreenOperateLogBizc greenOperateLogBizc;
|
||
@Resource
|
||
private AliasManageController aliasManageController;
|
||
|
||
private ReentrantLock lock = new ReentrantLock();
|
||
|
||
/**提交订单*/
|
||
|
||
@SuppressWarnings("deprecation")
|
||
@RequestMapping(value="/submitOrder", method = RequestMethod.POST)
|
||
@Transactional(rollbackFor=Exception.class)
|
||
@ResponseBody
|
||
public RestResult submit(@RequestBody GreenFoodOrderListDTO greenfoodorderlists){
|
||
|
||
try {
|
||
|
||
lock.lock();
|
||
|
||
|
||
String userId = greenfoodorderlists.getUserId();
|
||
GreenUserInfo info = getUserInfo(userId);
|
||
if(info==null){
|
||
return new RestResult(Constant.FAILED, "非法用户");
|
||
}
|
||
|
||
int week = DateUtil.getWeek(new Date());
|
||
if(week==4||week==6||week==7){
|
||
return new RestResult(Constant.FAILED,"今日不可预定!");
|
||
}
|
||
|
||
//一次查询 详情中的菜品
|
||
StringBuilder stringBuilder = new StringBuilder();
|
||
stringBuilder.append("(");
|
||
//String stringId = null;
|
||
|
||
|
||
for (GreenFoodOrderDetailDTO orderDetail : greenfoodorderlists.getOrderDetails()) {
|
||
|
||
String goodsId = String.valueOf(orderDetail.getGoodsId());
|
||
stringBuilder.append(goodsId + ",");
|
||
}
|
||
String substring = null;
|
||
if (stringBuilder != null) {
|
||
substring = stringBuilder.substring(0, stringBuilder.length()-1) +")";
|
||
}
|
||
|
||
|
||
|
||
String sql = "select * from GREEN_FOODS_KINDS where id in " + substring;
|
||
|
||
List<Map<String,Object>> list1 = hibernateDao.queryForListWithSql(sql);
|
||
|
||
//加锁 防止扣负
|
||
Long diningRoomId = greenfoodorderlists.getDiningRoomId();
|
||
List<Map<String,Object>> menuList = null;
|
||
|
||
//计算价钱
|
||
String Id = null;
|
||
BigDecimal scale = BigDecimal.ZERO;
|
||
BigDecimal totalMoney = BigDecimal.ZERO.setScale(2, BigDecimal.ROUND_UP);
|
||
BigDecimal subNum = BigDecimal.ZERO.setScale(2, BigDecimal.ROUND_UP);
|
||
for (int i = 0; i < greenfoodorderlists.getOrderDetails().size(); i++) {
|
||
|
||
for (int j = 0; j < list1.size(); j++) {
|
||
Id = list1.get(j).get("id").toString();
|
||
String goodsId = String.valueOf(greenfoodorderlists.getOrderDetails().get(i).getGoodsId());
|
||
|
||
if (Id.equals(goodsId)) {
|
||
|
||
String num = greenfoodorderlists.getOrderDetails().get(i).getGoodsNum();
|
||
String price = list1.get(j).get("price").toString();
|
||
BigDecimal numDecimal = new BigDecimal(num);
|
||
BigDecimal priceDecimal = new BigDecimal(price);
|
||
scale = numDecimal.multiply(priceDecimal).setScale(2,BigDecimal.ROUND_UP);
|
||
totalMoney = totalMoney.add(scale);
|
||
String sqlStr = "SELECT a.quota qt,a.*"
|
||
+ " FROM GREEN_FOODS_VARIETY a left join GREEN_FOODS_KINDS b on a.kinds_id = b.id"
|
||
+ " where a.is_deleted = 'N' and a.location_id = ? and kinds_id = ?"
|
||
+ " and a.upload_date = to_char(sysdate,'yyyy-mm-dd') ";
|
||
menuList = hibernateDao.queryForListWithSql(sqlStr,new Object[]{diningRoomId, Long.valueOf(goodsId)});
|
||
// String quotaString = greenfoodorderlists.getOrderDetails().get(i).getQuota();
|
||
String quotaString = null;
|
||
if(menuList.size()>0){
|
||
quotaString = menuList.get(0).get("qt").toString();
|
||
}else {
|
||
quotaString = "10";
|
||
}
|
||
Integer quotaInteger = Integer.valueOf(quotaString);
|
||
if (quotaInteger > 0) {
|
||
String quotaSql = "SELECT d.id as \"id\",d.order_id as \"order_id\",d.goods_id as \"goods_id\",d.goods_num as \"goods_num\",d.creator as \"creator\","
|
||
+ "d.modifier as \"modifier\",d.gmt_created as \"gmt_created\",d.gmt_modified as \"gmt_modified\",d.is_deleted as \"is_deleted\","
|
||
+ "l.user_id as \"user_id\""
|
||
+ " FROM GREEN_FOOD_ORDER_DETAIL d LEFT JOIN GREEN_FOOD_ORDER_LIST l on l.id = d.order_id"
|
||
+ " WHERE l.user_id = ? AND l.dining_room_id = ? AND l.order_status = 1"
|
||
+ " AND d.GOODS_ID = ?"
|
||
+ " AND to_char(d.gmt_created,'yyyy-mm') = to_char(sysdate,'yyyy-mm') ";
|
||
List<Map<String,Object>> quotaList = hibernateDao.queryForListWithSql(quotaSql,new Object[]{userId, diningRoomId, Long.valueOf(goodsId)} );
|
||
int quotaInt = 0;
|
||
if (!quotaList.isEmpty()) {
|
||
for (Map<String, Object> map : quotaList) {
|
||
Integer goodsNum = Integer.valueOf(String.valueOf(map.get("goods_num")));
|
||
quotaInt = quotaInt + goodsNum;
|
||
}
|
||
}
|
||
|
||
if (Integer.valueOf(num) + quotaInt > quotaInteger) {
|
||
return new RestResult(Constant.FAILED,"已超出今日菜品限购数量!");
|
||
}
|
||
}
|
||
|
||
if (menuList != null && !menuList.isEmpty()) {
|
||
Integer noBuyNum = Integer.parseInt(menuList.get(0).get("no_buy_num").toString()) ;
|
||
if (noBuyNum != null) {
|
||
BigDecimal noBuyNumDecimal = new BigDecimal(noBuyNum);
|
||
if(numDecimal.intValue()>noBuyNumDecimal.intValue()){
|
||
return new RestResult(Constant.FAILED,"订购数量超剩余数量!");
|
||
}
|
||
subNum = noBuyNumDecimal.subtract(numDecimal);
|
||
hibernateDao.updateWithSql("update GREEN_FOODS_VARIETY set no_buy_num = ? where is_deleted = 'N' and id = ?",new Object[]{ subNum,menuList.get(0).get("id")});
|
||
|
||
}
|
||
|
||
}
|
||
|
||
|
||
}
|
||
}
|
||
|
||
}
|
||
|
||
|
||
greenfoodorderlists.setPayPrice( String.valueOf(totalMoney));
|
||
greenfoodorderlists.setOrderTime(DateUtil.now());
|
||
greenfoodorderlists.setPayStatus(PayStatus.PAY_STATUS_SUCCESS.getName());
|
||
greenfoodorderlists.setTakeStatus(TakeStatus.TAKE_STATUS_CANCEL.getName());
|
||
greenfoodorderlists.setOrderStatus(OrderStatus.ORDER_STATUS_SUCCESS.getName());
|
||
greenfoodorderlists.setBuyerName(info.getRealName());
|
||
greenfoodorderlists.setBuyerPhone(info.getMobile());
|
||
//int week = DateUtil.getWeek(new Date());
|
||
String takeDay = "";
|
||
if(week==1){
|
||
takeDay = DateUtil.addDays(4, new Date()).toString("yyyy-MM-dd");
|
||
}else if(week==2){
|
||
takeDay = DateUtil.addDays(3, new Date()).toString("yyyy-MM-dd");
|
||
}else if(week==3){
|
||
takeDay = DateUtil.addDays(2, new Date()).toString("yyyy-MM-dd");
|
||
}
|
||
// else if(week==4){
|
||
// takeDay = DateUtil.addDays(6, new Date()).toString("yyyy-MM-dd");
|
||
// }else if(week==5){
|
||
// takeDay = DateUtil.addDays(5, new Date()).toString("yyyy-MM-dd");
|
||
// }else if(week==6){
|
||
// takeDay = DateUtil.addDays(4, new Date()).toString("yyyy-MM-dd");
|
||
// }else if(week==7){
|
||
// takeDay = DateUtil.addDays(3, new Date()).toString("yyyy-MM-dd");
|
||
// }
|
||
StringBuffer sb = new StringBuffer();
|
||
//查询订单
|
||
sb.append("select * from ( select to_char(DATE_NAME,'yyyy-mm-dd') as \"date_name\" from green_calendar"
|
||
+ " where to_char(DATE_NAME,'yyyy-mm-dd')>=? and isholiday='1' and IS_DELETE='N' order by DATE_NAME )where rownum<=30 ");
|
||
List<Map<String,Object>> calList = hibernateDao.queryForListWithSql(sb.toString()
|
||
,new Object[]{ takeDay});
|
||
boolean b = false;
|
||
do{
|
||
for(int i=0;i<calList.size();i++){
|
||
String date_name = (String) calList.get(i).get("date_name");
|
||
if(date_name.equals(takeDay)){
|
||
DateTime dt = new DateTime(takeDay,"yyyy-MM-dd");
|
||
Integer week2 = dt.getDayOfWeek();
|
||
if(week2==3){
|
||
dt = dt.addDays(2);
|
||
}else if(week2==5){
|
||
dt = dt.addDays(5);
|
||
}
|
||
takeDay = dt.toString("yyyy-MM-dd");
|
||
b=true;
|
||
}else{
|
||
b=false;
|
||
}
|
||
}
|
||
}while(b);
|
||
greenfoodorderlists.setTakeFoodTime(takeDay);
|
||
|
||
|
||
GreenFoodOrderList greenFoodOrderList = new GreenFoodOrderList();
|
||
BeanUtils.copyProperties(greenfoodorderlists, greenFoodOrderList);
|
||
greenFoodOrderList.setPayPrice(String.valueOf(totalMoney));
|
||
hibernateDao.saveObject(greenFoodOrderList);
|
||
|
||
|
||
|
||
//ArrayList<GreenFoodOrderDetail> list = new ArrayList<GreenFoodOrderDetail>();
|
||
for (GreenFoodOrderDetailDTO greenFoodOrderDetail : greenFoodOrderList.getOrderDetails()) {
|
||
if(greenFoodOrderList.getId() != null){
|
||
greenFoodOrderDetail.setOrderId(greenFoodOrderList.getId());
|
||
GreenFoodOrderDetail greenFoodOrderDetail2 = new GreenFoodOrderDetail();
|
||
BeanUtils.copyProperties(greenFoodOrderDetail, greenFoodOrderDetail2);
|
||
//list.add(greenFoodOrderDetail);
|
||
greenfoodorderdetailBizc.add(greenFoodOrderDetail2);
|
||
}
|
||
}
|
||
HashMap<String, java.lang.Object> data = new HashMap<String,Object>();
|
||
data.put("orderId", greenfoodorderlists.getId());
|
||
String title = "外卖预订";
|
||
String text = "您好,您已成功预订外卖,取货地点:"+greenFoodOrderList.getTakeFoodPlace()+",预计取货日期:"+greenFoodOrderList.getTakeFoodTime()+",请注意查看。";
|
||
String url = "/pages/take-out2/take-out3?currentTabIndex=1";
|
||
aliasManageController.pushToSingle(greenFoodOrderList.getUserId(), title, text, url);
|
||
|
||
String role = "14002";//外卖受理权限
|
||
String getUserSql = "SELECT u.id,u.ISC_ID FROM green_user_info u "
|
||
+ " LEFT JOIN green_user_role_rel r ON u.id = r.user_id "
|
||
+ " LEFT JOIN green_role_permission p ON p.role_id = r.role_id "
|
||
+ " WHERE p.permission_id = ? ";
|
||
List<Map<String,Object>> userList2 = hibernateDao.queryForListWithSql(getUserSql, new Object[]{role});
|
||
title = "外卖受理";
|
||
text = "您好,您有新的外卖订单,请注意查看。";
|
||
url = "/pages/today-order/today-order";
|
||
aliasManageController.pushToSingleBatch(userList2, title, text, url);
|
||
|
||
|
||
|
||
//greenOperateLogBizc.addLog(Constant.TAKE_OUT_FOOD, Constant.SUBMIT, Constant.OPERATE_SUCCESS, "提交外卖订单", Constant.LOG_OPERATE, greenfoodorderlists.getUserId());
|
||
return new RestResult(Constant.SUCCESS,"成功",data);
|
||
}catch (ServiceValidatorBaseException e) {
|
||
throw e;
|
||
}catch (Exception e) {
|
||
e.printStackTrace();
|
||
//greenOperateLogBizc.addLog(Constant.TAKE_OUT_FOOD, Constant.SUBMIT, Constant.OPERATE_FAIL, "提交外卖订单", Constant.LOG_ERROR, greenfoodorderlists.getUserId());
|
||
//设置手动回滚
|
||
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
|
||
return new RestResult(Constant.FAILED,"返回异常");
|
||
}finally {
|
||
if (lock.isLocked()) {
|
||
lock.unlock();
|
||
}
|
||
|
||
}
|
||
}
|
||
|
||
}
|