controller.java.vm 11.9 KB
Newer Older
liqin's avatar
liqin committed
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 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
package ${package.Controller};

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
#if(${superControllerClassPackage})
import com.wisenergy.DigDataStation.common.controller.BaseController;
#end
import ${package.Entity}.${entity};
import ${package.Service}.${table.serviceName};
import com.wisenergy.DigDataStation.utils.Exception.InterfaceException;
import com.wisenergy.DigDataStation.utils.Exception.RESPONSE_CODE_ENUM;
#if(${swagger2})

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

#end
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
#if(${restControllerStyle})
import org.springframework.web.bind.annotation.RestController;
#else
import org.springframework.stereotype.Controller;
#end

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

/**
 * <pre>
 * $!{table.comment} 前端控制器
 * </pre>
 *
 * @author ${author}
 * @since ${date}
 */
@Slf4j
#if(${restControllerStyle})
@RestController
#else
@Controller
#end
@RequestMapping("/interface/${table.entityPath}")
@Api(tags = {"$!{table.comment}操作接口"})
#if(${kotlin})
class ${table.controllerName}#if(${superControllerClass}) : ${superControllerClass}()#end
#else
#if(${superControllerClass})
public class ${table.controllerName} extends ${superControllerClass} {
#else
public class ${table.controllerName} {
#end

    @Resource
    private ${table.serviceName} ${table.entityPath}Service;

    /**
     * 批量新增业务系统节点
     *
     * @return 操作结果
     */
    @PostMapping("/batchSave")
	#if(${cfg.requiresPermissions})
	@RequiresPermissions("$!{cfg.colonTableName}:batch:save")
	#end
    public Map<String, Object> batchSave${entity}(#if(${cfg.paramValidation})@Validated#end @RequestBody List<${entity}> ${table.entityPath}List) {
        // 保存业务节点信息
        boolean result = ${table.entityPath}Service.saveBatch(${table.entityPath}List);
        // 返回操作结果
        if (result) {
            return getSuccessResult();
        } else {
            // 保存失败
            return getFailResult();
        }
    }

    /**
     * 添加$!{table.comment}
     */
    @PostMapping("/save")
	#if(${cfg.requiresPermissions})
	@RequiresPermissions("$!{cfg.colonTableName}:save")
	#end
	@ApiOperation(value = "添加${entity}对象", notes = "添加$!{table.comment}")
	public Map<String, Object> save${entity}(#if(${cfg.paramValidation})@Validated#end ${entity} ${table.entityPath}) {
        // 保存业务节点信息
        boolean result = ${table.entityPath}Service.save(${table.entityPath});
        // 返回操作结果
        if (result) {
            return getSuccessResult();
        } else {
            // 保存失败
            return getFailResult();
        }
    }

    /**
     * 重命名业务节点
     *
     * @param id      业务节点ID
     * @param newName 业务节点新名称
     * @return 操作结果
     */
    @PutMapping("/rename")
	#if(${cfg.requiresPermissions})
	@RequiresPermissions("$!{cfg.colonTableName}:rename")
	#end
    public Map<String, Object> rename(Long id, String newName) {
        ${entity} entity = new ${entity}();
        entity.setId(id);
        entity.set${entity}Name(newName);
        boolean result = ${table.entityPath}Service.updateById(entity);
        if (result) {
            // 返回操作结果
            return getSuccessResult();
        } else {
            // 修改失败,业务节点名称重复
            return getFailResult(RESPONSE_CODE_ENUM.CREATE_FLOW_NODE_ERROR.getCode(), RESPONSE_CODE_ENUM.CREATE_FLOW_NODE_ERROR.getMsg());
        }
    }

    /**
     * 修改$!{table.comment}
     */
    @PutMapping("/update")
	#if(${cfg.requiresPermissions})
	@RequiresPermissions("$!{cfg.colonTableName}:update")
	#end
    @ApiOperation(value = "修改${entity}对象", notes = "修改$!{table.comment}")
    public Map<String, Object> update${entity}(#if(${cfg.paramValidation})@Validated#end @RequestBody ${entity} ${cfg.entityObjectName}) {
    #if(${cfg.generatorStrategy} == 'ALL')
        boolean flag = ${table.entityPath}Service.update${entity}(${cfg.entityObjectName});
    #else
        boolean flag = ${table.entityPath}Service.updateById(${cfg.entityObjectName});
    #end
        if (flag) {
            return getSuccessResult();
        }
        return getFailResult();
    }

