admin
2019-07-24 c8a99ed8218fe034e9c33236969fcbec9eac75bb
fanli/src/main/java/com/yeshi/fanli/service/impl/order/CommonOrderServiceImpl.java
@@ -13,28 +13,56 @@
import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.yeshi.utils.taobao.TbImgUtil;
import com.yeshi.fanli.dao.mybatis.order.CommonOrderGoodsMapper;
import com.yeshi.fanli.dao.mybatis.order.CommonOrderMapper;
import com.yeshi.fanli.dao.mybatis.order.CommonOrderTradeIdMapMapper;
import com.yeshi.fanli.dao.mybatis.taobao.TaoBaoWeiQuanOrderMapper;
import com.yeshi.fanli.entity.bus.user.HongBao;
import com.yeshi.fanli.dto.pdd.PDDGoodsDetail;
import com.yeshi.fanli.entity.bus.user.HongBaoV2;
import com.yeshi.fanli.entity.bus.user.UserInfo;
import com.yeshi.fanli.entity.bus.user.UserSystemCouponRecord;
import com.yeshi.fanli.entity.goods.CommonGoods;
import com.yeshi.fanli.entity.jd.JDGoods;
import com.yeshi.fanli.entity.jd.JDOrder;
import com.yeshi.fanli.entity.jd.JDOrderItem;
import com.yeshi.fanli.entity.order.CommonOrder;
import com.yeshi.fanli.entity.order.CommonOrderGoods;
import com.yeshi.fanli.entity.order.CommonOrderTradeIdMap;
import com.yeshi.fanli.entity.order.HongBaoOrder;
import com.yeshi.fanli.entity.pdd.PDDOrder;
import com.yeshi.fanli.entity.system.SystemCoupon.CouponTypeEnum;
import com.yeshi.fanli.entity.taobao.TaoBaoGoodsBrief;
import com.yeshi.fanli.entity.taobao.TaoBaoOrder;
import com.yeshi.fanli.entity.taobao.TaoBaoWeiQuanOrder;
import com.yeshi.fanli.exception.order.CommonOrderException;
import com.yeshi.fanli.exception.taobao.TaobaoGoodsDownException;
import com.yeshi.fanli.log.LogHelper;
import com.yeshi.fanli.service.inter.common.JumpDetailV2Service;
import com.yeshi.fanli.service.inter.config.ConfigService;
import com.yeshi.fanli.service.inter.config.SystemCouponService;
import com.yeshi.fanli.service.inter.goods.CommonGoodsService;
import com.yeshi.fanli.service.inter.hongbao.HongBaoV2Service;
import com.yeshi.fanli.service.inter.order.CommonOrderService;
import com.yeshi.fanli.service.inter.order.HongBaoOrderService;
import com.yeshi.fanli.service.inter.user.UserSystemCouponRecordService;
import com.yeshi.fanli.service.inter.user.UserSystemCouponService;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.MoneyBigDecimalUtil;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.factory.CommonOrderGoodsFactory;
import com.yeshi.fanli.util.factory.goods.CommonOrderFactory;
import com.yeshi.fanli.util.jd.JDApiUtil;
import com.yeshi.fanli.util.pinduoduo.PinDuoDuoApiUtil;
import com.yeshi.fanli.util.taobao.TaoBaoOrderUtil;
import com.yeshi.fanli.util.taobao.TaoBaoUtil;
import com.yeshi.fanli.util.taobao.TaoKeApiUtil;
import com.yeshi.fanli.vo.msg.ClientTextStyleVO;
import com.yeshi.fanli.vo.order.CommonOrderGoodsVO;
import com.yeshi.fanli.vo.order.CommonOrderVO;
import net.sf.json.JSONObject;
@Service
public class CommonOrderServiceImpl implements CommonOrderService {
@@ -47,6 +75,41 @@
   @Resource
   private CommonOrderGoodsMapper commonOrderGoodsMapper;
   @Resource
   private JumpDetailV2Service jumpDetailV2Service;
   @Resource
   private ConfigService configService;
   @Resource
   private UserSystemCouponService userSystemCouponService;
   @Resource
   private UserSystemCouponRecordService userSystemCouponRecordService;
   @Resource
   private SystemCouponService systemCouponService;
   @Resource
   private HongBaoV2Service hongBaoV2Service;
   @Resource
   private CommonOrderTradeIdMapMapper commonOrderTradeIdMapMapper;
   @Resource
   private CommonGoodsService commonGoodsService;
   @Resource
   private HongBaoOrderService hongBaoOrderService;
   // 奖励订单图片
   public final static String PIC_REWARD = "http://img.flqapp.com/resource/order/order_state_reward.png";
   // 免单状态图片
   public final static String PIC_FREE_ON = "http://img.flqapp.com/resource/order/order_state_freeing.png";
   public final static String PIC_FREE_FAIL = "http://img.flqapp.com/resource/order/order_state_free_fail.png";
   public final static String PIC_FREE_SUCCEED = "http://img.flqapp.com/resource/order/order_state_free_suc.png";
   @Override
   public int insert(CommonOrder record) {
@@ -82,15 +145,14 @@
   public List<CommonOrderVO> listGroupOrderNoByUid(long start, int count, Long uid, Integer state, Integer type,
         Integer orderState, String orderNo, String startTime, String endTime, Integer dateType)
         throws CommonOrderException {
      return commonOrderMapper.listGroupOrderNoByUid(start, count, uid, state, type, orderState, orderNo, startTime,
            endTime, dateType);
      return commonOrderMapper.listUserOrder(start, count, uid, state, type, orderState, orderNo, startTime, endTime,
            dateType);
   }
   @Override
   public long countGroupOrderNoByUid(Long uid, Integer state, Integer type, Integer orderState, String orderNo,
         String startTime, String endTime, Integer dateType) throws CommonOrderException {
      return commonOrderMapper.countGroupOrderNoByUid(uid, state, type, orderState, orderNo, startTime, endTime,
            dateType);
      return commonOrderMapper.countUserOrder(uid, state, type, orderState, orderNo, startTime, endTime, dateType);
   }
   @Override
@@ -109,31 +171,37 @@
      }
      // 商品信息
      List<CommonOrderVO> listGoods = commonOrderMapper.listByOrderNoAndType(listOrder);
      List<CommonOrderVO> listGoods = commonOrderMapper.listOrderGoodsInfo(listOrder);
      // 订单商品为空
      if (listGoods == null || listGoods.size() == 0) {
         return listOrder;
      }
      // 数据加工重新组织
      listDataFactory(listOrder, listGoods);
      listDataFactory(listOrder, listGoods, uid);
      return listOrder;
   }
   @Override
   public Map<String, BigDecimal> countOrderByHongBaoType(Long uid, Integer day) {
      return commonOrderMapper.countOrderByHongBaoType(uid, day);
   public Map<String, BigDecimal> countHistoryOrder(Long uid, Integer day) {
      return commonOrderMapper.countHistoryOrder(uid, day);
   }
   @Override
   public long countOrder(Long uid, Integer type, Integer day) {
      return commonOrderMapper.countBonusOrder(uid, type, day);
   public long countBonusOrderNumber(Long uid, Integer type, Integer day, String startTime, String endTime) {
      return commonOrderMapper.countBonusOrderNumber(uid, type, day, startTime, endTime);
   }
   @Override
   public BigDecimal countOrderMoney(Long uid, Integer type, Integer day) {
      return commonOrderMapper.countBonusOrderMoney(uid, type, day);
   public BigDecimal countBonusOrderMoney(Long uid, Integer type, Integer day, String startTime, String endTime) {
      return commonOrderMapper.countBonusOrderMoney(uid, type, day, startTime, endTime);
   }
   @Override
   public Map<String, Object> countBonusOrderMoneyAndNumber(Long uid, Integer type, Integer day, String startTime,
         String endTime) {
      return commonOrderMapper.countBonusOrderMoneyAndNumber(uid, type, day, startTime, endTime);
   }
   @Override
@@ -142,7 +210,15 @@
      return commonOrderMapper.countByUidAndOrderState(uid, type, startTime, endTime, day);
   }
   public void listDataFactory(List<CommonOrderVO> listOrder, List<CommonOrderVO> listGoods) {
   public void listDataFactory(List<CommonOrderVO> listOrder, List<CommonOrderVO> listGoods, Long uid) {
      List<String> listNo = new ArrayList<String>();
      for (CommonOrderVO commonOrderVO : listOrder) {
         listNo.add(commonOrderVO.getOrderNo());
      }
      // 已经使用券订单
      List<UserSystemCouponRecord> couponRecordList = userSystemCouponRecordService.getRecordByOrderNoList(listNo);
      /* 组合商品信息 */
      for (CommonOrderVO commonOrder : listGoods) {
@@ -171,6 +247,11 @@
                  e.printStackTrace();
               }
               String picture = commonGoodsVO.getPicture();
               if (!StringUtil.isNullOrEmpty(picture) && !picture.contains("320x320")) {
                  commonGoodsVO.setPicture(TbImgUtil.getTBSize320Img(picture));
               }
               Integer hongBaoType = order.getHongBaoType();
               // 邀请订单信息保护
               if (HongBaoV2.TYPE_YAOQING == hongBaoType || HongBaoV2.TYPE_YIJI == hongBaoType
@@ -184,9 +265,15 @@
                  commonGoodsVO.setGoodsTitle(titleMap);
               }
               // 购买数量
               commonGoodsVO.setActualCount(commonOrder.getTotalCount() + "件");
               String settlement = commonOrder.getTotalSettlement().setScale(2, BigDecimal.ROUND_DOWN).toString();
               commonGoodsVO.setActualPay("实付款:¥" + settlement);
               BigDecimal totalSettlement = commonOrder.getTotalSettlement();
               if (totalSettlement == null || totalSettlement.compareTo(new BigDecimal(0)) <= 0) {
                  totalSettlement = commonOrder.getTotalPayment();
               }
               // 实付款
               commonGoodsVO.setActualPay("付款金额:¥" + totalSettlement);
               listOrderGoods.add(commonGoodsVO);
@@ -208,15 +295,16 @@
      SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd HH:mm");
      SimpleDateFormat formatday = new SimpleDateFormat("yyyy.MM.dd");
      // 是否存在奖励券
      boolean hasRewardCoupon = userSystemCouponService.hasRewardCoupon(uid);
      /* 组织订单状态 等信息 */
      for (CommonOrderVO order : listOrder) {
         Date createTime = order.getCreateTime();
         if (createTime != null) {
            order.setObtainTime(createTime.getTime());
         }
         Date thirdCreateTime = order.getThirdCreateTime();
         if (thirdCreateTime != null) {
            order.setDownTime("下单时间:" + format.format(thirdCreateTime));
            order.setObtainTime(thirdCreateTime.getTime());
         }
         Date settleTime = order.getSettleTime();
@@ -228,9 +316,23 @@
         String orderStateContent = "";
         Map<String, String> orderStateMap = new HashMap<String, String>();
         // 订单状态
         Integer orderState = order.getState();
         // 红包状态
         Integer hongBaoState = order.getHongBaoState();
         Integer orderState = order.getState();
         Integer stateWholeOrder = order.getStateWholeOrder();
         if (CommonOrder.STATE_WHOLE_ORDER_BUFENYOUXIAO == stateWholeOrder) {
            // 整个订单部分失效:判断真实状态 订单、红包
            CommonOrderVO buFenOrder = commonOrderMapper.getBuFenOrderState(uid, order.getOrderNo());
            if (buFenOrder != null) {
               // 有效的订单状态
               orderState = buFenOrder.getState();
               // 有效的红包状态
               hongBaoState = buFenOrder.getHongBaoState();
            }
         }
         if (CommonOrder.STATE_FK == orderState) {
            orderStateContent = "已付款";
         } else if (CommonOrder.STATE_JS == orderState) {
@@ -241,8 +343,8 @@
            orderStateContent = "已维权";
            /* 订单维权 判断是否全部维权 */
            List<TaoBaoWeiQuanOrder> listWQ =
                  taoBaoWeiQuanOrderMapper.selectListByOrderIdAndState(order.getOrderNo(), "维权成功");
            List<TaoBaoWeiQuanOrder> listWQ = taoBaoWeiQuanOrderMapper
                  .selectListByOrderIdAndState(order.getOrderNo(), "维权成功");
            boolean isPart = false;// 默认失效
@@ -271,6 +373,8 @@
         order.setOrderState(orderStateMap);
         String hongbaoInfo = "";
         // 订单标识
         List<String> signList = new ArrayList<String>();
         /* 订单返利类型 转换 */
         Integer hongBaoType = order.getHongBaoType();
         if (HongBaoV2.TYPE_ZIGOU == hongBaoType || 2 == hongBaoType) {
@@ -278,11 +382,14 @@
            hongbaoInfo = "返利";
            order.setOrderOrigin("1");
            order.setHongBaoTypePic(CommonOrder.TYPE_FANLI);
            signList.add(CommonOrder.TYPE_FANLI);
         } else if (HongBaoV2.TYPE_SHARE_GOODS == hongBaoType) {
            // 分享
            hongbaoInfo = "奖金";
            order.setOrderOrigin("2");
            order.setHongBaoTypePic(CommonOrder.TYPE_SHARE);
            signList.add(CommonOrder.TYPE_SHARE);
         } else if (HongBaoV2.TYPE_YAOQING == hongBaoType || HongBaoV2.TYPE_YIJI == hongBaoType
               || HongBaoV2.TYPE_ERJI == hongBaoType || HongBaoV2.TYPE_SHARE_YIJI == hongBaoType
               || HongBaoV2.TYPE_SHARE_ERJI == hongBaoType) {
@@ -290,6 +397,7 @@
            hongbaoInfo = "奖金";
            order.setOrderOrigin("3");
            order.setHongBaoTypePic(CommonOrder.TYPE_INVITE);
            signList.add(CommonOrder.TYPE_INVITE);
         }
         String hongBaoDate = null;
@@ -351,29 +459,158 @@
         hongBaoMap.put("content", hongbaoInfo + " ¥" + hongBao.setScale(2, BigDecimal.ROUND_DOWN).toString());
         hongBaoMap.put("fontColor", hongbaoInfoFontColor);
         order.setHongBaoInfo(hongBaoMap);
         // 非自购的订单 不显示返利、免单详情
         if (HongBaoV2.TYPE_ZIGOU != hongBaoType) {
            // 订单标识
            order.setSignList(signList);
         } else {
            // 是否免单商品
            boolean freeOrder = false;
            // 是否奖励成功
            boolean rewardSuccess = false;
            // 奖励订单、免单 使用记录
            if (couponRecordList != null && couponRecordList.size() > 0) {
               for (UserSystemCouponRecord couponRecord : couponRecordList) {
                  // 订单号匹配
                  if (order.getOrderNo().equals(couponRecord.getOrderNo())) {
                     String systemCouponType = couponRecord.getCouponType();
                     Integer state = couponRecord.getState();
                     // 奖励订单: 且成功
                     if (CouponTypeEnum.rebatePercentCoupon.name().equals(systemCouponType)
                           && UserSystemCouponRecord.STATE_SUCCESS == state) {
                        rewardSuccess = true;
                        signList.add(PIC_REWARD); // 加入奖励成功图片
                     } else if (CouponTypeEnum.freeCoupon.name().equals(systemCouponType)
                           || CouponTypeEnum.welfareFreeCoupon.name().equals(systemCouponType)) {
                        if (UserSystemCouponRecord.STATE_FREE_ON == state) {
                           // 免单中
                           freeOrder = true;
                           signList.add(PIC_FREE_ON);
                        } else if (UserSystemCouponRecord.STATE_SUCCESS == state) {
                           // 免单成功
                           freeOrder = true;
                           signList.add(PIC_FREE_SUCCEED);
                        } else if (UserSystemCouponRecord.STATE_FAIL_RULE == state
                              || UserSystemCouponRecord.STATE_FAIL_DRAWBACK == state) {
                           // 规则不匹配、退款
                           freeOrder = true;
                           signList.add(PIC_FREE_FAIL);
                        }
                     }
                     break;
                  }
               }
            }
            // 订单标识
            order.setSignList(signList);
            boolean rewardOrder = false;
            int type = 1; // 1 常规跳转页面 2弹出选项(奖励券)
            if (rewardSuccess) {
               // 奖励成功
               rewardOrder = true;
            } else if (hasRewardCoupon && !freeOrder && HongBaoV2.TYPE_ZIGOU == hongBaoType
                  && HongBaoV2.STATE_YILINGQU == hongBaoState) {
               // 有可用的奖励券 、非免单订单 、且已到账的 、返利订单
               type = 2;
               rewardOrder = true;
            }
            if (rewardOrder) {
               ClientTextStyleVO rewardStyleVO = new ClientTextStyleVO();
               // 奖励券进度详情
               rewardStyleVO.setContent("返利奖励 >");
               rewardStyleVO.setColor("#E5005C");
               rewardStyleVO.setBottomColor("#FFDCEA");
               Map<String, Object> jumpLink = new HashMap<String, Object>();
               jumpLink.put("orderNo", order.getOrderNo());
               Map<String, Object> jump = new HashMap<String, Object>();
               jump.put("type", type);
               jump.put("params", jumpLink);
               jump.put("jumpDetail", jumpDetailV2Service.getByTypeCache("rewardCouponDetail"));
               Map<String, Object> rewardMap = new HashMap<String, Object>();
               rewardMap.put("text", rewardStyleVO);
               rewardMap.put("jump", jump);
               order.setRewardDetail(rewardMap);
            }
            if (freeOrder) {
               ClientTextStyleVO rewardStyleVO = new ClientTextStyleVO();
               // 返利、免单详情
               rewardStyleVO.setContent("免单详情 >");
               rewardStyleVO.setColor("#E5005C");
               rewardStyleVO.setBottomColor("#FFDCEA");
               Map<String, Object> jumpLink = new HashMap<String, Object>();
               jumpLink.put("orderNo", order.getOrderNo());
               Map<String, Object> jump = new HashMap<String, Object>();
               jump.put("type", 1); // 页面跳转
               jump.put("params", jumpLink);
               jump.put("jumpDetail", jumpDetailV2Service.getByTypeCache("freeCouponDetail"));
               Map<String, Object> rewardMap = new HashMap<String, Object>();
               rewardMap.put("text", rewardStyleVO);
               rewardMap.put("jump", jump);
               order.setRewardDetail(rewardMap);
            }
         }
      }
   }
   @Override
   public List<CommonOrder> listBySourceTypeAndOrderId(int sourceType, String orderId) {
      return null;
      return commonOrderMapper.listBySourceTypeAndOrderNo(sourceType, orderId);
   }
   @Transactional
   @Override
   public CommonOrder addTaoBaoOrder(TaoBaoOrder taoBaoOrder, Long uid) throws CommonOrderException {
      CommonOrder commonOrder = commonOrderMapper.selectByOrderNoAndOrderTypeAndOrderBy(taoBaoOrder.getOrderId(),
            Constant.SOURCE_TYPE_TAOBAO, taoBaoOrder.getOrderBy());
      if (commonOrder == null)// 新增
      {
         commonOrder = TaoBaoOrderUtil.convert(taoBaoOrder);
         CommonOrderGoods goods = new CommonOrderGoods();
         goods.setGoodsId(taoBaoOrder.getAuctionId() + "");
         goods.setGoodsType(Constant.SOURCE_TYPE_TAOBAO);
         // 商品是否已经插入
         List<CommonOrderGoods> commonGoodsList = commonOrderGoodsMapper
               .listByGoodsIdAndGoodsType(goods.getGoodsId(), goods.getGoodsType());
   public List<CommonOrder> addTaoBaoOrder(List<TaoBaoOrder> taoBaoOrders, Long uid) throws CommonOrderException {
      List<CommonOrder> commonOrderList = new ArrayList<>();
      // 判断所有的订单状态
      int invalidCount = 0;
      for (TaoBaoOrder tb : taoBaoOrders) {
         if ("订单失效".equalsIgnoreCase(tb.getOrderState())) {
            invalidCount++;
         }
      }
      // 获取整体订单的状态
      int wholeOrderState = 0;
      if (taoBaoOrders.size() == invalidCount)
         wholeOrderState = CommonOrder.STATE_WHOLE_ORDER_SHIXIAO;
      else if (invalidCount == 0)
         wholeOrderState = CommonOrder.STATE_WHOLE_ORDER_YOUXIAO;
      else
         wholeOrderState = CommonOrder.STATE_WHOLE_ORDER_BUFENYOUXIAO;
      for (TaoBaoOrder taoBaoOrder : taoBaoOrders) {
         CommonOrder newCommonOrder = TaoBaoOrderUtil.convert(taoBaoOrder);
         CommonOrderGoods cog = new CommonOrderGoods();
         cog.setGoodsId(taoBaoOrder.getAuctionId() + "");
         cog.setGoodsType(Constant.SOURCE_TYPE_TAOBAO);
         newCommonOrder.setCommonOrderGoods(cog);
         newCommonOrder.setStateWholeOrder(wholeOrderState);
         // 订单商品插入
         List<CommonOrderGoods> commonGoodsList = commonOrderGoodsMapper.listByGoodsIdAndGoodsType(cog.getGoodsId(),
               cog.getGoodsType());
         if (commonGoodsList.size() <= 0)// 不存在就插入商品
         {
            TaoBaoGoodsBrief taoBaoGoods = null;
@@ -381,43 +618,603 @@
               taoBaoGoods = TaoKeApiUtil.getSimpleGoodsInfo(taoBaoOrder.getAuctionId());
            } catch (TaobaoGoodsDownException e) {
               e.printStackTrace();
               LogHelper.errorDetailInfo(e, "AUCTIONID:" + taoBaoOrder.getAuctionId(), "");
               try {
                  taoBaoGoods = TaoBaoUtil.getSimpleGoodsBrief(taoBaoOrder.getAuctionId());
               } catch (Exception e1) {
                  CommonGoods commonGoods = commonGoodsService.getCommonGoodsByGoodsIdAndGoodsType(
                        taoBaoOrder.getAuctionId(), Constant.SOURCE_TYPE_TAOBAO);
                  if (commonGoods != null)
                     taoBaoGoods = TaoBaoUtil.convert(commonGoods);
               }
            }
            if (taoBaoGoods != null) {
               goods = CommonOrderGoodsFactory.create(taoBaoGoods);
               cog = CommonOrderGoodsFactory.create(taoBaoGoods);
            }
            goods.setCreateTime(new Date());
            goods.setUpdateTime(new Date());
            cog.setCreateTime(new Date());
            cog.setUpdateTime(new Date());
            commonOrderGoodsMapper.insertSelective(cog);
         }
         newCommonOrder.setUserInfo(new UserInfo(uid));
         commonOrderList.add(addCommonOrder(newCommonOrder));
      }
      return commonOrderList;
   }
   @Transactional
   @Override
   public List<CommonOrder> addPDDOrder(List<PDDOrder> pddOrderList, Long uid) throws CommonOrderException {
      List<CommonOrder> commonOrderList = new ArrayList<>();
      // 判断所有的订单状态
      int invalidCount = 0;
      for (PDDOrder order : pddOrderList) {
         if (order.getOrderStatus() == -1 || order.getOrderStatus() == 4 || order.getOrderStatus() == 8) {
            invalidCount++;
         }
      }
      // 获取整体订单的状态
      int wholeOrderState = 0;
      if (pddOrderList.size() == invalidCount)
         wholeOrderState = CommonOrder.STATE_WHOLE_ORDER_SHIXIAO;
      else if (invalidCount == 0)
         wholeOrderState = CommonOrder.STATE_WHOLE_ORDER_YOUXIAO;
      else
         wholeOrderState = CommonOrder.STATE_WHOLE_ORDER_BUFENYOUXIAO;
      for (PDDOrder pddOrder : pddOrderList) {
         CommonOrder newCommonOrder = CommonOrderFactory.create(pddOrder);
         CommonOrderGoods cog = new CommonOrderGoods();
         cog.setGoodsId(pddOrder.getGoodsId() + "");
         cog.setGoodsType(Constant.SOURCE_TYPE_PDD);
         newCommonOrder.setCommonOrderGoods(cog);
         newCommonOrder.setStateWholeOrder(wholeOrderState);
         // 订单商品插入
         List<CommonOrderGoods> commonGoodsList = commonOrderGoodsMapper.listByGoodsIdAndGoodsType(cog.getGoodsId(),
               cog.getGoodsType());
         if (commonGoodsList.size() <= 0)// 不存在就插入商品
         {
            PDDGoodsDetail pddGoods = null;
            pddGoods = PinDuoDuoApiUtil.getGoodsDetail(Long.parseLong(cog.getGoodsId()));
            if (pddGoods != null) {
               cog = CommonOrderGoodsFactory.create(pddGoods);
            }
            cog.setCreateTime(new Date());
            cog.setUpdateTime(new Date());
            commonOrderGoodsMapper.insertSelective(cog);
            newCommonOrder.setCommonOrderGoods(cog);
         }
         newCommonOrder.setUserInfo(new UserInfo(uid));
         commonOrderList.add(addCommonOrder(newCommonOrder));
      }
      return commonOrderList;
   }
   @Transactional
   @Override
   public List<CommonOrder> addJDOrder(JDOrder jdOrder, Long uid) throws CommonOrderException {
      List<CommonOrder> commonOrderList = new ArrayList<>();
      // 判断所有的订单状态
      int invalidCount = 0;
      for (JDOrderItem tb : jdOrder.getOrderItemList()) {
         if (tb.getValidCode() < 16) {
            invalidCount++;
         }
      }
      // 获取整体订单的状态
      int wholeOrderState = 0;
      if (jdOrder.getOrderItemList().size() == invalidCount)
         wholeOrderState = CommonOrder.STATE_WHOLE_ORDER_SHIXIAO;
      else if (invalidCount == 0)
         wholeOrderState = CommonOrder.STATE_WHOLE_ORDER_YOUXIAO;
      else
         wholeOrderState = CommonOrder.STATE_WHOLE_ORDER_BUFENYOUXIAO;
      List<JDOrderItem> orderItemList = jdOrder.getOrderItemList();
      jdOrder.setOrderItemList(null);
      for (JDOrderItem itemOrder : orderItemList) {
         itemOrder.setOrder(jdOrder);
         CommonOrder newCommonOrder = CommonOrderFactory.create(itemOrder);
         CommonOrderGoods cog = new CommonOrderGoods();
         cog.setGoodsId(itemOrder.getSkuId() + "");
         cog.setGoodsType(Constant.SOURCE_TYPE_JD);
         newCommonOrder.setCommonOrderGoods(cog);
         newCommonOrder.setStateWholeOrder(wholeOrderState);
         // 订单商品插入
         List<CommonOrderGoods> commonGoodsList = commonOrderGoodsMapper.listByGoodsIdAndGoodsType(cog.getGoodsId(),
               cog.getGoodsType());
         if (commonGoodsList.size() <= 0)// 不存在就插入商品
         {
            JDGoods goods = JDApiUtil.getGoodsDetail(itemOrder.getSkuId());
            if (goods != null) {
               cog = CommonOrderGoodsFactory.create(goods);
            }
            cog.setCreateTime(new Date());
            cog.setUpdateTime(new Date());
            commonOrderGoodsMapper.insertSelective(cog);
         }
         newCommonOrder.setUserInfo(new UserInfo(uid));
         commonOrderList.add(addCommonOrder(newCommonOrder));
      }
      return commonOrderList;
   }
   /**
    * 添加订单
    *
    * @param commonOrder
    */
   @Transactional
   private CommonOrder addCommonOrder(CommonOrder commonOrder) throws CommonOrderException {
      // 判断商品是否存在
      if (commonOrder == null)
         throw new CommonOrderException(1, "订单为空");
      if (commonOrder.getUserInfo() == null)
         throw new CommonOrderException(2, "订单用户为空");
      if (commonOrder.getCommonOrderGoods() == null || commonOrder.getCommonOrderGoods().getGoodsId() == null
            || commonOrder.getCommonOrderGoods().getGoodsType() == null)
         throw new CommonOrderException(3, "订单商品为空");
      // 不存在就插入,存在就不管
      CommonOrderGoods goods = commonOrder.getCommonOrderGoods();
      // 之前不存在于数据库
      if (commonOrder.getCommonOrderGoods().getId() == null) {
         List<CommonOrderGoods> commonGoodsList = commonOrderGoodsMapper
               .listByGoodsIdAndGoodsType(goods.getGoodsId(), goods.getGoodsType());
         if (commonGoodsList == null || commonGoodsList.size() < 1) {// 不存在
            commonOrderGoodsMapper.insertSelective(goods);
         } else
            goods.setId(commonGoodsList.get(commonGoodsList.size() - 1).getId());
         } else {// 存在
            goods = commonGoodsList.get(0);
         }
         if (goods.getId() == null)
            throw new CommonOrderException(4, "商品插入失败");
         commonOrder.setCommonOrderGoods(goods);
         commonOrder.setUserInfo(new UserInfo(uid));
      }
      CommonOrder oldCommonOrder = null;
      if (!StringUtil.isNullOrEmpty(commonOrder.getTradeId()))
         oldCommonOrder = commonOrderMapper.selectBySourceTypeAndTradeId(commonOrder.getSourceType(),
               commonOrder.getTradeId());
      else
         oldCommonOrder = commonOrderMapper.selectByOrderNoAndOrderTypeAndOrderBy(commonOrder.getOrderNo(),
               commonOrder.getSourceType(), commonOrder.getOrderBy());
      if (oldCommonOrder == null)// 新增
      {
         commonOrder.setCreateTime(new Date());
         commonOrderMapper.insertSelective(commonOrder);
         // 插入映射,保证交易ID的完整性
         commonOrderTradeIdMapMapper.insertSelective(new CommonOrderTradeIdMap(commonOrder.getId(),
               commonOrder.getTradeId(), new Date(), commonOrder.getSourceType()));
         return commonOrder;
      } else {// 修改
         // 已经结算或者已经失效的订单不处理
         if (commonOrder.getState() == CommonOrder.STATE_JS || commonOrder.getState() == CommonOrder.STATE_SX)
            return commonOrder;
         CommonOrder newCommonOrder = TaoBaoOrderUtil.convert(taoBaoOrder);
         CommonOrder updateCommonOrder = new CommonOrder(commonOrder.getId());
         updateCommonOrder.seteIncome(newCommonOrder.geteIncome());
         updateCommonOrder.setEstimate(newCommonOrder.getEstimate());
         updateCommonOrder.setPayment(newCommonOrder.getPayment());
         updateCommonOrder.setSettlement(newCommonOrder.getSettlement());
         updateCommonOrder.setSettleTime(newCommonOrder.getSettleTime());
         updateCommonOrder.setState(newCommonOrder.getState());
         updateCommonOrder.setUpdateTime(new Date());
         commonOrderMapper.updateByPrimaryKeySelective(updateCommonOrder);
         return commonOrderMapper.selectByPrimaryKey(updateCommonOrder.getId());
         // 已经结算,已经失效,状态未改变的订单不处理
         if (oldCommonOrder.getState() == CommonOrder.STATE_JS || oldCommonOrder.getState() == CommonOrder.STATE_SX
               || (oldCommonOrder.getState().intValue() == commonOrder.getState())) {
            return oldCommonOrder;
         }
         // 交易ID一致才修改
         if (commonOrder.getTradeId() != null
               && commonOrder.getTradeId().equalsIgnoreCase(oldCommonOrder.getTradeId())) {
            CommonOrder updateCommonOrder = new CommonOrder(oldCommonOrder.getId());
            updateCommonOrder.seteIncome(commonOrder.geteIncome());
            updateCommonOrder.setEstimate(commonOrder.getEstimate());
            updateCommonOrder.setPayment(commonOrder.getPayment());
            updateCommonOrder.setSettlement(commonOrder.getSettlement());
            updateCommonOrder.setSettleTime(commonOrder.getSettleTime());
            updateCommonOrder.setState(commonOrder.getState());
            updateCommonOrder.setStateWholeOrder(commonOrder.getStateWholeOrder());
            updateCommonOrder.setUpdateTime(new Date());
            updateCommonOrder.setTradeId(commonOrder.getTradeId());
            commonOrderMapper.updateByPrimaryKeySelective(updateCommonOrder);
            return commonOrderMapper.selectByPrimaryKey(updateCommonOrder.getId());
         } else
            return oldCommonOrder;
      }
   }
   @Override
   public Map<String, Object> countMoneyGroupOrderNoByUid(Long uid, Integer type, String orderNo, String startTime,
         String endTime, Integer day) {
      return commonOrderMapper.countMoneyGroupOrderNoByUid(uid, type, orderNo, startTime, endTime, day);
   public long countByUidAndOrderStateWithOrderBalanceTime(Long uid, int state, Date minDate, Date maxDate) {
      return commonOrderMapper.countByUidAndOrderStateWithOrderBalanceTime(uid, state, minDate, maxDate);
   }
   @Override
   public List<CommonOrderVO> listQueryByUid(long start, int count, Long uid, Integer state, Integer type,
         Integer orderState, String orderNo, String startTime, String endTime, Integer dateType)
         throws CommonOrderException {
      List<CommonOrderVO> list = commonOrderMapper.listQueryByUid(start, count, uid, state, type, orderState, orderNo,
            startTime, endTime, dateType);
      if (list == null) {
         list = new ArrayList<CommonOrderVO>();
      }
      if (list.size() == 0) {
         return list;
      }
      SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
      for (CommonOrderVO order : list) {
         // 下单时间
         Date thirdCreateTime = order.getThirdCreateTime();
         if (thirdCreateTime != null) {
            order.setDownTime(format.format(thirdCreateTime));
         }
         // 收货时间
         Date settleTime = order.getSettleTime();
         if (settleTime != null) {
            order.setReceiveTime(format.format(settleTime));
         }
         // 到账时间
         Date accountTime = order.getAccountTime();
         if (accountTime != null) {
            order.setHongBaoDate(format.format(accountTime));
         }
         BigDecimal settlement = order.getSettlement();
         if (settlement != null && settlement.compareTo(new BigDecimal(0)) > 0) {
            order.setPayment(settlement); // 实际付款金额
         }
         /* 订单返利类型 转换 */
         Integer hongBaoType = order.getHongBaoType();
         if (HongBaoV2.TYPE_ZIGOU == hongBaoType || 2 == hongBaoType) {
            // 自购
            order.setHongBaoType(1);
         } else if (HongBaoV2.TYPE_SHARE_GOODS == hongBaoType) {
            // 分享
            order.setHongBaoType(2);
         } else if (HongBaoV2.TYPE_YAOQING == hongBaoType || HongBaoV2.TYPE_YIJI == hongBaoType
               || HongBaoV2.TYPE_ERJI == hongBaoType || HongBaoV2.TYPE_SHARE_YIJI == hongBaoType
               || HongBaoV2.TYPE_SHARE_ERJI == hongBaoType) {
            // 邀请
            order.setHongBaoType(3);
         }
         Integer hongBaoState = order.getHongBaoState();
         if (HongBaoV2.STATE_KELINGQU == hongBaoState || HongBaoV2.STATE_BUKELINGQU == hongBaoState) {
            // 未到账
            order.setHongBaoState(1);
         } else if (HongBaoV2.STATE_YILINGQU == hongBaoState) {
            // 已到账
            order.setHongBaoState(2);
         } else if (HongBaoV2.STATE_SHIXIAO == hongBaoState) {
            // 已失效
            order.setHongBaoState(3);
         }
      }
      return list;
   }
   @Override
   public long countQueryByUid(Long uid, Integer state, Integer type, Integer orderState, String orderNo,
         String startTime, String endTime, Integer dateType) throws CommonOrderException {
      return commonOrderMapper.countQueryByUid(uid, state, type, orderState, orderNo, startTime, endTime, dateType);
   }
   @Override
   public List<CommonOrderVO> listQuery(long start, int count, Integer keyType, String key, Integer state,
         Integer type, Integer orderState, String startTime, String endTime) throws CommonOrderException {
      List<CommonOrderVO> list = commonOrderMapper.listQuery(start, count, keyType, key, state, type, orderState,
            startTime, endTime);
      if (list == null) {
         list = new ArrayList<CommonOrderVO>();
      }
      if (list.size() == 0) {
         return list;
      }
      SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
      for (CommonOrderVO order : list) {
         // 下单时间
         Date thirdCreateTime = order.getThirdCreateTime();
         if (thirdCreateTime != null) {
            order.setDownTime(format.format(thirdCreateTime));
         }
         // 收货时间
         Date settleTime = order.getSettleTime();
         if (settleTime != null) {
            order.setReceiveTime(format.format(settleTime));
         }
         // 到账时间
         Date accountTime = order.getAccountTime();
         if (accountTime != null) {
            order.setHongBaoDate(format.format(accountTime));
         }
         BigDecimal settlement = order.getSettlement();
         if (settlement != null && settlement.compareTo(new BigDecimal(0)) > 0) {
            order.setPayment(settlement); // 实际付款金额
         }
         /* 订单返利类型 转换 */
         Integer hongBaoType = order.getHongBaoType();
         if (HongBaoV2.TYPE_ZIGOU == hongBaoType || 2 == hongBaoType) {
            // 自购
            order.setHongBaoType(1);
         } else if (HongBaoV2.TYPE_SHARE_GOODS == hongBaoType) {
            // 分享
            order.setHongBaoType(2);
         } else if (HongBaoV2.TYPE_YAOQING == hongBaoType || HongBaoV2.TYPE_YIJI == hongBaoType
               || HongBaoV2.TYPE_ERJI == hongBaoType || HongBaoV2.TYPE_SHARE_YIJI == hongBaoType
               || HongBaoV2.TYPE_SHARE_ERJI == hongBaoType) {
            // 邀请
            order.setHongBaoType(3);
         }
         Integer hongBaoState = order.getHongBaoState();
         if (HongBaoV2.STATE_KELINGQU == hongBaoState || HongBaoV2.STATE_BUKELINGQU == hongBaoState) {
            // 未到账
            order.setHongBaoState(1);
         } else if (HongBaoV2.STATE_YILINGQU == hongBaoState) {
            // 已到账
            order.setHongBaoState(2);
         } else if (HongBaoV2.STATE_SHIXIAO == hongBaoState) {
            // 已失效
            order.setHongBaoState(3);
         }
         List<HongBaoV2> listhb = hongBaoV2Service.listChildrenById(order.getHongbaoId());
         if (listhb != null && listhb.size() > 0) {
            if (listhb.size() == 1) {
               UserInfo userInfo = listhb.get(0).getUserInfo();
               if (userInfo != null) {
                  order.setLevelOneId(userInfo.getId() + "");
                  order.setLevelOneMoney(listhb.get(0).getMoney() + "");
               }
            } else if (listhb.size() == 2) {
               UserInfo userInfo = listhb.get(0).getUserInfo();
               if (userInfo != null) {
                  order.setLevelOneId(userInfo.getId() + "");
                  order.setLevelOneMoney(listhb.get(0).getMoney() + "");
               }
               UserInfo userInfo2 = listhb.get(1).getUserInfo();
               if (userInfo2 != null) {
                  order.setLevelTwoId(userInfo2.getId() + "");
                  order.setLevelTwoMoney(listhb.get(1).getMoney() + "");
               }
            }
         }
      }
      return list;
   }
   @Override
   public long countQuery(Integer keyType, String key, Integer state, Integer type, Integer orderState,
         String startTime, String endTime) throws CommonOrderException {
      return commonOrderMapper.countQuery(keyType, key, state, type, orderState, startTime, endTime);
   }
   @Override
   public CommonOrderVO getInfoByOrderNo(Long uid, String orderNo) throws CommonOrderException {
      List<CommonOrderVO> list = getOrderByUid(1, uid, null, null, null, orderNo, null, null, null);
      if (list == null || list.size() == 0) {
         return null;
      } else {
         return list.get(0);
      }
   }
   @Override
   public CommonOrderVO getCommonOrderByOrderNo(Long uid, String orderNo, Integer orderState)
         throws CommonOrderException {
      CommonOrderVO commonOrderVO = commonOrderMapper.getCommonOrderByOrderNo(uid, orderNo, orderState);
      // 订单信息为空
      if (commonOrderVO == null) {
         return null;
      }
      List<CommonOrderVO> listOrder = new ArrayList<CommonOrderVO>();
      listOrder.add(commonOrderVO);
      // 商品信息
      List<CommonOrderVO> listGoods = commonOrderMapper.listOrderGoodsInfo(listOrder);
      // 订单商品为空
      if (listGoods == null || listGoods.size() == 0) {
         return null;
      }
      /* 组合商品信息 */
      for (CommonOrderVO commonOrder : listGoods) {
         CommonOrderGoods goods = commonOrder.getCommonOrderGoods();
         if (goods == null) {
            continue;
         }
         String orderNo1 = commonOrder.getOrderNo();
         Integer sourceType = commonOrder.getSourceType();
         String orderNo2 = commonOrderVO.getOrderNo();
         Integer sourceType2 = commonOrderVO.getSourceType();
         // 来源、订单号相同
         if (sourceType.equals(sourceType2) && orderNo1.equals(orderNo2)) {
            // 加入商品信息
            List<CommonOrderGoodsVO> listOrderGoods = commonOrderVO.getListOrderGoods();
            CommonOrderGoodsVO commonGoodsVO = new CommonOrderGoodsVO();
            try {
               PropertyUtils.copyProperties(commonGoodsVO, goods);
            } catch (Exception e) {
               e.printStackTrace();
            }
            String picture = commonGoodsVO.getPicture();
            if (!StringUtil.isNullOrEmpty(picture) && !picture.contains("320x320")) {
               commonGoodsVO.setPicture(TbImgUtil.getTBSize320Img(picture));
            }
            Integer hongBaoType = commonOrderVO.getHongBaoType();
            // 邀请订单信息保护
            if (HongBaoV2.TYPE_YAOQING == hongBaoType || HongBaoV2.TYPE_YIJI == hongBaoType
                  || HongBaoV2.TYPE_ERJI == hongBaoType || HongBaoV2.TYPE_SHARE_YIJI == hongBaoType
                  || HongBaoV2.TYPE_SHARE_ERJI == hongBaoType) {
               Map<String, String> titleMap = new HashMap<String, String>();
               titleMap.put("content", "为保障用户隐私,商品信息已隐藏!");
               titleMap.put("fontColor", "#888888");
               titleMap.put("bottomColor", "#E9E9E9");
               commonGoodsVO.setTitle(null);
               commonGoodsVO.setGoodsTitle(titleMap);
            }
            // 购买数量
            commonGoodsVO.setActualCount(commonOrder.getTotalCount() + "件");
            BigDecimal totalSettlement = commonOrder.getTotalSettlement();
            if (totalSettlement == null || totalSettlement.compareTo(new BigDecimal(0)) <= 0) {
               totalSettlement = commonOrder.getTotalPayment();
            }
            // 实付款
            commonGoodsVO.setActualPay("付款金额:¥" + totalSettlement);
            listOrderGoods.add(commonGoodsVO);
            Integer orderType = commonOrder.getOrderType();
            if (orderType == null) {
               String shopType = commonGoodsVO.getShopType();
               if (CommonOrderGoodsVO.TYPE_TAOBAO.equalsIgnoreCase(shopType)) {
                  commonOrder.setOrderType(1);
               } else if (CommonOrderGoodsVO.TYPE_TMALL.equalsIgnoreCase(shopType)) {
                  commonOrder.setOrderType(2);
               }
            }
            break;
         }
      }
      return commonOrderVO;
   }
   @Override
   public List<CommonOrderVO> getCouponHongbaoByOrderNo(Long uid, Integer hongBaoState, String orderNo)
         throws CommonOrderException {
      List<CommonOrderVO> listOrder = commonOrderMapper.getCouponHongbaoByOrderNo(uid, hongBaoState, orderNo);
      // 订单信息为空
      if (listOrder == null || listOrder.size() == 0) {
         listOrder = new ArrayList<CommonOrderVO>();
         return listOrder;
      }
      // 商品信息
      List<CommonOrderVO> listGoods = commonOrderMapper.listOrderGoodsInfo(listOrder);
      // 订单商品为空
      if (listGoods == null || listGoods.size() == 0) {
         return listOrder;
      }
      // 数据加工重新组织
      listDataFactory(listOrder, listGoods, uid);
      return listOrder;
   }
   @Override
   public JSONObject getRewardJumpInfo(String orderNo) {
      JSONObject map = new JSONObject();
      // 订单标识
      List<String> signList = new ArrayList<String>();
      signList.add(CommonOrder.TYPE_FANLI);
      signList.add(PIC_REWARD); // 加入奖励成功图片
      map.put("signList", signList);
      ClientTextStyleVO rewardStyleVO = new ClientTextStyleVO();
      // 奖励券进度详情
      rewardStyleVO.setContent("返利奖励 >");
      rewardStyleVO.setColor("#E5005C");
      rewardStyleVO.setBottomColor("#FFDCEA");
      Map<String, Object> jumpLink = new HashMap<String, Object>();
      jumpLink.put("orderNo", orderNo);
      Map<String, Object> jump = new HashMap<String, Object>();
      jump.put("type", 1);
      jump.put("params", jumpLink);
      jump.put("jumpDetail", jumpDetailV2Service.getByTypeCache("rewardCouponDetail"));
      Map<String, Object> rewardMap = new HashMap<String, Object>();
      rewardMap.put("text", rewardStyleVO);
      rewardMap.put("jump", jump);
      map.put("rewardDetail", rewardMap);
      return map;
   }
   @Override
   public CommonOrder selectBySourceTypeAndTradeId(int sourceType, String tradeId) {
      return commonOrderMapper.selectBySourceTypeAndTradeId(sourceType, tradeId);
   }
   @Transactional
   @Override
   public void deleteErrorCommonOrder(Long coId) {
      // 查询是否返利到账
      HongBaoOrder hongBaoOrder = hongBaoOrderService.selectDetailByCommonOrderId(coId);
      if (hongBaoOrder.getHongBaoV2().getState() == HongBaoV2.STATE_YILINGQU)
         return;
      List<HongBaoV2> hongBaoList = hongBaoV2Service.listChildrenById(hongBaoOrder.getHongBaoV2().getId());
      boolean canDelete = true;
      for (HongBaoV2 child : hongBaoList)
         if (child.getState() == HongBaoV2.STATE_YILINGQU) {
            canDelete = false;
            break;
         }
      // 不能删除
      if (!canDelete)
         return;
      // 删除child
      for (HongBaoV2 child : hongBaoList)
         hongBaoV2Service.deleteByPrimaryKey(child.getId());
      // 删除主红包
      hongBaoV2Service.deleteByPrimaryKey(hongBaoOrder.getHongBaoV2().getId());
      // 删除hongbaoOrder
      hongBaoOrderService.deleteByPrimaryKey(hongBaoOrder.getId());
      // 删除CommonOrder
      commonOrderMapper.deleteByPrimaryKey(hongBaoOrder.getCommonOrder().getId());
   }
   @Override
   public List<CommonOrder> listBySourceTypeAndTradeId(int sourceType, String tradeId) {
      return commonOrderMapper.listBySourceTypeAndTradeId(sourceType, tradeId);
   }
   @Override
   public List<CommonOrder> listBySourceTypeAndStateAndThirdCrateTime(int sourceType, Integer state, Long minTime,
         Long maxTime, int page, int pageSize) {
      return commonOrderMapper.listBySourceTypeAndStateAndThirdCrateTime(sourceType, state, minTime, maxTime,
            (page - 1) * pageSize, pageSize);
   }
}