¶Ô±ÈÐÂÎļþ |
| | |
| | | package org.jeecg.modules.system.controller; |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import io.swagger.annotations.Api; |
| | | import io.swagger.annotations.ApiOperation; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.shiro.SecurityUtils; |
| | | import org.jeecg.common.api.vo.Result; |
| | | import org.jeecg.common.aspect.annotation.AutoLog; |
| | | import org.jeecg.common.constant.CommonConstant; |
| | | import org.jeecg.common.system.query.QueryGenerator; |
| | | import org.jeecg.common.system.vo.LoginUser; |
| | | import org.jeecg.common.util.ImportExcelUtil; |
| | | import org.jeecg.common.util.oConvertUtils; |
| | | import org.jeecg.modules.system.entity.SysPosition; |
| | | import org.jeecg.modules.system.service.ISysBusinessCodeRuleService; |
| | | import org.jeecg.modules.system.service.ISysPositionService; |
| | | 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; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.web.bind.annotation.*; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | import org.springframework.web.multipart.MultipartHttpServletRequest; |
| | | import org.springframework.web.servlet.ModelAndView; |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.IOException; |
| | | import java.io.UnsupportedEncodingException; |
| | | import java.net.URLDecoder; |
| | | import java.util.ArrayList; |
| | | import java.util.Arrays; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | /** |
| | | * @Description: èå¡è¡¨ |
| | | * @Author: jeecg-boot |
| | | * @Date: 2019-09-19 |
| | | * @Version: V1.0 |
| | | */ |
| | | @Slf4j |
| | | @Api(tags = "èå¡è¡¨") |
| | | @RestController |
| | | @RequestMapping("/sys/position") |
| | | public class SysPositionController { |
| | | |
| | | @Autowired |
| | | private ISysPositionService sysPositionService; |
| | | |
| | | @Autowired |
| | | private ISysBusinessCodeRuleService sysBusinessCodeRuleService; |
| | | |
| | | /** |
| | | * å页å表æ¥è¯¢ |
| | | * |
| | | * @param sysPosition |
| | | * @param pageNo |
| | | * @param pageSize |
| | | * @param req |
| | | * @return |
| | | */ |
| | | @AutoLog(value = "èå¡è¡¨-å页å表æ¥è¯¢") |
| | | @ApiOperation(value = "èå¡è¡¨-å页å表æ¥è¯¢", notes = "èå¡è¡¨-å页å表æ¥è¯¢") |
| | | @GetMapping(value = "/list") |
| | | public Result<IPage<SysPosition>> queryPageList(SysPosition sysPosition, |
| | | @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, |
| | | @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, |
| | | HttpServletRequest req) { |
| | | Result<IPage<SysPosition>> result = new Result<IPage<SysPosition>>(); |
| | | QueryWrapper<SysPosition> queryWrapper = QueryGenerator.initQueryWrapper(sysPosition, req.getParameterMap()); |
| | | Page<SysPosition> page = new Page<SysPosition>(pageNo, pageSize); |
| | | IPage<SysPosition> pageList = sysPositionService.page(page, queryWrapper); |
| | | result.setSuccess(true); |
| | | result.setResult(pageList); |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | | * æ·»å |
| | | * |
| | | * @param sysPosition |
| | | * @return |
| | | */ |
| | | @AutoLog(value = "èå¡è¡¨-æ·»å ") |
| | | @ApiOperation(value = "èå¡è¡¨-æ·»å ", notes = "èå¡è¡¨-æ·»å ") |
| | | @PostMapping(value = "/add") |
| | | public Result<SysPosition> add(@RequestBody SysPosition sysPosition) { |
| | | String codeSeq = sysBusinessCodeRuleService.generateBusinessCodeSeq("PositionCodeRule"); |
| | | sysPosition.setCode(codeSeq); |
| | | Result<SysPosition> result = new Result<SysPosition>(); |
| | | try { |
| | | sysPositionService.save(sysPosition); |
| | | result.success("æ·»å æåï¼"); |
| | | } catch (Exception e) { |
| | | log.error(e.getMessage(), e); |
| | | result.error500("æä½å¤±è´¥"); |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | | * ç¼è¾ |
| | | * |
| | | * @param sysPosition |
| | | * @return |
| | | */ |
| | | @AutoLog(value = "èå¡è¡¨-ç¼è¾") |
| | | @ApiOperation(value = "èå¡è¡¨-ç¼è¾", notes = "èå¡è¡¨-ç¼è¾") |
| | | @RequestMapping(value = "/edit", method ={RequestMethod.PUT, RequestMethod.POST}) |
| | | public Result<SysPosition> edit(@RequestBody SysPosition sysPosition) { |
| | | Result<SysPosition> result = new Result<SysPosition>(); |
| | | SysPosition sysPositionEntity = sysPositionService.getById(sysPosition.getId()); |
| | | if (sysPositionEntity == null) { |
| | | result.error500("æªæ¾å°å¯¹åºå®ä½"); |
| | | } else { |
| | | boolean ok = sysPositionService.updateById(sysPosition); |
| | | //TODO è¿åfalse说æä»ä¹ï¼ |
| | | if (ok) { |
| | | result.success("ä¿®æ¹æå!"); |
| | | } |
| | | } |
| | | |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | | * éè¿idå é¤ |
| | | * |
| | | * @param id |
| | | * @return |
| | | */ |
| | | @AutoLog(value = "èå¡è¡¨-éè¿idå é¤") |
| | | @ApiOperation(value = "èå¡è¡¨-éè¿idå é¤", notes = "èå¡è¡¨-éè¿idå é¤") |
| | | @DeleteMapping(value = "/delete") |
| | | public Result<?> delete(@RequestParam(name = "id", required = true) String id) { |
| | | try { |
| | | sysPositionService.removeById(id); |
| | | } catch (Exception e) { |
| | | log.error("å é¤å¤±è´¥", e.getMessage()); |
| | | return Result.error("å é¤å¤±è´¥!"); |
| | | } |
| | | return Result.ok("å 餿å!"); |
| | | } |
| | | |
| | | /** |
| | | * æ¹éå é¤ |
| | | * |
| | | * @param ids |
| | | * @return |
| | | */ |
| | | @AutoLog(value = "èå¡è¡¨-æ¹éå é¤") |
| | | @ApiOperation(value = "èå¡è¡¨-æ¹éå é¤", notes = "èå¡è¡¨-æ¹éå é¤") |
| | | @DeleteMapping(value = "/deleteBatch") |
| | | public Result<SysPosition> deleteBatch(@RequestParam(name = "ids", required = true) String ids) { |
| | | Result<SysPosition> result = new Result<SysPosition>(); |
| | | if (ids == null || "".equals(ids.trim())) { |
| | | result.error500("åæ°ä¸è¯å«ï¼"); |
| | | } else { |
| | | this.sysPositionService.removeByIds(Arrays.asList(ids.split(","))); |
| | | result.success("å 餿å!"); |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | | * éè¿idæ¥è¯¢ |
| | | * |
| | | * @param id |
| | | * @return |
| | | */ |
| | | @AutoLog(value = "èå¡è¡¨-éè¿idæ¥è¯¢") |
| | | @ApiOperation(value = "èå¡è¡¨-éè¿idæ¥è¯¢", notes = "èå¡è¡¨-éè¿idæ¥è¯¢") |
| | | @GetMapping(value = "/queryById") |
| | | public Result<SysPosition> queryById(@RequestParam(name = "id", required = true) String id) { |
| | | Result<SysPosition> result = new Result<SysPosition>(); |
| | | SysPosition sysPosition = sysPositionService.getById(id); |
| | | if (sysPosition == null) { |
| | | result.error500("æªæ¾å°å¯¹åºå®ä½"); |
| | | } else { |
| | | result.setResult(sysPosition); |
| | | result.setSuccess(true); |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | | * 导åºexcel |
| | | * |
| | | * @param request |
| | | * @param response |
| | | */ |
| | | @RequestMapping(value = "/exportXls") |
| | | public ModelAndView exportXls(HttpServletRequest request, HttpServletResponse response) { |
| | | // Step.1 ç»è£
æ¥è¯¢æ¡ä»¶ |
| | | QueryWrapper<SysPosition> queryWrapper = null; |
| | | try { |
| | | String paramsStr = request.getParameter("paramsStr"); |
| | | if (oConvertUtils.isNotEmpty(paramsStr)) { |
| | | String deString = URLDecoder.decode(paramsStr, "UTF-8"); |
| | | SysPosition sysPosition = JSON.parseObject(deString, SysPosition.class); |
| | | queryWrapper = QueryGenerator.initQueryWrapper(sysPosition, request.getParameterMap()); |
| | | } |
| | | } catch (UnsupportedEncodingException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | |
| | | //Step.2 AutoPoi 导åºExcel |
| | | ModelAndView mv = new ModelAndView(new JeecgEntityExcelView()); |
| | | List<SysPosition> pageList = sysPositionService.list(queryWrapper); |
| | | LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal(); |
| | | //å¯¼åºæä»¶åç§° |
| | | mv.addObject(NormalExcelConstants.FILE_NAME, "èå¡è¡¨å表"); |
| | | mv.addObject(NormalExcelConstants.CLASS, SysPosition.class); |
| | | mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("èå¡è¡¨åè¡¨æ°æ®", "导åºäºº:"+user.getRealname(),"导åºä¿¡æ¯")); |
| | | mv.addObject(NormalExcelConstants.DATA_LIST, pageList); |
| | | return mv; |
| | | } |
| | | |
| | | /** |
| | | * éè¿excel导å
¥æ°æ® |
| | | * |
| | | * @param request |
| | | * @param response |
| | | * @return |
| | | */ |
| | | @RequestMapping(value = "/importExcel", method = RequestMethod.POST) |
| | | public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response)throws IOException { |
| | | MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; |
| | | Map<String, MultipartFile> fileMap = multipartRequest.getFileMap(); |
| | | // éè¯¯ä¿¡æ¯ |
| | | List<String> errorMessage = new ArrayList<>(); |
| | | int successLines = 0, errorLines = 0; |
| | | 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<Object> listSysPositions = ExcelImportUtil.importExcel(file.getInputStream(), SysPosition.class, params); |
| | | List<String> list = ImportExcelUtil.importDateSave(listSysPositions, ISysPositionService.class, errorMessage,CommonConstant.SQL_INDEX_UNIQ_CODE); |
| | | errorLines+=list.size(); |
| | | successLines+=(listSysPositions.size()-errorLines); |
| | | } catch (Exception e) { |
| | | log.error(e.getMessage(), e); |
| | | return Result.error("æä»¶å¯¼å
¥å¤±è´¥:" + e.getMessage()); |
| | | } finally { |
| | | try { |
| | | file.getInputStream().close(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | } |
| | | return ImportExcelUtil.imporReturnRes(errorLines,successLines,errorMessage); |
| | | } |
| | | |
| | | /** |
| | | * éè¿codeæ¥è¯¢ |
| | | * |
| | | * @param code |
| | | * @return |
| | | */ |
| | | @AutoLog(value = "èå¡è¡¨-éè¿codeæ¥è¯¢") |
| | | @ApiOperation(value = "èå¡è¡¨-éè¿codeæ¥è¯¢", notes = "èå¡è¡¨-éè¿codeæ¥è¯¢") |
| | | @GetMapping(value = "/queryByCode") |
| | | public Result<SysPosition> queryByCode(@RequestParam(name = "code", required = true) String code) { |
| | | Result<SysPosition> result = new Result<SysPosition>(); |
| | | QueryWrapper<SysPosition> queryWrapper = new QueryWrapper<SysPosition>(); |
| | | queryWrapper.eq("code",code); |
| | | SysPosition sysPosition = sysPositionService.getOne(queryWrapper); |
| | | if (sysPosition == null) { |
| | | result.error500("æªæ¾å°å¯¹åºå®ä½"); |
| | | } else { |
| | | result.setResult(sysPosition); |
| | | result.setSuccess(true); |
| | | } |
| | | return result; |
| | | } |
| | | } |