	/**
	 * 删除$!{table.comment}
	 */
    @DeleteMapping("/delete/{id}")
	#if(${cfg.requiresPermissions})
	@RequiresPermissions("$!{cfg.colonTableName}:delete")
	#end
	@ApiOperation(value = "删除${entity}对象", notes = "删除$!{table.comment}")
	@ApiImplicitParams(value = {
			@ApiImplicitParam(name = "id", value = "对象标识ID", paramType = "path", dataType = "Long")
	})
    public Map<String, Object> delete${entity}(@PathVariable("id") Long id) {
        boolean result = ${table.entityPath}Service.removeById(id);
        if (result) {
            return getSuccessResult();
        }
        return getFailResult();
    }

    @GetMapping("/getList")
	#if(${cfg.requiresPermissions})
	@RequiresPermissions("$!{cfg.colonTableName}:list")
	#end
	@ApiOperation(value = "获取${entity}列表", notes = "$!{table.comment}列表")
    public Map<String, Object> get${entity}List() {
        // 通过业务流程ID和业务ID获取未删除业务节点集合
        List<${entity}> ${table.entityPath}s = ${table.entityPath}Service.list();
        // 初始化返回对象
        Map<String, Object> map = new HashMap<>();
        map.put("data", ${table.entityPath}s);
        return getResult(map);
    }

