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
package cn.stylefeng.guns.modular.business.controller;
 
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.StrUtil;
import cn.stylefeng.guns.modular.business.dto.BaseIdNameDTO;
import cn.stylefeng.guns.modular.business.dto.MentalTestQuestionOptionDTO;
import cn.stylefeng.guns.modular.business.dto.MentalTestTopicDTO;
import cn.stylefeng.guns.modular.business.dto.request.MentalTestTopicAddUpdateRequest;
import cn.stylefeng.guns.modular.business.entity.MentalTestClass;
import cn.stylefeng.guns.modular.business.entity.MentalTestResultSet;
import cn.stylefeng.guns.modular.business.entity.MentalTestTopic;
import cn.stylefeng.guns.modular.business.service.IMentalTestClassService;
import cn.stylefeng.guns.modular.business.service.IMentalTestResultSetService;
import cn.stylefeng.guns.modular.business.service.IMentalTestTopicService;
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.DeleteEnum;
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.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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.*;
 
import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
 
/**
 * 心理测试题库管理类
 *
 * @author guo
 */
@RestController
@Api(tags = "心理测试题库管理类")
@ApiResource(name = "心理测试题库管理类", resBizType = ResBizTypeEnum.BUSINESS)
@RequestMapping("/business")
public class MentalTestTopicController {
 
    @Resource
    private IMentalTestClassService mentalTestClassService;
 
    @Resource
    private IMentalTestTopicService mentalTestTopicService;
 
    @Resource
    private IMentalTestResultSetService mentalTestResultSetService;
 
    @ApiOperation("题库类型(选择)列表")
    @GetResource(name = "题库类型(选择)列表", path = "/classSelectList")
    public ResponseData<List<MentalTestClass>> classSelectList() {
        List<MentalTestClass> list = mentalTestClassService.list(
                Wrappers.<MentalTestClass>lambdaQuery()
                        .eq(MentalTestClass::getIsDelete, DeleteEnum.NOT_DELETE.getCode())
                        .eq(MentalTestClass::getStatusFlag, StatusEnum.ENABLE.getCode())
        );
 
        return new SuccessResponseData(list);
    }
 
