${entityName}Controller.javai 11.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
package ${bussiPackage}.${entityPackage}.controller;

import java.io.UnsupportedEncodingException;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
20 21
import org.jeecg.common.system.vo.LoginUser;
import org.apache.shiro.SecurityUtils;
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
<#list subTables as sub>
import ${bussiPackage}.${entityPackage}.entity.${sub.entityName};
</#list>
import ${bussiPackage}.${entityPackage}.entity.${entityName};
import ${bussiPackage}.${entityPackage}.vo.${entityName}Page;
import ${bussiPackage}.${entityPackage}.service.I${entityName}Service;
<#list subTables as sub>
import ${bussiPackage}.${entityPackage}.service.I${sub.entityName}Service;
</#list>
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import com.alibaba.fastjson.JSON;
45 46 47
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
48 49 50 51 52 53 54

 /**
 * @Description: ${tableVo.ftlDescription}
 * @Author: jeecg-boot
 * @Date:   ${.now?string["yyyy-MM-dd"]}
 * @Version: V1.0
 */
55
@Api(tags="${tableVo.ftlDescription}")
56 57 58 59 60 61 62 63 64 65 66 67
@RestController
@RequestMapping("/${entityPackage}/${entityName?uncap_first}")
@Slf4j
public class ${entityName}Controller {
	@Autowired
	private I${entityName}Service ${entityName?uncap_first}Service;
	<#list subTables as sub>
	@Autowired
	private I${sub.entityName}Service ${sub.entityName?uncap_first}Service;
	</#list>
	
	/**
68 69
	 * 分页列表查询
	 *
70 71 72 73 74 75
	 * @param ${entityName?uncap_first}
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
76 77
	@AutoLog(value = "${tableVo.ftlDescription}-分页列表查询")
	@ApiOperation(value="${tableVo.ftlDescription}-分页列表查询", notes="${tableVo.ftlDescription}-分页列表查询")
78
	@GetMapping(value = "/list")
79 80 81 82
	public Result<?> queryPageList(${entityName} ${entityName?uncap_first},
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
83 84 85
		QueryWrapper<${entityName}> queryWrapper = QueryGenerator.initQueryWrapper(${entityName?uncap_first}, req.getParameterMap());
		Page<${entityName}> page = new Page<${entityName}>(pageNo, pageSize);
		IPage<${entityName}> pageList = ${entityName?uncap_first}Service.page(page, queryWrapper);
86
		return Result.ok(pageList);
87 88 89
	}
	
	/**
90 91
	 *   添加
	 *
92 93 94
	 * @param ${entityName?uncap_first}Page
	 * @return
	 */
95 96
	@AutoLog(value = "${tableVo.ftlDescription}-添加")
	@ApiOperation(value="${tableVo.ftlDescription}-添加", notes="${tableVo.ftlDescription}-添加")
97
	@PostMapping(value = "/add")
98 99 100 101 102
	public Result<?> add(@RequestBody ${entityName}Page ${entityName?uncap_first}Page) {
		${entityName} ${entityName?uncap_first} = new ${entityName}();
		BeanUtils.copyProperties(${entityName?uncap_first}Page, ${entityName?uncap_first});
		${entityName?uncap_first}Service.saveMain(${entityName?uncap_first}, <#list subTables as sub>${entityName?uncap_first}Page.get${sub.entityName}List()<#if sub_has_next>,</#if></#list>);
		return Result.ok("添加成功!");
103 104 105
	}
	
	/**
106 107
	 *  编辑
	 *
108 109 110
	 * @param ${entityName?uncap_first}Page
	 * @return
	 */
111 112
	@AutoLog(value = "${tableVo.ftlDescription}-编辑")
	@ApiOperation(value="${tableVo.ftlDescription}-编辑", notes="${tableVo.ftlDescription}-编辑")
113
	@PutMapping(value = "/edit")
114
	public Result<?> edit(@RequestBody ${entityName}Page ${entityName?uncap_first}Page) {
115 116 117 118
		${entityName} ${entityName?uncap_first} = new ${entityName}();
		BeanUtils.copyProperties(${entityName?uncap_first}Page, ${entityName?uncap_first});
		${entityName} ${entityName?uncap_first}Entity = ${entityName?uncap_first}Service.getById(${entityName?uncap_first}.getId());
		if(${entityName?uncap_first}Entity==null) {
119
			return Result.error("未找到对应数据");
120
		}
121 122
		${entityName?uncap_first}Service.updateMain(${entityName?uncap_first}, <#list subTables as sub>${entityName?uncap_first}Page.get${sub.entityName}List()<#if sub_has_next>,</#if></#list>);
		return Result.ok("编辑成功!");
123 124 125
	}
	
	/**
126 127
	 *   通过id删除
	 *
128 129 130
	 * @param id
	 * @return
	 */
131 132
	@AutoLog(value = "${tableVo.ftlDescription}-通过id删除")
	@ApiOperation(value="${tableVo.ftlDescription}-通过id删除", notes="${tableVo.ftlDescription}-通过id删除")
133 134
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
135
		${entityName?uncap_first}Service.delMain(id);
136 137 138 139
		return Result.ok("删除成功!");
	}
	
	/**
140 141
	 *  批量删除
	 *
142 143 144
	 * @param ids
	 * @return
	 */
145 146
	@AutoLog(value = "${tableVo.ftlDescription}-批量删除")
	@ApiOperation(value="${tableVo.ftlDescription}-批量删除", notes="${tableVo.ftlDescription}-批量删除")
147
	@DeleteMapping(value = "/deleteBatch")
148 149 150
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.${entityName?uncap_first}Service.delBatchMain(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功!");
151 152 153
	}
	
	/**
154 155
	 * 通过id查询
	 *
156 157 158
	 * @param id
	 * @return
	 */
159 160
	@AutoLog(value = "${tableVo.ftlDescription}-通过id查询")
	@ApiOperation(value="${tableVo.ftlDescription}-通过id查询", notes="${tableVo.ftlDescription}-通过id查询")
161
	@GetMapping(value = "/queryById")
162
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
163 164
		${entityName} ${entityName?uncap_first} = ${entityName?uncap_first}Service.getById(id);
		if(${entityName?uncap_first}==null) {
165
			return Result.error("未找到对应数据");
166
		}
167 168
		return Result.ok(${entityName?uncap_first});

169 170 171 172
	}
	
