无关风月
21 小时以前 7cab5bda99ca42188bc15b2dae7d1fa4d1833fd9
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package com.ruoyi.admin.controller;
 
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.admin.entity.*;
import com.ruoyi.admin.request.AuditFranchiseeWithdrawDTO;
import com.ruoyi.admin.request.FranchiseeWithdrawDTO;
import com.ruoyi.admin.service.FranchiseeService;
import com.ruoyi.admin.service.SysUserService;
import com.ruoyi.admin.service.TFranchiseeBalanceChangeService;
import com.ruoyi.admin.service.TFranchiseeWithdrawService;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.admin.request.WithdrawDTO;
import com.ruoyi.order.api.entity.Order;
import com.ruoyi.system.api.model.LoginUser;
import com.ruoyi.system.api.model.LoginUserInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
 
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 师傅信息表 前端控制器
 * </p>
 *
 * @author hjl
 * @since 2024-06-03
 */
@RestController
@RequestMapping("/franchiseeWithdraw")
@Api(value = "加盟商提现迭代新增", tags = "加盟商提现迭代新增")
public class AppFranchiseeController {
 
 
    @Resource
    private TokenService tokenService;
    @Resource
    private FranchiseeService franchiseeService;
    @Resource
    private TFranchiseeBalanceChangeService franchiseeBalanceChangeService;
    @Resource
    private TFranchiseeWithdrawService franchiseeWithdrawService;
 
    @Resource
    private SysUserService sysUserService;
 
    @PostMapping("/applyWithdraw")
    @ApiOperation(value = "APP-加盟商申请提现", tags = {"提现"})
    public R check(@RequestBody WithdrawDTO dto) {
        LoginUser loginUser = tokenService.getLoginUser();
        if (loginUser==null){
            return R.fail("登录失效");
        }
        SysUser user = sysUserService.getById(loginUser.getUserid());
 
        Franchisee franchisee = franchiseeService.getById(user.getFranchiseeId());
        if (franchisee.getBalance().subtract(dto.getMoney()).compareTo(BigDecimal.ZERO)>=0){
            BigDecimal subtract = franchisee.getBalance().subtract(dto.getMoney());
            franchisee.setBalance(subtract);
            franchiseeService.updateById(franchisee);
        }else{
            return R.fail("余额不足");
        }
        // 扣去余额 生成余额明细
        TFranchiseeBalanceChange balanceChange = new TFranchiseeBalanceChange();
        balanceChange.setAmount(dto.getMoney());
        balanceChange.setIs_pay(1);
        balanceChange.setFranchiseeName(franchisee.getName());
        balanceChange.setFranchiseeId(franchisee.getId()+"");
        balanceChange.setRemark("余额提现");
        balanceChange.setType(3);
        balanceChange.setIsDelete(0);
        franchiseeBalanceChangeService.save(balanceChange);
        // 生成提现记录
        FranchiseeWithdraw franchiseeWithdraw = new FranchiseeWithdraw();
        franchiseeWithdraw.setChangeId(balanceChange.getId());
        franchiseeWithdraw.setFranchiseeId(Integer.valueOf(balanceChange.getFranchiseeId()));
        franchiseeWithdraw.setStatus(1);
        franchiseeWithdraw.setContent(dto.getContent());
        franchiseeWithdraw.setMoney(dto.getMoney());
        franchiseeWithdraw.setCreateTime(new Date());
        franchiseeWithdraw.setUpdateTime(new Date());
        franchiseeWithdraw.setIsDelete(0);
        franchiseeWithdrawService.save(franchiseeWithdraw);
        return R.ok();
    }
 
    @ApiOperation(value = "APP-加盟商提现记录", tags = {"提现"})
    @GetMapping(value = "/franchiseeWithdrawListApp")
 
