Pu Zhibing
2025-03-25 d3e9a09cb54017a8063e5bfe3ace5012f66f3130
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
package com.ruoyi.other.controller;
 
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.account.api.feignClient.AppUserClient;
import com.ruoyi.account.api.feignClient.UserPointClient;
import com.ruoyi.account.api.model.AppUser;
import com.ruoyi.account.api.model.UserPoint;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.ServletUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.other.api.domain.PointSetting;
import com.ruoyi.other.api.domain.Share;
import com.ruoyi.other.api.domain.Shop;
import com.ruoyi.other.api.feignClient.ShopClient;
import com.ruoyi.other.enums.ShareAddType;
import com.ruoyi.other.enums.ShareAuditStatus;
import com.ruoyi.other.service.PointSettingService;
import com.ruoyi.other.service.ShareService;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.api.feignClient.SysUserClient;
import com.ruoyi.system.api.model.LoginUser;
import io.swagger.annotations.*;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
 
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author luodangjia
 * @since 2024-11-20
 */
@Api("分享")
@RestController
@RequestMapping("/share")
public class ShareController extends BaseController {
    @Resource
    private ShareService shareService;
    @Resource
    private TokenService tokenService;
    @Resource
    private AppUserClient appUserClient;
    @Resource
    private ShopClient shopClient;
    @Resource
    private SysUserClient sysUserClient;
    @Resource
    private UserPointClient userPointClient;
    @Resource
    private PointSettingService pointSettingService;
    
    
 
    /**
     * 分享列表
     */
    @ApiOperation(value = "分享列表", tags = {"小程序-个人中心-门店管理"})
    @ApiImplicitParams({
            @ApiImplicitParam(value = "对象id(addType=1:平台添加,addType=2:推广人添加,addType=3:门店添加)", name = "objectId", required = true, dataType = "int"),
    })
    @GetMapping("/list")
    public R<List<Share>> list(@RequestParam Integer objectId) {
        List<Share> list = shareService.list(new LambdaQueryWrapper<Share>()
                .eq(Share::getObjectId, objectId)
                .eq(Share::getDelFlag, 0).or()
                .eq(Share::getAddType, 1)
                .eq(Share::getDelFlag, 0).orderByDesc(Share::getCreateTime));
        return R.ok(list);
    }
 
 
    @ApiOperation(value = "分享页列表", tags = {"小程序-推广中心"})
    @GetMapping("/recommand/list")
    public R<List<Share>> recommandlist() {
        Long userid = tokenService.getLoginUserApplet().getUserid();
        return R.ok(shareService.list(new LambdaQueryWrapper<Share>().eq(Share::getAddType, 2)
                .eq(Share::getObjectId, userid).or().eq(Share::getAddType, 1).eq(Share::getAuditStatus, 1)
                .eq(Share::getDelFlag, 0).orderByDesc(Share::getCreateTime)));
    }
 
 
    /**
     * 分享添加
     */
    @ApiOperation(value = "分享添加", tags = {"小程序-个人中心-门店管理"})
    @PostMapping
    public R<Void> add(@RequestBody Share share) {
        Long userid = tokenService.getLoginUserApplet().getUserid();
        if (share.getAddType() == 1) {
            share.setAuditStatus(ShareAuditStatus.WAIT.getCode());
        } else if (share.getAddType() == 2) {
            share.setAuditStatus(ShareAuditStatus.WAIT.getCode());
            share.setObjectId(userid);
        }
        share.setDelFlag(0);
        share.setCreateTime(LocalDateTime.now());
        shareService.save(share);
        return R.ok();
    }
 
    /**
     * 分享删除
     */
    @ApiOperation(value = "分享删除", tags = {"小程序-个人中心-门店管理"})
    @DeleteMapping
    public R<Void> delete(@RequestBody Share share) {
        shareService.removeById(share);
        return R.ok();
    }
 
    /**
     * 分享编辑
     */
    @ApiOperation(value = "分享编辑", tags = {"小程序-个人中心-门店管理"})
    @PutMapping
    public R<Void> edit(@RequestBody Share share) {
        share.setAuditStatus(ShareAuditStatus.WAIT.getCode());
        shareService.updateById(share);
        return R.ok();
    }
 
