| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530 |
- package com.cyksj.web.controller.manage;
- import cn.hutool.core.collection.CollUtil;
- import cn.hutool.core.date.DateTime;
- import cn.hutool.core.util.ObjectUtil;
- import cn.hutool.core.util.StrUtil;
- import com.alibaba.excel.support.ExcelTypeEnum;
- import com.baomidou.mybatisplus.core.toolkit.Wrappers;
- import com.cyksj.common.annotation.Log;
- import com.cyksj.common.annotation.NoSubmit;
- import com.cyksj.common.constant.Constant;
- import com.cyksj.common.exception.BusinessRuntimeException;
- import com.cyksj.dto.Result;
- import com.cyksj.enums.BusinessType;
- import com.cyksj.enums.GatewayApiCode;
- import com.cyksj.enums.GatewayResponse;
- import com.cyksj.mapper.*;
- import com.cyksj.mapper.channel.ChannelPopularizeMapper;
- import com.cyksj.mapper.manage.cms.CmsUserMapper;
- import com.cyksj.mapper.manage.coupon.CouponMapper;
- import com.cyksj.model.entity.*;
- import com.cyksj.model.excel.ExcelOrderDonData;
- import com.cyksj.model.manage.views.AccountView;
- import com.cyksj.model.manage.views.OrderDonBackView;
- import com.cyksj.model.request.ChangeRelationReq;
- import com.cyksj.model.request.ClickOutReq;
- import com.cyksj.model.request.OrderRefundReq;
- import com.cyksj.model.views.*;
- import com.cyksj.service.mange.CmsOrderDonService;
- import com.cyksj.service.order.OrderDonCommentService;
- import com.cyksj.service.user.UserBindRelationService;
- import com.cyksj.service.user.UserService;
- import com.cyksj.web.util.EasyExcelUtils;
- import com.cyksj.web.util.StpAbroadUtil;
- import com.ejlchina.searcher.BeanSearcher;
- import com.ejlchina.searcher.SearchResult;
- import com.ejlchina.searcher.param.Operator;
- import com.ejlchina.searcher.util.MapBuilder;
- import com.ejlchina.searcher.util.MapUtils;
- import lombok.RequiredArgsConstructor;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.dao.DuplicateKeyException;
- import org.springframework.validation.annotation.Validated;
- import org.springframework.web.bind.annotation.*;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.math.BigDecimal;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.List;
- import java.util.Optional;
- import java.util.stream.Collectors;
- /**
- * @author chan
- * @date 2021/10/12 下午11:05
- */
- @Slf4j
- @RequestMapping("/manage/order")
- @RestController
- @RequiredArgsConstructor
- public class CmsOrderController {
- private final HttpServletRequest request;
- private final CmsOrderDonService cmsOrderDonService;
- private final UserService userService;
- private final BeanSearcher beanSearcher;
- private final CouponMapper couponMapper;
- private final OrderCommentMapper orderCommentMapper;
- private final OrderDonCommentService orderDonCommentService;
- private final GoodsDonSkuMapper skuMapper;
- private final ChannelPopularizeMapper channelPopularizeMapper;
- private final GroupsRelationMapper relationMapper;
- private final CmsUserMapper cmsUserMapper;
- private final UserMapper userMapper;
- private final OrderDonMapper orderDonMapper;
- private final UserBindRelationService userBindRelationService;
- private final OrderDonCustomerServiceRelateMapper orderDonCustomerServiceRelateMapper;
- @GetMapping("/get")
- public Result<SearchResult<OrderDonBackView>> get(Long channelId, String account, String showId, Long acId, String nickname, String register, Boolean isGoogle, Boolean isSubsidy, Boolean isRenewPackage) {
- MapBuilder builder = MapUtils.flatBuilder(request.getParameterMap());
- if (channelId != null) {
- List<Long> popularizeIds = channelPopularizeMapper.getPopularizeIdByChannelId(channelId);
- builder.field(OrderDonBackView::getPopularizeId, popularizeIds).op(Operator.InList);
- }
- if (StrUtil.isNotBlank(account)) {
- GroupsRelation relation = relationMapper.selectOne(Wrappers.lambdaQuery(GroupsRelation.class)
- .eq(GroupsRelation::getAccount, account).last("limit 1"));
- if (relation != null) {
- builder.field(OrderDonBackView::getRelationId, relation.getId());
- } else {
- builder.field(OrderDonBackView::getRelationId).op(Operator.IsNull);
- }
- }
- if (StrUtil.isNotBlank(showId)) {
- builder.field(OrderDonBackView::getUserId, userService.getUserIdByShowId(showId));
- }
- String conditionSql = StrUtil.EMPTY;
- if (acId != null) {
- conditionSql += String.format(" o.user_id in (select user_id from corp_user where ac_id = %s)", acId);
- }
- if (!ObjectUtil.isAllEmpty(nickname, register, isGoogle)) {
- if (StrUtil.isNotEmpty(conditionSql)) {
- conditionSql += " and ";
- }
- conditionSql += " exists (select u.id from user u where u.id = o.user_id";
- if (StrUtil.isNotEmpty(nickname)) {
- conditionSql += String.format(" and u.nickname like '%s%%'", nickname);
- }
- if (StrUtil.isNotEmpty(register)) {
- if ("phone".equals(register)) {
- conditionSql += " and u.login_phone != ''";
- } else if ("email".equals(register)) {
- conditionSql += " and u.email != ''";
- } else if ("wechat".equals(register)) {
- conditionSql += " and u.unionid != ''";
- }
- }
- if (isGoogle != null && isGoogle) {
- conditionSql += String.format(" and u.is_google is %s", isGoogle);
- }
- conditionSql += " limit 1)";
- }
- if (isSubsidy != null) {
- if (StrUtil.isNotEmpty(conditionSql)) {
- conditionSql += " and ";
- }
- conditionSql += " exists (select 1 from order_don_subsidy_relate ocr where ocr.order_id = o.id limit 1)";
- }
- if (isRenewPackage != null) {
- if (StrUtil.isNotEmpty(conditionSql)) {
- conditionSql += " and ";
- }
- conditionSql += " exists (select 1 from order_don_renew_record odrr1 where odrr1.order_id = o.id limit 1)";
- }
- if (StrUtil.isNotBlank(conditionSql)) {
- builder.put("condition", conditionSql);
- }
- SearchResult<OrderDonBackView> search = beanSearcher.search(OrderDonBackView.class, builder.build());
- search.getDataList().forEach(data -> {
- if (data.getPopularizeId() != null) {
- Optional.ofNullable(channelPopularizeMapper.selectChannelByPid(data.getPopularizeId()))
- .ifPresent(channelName -> data.setChannel(channelName));
- }
- if (data.getCouponId() != null) {
- Optional.ofNullable(couponMapper.selectById(data.getCouponId()))
- .ifPresent(coupon -> data.setCouponName(coupon.getName()));
- data.setCouponMoney(data.getCouponMoney() == null ? BigDecimal.ZERO : data.getCouponMoney());
- }
- if (data.getCouponUserId() != 0) {
- data.setCouponName(couponMapper.getCouponNameByCouponUserId(data.getCouponUserId()));
- data.setCouponMoney(data.getCouponMoney() == null ? BigDecimal.ZERO : data.getCouponMoney());
- }
- if (data.getRelationId() != 0) {
- Optional.ofNullable(relationMapper.getGroupRelationAccountView(data.getRelationId()))
- .ifPresent(e -> {
- data.setGtAccount(e.getTripsAccount());
- data.setAccount(e.getAccount());
- });
- }
- User user = userMapper.selectById(data.getUserId());
- if (user != null) {
- data.setNickname(user.getNickname());
- data.setLoginPhone(user.getLoginPhone());
- data.setEmail(user.getEmail());
- data.setRegister(StrUtil.isNotEmpty(user.getUnionid()) ? "wechat" : StrUtil.isNotEmpty(user.getLoginPhone()) ? "phone" : "email");
- }
- if (data.getGoodsDonType() != null && data.getGoodsDonType() == 1) {
- //获取订单关联客服
- Optional.ofNullable(orderDonMapper.selectOrderRelate(data.getId())).ifPresent(e -> {
- data.setCmsUserId(e.getCmsUserId());
- data.setNickname(e.getNickname());
- });
- }
- });
- return GatewayResponse.SUCCESS.newBuilder().toResult(search);
- }
- /**
- * 用户退款订单列表
- */
- @GetMapping("/get/refund/list")
- public Result<SearchResult<RefundOrderDonView>> getRefundOrderDonView() {
- SearchResult<RefundOrderDonView> search = beanSearcher.search(RefundOrderDonView.class, MapUtils.flatBuilder(request.getParameterMap())
- .build());
- search.getDataList().forEach(data -> {
- Optional.ofNullable(userService.getById(data.getUserId()))
- .ifPresent(user -> data.setNickname(user.getNickname()));
- });
- return GatewayResponse.SUCCESS.newBuilder().toResult(search);
- }
- /**
- * 订单导出
- */
- @GetMapping("/export")
- @Log(module = "订单管理/导出订单", businessType = BusinessType.EXPORT, isSaveRequestData = true)
- public void exportOrders(HttpServletResponse response) {
- List<ExcelOrderDonData> list = beanSearcher.searchAll(ExcelOrderDonData.class, MapUtils.flatBuilder(request.getParameterMap()).build());
- list.forEach(data -> {
- data.setMoney(data.getMoney().divide(BigDecimal.valueOf(100)));
- OrderDon.Type type = data.getType();
- data.setTypeStr(type != null ? type.getDesc() : "");
- OrderDon.Status status = data.getStatus();
- data.setStatusStr(status != null ? status.getDesc() : "");
- if (data.getChannelId() == null) {
- data.setChannel("自然流量渠道");
- }
- if (data.getCouponUserId() != 0) {
- data.setCouponName(couponMapper.getCouponNameByCouponUserId(data.getCouponUserId()));
- data.setCouponMoney(data.getCouponMoney() == null ? BigDecimal.ZERO : data.getCouponMoney().divide(BigDecimal.valueOf(100)));
- }
- });
- EasyExcelUtils.createExcelStreamMutilByEasyExcel(response, ExcelOrderDonData.class, list, "订单列表", "订单列表", ExcelTypeEnum.XLSX, null);
- }
- @PutMapping("/update/status")
- public Result<String> upStatus(@RequestBody OrderDon orderDon){
- cmsOrderDonService.update(Wrappers.lambdaUpdate(OrderDon.class).eq(OrderDon::getId,orderDon.getId()).set(OrderDon::getStatus,orderDon.getStatus()));
- return GatewayResponse.SUCCESS.newBuilder().toResult();
- }
- /**
- * 修改订单状态
- */
- @PutMapping("/put/status/{orderId}")
- public Result<String> updateOrderStatus(@PathVariable Long orderId) {
- OrderDon orderDon = cmsOrderDonService.getById(orderId);
- if (orderDon == null) {
- throw BusinessRuntimeException.getInstance("该订单不存在");
- }
- if (orderDon.getStatus() == OrderDon.Status.complete) {
- throw BusinessRuntimeException.getInstance("该订单已是已完成状态");
- }
- if (orderDon.getStatus() != OrderDon.Status.hasPayment) {
- throw BusinessRuntimeException.getInstance("该订单不是未支付状态");
- }
- orderDon.setStatus(OrderDon.Status.complete);
- cmsOrderDonService.updateById(orderDon);
- return GatewayResponse.SUCCESS.newBuilder().toResult("修改订单状态成功");
- }
- /**
- * 新增平台评论
- */
- @PostMapping("/post/orderComment")
- public Result<String> insertOrderComment(@RequestBody @Validated OrderComment orderComment) {
- if (StrUtil.isEmpty(orderComment.getHeadImgUrl())) {
- orderComment.setHeadImgUrl(Constant.DEFAULT_HEAD_IMG);
- }
- if (orderComment.getCommentTime() == null) {
- orderComment.setCommentTime(DateTime.now().toDateStr());
- }
- orderComment.setVerifyStatus(OrderComment.VerifyStatus.success);
- orderCommentMapper.insert(orderComment);
- return GatewayResponse.SUCCESS.newBuilder().toResult("新增成功");
- }
- /**
- * 编辑平台评论
- */
- @PutMapping("/put/orderComment")
- public Result<String> updateOrderComment(@RequestBody @Validated OrderComment orderComment) {
- if (orderComment.getCommentTime() == null) {
- orderComment.setCommentTime(DateTime.now().toDateStr());
- }
- if (orderComment.getVerifyStatus() == null) {
- orderComment.setVerifyStatus(OrderComment.VerifyStatus.success);
- }
- orderCommentMapper.updateById(orderComment);
- return GatewayResponse.SUCCESS.newBuilder().toResult("编辑成功");
- }
- /**
- * 删除平台评论
- */
- @DeleteMapping("/delete/orderComment/{id}")
- public Result<String> deleteOrderCommentById(@PathVariable Long id) {
- orderCommentMapper.deleteById(id);
- return GatewayResponse.SUCCESS.newBuilder().toResult("删除成功");
- }
- /**
- * 平台评论列表
- */
- @GetMapping("/get/orderComment")
- public Result<SearchResult<OrderCommentView>> getOrderCommentView() {
- SearchResult<OrderCommentView> search = beanSearcher.search(OrderCommentView.class, MapUtils.flatBuilder(request.getParameterMap()).build());
- return GatewayResponse.SUCCESS.newBuilder().toResult(search);
- }
- /**
- * 配置订单平台评价列表
- */
- @PostMapping("/post/orderComment/reward/config")
- public Result<String> saveOrUpdateOrderCommentConfig(@RequestBody List<OrderCommentRewardConfig> configs) {
- orderDonCommentService.saveOrUpdateOrderCommentConfig(configs);
- return GatewayResponse.SUCCESS.newBuilder().toResult();
- }
- /**
- * 获取配置 订单平台评价
- */
- @GetMapping("/get/orderComment/reward/config")
- public Result<List<OrderCommentRewardConfigView>> getOrderCommentRewardConfig() {
- List<OrderCommentRewardConfigView> views = beanSearcher.searchAll(OrderCommentRewardConfigView.class, MapUtils.builder()
- .orderBy(OrderCommentRewardConfigView::getGoodsId).desc()
- .build());
- return GatewayResponse.SUCCESS.newBuilder().toResult(views);
- }
- /**
- * 踢出车队
- */
- @PutMapping("/kickOut")
- @NoSubmit
- public Result<String> kickOut(@RequestBody @Validated ClickOutReq req) {
- long adminId = StpAbroadUtil.getLoginIdAsLong();
- cmsOrderDonService.kickOutRelationId(adminId, req);
- return GatewayResponse.SUCCESS.newBuilder().toResult();
- }
- /**
- * 踢出车队列表
- */
- @GetMapping("/get/kickOut/list")
- public Result<SearchResult<UserKickOutView>> getKickOutList() {
- SearchResult<UserKickOutView> search = beanSearcher.search(UserKickOutView.class, MapUtils.flatBuilder(request.getParameterMap()).build());
- return GatewayResponse.SUCCESS.newBuilder().toResult(search);
- }
- /**
- * 订单统一退款
- */
- @PostMapping("/unified/refund")
- @Log(module = "订单管理/统一退款", businessType = BusinessType.PUT, isSaveRequestData = true)
- public Result<String> unifiedRefund(@RequestBody @Validated OrderRefundReq refundReq) throws Exception {
- long adminId = StpAbroadUtil.getLoginIdAsLong();
- CmsUser cmsUser = cmsUserMapper.selectById(adminId);
- if (cmsUser == null) {
- throw BusinessRuntimeException.getGatewayApiCode(GatewayApiCode.CMS_TOKEN_VALID);
- }
- refundReq.setOperator(cmsUser.getNickname());
- cmsOrderDonService.unifiedRefund(refundReq);
- return GatewayResponse.SUCCESS.newBuilder().toResult("退款成功");
- }
- /**
- * 更换用户车票
- */
- @PutMapping("/put/sameSpec/empty/relation")
- @Log(module = "更换用户车票", businessType = BusinessType.PUT, isSaveRequestData = true)
- public Result<String> changeRelation(@RequestBody @Validated ChangeRelationReq req) throws Exception {
- cmsOrderDonService.changeRelation(req);
- return GatewayResponse.SUCCESS.newBuilder().toResult();
- }
- /**
- * 多余车位的空账号
- */
- @GetMapping("/get/sameSpec/empty/relation")
- public Result<SearchResult<EmptyGroupsRelationView>> getSameSpecEmptyRelation(Long skuId, Long groupsId, Long relationId, Boolean isNormal) {
- MapBuilder builder = MapUtils.flatBuilder(request.getParameterMap())
- .field(EmptyGroupsRelationView::getStatus, GroupsTrips.Status.down.name()).op(Operator.NotEqual)
- .field(EmptyGroupsRelationView::getSkuId, skuId).orderBy(EmptyGroupsRelationView::getAvailableNum)
- .desc();
- GoodsDonSku sku = skuMapper.selectById(skuId);
- Date expiryTime = null;
- if (relationId != null) {
- GroupsRelation relation = relationMapper.selectById(relationId);
- if (relation != null) expiryTime = relation.getExpiryTime();
- }
- if (groupsId != null) {
- builder.field(EmptyGroupsRelationView::getGroupsId, groupsId).op(Operator.NotEqual);
- }
- String condition = StrUtil.EMPTY;
- if (isNormal != null && isNormal) {
- condition += String.format("gt.account_id not in (select account_id from work_order where sku_id = %s and account_id is not null)", skuId);
- }
- if (StrUtil.isNotEmpty(condition)) {
- condition += " and ";
- }
- Integer maxNum = sku.getNum();
- Integer extraNum = 5;
- //GPT 4人月付
- if (sku.getId() == 178l) {
- extraNum = 2;
- }
- maxNum += extraNum;
- condition += String.format("(available_parking_num = 0 or available_parking_num > (select count(*) from groups_relation gr where gr.groups_id = gt.id and gr.user_id != 0 and gr.num <= %s))", maxNum);
- if (StrUtil.isNotEmpty(condition)) {
- builder.put("condition", condition);
- }
- if (expiryTime != null) {
- builder.field(AccountView::getExpiryTime, expiryTime).op(Operator.GreaterEqual);
- builder.orderBy(AccountView::getExpiryTime).asc();
- }
- if (sku.getNum() == 1 || !Constant.AI_goodsIds.contains(sku.getGoodsId())) {
- builder.field(EmptyGroupsRelationView::getAvailableNum, 0).op(Operator.GreaterThan);
- }
- SearchResult<EmptyGroupsRelationView> search = beanSearcher.search(EmptyGroupsRelationView.class,
- builder.build());
- return GatewayResponse.SUCCESS.newBuilder().toResult(search);
- }
- /**
- * 客服退款每月数据
- */
- @GetMapping("/get/refundOrder/data")
- public Result<SearchResult<RefundOrderDonServiceDataView>> getRefundOrderData(String startTime, String endTime, String operator) {
- String time = StrUtil.EMPTY;
- if (StrUtil.isNotBlank(startTime)) {
- time += String.format(" and o.created_time >= '%s'", startTime);
- }
- if (StrUtil.isNotBlank(endTime)) {
- time += String.format(" and o.created_time < '%s'", endTime);
- }
- MapBuilder builder = MapUtils.flatBuilder(request.getParameterMap());
- if (StrUtil.isNotBlank(time)) {
- builder.put("time", time);
- }
- if (StrUtil.isEmpty(operator)) {
- List<CmsUserVO> userVOS = beanSearcher.searchAll(CmsUserVO.class, MapUtils.builder().field(CmsUserVO::getRoleNames, "客服").op(Operator.Contain).build());
- List<String> collect = userVOS.stream().map(CmsUserVO::getNickname).collect(Collectors.toList());
- if (CollUtil.isNotEmpty(collect)) {
- builder.field(RefundOrderDonServiceDataView::getOperator, collect).op(Operator.InList);
- }
- }
- SearchResult<RefundOrderDonServiceDataView> search = beanSearcher.search(RefundOrderDonServiceDataView.class, builder.build());
- search.getDataList().forEach(e->{
- String goodsName = orderDonMapper.selectMaxRefundGoodsName(e.getOperator(), startTime, endTime);
- e.setGoodsName(goodsName);
- });
- return GatewayResponse.SUCCESS.newBuilder().toResult(search);
- }
- /**
- * 设置订单客服
- */
- @PutMapping("/set/service")
- public Result<String> setOrderCustomerService(@RequestBody OrderDonCustomerServiceRelate relate) {
- OrderDonCustomerServiceRelate orderDonCustomerServiceRelate = orderDonCustomerServiceRelateMapper.selectOne(Wrappers.lambdaQuery(OrderDonCustomerServiceRelate.class)
- .eq(OrderDonCustomerServiceRelate::getOrderId, relate.getOrderId()).last("limit 1"));
- if (orderDonCustomerServiceRelate != null) {
- relate.setId(orderDonCustomerServiceRelate.getId());
- }
- try {
- if (relate.getId() == null) {
- orderDonCustomerServiceRelateMapper.insert(relate);
- } else {
- orderDonCustomerServiceRelateMapper.updateById(relate);
- }
- } catch (DuplicateKeyException e) {
- }
- return GatewayResponse.SUCCESS.newBuilder().toResult();
- }
- /**
- * 用户退款详情
- */
- @GetMapping("/get/user/refund/detail")
- public Result<OrderDonUserRefundDetailView> getUserRefundDetail(Long userId) {
- if (userId == null || userId == 0) {
- return GatewayResponse.SUCCESS.newBuilder().toResult();
- }
- List<Long> userIdList = null;
- try {
- userIdList = userBindRelationService.getRelationUserIdList(userId, null);
- } catch (Exception e) {
- }
- String userIds = null;
- if (CollUtil.isNotEmpty(userIdList)) {
- userIds = userIdList.toString().replaceAll("\\[", "").replaceAll("]", "");
- }
- MapBuilder builder = MapUtils.builder();
- if (StrUtil.isNotBlank(userIds)) {
- builder.put("uid", String.format(" and user_id in (%s)", userIds));
- } else {
- builder.put("uid", String.format(" and user_id = %s", userId));
- }
- OrderDonUserRefundDetailView orderDonUserRefundDetailView = beanSearcher.searchFirst(OrderDonUserRefundDetailView.class, builder.build());
- return GatewayResponse.SUCCESS.newBuilder().toResult(orderDonUserRefundDetailView);
- }
- /**
- * 用户订单列表
- */
- @GetMapping("/get/user/orders")
- public Result<SearchResult<OrderDonUserRefundDetailView.OrderDonDetailView>> getUserOrdersDetail(Long userId) {
- if (userId == null || userId == 0) {
- return GatewayResponse.SUCCESS.newBuilder().toResult();
- }
- List<Long> userIdList = null;
- try {
- userIdList = userBindRelationService.getRelationUserIdList(userId, null);
- } catch (Exception e) {
- }
- if (userIdList == null) {
- userIdList = new ArrayList<>();
- userIdList.add(userId);
- }
- SearchResult<OrderDonUserRefundDetailView.OrderDonDetailView> search = beanSearcher.search(OrderDonUserRefundDetailView.OrderDonDetailView.class, MapUtils.flatBuilder(request.getParameterMap())
- .field(OrderDonUserRefundDetailView.OrderDonDetailView::getUserId, userIdList).op(Operator.InList)
- .build());
- return GatewayResponse.SUCCESS.newBuilder().toResult(search);
- }
- }
|