admin
2025-02-25 30d8e227e8d823b6c38c3b9c90ac2df03b63befe
fanli/src/main/java/com/yeshi/fanli/controller/client/v1/UserOrderController.java
@@ -1,729 +1,874 @@
package com.yeshi.fanli.controller.client.v1;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.yeshi.utils.DateUtil;
import org.yeshi.utils.IPUtil;
import org.yeshi.utils.JsonUtil;
import org.yeshi.utils.encrypt.DESUtil;
import com.yeshi.fanli.entity.AppVersionInfo;
import com.yeshi.fanli.entity.accept.AcceptData;
import com.yeshi.fanli.entity.bus.user.LostOrder;
import com.yeshi.fanli.entity.bus.user.Order;
import com.yeshi.fanli.entity.bus.user.UserExtraTaoBaoInfo;
import com.yeshi.fanli.entity.bus.user.UserInfo;
import com.yeshi.fanli.entity.order.CommonOrder;
import com.yeshi.fanli.entity.taobao.TaoBaoOrder;
import com.yeshi.fanli.exception.order.CommonOrderException;
import com.yeshi.fanli.log.LogHelper;
import com.yeshi.fanli.service.inter.config.AppVersionService;
import com.yeshi.fanli.service.inter.config.ConfigService;
import com.yeshi.fanli.service.inter.count.HongBaoV2CountService;
import com.yeshi.fanli.service.inter.order.CommonOrderService;
import com.yeshi.fanli.service.inter.order.LostOrderService;
import com.yeshi.fanli.service.inter.order.OrderService;
import com.yeshi.fanli.service.inter.taobao.TaoBaoOrderService;
import com.yeshi.fanli.service.inter.user.UserExtraTaoBaoInfoService;
import com.yeshi.fanli.service.inter.user.UserInfoExtraService;
import com.yeshi.fanli.service.inter.user.UserInfoService;
import com.yeshi.fanli.util.CMQManager;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.account.UserUtil;
import com.yeshi.fanli.vo.order.CommonOrderVO;
import com.yeshi.fanli.vo.user.UserInfoExtraVO;
import net.sf.json.JSONObject;
@Controller
@RequestMapping("api/v1/user/order")
public class UserOrderController {
   @Resource
   private ConfigService configService;
   @Resource
   private AppVersionService appVersionService;
   @Resource
   private UserInfoService userInfoService;
   @Resource
   private UserInfoExtraService userInfoExtraService;
   @Resource
   private CommonOrderService commonOrderService;
   @Resource
   private HongBaoV2CountService hongBaoV2CountService;
   @Resource
   private UserExtraTaoBaoInfoService userExtraTaoBaoInfoService;
   @Resource
   private TaoBaoOrderService taoBaoOrderService;
   @Resource
   private OrderService orderService;
   @Resource
   private LostOrderService lostOrderService;
   /**
    * 订单列表
    *
    * @param acceptData
    * @param page
    * @param uid
    * @param state
    *            状态:0全部 1-未到账 2-已到账 3-已失效 4已收货
    * @param type
    *            类型:1-返利订单 2-分享订单 3-邀请订单
    * @param orderState
    *            1有效订单 2 维权订单 3失效订单
    * @param orderNo
    *            订单号
    * @param startTime
    *            起始时间
    * @param endTime
    *            结束时间
    * @param slotTime
    *            时间段:1-最近三天 2-最近七天 3最近半月 4本月 5近三月 6近半年
    * @param dateType
    *            1-今日 2-昨天 3-本月 4-上个月 |
    * @param needCount
    * @param out
    */
   @RequestMapping(value = "getorder", method = RequestMethod.POST)
   public void getOrder(AcceptData acceptData, Integer page, Long uid, Integer state, Integer type, Integer orderState,
         String orderNo, String startTime, String endTime, Integer slotTime, boolean needCount, Integer dateType,
         Integer goodsType, PrintWriter out) {
      if (uid == null) {
         out.print(JsonUtil.loadFalseResult(1, "用户未登录"));
         return;
      }
      if (page == null || page < 1) {
         page = 1;
      }
      if (state != null && state == 0) {
         state = null;// 所有状态
      }
      // 转换状态
      if (state != null && state == 4) {
         orderState = state; // 已收货状态
         state = null; // 清空
      }
      if (type != null && type == 0) {
         type = null; // 所有类型订单
      }
      if (goodsType != null && goodsType == 0) {
         goodsType = null; // 所有平台订单
      }
      try {
         if (slotTime != null) {
            SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
            endTime = sd.format(new Date());
            startTime = convertDate(slotTime, endTime);
         }
      } catch (Exception e) {
         e.printStackTrace();
      }
      if (endTime != null && endTime.trim().length() > 0) {
         endTime += " 23:59:59";
      }
      try {
         long count = 0;
         int totalValid = 0;
         int totalProces = 0;
         int totalInvite = 0;
         long todayTotal = 0;
         BigDecimal todayMoney = null;
         // 查询列表
         List<CommonOrderVO> list = commonOrderService.getOrderByUid(acceptData, page, uid, state, type, orderState, orderNo,
               startTime, endTime, dateType, goodsType);
         if (list != null && list.size() > 0) {
            // 统计总数
            count = commonOrderService.countGroupOrderNoByUid(uid, state, type, orderState, orderNo, startTime,
                  endTime, dateType, goodsType);
         }
         // 需要统计筛选信息 :未失效的总金额 以及订单
         if (needCount && page == 1) {
            todayMoney = commonOrderService.countBonusOrderMoney(uid, type, dateType, startTime, endTime, goodsType);
            todayTotal = commonOrderService.countBonusOrderNumber(uid, type, dateType, startTime, endTime, goodsType);
            // 统计有效的订单数量 、 失效订单数量 、维权订单数量
            Map<String, BigDecimal> countOrder = commonOrderService.countByUidAndOrderState(uid, type, startTime,
                  endTime, dateType, goodsType);
            if (countOrder.get("totalValid") != null) {
               totalValid = countOrder.get("totalValid").intValue();
            }
            if (countOrder.get("totalProces") != null) {
               totalProces = countOrder.get("totalProces").intValue();
            }
            if (countOrder.get("totalInvite") != null) {
               totalInvite = countOrder.get("totalInvite").intValue();
            }
         }
         if (todayMoney == null) {
            todayMoney = new BigDecimal(0.00);
         }
         String helpUrl = configService.get("order_list_help");
         JSONObject data = new JSONObject();
         data.put("count", count);
         data.put("result_list", JsonUtil.getApiCommonGson().toJson(list));
         data.put("helpUrl", helpUrl);
         if (type != null && type == 1) {
            data.put("todayMoney", "预估返利总额 ¥" + todayMoney.setScale(2, BigDecimal.ROUND_DOWN).toString());
         } else if (type != null && (type == 2 || type == 3)) {
            data.put("todayMoney", "预估奖金总额 ¥" + todayMoney.setScale(2, BigDecimal.ROUND_DOWN).toString());
         } else {
            data.put("todayMoney", "预估总额 ¥" + todayMoney.setScale(2, BigDecimal.ROUND_DOWN).toString());
         }
         data.put("todayTotal", "共" + todayTotal + "笔");
         data.put("totalValid", totalValid); // 有效数量
         data.put("totalProces", totalProces); // 维权数量
         data.put("totalInvite", totalInvite); // 失效数量
         out.print(JsonUtil.loadTrueResult(data));
      } catch (CommonOrderException e) {
         out.print(JsonUtil.loadFalseResult(e.getCode(), e.getMsg()));
      } catch (Exception e) {
         out.print(JsonUtil.loadFalseResult(1, "查询失败"));
         e.printStackTrace();
      }
   }
   /**
    * 用户订单统计
    *
    * @param acceptData
    * @param uid
    *            用户id
    * @param out
    */
   @RequestMapping(value = "countorder", method = RequestMethod.POST)
   public void countOrder(AcceptData acceptData, Long uid, PrintWriter out) {
      if (uid == null) {
         out.print(JsonUtil.loadFalseResult(1, "用户未登录"));
         return;
      }
      try {
         UserInfo user = userInfoService.selectByPKey(uid);
         if (user == null) {
            out.print(JsonUtil.loadFalseResult(1, "用户不存在"));
            return;
         }
         UserInfo userInfo = UserUtil.filterForClientUser(user);
         // 1.5.0 版本之后返回新的等级
         String version = acceptData.getVersion();
         if (version != null && version.trim().length() > 0) {
            int versionCode = Integer.parseInt(version);
            String platform = acceptData.getPlatform();
            AppVersionInfo versionInfo = appVersionService.getByPlatformAndVersion(platform, "1.5.0");
            if (versionInfo != null) {
               int versionCode150 = versionInfo.getVersionCode();
               if (versionCode >= versionCode150) {
                  UserInfoExtraVO userInfoExtra = userInfoExtraService.getInfoExtraVOByUid(user.getId());
                  if (userInfoExtra != null && userInfoExtra.getUserRank() != null) {
                     String picture = userInfoExtra.getUserRank().getPicture();
                     String icon = userInfoExtra.getUserRank().getIcon();
                     userInfo.setRankIcon(icon);
                     userInfo.setRankNamePicture(picture);
                  }
               }
            }
         }
         JSONObject data = new JSONObject();
         data.put("userInfo", userInfo);
         /* 总订单统计 */
         Map<String, BigDecimal> countOrder = commonOrderService.countHistoryOrder(uid, null, null);
         int self = 0;
         if (countOrder.get("totalSelf") != null) {
            self = countOrder.get("totalSelf").intValue();
         }
         int shared = 0;
         if (countOrder.get("totalShared") != null) {
            shared = countOrder.get("totalShared").intValue();
         }
         int invite = 0;
         if (countOrder.get("totalInvite") != null) {
            invite = countOrder.get("totalInvite").intValue();
         }
         int total = self + shared + invite;
         data.put("total", total);
         data.put("self", self);
         data.put("shared", shared);
         data.put("invite", invite);
         /* 今日订单统计 */
         Map<String, BigDecimal> countToday = commonOrderService.countHistoryOrder(uid, 1, null);
         int todaySelf = 0;
         if (countToday.get("totalSelf") != null) {
            todaySelf = countToday.get("totalSelf").intValue();
         }
         int todayShared = 0;
         if (countToday.get("totalShared") != null) {
            todayShared = countToday.get("totalShared").intValue();
         }
         int todayInvite = 0;
         if (countToday.get("totalInvite") != null) {
            todayInvite = countToday.get("totalInvite").intValue();
         }
         int todayTotal = todaySelf + todayShared + todayInvite;
         JSONObject todaydata = new JSONObject();
         todaydata.put("total", todayTotal);
         todaydata.put("self", todaySelf);
         todaydata.put("shared", todayShared);
         todaydata.put("invite", todayInvite);
         data.put("today", todaydata);
         /* 昨日订单统计 */
         Map<String, BigDecimal> countYesterday = commonOrderService.countHistoryOrder(uid, 2, null);
         int yesterdaySelf = 0;
         if (countYesterday.get("totalSelf") != null) {
            yesterdaySelf = countYesterday.get("totalSelf").intValue();
         }
         int yesterdayShared = 0;
         if (countYesterday.get("totalShared") != null) {
            yesterdayShared = countYesterday.get("totalShared").intValue();
         }
         int yesterdayInvite = 0;
         if (countYesterday.get("totalInvite") != null) {
            yesterdayInvite = countYesterday.get("totalInvite").intValue();
         }
         int yesterdayTotal = yesterdaySelf + yesterdayShared + yesterdayInvite;
         JSONObject yesterdaydata = new JSONObject();
         yesterdaydata.put("total", yesterdayTotal);
         yesterdaydata.put("self", yesterdaySelf);
         yesterdaydata.put("shared", yesterdayShared);
         yesterdaydata.put("invite", yesterdayInvite);
         data.put("yesterday", yesterdaydata);
         out.print(JsonUtil.loadTrueResult(data));
      } catch (Exception e) {
         out.print(JsonUtil.loadFalseResult(1, "获取信息失败"));
         e.printStackTrace();
      }
   }
   @RequestMapping(value = "getTotalOrderNum")
   public void getTotalOrderNum(AcceptData acceptData, Long uid, PrintWriter out) {
      if (uid == null) {
         out.print(JsonUtil.loadFalseResult(1, "用户未登录"));
         return;
      }
      UserInfo user = userInfoService.selectByPKey(uid);
      if (user == null) {
         out.print(JsonUtil.loadFalseResult(1, "用户不存在"));
         return;
      }
      UserInfo userInfo = UserUtil.filterForClientUser(user);
      // 1.5.0 版本之后返回新的等级
      String version = acceptData.getVersion();
      if (version != null && version.trim().length() > 0) {
         int versionCode = Integer.parseInt(version);
         String platform = acceptData.getPlatform();
         AppVersionInfo versionInfo = appVersionService.getByPlatformAndVersion(platform, "1.5.0");
         if (versionInfo != null) {
            int versionCode150 = versionInfo.getVersionCode();
            if (versionCode >= versionCode150) {
               UserInfoExtraVO userInfoExtra = userInfoExtraService.getInfoExtraVOByUid(user.getId());
               if (userInfoExtra != null && userInfoExtra.getUserRank() != null) {
                  String picture = userInfoExtra.getUserRank().getPicture();
                  String icon = userInfoExtra.getUserRank().getIcon();
                  userInfo.setRankIcon(icon);
                  userInfo.setRankNamePicture(picture);
               }
            }
         }
      }
      /* 总订单统计 */
      Map<String, BigDecimal> countOrder = commonOrderService.countHistoryOrder(uid, null, null);
      int self = 0; // 返利订单
      if (countOrder.get("totalSelf") != null) {
         self = countOrder.get("totalSelf").intValue();
      }
      int shared = 0;  // 分享订单
      if (countOrder.get("totalShared") != null) {
         shared = countOrder.get("totalShared").intValue();
      }
      int invite = 0;  // 邀请订单
      if (countOrder.get("totalInvite") != null) {
         invite = countOrder.get("totalInvite").intValue();
      }
      int total = self + shared + invite;
      JSONObject data = new JSONObject();
      data.put("total", total);
      data.put("self", self);
      data.put("shared", shared);
      data.put("invite", invite);
      data.put("userInfo", userInfo);
      out.print(JsonUtil.loadTrueResult(data));
   }
   @RequestMapping(value = "getNearOrderNum", method = RequestMethod.POST)
   public void getNearOrderNum(AcceptData acceptData, Long uid, Integer goodsType, PrintWriter out) {
      if (uid == null) {
         out.print(JsonUtil.loadFalseResult(1, "用户未登录"));
         return;
      }
      if (goodsType != null && goodsType == 0) {
         goodsType = null;
      }
      /* 今日订单统计 */
      Map<String, BigDecimal> countToday = commonOrderService.countHistoryOrder(uid, 1, goodsType);
      int todaySelf = 0;
      if (countToday.get("totalSelf") != null) {
         todaySelf = countToday.get("totalSelf").intValue();
      }
      int todayShared = 0;
      if (countToday.get("totalShared") != null) {
         todayShared = countToday.get("totalShared").intValue();
      }
      int todayInvite = 0;
      if (countToday.get("totalInvite") != null) {
         todayInvite = countToday.get("totalInvite").intValue();
      }
      int todayTotal = todaySelf + todayShared + todayInvite;
      JSONObject todaydata = new JSONObject();
      todaydata.put("total", todayTotal);
      todaydata.put("self", todaySelf);
      todaydata.put("shared", todayShared);
      todaydata.put("invite", todayInvite);
      /* 昨日订单统计 */
      Map<String, BigDecimal> countYesterday = commonOrderService.countHistoryOrder(uid, 2, goodsType);
      int yesterdaySelf = 0;
      if (countYesterday.get("totalSelf") != null) {
         yesterdaySelf = countYesterday.get("totalSelf").intValue();
      }
      int yesterdayShared = 0;
      if (countYesterday.get("totalShared") != null) {
         yesterdayShared = countYesterday.get("totalShared").intValue();
      }
      int yesterdayInvite = 0;
      if (countYesterday.get("totalInvite") != null) {
         yesterdayInvite = countYesterday.get("totalInvite").intValue();
      }
      int yesterdayTotal = yesterdaySelf + yesterdayShared + yesterdayInvite;
      JSONObject yesterdaydata = new JSONObject();
      yesterdaydata.put("total", yesterdayTotal);
      yesterdaydata.put("self", yesterdaySelf);
      yesterdaydata.put("shared", yesterdayShared);
      yesterdaydata.put("invite", yesterdayInvite);
      JSONObject data = new JSONObject();
      data.put("today", todaydata);
      data.put("yesterday", yesterdaydata);
      out.print(JsonUtil.loadTrueResult(data));
   }
   /**
    * 统计奖金
    *
    * @param acceptData
    * @param uid
    * @param out
    */
   @RequestMapping(value = "countBonus", method = RequestMethod.POST)
   public void countBonus(AcceptData acceptData, Long uid, Integer dateType, PrintWriter out) {
      if (uid == null) {
         out.print(JsonUtil.loadFalseResult(1, "用户未登录"));
         return;
      }
      try {
         Object shareCount = 0;
         BigDecimal sharemoney = new BigDecimal(0.00);
         Object inviteCount = 0;
         BigDecimal inviteMoney = new BigDecimal(0.00);
         Map<String, Object> shareMap = commonOrderService.countBonusOrderMoneyAndNumber(uid, 2, dateType, null,
               null);
         if (shareMap != null) {
            Object totalNum = shareMap.get("totalNum");
            if (totalNum != null) {
               shareCount = totalNum;
            }
            Object totalmoney = shareMap.get("totalmoney");
            if (totalmoney != null) {
               sharemoney = (BigDecimal) totalmoney;
            }
         }
         // 邀请统计
         Map<String, Object> inviteMap = commonOrderService.countBonusOrderMoneyAndNumber(uid, 3, dateType, null,
               null);
         if (inviteMap != null) {
            Object totalNum = inviteMap.get("totalNum");
            if (totalNum != null) {
               inviteCount = totalNum;
            }
            Object totalmoney = inviteMap.get("totalmoney");
            if (totalmoney != null) {
               inviteMoney = (BigDecimal) totalmoney;
            }
         }
         JSONObject data = new JSONObject();
         data.put("shareCount", shareCount);
         data.put("sharemoney", sharemoney.setScale(2, BigDecimal.ROUND_DOWN).toString());
         data.put("inviteCount", inviteCount);
         data.put("inviteMoney", inviteMoney.setScale(2, BigDecimal.ROUND_DOWN).toString());
         data.put("showTiCheng", hongBaoV2CountService.getTotalTiChengCount(uid) > 0);
         out.print(JsonUtil.loadTrueResult(data));
      } catch (Exception e) {
         out.print(JsonUtil.loadFalseResult(1, "获取信息失败"));
         e.printStackTrace();
      }
   }
   /**
    * 上传解析到的淘宝订单
    *
    * @param acceptData
    * @param uid
    * @param orderData
    * @param source
    * @param out
    */
   @RequestMapping(value = "uploadParsedOrder", method = RequestMethod.POST)
   public void uploadParsedOrder(AcceptData acceptData, Long uid, String orderData, String source,
         HttpServletRequest request, PrintWriter out) {
      if (uid == null || StringUtil.isNullOrEmpty(orderData)) {
         out.print(JsonUtil.loadFalseResult(1, "数据错误"));
         return;
      }
      String[] orders = null;
      try {
         String orderIds = DESUtil.decode(orderData, StringUtil.getBase64String("YeShiFANLI889*+"),
               StringUtil.getBase64String("*M#34f?,"));
         orders = orderIds.split(",");
      } catch (Exception e) {
         e.printStackTrace();
      }
      if ("findOrder".equalsIgnoreCase(source)) {
         boolean success = false;
         // 匹配订单
         for (String order : orders) {
            if (!StringUtil.isNullOrEmpty(order)) {
               List<TaoBaoOrder> orderList = taoBaoOrderService.getTaoBaoOrderByOrderId(order);
               // 计算订单状态
               if (orderList != null && orderList.size() > 0) {
                  int inValid = 0;
                  for (TaoBaoOrder taoBaoOrder : orderList) {
                     if (taoBaoOrder.getOrderState().equalsIgnoreCase("订单失效")) {
                        inValid++;
                     }
                  }
                  // 订单全是失效
                  if (inValid == orderList.size())
                     continue;
                  List<CommonOrder> commonOrderList = commonOrderService
                        .listBySourceTypeAndOrderId(Constant.SOURCE_TYPE_TAOBAO, order);
                  if (commonOrderList == null || commonOrderList.size() == 0) {
                     Order o = orderService.findOrderByOrderIdAndType(order, Order.ORDER_TYPE_TAOBAO);
                     if (o != null)// 原来已经存在,则不需要找回
                        continue;
                     Order or = new Order();
                     or.setBeizhu("人工自动找回补单");
                     or.setCreatetime(System.currentTimeMillis());
                     or.setOrderId(order);
                     or.setOrderType(1);
                     or.setState(Order.STATE_YIZHIFU);
                     or.setUserInfo(new UserInfo(uid));
                     or.setVersion(2);
                     // 加入到订单表
                     boolean isS;
                     try {
                        isS = orderService.addOrder(or);
                        if (isS) {
                           CMQManager.getInstance().addTaoBaoOrderMsg(order, orderList);
                           // 加入到LostOrder
                           LostOrder lo = new LostOrder();
                           lo.setCreateTime(System.currentTimeMillis());
                           lo.setHandleTime(System.currentTimeMillis());
                           lo.setIpInfo(IPUtil.getRemotIP(request) + ":" + request.getRemotePort());
                           lo.setOrder(or);
                           lo.setOrderId(order);
                           lo.setState(LostOrder.STATE_PASS);
                           lo.setResultCode(LostOrder.RESULT_CODE_VERFING);
                           lo.setType(LostOrder.TYPE_TAOBAO);
                           lo.setUserInfo(new UserInfo(uid));
                           lostOrderService.saveLostOrder(lo);
                           if (success == false)
                              success = true;
                        }
                     } catch (Exception e) {
                        try {
                           LogHelper.errorDetailInfo(e, order, "");
                        } catch (Exception e1) {
                           e1.printStackTrace();
                        }
                     }
                  }
               }
            }
         }
         if (success) {
            out.print(JsonUtil.loadTrue(0, null, "订单找回成功,请到“我的-订单”中查看"));
            return;
         } else {
            out.print(JsonUtil.loadFalseResult("没有可找回的订单,请尝试手动找回"));
            return;
         }
      } else {
         String orderId = null;
         if (orders != null && orders.length > 0)
            orderId = orders[0];
         if (!StringUtil.isNullOrEmpty(orderId) && orderId.length() > 6) {
            orderId = orderId.substring(orderId.length() - 6, orderId.length());
            UserExtraTaoBaoInfo info = userExtraTaoBaoInfoService.getByUid(uid);
            if (info != null) {
               UserExtraTaoBaoInfo update = new UserExtraTaoBaoInfo();
               update.setId(info.getId());
               update.setTaoBaoOrderEnd6Num(orderId);
               userExtraTaoBaoInfoService.updateSelective(update);
            }
         }
      }
      out.print(JsonUtil.loadTrueResult(""));
   }
   /**
    * 时间转换
    *
    * @param slotTime
    * @param startTime
    * @return
    * @throws Exception
    */
   public String convertDate(Integer slotTime, String endTime) throws Exception {
      String startTime = null;
      switch (slotTime) {
      case 1: // 最近三天
         startTime = DateUtil.reduceDay(2, endTime);
         break;
      case 2: // 最近七天
         startTime = DateUtil.reduceDay(6, endTime);
         break;
      case 3: // 最近15天 (半月)
         startTime = DateUtil.reduceDay(14, endTime);
         break;
      case 4: // 最近三十天 (本月)
         startTime = DateUtil.reduceDay(29, endTime);
         break;
      case 5: // 最近九十天(近三月)
         startTime = DateUtil.reduceDay(3 * 30 - 1, endTime);
         break;
      case 6: // 最近一百八十天(近半年)
         startTime = DateUtil.reduceDay(6 * 30 - 1, endTime);
         break;
      default:
         break;
      }
      return startTime;
   }
}
package com.yeshi.fanli.controller.client.v1;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import com.yeshi.fanli.util.*;
import com.yeshi.fanli.util.mq.rabbit.RabbitmqManager;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.yeshi.utils.DateUtil;
import org.yeshi.utils.IPUtil;
import org.yeshi.utils.JsonUtil;
import org.yeshi.utils.TimeUtil;
import org.yeshi.utils.encrypt.DESUtil;
import com.yeshi.fanli.entity.AppVersionInfo;
import com.yeshi.fanli.entity.accept.AcceptData;
import com.yeshi.fanli.entity.bus.user.HongBaoV2;
import com.yeshi.fanli.entity.bus.user.LostOrder;
import com.yeshi.fanli.entity.bus.user.Order;
import com.yeshi.fanli.entity.bus.user.UserExtraTaoBaoInfo;
import com.yeshi.fanli.entity.bus.user.UserInfo;
import com.yeshi.fanli.entity.order.CommonOrder;
import com.yeshi.fanli.entity.order.TeamOrderStatistic;
import com.yeshi.fanli.entity.system.ConfigKeyEnum;
import com.yeshi.fanli.entity.taobao.TaoBaoOrder;
import com.yeshi.fanli.exception.order.CommonOrderException;
import com.yeshi.fanli.log.LogHelper;
import com.yeshi.fanli.service.inter.config.AppVersionService;
import com.yeshi.fanli.service.inter.config.ConfigService;
import com.yeshi.fanli.service.inter.count.HongBaoV2CountService;
import com.yeshi.fanli.service.inter.order.CommonOrderService;
import com.yeshi.fanli.service.inter.order.LostOrderService;
import com.yeshi.fanli.service.inter.order.OrderService;
import com.yeshi.fanli.service.inter.order.TeamOrderStatisticService;
import com.yeshi.fanli.service.inter.order.dividents.TeamDividentsSourceOrderService;
import com.yeshi.fanli.service.inter.order.tb.TaoBaoOrderService;
import com.yeshi.fanli.service.inter.user.UserInfoExtraService;
import com.yeshi.fanli.service.inter.user.UserInfoService;
import com.yeshi.fanli.service.inter.user.tb.UserExtraTaoBaoInfoService;
import com.yeshi.fanli.util.account.UserUtil;
import com.yeshi.fanli.vo.order.CommonOrderVO;
import com.yeshi.fanli.vo.order.TeamOrderStatisticVO;
import com.yeshi.fanli.vo.user.UserInfoExtraVO;
import net.sf.json.JSONObject;
@Controller
@RequestMapping("api/v1/user/order")
public class UserOrderController {
   @Resource
   private ConfigService configService;
   @Resource
   private AppVersionService appVersionService;
   @Resource
   private UserInfoService userInfoService;
   @Resource
   private UserInfoExtraService userInfoExtraService;
   @Resource
   private CommonOrderService commonOrderService;
   @Resource
   private HongBaoV2CountService hongBaoV2CountService;
   @Resource
   private UserExtraTaoBaoInfoService userExtraTaoBaoInfoService;
   @Resource
   private TaoBaoOrderService taoBaoOrderService;
   @Resource
   private OrderService orderService;
   @Resource
   private LostOrderService lostOrderService;
   @Resource
   private RedisManager redisManager;
//   @Resource
   private TeamOrderStatisticService teamOrderStatisticService;
   @Resource
   private TeamDividentsSourceOrderService teamDividentsSourceOrderService;
   @Resource
   private RabbitmqManager rabbitmqManager;
   /**
    * 订单列表
    *
    * @param acceptData
    * @param page
    * @param uid
    * @param state
    *            状态:0全部 1-未到账 2-已到账 3-已失效 4已收货
    * @param type
    *            类型:1-返利订单 2-分享订单 3-邀请订单
    * @param orderState
    *            1有效订单 2 维权订单 3失效订单
    * @param orderNo
    *            订单号
    * @param startTime
    *            起始时间
    * @param endTime
    *            结束时间
    * @param slotTime
    *            时间段:1-最近三天 2-最近七天 3最近半月 4本月 5近三月 6近半年
    * @param dateType
    *            1-今日 2-昨天 3-本月 4-上个月 |
    * @param needCount
    * @param out
    */
   @RequestMapping(value = "getorder", method = RequestMethod.POST)
   public void getOrder(AcceptData acceptData, Integer page, Long uid, Integer state,
         @RequestParam(name = "type", required = false) String type1, Integer orderState, String orderNo,
         String startTime, String endTime, Integer slotTime, Boolean needCount, Integer dateType, Integer goodsType,
         PrintWriter out) {
      Integer type = null;
      if (StringUtil.isNullOrEmpty(type1)) {
         type = null;
      } else {
         type = Integer.parseInt(type1);
      }
      if (uid == null) {
         out.print(JsonUtil.loadFalseResult(1, "用户未登录"));
         return;
      }
      if (needCount == null)
         needCount = false;
      if (page == null || page < 1) {
         page = 1;
      }
      if (state != null && state == 0) {
         state = null;// 所有状态
      }
      // 转换状态
      if (state != null && state == 4) {
         orderState = state; // 已收货状态
         state = null; // 清空
      }
      // 转换状态
      if (state != null && state == 5) {
         orderState = 2; // 已维权
         state = null; // 清空
      }
      if (type != null && type == 0) {
         type = null; // 所有类型订单
      }
      Integer tempState = state;
      // 转换状态
      if (state != null && orderState != null && (orderState == 2 || orderState == 3)) {
         state = null; // 清空
      }
      if (!VersionUtil.greaterThan_1_6_0(acceptData.getPlatform(), acceptData.getVersion())) {
         goodsType = Constant.SOURCE_TYPE_TAOBAO;
      } else if (goodsType != null && goodsType == 0) {
         goodsType = null; // 所有平台订单
      }
      List<Integer> listSource = new ArrayList<>();
      if (goodsType != null) {
         listSource.add(goodsType);
      }
      try {
         if (slotTime != null) {
            SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
            endTime = sd.format(new Date());
            startTime = convertDate(slotTime, endTime);
         }
      } catch (Exception e) {
         e.printStackTrace();
      }
      if (endTime != null && endTime.trim().length() > 0) {
         endTime += " 23:59:59";
      }
      String orderFindUrl = null;
      try {
         long count = 0;
         long totalValid = 0;
         long totalProces = 0;
         long totalInvite = 0;
         long todayTotal = 0;
         BigDecimal todayMoney = null;
         // 查询列表
         List<CommonOrderVO> list = commonOrderService.getOrderByUid(acceptData, page, uid, state, type, orderState,
               orderNo, startTime, endTime, dateType, listSource);
         if (list != null && list.size() > 0) {
            // 统计总数
            count = commonOrderService.countGroupOrderNoByUid(uid, state, type, orderState, orderNo, startTime,
                  endTime, dateType, listSource);
         }
         // 需要统计筛选信息 :未失效的总金额 以及订单
         if (needCount && page == 1) {
            todayMoney = commonOrderService.countBonusOrderMoney(uid, type, dateType, startTime, endTime,
                  goodsType);
            todayTotal = commonOrderService.countBonusOrderNumber(uid, type, dateType, startTime, endTime,
                  goodsType);
            // 有效订单
            totalValid = commonOrderService.countUserOrderToApp(uid, type, startTime, endTime, dateType, goodsType,
                  tempState, 1);
            // 维权订单
            totalProces = commonOrderService.countUserOrderToApp(uid, type, startTime, endTime, dateType, goodsType,
                  null, 2);
            // 失效订单
            totalInvite = commonOrderService.countUserOrderToApp(uid, type, startTime, endTime, dateType, goodsType,
                  null, 3);
         }
         if (todayMoney == null) {
            todayMoney = new BigDecimal(0.00);
         }
         // 判断是否需要显示订单找回提示
         if (VersionUtil.greaterThan_2_0_2(acceptData.getPlatform(), acceptData.getVersion()))
            if (state == null && page == 1) {
               if (redisManager.frequencyLimit("orderlist-" + acceptData.getDevice(), 8, 4) || count == 0L) {// 8秒内请求3次触发
                  orderFindUrl = configService.getValue(ConfigKeyEnum.orderFindNotifyUrl.getKey(), SystemInfoUtil.getSystem(acceptData));
               }
            }
         JSONObject data = new JSONObject();
         data.put("count", count);
         data.put("result_list", JsonUtil.getApiCommonGson().toJson(list));
         data.put("helpUrl", configService.getValue(ConfigKeyEnum.orderListHelp.getKey(),SystemInfoUtil.getSystem(acceptData)));
         if (type != null && type == 1) {
            data.put("todayMoney", "预估返利总额 ¥" + todayMoney.setScale(2, BigDecimal.ROUND_DOWN).toString());
         } else if (type != null && (type == 2 || type == 3)) {
            data.put("todayMoney", "预估奖金总额 ¥" + todayMoney.setScale(2, BigDecimal.ROUND_DOWN).toString());
         } else {
            data.put("todayMoney", "预估总额 ¥" + todayMoney.setScale(2, BigDecimal.ROUND_DOWN).toString());
         }
         data.put("todayTotal", "共" + todayTotal + "笔");
         data.put("totalValid", totalValid); // 有效数量
         data.put("totalProces", totalProces); // 维权数量
         data.put("totalInvite", totalInvite); // 失效数量
         if (orderFindUrl != null)
            data.put("findOrderHelpUrl", orderFindUrl);
         out.print(JsonUtil.loadTrueResult(data));
      } catch (CommonOrderException e) {
         out.print(JsonUtil.loadFalseResult(e.getCode(), e.getMsg()));
      } catch (Exception e) {
         out.print(JsonUtil.loadFalseResult(1, "查询失败"));
         LogHelper.errorDetailInfo(e);
      }
   }
   /**
    * 用户订单统计
    *
    * @param acceptData
    * @param uid
    *            用户id
    * @param out
    */
   @RequestMapping(value = "countorder", method = RequestMethod.POST)
   public void countOrder(AcceptData acceptData, Long uid, PrintWriter out) {
      if (uid == null) {
         out.print(JsonUtil.loadFalseResult(1, "用户未登录"));
         return;
      }
      try {
         UserInfo user = userInfoService.selectByPKey(uid);
         if (user == null) {
            out.print(JsonUtil.loadFalseResult(1, "用户不存在"));
            return;
         }
         UserInfo userInfo = UserUtil.filterForClientUser(user);
         // 1.5.0 版本之后返回新的等级
         String version = acceptData.getVersion();
         if (version != null && version.trim().length() > 0) {
            int versionCode = Integer.parseInt(version);
            String platform = acceptData.getPlatform();
            AppVersionInfo versionInfo = appVersionService.getByPlatformAndVersion(platform, "1.5.0",acceptData.getSystem());
            if (versionInfo != null) {
               int versionCode150 = versionInfo.getVersionCode();
               if (versionCode >= versionCode150) {
                  UserInfoExtraVO userInfoExtra = userInfoExtraService.getInfoExtraVOByUid(user.getId());
                  if (userInfoExtra != null && userInfoExtra.getUserRank() != null) {
                     String picture = userInfoExtra.getUserRank().getPicture();
                     String icon = userInfoExtra.getUserRank().getIcon();
                     userInfo.setRankIcon(icon);
                     userInfo.setRankNamePicture(picture);
                  }
               }
            }
         }
         JSONObject data = new JSONObject();
         data.put("userInfo", userInfo);
         Integer goodsType = null; // 版本区分
         if (!VersionUtil.greaterThan_1_6_0(acceptData.getPlatform(), acceptData.getVersion())) {
            goodsType = Constant.SOURCE_TYPE_TAOBAO;
         }
         /* 总订单统计 */
         Map<String, BigDecimal> countOrder = commonOrderService.countHistoryOrder(uid, null, goodsType);
         int self = 0;
         if (countOrder.get("totalSelf") != null) {
            self = countOrder.get("totalSelf").intValue();
         }
         int shared = 0;
         if (countOrder.get("totalShared") != null) {
            shared = countOrder.get("totalShared").intValue();
         }
         int invite = 0;
         if (countOrder.get("totalInvite") != null) {
            invite = countOrder.get("totalInvite").intValue();
         }
         int total = self + shared + invite;
         data.put("total", total);
         data.put("self", self);
         data.put("shared", shared);
         data.put("invite", invite);
         /* 今日订单统计 */
         Map<String, BigDecimal> countToday = commonOrderService.countHistoryOrder(uid, 1, goodsType);
         int todaySelf = 0;
         if (countToday.get("totalSelf") != null) {
            todaySelf = countToday.get("totalSelf").intValue();
         }
         int todayShared = 0;
         if (countToday.get("totalShared") != null) {
            todayShared = countToday.get("totalShared").intValue();
         }
         int todayInvite = 0;
         if (countToday.get("totalInvite") != null) {
            todayInvite = countToday.get("totalInvite").intValue();
         }
         int todayTotal = todaySelf + todayShared + todayInvite;
         JSONObject todaydata = new JSONObject();
         todaydata.put("total", todayTotal);
         todaydata.put("self", todaySelf);
         todaydata.put("shared", todayShared);
         todaydata.put("invite", todayInvite);
         data.put("today", todaydata);
         /* 昨日订单统计 */
         Map<String, BigDecimal> countYesterday = commonOrderService.countHistoryOrder(uid, 2, goodsType);
         int yesterdaySelf = 0;
         if (countYesterday.get("totalSelf") != null) {
            yesterdaySelf = countYesterday.get("totalSelf").intValue();
         }
         int yesterdayShared = 0;
         if (countYesterday.get("totalShared") != null) {
            yesterdayShared = countYesterday.get("totalShared").intValue();
         }
         int yesterdayInvite = 0;
         if (countYesterday.get("totalInvite") != null) {
            yesterdayInvite = countYesterday.get("totalInvite").intValue();
         }
         int yesterdayTotal = yesterdaySelf + yesterdayShared + yesterdayInvite;
         JSONObject yesterdaydata = new JSONObject();
         yesterdaydata.put("total", yesterdayTotal);
         yesterdaydata.put("self", yesterdaySelf);
         yesterdaydata.put("shared", yesterdayShared);
         yesterdaydata.put("invite", yesterdayInvite);
         data.put("yesterday", yesterdaydata);
         out.print(JsonUtil.loadTrueResult(data));
      } catch (Exception e) {
         out.print(JsonUtil.loadFalseResult(1, "获取信息失败"));
         e.printStackTrace();
      }
   }
   /**
    * 统计各个平台数据
    *
    * @param acceptData
    * @param uid
    * @param goodsType
    * @param out
    */
   @RequestMapping(value = "getNearOrderNum", method = RequestMethod.POST)
   public void getNearOrderNum(AcceptData acceptData, Long uid, Integer goodsType, PrintWriter out) {
      if (uid == null) {
         out.print(JsonUtil.loadFalseResult(1, "用户未登录"));
         return;
      }
      if (goodsType == null || goodsType > 3 || goodsType < 1) {
         out.print(JsonUtil.loadFalseResult(1, "平台类型缺失或不正确"));
         return;
      }
      /* 今日订单统计 */
      Map<String, BigDecimal> countToday = commonOrderService.countHistoryOrder(uid, 1, goodsType);
      int todaySelf = 0;
      if (countToday.get("totalSelf") != null) {
         todaySelf = countToday.get("totalSelf").intValue();
      }
      int todayShared = 0;
      if (countToday.get("totalShared") != null) {
         todayShared = countToday.get("totalShared").intValue();
      }
      int todayInvite = 0;
      if (countToday.get("totalInvite") != null) {
         todayInvite = countToday.get("totalInvite").intValue();
      }
      int todayTotal = todaySelf + todayShared + todayInvite;
      JSONObject todaydata = new JSONObject();
      todaydata.put("total", todayTotal);
      todaydata.put("self", todaySelf);
      todaydata.put("shared", todayShared);
      todaydata.put("invite", todayInvite);
      /* 昨日订单统计 */
      Map<String, BigDecimal> countYesterday = commonOrderService.countHistoryOrder(uid, 2, goodsType);
      int yesterdaySelf = 0;
      if (countYesterday.get("totalSelf") != null) {
         yesterdaySelf = countYesterday.get("totalSelf").intValue();
      }
      int yesterdayShared = 0;
      if (countYesterday.get("totalShared") != null) {
         yesterdayShared = countYesterday.get("totalShared").intValue();
      }
      int yesterdayInvite = 0;
      if (countYesterday.get("totalInvite") != null) {
         yesterdayInvite = countYesterday.get("totalInvite").intValue();
      }
      int yesterdayTotal = yesterdaySelf + yesterdayShared + yesterdayInvite;
      JSONObject yesterdaydata = new JSONObject();
      yesterdaydata.put("total", yesterdayTotal);
      yesterdaydata.put("self", yesterdaySelf);
      yesterdaydata.put("shared", yesterdayShared);
      yesterdaydata.put("invite", yesterdayInvite);
      JSONObject data = new JSONObject();
      data.put("today", todaydata);
      data.put("yesterday", yesterdaydata);
      out.print(JsonUtil.loadTrueResult(data));
   }
   /**
    * 统计奖金
    *
    * @param acceptData
    * @param uid
    * @param out
    */
   @RequestMapping(value = "countBonus", method = RequestMethod.POST)
   public void countBonus(AcceptData acceptData, Long uid, Integer dateType, PrintWriter out) {
      if (uid == null) {
         out.print(JsonUtil.loadFalseResult(1, "用户未登录"));
         return;
      }
      long nowTime = System.currentTimeMillis();
      long recievedTime = TimeUtil.convertToTimeTemp(TimeUtil.getGernalTime(nowTime, "yyyy-MM") + "-25 10",
            "yyyy-MM-dd HH");
      try {
         Object shareCount = 0;
         BigDecimal sharemoney = new BigDecimal(0.00);
         Object inviteCount = 0;
         BigDecimal inviteMoney = new BigDecimal(0.00);
         if (nowTime > recievedTime && dateType == 4) {// 实际到账
            List<Integer> inviteTypes = new ArrayList<>();
            inviteTypes.add(HongBaoV2.TYPE_YIJI);
            inviteTypes.add(HongBaoV2.TYPE_ERJI);
            inviteTypes.add(HongBaoV2.TYPE_SHARE_YIJI);
            inviteTypes.add(HongBaoV2.TYPE_SHARE_ERJI);
            List<Integer> shareTypes = new ArrayList<>();
            shareTypes.add(HongBaoV2.TYPE_SHARE_GOODS);
            Date minGetTime = new Date(
                  TimeUtil.convertToTimeTemp(TimeUtil.getGernalTime(nowTime, "yyyy-MM") + "-25", "yyyy-MM-dd"));
            Date maxGetTime = new Date(nowTime);
            inviteMoney = hongBaoV2CountService.sumAlreadyGetMoneyByUid(uid, inviteTypes, minGetTime, maxGetTime);
            inviteCount = hongBaoV2CountService.countAlreadyGetMoneyByUid(uid, inviteTypes, minGetTime, maxGetTime);
            sharemoney = hongBaoV2CountService.sumAlreadyGetMoneyByUid(uid, shareTypes, minGetTime, maxGetTime);
            shareCount = hongBaoV2CountService.countAlreadyGetMoneyByUid(uid, shareTypes, minGetTime, maxGetTime);
         } else {// 预估到账
            Integer goodsType = null; // 版本区分
            if (!VersionUtil.greaterThan_1_6_0(acceptData.getPlatform(), acceptData.getVersion())) {
               goodsType = Constant.SOURCE_TYPE_TAOBAO;
            }
            Map<String, Object> shareMap = commonOrderService.countBonusOrderMoneyAndNumber(uid, 2, dateType, null,
                  null, goodsType);
            if (shareMap != null) {
               Object totalNum = shareMap.get("totalNum");
               if (totalNum != null) {
                  shareCount = totalNum;
               }
               Object totalmoney = shareMap.get("totalmoney");
               if (totalmoney != null) {
                  sharemoney = (BigDecimal) totalmoney;
               }
            }
            // 邀请统计
            Map<String, Object> inviteMap = commonOrderService.countBonusOrderMoneyAndNumber(uid, 3, dateType, null,
                  null, goodsType);
            if (inviteMap != null) {
               Object totalNum = inviteMap.get("totalNum");
               if (totalNum != null) {
                  inviteCount = totalNum;
               }
               Object totalmoney = inviteMap.get("totalmoney");
               if (totalmoney != null) {
                  inviteMoney = (BigDecimal) totalmoney;
               }
            }
         }
         JSONObject data = new JSONObject();
         data.put("shareCount", shareCount);
         data.put("sharemoney", sharemoney.setScale(2, BigDecimal.ROUND_DOWN).toString());
         data.put("inviteCount", inviteCount);
         data.put("inviteMoney", inviteMoney.setScale(2, BigDecimal.ROUND_DOWN).toString());
         data.put("showTiCheng", hongBaoV2CountService.getTotalTiChengCount(uid) > 0);
         out.print(JsonUtil.loadTrueResult(data));
      } catch (Exception e) {
         out.print(JsonUtil.loadFalseResult(1, "获取信息失败"));
         e.printStackTrace();
      }
   }
   /**
    * 上传解析到的淘宝订单
    *
    * @param acceptData
    * @param uid
    * @param orderData
    * @param source
    * @param out
    */
   @RequestMapping(value = "uploadParsedOrder", method = RequestMethod.POST)
   public void uploadParsedOrder(AcceptData acceptData, Long uid, String orderData, String source,
         HttpServletRequest request, PrintWriter out) {
      if (uid == null || StringUtil.isNullOrEmpty(orderData)) {
         out.print(JsonUtil.loadFalseResult(1, "数据错误"));
         return;
      }
      String[] orders = null;
      try {
         String orderIds = DESUtil.decode(orderData, StringUtil.getBase64String("YeShiFANLI889*+"),
               StringUtil.getBase64String("*M#34f?,"));
         orders = orderIds.split(",");
      } catch (Exception e) {
         e.printStackTrace();
      }
      if ("findOrder".equalsIgnoreCase(source)) {
         boolean success = false;
         // 匹配订单
         for (String order : orders) {
            if (!StringUtil.isNullOrEmpty(order)) {
               List<TaoBaoOrder> orderList = taoBaoOrderService.getTaoBaoOrderByOrderId(order);
               // 计算订单状态
               if (orderList != null && orderList.size() > 0) {
                  int inValid = 0;
                  for (TaoBaoOrder taoBaoOrder : orderList) {
                     if (taoBaoOrder.getOrderState().equalsIgnoreCase("订单失效")) {
                        inValid++;
                     }
                  }
                  // 订单全是失效
                  if (inValid == orderList.size())
                     continue;
                  List<CommonOrder> commonOrderList = commonOrderService
                        .listBySourceTypeAndOrderId(Constant.SOURCE_TYPE_TAOBAO, order);
                  if (commonOrderList == null || commonOrderList.size() == 0) {
                     Order o = orderService.findOrderByOrderIdAndType(order, Order.ORDER_TYPE_TAOBAO);
                     if (o != null)// 原来已经存在,则不需要找回
                        continue;
                     Order or = new Order();
                     or.setBeizhu("人工自动找回补单");
                     or.setCreatetime(System.currentTimeMillis());
                     or.setOrderId(order);
                     or.setOrderType(1);
                     or.setState(Order.STATE_YIZHIFU);
                     or.setUserInfo(new UserInfo(uid));
                     or.setVersion(2);
                     // 加入到订单表
                     boolean isS;
                     try {
                        isS = orderService.addOrder(or);
                        if (isS) {
                           rabbitmqManager.addTaoBaoOrderMsg(order);
                           // 加入到LostOrder
                           LostOrder lo = new LostOrder();
                           lo.setCreateTime(System.currentTimeMillis());
                           lo.setHandleTime(System.currentTimeMillis());
                           lo.setIpInfo(IPUtil.getRemotIP(request) + ":" + request.getRemotePort());
                           lo.setOrder(or);
                           lo.setOrderId(order);
                           lo.setState(LostOrder.STATE_PASS);
                           lo.setResultCode(LostOrder.RESULT_CODE_VERFING);
                           lo.setType(LostOrder.TYPE_TAOBAO);
                           lo.setUserInfo(new UserInfo(uid));
                           lostOrderService.saveLostOrder(lo);
                           if (success == false)
                              success = true;
                        }
                     } catch (Exception e) {
                        try {
                           LogHelper.errorDetailInfo(e, order, "");
                        } catch (Exception e1) {
                           e1.printStackTrace();
                        }
                     }
                  }
               }
            }
         }
         if (success) {
            out.print(JsonUtil.loadTrue(0, null, "订单找回成功,请到“我的-订单”中查看"));
            return;
         } else {
            out.print(JsonUtil.loadFalseResult("没有可找回的订单,请尝试手动找回"));
            return;
         }
      } else {
         String orderId = null;
         if (orders != null && orders.length > 0)
            orderId = orders[0];
         if (!StringUtil.isNullOrEmpty(orderId) && orderId.length() > 6) {
            orderId = orderId.substring(orderId.length() - 6, orderId.length());
            UserExtraTaoBaoInfo info = userExtraTaoBaoInfoService.getByUid(uid);
            if (info != null) {
               UserExtraTaoBaoInfo update = new UserExtraTaoBaoInfo();
               update.setId(info.getId());
               update.setTaoBaoOrderEnd6Num(orderId);
               userExtraTaoBaoInfoService.updateSelective(update);
            }
         }
      }
      out.print(JsonUtil.loadTrueResult(""));
   }
   /**
    * 时间转换
    *
    * @param slotTime
    * @param startTime
    * @return
    * @throws Exception
    */
   public String convertDate(Integer slotTime, String endTime) throws Exception {
      String startTime = null;
      switch (slotTime) {
      case 1: // 最近三天
         startTime = DateUtil.reduceDay(2, endTime);
         break;
      case 2: // 最近七天
         startTime = DateUtil.reduceDay(6, endTime);
         break;
      case 3: // 最近15天 (半月)
         startTime = DateUtil.reduceDay(14, endTime);
         break;
      case 4: // 最近三十天 (本月)
         startTime = DateUtil.reduceDay(29, endTime);
         break;
      case 5: // 最近九十天(近三月)
         startTime = DateUtil.reduceDay(3 * 30 - 1, endTime);
         break;
      case 6: // 最近一百八十天(近半年)
         startTime = DateUtil.reduceDay(6 * 30 - 1, endTime);
         break;
      default:
         break;
      }
      return startTime;
   }
   /**
    * 获取团队订单统计
    * @Title: getTeamOrderStatistic
    * @Description:
    * @param acceptData
    * @param uid
    * @param out
    * void 返回类型
    * @throws
    */
   @RequestMapping(value = "getTeamOrderStatistic")
   public void getTeamOrderStatistic(AcceptData acceptData, Long uid, PrintWriter out) {
      long now = System.currentTimeMillis();
      TeamOrderStatisticVO vo = new TeamOrderStatisticVO();
      Date minDate = new Date(TimeUtil.convertToTimeTemp(TimeUtil.getGernalTime(now, "yyyyMMdd"), "yyyyMMdd"));
      Date maxDate = new Date(
            TimeUtil.convertToTimeTemp(TimeUtil.getGernalTime(now + 1000 * 60 * 60 * 24L, "yyyyMMdd"), "yyyyMMdd"));
      List<Integer> countList = new ArrayList<>();
      // 统计今日
      TeamOrderStatistic statistic = teamOrderStatisticService.countByUidAndDate(uid, minDate, maxDate);
      if (statistic == null) {
         countList.add(0);
         countList.add(0);
         countList.add(0);
      } else {
         countList.add(statistic.getDirectOrderCount() == null ? 0 : statistic.getDirectOrderCount());
         countList.add(statistic.getInDirectOrderCount() == null ? 0 : statistic.getInDirectOrderCount());
         countList.add(statistic.getOtherOrderCount() == null ? 0 : statistic.getOtherOrderCount());
      }
      vo.setToday(countList);
      // 统计昨日
      minDate = new Date(
            TimeUtil.convertToTimeTemp(TimeUtil.getGernalTime(now - 1000 * 60 * 60 * 24L, "yyyyMMdd"), "yyyyMMdd"));
      maxDate = new Date(TimeUtil.convertToTimeTemp(TimeUtil.getGernalTime(now, "yyyyMMdd"), "yyyyMMdd"));
      countList = new ArrayList<>();
      statistic = teamOrderStatisticService.countByUidAndDate(uid, minDate, maxDate);
      if (statistic == null) {
         countList.add(0);
         countList.add(0);
         countList.add(0);
      } else {
         countList.add(statistic.getDirectOrderCount() == null ? 0 : statistic.getDirectOrderCount());
         countList.add(statistic.getInDirectOrderCount() == null ? 0 : statistic.getInDirectOrderCount());
         countList.add(statistic.getOtherOrderCount() == null ? 0 : statistic.getOtherOrderCount());
      }
      vo.setYesterday(countList);
      // 统计本月
      minDate = new Date(
            TimeUtil.convertToTimeTemp(TimeUtil.getGernalTime(now - 1000 * 60 * 60 * 24L, "yyyyMM"), "yyyyMM"));
      Calendar ca = Calendar.getInstance();
      ca.setTimeInMillis(now);
      ca.add(Calendar.MONTH, 1);
      maxDate = new Date(
            TimeUtil.convertToTimeTemp(TimeUtil.getGernalTime(ca.getTimeInMillis(), "yyyyMM"), "yyyyMM"));
      countList = new ArrayList<>();
      statistic = teamOrderStatisticService.countByUidAndDate(uid, minDate, maxDate);
      if (statistic == null) {
         countList.add(0);
         countList.add(0);
         countList.add(0);
      } else {
         countList.add(statistic.getDirectOrderCount() == null ? 0 : statistic.getDirectOrderCount());
         countList.add(statistic.getInDirectOrderCount() == null ? 0 : statistic.getInDirectOrderCount());
         countList.add(statistic.getOtherOrderCount() == null ? 0 : statistic.getOtherOrderCount());
      }
      vo.setThisMonth(countList);
      // 统计 累计
      minDate = new Date(0L);
      maxDate = new Date(now);
      countList = new ArrayList<>();
      statistic = teamOrderStatisticService.countByUidAndDate(uid, minDate, maxDate);
      if (statistic == null) {
         countList.add(0);
         countList.add(0);
         countList.add(0);
      } else {
         countList.add(statistic.getDirectOrderCount() == null ? 0 : statistic.getDirectOrderCount());
         countList.add(statistic.getInDirectOrderCount() == null ? 0 : statistic.getInDirectOrderCount());
         countList.add(statistic.getOtherOrderCount() == null ? 0 : statistic.getOtherOrderCount());
      }
      vo.setTotal(countList);
   }
   /**
    * 获取团队订单排行
    * @Title: getTeamOrderRank
    * @Description:
    * @param acceptData
    * @param uid
    * @param type 1-今日 2-昨日 3-本月
    * @param out
    * void 返回类型
    * @throws
    */
   @RequestMapping(value = "getTeamOrderRank")
   public void getTeamOrderRank(AcceptData acceptData, Long uid, int type, PrintWriter out) {
      // TODO 需求不明确
   }
   @RequestMapping(value = "searchOrderTeam")
   public void searchOrderTeam(AcceptData acceptData, String key, PrintWriter out) {
//      teamDividentsSourceOrderService.selectByOrderNoSourceType(orderNo, sourceType);
   }
}