MallOrderController.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. package com.tzy.controller.mall;
  2. import com.tzy.annotation.ApiLimitRule;
  3. import com.tzy.annotation.ApiLog;
  4. import com.tzy.common.config.handle.ApiVersion;
  5. import com.tzy.common.constant.Constants;
  6. import com.tzy.common.constant.MqConstans;
  7. import com.tzy.common.core.controller.BaseController;
  8. import com.tzy.common.dto.InDto;
  9. import com.tzy.common.dto.OutDTO;
  10. import com.tzy.common.enums.BusinessType;
  11. import com.tzy.common.exception.ServiceException;
  12. import com.tzy.common.utils.DateUtils;
  13. import com.tzy.common.utils.bean.JSONTools;
  14. import com.tzy.dto.EvaluationDTO;
  15. import com.tzy.dto.OrderReq;
  16. import com.tzy.framework.util.RedisUtils;
  17. import com.tzy.framework.web.domain.LimitRule;
  18. import com.tzy.member.benefits.mapper.MemberBenefitsRecordMapper;
  19. import com.tzy.pojo.item.EvaluationRecord;
  20. import com.tzy.pojo.item.OrderList;
  21. import com.tzy.pojo.item.Sku;
  22. import com.tzy.service.MallOrderService;
  23. import com.tzy.sportcard.api.domain.AppUserInfoDto;
  24. import com.tzy.sportcard.api.service.AsyncAppService;
  25. import com.tzy.sportcard.api.service.MineApiService;
  26. import com.tzy.sportcard.group.domain.CardGroupOrderInfo;
  27. import com.tzy.sportcard.group.service.ICardGroupOrderInfoService;
  28. import io.swagger.annotations.Api;
  29. import io.swagger.annotations.ApiOperation;
  30. import lombok.extern.slf4j.Slf4j;
  31. import org.apache.logging.log4j.util.Strings;
  32. import org.springframework.amqp.rabbit.core.RabbitTemplate;
  33. import org.springframework.beans.factory.annotation.Autowired;
  34. import org.springframework.context.annotation.Lazy;
  35. import org.springframework.web.bind.annotation.*;
  36. import org.teasoft.bee.osql.Condition;
  37. import org.teasoft.bee.osql.FunctionType;
  38. import org.teasoft.bee.osql.Op;
  39. import org.teasoft.bee.osql.SuidRich;
  40. import org.teasoft.honey.osql.core.ConditionImpl;
  41. import javax.annotation.Resource;
  42. import java.util.List;
  43. import java.util.Objects;
  44. /**
  45. * @author by po'yi
  46. * @Classname MallOrderController
  47. * @Description 订单
  48. * @Date 2022/3/18 11:56
  49. */
  50. @Slf4j
  51. @Api("订单")
  52. @RestController
  53. @RequestMapping("/api/{version}/order")
  54. public class MallOrderController extends BaseController {
  55. @Autowired
  56. private MineApiService mineApiService;
  57. @Autowired
  58. private MallOrderService mallOrderService;
  59. @Autowired
  60. private ICardGroupOrderInfoService cardGroupOrderInfoService;
  61. @Autowired
  62. private SuidRich suidRich;
  63. @Autowired
  64. private RedisUtils redisUtils;
  65. @Autowired
  66. @Lazy
  67. private AsyncAppService asyncAppService;
  68. @Resource
  69. private MemberBenefitsRecordMapper memberBenefitsRecordMapper;
  70. @Resource
  71. private RabbitTemplate rabbitTemplate;
  72. @ApiLog(title = "提交订单", businessType = BusinessType.SEARCH)
  73. @ResponseBody
  74. @ApiVersion(1.0)
  75. @ApiOperation("提交订单")
  76. @ApiLimitRule(seconds = 2,limitCount=10,type= LimitRule.ACT,msg="抱歉,抢购人数太多,请稍后再试!")
  77. @PostMapping("/submit")
  78. public OutDTO submitOrder(@RequestBody OrderReq inDto ){
  79. return OutDTO.error500("请更新app最新版本");
  80. }
  81. @ApiLog(title = "预支付2", businessType = BusinessType.SEARCH)
  82. @ResponseBody
  83. @ApiVersion(1.0)
  84. @ApiOperation("预支付2")
  85. @PostMapping("/prePayNew")
  86. public OutDTO orderPrePayNew(@RequestBody InDto inDto ){
  87. return OutDTO.error500("请先升级app");
  88. }
  89. @ApiLog(title = "积分兑换", businessType = BusinessType.SEARCH)
  90. @ResponseBody
  91. @ApiVersion(1.0)
  92. @ApiOperation("积分兑换 ")
  93. @ApiLimitRule(seconds = 1,limitCount=1,type= LimitRule.USER,msg="操作拼豆,请稍后再试!")
  94. @PostMapping("/buyOfPoint")
  95. public OutDTO buyOfPoint(@RequestBody InDto inDto ){
  96. return OutDTO.error500("请更新app最新版本");
  97. }
  98. @ApiLog(title = "积分兑换优惠劵", businessType = BusinessType.SEARCH)
  99. @ResponseBody
  100. @ApiVersion(1.0)
  101. @ApiOperation("积分兑换优惠劵,根据spuId兑换")
  102. @ApiLimitRule(seconds = 2,limitCount=10,type= LimitRule.ACT_TOKEN,msg="抱歉,抢购人数太多,请稍后再试!")
  103. @PostMapping("/exchangeCouponByPoint")
  104. public OutDTO exchangeCouponByPoint(@RequestBody InDto inDto ){
  105. Object spuId = inDto.get("spuId");
  106. if(spuId==null){
  107. return OutDTO.error500("参数skuId为空");
  108. }
  109. Integer skuId=getCouponSkuId(Long.valueOf(spuId.toString()));
  110. Integer purchaseCount=1;
  111. if(inDto.get("purchaseCount")!=null){
  112. //购买数量
  113. purchaseCount = (Integer) inDto.get("purchaseCount");
  114. if(purchaseCount<1 ){
  115. return OutDTO.error500("请选择兑换数量");
  116. }
  117. }
  118. AppUserInfoDto userInfo = mineApiService.checkUserNew(inDto);
  119. return mallOrderService.buyOfPoint(userInfo.getId(),skuId,inDto.get("addressId"),purchaseCount);
  120. }
  121. private Integer getCouponSkuId(Long spuId){
  122. Object o = redisUtils.get(Constants.SKU_COUPON_ID_CACHE + spuId);
  123. Integer skuId;
  124. if(o==null){
  125. Sku sku = new Sku();
  126. sku.setSpuId(spuId);
  127. sku.setStatus(201L);
  128. List<Sku> skus = suidRich.select(sku);
  129. if(skus.isEmpty()||skus.size()!=1){
  130. throw new ServiceException(500, "优惠劵数量异常,请联系管理员!");
  131. }
  132. skuId = skus.get(0).getId().intValue();
  133. redisUtils.set(Constants.SKU_COUPON_ID_CACHE + spuId,skuId,Constants.ONE_HOUR);
  134. }else{
  135. skuId=(Integer)o;
  136. }
  137. return skuId;
  138. }
  139. @ApiLog(title = "取消订单",businessType = BusinessType.UPDATE,indto = "{{inDto}}")
  140. @ApiOperation("取消订单")
  141. @ResponseBody
  142. @PostMapping(value = "/cancelOrder")
  143. public OutDTO cancelOrder(@RequestBody InDto inDto){
  144. mineApiService.checkUserNew(inDto);
  145. Integer orderId = (Integer) inDto.get("orderId");
  146. CardGroupOrderInfo cardGroupOrderInfo = cardGroupOrderInfoService.selectCardGroupOrderInfoById(orderId.longValue());
  147. if(null == cardGroupOrderInfo ){
  148. return OutDTO.error500("未找到订单信息");
  149. }
  150. //判断是否符合取消订单的条件
  151. Long status = cardGroupOrderInfo.getStatus();
  152. if(status != 100){
  153. return OutDTO.error500("不符合取消订单的条件!");
  154. }
  155. // 唤起支付前做一个查询
  156. if(cardGroupOrderInfoService.checkOrderIsPay(orderId.longValue())){
  157. return OutDTO.error500("订单支付中,无法取消!");
  158. }
  159. // 抢拍订单不允许取消
  160. String orderSubType = cardGroupOrderInfo.getOrderSubType();
  161. if(Strings.isNotEmpty(orderSubType) && Constants.AUCTION_ORDER_COMMON_AMOUNT.equals(orderSubType)){
  162. return OutDTO.error500("抢拍订单不能取消!");
  163. }
  164. //用户取消
  165. cardGroupOrderInfo.setStatus(202L);
  166. // 释放库存
  167. if (Constants.ORDER_TYPE_SHOP.equals(cardGroupOrderInfo.getOrderType())) {
  168. mallOrderService.cancelOrder(cardGroupOrderInfo);
  169. // 如果是权益兑换订单 删除权益兑换记录
  170. String prop3 = cardGroupOrderInfo.getProp3();
  171. if (Constants.MEMBER_BENEFITS.equals(cardGroupOrderInfo.getOrderSubType()) && Strings.isNotEmpty(prop3)) {
  172. // 根据权益兑换记录id删除权益兑换记录
  173. memberBenefitsRecordMapper.deleteMemberBenefitsRecordById(Long.parseLong(prop3));
  174. }
  175. if (cardGroupOrderInfo.getGiveOrderId() != null) {
  176. rabbitTemplate.convertAndSend(MqConstans.EXCHANGE_DELAYED_ORDER_EXPIRED, MqConstans.ROUTING_KEY_ORDER_EXPIRED, cardGroupOrderInfo.getGiveOrderId());
  177. }
  178. } else if (Constants.ORDER_TYPE_GIFT_CARD.equals(cardGroupOrderInfo.getOrderType())) {
  179. CardGroupOrderInfo updateOrder = new CardGroupOrderInfo();
  180. updateOrder.setId(orderId.longValue());
  181. updateOrder.setStatus(202L);
  182. cardGroupOrderInfoService.updateCardGroupOrderInfo(updateOrder);
  183. } else {
  184. cardGroupOrderInfoService.cancelOrder(cardGroupOrderInfo);
  185. }
  186. cardGroupOrderInfoService.returnCouponByOrderId(orderId.longValue());
  187. //回退积分
  188. mineApiService.addUserPoint(cardGroupOrderInfo, true, Constants.POINT_TYPE_COMMON_RETURN);
  189. return OutDTO.ok();
  190. }
  191. @ApiLog(title = "用户申请售后",businessType = BusinessType.UPDATE,indto = "{{inDto}}")
  192. @ApiOperation("用户申请售后,退款,换货")
  193. @ResponseBody
  194. @PostMapping(value = "/afterSale")
  195. public OutDTO refundOrder(@RequestBody InDto inDto){
  196. AppUserInfoDto userInfo = mineApiService.checkUserNew(inDto);
  197. Integer orderId = (Integer) inDto.get("orderId");
  198. CardGroupOrderInfo cardGroupOrderInfo = cardGroupOrderInfoService.getOrderByUserAndId(userInfo.getId(),orderId.longValue());
  199. if(null == cardGroupOrderInfo ){
  200. return OutDTO.error(500,"未找到订单信息");
  201. }
  202. if(cardGroupOrderInfo.getPoint()!=null&&cardGroupOrderInfo.getPoint()>0){
  203. return OutDTO.error500("积分订单无法申请,请联系客服!");
  204. }
  205. //判断是否符合取消订单的条件
  206. Long status = cardGroupOrderInfo.getStatus();
  207. if(!Constants.refundOrderStatus.contains(status.intValue())){
  208. return OutDTO.error500("订单不符合申请退款的条件");
  209. }
  210. // 抢拍订单不允许退款
  211. String orderSubType = cardGroupOrderInfo.getOrderSubType();
  212. if(Strings.isNotEmpty(orderSubType) && Constants.AUCTION_ORDER_COMMON_AMOUNT.equals(orderSubType)){
  213. return OutDTO.error500("抢拍订单不能退款!");
  214. }
  215. OrderList orderList = new OrderList();
  216. orderList.setOrderId(cardGroupOrderInfo.getId());
  217. Condition beeCondition = new ConditionImpl();
  218. beeCondition.op("liveId", Op.gt,0);
  219. String living = suidRich.selectWithFun(orderList, FunctionType.COUNT, "*", beeCondition);
  220. if(Integer.valueOf(living)>0){
  221. return OutDTO.error500("订单正在直播中,不符合申请退款的条件");
  222. }
  223. String refundType =inDto.getString("refundType");
  224. String refundReason =inDto.getString("refundReason");
  225. String refundDesc =inDto.getString("refundDesc");
  226. CardGroupOrderInfo updateOrder = new CardGroupOrderInfo();
  227. updateOrder.setId(cardGroupOrderInfo.getId());
  228. updateOrder.setUserRefundTime(DateUtils.getTimestampNow());
  229. updateOrder.setRefundType(refundType);
  230. updateOrder.setRefundReason(refundReason);
  231. updateOrder.setRefundDesc(refundDesc);
  232. updateOrder.setRefundStatus(1);
  233. cardGroupOrderInfoService.updateCardGroupOrderInfo(updateOrder);
  234. asyncAppService.sendMerchantRefundNotice(cardGroupOrderInfo);
  235. return OutDTO.ok();
  236. }
  237. @ApiLog(title = "用户评价订单",businessType = BusinessType.UPDATE,indto = "{{inDto}}")
  238. @ApiOperation("用户评价订单")
  239. @ResponseBody
  240. @PostMapping(value = "/evaluation")
  241. public OutDTO evaluateOrder(@RequestBody InDto inDto){
  242. AppUserInfoDto appUserInfoDto = mineApiService.checkUserNew(inDto);
  243. Integer orderId = (Integer) inDto.get("orderId");
  244. CardGroupOrderInfo cardGroupOrderInfo = cardGroupOrderInfoService.selectCardGroupOrderInfoById(orderId.longValue());
  245. if(null == cardGroupOrderInfo ){
  246. return OutDTO.error(10040,"未找到订单信息");
  247. }
  248. if(cardGroupOrderInfo.getStatus()!=301){
  249. return OutDTO.error(500,"订单未签收!");
  250. }
  251. //json
  252. String evaluation =inDto.getString("evaluation");
  253. CardGroupOrderInfo updateOrder = new CardGroupOrderInfo();
  254. updateOrder.setId(cardGroupOrderInfo.getId());
  255. updateOrder.setEvaluation(evaluation);
  256. cardGroupOrderInfoService.updateCardGroupOrderInfo(updateOrder);
  257. EvaluationDTO evaluationDTO = JSONTools.jsonStr2obj(evaluation, EvaluationDTO.class);
  258. EvaluationRecord evaluationRecord = new EvaluationRecord();
  259. evaluationRecord.setCreateUserId(appUserInfoDto.getId());
  260. evaluationRecord.setAvatar(appUserInfoDto.getAvatar());
  261. evaluationRecord.setNickName(appUserInfoDto.getNickname());
  262. evaluationRecord.setAnonymous(evaluationDTO.getAnonymous()?1:0);
  263. evaluationRecord.setContent(evaluationDTO.getContent());
  264. evaluationRecord.setImgUrl(evaluationDTO.getImgArrAll());
  265. evaluationRecord.setValue(evaluationDTO.getValue());
  266. evaluationRecord.setType("spu");
  267. List<OrderList> orderLists = mallOrderService.getOrderListByOrderId(cardGroupOrderInfo.getId());
  268. evaluationRecord.setRefId(orderLists.get(0).getSpuId());
  269. evaluationRecord.setCreateTime(DateUtils.getTimestampNow());
  270. suidRich.insert(evaluationRecord);
  271. return OutDTO.ok();
  272. }
  273. @ApiLog(title = "用户填写退换货物流",businessType = BusinessType.UPDATE,indto = "{{inDto}}")
  274. @ApiOperation("用户填写退换货物流")
  275. @ResponseBody
  276. @PostMapping(value = "/refundShip")
  277. public OutDTO refundShip(@RequestBody InDto inDto){
  278. mineApiService.checkUserNew(inDto);
  279. Integer orderId = (Integer) inDto.get("orderId");
  280. CardGroupOrderInfo cardGroupOrderInfo = cardGroupOrderInfoService.selectCardGroupOrderInfoById(orderId.longValue());
  281. if(null == cardGroupOrderInfo ){
  282. return OutDTO.error(10040,"未找到订单信息");
  283. }
  284. if(cardGroupOrderInfo.getRefundStatus()!=2){
  285. return OutDTO.error(500,"请等待商家同意!");
  286. }
  287. String refundCurierCompany =inDto.getString("refundCurierCompany");
  288. String refundCurierNum =inDto.getString("refundCurierNum");
  289. CardGroupOrderInfo updateOrder = new CardGroupOrderInfo();
  290. updateOrder.setId(cardGroupOrderInfo.getId());
  291. updateOrder.setRefundCurierCompany(refundCurierCompany);
  292. updateOrder.setRefundCourierNum(refundCurierNum);
  293. updateOrder.setRefundShipTime(DateUtils.getTimestampNow());
  294. updateOrder.setRefundStatus(4);
  295. cardGroupOrderInfoService.updateCardGroupOrderInfo(updateOrder);
  296. return OutDTO.ok();
  297. }
  298. @ApiLog(title = "售后客服介入或接受商家拒绝退款操作",businessType = BusinessType.UPDATE,indto = "{{inDto}}")
  299. @ApiOperation("售后客服介入或接受商家拒绝退款操作")
  300. @ResponseBody
  301. @PostMapping(value = "/saleCustomerServiceOrNot")
  302. public OutDTO saleCustomerService(@RequestBody InDto inDto) {
  303. mineApiService.checkUserNew(inDto);
  304. Integer orderId = (Integer) inDto.get("orderId");
  305. CardGroupOrderInfo cardGroupOrderInfo = cardGroupOrderInfoService.selectCardGroupOrderInfoById(orderId.longValue());
  306. if (null == cardGroupOrderInfo) {
  307. return OutDTO.error(10040, "未找到订单信息");
  308. }
  309. if (cardGroupOrderInfo.getRefundStatus() != 3) {
  310. return OutDTO.error(500, "请等待商家操作!");
  311. }
  312. CardGroupOrderInfo updateOrder = new CardGroupOrderInfo();
  313. updateOrder.setId(cardGroupOrderInfo.getId());
  314. if (inDto.get("refundStatus") == null) {
  315. updateOrder.setRefundStatus(6);
  316. } else {
  317. updateOrder.setRefundStatus((Integer) inDto.get("refundStatus"));
  318. }
  319. cardGroupOrderInfoService.updateCardGroupOrderInfo(updateOrder);
  320. return OutDTO.ok();
  321. }
  322. }