guyue
6 天以前 1b6b900d88e109e5d1fe7a89d4c087148db4fd9d
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
package com.linghu.controller;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.linghu.mapper.KeywordMapper;
import com.linghu.mapper.ReferenceMapper;
import com.linghu.model.common.ResponseResult;
import com.linghu.model.entity.Keyword;
import com.linghu.model.entity.Reference;
import com.linghu.model.entity.Type;
import com.linghu.service.KeywordService;
import com.linghu.service.TypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
 
@RestController
    @RequestMapping("/type")
@Api(value = "类型相关接口", tags = "设置-类型")
public class TypeController {
 
    @Autowired
    private TypeService typeService;
    @Autowired
    private KeywordMapper keywordMapper;
    @Autowired
    private ReferenceMapper referenceMapper;
 
    @PostMapping
    @ApiOperation(value = "添加类型")
    public ResponseResult<Type> add(@RequestBody Type type) {
        boolean success = typeService.save(type);
        if (success) {
            return ResponseResult.success(type);
        }
        return ResponseResult.error("添加类型失败");
    }
 
    @PostMapping("/batch")
    @ApiOperation(value = "批量添加类型")
    public ResponseResult<Void> batchAdd(@RequestBody List<Type> types) {
        boolean success = typeService.saveBatch(types);
        if (success) {
            return ResponseResult.success();
        }
        return ResponseResult.error("批量添加类型失败");
    }
 
    @DeleteMapping("/{typeId}")
    @ApiOperation(value = "删除类型")
    public ResponseResult<Void> delete(@PathVariable Integer typeId) {
 
        typeService.removeById(typeId);
 
        return ResponseResult.success();
    }
 
    @PutMapping
    @ApiOperation(value = "更新类型")
    public ResponseResult<Void> update(@RequestBody Type type) {
        typeService.updateById(type);
        return ResponseResult.success();
    }
 
    @PutMapping("/batch")
    @ApiOperation(value = "批量更新类型")
    public ResponseResult<Void> batchUpdate(@RequestBody List<Type> types) {
        boolean success = typeService.updateBatchById(types);
        if (success) {
            return ResponseResult.success();
        }
        return ResponseResult.error("批量更新类型失败");
    }
 
    @GetMapping("/{typeId}")
    @ApiOperation(value = "根据ID查询类型")
    public ResponseResult<Type> getById(@PathVariable Integer typeId) {
        Type type = typeService.getById(typeId);
        if (type != null ) {
            return ResponseResult.success(type);
        }
        return ResponseResult.error("类型不存在");
    }
 
    @DeleteMapping("/batch")
    @ApiOperation(value = "批量删除类型")
    public ResponseResult<Void> batchDelete(@RequestBody List<Integer> typeIds) {
        List<Type> types = typeIds.stream().map(id -> {
            Type type = new Type();
            type.setType_id(id);
            return type;
        }).collect(java.util.stream.Collectors.toList());
        boolean success = typeService.updateBatchById(types);
        if (success) {
            return ResponseResult.success();
        }
        return ResponseResult.error("批量删除类型失败");
    }
 
 
    @GetMapping("/list")
    @ApiOperation(value = "查询类型列表,不传页数和大小就查全部")
    public ResponseResult<List<Type>> list(
            @RequestParam(required = false) Integer page,
            @RequestParam(required = false) Integer pageSize,
            @RequestParam(required = false) Integer keywordId,
            @RequestParam(required = false) Integer questionId,
            @RequestParam(required = false) Integer isNow
            ) {
        List<Integer> typeIds=new ArrayList<>();
        Keyword keyword=new Keyword();
        //先查找当前关键词下,所有的回答 的 所有的平台名称
        if (keywordId != null) {
             keyword = keywordMapper.selectById(keywordId);
        }
 
 
            typeIds= keywordMapper.getTypeIds(keywordId,questionId,isNow,keyword);
 
 
 
     /*   if (keywordId != null && questionId == null) {
 
            List<Reference> references = referenceMapper.selectList(new LambdaQueryWrapper<Reference>()
                    .eq(Reference::getKeyword_id, keywordId)
                    .eq(Reference::getNum,isNow == 0 ? 1 : keyword.getNum())
            );
            typeIds = references.stream().map(Reference::getType_id).filter(Objects::nonNull).distinct().collect(Collectors.toList());
        }
        if (questionId != null) {
            List<Reference> references = referenceMapper.selectList(new LambdaQueryWrapper<Reference>()
                    .eq(Reference::getKeyword_id, keywordId)
                    .eq(Reference::getNum, isNow == 0 ? 1 : keyword.getNum())
                    .eq(Reference::getQuestion_id, questionId)
            );
            typeIds = references.stream().map(Reference::getType_id).filter(Objects::nonNull).distinct().collect(Collectors.toList());
        }
*/
        LambdaQueryWrapper<Type> queryWrapper = new LambdaQueryWrapper<>();
        if (!typeIds.isEmpty()) {
            queryWrapper.in(Type::getType_id, typeIds);
        }else {
            if (keywordId != null) {
                return ResponseResult.success(new ArrayList<>());
 
            }else {
                List<Type> list = typeService.list(queryWrapper);
                return ResponseResult.success(list);
            }
 
        }
 
 
            List<Type> list = typeService.list(queryWrapper);
            return ResponseResult.success(list);
 
    }
}