基于mall4j产品的二开项目后端
lee
2024-12-18 efdb99f8cecc4afb592afad79c761081d5d5cf22
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
 * Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.
 *
 * https://www.mall4j.com/
 *
 * 未经允许,不可做商业用途!
 *
 * 版权所有,侵权必究!
 */
package com.yami.shop.api.controller;
 
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yami.shop.bean.model.ShopBankCard;
import com.yami.shop.bean.model.ShopDetail;
import com.yami.shop.bean.model.ShopWithdrawCash;
import com.yami.shop.common.config.Constant;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.security.api.util.SecurityUtils;
import com.yami.shop.service.ShopBankCardService;
import com.yami.shop.service.ShopDetailService;
import com.yami.shop.service.ShopWithdrawCashService;
import com.yami.shop.sys.common.model.ShopEmployee;
import com.yami.shop.sys.common.service.ShopEmployeeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
 
import javax.validation.Valid;
import java.util.List;
import java.util.Objects;
 
 
/**
 * 商家提现申请信息
 *
 * @author YXF
 *  * @date 2020-04-07 14:22:08
 */
@RestController
@AllArgsConstructor
@RequestMapping("/p/shop/shopBankCard" )
@Tag(name = "银行卡接口")
public class ShopBankCardController {
 
    private final ShopBankCardService shopBankCardService;
    private final ShopWithdrawCashService shopWithdrawCashService;
    private final ShopDetailService shopDetailService;
    private final ShopEmployeeService shopEmployeeService;
 
    @GetMapping("/getShopBankCardList")
    @Operation(summary = "获取店铺下的银行卡列表" , description = "获取店铺下的银行卡列表")
    public ServerResponseEntity<List<ShopBankCard>> getShopBankCardList() {
        ShopDetail shopDetail = shopDetailService.getShopDetailByUserId(SecurityUtils.getUser().getUserId());
        if (Objects.isNull(shopDetail)){{
            return ServerResponseEntity.success();
        }}
        Long shopId = shopDetail.getShopId();
        List<ShopBankCard> list = shopBankCardService.list(new LambdaQueryWrapper<ShopBankCard>().eq(ShopBankCard::getShopId, shopId).eq(ShopBankCard::getStatus,1));
        return ServerResponseEntity.success(list);
    }
 
    @GetMapping
    @Operation(summary = "根据银行卡id获取银行卡信息" , description = "根据银行卡id获取银行卡信息")
    public ServerResponseEntity<ShopBankCard> getById(@RequestParam("shopBankCardId") Long shopBankCardId) {
        ShopDetail shopDetail = shopDetailService.getShopDetailByUserId(SecurityUtils.getUser().getUserId());
        if (Objects.isNull(shopDetail)){{
            return ServerResponseEntity.success();
        }}
        Long shopId = shopDetail.getShopId();
        ShopBankCard shopBankCard = shopBankCardService.getOne(Wrappers.lambdaQuery(ShopBankCard.class)
                .eq(ShopBankCard::getShopBankCardId, shopBankCardId)
                .eq(ShopBankCard::getShopId, shopId)
        );
        return ServerResponseEntity.success(shopBankCard);
    }
 
    @PutMapping
    @Operation(summary = "更新银行卡信息" , description = "更新银行卡信息")
    @PreAuthorize("@pms.hasPermission('shop:shopBankCard:update')")
    public ServerResponseEntity<Void> update(@RequestBody @Valid ShopBankCard shopBankCard) {
        shopBankCardService.updateByShopId(shopBankCard, shopDetailService.getShopDetailByUserId(SecurityUtils.getUser().getUserId()).getShopId());
        return ServerResponseEntity.success();
    }
 
    @PostMapping
    @Operation(summary = "添加单个银行卡信息" , description = "添加单个银行卡信息")
    @PreAuthorize("@pms.hasPermission('shop:shopBankCard:save')")
    public ServerResponseEntity<Long> save(@RequestBody @Valid ShopBankCard shopBankCard) {
        ShopDetail shopDetail = shopDetailService.getShopDetailByUserId(SecurityUtils.getUser().getUserId());
        if (Objects.isNull(shopDetail)){{
            return ServerResponseEntity.success();
        }}
        Long shopId = shopDetail.getShopId();
        shopBankCard.setShopId(shopId);
        int count = shopBankCardService.count(new LambdaQueryWrapper<ShopBankCard>().eq(ShopBankCard::getShopId, shopId).eq(ShopBankCard::getStatus, 1));
        if (count >= Constant.MAX_SHOP_BANK_CARD) {
            throw new YamiShopBindException("yami.shop.max.card.num");
        }
        return ServerResponseEntity.success(shopBankCardService.insertSelective(shopBankCard));
    }
 
