guohongjin
2024-05-01 1901fceb6ddaa56a57f3131191454554c3e77e68
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
package cn.stylefeng.guns.modular.business.controller;
 
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import cn.stylefeng.guns.modular.business.dto.BaseIdNameDTO;
import cn.stylefeng.guns.modular.business.dto.CounsellingOrderReservationResponseDTO;
import cn.stylefeng.guns.modular.business.dto.UserCounsellingTotalDTO;
import cn.stylefeng.guns.modular.business.dto.UserMentalAppointmentPageResponseDTO;
import cn.stylefeng.guns.modular.business.dto.request.UserCounsellingOrderReservationRequestDTO;
import cn.stylefeng.guns.modular.business.entity.*;
import cn.stylefeng.guns.modular.business.service.*;
import cn.stylefeng.roses.kernel.customer.api.pojo.UserDetailMentalTestRecordDTO;
import cn.stylefeng.roses.kernel.customer.api.pojo.UserDetailMentalTestRecordNlwpDTO;
import cn.stylefeng.roses.kernel.customer.api.pojo.UserManagePageResponseDTO;
import cn.stylefeng.roses.kernel.customer.modular.entity.Customer;
import cn.stylefeng.roses.kernel.customer.modular.service.CustomerService;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.stylefeng.roses.kernel.rule.annotation.BusinessLog;
import cn.stylefeng.roses.kernel.rule.enums.CustomerUserTypeEnum;
import cn.stylefeng.roses.kernel.rule.enums.ResBizTypeEnum;
import cn.stylefeng.roses.kernel.rule.enums.StatusEnum;
import cn.stylefeng.roses.kernel.rule.pojo.response.ErrorResponseData;
import cn.stylefeng.roses.kernel.rule.pojo.response.ResponseData;
import cn.stylefeng.roses.kernel.rule.pojo.response.SuccessResponseData;
import cn.stylefeng.roses.kernel.scanner.api.annotation.ApiResource;
import cn.stylefeng.roses.kernel.scanner.api.annotation.GetResource;
import cn.stylefeng.roses.kernel.scanner.api.annotation.PostResource;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;
 
@RestController
@Api(tags = "用户管理")
@ApiResource(name = "用户管理", resBizType = ResBizTypeEnum.BUSINESS)
@RequestMapping("/userManage")
public class UserManageController {
 
    @Resource
    private IAreaService areaService;
 
    @Resource
    private CustomerService customerService;
 
    @Resource
    private IMentalTestRecordService mentalTestRecordService;
 
    @Resource
    private IMentalTestQuestionService mentalTestQuestionService;
 
    @Resource
    private IMentalTestOptionService mentalTestOptionService;
 
    @Resource
    private IMentalTestResultService mentalTestResultService;
 
    @Resource
    private IMentalTestResultSetService mentalTestResultSetService;
 
    @Resource
    private IMentalAppointmentService mentalAppointmentService;
 
    @Resource
    private ICounsellingOrderReservationService counsellingOrderReservationService;
 
    @Resource
    private ICounsellingOrderService counsellingOrderService;
 
    @Resource
    private IImGroupService imGroupService;
 
