LearningContentBoardController.java 9.79 KB
Newer Older
liqin's avatar
liqin committed
1
package cn.chnmuseum.party.web.controller;
liqin's avatar
liqin committed
2

liqin's avatar
liqin committed
3 4 5 6
import cn.chnmuseum.party.common.vo.GenericPageParam;
import cn.chnmuseum.party.model.LearningContentBoard;
import cn.chnmuseum.party.service.LearningContentBoardService;
import cn.chnmuseum.party.web.controller.base.BaseController;
liqin's avatar
liqin committed
7 8 9 10 11 12 13 14
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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 lombok.extern.slf4j.Slf4j;
wzp's avatar
wzp committed
15
import org.apache.shiro.authz.annotation.RequiresAuthentication;
16
import org.springframework.util.CollectionUtils;
liqin's avatar
liqin committed
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <pre>
 * 学习内容展板 前端控制器
 * </pre>
 *
 * @author Danny Lee
 * @since 2021-03-26
 */
@Slf4j
@RestController
@RequestMapping("/learningContentBoard")
@Api(tags = {"学习内容-展板列表操作接口"})
public class LearningContentBoardController extends BaseController {

    @Resource
    private LearningContentBoardService learningContentBoardService;

    @GetMapping("/getList")
wzp's avatar
wzp committed
41
    @RequiresAuthentication  //@RequiresPermissions("learning:content:board:list")
liqin's avatar
liqin committed
42 43 44 45 46 47 48 49 50 51 52 53 54 55
    @ApiOperation(value = "获取学习内容展板全部列表(无分页)", notes = "获取学习内容展板全部列表(无分页)")
    public Map<String, Object> getLearningContentBoardList(String learningContentId) {
        final List<LearningContentBoard> learningContentBoardList = learningContentBoardService.getBoardListByLearningContentId(learningContentId);
        return getResult(learningContentBoardList);
    }

    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "_index", value = "分页起始偏移量", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "_size", value = "返回条数", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "nameOrCode", value = "名称或编码", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "startDate", value = "创建时间-开始", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "endDate", value = "创建时间-结束", paramType = "query", dataType = "String")
    })
    @PostMapping("/getPageList")
wzp's avatar
wzp committed
56
    @RequiresAuthentication  //@RequiresPermissions("learning:content:board:page")
liqin's avatar
liqin committed
57 58 59 60 61 62 63 64
    @ApiOperation(value = "获取学习内容展板分页列表", notes = "获取学习内容展板分页列表")
    public Map<String, Object> getLearningContentBoardPageList(GenericPageParam genericPageParam) {
        LambdaQueryWrapper<LearningContentBoard> queryWrapper = new LambdaQueryWrapper<>();
        // 设置查询内容
        queryWrapper.select(
                LearningContentBoard::getId,
                LearningContentBoard::getLearningContentId,
                LearningContentBoard::getExhibitionBoardId);
liqin's avatar
liqin committed
65 66
        // 设置排序规则
        queryWrapper.orderByDesc(LearningContentBoard::getSortorder);
liqin's avatar
liqin committed
67 68 69 70 71 72 73 74 75 76 77 78 79
        Page<LearningContentBoard> page = this.learningContentBoardService.page(getPage(), queryWrapper);
        return getResult(page);
    }

    /**
     * 通用排序方法(拖拽排序/所有排序完成后点击保存)
     *
     * @param sourceId 源实体ID
     * @param targetId 目标实体ID
     * @return Void
     */
    @ApiOperation(value = "学习内容-展板排序")
    @PutMapping(value = "/sort")
wzp's avatar
wzp committed
80
    @RequiresAuthentication  //@RequiresPermissions("learning:content:board:sort")
