123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- package ${package};
- import cn.hutool.core.bean.BeanUtil;
- import cn.hutool.core.util.ObjectUtil;
- import cn.hutool.core.util.StrUtil;
- import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
- import com.baomidou.mybatisplus.core.metadata.IPage;
- import com.baomidou.mybatisplus.core.toolkit.Wrappers;
- import com.xjrsoft.common.constant.GlobalConstant;
- import com.baomidou.mybatisplus.core.toolkit.StringPool;
- import com.xjrsoft.common.page.ConventPage;
- import com.xjrsoft.common.page.PageOutput;
- import com.xjrsoft.common.model.result.RT;
- import com.xjrsoft.common.utils.VoToColumnUtil;
- import com.xjrsoft.module.${outputArea}.dto.Add${entityClass}Dto;
- import com.xjrsoft.module.${outputArea}.dto.Update${entityClass}Dto;
- import cn.dev33.satoken.annotation.SaCheckPermission;
- <#if codeRules != "">
- import com.xjrsoft.module.system.service.ICodeRuleService;
- </#if>
- <#if isImport || isExport>
- import com.alibaba.excel.EasyExcel;
- </#if>
- <#if isImport>
- import org.springframework.web.multipart.MultipartFile;
- import java.io.IOException;
- </#if>
- <#if isExport>
- import com.alibaba.excel.support.ExcelTypeEnum;
- import org.springframework.http.ResponseEntity;
- import java.io.ByteArrayOutputStream;
- import java.util.ArrayList;
- </#if>
- <#if !isPage>
- import com.xjrsoft.module.${outputArea}.dto.${entityClass}ListDto;
- <#else>
- import com.xjrsoft.module.${outputArea}.dto.${entityClass}PageDto;
- </#if>
- import com.xjrsoft.module.${outputArea}.entity.${entityClass};
- import com.xjrsoft.module.${outputArea}.service.I${className}Service;
- <#if !isPage>
- import com.xjrsoft.module.${outputArea}.vo.${entityClass}ListVo;
- <#else>
- import com.xjrsoft.module.${outputArea}.vo.${entityClass}PageVo;
- </#if>
- <#if isDataAuth>
- import com.xjrsoft.module.authority.dto.BatchSetDataAuthDto;
- import java.util.stream.Collectors;
- import java.util.ArrayList;
- </#if>
- import com.xjrsoft.module.${outputArea}.vo.${entityClass}Vo;
- import io.swagger.annotations.Api;
- import io.swagger.annotations.ApiOperation;
- import lombok.AllArgsConstructor;
- import org.springframework.web.bind.annotation.*;
- import javax.validation.Valid;
- import javax.validation.constraints.NotNull;
- import java.util.List;
- /**
- * @title: ${tableComment}
- * @Author ${author}
- * @Date: ${date}
- * @Version 1.0
- */
- @RestController
- @RequestMapping("/${outputArea}" + "/${className?uncap_first}")
- @Api(value = "/${outputArea}" + "/${className?uncap_first}",tags = "${tableComment}代码")
- @AllArgsConstructor
- public class ${className}Controller {
- private final I${className}Service ${className?uncap_first}Service;
- <#if codeRules != "">
- private final ICodeRuleService codeRuleService;
- </#if>
- <#if !isPage>
- @GetMapping(value = "/list")
- @ApiOperation(value="${tableComment}列表(不分页)")
- @SaCheckPermission("${className?lower_case}:detail")
- public RT<List<${entityClass}ListVo>> list(@Valid ${entityClass}ListDto dto){
- LambdaQueryWrapper<${entityClass}> queryWrapper = new LambdaQueryWrapper<>();
- queryWrapper
- <#--属性遍历-->
- <#list fields as field>
- <#if field.fieldType == "String">
- .like(StrUtil.isNotBlank(dto.get${field.fieldName?cap_first}()),${entityClass}::get${field.fieldName?cap_first},dto.get${field.fieldName?cap_first}())
- <#elseif field.fieldType == "Integer" || field.fieldType == "Long"|| field.fieldType == "Double"|| field.fieldType == "Float"|| field.fieldType == "Boolean"|| field.fieldType == "BigDecimal" >
- .eq(ObjectUtil.isNotNull(dto.get${field.fieldName?cap_first}()),${entityClass}::get${field.fieldName?cap_first},dto.get${field.fieldName?cap_first}())
- <#elseif field.fieldType == "LocalDateTime" || field.fieldType == "LocalTime">
- .between(ObjectUtil.isNotNull(dto.get${field.fieldName?cap_first}Start()) && ObjectUtil.isNotNull(dto.get${field.fieldName?cap_first}End()),${entityClass}::get${field.fieldName?cap_first},dto.get${field.fieldName?cap_first}Start(),dto.get${field.fieldName?cap_first}End())
- <#else>
- .like(StrUtil.isNotBlank(dto.get${field.fieldName?cap_first}()),${entityClass}::get${field.fieldName?cap_first},dto.get${field.fieldName?cap_first}())
- </#if>
- </#list>
- <#--默认排序-->
- <#if orderField?? && orderField != "">
- <#if orderType>
- .orderByDesc(${entityClass}::get${orderField?cap_first})
- <#else>
- .orderByAsc(${entityClass}::get${orderField?cap_first})
- </#if>
- <#-- 如果需要根据前端所传字段排序 屏蔽上面 放开下面-->
- <#-- .orderBy(StrUtil.isBlank(dto.getOrderField()),${orderType?string("false","true")},${entityClass}::get${orderField?cap_first})-->
- <#-- .last(StrUtil.isNotBlank(dto.getOrderField()) && StrUtil.isNotBlank(dto.getOrder()),"order by " + dto.getOrderField() + StringPool.SPACE + dto.getOrder())-->
- </#if>
- .select(${entityClass}.class,x -> VoToColumnUtil.fieldsToColumns(${entityClass}ListVo.class).contains(x.getProperty()));
- List<${entityClass}> list = ${className?uncap_first}Service.list(queryWrapper);
- List<${entityClass}ListVo> listVos = BeanUtil.copyToList(list, ${entityClass}ListVo.class);
- return RT.ok(listVos);
- }
- <#else>
- @GetMapping(value = "/page")
- @ApiOperation(value="${tableComment}列表(分页)")
- @SaCheckPermission("${className?lower_case}:detail")
- public RT<PageOutput<${entityClass}PageVo>> page(@Valid ${entityClass}PageDto dto){
- LambdaQueryWrapper<${entityClass}> queryWrapper = new LambdaQueryWrapper<>();
- queryWrapper
- <#--属性遍历-->
- <#list fields as field>
- <#if field.fieldType == "String">
- .like(StrUtil.isNotBlank(dto.get${field.fieldName?cap_first}()),${entityClass}::get${field.fieldName?cap_first},dto.get${field.fieldName?cap_first}())
- <#elseif field.fieldType == "Integer" || field.fieldType == "Long"|| field.fieldType == "Double"|| field.fieldType == "Float"|| field.fieldType == "Boolean" || field.fieldType == "BigDecimal" >
- .eq(ObjectUtil.isNotNull(dto.get${field.fieldName?cap_first}()),${entityClass}::get${field.fieldName?cap_first},dto.get${field.fieldName?cap_first}())
- <#elseif field.fieldType == "LocalDateTime" || field.fieldType == "LocalTime">
- .between(ObjectUtil.isNotNull(dto.get${field.fieldName?cap_first}Start()) && ObjectUtil.isNotNull(dto.get${field.fieldName?cap_first}End()),${entityClass}::get${field.fieldName?cap_first},dto.get${field.fieldName?cap_first}Start(),dto.get${field.fieldName?cap_first}End())
- <#else>
- .like(StrUtil.isNotBlank(dto.get${field.fieldName?cap_first}()),${entityClass}::get${field.fieldName?cap_first},dto.get${field.fieldName?cap_first}())
- </#if>
- </#list>
- <#--默认排序-->
- <#if orderField?? && orderField != "">
- <#if orderType>
- .orderByDesc(${entityClass}::get${orderField?cap_first})
- <#else>
- .orderByAsc(${entityClass}::get${orderField?cap_first})
- </#if>
- <#-- .orderBy(StrUtil.isBlank(dto.getOrderField()),${orderType?string("false","true")},${entityClass}::get${orderField?cap_first})-->
- </#if>
- .select(${entityClass}.class,x -> VoToColumnUtil.fieldsToColumns(${entityClass}PageVo.class).contains(x.getProperty()));
- IPage<${entityClass}> page = ${className?uncap_first}Service.page(ConventPage.getPage(dto), queryWrapper);
- PageOutput<${entityClass}PageVo> pageOutput = ConventPage.getPageOutput(page, ${entityClass}PageVo.class);
- return RT.ok(pageOutput);
- }
- </#if>
- @GetMapping(value = "/info")
- @ApiOperation(value="根据id查询${tableComment}信息")
- @SaCheckPermission("${className?lower_case}:detail")
- public RT<${entityClass}Vo> info(@RequestParam Long id){
- ${entityClass} ${entityClass?uncap_first} = ${className?uncap_first}Service.${isMulti?string("getByIdDeep","getById")}(id);
- if (${entityClass?uncap_first} == null) {
- return RT.error("找不到此数据!");
- }
- return RT.ok(BeanUtil.toBean(${entityClass?uncap_first}, ${entityClass}Vo.class));
- }
- @PostMapping
- @ApiOperation(value = "新增${tableComment}")
- @SaCheckPermission("${className?lower_case}:add")
- public RT<Boolean> add(@Valid @RequestBody Add${entityClass}Dto dto){
- <#if isMulti>
- ${entityClass} ${entityClass?uncap_first} = BeanUtil.toBean(dto, ${entityClass}.class);
- boolean isSuccess = ${className?uncap_first}Service.add(${entityClass?uncap_first});
- <#else >
- ${entityClass} ${entityClass?uncap_first} = BeanUtil.toBean(dto, ${entityClass}.class);
- boolean isSuccess = ${className?uncap_first}Service.save(${entityClass?uncap_first});
- </#if>
- <#if codeRules != "">
- codeRuleService.useEncode("${codeRules}");
- </#if>
- return RT.ok(isSuccess);
- }
- @PutMapping
- @ApiOperation(value = "修改${tableComment}")
- @SaCheckPermission("${className?lower_case}:edit")
- public RT<Boolean> update(@Valid @RequestBody Update${entityClass}Dto dto){
- <#if isMulti>
- ${entityClass} ${entityClass?uncap_first} = BeanUtil.toBean(dto, ${entityClass}.class);
- return RT.ok(${className?uncap_first}Service.update(${entityClass?uncap_first}));
- <#else >
- ${entityClass} ${entityClass?uncap_first} = BeanUtil.toBean(dto, ${entityClass}.class);
- return RT.ok(${className?uncap_first}Service.updateById(${entityClass?uncap_first}));
- </#if>
- }
- @DeleteMapping
- @ApiOperation(value = "删除${tableComment}")
- @SaCheckPermission("${className?lower_case}:delete")
- public RT<Boolean> delete(@Valid @RequestBody List<Long> ids){
- <#if isMulti>
- return RT.ok(${className?uncap_first}Service.delete(ids));
- <#else >
- return RT.ok(${className?uncap_first}Service.removeBatchByIds(ids));
- </#if>
- }
- <#if isPage>
- <#assign importClass = "${entityClass}Page">
- <#else>
- <#assign importClass = "${entityClass}List">
- </#if>
- <#if isImport>
- @PostMapping("/import")
- @ApiOperation(value = "导入")
- public RT<Boolean> importData(@RequestParam MultipartFile file) throws IOException {
- List<${importClass}Vo> savedDataList = EasyExcel.read(file.getInputStream()).head(${importClass}Vo.class).sheet().doReadSync();
- Boolean result = ${className?uncap_first}Service.saveBatch(BeanUtil.copyToList(savedDataList, ${entityClass}.class));
- return RT.ok(result);
- }
- </#if>
- <#if isExport>
- @GetMapping("/export")
- @ApiOperation(value = "导出")
- public ResponseEntity<byte[]> exportData(@Valid ${importClass}Dto dto, @RequestParam(defaultValue = "false") Boolean isTemplate) {
- <#if isPage>
- List<${importClass}Vo> customerList = isTemplate != null && isTemplate ? new ArrayList<>() : ((PageOutput<${importClass}Vo>) page(dto).getData()).getList();
- <#else>
- List<${importClass}Vo> customerList = (List<${importClass}Vo>) list(dto).getData();
- </#if>
- ByteArrayOutputStream bot = new ByteArrayOutputStream();
- EasyExcel.write(bot, ${importClass}Vo.class).automaticMergeHead(false).excelType(ExcelTypeEnum.XLSX).sheet().doWrite(customerList);
- return RT.fileStream(bot.toByteArray(), "${className}" + ExcelTypeEnum.XLSX.getValue());
- }
- </#if>
- <#if isDataAuth>
- @PutMapping("/data-auth")
- @ApiOperation(value = "批量设置权限所属人")
- public RT setDataAUth(@RequestBody BatchSetDataAuthDto dto){
- List<${entityClass}> toUpdateList = new ArrayList<>();
- <#-- String userIdStr = dto.getUserIdList().stream().collect(Collectors.joining(StringPool.COMMA));-->
- for (Long dataId : dto.getDataIdList()) {
- ${entityClass} ${entityClass?uncap_first} = new ${entityClass}();
- ${entityClass?uncap_first}.set${pkField?cap_first}(dataId);
- ${entityClass?uncap_first}.setRuleUserId(dto.getUserIdList().get(0));
- toUpdateList.add(${entityClass?uncap_first});
- }
- return RT.ok(${className?uncap_first}Service.updateBatchById(toUpdateList));
- }
- </#if>
- }
|