    @ApiOperation("用户信息(分页)")
    @GetResource(name = "用户信息(分页)", path = "/page")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "当前页数", dataTypeClass = Integer.class, defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "分页条数", dataTypeClass = Integer.class, defaultValue = "20"),
            @ApiImplicitParam(name = "name", value = "名称", dataTypeClass = String.class),
            @ApiImplicitParam(name = "telephone", value = "联系电话", dataTypeClass = String.class),
            @ApiImplicitParam(name = "statusFlag", value = "用户状态:1-启用,2-禁用", dataTypeClass = Integer.class),
    })
    public ResponseData<PageResult<UserManagePageResponseDTO>> page(Integer pageNo, Integer pageSize, String name, String telephone, Integer statusFlag) {
        Page<UserManagePageResponseDTO> page = customerService.getCustomerPage(
                PageFactory.page(pageNo, pageSize),
                CustomerUserTypeEnum.USER.getCode(),
                null,
                name,
                telephone,
                statusFlag
        );
        return new SuccessResponseData<>(PageResultFactory.createPageResult(page));
    }
 
    @ApiOperation("获取详情")
    @GetResource(name = "获取详情", path = "/detail/{id}")
    public ResponseData<UserManagePageResponseDTO> detail(@PathVariable("id") Long id) {
        // 查询用户详情
        Page<UserManagePageResponseDTO> page = customerService.getCustomerPage(
                PageFactory.page(1, 1),
                CustomerUserTypeEnum.USER.getCode(),
                id,
                null, null, null
        );
 
        // 用户信息
        UserManagePageResponseDTO dto = page.getRecords().stream().findFirst().orElseThrow(() -> new RuntimeException("用户不存在"));
 
        // 心理测试记录(最新1条)
        MentalTestRecord mentalTestRecord = mentalTestRecordService.getOne(
                Wrappers.<MentalTestRecord>lambdaQuery()
                        .eq(MentalTestRecord::getUserId, dto.getCustomerId())
                        .eq(MentalTestRecord::getResultCalculateMode, 2)
                        .orderByDesc(MentalTestRecord::getCreateTime)
                        .last("limit 1")
        );
        dto.setMentalTestRecord(BeanUtil.toBean(mentalTestRecord, UserDetailMentalTestRecordDTO.class));
 
        if (StrUtil.isNotBlank(dto.getCityCode())) {
            dto.setCityCodeStr(areaService.getNameByCodeAddPrefix("/", dto.getCityCode()));
        }
 
        return new SuccessResponseData<>(dto);
    }
 
    @ApiOperation(value = "查询NlWP", notes = "使用心理测试记录ID进行查询")
    @GetResource(name = "查询NlWP", path = "/mentalTestRecord")
    public ResponseData<UserDetailMentalTestRecordNlwpDTO> mentalTestRecord(Long id) {
        // 用户详情心理测试记录NLWP
        UserDetailMentalTestRecordNlwpDTO dto = new UserDetailMentalTestRecordNlwpDTO();
 
        // 答题记录
        MentalTestRecord mentalTestRecord = mentalTestRecordService.getById(id);
        dto.setContent(mentalTestRecord.getContent());
 
        // 封装选项列表
        dto.setItemList(
                mentalTestResultSetService.list(
                                Wrappers.<MentalTestResultSet>lambdaQuery()
                                        .eq(MentalTestResultSet::getTopicId, mentalTestRecord.getTopicId())
                                        .eq(MentalTestResultSet::getResultCalculateMode, mentalTestRecord.getResultCalculateMode())
                        ).stream()
                        .map(o -> BeanUtil.toBean(o, UserDetailMentalTestRecordNlwpDTO.ItemDTO.class))
                        .collect(Collectors.toList())
        );
 
        // 查询所有问题、选择
        List<MentalTestQuestion> questionAll = mentalTestQuestionService.list(
                Wrappers.<MentalTestQuestion>lambdaQuery()
                        .select(MentalTestQuestion::getId, MentalTestQuestion::getTitle)
                        .eq(MentalTestQuestion::getTopicId, mentalTestRecord.getTopicId())
        );
        List<MentalTestOption> optionAll = mentalTestOptionService.list(
                Wrappers.<MentalTestOption>lambdaQuery().eq(MentalTestOption::getTopicId, mentalTestRecord.getTopicId())
        );
 
 
        // 所有答题
        List<MentalTestResult> answerAll = mentalTestResultService.list(
                Wrappers.<MentalTestResult>lambdaQuery().eq(MentalTestResult::getAnswerNo, mentalTestRecord.getAnswerNo())
        );
 
        List<Long> questionIds = answerAll.stream().map(MentalTestResult::getQuestionId).distinct().collect(Collectors.toList());
 
        // 封装问题答案列表
        dto.setQaList(
                questionIds.stream().map(qid -> {
                    UserDetailMentalTestRecordNlwpDTO.QaDTO qaDTO = new UserDetailMentalTestRecordNlwpDTO.QaDTO();
 
                    qaDTO.setTitle(questionAll.stream()
                            .filter(q -> q.getId().equals(qid))
                            .findFirst().map(MentalTestQuestion::getTitle).orElse("")
                    );
 
                    qaDTO.setOptionList(answerAll.stream()
                            .filter(as -> as.getQuestionId().equals(qid))
                            .map(as -> {
                                UserDetailMentalTestRecordNlwpDTO.QaDTO.OptionDto optionDto = new UserDetailMentalTestRecordNlwpDTO.QaDTO.OptionDto();
 
                                MentalTestOption option = optionAll.stream()
                                        .filter(op -> as.getQuestionId().equals(qid) && as.getItemNo().equals(op.getItemNo()))
                                        .findFirst()
                                        .orElse(new MentalTestOption());
 
                                optionDto.setItemNo(option.getItemNo());
                                optionDto.setContent(option.getContent());
 
                                return optionDto;
                            })
                            .collect(Collectors.toList())
                    );
                    return qaDTO;
                }).collect(Collectors.toList())
        );
        return new SuccessResponseData<>(dto);
    }
 
    @ApiOperation("修改状态")
    @PostResource(name = "修改状态", path = "/updateStatus")
    @BusinessLog
    public ResponseData<?> updateStatus(String ids, Integer status) {
        if (StrUtil.isBlank(ids)){
            return new ErrorResponseData<>("500","id不能为空");
        }
        for (String id : ids.split(",")) {
            Customer o = new Customer();
            o.setCustomerId(Long.valueOf(id));
            o.setStatusFlag(status);
            customerService.updateCustomerRemoveCache(o);
        }
        return new SuccessResponseData<>();
    }
 
    @BusinessLog
    @ApiOperation("修改销售")
    @PostResource(name = "修改销售", path = "/updateClassConsultWorkerId")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户id", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "classWorkerId", value = "幸福顾问(课程销售)", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "consultWorkerId", value = "咨询顾问(咨询销售)", dataTypeClass = Long.class),
    })
    public ResponseData<?> updateClassConsultWorkerId(Long id, Long classWorkerId, Long consultWorkerId) {
        if (id == null){
            return new ErrorResponseData<>("500","id不能为空");
        }
 
        Customer o = new Customer();
        o.setCustomerId(Long.valueOf(id));
        if (classWorkerId != null) {
            o.setClassWorkerId(classWorkerId);
            //更新指定业务的顾问信息
            imGroupService.updateGroupUserForOrderinfo(classWorkerId,1,id);
        }
        if (consultWorkerId != null) {
            o.setConsultWorkerId(consultWorkerId);
            imGroupService.updateGroupUserForOrderinfo(consultWorkerId,2,id);
        }
        boolean update = customerService.updateCustomerRemoveCache(o);
 
 
        return new SuccessResponseData<>(update);
    }
 
    @ApiOperation("用户(选择)列表")
    @GetResource(name = "用户(选择)列表", path = "/userSelectList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "showId", value = "用户ID", dataTypeClass = String.class),
            @ApiImplicitParam(name = "nickName", value = "用户昵称", dataTypeClass = String.class),
    })
    public ResponseData<List<BaseIdNameDTO>> userSelectList(String showId, String nickName) {
        List<BaseIdNameDTO> list = customerService.list(
                Wrappers.<Customer>lambdaQuery()
                        .eq(Customer::getStatusFlag, StatusEnum.ENABLE.getCode())
                        .eq(Customer::getUserType, CustomerUserTypeEnum.USER.getCode())
                        .like(StrUtil.isNotBlank(showId), Customer::getShowId, showId)
                        .like(StrUtil.isNotBlank(nickName), Customer::getNickName, nickName)
        ).stream().map(
                o -> BaseIdNameDTO.builder()
                        .id(o.getCustomerId())
                        .name(o.getNickName())
                        .build()
        ).collect(Collectors.toList());
 
        return new SuccessResponseData(list);
    }
 
    @ApiOperation("性格分析1v1咨询预约记录(分页)")
    @GetResource(name = "性格分析1v1咨询预约记录(分页)", path = "/userMentalAppointmentPage")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "当前页数", dataTypeClass = Integer.class, defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "分页条数", dataTypeClass = Integer.class, defaultValue = "20"),
            @ApiImplicitParam(name = "userId", value = "用户ID", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "name", value = "咨询师姓名", dataTypeClass = String.class),
            @ApiImplicitParam(name = "telephone", value = "咨询师电话", dataTypeClass = String.class),
            @ApiImplicitParam(name = "statusFlag", value = "状态:1预约中,2服务中,3已完成,9已取消", dataTypeClass = Integer.class),
    })
    public ResponseData<PageResult<UserMentalAppointmentPageResponseDTO>> userMentalAppointmentPage(Integer pageNo, Integer pageSize, Long userId, String name, String telephone, Integer statusFlag) {
        Page<UserMentalAppointmentPageResponseDTO> page = mentalAppointmentService.userMentalAppointmentPage(
                PageFactory.page(pageNo, pageSize),
                userId,
                name,
                telephone,
                statusFlag
        );
        return new SuccessResponseData<>(PageResultFactory.createPageResult(page));
    }
 
 
    @ApiOperation("根据用户id分页查询预约记录")
    @GetResource(name = "根据用户id分页查询预约记录", path = "/counsellingInfo/getCounsellingOrderReservationByUserId", requiredPermission = false)
    public ResponseData<PageResult<CounsellingOrderReservationResponseDTO>> getCounsellingOrderReservationByUserId(UserCounsellingOrderReservationRequestDTO req) {
        // 根据用户id查询预约记录
        Page<CounsellingOrderReservation> page = this.counsellingOrderReservationService.findOrderReservationByUserIdPage(PageFactory.defaultPage(req), req);
        if (CollectionUtil.isNotEmpty(page.getRecords())){
            List<CounsellingOrderReservationResponseDTO> list = BeanUtil.copyToList(page.getRecords(),CounsellingOrderReservationResponseDTO.class);
            return new SuccessResponseData<>(PageResultFactory.createPageResult(list,page.getTotal(), Convert.toInt(page.getSize()),Convert.toInt(page.getCurrent())));
        }
        return new SuccessResponseData<>(PageResultFactory.createPageResult(Lists.newArrayList(),page.getTotal(), Convert.toInt(page.getSize()),Convert.toInt(page.getCurrent())));
    }
 
    @ApiOperation("根据用户id查询咨询课时统计")
    @GetResource(name = "根据用户id查询咨询课时统计", path = "/counsellingInfo/getCounsellingNumTotalByUserId", requiredPermission = false)
    public ResponseData<UserCounsellingTotalDTO> getCounsellingNumTotalByUserId(UserCounsellingOrderReservationRequestDTO userCounsellingOrderReservationRequestDTO) {
 
        return new SuccessResponseData<>(this.counsellingOrderReservationService.getCounsellingNumTotalByUserId(userCounsellingOrderReservationRequestDTO));
    }
}