UserFinancialController.java 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  1. package cn.com.lzt.userfinancial.controller;
  2. import cn.com.lzt.userfinancial.entity.UserFinancialEntity;
  3. import cn.com.lzt.userfinancial.service.UserFinancialServiceI;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. import org.apache.log4j.Logger;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.stereotype.Controller;
  11. import org.springframework.ui.ModelMap;
  12. import org.springframework.web.bind.annotation.RequestMapping;
  13. import org.springframework.web.bind.annotation.ResponseBody;
  14. import org.springframework.web.servlet.ModelAndView;
  15. import org.jeecgframework.core.common.controller.BaseController;
  16. import org.jeecgframework.core.common.exception.BusinessException;
  17. import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
  18. import org.jeecgframework.core.common.model.json.AjaxJson;
  19. import org.jeecgframework.core.common.model.json.DataGrid;
  20. import org.jeecgframework.core.constant.Globals;
  21. import org.jeecgframework.core.util.StringUtil;
  22. import org.jeecgframework.tag.core.easyui.TagUtil;
  23. import org.jeecgframework.web.system.service.SystemService;
  24. import org.jeecgframework.core.util.MyBeanUtils;
  25. import org.jeecgframework.poi.excel.ExcelImportUtil;
  26. import org.jeecgframework.poi.excel.entity.ExportParams;
  27. import org.jeecgframework.poi.excel.entity.ImportParams;
  28. import org.jeecgframework.poi.excel.entity.vo.NormalExcelConstants;
  29. import org.jeecgframework.core.util.ResourceUtil;
  30. import java.io.IOException;
  31. import org.springframework.web.bind.annotation.RequestMethod;
  32. import org.springframework.web.multipart.MultipartFile;
  33. import org.springframework.web.multipart.MultipartHttpServletRequest;
  34. import java.util.Map;
  35. import org.jeecgframework.core.util.ExceptionUtil;
  36. import org.springframework.http.ResponseEntity;
  37. import org.springframework.web.bind.annotation.PathVariable;
  38. import org.springframework.web.bind.annotation.RequestBody;
  39. import org.springframework.web.bind.annotation.ResponseStatus;
  40. import org.springframework.http.HttpHeaders;
  41. import org.springframework.http.HttpStatus;
  42. import org.jeecgframework.core.beanvalidator.BeanValidators;
  43. import java.util.Set;
  44. import javax.validation.ConstraintViolation;
  45. import javax.validation.Validator;
  46. import java.net.URI;
  47. import org.springframework.http.MediaType;
  48. import org.springframework.web.util.UriComponentsBuilder;
  49. /**
  50. * @Title: Controller
  51. * @Description: 员工财务信息表
  52. * @author onlineGenerator
  53. * @date 2017-10-25 18:41:40
  54. * @version V1.0
  55. *
  56. */
  57. @Controller
  58. @RequestMapping("/userFinancialController")
  59. public class UserFinancialController extends BaseController {
  60. /**
  61. * Logger for this class
  62. */
  63. private static final Logger logger = Logger.getLogger(UserFinancialController.class);
  64. @Autowired
  65. private UserFinancialServiceI userFinancialService;
  66. @Autowired
  67. private SystemService systemService;
  68. @Autowired
  69. private Validator validator;
  70. /**
  71. * 员工财务信息表列表 页面跳转
  72. *
  73. * @return
  74. */
  75. @RequestMapping(params = "list")
  76. public ModelAndView list(String userId, HttpServletRequest request) {
  77. request.setAttribute("userId", userId);
  78. return new ModelAndView("cn/com/lzt/userfinancial/userFinancialList");
  79. }
  80. /**
  81. * 员工财务信息表列表 页面跳转
  82. *
  83. * @return
  84. */
  85. @RequestMapping(params = "view")
  86. public ModelAndView view(String userId, HttpServletRequest request) {
  87. request.setAttribute("userId", userId);
  88. return new ModelAndView("cn/com/lzt/userfinancial/userFinancialList-view");
  89. }
  90. /**
  91. * easyui AJAX请求数据
  92. *
  93. * @param request
  94. * @param response
  95. * @param dataGrid
  96. * @param user
  97. */
  98. @RequestMapping(params = "datagrid")
  99. public void datagrid(String userId, UserFinancialEntity userFinancial,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
  100. CriteriaQuery cq = new CriteriaQuery(UserFinancialEntity.class, dataGrid);
  101. //查询条件组装器
  102. org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, userFinancial, request.getParameterMap());
  103. try{
  104. //自定义追加查询条件
  105. cq.notEq("deleteFlag", Globals.Delete_Forbidden.toString());
  106. cq.eq("userid", userId);
  107. }catch (Exception e) {
  108. throw new BusinessException(e.getMessage());
  109. }
  110. cq.add();
  111. this.userFinancialService.getDataGridReturn(cq, true);
  112. TagUtil.datagrid(response, dataGrid);
  113. }
  114. /**
  115. * 保存新增/更新的行数据
  116. * @param page
  117. * @return
  118. */
  119. @RequestMapping(params = "saveRows")
  120. @ResponseBody
  121. public AjaxJson saveRows(UserFinancialEntity userFinancial,HttpServletRequest request){
  122. String message = null;
  123. AjaxJson j = new AjaxJson();
  124. message = "";
  125. String financialHql = "from UserFinancialEntity where userid=? and id <> ? and deleteFlag <> 1";
  126. List<UserFinancialEntity> userFinancialEntityList = userFinancialService.findHql(financialHql, userFinancial.getUserid(), userFinancial.getId());
  127. if (StringUtil.isNotEmpty(userFinancial.getId())) {
  128. // 判断本人是否添加了重复的银行卡号
  129. String financialByCardNumHql = "from UserFinancialEntity where cardNumber =? and userid =? and id <> ? and deleteFlag <> 1";
  130. List<UserFinancialEntity> userFinancialByCardNumEntityList = userFinancialService.findHql(financialByCardNumHql, userFinancial.getCardNumber(), userFinancial.getUserid(), userFinancial.getId());
  131. if(userFinancialByCardNumEntityList.size() > 0) {
  132. message = "银行卡信息更新失败,银行卡号重复,请重新填写";
  133. j.setSuccess(false);
  134. j.setMsg(message);
  135. return j;
  136. }
  137. if("1".equals(userFinancial.getIsdefault())) {
  138. UserFinancialEntity t = userFinancialService.get(UserFinancialEntity.class, userFinancial.getId());
  139. try {
  140. MyBeanUtils.copyBeanNotNull2Bean(userFinancial, t);
  141. userFinancialService.saveOrUpdate(t);
  142. for(UserFinancialEntity userFinancialEntity : userFinancialEntityList) {
  143. userFinancialEntity.setIsdefault("0");
  144. userFinancialService.saveOrUpdate(userFinancialEntity);
  145. }
  146. message = "银行卡信息更新成功";
  147. j.setSuccess(true);
  148. systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
  149. } catch (Exception e) {
  150. e.printStackTrace();
  151. message = "银行卡信息更新失败";
  152. j.setSuccess(false);
  153. throw new BusinessException(e.getMessage());
  154. }
  155. } else {
  156. if(userFinancialEntityList.size() == 0) {
  157. message = "请选择一张默认银行卡!";
  158. j.setSuccess(false);
  159. j.setMsg(message);
  160. return j;
  161. } else {
  162. boolean haveDefault = false;
  163. for(UserFinancialEntity userFinancialEntity : userFinancialEntityList) {
  164. if("1".equals(userFinancialEntity.getIsdefault())) {
  165. haveDefault = true;
  166. break;
  167. }
  168. }
  169. if(haveDefault) {
  170. UserFinancialEntity t = userFinancialService.get(UserFinancialEntity.class, userFinancial.getId());
  171. try {
  172. MyBeanUtils.copyBeanNotNull2Bean(userFinancial, t);
  173. userFinancialService.saveOrUpdate(t);
  174. message = "银行卡信息更新成功";
  175. j.setSuccess(true);
  176. systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
  177. } catch (Exception e) {
  178. e.printStackTrace();
  179. message = "银行卡信息更新失败";
  180. j.setSuccess(false);
  181. throw new BusinessException(e.getMessage());
  182. }
  183. } else {
  184. message = "请选择一张默认银行卡!";
  185. j.setSuccess(false);
  186. j.setMsg(message);
  187. return j;
  188. }
  189. }
  190. }
  191. } else {
  192. // 判断本人是否添加了重复的银行卡号
  193. String financialByCardNumHql = "from UserFinancialEntity where cardNumber =? and userid =? and deleteFlag <> 1";
  194. List<UserFinancialEntity> userFinancialByCardNumEntityList = userFinancialService.findHql(financialByCardNumHql, userFinancial.getCardNumber(), userFinancial.getUserid());
  195. if(userFinancialByCardNumEntityList.size() > 0) {
  196. message = "银行卡信息添加失败,银行卡号重复,请重新填写";
  197. j.setSuccess(false);
  198. j.setMsg(message);
  199. return j;
  200. }
  201. if("1".equals(userFinancial.getIsdefault())) {
  202. try {
  203. for(UserFinancialEntity userFinancialEntity : userFinancialEntityList) {
  204. userFinancialEntity.setIsdefault("0");
  205. userFinancialService.saveOrUpdate(userFinancialEntity);
  206. }
  207. userFinancial.setDeleteFlag(Globals.Delete_Normal.toString());
  208. userFinancial.setStatus(Globals.Enabled_Status.toString());
  209. userFinancialService.save(userFinancial);
  210. message = "银行卡信息添加成功";
  211. j.setSuccess(true);
  212. systemService.addLog(message, Globals.Log_Type_INSERT, Globals.Log_Leavel_INFO);
  213. } catch (Exception e) {
  214. e.printStackTrace();
  215. message = "银行卡信息添加失败";
  216. j.setSuccess(false);
  217. throw new BusinessException(e.getMessage());
  218. }
  219. } else {
  220. if(userFinancialEntityList.size() == 0) {
  221. message = "请选择一张默认银行卡!";
  222. j.setSuccess(false);
  223. j.setMsg(message);
  224. return j;
  225. } else {
  226. boolean haveDefault = false;
  227. for(UserFinancialEntity userFinancialEntity : userFinancialEntityList) {
  228. if("1".equals(userFinancialEntity.getIsdefault())) {
  229. haveDefault = true;
  230. break;
  231. }
  232. }
  233. if(haveDefault) {
  234. try {
  235. userFinancial.setDeleteFlag(Globals.Delete_Normal.toString());
  236. userFinancial.setStatus(Globals.Enabled_Status.toString());
  237. userFinancialService.save(userFinancial);
  238. message = "银行卡信息添加成功";
  239. j.setSuccess(true);
  240. systemService.addLog(message, Globals.Log_Type_INSERT, Globals.Log_Leavel_INFO);
  241. } catch (Exception e) {
  242. e.printStackTrace();
  243. message = "银行卡信息添加失败";
  244. j.setSuccess(false);
  245. throw new BusinessException(e.getMessage());
  246. }
  247. } else {
  248. message = "请选择一张默认银行卡!";
  249. j.setSuccess(false);
  250. j.setMsg(message);
  251. return j;
  252. }
  253. }
  254. }
  255. }
  256. j.setMsg(message);
  257. return j;
  258. }
  259. /**
  260. * 批量逻辑删除财务信息
  261. *
  262. * @return
  263. */
  264. @RequestMapping(params = "doBatchLogicDel")
  265. @ResponseBody
  266. public AjaxJson doBatchLogicDel(String ids,HttpServletRequest request){
  267. String message = null;
  268. AjaxJson j = new AjaxJson();
  269. message = "银行卡信息删除成功";
  270. try{
  271. for(String id:ids.split(",")){
  272. UserFinancialEntity userFinancial = systemService.getEntity(UserFinancialEntity.class, id);
  273. userFinancial.setDeleteFlag(Globals.Delete_Forbidden.toString());
  274. userFinancialService.saveOrUpdate(userFinancial);
  275. systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
  276. j.setSuccess(true);
  277. }
  278. }catch(Exception e){
  279. e.printStackTrace();
  280. message = "银行卡信息删除失败";
  281. j.setSuccess(false);
  282. throw new BusinessException(e.getMessage());
  283. }
  284. j.setMsg(message);
  285. return j;
  286. }
  287. /**
  288. * 启用财务信息
  289. *
  290. * @return
  291. */
  292. @RequestMapping(params = "doEnableObj")
  293. @ResponseBody
  294. public AjaxJson doEnableObj(String ids,HttpServletRequest request){
  295. String message = null;
  296. AjaxJson j = new AjaxJson();
  297. message = "银行卡信息启用成功";
  298. try{
  299. for(String id:ids.split(",")){
  300. UserFinancialEntity userFinancial = systemService.getEntity(UserFinancialEntity.class, id);
  301. userFinancial.setStatus(Globals.Enabled_Status.toString());
  302. userFinancialService.saveOrUpdate(userFinancial);
  303. systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
  304. j.setSuccess(true);
  305. }
  306. }catch(Exception e){
  307. e.printStackTrace();
  308. message = "银行卡信息启用失败";
  309. j.setSuccess(false);
  310. throw new BusinessException(e.getMessage());
  311. }
  312. j.setMsg(message);
  313. return j;
  314. }
  315. /**
  316. * 停用财务信息
  317. *
  318. * @return
  319. */
  320. @RequestMapping(params = "doDisableObj")
  321. @ResponseBody
  322. public AjaxJson doDisableObj(String ids,HttpServletRequest request){
  323. String message = null;
  324. AjaxJson j = new AjaxJson();
  325. message = "银行卡信息停用成功";
  326. try{
  327. for(String id:ids.split(",")){
  328. UserFinancialEntity userFinancial = systemService.getEntity(UserFinancialEntity.class, id);
  329. userFinancial.setStatus(Globals.Disabled_Status.toString());
  330. userFinancialService.saveOrUpdate(userFinancial);
  331. systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
  332. j.setSuccess(true);
  333. }
  334. }catch(Exception e){
  335. e.printStackTrace();
  336. message = "银行卡信息停用失败";
  337. j.setSuccess(false);
  338. throw new BusinessException(e.getMessage());
  339. }
  340. j.setMsg(message);
  341. return j;
  342. }
  343. /**
  344. * 删除员工财务信息表
  345. *
  346. * @return
  347. */
  348. @RequestMapping(params = "doDel")
  349. @ResponseBody
  350. public AjaxJson doDel(UserFinancialEntity userFinancial, HttpServletRequest request) {
  351. String message = null;
  352. AjaxJson j = new AjaxJson();
  353. userFinancial = systemService.getEntity(UserFinancialEntity.class, userFinancial.getId());
  354. message = "员工财务信息表删除成功";
  355. try{
  356. userFinancialService.delete(userFinancial);
  357. systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
  358. }catch(Exception e){
  359. e.printStackTrace();
  360. message = "员工财务信息表删除失败";
  361. throw new BusinessException(e.getMessage());
  362. }
  363. j.setMsg(message);
  364. return j;
  365. }
  366. /**
  367. * 批量删除员工财务信息表
  368. *
  369. * @return
  370. */
  371. @RequestMapping(params = "doBatchDel")
  372. @ResponseBody
  373. public AjaxJson doBatchDel(String ids,HttpServletRequest request){
  374. String message = null;
  375. AjaxJson j = new AjaxJson();
  376. message = "员工财务信息表删除成功";
  377. try{
  378. for(String id:ids.split(",")){
  379. UserFinancialEntity userFinancial = systemService.getEntity(UserFinancialEntity.class,
  380. id
  381. );
  382. userFinancialService.delete(userFinancial);
  383. systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
  384. }
  385. }catch(Exception e){
  386. e.printStackTrace();
  387. message = "员工财务信息表删除失败";
  388. throw new BusinessException(e.getMessage());
  389. }
  390. j.setMsg(message);
  391. return j;
  392. }
  393. /**
  394. * 添加员工财务信息表
  395. *
  396. * @param ids
  397. * @return
  398. */
  399. @RequestMapping(params = "doAdd")
  400. @ResponseBody
  401. public AjaxJson doAdd(UserFinancialEntity userFinancial, HttpServletRequest request) {
  402. String message = null;
  403. AjaxJson j = new AjaxJson();
  404. message = "员工财务信息表添加成功";
  405. try{
  406. userFinancialService.save(userFinancial);
  407. systemService.addLog(message, Globals.Log_Type_INSERT, Globals.Log_Leavel_INFO);
  408. }catch(Exception e){
  409. e.printStackTrace();
  410. message = "员工财务信息表添加失败";
  411. throw new BusinessException(e.getMessage());
  412. }
  413. j.setMsg(message);
  414. return j;
  415. }
  416. /**
  417. * 更新员工财务信息表
  418. *
  419. * @param ids
  420. * @return
  421. */
  422. @RequestMapping(params = "doUpdate")
  423. @ResponseBody
  424. public AjaxJson doUpdate(UserFinancialEntity userFinancial, HttpServletRequest request) {
  425. String message = null;
  426. AjaxJson j = new AjaxJson();
  427. message = "员工财务信息表更新成功";
  428. UserFinancialEntity t = userFinancialService.get(UserFinancialEntity.class, userFinancial.getId());
  429. try {
  430. MyBeanUtils.copyBeanNotNull2Bean(userFinancial, t);
  431. userFinancialService.saveOrUpdate(t);
  432. systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
  433. } catch (Exception e) {
  434. e.printStackTrace();
  435. message = "员工财务信息表更新失败";
  436. throw new BusinessException(e.getMessage());
  437. }
  438. j.setMsg(message);
  439. return j;
  440. }
  441. /**
  442. * 员工财务信息表新增页面跳转
  443. *
  444. * @return
  445. */
  446. @RequestMapping(params = "goAdd")
  447. public ModelAndView goAdd(UserFinancialEntity userFinancial, HttpServletRequest req) {
  448. if (StringUtil.isNotEmpty(userFinancial.getId())) {
  449. userFinancial = userFinancialService.getEntity(UserFinancialEntity.class, userFinancial.getId());
  450. req.setAttribute("userFinancialPage", userFinancial);
  451. }
  452. return new ModelAndView("cn/com/lzt/userfinancial/userFinancial-add");
  453. }
  454. /**
  455. * 员工财务信息表编辑页面跳转
  456. *
  457. * @return
  458. */
  459. @RequestMapping(params = "goUpdate")
  460. public ModelAndView goUpdate(UserFinancialEntity userFinancial, HttpServletRequest req) {
  461. if (StringUtil.isNotEmpty(userFinancial.getId())) {
  462. userFinancial = userFinancialService.getEntity(UserFinancialEntity.class, userFinancial.getId());
  463. req.setAttribute("userFinancialPage", userFinancial);
  464. }
  465. return new ModelAndView("cn/com/lzt/userfinancial/userFinancial-update");
  466. }
  467. /**
  468. * 导入功能跳转
  469. *
  470. * @return
  471. */
  472. @RequestMapping(params = "upload")
  473. public ModelAndView upload(HttpServletRequest req) {
  474. req.setAttribute("controller_name","userFinancialController");
  475. return new ModelAndView("common/upload/pub_excel_upload");
  476. }
  477. /**
  478. * 导出excel
  479. *
  480. * @param request
  481. * @param response
  482. */
  483. @RequestMapping(params = "exportXls")
  484. public String exportXls(UserFinancialEntity userFinancial,HttpServletRequest request,HttpServletResponse response
  485. , DataGrid dataGrid,ModelMap modelMap) {
  486. CriteriaQuery cq = new CriteriaQuery(UserFinancialEntity.class, dataGrid);
  487. org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, userFinancial, request.getParameterMap());
  488. List<UserFinancialEntity> userFinancials = this.userFinancialService.getListByCriteriaQuery(cq,false);
  489. modelMap.put(NormalExcelConstants.FILE_NAME,"员工财务信息表");
  490. modelMap.put(NormalExcelConstants.CLASS,UserFinancialEntity.class);
  491. modelMap.put(NormalExcelConstants.PARAMS,new ExportParams("员工财务信息表列表", "导出人:"+ResourceUtil.getSessionUser().getRealName(),
  492. "导出信息"));
  493. modelMap.put(NormalExcelConstants.DATA_LIST,userFinancials);
  494. return NormalExcelConstants.JEECG_EXCEL_VIEW;
  495. }
  496. /**
  497. * 导出excel 使模板
  498. *
  499. * @param request
  500. * @param response
  501. */
  502. @RequestMapping(params = "exportXlsByT")
  503. public String exportXlsByT(UserFinancialEntity userFinancial,HttpServletRequest request,HttpServletResponse response
  504. , DataGrid dataGrid,ModelMap modelMap) {
  505. modelMap.put(NormalExcelConstants.FILE_NAME,"员工财务信息表");
  506. modelMap.put(NormalExcelConstants.CLASS,UserFinancialEntity.class);
  507. modelMap.put(NormalExcelConstants.PARAMS,new ExportParams("员工财务信息表列表", "导出人:"+ResourceUtil.getSessionUser().getRealName(),
  508. "导出信息"));
  509. modelMap.put(NormalExcelConstants.DATA_LIST,new ArrayList());
  510. return NormalExcelConstants.JEECG_EXCEL_VIEW;
  511. }
  512. @SuppressWarnings("unchecked")
  513. @RequestMapping(params = "importExcel", method = RequestMethod.POST)
  514. @ResponseBody
  515. public AjaxJson importExcel(HttpServletRequest request, HttpServletResponse response) {
  516. AjaxJson j = new AjaxJson();
  517. MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
  518. Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
  519. for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
  520. MultipartFile file = entity.getValue();// 获取上传文件对象
  521. ImportParams params = new ImportParams();
  522. params.setTitleRows(2);
  523. params.setHeadRows(1);
  524. params.setNeedSave(true);
  525. try {
  526. List<UserFinancialEntity> listUserFinancialEntitys = ExcelImportUtil.importExcel(file.getInputStream(),UserFinancialEntity.class,params);
  527. for (UserFinancialEntity userFinancial : listUserFinancialEntitys) {
  528. userFinancialService.save(userFinancial);
  529. }
  530. j.setMsg("文件导入成功!");
  531. } catch (Exception e) {
  532. j.setMsg("文件导入失败!");
  533. logger.error(ExceptionUtil.getExceptionMessage(e));
  534. }finally{
  535. try {
  536. file.getInputStream().close();
  537. } catch (IOException e) {
  538. e.printStackTrace();
  539. }
  540. }
  541. }
  542. return j;
  543. }
  544. @RequestMapping(method = RequestMethod.GET)
  545. @ResponseBody
  546. public List<UserFinancialEntity> list() {
  547. List<UserFinancialEntity> listUserFinancials=userFinancialService.getList(UserFinancialEntity.class);
  548. return listUserFinancials;
  549. }
  550. @RequestMapping(value = "/{id}", method = RequestMethod.GET)
  551. @ResponseBody
  552. public ResponseEntity<?> get(@PathVariable("id") String id) {
  553. UserFinancialEntity task = userFinancialService.get(UserFinancialEntity.class, id);
  554. if (task == null) {
  555. return new ResponseEntity(HttpStatus.NOT_FOUND);
  556. }
  557. return new ResponseEntity(task, HttpStatus.OK);
  558. }
  559. @RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
  560. @ResponseBody
  561. public ResponseEntity<?> create(@RequestBody UserFinancialEntity userFinancial, UriComponentsBuilder uriBuilder) {
  562. //调用JSR303 Bean Validator进行校验,如果出错返回含400错误码及json格式的错误信息.
  563. Set<ConstraintViolation<UserFinancialEntity>> failures = validator.validate(userFinancial);
  564. if (!failures.isEmpty()) {
  565. return new ResponseEntity(BeanValidators.extractPropertyAndMessage(failures), HttpStatus.BAD_REQUEST);
  566. }
  567. //保存
  568. try{
  569. userFinancialService.save(userFinancial);
  570. } catch (Exception e) {
  571. e.printStackTrace();
  572. return new ResponseEntity(HttpStatus.NO_CONTENT);
  573. }
  574. //按照Restful风格约定,创建指向新任务的url, 也可以直接返回id或对象.
  575. String id = userFinancial.getId();
  576. URI uri = uriBuilder.path("/rest/userFinancialController/" + id).build().toUri();
  577. HttpHeaders headers = new HttpHeaders();
  578. headers.setLocation(uri);
  579. return new ResponseEntity(headers, HttpStatus.CREATED);
  580. }
  581. @RequestMapping(value = "/{id}", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE)
  582. public ResponseEntity<?> update(@RequestBody UserFinancialEntity userFinancial) {
  583. //调用JSR303 Bean Validator进行校验,如果出错返回含400错误码及json格式的错误信息.
  584. Set<ConstraintViolation<UserFinancialEntity>> failures = validator.validate(userFinancial);
  585. if (!failures.isEmpty()) {
  586. return new ResponseEntity(BeanValidators.extractPropertyAndMessage(failures), HttpStatus.BAD_REQUEST);
  587. }
  588. //保存
  589. try{
  590. userFinancialService.saveOrUpdate(userFinancial);
  591. } catch (Exception e) {
  592. e.printStackTrace();
  593. return new ResponseEntity(HttpStatus.NO_CONTENT);
  594. }
  595. //按Restful约定,返回204状态码, 无内容. 也可以返回200状态码.
  596. return new ResponseEntity(HttpStatus.NO_CONTENT);
  597. }
  598. @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
  599. @ResponseStatus(HttpStatus.NO_CONTENT)
  600. public void delete(@PathVariable("id") String id) {
  601. userFinancialService.deleteEntityById(UserFinancialEntity.class, id);
  602. }
  603. }