    public R<IPage<FranchiseeWithdraw>> franchiseeWithdrawListApp(
                                    @RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                                    @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        LoginUser loginWorker = tokenService.getLoginUser();
        if (null == loginWorker) {
            return R.loginExpire("登录失效!");
        }
        SysUser user = sysUserService.getById(loginWorker.getUserid());
        IPage<FranchiseeWithdraw> franchiseeWithdrawList = franchiseeWithdrawService
                .lambdaQuery().eq(FranchiseeWithdraw::getFranchiseeId, user.getFranchiseeId())
                .orderByDesc(FranchiseeWithdraw::getCreateTime)
                .page(Page.of(pageNum, pageSize));
        return R.ok(franchiseeWithdrawList);
    }
    @ApiOperation(value = "管理后台-加盟商提现记录", tags = {"提现"})
    @PostMapping(value = "/franchiseeWithdrawList")
    public R<IPage<FranchiseeWithdraw>> franchiseeWithdrawList(@RequestBody FranchiseeWithdrawDTO franchiseeWithdrawDTO) {
        LoginUser loginWorker = tokenService.getLoginUser();
        if (null == loginWorker) {
            return R.loginExpire("登录失效!");
        }
        SysUser byId = sysUserService.getById(loginWorker.getUserid());
 
        LambdaQueryWrapper<FranchiseeWithdraw> franchiseeWithdrawLambdaQueryWrapper = new LambdaQueryWrapper<>();
        franchiseeWithdrawLambdaQueryWrapper
                .orderByDesc(FranchiseeWithdraw::getCreateTime);
        if (StringUtils.hasLength(franchiseeWithdrawDTO.getFranchiseeName())){
            List<Integer> ids = franchiseeService.lambdaQuery().like(Franchisee::getName, franchiseeWithdrawDTO.getFranchiseeName()).list()
                    .stream().map(Franchisee::getId).collect(Collectors.toList());
            if (!ids.isEmpty()){
                franchiseeWithdrawLambdaQueryWrapper.in(FranchiseeWithdraw::getFranchiseeId, ids);
            }else{
                franchiseeWithdrawLambdaQueryWrapper.eq(FranchiseeWithdraw::getFranchiseeId, -1);
            }
        }
        if (StringUtils.hasLength(franchiseeWithdrawDTO.getStartTime())){
            franchiseeWithdrawLambdaQueryWrapper
                    .between(FranchiseeWithdraw::getCreateTime
                            ,franchiseeWithdrawDTO.getStartTime()
                            , franchiseeWithdrawDTO.getEndTime());
        }
        IPage<FranchiseeWithdraw> franchiseeWithdrawList = franchiseeWithdrawService
                .page(Page.of(franchiseeWithdrawDTO.getPageNum(), franchiseeWithdrawDTO.getPageSize()), franchiseeWithdrawLambdaQueryWrapper);
        List<Franchisee> list = franchiseeService.list();
        for (FranchiseeWithdraw record : franchiseeWithdrawList.getRecords()) {
            Franchisee franchisee = list.stream().filter(e -> e.getId().equals(record.getFranchiseeId())).findFirst().orElse(null);
            if (franchisee!=null){
                record.setFranchiseeName(franchisee.getName());
                record.setFranchiseePhone(franchisee.getHeadPhone());
            }
        }
        return R.ok(franchiseeWithdrawList);
    }
    @ApiOperation(value = "管理后台-加盟商提现详情", tags = {"提现"})
    @GetMapping(value = "/franchiseeWithdrawInfo")
    public R<FranchiseeWithdraw> franchiseeWithdrawInfo(@RequestParam(name = "id")Integer id) {
        LoginUser loginWorker = tokenService.getLoginUser();
        if (null == loginWorker) {
            return R.loginExpire("登录失效!");
        }
        FranchiseeWithdraw franchiseeWithdraw = franchiseeWithdrawService.getById(id);
        Franchisee franchisee = franchiseeService.getById(franchiseeWithdraw.getFranchiseeId());
        if (franchisee!=null){
            franchiseeWithdraw.setFranchiseePhone(franchisee.getHeadPhone());
            franchiseeWithdraw.setFranchiseeName(franchisee.getName());
        }
        return R.ok(franchiseeWithdraw);
    }
    @ApiOperation(value = "管理后台-审核", tags = {"提现"})
    @PostMapping(value = "/auditFranchiseeWithdraw")
    public R auditFranchiseeWithdraw(@RequestBody AuditFranchiseeWithdrawDTO auditFranchiseeWithdrawDTO) {
        LoginUser loginWorker = tokenService.getLoginUser();
        if (null == loginWorker) {
            return R.loginExpire("登录失效!");
        }
        SysUser byId = sysUserService.getById(loginWorker.getUserid());
        FranchiseeWithdraw franchiseeWithdraw = franchiseeWithdrawService.getById(auditFranchiseeWithdrawDTO.getId());
        franchiseeWithdraw.setStatus(auditFranchiseeWithdrawDTO.getStatus());
        franchiseeWithdraw.setResult(auditFranchiseeWithdrawDTO.getResult());
        franchiseeWithdraw.setImg(auditFranchiseeWithdrawDTO.getImg());
        franchiseeWithdraw.setUpdateBy(byId.getNickName());
        franchiseeWithdraw.setHandle(byId.getNickName());
        franchiseeWithdraw.setUpdateTime(new Date());
        franchiseeWithdrawService.updateById(franchiseeWithdraw);
        Franchisee franchisee = franchiseeService.getById(franchiseeWithdraw.getFranchiseeId());
        if (auditFranchiseeWithdrawDTO.getStatus()==3){
            // 回退运营商余额
            TFranchiseeBalanceChange balanceChange = new TFranchiseeBalanceChange();
            balanceChange.setAmount(franchiseeWithdraw.getMoney());
            balanceChange.setIs_pay(1);
            balanceChange.setFranchiseeName(franchisee.getName());
            balanceChange.setFranchiseeId(franchisee.getId()+"");
            balanceChange.setRemark("余额提现回退");
            balanceChange.setType(4);
            balanceChange.setIsDelete(0);
            franchiseeBalanceChangeService.save(balanceChange);
            BigDecimal add = franchisee.getBalance().add(franchiseeWithdraw.getMoney());
            franchisee.setBalance(add);
            franchiseeService.updateById(franchisee);
        }
        return R.ok();
    }
}