    @PutMapping("/setDefault")
    @Operation(summary = "设置为默认银行卡" , description = "设置为默认银行卡")
    @PreAuthorize("@pms.hasPermission('shop:shopBankCard:default')")
    public ServerResponseEntity<Void> setDefault(@RequestBody @Valid ShopBankCard shopBankCard) {
        ShopDetail shopDetail = shopDetailService.getShopDetailByUserId(SecurityUtils.getUser().getUserId());
        if (Objects.isNull(shopDetail)){{
            return ServerResponseEntity.success();
        }}
        Long shopId = shopDetail.getShopId();
        shopBankCardService.setDefault(shopBankCard.getShopBankCardId(),shopId);
        return ServerResponseEntity.success();
    }
 
    @PutMapping("/setNotDefault")
    @Operation(summary = "取消默认银行卡" , description = "取消默认银行卡")
    @PreAuthorize("@pms.hasPermission('shop:shopBankCard:noDefault')")
    public ServerResponseEntity<Void> setNotDefault(@RequestParam(value = "shopBankCardId") Long shopBankCardId) {
        shopBankCardService.setNotDefault(shopBankCardId, shopDetailService.getShopDetailByUserId(SecurityUtils.getUser().getUserId()).getShopId());
        return ServerResponseEntity.success();
    }
 
    @DeleteMapping("/{shopBankCardId}")
    @Operation(summary = "根据银行卡id删除银行卡信息" , description = "根据银行卡id删除银行卡信息")
    @PreAuthorize("@pms.hasPermission('shop:shopBankCard:delete')")
    public ServerResponseEntity<Void> removeById(@PathVariable("shopBankCardId") Long shopBankCardId) {
        ShopDetail shopDetail = shopDetailService.getShopDetailByUserId(SecurityUtils.getUser().getUserId());
        if (Objects.isNull(shopDetail)){{
            return ServerResponseEntity.success();
        }}
        Long shopId = shopDetail.getShopId();
        ShopBankCard shopBankCard = shopBankCardService.getById(shopBankCardId);
        if (Objects.equals(shopBankCard.getIsDefault(),1)){
            // 不能删除默认银行卡
            throw new YamiShopBindException("yami.shop.cannot.delete.card");
        }
        // 检查是否是最后一张银行卡
        if (shopBankCardService.count(Wrappers.lambdaQuery(ShopBankCard.class).eq(ShopBankCard::getShopId, shopId).eq(ShopBankCard::getStatus, 1)) == 1) {
            throw new YamiShopBindException("yami.shop.least.one.card");
        }
        int count = shopWithdrawCashService.count(new LambdaQueryWrapper<ShopWithdrawCash>()
                .eq(ShopWithdrawCash::getShopBankCardId, shopBankCardId).eq(ShopWithdrawCash::getStatus, 0));
        if(count > 0){
            // 正在用于申请提现的银行卡不能删除
            throw new YamiShopBindException("yami.shop.cannot.delete.check");
        }
        // 进行逻辑删除
        shopBankCardService.update(new LambdaUpdateWrapper<ShopBankCard>().set(ShopBankCard::getStatus,-1).eq(ShopBankCard::getShopBankCardId,shopBankCardId).eq(ShopBankCard::getShopId, shopId));
        return ServerResponseEntity.success();
    }
 
    @PostMapping("/saveAndApplyShop")
    @Operation(summary = "批量保存店铺银行卡信息并提交店铺审核信息" , description = "批量保存店铺银行卡信息并提交店铺审核信息")
    public ServerResponseEntity<Void> saveAndApplyShop(@Valid @RequestBody List<ShopBankCard> shopBankCards) {
        String id = SecurityUtils.getUser().getUserId();
        ShopDetail shopDetail = shopDetailService.getShopDetailByUserId(id);
        Long shopId = shopDetail.getShopId();
        Long employeeId = shopEmployeeService.getShopEmployeeByUserId(shopId);
        shopBankCardService.insertBatchAndSubmitApply(shopBankCards, shopId, employeeId,id);
        return ServerResponseEntity.success();
    }
}