template.Controller.vm Maven / Gradle / Ivy
The newest version!
package ${package}.modules.${moduleName}.controller;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.nb6868.onex.common.annotation.LogOperation;
import com.nb6868.onex.common.exception.ErrorCode;
import com.nb6868.onex.common.pojo.Const;
import com.nb6868.onex.common.pojo.MsgResult;
import com.nb6868.onex.common.pojo.PageData;
import com.nb6868.onex.common.pojo.Result;
import com.nb6868.onex.common.util.ConvertUtils;
import com.nb6868.onex.common.validator.AssertUtils;
import com.nb6868.onex.common.validator.ValidatorUtils;
import com.nb6868.onex.common.validator.group.AddGroup;
import com.nb6868.onex.common.validator.group.DefaultGroup;
import com.nb6868.onex.common.validator.group.UpdateGroup;
import com.nb6868.onex.common.util.ExcelUtils;
import ${package}.modules.${moduleName}.dto.${className}DTO;
import ${package}.modules.${moduleName}.excel.${className}Excel;
import ${package}.modules.${moduleName}.service.${className}Service;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* ${comments}
*
* @author ${author} ${email}
*/
@RestController("${moduleName}${className}")
@RequestMapping("/${moduleName}/${pathName}/")
@Validated
@Tag(name = "${comments}")
public class ${className}Controller {
@Autowired
private ${className}Service ${classname}Service;
@GetMapping("list")
@Operation(summary = "列表")
@RequiresPermissions("${moduleName}:${pathName}:list")
public Result> list(@ApiIgnore @RequestParam Map params) {
List<${className}DTO> list = ${classname}Service.listDto(params);
return new Result<>().success(list);
}
@GetMapping("page")
@Operation(summary = "分页")
@RequiresPermissions("${moduleName}:${pathName}:page")
public Result> page(@ApiIgnore @RequestParam Map params) {
PageData<${className}DTO> page = ${classname}Service.pageDto(params);
return new Result<>().success(page);
}
@GetMapping("info")
@Operation(summary = "信息")
@RequiresPermissions("${moduleName}:${pathName}:info")
public Result> info(@RequestParam @NotNull(message = "{id.require}") Long id) {
${className}DTO data = ${classname}Service.getDtoById(id);
AssertUtils.isNull(data, ErrorCode.DB_RECORD_NOT_EXISTED);
return new Result<>().success(data);
}
@PostMapping("save")
@Operation(summary = "保存")
@LogOperation("保存")
@RequiresPermissions("${moduleName}:${pathName}:save")
public Result> save(@Validated(value = {DefaultGroup.class, AddGroup.class}) @RequestBody ${className}DTO dto) {
${classname}Service.saveDto(dto);
return new Result<>().success(dto);
}
@PostMapping("update")
@Operation(summary = "修改")
@LogOperation("修改")
@RequiresPermissions("${moduleName}:${pathName}:update")
public Result> update(@Validated(value = {DefaultGroup.class, UpdateGroup.class}) @RequestBody ${className}DTO dto) {
${classname}Service.updateDto(dto);
return new Result<>().success(dto);
}
@PostMapping("delete")
@Operation(summary = "删除")
@LogOperation("删除")
@RequiresPermissions("${moduleName}:${pathName}:delete")
public Result> delete(@NotNull(message = "{id.require}") @RequestParam Long id) {
${classname}Service.removeById(id);
return new Result<>();
}
@DeleteMapping("deleteBatch")
@Operation(summary = "批量删除")
@LogOperation("批量删除")
@RequiresPermissions("${moduleName}:${pathName}:deleteBatch")
public Result> deleteBatch(@NotEmpty(message = "{ids.require}")@RequestBody List ids) {
${classname}Service.removeByIds(ids);
return new Result<>();
}
@GetMapping("export")
@Operation(summary = "导出")
@LogOperation("导出")
@RequiresPermissions("${moduleName}:${pathName}:export")
public void export(@ApiIgnore @RequestParam Map params, HttpServletResponse response) {
List<${className}DTO> list = ${classname}Service.listDto(params);
ExcelUtils.exportExcelToTarget(response, "${comments}", list, ${className}Excel.class);
}
@PostMapping("import")
@Operation(summary = "导入")
@LogOperation("导入")
@RequiresPermissions("${moduleName}:${pathName}:import")
public Result> importExcel(@RequestParam("file") MultipartFile file) {
AssertUtils.isTrue(file.isEmpty(), ErrorCode.UPLOAD_FILE_EMPTY);
ImportParams params = new ImportParams();
params.setStartSheetIndex(0);
List<${className}Excel> list = ExcelUtils.importExcel(file, ${className}Excel.class, params);
AssertUtils.isTrue(list.isEmpty(), ErrorCode.ERROR_REQUEST, "Excel内容为空");
AssertUtils.isTrue(list.size() > Const.EXCEL_IMPORT_LIMIT, ErrorCode.ERROR_REQUEST, "单次导入不得超过" + Const.EXCEL_IMPORT_LIMIT + "条");
List result = new ArrayList<>();
for (${className}Excel item : list) {
${className}DTO dto = ConvertUtils.sourceToTarget(item, ${className}DTO.class);
MsgResult validateResult = ValidatorUtils.getValidateResult(dto, DefaultGroup.class, AddGroup.class);
if (validateResult.isSuccess()) {
try {
${classname}Service.saveDto(dto);
result.add(new MsgResult().success("导入成功"));
} catch (Exception e) {
result.add(new MsgResult().error(ErrorCode.ERROR_REQUEST, e.getMessage()));
}
} else {
result.add(validateResult);
}
}
return new Result<>().success(result);
}
}