	/**
	 * $!{table.comment}分页列表
	 */
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "_index", value = "分页起始偏移量", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "_size", value = "返回条数", paramType = "query", dataType = "Integer")}
    )
    @PostMapping("/getPageList")
	#if(${cfg.requiresPermissions})
	@RequiresPermissions("$!{cfg.colonTableName}:page")
	#end
	@ApiOperation(value = "获取${entity}分页列表", notes = "$!{table.comment}分页列表")
    public Map<String, Object> get${entity}PageList() {
        Page<${entity}> page = getPage();
        QueryWrapper<${entity}> categoryWrapper = new QueryWrapper<>();
        categoryWrapper.orderByDesc("SORT");
        IPage<${entity}> categoryPage = this.${table.entityPath}Service.page(page, categoryWrapper);

        // 初始化返回对象
        Map<String, Object> map = new HashMap<>();
        map.put("data", categoryPage);
        return getResult(map);
    }

    /**
     * 获取$!{table.comment}
     *
     * @param id $!{table.comment}ID
     */
    @ApiOperation(value = "获取${entity}对象详情", notes = "查看$!{table.comment}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "对象标识ID", dataType = "Long", paramType = "path")
    })
    @GetMapping("/get/{id}")
	#if(${cfg.requiresPermissions})
	@RequiresPermissions("$!{cfg.colonTableName}:getById")
	#end
    public Map<String, Object> getById(@PathVariable("id") Long id) {
        ${entity} ${table.entityPath} = ${table.entityPath}Service.getById(id);
        // 初始化返回对象
        Map<String, Object> map = new HashMap<>();
        map.put("data", ${table.entityPath});
        return getResult(map);
    }

	/**
	 * 通用排序方法(偏移量为1并且点击后实时触发保存)
	 *
	 * @param firstId  第一个待排序实体ID
	 * @param secondId 第二个待排序实体ID
	 * @return
	 */
	@PutMapping("/updateSortorderByOneStep")
	public Map<String, Object> updateSortorderByOneStep(Long firstId, Long secondId) {
		${entity} theFirst = this.${table.entityPath}Service.getById(firstId);
		${entity} theSecond = this.${table.entityPath}Service.getById(secondId);

        Long firstSortorder = theFirst.getSort();
        Long secondSortorder = theSecond.getSort();

		boolean flag = false;
		if (firstSortorder > secondSortorder && firstSortorder > 1) {
			flag = true;
			theFirst.setSort(firstSortorder - 1L);
			theSecond.setSort(secondSortorder + 1L);
		} else if (firstSortorder < secondSortorder && secondSortorder > 1) {
			flag = true;
			theFirst.setSort(firstSortorder + 1L);
			theSecond.setSort(secondSortorder - 1L);
		}
		if (flag) {
			boolean firstResult = this.${table.entityPath}Service.updateById(theFirst);
			boolean secondResult = this.${table.entityPath}Service.updateById(theSecond);
			if (firstResult && secondResult) {
				return getSuccessResult();
			}
		}
		return getFailResult();
	}

	/**
	 * 通用排序方法(拖拽排序/所有排序完成后点击保存)
	 *
	 * @param sourceId 源实体ID
	 * @param targetId 目标实体ID
	 * @return Void
	 */
	@PutMapping("/updateSortorder")
	public Map<String,Object> updateSortorder(Long sourceId, Long targetId) {
		String moveType = null;
		${entity} theSource = this.${table.entityPath}Service.getById(sourceId);
		${entity} theTarget = this.${table.entityPath}Service.getById(targetId);

		if(theSource.getSort() > theTarget.getSort()) {
			moveType = "down";
		} else {
			moveType = "up";
		}
		Long sourceSortorder = theSource.getSort();
		Long targetSortorder = theTarget.getSort();

		boolean flag = false;
		//默认sortorder为降序排序,向上移动
		if ("up".equalsIgnoreCase(moveType) && sourceSortorder < targetSortorder) {
			flag = true;
			QueryWrapper<${entity}> wrapper = new QueryWrapper<>();
			wrapper.between("sortorder", sourceSortorder, targetSortorder);
			wrapper.select("id", "sortorder");
			List<${entity}> list = this.${table.entityPath}Service.list(wrapper);
			for (${entity} entity : list) {
				if (!entity.getId().equals(sourceId)) {
					entity.setSort(entity.getSort() - 1L);
					this.${table.entityPath}Service.updateById(entity);
				}
			}
		}
		//默认sortorder为降序排序,向下移动
		else if ("down".equalsIgnoreCase(moveType) && sourceSortorder > targetSortorder) {
			flag = true;
			QueryWrapper<${entity}> wrapper = new QueryWrapper<>();
			wrapper.between("sortorder", targetSortorder, sourceSortorder);
			wrapper.select("id", "sortorder");
			List<${entity}> slideList = this.${table.entityPath}Service.list(wrapper);
			for (${entity} entity : slideList) {
				if (!entity.getId().equals(sourceId)) {
					entity.setSort(entity.getSort() + 1L);
					this.${table.entityPath}Service.updateById(entity);
				}
			}
		}
		//默认sortorder为正序排序,向下移动
		else if ("down".equalsIgnoreCase(moveType) && sourceSortorder < targetSortorder) {
			flag = true;
			QueryWrapper<${entity}> wrapper = new QueryWrapper<>();
			wrapper.between("sortorder", sourceSortorder, targetSortorder);
			wrapper.select("id", "sortorder");
			List<${entity}> slideList = this.${table.entityPath}Service.list(wrapper);
			for (${entity} slide : slideList) {
				if (!slide.getId().equals(sourceId)) {
					slide.setSort(slide.getSort() - 1L);
					this.${table.entityPath}Service.updateById(slide);
				}
			}
		}
		//默认sortorder为正序排序,向上移动
		else if ("up".equalsIgnoreCase(moveType) && sourceSortorder > targetSortorder) {
			flag = true;
			QueryWrapper<${entity}> wrapper = new QueryWrapper<>();
			wrapper.between("sortorder", targetSortorder, sourceSortorder);
			wrapper.select("id", "sortorder");
			List<${entity}> slideList = this.${table.entityPath}Service.list(wrapper);
			for (${entity} slide : slideList) {
				if (!slide.getId().equals(sourceId)) {
					slide.setSort(slide.getSort() + 1L);
					this.${table.entityPath}Service.updateById(slide);
				}
			}
		}
		if (flag) {
			theSource.setSort(targetSortorder);
			this.${table.entityPath}Service.updateById(theSource);
			return getSuccessResult();
		}
		return getFailResult();
	}

}

#end