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
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.request.BannerAddUpdateRequest;
import cn.stylefeng.guns.modular.business.entity.Banner;
import cn.stylefeng.guns.modular.business.entity.CounsellingInfo;
import cn.stylefeng.guns.modular.business.entity.Course;
import cn.stylefeng.guns.modular.business.entity.MentalTestTopic;
import cn.stylefeng.guns.modular.business.service.IBannerService;
import cn.stylefeng.guns.modular.business.service.ICounsellingInfoService;
import cn.stylefeng.guns.modular.business.service.ICourseService;
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.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.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.List;
import java.util.stream.Collectors;
 
/**
 * banner管理类
 * @author guo
 */
@RestController
@Api(tags = "banner管理类")
@ApiResource(name = "banner管理类", resBizType = ResBizTypeEnum.BUSINESS)
@RequestMapping("/business")
public class BannerController  {
 
    @Resource
    private IBannerService bannerService;
 
    @Resource
    private IMentalTestTopicService mentalTestTopicService;
 
    @Resource
    private ICourseService courseService;
 
    @Resource
    private ICounsellingInfoService counsellingInfoService;
 
    /**
     * 添加banner
     */
    @ApiOperation("添加banner")
    @PostResource(name = "添加banner", path = "/banner/add")
    @BusinessLog
    public ResponseData<?> add(@RequestBody BannerAddUpdateRequest req) {
        // 验证ID是否有效
        ResponseData responseData = checkIdIsValid(req.getJumpModule(), req.getJumpPage(), req.getJumpId());
        if (responseData != null) {
            return responseData;
        }
 
        Banner banner = BeanUtil.toBean(req, Banner.class);
        this.bannerService.save(banner);
        return new SuccessResponseData<>();
    }
 
    /**
     * 验证ID是否有效
     */
    public ResponseData checkIdIsValid(Integer jumpModule, Integer jumpPage, Long jumpId) {
        // 跳转,详情页
        if (jumpModule != null && jumpPage != null && jumpPage == 2) {
            // 跳转模块:0不跳转,1心理测试,2课程,3咨询
            if (jumpModule == 1) {
                // 1心理测试
                MentalTestTopic o = mentalTestTopicService.getById(jumpId);
                if (o == null || o.getStatusFlag() != StatusEnum.ENABLE.getCode()) {
                    return new ErrorResponseData<>("心理测试跳转ID无效!");
                }
            } else if (jumpModule == 2) {
                // 2课程
                Course o = courseService.getById(jumpId);
                if (o == null || o.getListingStatus() != StatusEnum.ENABLE.getCode()) {
                    return new ErrorResponseData<>("课程跳转ID无效!");
                }
            } else if (jumpModule == 3) {
                // 3咨询
                CounsellingInfo o = counsellingInfoService.getById(jumpId);
                if (o == null || o.getListingStatus() != StatusEnum.ENABLE.getCode()) {
                    return new ErrorResponseData<>("咨询跳转ID无效!");
                }
            }
        }
        return null;
    }
 
    /**
     * 删除banner
     */
    @ApiOperation("删除banner")
    @PostResource(name = "删除banner", path = "/banner/delete")
    @BusinessLog
    public ResponseData<?> delete(@RequestParam String ids) {
        if (StrUtil.isBlank(ids)){
            return new ErrorResponseData<>("500","id不能为空");
        }
        LambdaUpdateWrapper<Banner> lambdaUpdateWrapper = new LambdaUpdateWrapper<Banner>().set(Banner::getIsDelete,true);
        lambdaUpdateWrapper.in(Banner::getId, ListUtil.toList(ids.split(",")));
        this.bannerService.update(lambdaUpdateWrapper);
        return new SuccessResponseData<>();
    }
 
    /**
     * 修改banner
     */
    @ApiOperation("修改banner")
    @PostResource(name = "修改banner", path = "/banner/edit")
    @BusinessLog
    public ResponseData<?> edit(@RequestBody BannerAddUpdateRequest req) {
        // 验证ID是否有效
        ResponseData responseData = checkIdIsValid(req.getJumpModule(), req.getJumpPage(), req.getJumpId());
        if (responseData != null) {
            return responseData;
        }
        Banner banner = BeanUtil.toBean(req, Banner.class);
        this.bannerService.updateById(banner);
        return new SuccessResponseData<>();
    }
 
