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;
/**
*
* $!{table.comment} 前端控制器
*
*
* @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 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 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 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 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 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 get${entity}List() {
// 通过业务流程ID和业务ID获取未删除业务节点集合
List<${entity}> ${table.entityPath}s = ${table.entityPath}Service.list();
// 初始化返回对象
Map 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 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 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 getById(@PathVariable("id") Long id) {
${entity} ${table.entityPath} = ${table.entityPath}Service.getById(id);
// 初始化返回对象
Map map = new HashMap<>();
map.put("data", ${table.entityPath});
return getResult(map);
}
/**
* 通用排序方法(偏移量为1并且点击后实时触发保存)
*
* @param firstId 第一个待排序实体ID
* @param secondId 第二个待排序实体ID
* @return
*/
@PutMapping("/updateSortorderByOneStep")
public Map 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 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