	<#list subTables as sub>
	/**
173 174
	 * 通过id查询
	 *
175 176 177
	 * @param id
	 * @return
	 */
178 179
	@AutoLog(value = "${sub.ftlDescription}集合-通过id查询")
	@ApiOperation(value="${sub.ftlDescription}集合-通过id查询", notes="${sub.ftlDescription}-通过id查询")
180
	@GetMapping(value = "/query${sub.entityName}ByMainId")
181
	public Result<?> query${sub.entityName}ListByMainId(@RequestParam(name="id",required=true) String id) {
182
		List<${sub.entityName}> ${sub.entityName?uncap_first}List = ${sub.entityName?uncap_first}Service.selectByMainId(id);
183
		return Result.ok(${sub.entityName?uncap_first}List);
184 185 186
	}
	</#list>

187 188 189 190 191 192 193 194
    /**
    * 导出excel
    *
    * @param request
    * @param ${entityName?uncap_first}
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, ${entityName} ${entityName?uncap_first}) {
195 196
      // Step.1 组装查询条件查询数据
      QueryWrapper<${entityName}> queryWrapper = QueryGenerator.initQueryWrapper(${entityName?uncap_first}, request.getParameterMap());
197 198 199
      LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

      //Step.2 获取导出数据
200 201 202 203 204
      List<${entityName}> queryList = ${entityName?uncap_first}Service.list(queryWrapper);
      // 过滤选中数据
      String selections = request.getParameter("selections");
      List<${entityName}> ${entityName?uncap_first}List = new ArrayList<${entityName}>();
      if(oConvertUtils.isEmpty(selections)) {
205
          ${entityName?uncap_first}List = queryList;
206
      }else {
207 208
          List<String> selectionList = Arrays.asList(selections.split(","));
          ${entityName?uncap_first}List = queryList.stream().filter(item -> selectionList.contains(item.getId())).collect(Collectors.toList());
209
      }
210 211

      // Step.3 组装pageList
212 213 214 215 216 217 218 219 220 221
      List<${entityName}Page> pageList = new ArrayList<${entityName}Page>();
      for (${entityName} main : ${entityName?uncap_first}List) {
          ${entityName}Page vo = new ${entityName}Page();
          BeanUtils.copyProperties(main, vo);
          <#list subTables as sub>
          List<${sub.entityName}> ${sub.entityName?uncap_first}List = ${sub.entityName?uncap_first}Service.selectByMainId(main.getId());
          vo.set${sub.entityName}List(${sub.entityName?uncap_first}List);
          </#list>
          pageList.add(vo);
      }
222 223

      // Step.4 AutoPoi 导出Excel
224 225 226
      ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
      mv.addObject(NormalExcelConstants.FILE_NAME, "${tableVo.ftlDescription}列表");
      mv.addObject(NormalExcelConstants.CLASS, ${entityName}Page.class);
227
      mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("${tableVo.ftlDescription}数据", "导出人:"+sysUser.getRealname(), "${tableVo.ftlDescription}"));
228 229
      mv.addObject(NormalExcelConstants.DATA_LIST, pageList);
      return mv;
230
    }
231

232 233 234 235 236 237 238 239 240
    /**
    * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
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
      MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
      Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
      for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
          MultipartFile file = entity.getValue();// 获取上传文件对象
          ImportParams params = new ImportParams();
          params.setTitleRows(2);
          params.setHeadRows(1);
          params.setNeedSave(true);
          try {
              List<${entityName}Page> list = ExcelImportUtil.importExcel(file.getInputStream(), ${entityName}Page.class, params);
              for (${entityName}Page page : list) {
                  ${entityName} po = new ${entityName}();
                  BeanUtils.copyProperties(page, po);
                  ${entityName?uncap_first}Service.saveMain(po, <#list subTables as sub>page.get${sub.entityName}List()<#if sub_has_next>,</#if></#list>);
              }
              return Result.ok("文件导入成功!数据行数:" + list.size());
          } catch (Exception e) {
              log.error(e.getMessage(),e);
              return Result.error("文件导入失败:"+e.getMessage());
          } finally {
              try {
                  file.getInputStream().close();
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
      }
      return Result.ok("文件导入失败!");
269
    }
270 271

}