    /**
     * 修改banner状态
     */
    @ApiOperation("修改banner状态")
    @PostResource(name = "修改banner状态", path = "/banner/updateStatus")
    @BusinessLog
    public ResponseData<?> updateStatus(String ids, Integer status) {
        if (StrUtil.isBlank(ids)){
            return new ErrorResponseData<>("500","id不能为空");
        }
        LambdaUpdateWrapper<Banner> lambdaUpdateWrapper = new LambdaUpdateWrapper<Banner>().set(Banner::getStatusFlag,status);
        lambdaUpdateWrapper.in(Banner::getId, ListUtil.toList(ids.split(",")));
        this.bannerService.update(lambdaUpdateWrapper);
        return new SuccessResponseData<>();
    }
 
    /**
     * 获取banner详情
     */
    @ApiOperation("获取banner详情")
    @GetResource(name = "获取banner详情", path = "/banner/detail/{id}", requiredPermission = false)
    public ResponseData<Banner> detail(@PathVariable("id") Long id) {
        Banner banner = this.bannerService.getById(id);
        return new SuccessResponseData<>(bannerService.wrapperBannerByJumpModule(banner));
    }
 
    /**
     * 获取banner列表
     */
    @ApiOperation("获取banner列表")
    @GetResource(name = "获取banner列表", path = "/banner/list", requiredPermission = false)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "banner名称", dataTypeClass = String.class),
            @ApiImplicitParam(name = "jumpModule", value = "跳转模块:0不跳转,1心理测试,2课程,3咨询", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "statusFlag", value = "状态:1上架,2下架", dataTypeClass = Integer.class),
    })
    public ResponseData<List<Banner>> list(String name, Integer jumpModule, Integer statusFlag) {
        LambdaQueryWrapper<Banner> lambdaQueryWrapper = new LambdaQueryWrapper<Banner>().eq(Banner::getIsDelete, false)
                .eq(StrUtil.isNotBlank(name), Banner::getName, name)
                .eq(jumpModule != null, Banner::getJumpModule, jumpModule)
                .eq(statusFlag != null, Banner::getStatusFlag, statusFlag)
                .orderByDesc(Banner::getId);
        return new SuccessResponseData<>(
                bannerService.list(lambdaQueryWrapper)
                        .stream()
                        .map(banner -> bannerService.wrapperBannerByJumpModule(banner)).collect(Collectors.toList())
        );
    }
 
    /**
     * 获取banner列表(分页)
     */
    @ApiOperation("获取banner列表(分页)")
    @GetResource(name = "获取banner列表(分页)", path = "/banner/page", requiredPermission = false)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "当前页数", dataTypeClass = Integer.class, defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "分页条数", dataTypeClass = Integer.class, defaultValue = "20"),
            @ApiImplicitParam(name = "name", value = "banner名称", dataTypeClass = String.class),
            @ApiImplicitParam(name = "jumpModule", value = "跳转模块:0不跳转,1心理测试,2课程,3咨询", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "jumpPage", value = "跳转页面,1主页,2详情页", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "statusFlag", value = "状态:1上架,2下架", dataTypeClass = Integer.class),
    })
    public ResponseData<PageResult<Banner>> page(Integer pageNo, Integer pageSize, String name, Integer jumpModule, Integer jumpPage, Integer statusFlag) {
        LambdaQueryWrapper<Banner> lambdaQueryWrapper = new LambdaQueryWrapper<Banner>().eq(Banner::getIsDelete, false)
                .eq(StrUtil.isNotBlank(name), Banner::getName, name)
                .eq(jumpModule != null, Banner::getJumpModule, jumpModule)
                .eq(jumpPage != null, Banner::getJumpPage, jumpPage)
                .eq(statusFlag != null, Banner::getStatusFlag, statusFlag)
                .orderByDesc(Banner::getId);
        Page<Banner> page = this.bannerService.page(PageFactory.page(pageNo, pageSize), lambdaQueryWrapper);
        page.getRecords().forEach(banner -> bannerService.wrapperBannerByJumpModule(banner));
        return new SuccessResponseData<>(PageResultFactory.createPageResult(page));
    }
 
}