    /**
     * 分享详情
     */
    @ApiOperation(value = "分享详情", tags = {"小程序-个人中心-门店管理"})
    @GetMapping("/detail/{id}")
    public R<Share> detail(@PathVariable("id") Integer id) {
        return R.ok(shareService.getById(id));
    }
 
 
    @ApiOperation(value = "广告管理-分享管理-添加", tags = {"管理后台"})
    @PostMapping("/manage/add")
    public R<Void> manage(@RequestBody Share share) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userid = loginUser.getSysUser().getUserId();
        SysUser sysUser = sysUserClient.getSysUser(userid).getData();
        share.setAddType(sysUser.getRoleType() == 1 ? 1 : 3);
        share.setAppletShare(0);
        if(sysUser.getRoleType() == 2){
            share.setObjectId(sysUser.getObjectId().longValue());
        }
        if (sysUser.getRoleType() == 1){
            share.setAuditStatus(ShareAuditStatus.SUCCESS.getCode());
        }else{
            share.setAuditStatus(ShareAuditStatus.WAIT.getCode());
        }
        share.setDelFlag(0);
        shareService.save(share);
        return R.ok();
    }
 
    @ApiOperation(value = "广告管理-分享管理-编辑", tags = {"管理后台"})
    @PostMapping("/manage/edit")
    public R<Void> manageedit(@RequestBody Share share) {
        if (null == share.getAuditStatus() || share.getAuditStatus()==2){
            share.setAuditStatus(0);
        }
        shareService.updateById(share);
        return R.ok();
    }
 
    @ApiOperation(value = "广告管理-分享管理-删除", tags = {"管理后台"})
    @GetMapping("/manage/delete")
    public R<Void> managedelete(@RequestParam Integer id) {
        shareService.removeById(id);
        return R.ok();
    }
 
    @ApiOperation(value = "广告管理-分享管理-列表", tags = {"管理后台"})
    @GetMapping("/manage/list")
    public R<Page<Share>> managelist(String name,
                                     Integer addType,
                                     Integer auditStatus,
                                     @RequestParam Integer pageNum, Integer pageSize) {
        //判断当前登陆人是平台还是门店
        Long userid = tokenService.getLoginUser().getUserid();
        SysUser sysUser = sysUserClient.getSysUser(userid).getData();
        if (sysUser.getRoleType()==1) {
            Page<Share> page = shareService.lambdaQuery()
                    .eq(Share::getAuditStatus, 1)
                    .eq(Share::getDelFlag, 0)
                    .like(name != null, Share::getName, name)
                    .eq(addType != null , Share::getAddType, addType)
                    .page(Page.of(pageNum, pageSize));
            return R.ok(page);
        }else {
            Page<Share> page = shareService.lambdaQuery()
                    .eq(auditStatus!= null, Share::getAuditStatus, auditStatus)
                    .eq(Share::getAddType,3)
                    .eq(Share::getDelFlag, 0)
                    .eq(Share::getObjectId, sysUser.getObjectId())
                    .like(name != null, Share::getName, name).page(Page.of(pageNum, pageSize));
            return R.ok(page);
        }
    }
 
    @ApiOperation(value = "广告管理-分享管理-设为小程序分享", tags = {"管理后台"})
    @GetMapping("/manage/set")
    public R<Void> set(@RequestParam Integer id) {
        //将所有分享设为不是小程序分享
        List<Share> list = shareService.lambdaQuery().list();
        if (!list.isEmpty()) {
            for (Share share : list) {
                share.setAppletShare(0);
            }
            shareService.updateBatchById(list);
        }
        //设置小程序分享
        Share byId = shareService.getById(id);
        byId.setAppletShare(1);
        shareService.updateById(byId);
        return R.ok();
    }
 
 
    @ApiOperation(value = "广告管理-分享管理-审核列表", tags = {"管理后台"})
    @GetMapping("/manage/auth/list")
    public R<Page<Share>> authmanagelist(String name,
                                         Integer addType,
                                         String authName,
                                         String authPhone,
                                         Integer auditStatus,
                                         @RequestParam Integer pageNum,
                                         Integer pageSize) {
 
        List<Integer> shopIds = new ArrayList<>();
        List<Long> userIds = new ArrayList<>();
        if (StringUtils.isNotEmpty(authName)){
            R<List<AppUser>> uR = appUserClient.getAppUserByName(authName);
            List<AppUser> userList = uR.getData();
            if (CollectionUtils.isEmpty(userList)){
                return R.ok(Page.of(pageNum, pageSize));
            }
            userIds.addAll(userList.stream().map(AppUser::getId).collect(Collectors.toList()));
            //门店名称
            Set<Integer> integers = shopClient.getShopIdByName(authName).getData();
            if(null != integers){
                shopIds.addAll(integers);
            }
        }
        if (StringUtils.isNotEmpty(authPhone)){
            R<List<AppUser>> uR = appUserClient.getAppUserByPhone(authPhone);
            List<AppUser> userList = uR.getData();
            if (CollectionUtils.isEmpty(userList)){
                return R.ok(Page.of(pageNum, pageSize));
            }
            userIds.addAll(userList.stream().map(AppUser::getId).collect(Collectors.toList()));
            //获取门店
            for (AppUser appUser : userList) {
                List<Shop> data = shopClient.getShopByUserId(appUser.getId()).getData();
                if(null != data && data.size() > 0){
                    shopIds.addAll(data.stream().map(Shop::getId).collect(Collectors.toList()));
                }
            }
        }
        Page<Share> page = shareService.authmanagelist(name, addType, auditStatus, userIds, shopIds, pageNum, pageSize);
        for (Share record : page.getRecords()) {
            if (record.getAddType() == 2) {
                AppUser appUserById = appUserClient.getAppUserById(Long.valueOf(record.getObjectId()));
                if (appUserById!=null) {
                    record.setAuthName(appUserById.getName());
                    record.setAuthPhone(appUserById.getPhone());
                }
            }
            if (record.getAddType() == 3) {
                R<Shop> shopById = shopClient.getShopById(record.getObjectId().intValue());
                if (shopById.getData() != null) {
                    record.setAuthName(shopById.getData().getName());
                }
            }
        }
        return R.ok(page);
    }
 
    @ApiOperation(value = "广告管理-分享管理-详情", tags = {"管理后台"})
    @GetMapping("/manage/auth/detail")
    public R<Share> authmanagelist(Integer id) {
        Share byId = shareService.getById(id);
        return R.ok(byId);
    }
 
    @ApiOperation(value = "广告管理-分享管理-审核", tags = {"管理后台"})
    @GetMapping("/manage/auth/check")
    public R<Share> authmanagecheck(Integer id, @ApiParam("1=审核通过,2=审核失败") Integer auditStatus, @ApiParam("备注") String auditMsg) {
        Share byId = shareService.getById(id);
        byId.setAuditStatus(auditStatus);
        byId.setAuditMsg(auditMsg);
        shareService.updateById(byId);
        return R.ok(byId);
    }
 
 
    @ApiOperation(value = "获取小程序分享数据", tags = {"小程序-分享"})
    @GetMapping("/auth/getAppletShare")
    public R<Share> getAppletShare(){
        Share one = shareService.getOne(new LambdaQueryWrapper<Share>().eq(Share::getDelFlag, 0).eq(Share::getAppletShare, 1)
                .eq(Share::getAuditStatus, 1).last(" limit 0, 1"));
        //添加每日分享积分
        String token = SecurityUtils.getToken(ServletUtils.getRequest());
        if(StringUtils.isNotEmpty(token)){
            Long userid = tokenService.getLoginUserApplet().getUserid();
            Boolean data = userPointClient.judgmentDailyShare(userid).getData();
            if(!data){
                AppUser appUser = appUserClient.getAppUserById(userid);
                PointSetting pointSetting = pointSettingService.getOne(new LambdaQueryWrapper<PointSetting>()
                        .eq(PointSetting::getId, appUser.getVipId()).eq(PointSetting::getDelFlag, 0));
                if(null != pointSetting){
                    Integer everySharePoint = pointSetting.getEverySharePoint();
                    if(everySharePoint > 0){
                        Integer lavePoint = appUser.getLavePoint();
                        appUser.setTotalSharePoint(appUser.getTotalSharePoint() + everySharePoint);
                        appUser.setLavePoint(appUser.getLavePoint() + everySharePoint);
                        appUser.setAvailablePoint(appUser.getAvailablePoint() + everySharePoint);
                        appUser.setTransferablePoint(appUser.getTransferablePoint() + everySharePoint);
                        appUser.setTotalAvailablePoint(appUser.getTotalAvailablePoint() + everySharePoint);
                        appUser.setTotalPoint(appUser.getTotalPoint() + everySharePoint);
                        appUserClient.editAppUserById(appUser);
                        //添加积分流水
                        UserPoint userPoint = new UserPoint();
                        userPoint.setAppUserId(userid);
                        userPoint.setType(4);
                        userPoint.setHistoricalPoint(lavePoint);
                        userPoint.setVariablePoint(everySharePoint);
                        userPoint.setBalance(appUser.getLavePoint());
                        userPoint.setCreateTime(LocalDateTime.now());
                        userPoint.setAppUserId(appUser.getId());
                        userPointClient.saveUserPoint(userPoint);
                    }
                }
 
            }
        }
        return R.ok(one);
    }
 
    //appUserShare/
    @GetMapping("/saveAppUserShare")
    public R<Share> saveAppUserShare() {
        return getAppletShare();
    }
 
    
}