liujie
2025-12-19 65dd77c05283c150949600090ccdc08774a39fc7
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
package com.ruoyi.web.controller.api;
 
 
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.basic.PageInfo;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.dto.UpdateAppUserDto;
import com.ruoyi.system.export.AppUserGoldImport;
import com.ruoyi.system.export.AppUserImport;
import com.ruoyi.system.model.TAppUser;
import com.ruoyi.system.model.TCompany;
import com.ruoyi.system.model.TGoldChangeRecord;
import com.ruoyi.system.query.AppUserListQuery;
import com.ruoyi.system.service.TAppUserService;
import com.ruoyi.system.service.TCompanyService;
import com.ruoyi.system.service.TGoldChangeRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
 
import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
 
/**
 * <p>
 * 用户管理 前端控制器
 * </p>
 *
 * @author xiaochen
 * @since 2025-11-06
 */
@RestController
@RequestMapping("/t-app-user")
@Api(tags = "(嘉视达)用户管理")
public class TAppUserController {
 
 
    @Resource
    private TAppUserService  appUserService;
 
 
    @Autowired
    private RedisTemplate redisTemplate;
 
    @Autowired
    private TokenService tokenService;
 
    @Autowired
    private TCompanyService companyService;
 
    @Autowired
    private TGoldChangeRecordService goldChangeRecordService;
 
 
    @ApiOperation(value = "用户管理分页列表")
    @PostMapping(value = "/pageList")
    public R<PageInfo<TAppUser>> pageList(@RequestBody AppUserListQuery query) {
        return R.ok(appUserService.pageList(query));
    }
 
 
    @ApiOperation(value = "修改状态")
    @GetMapping(value = "/changeStatus")
    @Log(title = "小程序用户-修改状态", businessType = BusinessType.UPDATE)
    public R pageList(@RequestParam Integer id) {
        TAppUser appUser = appUserService.getById(id);
        appUser.setStatus(appUser.getStatus()==1?2:1);
        appUserService.updateById(appUser);
        return R.ok();
    }
 
 
    @ApiOperation(value = "修改用户信息")
    @PostMapping(value = "/updateAppUser")
    @Log(title = "小程序用户-修改用户信息", businessType = BusinessType.UPDATE)
    public R updateAppUser(@RequestBody UpdateAppUserDto dto) {
 
        if(dto.getCode()!=null && !dto.getCode().isEmpty()){
            long count = appUserService.count(new LambdaQueryWrapper<TAppUser>().eq(TAppUser::getCode, dto.getCode()).ne(TAppUser::getId, dto.getId()));
            if (count > 0) {
                return R.fail("推广号已存在");
            }
        }
        TAppUser appUser = appUserService.getById(dto.getId());
        String code = appUser.getCode();
        if(dto.getCode()!=null && !dto.getCode().isEmpty() && !code.equals(dto.getCode())){
            appUser.setCode(dto.getCode());
            // 移除之前的二维码
            redisTemplate.delete("USER_WX_IMG" + appUser.getId());
        }
        if(dto.getPromotionStatus()!=null){
            appUser.setPromotionStatus(dto.getPromotionStatus());
        }
        if(dto.getUserType()!=null){
            appUser.setUserType(dto.getUserType());
        }
        if(dto.getReferrerUserId()!=null){
            appUser.setReferrerUserId(dto.getReferrerUserId());
        }
        if(dto.getCompanyId()!=null){
            appUser.setCompanyId(dto.getCompanyId());
        }
        appUserService.updateById(appUser);
        return R.ok();
    }
 
 
    @ApiOperation(value = "查看详情")
    @GetMapping(value = "/detail")
    public R<TAppUser> detail(@RequestParam String id) {
        return R.ok(appUserService.getById( id));
    }
 
 
    @Log(title = "小程序用户-金币导入", businessType = BusinessType.IMPORT)
    @ApiOperation(value = "小程序用户-金币导入")
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
    @PostMapping("/importGold")
    public R<String> importGold(@RequestPart("file") MultipartFile file) {
 
        Long userId = tokenService.getLoginUser().getUserId();
 
        List<AppUserGoldImport> imports = new ArrayList<>();
        ImportParams params = new ImportParams();
        params.setHeadRows(1); //表头行数
        InputStream inputStream = null;
        try {
            inputStream = file.getInputStream();
            imports = ExcelImportUtil.importExcel(inputStream,
                    AppUserGoldImport.class, params);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException("金币导入失败!");
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                throw new ServiceException(e.getMessage());
            }
        }
        if (CollectionUtils.isEmpty(imports)) {
            throw new ServiceException("金币导入数据为空!");
        }
        JSONObject result = new JSONObject();
        int i = 1;
        for (AppUserGoldImport appUserGoldImport : imports) {
            if (!StringUtils.hasLength(appUserGoldImport.getUserPhone())){
                result.append("第"+i+"行:[", "用户手机号"+"]不能为空");
                continue;
            }
            if (!StringUtils.hasLength(appUserGoldImport.getGold())){
                result.append("第"+i+"行:[", "金币"+"]不能为空");
                continue;
            }
 
            TAppUser user = appUserService.getOne(new LambdaQueryWrapper<TAppUser>().eq(TAppUser::getPhone, appUserGoldImport.getUserPhone()).ne(TAppUser::getStatus, 3).last("LIMIT 1"));
            if (user==null){
                result.append("第"+i+"行:[", "用户不存在"+"]");
                continue;
            }
            BigDecimal totalPrice= BigDecimal.ZERO;
            try {
                totalPrice= new BigDecimal(appUserGoldImport.getGold());
                user.setGoldBalance(user.getGoldBalance().add(totalPrice).setScale(2, RoundingMode.HALF_UP));
            } catch (Exception e) {
                result.append("第"+i+"行:[", "金币"+"]格式错误");
                continue;
            }
 
            // 添加变更记录
            TGoldChangeRecord goldChangeRecord = new TGoldChangeRecord();
            goldChangeRecord.setAppUserId(user.getId());
            goldChangeRecord.setType(1);
            goldChangeRecord.setPrice(totalPrice);
            goldChangeRecord.setBalance(user.getGoldBalance());
            goldChangeRecord.setCreateUserId(userId);
            goldChangeRecordService.save(goldChangeRecord);
 
            appUserService.updateById( user);
            i++;
        }
        if(!result.isEmpty()){
            return R.fail(result.toString());
        }
        return R.ok();
    }
 
 
 
    @Log(title = "小程序用户-用户导入", businessType = BusinessType.IMPORT)
    @ApiOperation(value = "小程序用户-用户导入")
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
    @PostMapping("/importUser")
    public R<String> importUser(@RequestPart("file") MultipartFile file) {
 
        List<AppUserImport> imports = new ArrayList<>();
        ImportParams params = new ImportParams();
        params.setHeadRows(1); //表头行数
        InputStream inputStream = null;
        try {
            inputStream = file.getInputStream();
            imports = ExcelImportUtil.importExcel(inputStream,
                    AppUserImport.class, params);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException("小程序用户导入失败!");
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                throw new ServiceException(e.getMessage());
            }
        }
        if (CollectionUtils.isEmpty(imports)) {
            throw new ServiceException("小程序用户导入数据为空!");
        }
        JSONObject result = new JSONObject();
        int i = 1;
        for (AppUserImport appUserImport : imports) {
            if (!StringUtils.hasLength(appUserImport.getUsername())){
                result.append("第"+i+"行:[", "用户名"+"]不能为空");
                continue;
            }
            if (!StringUtils.hasLength(appUserImport.getPhone())){
                result.append("第"+i+"行:[", "联系电话"+"]不能为空");
                continue;
            }
            if (!StringUtils.hasLength(appUserImport.getSex())){
                result.append("第"+i+"行:[", "性别"+"]不能为空");
                continue;
            }
            if (!StringUtils.hasLength(appUserImport.getBirth())){
                result.append("第"+i+"行:[", "出生日期"+"]不能为空");
                continue;
            }
            if (!StringUtils.hasLength(appUserImport.getRole())){
                result.append("第"+i+"行:[", "角色"+"]不能为空");
                continue;
            }
            if (!StringUtils.hasLength(appUserImport.getCode())){
                result.append("第"+i+"行:[", "推广号"+"]不能为空");
                continue;
            }
 
            TAppUser user = appUserService.getOne(new LambdaQueryWrapper<TAppUser>().eq(TAppUser::getPhone, appUserImport.getPhone()).ne(TAppUser::getStatus, 3).last("LIMIT 1"));
            if (user!=null){
                result.append("第"+i+"行:[", "联系电话已存在"+"]");
                continue;
            }
            TAppUser user1 = appUserService.getOne(new LambdaQueryWrapper<TAppUser>().eq(TAppUser::getCode, appUserImport.getCode()).ne(TAppUser::getStatus, 3).last("LIMIT 1"));
            if (user1!=null){
                result.append("第"+i+"行:[", "推广号已存在"+"]");
                continue;
            }
 
            String sex = appUserImport.getSex();
            Integer gender = "男".equals(sex) ? 1 : "女".equals(sex) ? 2 : null;
            if(gender==null){
                result.append("第"+i+"行:[", "性别"+"]格式错误");
                continue;
            }
 
            String birth = appUserImport.getBirth();
            LocalDate birthday = null;
            try {
                birthday = LocalDate.parse(birth);
            } catch (Exception e) {
                result.append("第"+i+"行:[", "出生日期"+"]格式错误");
                continue;
            }
 
            String role = appUserImport.getRole();
            Integer userType = "游客".equals(role) ? 1 : "营销员".equals(role) ? 2 : "合伙人".equals(role) ? 3 : null;
            if(userType==null){
                result.append("第"+i+"行:[", "角色"+"]格式错误");
                continue;
            }
 
            TAppUser tAppUser = new TAppUser();
            tAppUser.setName(appUserImport.getUsername());
            tAppUser.setCode(appUserImport.getCode());
            tAppUser.setPhone(appUserImport.getPhone());
            tAppUser.setGender(gender);
            tAppUser.setBirthday(birthday);
            tAppUser.setUserType(userType);
 
            if(appUserImport.getReferrerCode()!=null){
                TAppUser referrerUser = appUserService.getOne(new LambdaQueryWrapper<TAppUser>().eq(TAppUser::getCode, appUserImport.getReferrerCode()).ne(TAppUser::getStatus, 3).last("LIMIT 1"));
                if(referrerUser!=null){
                    tAppUser.setReferrerUserId(referrerUser.getId());
                    tAppUser.setReferrerStatus(1);
                    tAppUser.setReferrerTime(LocalDateTime.now());
                }
            }
            if(appUserImport.getDealerCode()!=null){
            }
            TCompany one = companyService.getOne(new LambdaQueryWrapper<TCompany>().eq(TCompany::getCode, appUserImport.getDealerCode()).ne(TCompany::getStatus, 3).last("LIMIT 1"));
            if(one!=null){
                tAppUser.setCompanyId(one.getId());
            }
            appUserService.save( user);
            i++;
        }
        if(!result.isEmpty()){
            return R.fail(result.toString());
        }
        return R.ok();
    }
 
}