DeviceController.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. package com.xcgl.device.controller;
  2. import cn.afterturn.easypoi.entity.vo.NormalExcelConstants;
  3. import cn.afterturn.easypoi.excel.entity.ExportParams;
  4. import com.xcgl.device.dao.DeviceDao;
  5. import com.xcgl.device.entity.DeviceEntity;
  6. import com.xcgl.device.service.DeviceServiceI;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. import javax.servlet.http.HttpServletRequest;
  10. import javax.servlet.http.HttpServletResponse;
  11. import com.xcgl.utils.OrderNumTools;
  12. import org.apache.log4j.Logger;
  13. import org.jeecgframework.poi.excel.entity.ImportParams;
  14. import org.jeecgframework.web.system.pojo.base.DictEntity;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.stereotype.Controller;
  17. import org.springframework.ui.ModelMap;
  18. import org.springframework.web.bind.annotation.RequestMapping;
  19. import org.springframework.web.bind.annotation.ResponseBody;
  20. import org.springframework.web.servlet.ModelAndView;
  21. import org.jeecgframework.core.common.controller.BaseController;
  22. import org.jeecgframework.core.common.exception.BusinessException;
  23. import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
  24. import org.jeecgframework.core.common.model.json.AjaxJson;
  25. import org.jeecgframework.core.common.model.json.DataGrid;
  26. import org.jeecgframework.core.constant.Globals;
  27. import org.jeecgframework.core.util.StringUtil;
  28. import org.jeecgframework.tag.core.easyui.TagUtil;
  29. import org.jeecgframework.web.system.service.SystemService;
  30. import org.jeecgframework.core.util.MyBeanUtils;
  31. import org.jeecgframework.poi.excel.ExcelImportUtil;
  32. import org.jeecgframework.core.util.ResourceUtil;
  33. import java.io.IOException;
  34. import org.springframework.web.bind.annotation.RequestMethod;
  35. import org.springframework.web.multipart.MultipartFile;
  36. import org.springframework.web.multipart.MultipartHttpServletRequest;
  37. import java.util.Map;
  38. import org.jeecgframework.core.util.ExceptionUtil;
  39. import org.springframework.http.ResponseEntity;
  40. import org.springframework.web.bind.annotation.PathVariable;
  41. import org.springframework.web.bind.annotation.RequestBody;
  42. import org.springframework.web.bind.annotation.ResponseStatus;
  43. import org.springframework.http.HttpHeaders;
  44. import org.springframework.http.HttpStatus;
  45. import org.jeecgframework.core.beanvalidator.BeanValidators;
  46. import java.util.Set;
  47. import javax.validation.ConstraintViolation;
  48. import javax.validation.Validator;
  49. import java.net.URI;
  50. import org.springframework.http.MediaType;
  51. import org.springframework.web.util.UriComponentsBuilder;
  52. import org.jeecgframework.web.cgform.entity.upload.CgUploadEntity;
  53. import org.jeecgframework.web.cgform.service.config.CgFormFieldServiceI;
  54. import java.util.HashMap;
  55. import java.util.stream.Collectors;
  56. /**
  57. * @Title: Controller
  58. * @Description: 设备档案
  59. * @author onlineGenerator
  60. * @date 2018-06-27 11:06:44
  61. * @version V1.0
  62. *
  63. */
  64. @Controller
  65. @RequestMapping("/deviceController")
  66. public class DeviceController extends BaseController {
  67. /**
  68. * Logger for this class
  69. */
  70. private static final Logger logger = Logger.getLogger(DeviceController.class);
  71. @Autowired
  72. private DeviceServiceI deviceService;
  73. @Autowired
  74. private SystemService systemService;
  75. @Autowired
  76. private Validator validator;
  77. @Autowired
  78. private CgFormFieldServiceI cgFormFieldService;
  79. @Autowired
  80. private DeviceDao deviceDao;
  81. /**
  82. * 设备档案列表 页面跳转
  83. *
  84. * @return
  85. */
  86. @RequestMapping(params = "list")
  87. public ModelAndView list(HttpServletRequest request) {
  88. if(request.getParameterMap().containsKey("flg")){
  89. request.setAttribute("flg", false);
  90. }else{
  91. request.setAttribute("flg", true);
  92. }
  93. return new ModelAndView("com/xcgl/device/deviceList");
  94. }
  95. /**
  96. * easyui AJAX请求数据
  97. * 修改逻辑,调整为模糊查询接口
  98. * @updateDate 2022年2月7日09:45:58
  99. * @updateUser 刘梦祥
  100. * @param request
  101. * @param response
  102. * @param dataGrid
  103. */
  104. @RequestMapping(params = "datagrid")
  105. public void datagrid(DeviceEntity device,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
  106. CriteriaQuery cq = new CriteriaQuery(DeviceEntity.class, dataGrid);
  107. //查询条件组装器
  108. //org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, device, request.getParameterMap());
  109. if(StringUtil.isNotEmpty(device.getCode())){
  110. cq.like("code","%"+device.getCode()+"%");
  111. }
  112. if(StringUtil.isNotEmpty(device.getName())){
  113. cq.like("name","%"+device.getName()+"%");
  114. }
  115. if(StringUtil.isNotEmpty(device.getTypeId())){
  116. cq.eq("typeId",device.getTypeId());
  117. }
  118. cq.add();
  119. this.deviceService.getDataGridReturn(cq, true);
  120. TagUtil.datagrid(response, dataGrid);
  121. }
  122. /**
  123. * 删除设备档案
  124. *
  125. * @return
  126. */
  127. @RequestMapping(params = "doDel")
  128. @ResponseBody
  129. public AjaxJson doDel(DeviceEntity device, HttpServletRequest request) {
  130. String message = null;
  131. AjaxJson j = new AjaxJson();
  132. device = systemService.getEntity(DeviceEntity.class, device.getId());
  133. message = "设备档案删除成功";
  134. try{
  135. deviceService.delete(device);
  136. systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
  137. }catch(Exception e){
  138. e.printStackTrace();
  139. message = "设备档案删除失败";
  140. throw new BusinessException(e.getMessage());
  141. }
  142. j.setMsg(message);
  143. return j;
  144. }
  145. /**
  146. * 添加设备档案
  147. *
  148. * @return
  149. */
  150. @RequestMapping(params = "doAdd")
  151. @ResponseBody
  152. public AjaxJson doAdd(DeviceEntity device, HttpServletRequest request) {
  153. String message = null;
  154. AjaxJson j = new AjaxJson();
  155. message = "设备档案添加成功";
  156. try{
  157. device.setDeviceStock("0");
  158. device.setCode(OrderNumTools.generateNextBillCode("DASB",4, "p_device", "code", 6));
  159. deviceService.save(device);
  160. systemService.addLog(message, Globals.Log_Type_INSERT, Globals.Log_Leavel_INFO);
  161. }catch(Exception e){
  162. e.printStackTrace();
  163. message = "设备档案添加失败";
  164. throw new BusinessException(e.getMessage());
  165. }
  166. j.setMsg(message);
  167. j.setObj(device);
  168. return j;
  169. }
  170. /**
  171. * 更新设备档案
  172. *
  173. * @return
  174. */
  175. @RequestMapping(params = "doUpdate")
  176. @ResponseBody
  177. public AjaxJson doUpdate(DeviceEntity device, HttpServletRequest request) {
  178. String message = null;
  179. AjaxJson j = new AjaxJson();
  180. message = "设备档案更新成功";
  181. DeviceEntity t = deviceService.get(DeviceEntity.class, device.getId());
  182. try {
  183. MyBeanUtils.copyBeanNotNull2Bean(device, t);
  184. deviceService.saveOrUpdate(t);
  185. systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
  186. } catch (Exception e) {
  187. e.printStackTrace();
  188. message = "设备档案更新失败";
  189. throw new BusinessException(e.getMessage());
  190. }
  191. j.setMsg(message);
  192. return j;
  193. }
  194. /**
  195. * 设备档案新增页面跳转
  196. *
  197. * @return
  198. */
  199. @RequestMapping(params = "goAdd")
  200. public ModelAndView goAdd(DeviceEntity device, HttpServletRequest req) {
  201. return new ModelAndView("com/xcgl/device/device-add");
  202. }
  203. /**
  204. * 设备档案编辑页面跳转
  205. *
  206. * @return
  207. */
  208. @RequestMapping(params = "goUpdate")
  209. public ModelAndView goUpdate(DeviceEntity device, HttpServletRequest req) {
  210. if (StringUtil.isNotEmpty(device.getId())) {
  211. device = deviceService.getEntity(DeviceEntity.class, device.getId());
  212. req.setAttribute("devicePage", device);
  213. }
  214. return new ModelAndView("com/xcgl/device/device-update");
  215. }
  216. /**
  217. * 导入功能跳转
  218. *
  219. * @return
  220. */
  221. @RequestMapping(params = "upload")
  222. public ModelAndView upload(HttpServletRequest req) {
  223. req.setAttribute("controller_name","deviceController");
  224. return new ModelAndView("common/upload/pub_excel_upload");
  225. }
  226. /**
  227. * 导出excel
  228. *
  229. */
  230. @RequestMapping(params = "exportXls")
  231. public String exportXls(ModelMap modelMap) {
  232. List<DeviceEntity> devices = deviceDao.getAllDevice();//this.deviceService.getListByCriteriaQuery(cq,false);
  233. modelMap.put(NormalExcelConstants.FILE_NAME,"设备基础档案");
  234. modelMap.put(NormalExcelConstants.CLASS,DeviceEntity.class);
  235. modelMap.put(NormalExcelConstants.PARAMS,new ExportParams("设备基础档案列表", "导出人:"+ResourceUtil.getSessionUser().getRealName(),
  236. "导出信息"));
  237. modelMap.put(NormalExcelConstants.DATA_LIST,devices);
  238. return NormalExcelConstants.EASYPOI_EXCEL_VIEW;
  239. }
  240. /**
  241. * 导出excel 使模板
  242. *
  243. * @param response
  244. */
  245. @RequestMapping(params = "exportXlsByT")
  246. public String exportXlsByT(HttpServletResponse response, ModelMap modelMap) throws Exception{
  247. List<DeviceEntity> devices = deviceService.getList(DeviceEntity.class);
  248. modelMap.put(NormalExcelConstants.FILE_NAME,"设备基础档案");
  249. modelMap.put(NormalExcelConstants.CLASS,DeviceEntity.class);
  250. modelMap.put(NormalExcelConstants.PARAMS,new ExportParams("设备基础档案列表", "导出人:"+ResourceUtil.getSessionUser().getRealName(),
  251. "导出信息"));
  252. List<DictEntity> deviceList = this.systemService.queryDict("P_device_type","type_code","type_name");
  253. Map<String, String> deviceMap = deviceList.stream().collect(Collectors.toMap(DictEntity::getTypecode, DictEntity::getTypename));
  254. if(devices != null && devices.size() > 0){
  255. for (DeviceEntity item:devices){
  256. if(item.getTypeId() != null){
  257. item.setTypeId(deviceMap.getOrDefault(item.getTypeId(),""));
  258. }
  259. }
  260. }
  261. modelMap.put(NormalExcelConstants.DATA_LIST,devices);
  262. return NormalExcelConstants.EASYPOI_EXCEL_VIEW;
  263. /*ExcelWriter writer = ExcelUtil.getWriter();
  264. writer.addHeaderAlias("code", "设备编码");
  265. writer.addHeaderAlias("name", "设备名称");
  266. writer.addHeaderAlias("deviceModel", "设备型号");
  267. writer.addHeaderAlias("deviceSpecs", "设备规格");
  268. writer.addHeaderAlias("deviceDepart", "设备单位");
  269. writer.addHeaderAlias("deviceFactory", "设备厂商");
  270. writer.addHeaderAlias("deviceStock", "库存");
  271. writer.merge(22, "人员档案");
  272. writer.setOnlyAlias(true);
  273. writer.write(devices, true);
  274. response.setContentType("application/vnd.ms-excel;charset=utf-8");
  275. String fileName = URLEncoder.encode("设备基础档案", "utf-8");
  276. response.setHeader("Content-Disposition", "attachment;filename=" + new String((fileName + ".xlsx").getBytes(), "iso-8859-1"));
  277. ServletOutputStream out= null;
  278. try {
  279. out = response.getOutputStream();
  280. writer.flush(out, true);
  281. } catch (IOException e) {
  282. e.printStackTrace();
  283. }finally {
  284. writer.close();
  285. }
  286. IoUtil.close(out);*/
  287. }
  288. @SuppressWarnings("unchecked")
  289. @RequestMapping(params = "importExcel", method = RequestMethod.POST)
  290. @ResponseBody
  291. public AjaxJson importExcel(HttpServletRequest request, HttpServletResponse response) {
  292. AjaxJson j = new AjaxJson();
  293. MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
  294. Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
  295. for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
  296. MultipartFile file = entity.getValue();// 获取上传文件对象
  297. ImportParams params = new ImportParams();
  298. params.setTitleRows(2);
  299. params.setHeadRows(1);
  300. params.setNeedSave(true);
  301. try {
  302. List<DeviceEntity> listDeviceEntitys = ExcelImportUtil.importExcel(file.getInputStream(),DeviceEntity.class,params);
  303. for (DeviceEntity device : listDeviceEntitys) {
  304. deviceService.save(device);
  305. }
  306. j.setMsg("文件导入成功!");
  307. } catch (Exception e) {
  308. j.setMsg("文件导入失败!");
  309. logger.error(ExceptionUtil.getExceptionMessage(e));
  310. }finally{
  311. try {
  312. file.getInputStream().close();
  313. } catch (IOException e) {
  314. e.printStackTrace();
  315. }
  316. }
  317. }
  318. return j;
  319. }
  320. /**
  321. * 获取文件附件信息
  322. *
  323. * @param id device主键id
  324. */
  325. @RequestMapping(params = "getFiles")
  326. @ResponseBody
  327. public AjaxJson getFiles(String id){
  328. List<CgUploadEntity> uploadBeans = cgFormFieldService.findByProperty(CgUploadEntity.class, "cgformId", id);
  329. List<Map<String,Object>> files = new ArrayList<Map<String,Object>>(0);
  330. for(CgUploadEntity b:uploadBeans){
  331. String title = b.getAttachmenttitle();//附件名
  332. String fileKey = b.getId();//附件主键
  333. String path = b.getRealpath();//附件路径
  334. String field = b.getCgformField();//表单中作为附件控件的字段
  335. Map<String, Object> file = new HashMap<String, Object>();
  336. file.put("title", title);
  337. file.put("fileKey", fileKey);
  338. file.put("path", path);
  339. file.put("field", field==null?"":field);
  340. files.add(file);
  341. }
  342. AjaxJson j = new AjaxJson();
  343. j.setObj(files);
  344. return j;
  345. }
  346. @RequestMapping(method = RequestMethod.GET)
  347. @ResponseBody
  348. public List<DeviceEntity> list() {
  349. List<DeviceEntity> listDevices=deviceService.getList(DeviceEntity.class);
  350. return listDevices;
  351. }
  352. @RequestMapping(value = "/{id}", method = RequestMethod.GET)
  353. @ResponseBody
  354. public ResponseEntity<?> get(@PathVariable("id") String id) {
  355. DeviceEntity task = deviceService.get(DeviceEntity.class, id);
  356. if (task == null) {
  357. return new ResponseEntity(HttpStatus.NOT_FOUND);
  358. }
  359. return new ResponseEntity(task, HttpStatus.OK);
  360. }
  361. @RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
  362. @ResponseBody
  363. public ResponseEntity<?> create(@RequestBody DeviceEntity device, UriComponentsBuilder uriBuilder) {
  364. //调用JSR303 Bean Validator进行校验,如果出错返回含400错误码及json格式的错误信息.
  365. Set<ConstraintViolation<DeviceEntity>> failures = validator.validate(device);
  366. if (!failures.isEmpty()) {
  367. return new ResponseEntity(BeanValidators.extractPropertyAndMessage(failures), HttpStatus.BAD_REQUEST);
  368. }
  369. //保存
  370. try{
  371. deviceService.save(device);
  372. } catch (Exception e) {
  373. e.printStackTrace();
  374. return new ResponseEntity(HttpStatus.NO_CONTENT);
  375. }
  376. //按照Restful风格约定,创建指向新任务的url, 也可以直接返回id或对象.
  377. String id = device.getId();
  378. URI uri = uriBuilder.path("/rest/deviceController/" + id).build().toUri();
  379. HttpHeaders headers = new HttpHeaders();
  380. headers.setLocation(uri);
  381. return new ResponseEntity(headers, HttpStatus.CREATED);
  382. }
  383. @RequestMapping(value = "/{id}", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE)
  384. public ResponseEntity<?> update(@RequestBody DeviceEntity device) {
  385. //调用JSR303 Bean Validator进行校验,如果出错返回含400错误码及json格式的错误信息.
  386. Set<ConstraintViolation<DeviceEntity>> failures = validator.validate(device);
  387. if (!failures.isEmpty()) {
  388. return new ResponseEntity(BeanValidators.extractPropertyAndMessage(failures), HttpStatus.BAD_REQUEST);
  389. }
  390. //保存
  391. try{
  392. deviceService.saveOrUpdate(device);
  393. } catch (Exception e) {
  394. e.printStackTrace();
  395. return new ResponseEntity(HttpStatus.NO_CONTENT);
  396. }
  397. //按Restful约定,返回204状态码, 无内容. 也可以返回200状态码.
  398. return new ResponseEntity(HttpStatus.NO_CONTENT);
  399. }
  400. @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
  401. @ResponseStatus(HttpStatus.NO_CONTENT)
  402. public void delete(@PathVariable("id") String id) {
  403. deviceService.deleteEntityById(DeviceEntity.class, id);
  404. }
  405. }