package com.yeshi.fanli.controller.admin; import java.io.PrintWriter; import java.math.BigDecimal; import java.util.ArrayList; 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.multipart.MultipartFile; import org.springframework.web.multipart.MultipartHttpServletRequest; import org.yeshi.utils.JsonUtil; import com.yeshi.fanli.dto.common.CategoryInfoDTO; import com.yeshi.fanli.entity.bus.user.HongBaoV2; import com.yeshi.fanli.entity.goods.CommonGoods; import com.yeshi.fanli.entity.money.UserMoneyDebt; import com.yeshi.fanli.entity.money.UserMoneyDebt.UserMoneyDebtTypeEnum; import com.yeshi.fanli.entity.money.UserMoneyDetail; import com.yeshi.fanli.entity.money.UserMoneyDetail.UserMoneyDetailTypeEnum; import com.yeshi.fanli.entity.order.CommonOrder; import com.yeshi.fanli.entity.order.CommonOrderGoods; import com.yeshi.fanli.entity.order.HongBaoOrder; import com.yeshi.fanli.entity.order.UserOrderWeiQuanRecord; import com.yeshi.fanli.entity.system.ConfigKeyEnum; import com.yeshi.fanli.entity.taobao.TaoBaoWeiQuanDrawBack; import com.yeshi.fanli.entity.taobao.TaoBaoWeiQuanOrder; import com.yeshi.fanli.exception.order.CommonOrderException; import com.yeshi.fanli.service.inter.config.ConfigService; import com.yeshi.fanli.service.inter.count.HongBaoV2CountService; import com.yeshi.fanli.service.inter.count.TaoBaoOrderCountService; import com.yeshi.fanli.service.inter.goods.CommonGoodsService; import com.yeshi.fanli.service.inter.money.UserMoneyDebtService; import com.yeshi.fanli.service.inter.money.UserMoneyDetailService; import com.yeshi.fanli.service.inter.money.tb.TaoBaoWeiQuanDrawBackService; import com.yeshi.fanli.service.inter.order.CommonOrderCountService; import com.yeshi.fanli.service.inter.order.CommonOrderService; import com.yeshi.fanli.service.inter.order.HongBaoOrderService; import com.yeshi.fanli.service.inter.order.OrderProcessService; import com.yeshi.fanli.service.inter.order.OrderService; import com.yeshi.fanli.service.inter.order.UserOrderWeiQuanRecordService; import com.yeshi.fanli.service.inter.order.tb.TaoBaoOrderService; import com.yeshi.fanli.service.inter.order.tb.TaoBaoWeiQuanOrderService; import com.yeshi.fanli.service.inter.user.UserSystemCouponRecordService; import com.yeshi.fanli.tag.PageEntity; import com.yeshi.fanli.util.Constant; import com.yeshi.fanli.util.StringUtil; import com.yeshi.fanli.util.taobao.TaoBaoOrderUtil; import com.yeshi.fanli.vo.order.CommonOrderVO; import net.sf.json.JSONObject; @Controller @RequestMapping("admin/new/api/v1/order") public class OrderAdminController { @Resource private OrderService orderService; @Resource private HongBaoV2CountService hongBaoV2CountService; @Resource private CommonOrderCountService commonOrderCountService; @Resource private CommonOrderService commonOrderService; @Resource private TaoBaoOrderService taoBaoOrderService; @Resource private TaoBaoOrderCountService taoBaoOrderCountService; @Resource private TaoBaoWeiQuanDrawBackService taoBaoWeiQuanDrawBackService; @Resource private TaoBaoWeiQuanOrderService taoBaoWeiQuanOrderService; @Resource private UserMoneyDetailService userMoneyDetailService; @Resource private OrderProcessService orderProcessService; @Resource private HongBaoOrderService hongBaoOrderService; @Resource private UserMoneyDebtService userMoneyDebtService; @Resource private UserSystemCouponRecordService userSystemCouponRecordService; @Resource private ConfigService configService; @Resource private UserOrderWeiQuanRecordService userOrderWeiQuanRecordService; @Resource private CommonGoodsService commonGoodsService; /** * 统计历史订单-淘宝订单 * * @param callback * @param request * @param out */ @RequestMapping(value = "countTaoBaoOrder") public void countTaoBaoOrder(String callback, HttpServletRequest request, PrintWriter out) { try { Map countByOdrerType = taoBaoOrderCountService.countByOdrerType(); JSONObject data = new JSONObject(); data.put("typeOrder", countByOdrerType); // 统计今日 未失效订单数量 相同订单号合并为一单 int todayOrder = taoBaoOrderCountService.countToday(); // 统计昨日 未失效订单数量 相同订单号合并为一单 int yesterdayOrder = taoBaoOrderCountService.countYesterday(); data.put("todayOrder", todayOrder); data.put("yesterdayOrder", yesterdayOrder); data.put("genzong", yesterdayOrder); data.put("yesterdayOrder", yesterdayOrder); JsonUtil.printMode(out, callback, JsonUtil.loadTrueResult(data)); } catch (Exception e) { JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("统计失败")); e.printStackTrace(); } } /** * 查询所有用户订单列表 * * @param callback * @param pageIndex * @param pageSize * @param uid * @param state * @param type * @param orderState * @param orderNo * @param startTime * @param endTime * @param out */ @RequestMapping(value = "getUserOrderList") public void getUserOrderList(String callback, Integer pageIndex, Integer pageSize, Integer keyType, String key, Integer state, Integer type, Integer orderState, String startTime, String endTime, Integer sourceType, Integer riskType, BigDecimal payment, PrintWriter out) { if (pageIndex == null || pageIndex < 1) { pageIndex = 1; } if (pageSize == null) pageSize = Constant.PAGE_SIZE; if (state != null && state == 0) { state = null;// 所有状态 } if (type != null && type == 0) { type = null; // 所有类型订单 } if (endTime != null && endTime.trim().length() > 0) { endTime += " 23:59:59"; } try { Date minTime = null; BigDecimal money = null; List listShopId = null; List listGoodsId = null; // 风险 if (riskType != null && !StringUtil.isNullOrEmpty(key) && keyType != null && keyType == 2) { long uid = Long.parseLong(key); // 查询同店铺商品,同商品订单超过一定数量的 List typeList = new ArrayList<>(); typeList.add(HongBaoV2.TYPE_ZIGOU); if (riskType == 1) { listGoodsId = commonOrderCountService.getSameGoodsOrderByUidAndHongBaoType(typeList, uid, Integer.parseInt(configService.get(ConfigKeyEnum.adminMinSameGoodsOrderCount.getKey()))); if (listGoodsId == null || listGoodsId.isEmpty()) { JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("暂无同商品订单")); return; } } else if (riskType == 2) { listShopId = commonOrderCountService.getSameShopOrderByUidAndHongBaoType(typeList, uid, Integer.parseInt(configService.get(ConfigKeyEnum.adminMinSameShopOrderCount.getKey()))); if (listShopId == null || listShopId.isEmpty()) { JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("暂无同店铺订单")); return; } } else if (riskType == 3) { money = new BigDecimal(100); minTime = new Date(System.currentTimeMillis() - 1000 * 60 * 60 * 24 * 180L); } } // 查询列表 List list = commonOrderService.listQuery((pageIndex - 1) * pageSize, pageSize, keyType, key, state, type, orderState, startTime, endTime, sourceType, listShopId, listGoodsId, minTime, money, payment); // 统计总数 long count = commonOrderService.countQuery(keyType, key, state, type, orderState, startTime, endTime, sourceType, listShopId, listGoodsId, minTime, money, payment); for (CommonOrderVO order : list) { CommonOrderGoods goods = order.getCommonOrderGoods(); if (goods != null) { CommonGoods commonGoods = commonGoodsService.getCommonGoodsByGoodsIdAndGoodsType(Long.parseLong(goods.getGoodsId()), goods.getGoodsType()); if (commonGoods != null) { String categoryInfo = commonGoods.getCategoryInfo(); if (!StringUtil.isNullOrEmpty(categoryInfo)) { JSONObject json = JSONObject.fromObject(categoryInfo); CategoryInfoDTO otherDTO = (CategoryInfoDTO) JSONObject.toBean(json, CategoryInfoDTO.class); if (otherDTO != null) { goods.setCategory1(otherDTO.getCid1Name()); goods.setCategory2(otherDTO.getCid2Name()); goods.setCategory3(otherDTO.getCid3Name()); } } } } if (order.getUserId() == null) continue; // 查询是否维权 TaoBaoWeiQuanOrder weiQuanOrder = taoBaoWeiQuanOrderService.listByorderItemId(order.getTradeId()); if (weiQuanOrder != null)// 有维权 { order.setWeiQuanState(weiQuanOrder.getState()); order.setState(CommonOrder.STATE_WQ); TaoBaoWeiQuanDrawBack drawBack = taoBaoWeiQuanDrawBackService.selectByOrderItemIdAndUid( weiQuanOrder.getOrderItemId(), Long.parseLong(order.getUserId())); if (drawBack != null) {// 已经退款 // 计算退款资金 UserMoneyDetail detail = null; switch (order.getHongBaoType()) { case 1: detail = userMoneyDetailService.selectByTypeAndUidAndIdentifyCode( UserMoneyDetailTypeEnum.fanliWeiQuan, Long.parseLong(order.getUserId()), drawBack.getId()); if (detail == null) detail = userMoneyDetailService.selectByTypeAndUidAndIdentifyCode( UserMoneyDetailTypeEnum.weiQuan, Long.parseLong(order.getUserId()), drawBack.getId()); break; case 3: detail = userMoneyDetailService.selectByTypeAndUidAndIdentifyCode( UserMoneyDetailTypeEnum.inviteWeiQuan, Long.parseLong(order.getUserId()), drawBack.getId()); break; case 2: detail = userMoneyDetailService.selectByTypeAndUidAndIdentifyCode( UserMoneyDetailTypeEnum.shareWeiQuan, Long.parseLong(order.getUserId()), drawBack.getId()); break; } if (detail != null) { // 退款成功 order.setWeiQuanBackMoney(detail.getMoney().abs().toString()); } else {// 尚未退款 order.setWeiQuanBackMoney(drawBack.getDrawBackMoney().toString()); // 查询出对应的hongBaoId CommonOrder commonOrder = commonOrderService.selectBySourceTypeAndTradeId( Constant.SOURCE_TYPE_TAOBAO, drawBack.getOrderItemId()); if (commonOrder != null) { HongBaoOrder hongBaoOrder = hongBaoOrderService .selectDetailByCommonOrderId(commonOrder.getId()); if (hongBaoOrder != null) { UserMoneyDebt debt = userMoneyDebtService.selectByTypeAndSourceId( UserMoneyDebtTypeEnum.order, hongBaoOrder.getHongBaoV2().getId()); if (debt != null && debt.getLeftMoney().compareTo(new BigDecimal(0)) > 0) { order.setWeiQuanBackMoney( new BigDecimal(0).subtract(debt.getLeftMoney()).toString());// 未扣除 } } } } } else {// 尚未退款 UserOrderWeiQuanRecord record = userOrderWeiQuanRecordService.selectByOrderInfoAndUid( Long.parseLong(order.getUserId()), weiQuanOrder.getOrderItemId(), Constant.SOURCE_TYPE_TAOBAO); if (record != null) order.setWeiQuanBackMoney("-" + record.getMoney()); else { order.setWeiQuanBackMoney("-" + order.getHongBao()); } } } } int totalPage = (int) (count % pageSize == 0 ? count / pageSize : count / pageSize + 1); PageEntity pe = new PageEntity(pageIndex, pageSize, count, totalPage); JSONObject data = new JSONObject(); data.put("pe", pe); data.put("result_list", list); JsonUtil.printMode(out, callback, JsonUtil.loadTrueResult(data)); } catch (CommonOrderException e) { JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult(e.getMsg())); } catch (Exception e) { JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("查询失败")); e.printStackTrace(); } } /** * 查询订单列表用户uid * * @param callback * @param pageIndex * @param pageSize * @param uid * @param state * @param type * @param orderState * @param orderNo * @param startTime * @param endTime * @param out */ @RequestMapping(value = "getHistoryOrder") public void getHistoryOrder(String callback, Integer pageIndex, Integer pageSize, Long uid, Integer state, Integer type, Integer orderState, String orderNo, String startTime, String endTime, PrintWriter out) { if (uid == null) { JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("uid不能为空")); return; } if (pageIndex == null || pageIndex < 1) { pageIndex = 1; } if (pageSize == null) pageSize = Constant.PAGE_SIZE; if (state != null && state == 0) { state = null;// 所有状态 } if (type != null && type == 0) { type = null; // 所有类型订单 } if (endTime != null && endTime.trim().length() > 0) { endTime += " 23:59:59"; } try { long count = 0; // 查询列表 List list = commonOrderService.listQueryByUid((pageIndex - 1) * pageSize, pageSize, uid, state, type, orderState, orderNo, startTime, endTime, null); if (list != null && list.size() > 0) { // 统计总数 count = commonOrderService.countQueryByUid(uid, state, type, orderState, orderNo, startTime, endTime, null); } int totalPage = (int) (count % pageSize == 0 ? count / pageSize : count / pageSize + 1); PageEntity pe = new PageEntity(pageIndex, pageSize, count, totalPage); JSONObject data = new JSONObject(); data.put("pe", pe); data.put("result_list", list); JsonUtil.printMode(out, callback, JsonUtil.loadTrueResult(data)); } catch (CommonOrderException e) { JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult(e.getMsg())); } catch (Exception e) { JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("查询失败")); e.printStackTrace(); } } /** * 当前未收货订单数 * * @param callback * @param type * 订单状态-暂未使用 * @param out */ @RequestMapping(value = "getHistoryUnreceived") public void getHistoryUnreceived(String callback, Integer type, PrintWriter out) { try { Long total = commonOrderCountService.countByState(1); if (total == null) { total = 0L; } JSONObject data = new JSONObject(); data.put("total", total); JsonUtil.printMode(out, callback, JsonUtil.loadTrueResult(data)); } catch (Exception e) { JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("操作异常")); e.printStackTrace(); } } @RequestMapping(value = "uploadTaoBaoWeiQuanOrder") public void uploadWeiQuanOrder(String callback, HttpServletRequest request, PrintWriter out) { try { // 1. 先判断httpRequest 是否含有文件类型 if (request instanceof MultipartHttpServletRequest) { MultipartHttpServletRequest fileRequest = (MultipartHttpServletRequest) request; MultipartFile file = fileRequest.getFile("file"); List orderList = TaoBaoOrderUtil.getWeiQuanOrders(file.getInputStream()); for (TaoBaoWeiQuanOrder order : orderList) taoBaoWeiQuanOrderService.addWeiQuanOrder(order);// 添加维权订单到数据库 // 处理维权订单 orderProcessService.weiQuanOrder(orderList); } JsonUtil.printMode(out, callback, JsonUtil.loadTrueResult("更新成功")); } catch (Exception e) { JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("更新失败")); } } @RequestMapping(value = "countOrderInfo") public void countOrderInfo(String callback, Long uid, PrintWriter out) { // 自购已到账 long selfNumEnd = hongBaoV2CountService.countMyDirectOrderByCashArrival(uid, Constant.VIP_ORDER_PAY, 1); // 分享已到账 long shareNumEnd = hongBaoV2CountService.countMyDirectOrderByCashArrival(uid, Constant.VIP_ORDER_PAY, 2); // 自购未到账 long selfNum = hongBaoV2CountService.countMyDirectOrderByCashNotArrival(uid, Constant.VIP_ORDER_PAY, 1); // 分享未到账 long shareNum = hongBaoV2CountService.countMyDirectOrderByCashNotArrival(uid, Constant.VIP_ORDER_PAY, 2); JSONObject data = new JSONObject(); data.put("selfNumEnd", selfNumEnd); data.put("shareNumEnd", shareNumEnd); data.put("selfNum", selfNum); data.put("shareNum", shareNum); JsonUtil.printMode(out, callback, JsonUtil.loadTrueResult(data)); } }