controller.java.vm 8.75 KB
package ${package.Controller};

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
#if(${superControllerClassPackage})
import cn.chnmuseum.party.web.controller.base.BaseController;
#end
import ${package.Entity}.${entity};
import ${package.Service}.${table.serviceName};
import cn.chnmuseum.party.common.enums.AuditStatusEnum;
import cn.chnmuseum.party.common.validator.groups.Add;
import cn.chnmuseum.party.common.validator.groups.Update;
import cn.chnmuseum.party.common.vo.GenericPageParam;
#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.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

/**
 * <pre>
 * $!{table.comment} 前端控制器
 * </pre>
 *
 * @author ${author}
 * @since ${date}
 */
@Slf4j
#if(${restControllerStyle})
@RestController
#else
@Controller
#end
@RequestMapping("/${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;

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

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

    @PutMapping("/update")
	#if(${cfg.requiresPermissions})
	@RequiresAuthentication  //@RequiresPermissions("$!{cfg.colonTableName}:update")
	#end
    @ApiOperation(value = "修改$!{table.comment}信息", notes = "修改$!{table.comment}信息")
    public Map<String, Object> update${entity}(#if(${cfg.paramValidation})@Validated(value = {Update.class})#end ${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();
    }

	@PutMapping("/updateAuditStatus/{id}")
	@RequiresAuthentication  //@RequiresPermissions("$!{cfg.colonTableName}:update:audit:status")
	@ApiOperation(value = "更新$!{table.comment}审核状态", notes = "更新$!{table.comment}审核状态")
	@ApiImplicitParams(value = {
			@ApiImplicitParam(name = "id", value = "标识ID", dataType = "String", paramType = "path"),
			@ApiImplicitParam(name = "status", value = "状态", paramType = "query", dataType = "String")
	})
	public Map<String, Object> updateStatus(@NotNull(message = "$!{table.comment}ID不能为空") @PathVariable("id") String id, @RequestParam("status") AuditStatusEnum status) {
		UpdateWrapper<${entity}> updateWrapper = new UpdateWrapper<>();
		updateWrapper.eq("id", id);
		updateWrapper.eq("audit_status", status.name());
		boolean flag = ${table.entityPath}Service.update(updateWrapper);
		if (flag) {
		return getSuccessResult();
		}
		return getFailResult();
    }

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

    @GetMapping("/getList")
	#if(${cfg.requiresPermissions})
	@RequiresAuthentication  //@RequiresPermissions("$!{cfg.colonTableName}:list")
	#end
	@ApiOperation(value = "获取$!{table.comment}全部列表(无分页)", notes = "获取$!{table.comment}全部列表(无分页)")
	@ApiImplicitParams(value = {
			@ApiImplicitParam(name = "auditStatus", value = "审核状态", paramType = "query", dataType = "String")
	})
    public Map<String, Object> get${entity}List(@RequestParam(value = "auditStatus", defaultValue = "APPROVED_FINAL", required = false) AuditStatusEnum auditStatus) {
        List<${entity}> ${table.entityPath}List = ${table.entityPath}Service.list(Wrappers.<${entity}>lambdaQuery().eq(${entity}::getAuditStatus, auditStatus.name()));
        return getResult(${table.entityPath}List);
    }

	@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")
	#if(${cfg.requiresPermissions})
	@RequiresAuthentication  //@RequiresPermissions("$!{cfg.colonTableName}:page")
	#end
	@ApiOperation(value = "获取$!{table.comment}分页列表", notes = "获取$!{table.comment}分页列表")
    public Map<String, Object> get${entity}PageList(GenericPageParam genericPageParam) {
		LambdaQueryWrapper<${entity}> queryWrapper = new LambdaQueryWrapper<>();
		// 对名称或编码模糊查询
		if (StringUtils.isNotBlank(genericPageParam.getNameOrCode())) {
			queryWrapper.like(${entity}::getName, genericPageParam.getNameOrCode());
		}
		// 根据创建时间区间检索
		if (genericPageParam.getStartDate() != null && genericPageParam.getEndDate() != null) {
			queryWrapper.ge(${entity}::getCreateTime, genericPageParam.getStartDate().atTime(0, 0, 0))
				.le(${entity}::getCreateTime, genericPageParam.getEndDate().atTime(23, 59, 59));
		}
		// 设置排序规则
		queryWrapper.orderByDesc(${entity}::getCreateTime);
		// 设置查询内容
		queryWrapper.select(
				${entity}::getId,
				${entity}::getName,
				${entity}::getAuditStatus,
				${entity}::getCreateTime,
				${entity}::getUpdateTime);
		Page<${entity}> page = this.${table.entityPath}Service.page(getPage(), queryWrapper);
		for (${entity} ${table.entityPath} : page.getRecords()) {

		}
		return getResult(page);
    }

    @ApiOperation(value = "获取$!{table.comment}详情", notes = "获取$!{table.comment}详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "标识ID", dataType = "String", paramType = "path")
    })
    @GetMapping("/get/{id}")
	#if(${cfg.requiresPermissions})
	@RequiresAuthentication  //@RequiresPermissions("$!{cfg.colonTableName}:get:id")
	#end
    public Map<String, Object> getById(@PathVariable("id") String id) {
        ${entity} ${table.entityPath} = ${table.entityPath}Service.getById(id);
		return getResult(${table.entityPath});
    }

}

#end