liqin's avatar
liqin committed
81 82 83
    public Map<String, Object> sort(String sourceId, String targetId) {
        LearningContentBoard theSource = this.learningContentBoardService.getById(sourceId);
        LearningContentBoard theTarget = this.learningContentBoardService.getById(targetId);
liqin's avatar
liqin committed
84
        String moveType = theSource.getSortorder() > theTarget.getSortorder() ? "down" : "up";
liqin's avatar
liqin committed
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
        Integer sourceSortorder = theSource.getSortorder();
        Integer targetSortorder = theTarget.getSortorder();

        //默认sortorder为降序排序,向上移动
        if ("up".equalsIgnoreCase(moveType) && sourceSortorder < targetSortorder) {
            QueryWrapper<LearningContentBoard> wrapper = new QueryWrapper<>();
            wrapper.between("sortorder", sourceSortorder, targetSortorder);
            wrapper.select("id", "sortorder");
            List<LearningContentBoard> list = this.learningContentBoardService.list(wrapper);
            for (LearningContentBoard entity : list) {
                if (!entity.getId().equals(sourceId)) {
                    entity.setSortorder(entity.getSortorder() - 1);
                    this.learningContentBoardService.updateById(entity);
                }
            }
liqin's avatar
liqin committed
100 101
            theSource.setSortorder(targetSortorder);
            this.learningContentBoardService.updateById(theSource);
liqin's avatar
liqin committed
102 103 104 105 106 107 108 109 110 111 112 113 114
        }
        //默认sortorder为降序排序,向下移动
        else if ("down".equalsIgnoreCase(moveType) && sourceSortorder > targetSortorder) {
            QueryWrapper<LearningContentBoard> wrapper = new QueryWrapper<>();
            wrapper.between("sortorder", targetSortorder, sourceSortorder);
            wrapper.select("id", "sortorder");
            List<LearningContentBoard> slideList = this.learningContentBoardService.list(wrapper);
            for (LearningContentBoard entity : slideList) {
                if (!entity.getId().equals(sourceId)) {
                    entity.setSortorder(entity.getSortorder() + 1);
                    this.learningContentBoardService.updateById(entity);
                }
            }
liqin's avatar
liqin committed
115 116
            theSource.setSortorder(targetSortorder);
            this.learningContentBoardService.updateById(theSource);
liqin's avatar
liqin committed
117 118 119 120 121 122 123 124 125 126 127 128 129
        }
        //默认sortorder为正序排序,向下移动
        else if ("down".equalsIgnoreCase(moveType) && sourceSortorder < targetSortorder) {
            QueryWrapper<LearningContentBoard> wrapper = new QueryWrapper<>();
            wrapper.between("sortorder", sourceSortorder, targetSortorder);
            wrapper.select("id", "sortorder");
            List<LearningContentBoard> slideList = this.learningContentBoardService.list(wrapper);
            for (LearningContentBoard slide : slideList) {
                if (!slide.getId().equals(sourceId)) {
                    slide.setSortorder(slide.getSortorder() - 1);
                    this.learningContentBoardService.updateById(slide);
                }
            }
liqin's avatar
liqin committed
130 131
            theSource.setSortorder(targetSortorder);
            this.learningContentBoardService.updateById(theSource);
liqin's avatar
liqin committed
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
        }
        //默认sortorder为正序排序,向上移动
        else if ("up".equalsIgnoreCase(moveType) && sourceSortorder > targetSortorder) {
            QueryWrapper<LearningContentBoard> wrapper = new QueryWrapper<>();
            wrapper.between("sortorder", targetSortorder, sourceSortorder);
            wrapper.select("id", "sortorder");
            List<LearningContentBoard> slideList = this.learningContentBoardService.list(wrapper);
            for (LearningContentBoard slide : slideList) {
                if (!slide.getId().equals(sourceId)) {
                    slide.setSortorder(slide.getSortorder() + 1);
                    this.learningContentBoardService.updateById(slide);
                }
            }
            theSource.setSortorder(targetSortorder);
            this.learningContentBoardService.updateById(theSource);
        }
liqin's avatar
liqin committed
148
        return getSuccessResult();
liqin's avatar
liqin committed
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

    /**
     * 修改后的接口
     * 通用排序方法(拖拽排序/所有排序完成后点击保存)
     *
     * @param sourceId 源实体ID
     * @param targetId 目标实体ID
     * @return Void
     */
    @ApiOperation(value = "根据学习内容id-展板排序")
    @PutMapping(value = "/sortById")
    @ApiImplicitParams(value = {

            @ApiImplicitParam(name = "id", value = "学习内容id", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "sourceId", value = "展板源id", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "targetId", value = "展板目标id", paramType = "query", dataType = "String")
    })
    @RequiresAuthentication  //@RequiresPermissions("learning:content:board:sort")
    public Map<String, Object> sortById(String id, String sourceId, String targetId) {

        LambdaQueryWrapper<LearningContentBoard> sourceEq = new QueryWrapper<LearningContentBoard>().lambda()
                .eq(LearningContentBoard::getLearningContentId, id)
                .eq(LearningContentBoard::getExhibitionBoardId, sourceId);
        //
        LambdaQueryWrapper<LearningContentBoard> targetEq = new QueryWrapper<LearningContentBoard>().lambda()
                .eq(LearningContentBoard::getLearningContentId, id)
                .eq(LearningContentBoard::getExhibitionBoardId, targetId);

        List<LearningContentBoard> theSource = this.learningContentBoardService.list(sourceEq);
        List<LearningContentBoard> theTarget = this.learningContentBoardService.list(targetEq);

        if (CollectionUtils.isEmpty(theSource) && CollectionUtils.isEmpty(theTarget)) {
            return getFailResult("排序展板不存在");
        }
        //排序字段互换
186 187
        Integer sortorderSource = theSource.get(0).getSortorder();
        Integer sortorderTarget = theTarget.get(0).getSortorder();
188 189

        theSource.stream().forEach(s -> {
190
            s.setSortorder(sortorderTarget);
191 192 193
        });

        theTarget.stream().forEach(t -> {
194
            t.setSortorder(sortorderSource);
195 196 197 198 199 200 201 202
        });

        boolean batch = learningContentBoardService.updateBatchById(theSource);
        boolean batch1 = learningContentBoardService.updateBatchById(theTarget);

        return getSuccessResult();

    }
liqin's avatar
liqin committed
203 204
}