TBOvertimeBudgetController.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. package cn.com.lzt.overtimebudget.controller;
  2. import cn.com.lzt.overtimebudget.dto.TBOvertimeBudgetAddDto;
  3. import cn.com.lzt.overtimebudget.entity.TBOvertimeBudgetEntity;
  4. import cn.com.lzt.overtimebudget.service.TBOvertimeBudgetServiceI;
  5. import cn.com.lzt.useractiviti.data.service.UseractivitiDataServiceI;
  6. import org.apache.commons.lang.StringUtils;
  7. import org.apache.log4j.Logger;
  8. import org.jeecgframework.core.beanvalidator.BeanValidators;
  9. import org.jeecgframework.core.common.controller.BaseController;
  10. import org.jeecgframework.core.common.exception.BusinessException;
  11. import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
  12. import org.jeecgframework.core.common.model.json.AjaxJson;
  13. import org.jeecgframework.core.common.model.json.DataGrid;
  14. import org.jeecgframework.core.constant.Globals;
  15. import org.jeecgframework.core.util.ExceptionUtil;
  16. import org.jeecgframework.core.util.ResourceUtil;
  17. import org.jeecgframework.core.util.StringUtil;
  18. import org.jeecgframework.poi.excel.ExcelImportUtil;
  19. import org.jeecgframework.poi.excel.entity.ExportParams;
  20. import org.jeecgframework.poi.excel.entity.ImportParams;
  21. import org.jeecgframework.poi.excel.entity.vo.NormalExcelConstants;
  22. import org.jeecgframework.tag.core.easyui.TagUtil;
  23. import org.jeecgframework.web.system.pojo.base.TSType;
  24. import org.jeecgframework.web.system.service.SystemService;
  25. import org.springframework.beans.factory.annotation.Autowired;
  26. import org.springframework.http.HttpHeaders;
  27. import org.springframework.http.HttpStatus;
  28. import org.springframework.http.MediaType;
  29. import org.springframework.http.ResponseEntity;
  30. import org.springframework.stereotype.Controller;
  31. import org.springframework.ui.ModelMap;
  32. import org.springframework.web.bind.annotation.*;
  33. import org.springframework.web.multipart.MultipartFile;
  34. import org.springframework.web.multipart.MultipartHttpServletRequest;
  35. import org.springframework.web.servlet.ModelAndView;
  36. import org.springframework.web.util.UriComponentsBuilder;
  37. import javax.servlet.http.HttpServletRequest;
  38. import javax.servlet.http.HttpServletResponse;
  39. import javax.validation.ConstraintViolation;
  40. import javax.validation.Validator;
  41. import java.io.IOException;
  42. import java.net.URI;
  43. import java.util.*;
  44. /**
  45. * @Title: Controller
  46. * @Description: 加班预算
  47. * @author onlineGenerator
  48. * @date 2019-10-16 16:55:19
  49. * @version V1.0
  50. *
  51. */
  52. @Controller
  53. @RequestMapping("/tBOvertimeBudgetController")
  54. public class TBOvertimeBudgetController extends BaseController {
  55. /**
  56. * Logger for this class
  57. */
  58. private static final Logger logger = Logger.getLogger(TBOvertimeBudgetController.class);
  59. @Autowired
  60. private TBOvertimeBudgetServiceI tBOvertimeBudgetService;
  61. @Autowired
  62. private SystemService systemService;
  63. @Autowired
  64. private Validator validator;
  65. @Autowired
  66. private UseractivitiDataServiceI useractivitiDataService;
  67. /**
  68. * 加班预算列表 页面跳转
  69. *
  70. * @return
  71. */
  72. @RequestMapping(params = "list")
  73. public ModelAndView list(HttpServletRequest request) {
  74. return new ModelAndView("cn/com/lzt/overtimebudget/tBOvertimeBudgetList");
  75. }
  76. /**
  77. * easyui AJAX请求数据
  78. *
  79. * @param request
  80. * @param response
  81. * @param dataGrid
  82. */
  83. @RequestMapping(params = "datagrid")
  84. public void datagrid(TBOvertimeBudgetEntity tBOvertimeBudget,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
  85. CriteriaQuery cq = new CriteriaQuery(TBOvertimeBudgetEntity.class, dataGrid);
  86. //查询条件组装器
  87. org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, tBOvertimeBudget, request.getParameterMap());
  88. try{
  89. //自定义追加查询条件
  90. }catch (Exception e) {
  91. throw new BusinessException(e.getMessage());
  92. }
  93. cq.add();
  94. this.tBOvertimeBudgetService.getDataGridReturn(cq, true);
  95. TagUtil.datagrid(response, dataGrid);
  96. }
  97. /**
  98. * 删除加班预算
  99. *
  100. * @return
  101. */
  102. @RequestMapping(params = "doDel")
  103. @ResponseBody
  104. public AjaxJson doDel(TBOvertimeBudgetEntity tBOvertimeBudget, HttpServletRequest request) {
  105. String message = null;
  106. AjaxJson j = new AjaxJson();
  107. tBOvertimeBudget = systemService.getEntity(TBOvertimeBudgetEntity.class, tBOvertimeBudget.getId());
  108. message = "加班预算删除成功";
  109. try{
  110. tBOvertimeBudgetService.delete(tBOvertimeBudget);
  111. systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
  112. }catch(Exception e){
  113. e.printStackTrace();
  114. message = "加班预算删除失败";
  115. throw new BusinessException(e.getMessage());
  116. }
  117. j.setMsg(message);
  118. return j;
  119. }
  120. /**
  121. * 批量删除加班预算
  122. *
  123. * @return
  124. */
  125. @RequestMapping(params = "doBatchDel")
  126. @ResponseBody
  127. public AjaxJson doBatchDel(String ids,HttpServletRequest request){
  128. String message = null;
  129. AjaxJson j = new AjaxJson();
  130. message = "加班预算删除成功";
  131. try{
  132. for(String id:ids.split(",")){
  133. TBOvertimeBudgetEntity tBOvertimeBudget = systemService.getEntity(TBOvertimeBudgetEntity.class,
  134. id
  135. );
  136. tBOvertimeBudgetService.delete(tBOvertimeBudget);
  137. systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
  138. }
  139. }catch(Exception e){
  140. e.printStackTrace();
  141. message = "加班预算删除失败";
  142. throw new BusinessException(e.getMessage());
  143. }
  144. j.setMsg(message);
  145. return j;
  146. }
  147. /**
  148. * 添加加班预算
  149. *
  150. * @return
  151. */
  152. @RequestMapping(params = "doAdd")
  153. @ResponseBody
  154. public AjaxJson doAdd(TBOvertimeBudgetAddDto dto, HttpServletRequest request) {
  155. String message = null;
  156. AjaxJson j = new AjaxJson();
  157. message = "加班预算添加成功";
  158. try{
  159. TBOvertimeBudgetEntity firstEntity = dto.getEntityList().get(0);
  160. if(StringUtils.isBlank(firstEntity.getId())) {//添加时判断是否存在
  161. String querySql = "select 1 from t_b_overtime_budget where depart_id='%s' and years=%s";
  162. List<?> list = systemService.findListbySql(String.format(querySql, firstEntity.getDepartId(), firstEntity.getYears()));
  163. if (list.size() > 0) {
  164. throw new BusinessException("加班预算已存在");
  165. }
  166. }
  167. String depId = firstEntity.getDepartId();
  168. String deleteSql = "delete from t_b_overtime_budget where depart_id=? and years =? ";
  169. int delCount = systemService.executeSql(deleteSql,depId,firstEntity.getYears());
  170. if(delCount>0){
  171. message="加班预算修改成功";
  172. }
  173. for(TBOvertimeBudgetEntity entity:dto.getEntityList()){
  174. entity.setDepartId(firstEntity.getDepartId());
  175. entity.setDepName(firstEntity.getDepName());
  176. entity.setYears(firstEntity.getYears());
  177. tBOvertimeBudgetService.save(entity);
  178. }
  179. systemService.addLog(message, Globals.Log_Type_INSERT, Globals.Log_Leavel_INFO);
  180. }catch(Exception e){
  181. e.printStackTrace();
  182. message = "加班预算添加失败";
  183. throw new BusinessException(e.getMessage());
  184. }
  185. j.setMsg(message);
  186. return j;
  187. }
  188. /**
  189. * 更新加班预算
  190. *
  191. * @return
  192. */
  193. @RequestMapping(params = "doUpdate")
  194. @ResponseBody
  195. public AjaxJson doUpdate(TBOvertimeBudgetAddDto dto, HttpServletRequest request) {
  196. String message = null;
  197. AjaxJson j = new AjaxJson();
  198. message = "加班预算更新成功";
  199. /*TBOvertimeBudgetEntity t = tBOvertimeBudgetService.get(TBOvertimeBudgetEntity.class, tBOvertimeBudget.getId());
  200. try {
  201. MyBeanUtils.copyBeanNotNull2Bean(tBOvertimeBudget, t);
  202. tBOvertimeBudgetService.saveOrUpdate(t);
  203. systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
  204. } catch (Exception e) {
  205. e.printStackTrace();
  206. message = "加班预算更新失败";
  207. throw new BusinessException(e.getMessage());
  208. }*/
  209. j.setMsg(message);
  210. return j;
  211. }
  212. /**
  213. * 加班预算新增页面跳转
  214. *
  215. * @return
  216. */
  217. @RequestMapping(params = "goAdd")
  218. public ModelAndView goAdd(TBOvertimeBudgetEntity tBOvertimeBudget, HttpServletRequest req) {
  219. Calendar c = Calendar.getInstance();
  220. int year = c.get(Calendar.YEAR);
  221. req.setAttribute("year",year);
  222. List<TSType> typeList = systemService.getTypesByGroupcodeFromCache("overtime_budget_type");
  223. req.setAttribute("typeList",typeList);
  224. return new ModelAndView("cn/com/lzt/overtimebudget/tBOvertimeBudget-add");
  225. }
  226. /**
  227. * 加班预算编辑页面跳转
  228. *
  229. * @return
  230. */
  231. @RequestMapping(params = "goUpdate")
  232. public ModelAndView goUpdate(TBOvertimeBudgetEntity tBOvertimeBudget, HttpServletRequest req) {
  233. if (StringUtil.isNotEmpty(tBOvertimeBudget.getId())) {
  234. tBOvertimeBudget = tBOvertimeBudgetService.getEntity(TBOvertimeBudgetEntity.class, tBOvertimeBudget.getId());
  235. req.setAttribute("firstEntity", tBOvertimeBudget);
  236. List<TBOvertimeBudgetEntity> yearDataList =
  237. systemService.findHql("from TBOvertimeBudgetEntity where departId=? and years=? "
  238. ,tBOvertimeBudget.getDepartId(),tBOvertimeBudget.getYears());
  239. Map<String,TBOvertimeBudgetEntity> yearDataMap = new HashMap<>(yearDataList.size());
  240. for(TBOvertimeBudgetEntity entity:yearDataList){
  241. yearDataMap.put(entity.getBudgetType(),entity);
  242. }
  243. List<TSType> typeList = systemService.getTypesByGroupcodeFromCache("overtime_budget_type");
  244. req.setAttribute("typeList",typeList);
  245. req.setAttribute("yearDataMap",yearDataMap);
  246. }
  247. return new ModelAndView("cn/com/lzt/overtimebudget/tBOvertimeBudget-add");
  248. }
  249. /**
  250. * 导入功能跳转
  251. *
  252. * @return
  253. */
  254. @RequestMapping(params = "upload")
  255. public ModelAndView upload(HttpServletRequest req) {
  256. req.setAttribute("controller_name","tBOvertimeBudgetController");
  257. return new ModelAndView("common/upload/pub_excel_upload");
  258. }
  259. /**
  260. * 导出excel
  261. *
  262. * @param request
  263. * @param response
  264. */
  265. @RequestMapping(params = "exportXls")
  266. public String exportXls(TBOvertimeBudgetEntity tBOvertimeBudget,HttpServletRequest request,HttpServletResponse response
  267. , DataGrid dataGrid,ModelMap modelMap) {
  268. CriteriaQuery cq = new CriteriaQuery(TBOvertimeBudgetEntity.class, dataGrid);
  269. org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, tBOvertimeBudget, request.getParameterMap());
  270. List<TBOvertimeBudgetEntity> tBOvertimeBudgets = this.tBOvertimeBudgetService.getListByCriteriaQuery(cq,false);
  271. modelMap.put(NormalExcelConstants.FILE_NAME,"加班预算");
  272. modelMap.put(NormalExcelConstants.CLASS,TBOvertimeBudgetEntity.class);
  273. modelMap.put(NormalExcelConstants.PARAMS,new ExportParams("加班预算列表", "导出人:"+ResourceUtil.getSessionUser().getRealName(),
  274. "导出信息"));
  275. modelMap.put(NormalExcelConstants.DATA_LIST,tBOvertimeBudgets);
  276. return NormalExcelConstants.JEECG_EXCEL_VIEW;
  277. }
  278. /**
  279. * 导出excel 使模板
  280. *
  281. * @param request
  282. * @param response
  283. */
  284. @RequestMapping(params = "exportXlsByT")
  285. public String exportXlsByT(TBOvertimeBudgetEntity tBOvertimeBudget,HttpServletRequest request,HttpServletResponse response
  286. , DataGrid dataGrid,ModelMap modelMap) {
  287. modelMap.put(NormalExcelConstants.FILE_NAME,"加班预算");
  288. modelMap.put(NormalExcelConstants.CLASS,TBOvertimeBudgetEntity.class);
  289. modelMap.put(NormalExcelConstants.PARAMS,new ExportParams("加班预算列表", "导出人:"+ResourceUtil.getSessionUser().getRealName(),
  290. "导出信息"));
  291. modelMap.put(NormalExcelConstants.DATA_LIST,new ArrayList());
  292. return NormalExcelConstants.JEECG_EXCEL_VIEW;
  293. }
  294. @SuppressWarnings("unchecked")
  295. @RequestMapping(params = "importExcel", method = RequestMethod.POST)
  296. @ResponseBody
  297. public AjaxJson importExcel(HttpServletRequest request, HttpServletResponse response) {
  298. AjaxJson j = new AjaxJson();
  299. MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
  300. Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
  301. for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
  302. MultipartFile file = entity.getValue();// 获取上传文件对象
  303. ImportParams params = new ImportParams();
  304. params.setTitleRows(2);
  305. params.setHeadRows(1);
  306. params.setNeedSave(true);
  307. try {
  308. List<TBOvertimeBudgetEntity> listTBOvertimeBudgetEntitys = ExcelImportUtil.importExcel(file.getInputStream(),TBOvertimeBudgetEntity.class,params);
  309. for (TBOvertimeBudgetEntity tBOvertimeBudget : listTBOvertimeBudgetEntitys) {
  310. tBOvertimeBudgetService.save(tBOvertimeBudget);
  311. }
  312. j.setMsg("文件导入成功!");
  313. } catch (Exception e) {
  314. j.setMsg("文件导入失败!");
  315. logger.error(ExceptionUtil.getExceptionMessage(e));
  316. }finally{
  317. try {
  318. file.getInputStream().close();
  319. } catch (IOException e) {
  320. e.printStackTrace();
  321. }
  322. }
  323. }
  324. return j;
  325. }
  326. @RequestMapping(method = RequestMethod.GET)
  327. @ResponseBody
  328. public List<TBOvertimeBudgetEntity> list() {
  329. List<TBOvertimeBudgetEntity> listTBOvertimeBudgets=tBOvertimeBudgetService.getList(TBOvertimeBudgetEntity.class);
  330. return listTBOvertimeBudgets;
  331. }
  332. @RequestMapping(value = "/{id}", method = RequestMethod.GET)
  333. @ResponseBody
  334. public ResponseEntity<?> get(@PathVariable("id") String id) {
  335. TBOvertimeBudgetEntity task = tBOvertimeBudgetService.get(TBOvertimeBudgetEntity.class, id);
  336. if (task == null) {
  337. return new ResponseEntity(HttpStatus.NOT_FOUND);
  338. }
  339. return new ResponseEntity(task, HttpStatus.OK);
  340. }
  341. @RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
  342. @ResponseBody
  343. public ResponseEntity<?> create(@RequestBody TBOvertimeBudgetEntity tBOvertimeBudget, UriComponentsBuilder uriBuilder) {
  344. //调用JSR303 Bean Validator进行校验,如果出错返回含400错误码及json格式的错误信息.
  345. Set<ConstraintViolation<TBOvertimeBudgetEntity>> failures = validator.validate(tBOvertimeBudget);
  346. if (!failures.isEmpty()) {
  347. return new ResponseEntity(BeanValidators.extractPropertyAndMessage(failures), HttpStatus.BAD_REQUEST);
  348. }
  349. //保存
  350. try{
  351. tBOvertimeBudgetService.save(tBOvertimeBudget);
  352. } catch (Exception e) {
  353. e.printStackTrace();
  354. return new ResponseEntity(HttpStatus.NO_CONTENT);
  355. }
  356. //按照Restful风格约定,创建指向新任务的url, 也可以直接返回id或对象.
  357. String id = tBOvertimeBudget.getId();
  358. URI uri = uriBuilder.path("/rest/tBOvertimeBudgetController/" + id).build().toUri();
  359. HttpHeaders headers = new HttpHeaders();
  360. headers.setLocation(uri);
  361. return new ResponseEntity(headers, HttpStatus.CREATED);
  362. }
  363. @RequestMapping(value = "/{id}", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE)
  364. public ResponseEntity<?> update(@RequestBody TBOvertimeBudgetEntity tBOvertimeBudget) {
  365. //调用JSR303 Bean Validator进行校验,如果出错返回含400错误码及json格式的错误信息.
  366. Set<ConstraintViolation<TBOvertimeBudgetEntity>> failures = validator.validate(tBOvertimeBudget);
  367. if (!failures.isEmpty()) {
  368. return new ResponseEntity(BeanValidators.extractPropertyAndMessage(failures), HttpStatus.BAD_REQUEST);
  369. }
  370. //保存
  371. try{
  372. tBOvertimeBudgetService.saveOrUpdate(tBOvertimeBudget);
  373. } catch (Exception e) {
  374. e.printStackTrace();
  375. return new ResponseEntity(HttpStatus.NO_CONTENT);
  376. }
  377. //按Restful约定,返回204状态码, 无内容. 也可以返回200状态码.
  378. return new ResponseEntity(HttpStatus.NO_CONTENT);
  379. }
  380. @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
  381. @ResponseStatus(HttpStatus.NO_CONTENT)
  382. public void delete(@PathVariable("id") String id) {
  383. tBOvertimeBudgetService.deleteEntityById(TBOvertimeBudgetEntity.class, id);
  384. }
  385. //检查项目岗位编制
  386. @RequestMapping(params = "checkPost")
  387. @ResponseBody
  388. public AjaxJson checkPost(@RequestParam("departId") String departId,@RequestParam("postId") String postId) {
  389. AjaxJson j = new AjaxJson();
  390. return j;
  391. }
  392. }