admin
2020-05-06 24a8d17e007545f7426c48352109aa1a9c6587ee
fanli/src/main/java/com/yeshi/fanli/service/impl/order/OrderServiceImpl.java
@@ -1,47 +1,22 @@
package com.yeshi.fanli.service.impl.order;
import java.io.Serializable;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock;
import javax.annotation.Resource;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.orm.hibernate4.HibernateCallback;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.yeshi.utils.NumberUtil;
import com.yeshi.fanli.dao.mybatis.order.OrderMapper;
import com.yeshi.fanli.dao.order.OrderDao;
import com.yeshi.fanli.entity.bus.user.HongBaoV2;
import com.yeshi.fanli.entity.bus.user.Order;
import com.yeshi.fanli.service.inter.config.ConfigService;
import com.yeshi.fanli.service.inter.hongbao.ThreeSaleSerivce;
import com.yeshi.fanli.service.inter.order.HongBaoOrderService;
import com.yeshi.fanli.service.inter.order.OrderService;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.TimeUtil;
import com.yeshi.fanli.util.Utils;
import com.yeshi.fanli.service.inter.user.invite.ThreeSaleSerivce;
@Service
public class OrderServiceImpl implements OrderService {
   @Resource
   private OrderDao orderDao;
   @Resource
   private OrderService orderService;
   @Resource
   private ConfigService configService;
@@ -55,24 +30,8 @@
   @Resource
   private HongBaoOrderService hongBaoOrderService;
   public List<Order> getOrderByUid(int index, long uid) {
      int start = index * Constant.PAGE_SIZE;
      return orderDao.list("from Order o where o.userInfo.id=? and o.money>0 order by o.createtime desc", start,
            Constant.PAGE_SIZE, new Serializable[] { uid });
   }
   public long getOrderCountByTime(long startTime, long endTime) {
      long count = orderDao.getCount("select count(*) from Order where createtime >=? and  createtime < ?",
            new Serializable[] { startTime, endTime });
      return count;
   }
   @Transactional(propagation = Propagation.NESTED, rollbackFor = Exception.class)
   // @Transactional(propagation = Propagation.NESTED, rollbackFor =
   // Exception.class)
   public boolean addOrder(Order order) throws Exception {
      order.setVersion(2);
      WriteLock writeLock = orderLock.writeLock();
@@ -84,11 +43,14 @@
         if (find != null) {
            return false;
         }
         Integer orderType = order.getOrderType();
         if (orderType == null) {
            orderType = Order.ORDER_TYPE_TAOBAO;
         }
         // 查找是否存在分享赚订单
         if (hongBaoOrderService.countByOrderNoAndHongBaoType(order.getOrderId(), HongBaoV2.TYPE_SHARE_GOODS) > 0)
         if (hongBaoOrderService.countByOrderNoAndHongBaoType(order.getOrderId(), HongBaoV2.TYPE_SHARE_GOODS, orderType) > 0)
            return false;
         orderDao.save(order);
         orderMapper.insertSelective(order);
      } finally {
         writeLock.unlock();
      }
@@ -119,279 +81,18 @@
      return true;
   }
   public boolean isfirstOrder(long uid) {
      List<Order> list = orderDao.list("from Order o where o.userInfo.id = ? and o.state = 1 ", 0, 1,
            new Serializable[] { uid });
      if (list.size() == 0) {
         return true;
      }
      return false;
   }
   public int getCount(long uid) {
      Long lcount = orderDao.getCount("select count(o.id) from Order o where o.userInfo.id = ? ",
            new Serializable[] { uid });
      return lcount.intValue();
   }
   public List<Order> getOrderList(String key, int index) {
      int start = index * Constant.PAGE_SIZE;
      boolean b = NumberUtil.isNumeric(key);
      if (b) {
         long uid = Long.parseLong(key);
         return orderDao.list(
               "from Order o where o.userInfo.id = ? or o.userInfo.nickName like ? order by o.id desc ", start,
               Constant.PAGE_SIZE, new Serializable[] { uid, "%" + key + "%" });
      }
      return orderDao.list("from Order o where o.userInfo.nickName like ? order by o.id desc", start,
            Constant.PAGE_SIZE, new Serializable[] { "%" + key + "%" });
   }
   public int getCount() {
      Long lcount = orderDao.getCount("select count(o.id) from Order o");
      return lcount.intValue();
   }
   @SuppressWarnings("unchecked")
   public Map<String, Integer> getnewOrderByDate(final int days, Date date) {
      final Map<String, Integer> map = new HashMap<String, Integer>();
      long timestampms = date.getTime();
      final long timestamps = timestampms / 1000;
      List<String> list = TimeUtil.getEmupDate(days, timestampms);
      for (String dataStr : list) {
         map.put(dataStr, 0);
      }
      return (Map<String, Integer>) orderDao.excute(new HibernateCallback<Map<String, Integer>>() {
         public Map<String, Integer> doInHibernate(Session session) throws HibernateException {
            SQLQuery sqlQuery = session.createSQLQuery(
                  "SELECT DATE(FROM_UNIXTIME(createtime/1000)) c,COUNT(*) FROM yeshi_ec_order WHERE DATE_SUB(FROM_UNIXTIME(?), INTERVAL ? DAY) <= DATE(FROM_UNIXTIME(createtime/1000)) AND DATE(FROM_UNIXTIME(createtime/1000)) <= FROM_UNIXTIME(?) GROUP BY c");
            sqlQuery.setParameter(0, timestamps);
            sqlQuery.setParameter(1, days);
            sqlQuery.setParameter(2, timestamps);
            List<Object[]> list = sqlQuery.list();
            for (Object[] objArr : list) {
               map.put(TimeUtil.getSimpleDate((Date) objArr[0]), ((BigInteger) objArr[1]).intValue());
            }
            return map;
         }
      });
   }
   @SuppressWarnings("unchecked")
   public Map<String, Integer> getnewOrderByMonth(final int months, final Date endDate) {
      List<String> monthList = Utils.getDateMonthList(months, endDate);
      final Map<String, Integer> map = new HashMap<String, Integer>();
      for (String monthStr : monthList) {
         map.put(monthStr, 0);
      }
      return (Map<String, Integer>) orderDao.excute(new HibernateCallback<Map<String, Integer>>() {
         public Map<String, Integer> doInHibernate(Session session) throws HibernateException {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(endDate);
            calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) + 1);
            calendar.set(Calendar.DAY_OF_MONTH, 1);
            long curTime = (calendar.getTime().getTime()) / 1000;
            SQLQuery sqlQuery = session.createSQLQuery(
                  "SELECT DATE_FORMAT(FROM_UNIXTIME(createtime/1000),'%Y-%m') months,COUNT(*) FROM yeshi_ec_order WHERE DATE(FROM_UNIXTIME(createtime/1000)) >= DATE_SUB(FROM_UNIXTIME(?),INTERVAL ? MONTH) AND DATE(FROM_UNIXTIME(createtime/1000)) < FROM_UNIXTIME(?) GROUP BY months");
            sqlQuery.setParameter(0, curTime);
            sqlQuery.setParameter(1, months);
            sqlQuery.setParameter(2, curTime);
            List<Object[]> list = sqlQuery.list();
            for (Object[] objArr : list) {
               map.put((String) objArr[0], ((BigInteger) objArr[1]).intValue());
            }
            return map;
         }
      });
   }
   @SuppressWarnings("unchecked")
   public Map<String, Integer> getOrderTotalByDate(int days, Date date) {
      final Map<String, Integer> map = new HashMap<String, Integer>();
      long timestampms = date.getTime();
      final List<String> list = TimeUtil.getEmupDate(days, timestampms);
      for (String dataStr : list) {
         map.put(dataStr, 0);
      }
      return (Map<String, Integer>) orderDao.excute(new HibernateCallback<Map<String, Integer>>() {
         public Map<String, Integer> doInHibernate(Session session) throws HibernateException {
            StringBuffer sb = new StringBuffer();
            int ii = 0;
            for (String day : list) {
               if (ii == 0) {
                  sb.append("SELECT '" + day + "' AS t,IFNULL(SUM(a.s),0) FROM yeshi_ec_order_day a WHERE a.c<= '"
                        + day + "' ");
               } else {
                  sb.append("UNION ALL SELECT '" + day
                        + "' AS t,IFNULL(SUM(a.s),0) FROM yeshi_ec_order_day a WHERE a.c<= '" + day + "' ");
               }
               ii++;
            }
            SQLQuery sqlQuery = session.createSQLQuery(sb.toString());
            List<Object[]> list = sqlQuery.list();
            for (Object[] objArr : list) {
               map.put(String.valueOf(objArr[0]), ((BigDecimal) objArr[1]).intValue());
            }
            return map;
         }
      });
   }
   @SuppressWarnings("unchecked")
   public Map<String, Integer> getOrderTotalByMonth(int months, Date date) {
      final List<String> monthList = Utils.getDateMonthList(months, date);
      final Map<String, Integer> map = new HashMap<String, Integer>();
      for (String monthStr : monthList) {
         map.put(monthStr, 0);
      }
      return (Map<String, Integer>) orderDao.excute(new HibernateCallback<Map<String, Integer>>() {
         public Map<String, Integer> doInHibernate(Session session) throws HibernateException {
            StringBuffer sb = new StringBuffer();
            int ii = 0;
            for (String month : monthList) {
               if (ii == 0) {
                  sb.append("SELECT '" + month
                        + "' AS t,IFNULL(SUM(a.s),0) FROM yeshi_ec_order_month a WHERE a.months<= '" + month
                        + "' ");
               } else {
                  sb.append("UNION ALL SELECT '" + month
                        + "' AS t,IFNULL(SUM(a.s),0) FROM yeshi_ec_order_month a WHERE a.months<= '" + month
                        + "' ");
               }
               ii++;
            }
            SQLQuery sqlQuery = session.createSQLQuery(sb.toString());
            List<Object[]> list = sqlQuery.list();
            for (Object[] objArr : list) {
               map.put((String) objArr[0], ((BigDecimal) objArr[1]).intValue());
            }
            return map;
         }
      });
   }
   public Order getOrder(final String orderid, final int orderType) {
      return (Order) orderDao.excute(new HibernateCallback<Order>() {
         public Order doInHibernate(Session session) throws HibernateException {
            Query query = session.createQuery("from Order o where o.orderId = ? and o.orderType = ? ");
            query.setParameter(0, orderid);
            query.setParameter(1, orderType);
            List<Order> list = query.list();
            if (list.size() > 0) {
               return list.get(0);
            }
            return null;
         }
      });
   }
   @Transactional
   public List<Order> setOrderState(final String orderid, final int orderType) {
      List<Order> orderList = orderMapper.selectOrderByOrderIdAndOrderType(orderid, orderType);
      if (orderList != null)
         for (Order order : orderList) {
            if (order.getDrawbackTime() == null || order.getDrawbackTime() <= 0) {
               Order updateOrder = new Order();
               updateOrder.setId(order.getId());
               updateOrder.setDrawbackTime(java.lang.System.currentTimeMillis());
               updateOrder.setState(Order.STATE_SHIXIAO);
               orderMapper.updateByPrimaryKeySelective(updateOrder);
               order.setDrawbackTime(updateOrder.getDrawbackTime());
               order.setState(updateOrder.getState());
            }
         }
      return orderList;
   }
   public int getCount(String key) {
      if (NumberUtil.isNumeric(key)) {
         if (key.trim().length() < 18) {
            long lk = Long.parseLong(key);
            return (int) orderDao.getCount(
                  "select count(*) from Order o where o.userInfo.id = ? or o.userInfo.nickName like ? ",
                  new Serializable[] { lk, "%" + key + "%" });
         } else {
            return (int) orderDao.getCount("select count(*) from Order o where  o.orderId like ? ",
                  new Serializable[] { "%" + key + "%" });
         }
      } else {
         return (int) orderDao.getCount("select count(*) from Order o where o.userInfo.nickName like ? ",
               new Serializable[] { "%" + key + "%" });
      }
   }
   public Order find(long id) {
      return orderDao.find(Order.class, id);
   }
   public void update(Order findOrder) {
      orderDao.update(findOrder);
   }
   @Override
   public Order findOrderByOrderIdAndType(String orderId, int type) {
      List<Order> list = orderDao.list("from Order o where o.orderId=? and o.orderType=?", 0, 1,
            new Serializable[] { orderId, type });
      if (list.size() > 0) {
         return list.get(0);
      }
      return null;
   }
   @Override
   public Order findOrderByOrderIdAndTypeAndVersion(String orderId, int type, int version) {
      List<Order> list = orderDao.list("from Order o where o.orderId=? and o.orderType=? and o.version=?", 0, 1,
            new Serializable[] { orderId, type, version });
      if (list.size() > 0) {
         return list.get(0);
      }
      return null;
      Order order = orderMapper.selectOrderByOrderIdAndOrderType(orderId, type);
      return order;
   }
   @Override
   public Order getSystemOrderByUid(int type, long uid) {
      List<Order> list = orderDao.list(
            "from Order o where o.userInfo.id = ? and o.orderType = ? and o.beizhu = '系统添加'", 0, 1,
            new Serializable[] { uid, type });
      if (list.size() > 0) {
      List<Order> list = orderMapper.listByUidAndOrderTypeAndBeiZhu(uid, type, "系统添加");
      if (list != null && list.size() > 0)
         return list.get(0);
      }
      return null;
   }
   @Override
   public List<Order> findOldOrderListAll() {
      List<Order> list = orderDao.list("from Order or where or.version=1");
      return list;
   }
   @Override
   public Order getLatestOrder() {
      List<Order> orderList = orderDao.list("from Order order order by order.thirdCreateTime desc", 0, 1, null);
      if (orderList == null || orderList.size() == 0)
         return null;
      return orderList.get(0);
   }
}