    @ApiOperation("题库(选择)列表")
    @GetResource(name = "题库(选择)列表", path = "/topicSelectList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "title", value = "标题", dataTypeClass = String.class),
            @ApiImplicitParam(name = "testType", value = "测试类型:0免费,1付费", dataTypeClass = Integer.class),
    })
    public ResponseData<List<BaseIdNameDTO>> topicSelectList(String title, Integer testType) {
        List<BaseIdNameDTO> list = mentalTestTopicService.list(
                Wrappers.<MentalTestTopic>lambdaQuery()
                        .eq(MentalTestTopic::getIsDelete, DeleteEnum.NOT_DELETE.getCode())
                        .like(StrUtil.isNotBlank(title), MentalTestTopic::getTitle, title)
                        .eq(testType != null, MentalTestTopic::getTestType, testType)
        ).stream().map(
                o -> BaseIdNameDTO.builder()
                        .id(o.getId())
                        .name(o.getTitle())
                        .build()
        ).collect(Collectors.toList());
 
        return new SuccessResponseData(list);
    }
 
    /**
     * 添加心理测试题库
     */
    @ApiOperation("添加心理测试题库")
    @PostResource(name = "添加心理测试题库", path = "/mentalTestTopic/add")
    @BusinessLog
    public ResponseData<?> add(@RequestBody MentalTestTopicAddUpdateRequest req) {
        this.mentalTestTopicService.saveTopic(req);
        return new SuccessResponseData<>();
    }
 
    /**
     * 删除心理测试题库
     */
    @ApiOperation("删除心理测试题库")
    @PostResource(name = "删除心理测试题库", path = "/mentalTestTopic/delete")
    @BusinessLog
    public ResponseData<?> delete(@RequestParam String ids) {
        if (StrUtil.isBlank(ids)) {
            return new ErrorResponseData<>("500", "id不能为空");
        }
        LambdaUpdateWrapper<MentalTestTopic> lambdaUpdateWrapper = new LambdaUpdateWrapper<MentalTestTopic>().set(MentalTestTopic::getIsDelete, true);
        lambdaUpdateWrapper.in(MentalTestTopic::getId, ListUtil.toList(ids.split(",")));
        this.mentalTestTopicService.update(lambdaUpdateWrapper);
        return new SuccessResponseData<>();
    }
 
    /**
     * 修改心理测试题库
     */
    @ApiOperation(value = "修改心理测试题库")
    @PostResource(name = "修改心理测试题库", path = "/mentalTestTopic/edit")
    @BusinessLog
    public ResponseData<?> edit(@RequestBody MentalTestTopicAddUpdateRequest req) {
        // 用户购买数量
        MentalTestTopic mentalTestTopic = mentalTestTopicService.getById(req.getId());
        if (mentalTestTopic != null && mentalTestTopic.getTestPeopleNum() > 0) {
            // 仅修改标题
            this.mentalTestTopicService.update(
                    Wrappers.<MentalTestTopic>lambdaUpdate()
                            .set(MentalTestTopic::getTitle, req.getTitle())
                            .eq(MentalTestTopic::getId, req.getId())
            );
            return new SuccessResponseData<>("用户已购买仅标题修改成功");
        } else {
            // 全量修改
            this.mentalTestTopicService.editTopic(req);
            return new SuccessResponseData<>();
        }
    }
 
    /**
     * 修改心理测试题库状态
     */
    @ApiOperation("修改心理测试题库状态")
    @PostResource(name = "修改心理测试题库状态", path = "/mentalTestTopic/updateStatus")
    @BusinessLog
    public ResponseData<?> updateStatus(String ids, Integer status) {
        if (StrUtil.isBlank(ids)) {
            return new ErrorResponseData<>("500", "id不能为空");
        }
        LambdaUpdateWrapper<MentalTestTopic> lambdaUpdateWrapper = new LambdaUpdateWrapper<MentalTestTopic>().set(MentalTestTopic::getStatusFlag, status);
        lambdaUpdateWrapper.in(MentalTestTopic::getId, ListUtil.toList(ids.split(",")));
        this.mentalTestTopicService.update(lambdaUpdateWrapper);
        return new SuccessResponseData<>();
    }
 
    /**
     * 获取心理测试题库详情
     */
    @ApiOperation("获取心理测试题库详情")
    @GetResource(name = "获取心理测试题库详情", path = "/mentalTestTopic/detail/{id}", requiredPermission = false)
    public ResponseData<MentalTestTopicDTO> detail(@PathVariable("id") Long id) {
        // 心理测试题库信息
        MentalTestTopic mentalTestTopic = this.mentalTestTopicService.getById(id);
        MentalTestTopicDTO dto = BeanUtil.toBean(mentalTestTopic, MentalTestTopicDTO.class);
 
        // 获取心理测试问题、选项
        List<MentalTestQuestionOptionDTO> questionList = mentalTestTopicService.getQuestionOptionByTopicId(Long.valueOf(id));
        dto.setQuestionList(questionList);
 
        // 获取结果区间配置
        List<MentalTestResultSet> resultSetList = mentalTestResultSetService.list(
                Wrappers.<MentalTestResultSet>lambdaQuery()
                        .eq(MentalTestResultSet::getTopicId, id)
        );
        dto.setResultSetList(resultSetList);
 
        return new SuccessResponseData<>(dto);
    }
 
    /**
     * 获取心理测试题库列表
     */
    @ApiOperation("获取心理测试题库列表")
    @GetResource(name = "获取心理测试题库列表", path = "/mentalTestTopic/list", requiredPermission = false)
    public ResponseData<List<MentalTestTopic>> list(MentalTestTopic mentalTestTopic) {
        LambdaQueryWrapper<MentalTestTopic> lambdaQueryWrapper = new LambdaQueryWrapper<MentalTestTopic>()
                .eq(MentalTestTopic::getIsDelete, DeleteEnum.NOT_DELETE.getCode())
                .eq(MentalTestTopic::getStatusFlag, mentalTestTopic.getStatusFlag() == null ? StatusEnum.ENABLE.getCode() : mentalTestTopic.getStatusFlag())
                .eq(StrUtil.isNotBlank(mentalTestTopic.getTitle()), MentalTestTopic::getTitle, mentalTestTopic.getTitle())
                .eq(mentalTestTopic.getTestType() != null, MentalTestTopic::getTestType, mentalTestTopic.getTestType())
                .eq(mentalTestTopic.getConsultOne() != null, MentalTestTopic::getConsultOne, mentalTestTopic.getConsultOne())
                .eq(mentalTestTopic.getResultCalculateMode() != null, MentalTestTopic::getResultCalculateMode, mentalTestTopic.getResultCalculateMode())
                .orderByDesc(MentalTestTopic::getId);
        List<MentalTestTopic> list = mentalTestTopicService.list(lambdaQueryWrapper);
        return new SuccessResponseData<>(list.stream()
                .filter(o -> {
                    boolean flag = true;
                    if (StrUtil.isNotBlank(mentalTestTopic.getClassId())) {
                        flag = false;
                        if (StrUtil.isNotBlank(o.getClassId())) {
                            String[] idArr = o.getClassId().split(",");
                            List<String> reqIdList = Arrays.stream(mentalTestTopic.getClassId().split(",")).collect(Collectors.toList());
                            flag = Stream.of(idArr).anyMatch(reqIdList::contains);
                        }
                    }
                    return flag;
                })
                .collect(Collectors.toList())
        );
    }
 
    /**
     * 获取心理测试题库列表(分页)
     */
    @ApiOperation("获取心理测试题库列表(分页)")
    @GetResource(name = "获取心理测试题库列表(分页)", path = "/mentalTestTopic/page", requiredPermission = false)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "分页:第几页(从1开始)", dataTypeClass = Integer.class, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页:每页大小(默认20)", dataTypeClass = Integer.class, paramType = "query"),
            @ApiImplicitParam(name = "title", value = "测试标题", dataTypeClass = String.class, paramType = "query"),
            @ApiImplicitParam(name = "statusFlag", value = "状态:1正常,2下架", dataTypeClass = Integer.class, paramType = "query"),
    })
    public ResponseData<PageResult<MentalTestTopic>> page(Integer pageNo, Integer pageSize, String title, Integer statusFlag) {
        LambdaQueryWrapper<MentalTestTopic> lambdaQueryWrapper = new LambdaQueryWrapper<MentalTestTopic>()
                .eq(MentalTestTopic::getIsDelete, DeleteEnum.NOT_DELETE.getCode())
                .like(StrUtil.isNotBlank(title), MentalTestTopic::getTitle, title)
                .eq(statusFlag != null && statusFlag != -1, MentalTestTopic::getStatusFlag, statusFlag)
                .orderByDesc(MentalTestTopic::getId);
        Page<MentalTestTopic> page = this.mentalTestTopicService.page(PageFactory.page(pageNo, pageSize), lambdaQueryWrapper);
        return new SuccessResponseData<>(PageResultFactory.createPageResult(page));
    }
 
}