BudgetTempSheetController.java 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943
  1. package cn.com.lzt.budget.tempsheet.controller;
  2. import cn.com.lzt.budget.control.entity.BudgetControlEntity;
  3. import cn.com.lzt.budget.data.dto.BudgetDimMatchDto;
  4. import cn.com.lzt.budget.data.service.BudgetDataService;
  5. import cn.com.lzt.budget.data.util.BudgetConstant;
  6. import cn.com.lzt.budget.data.util.BudgetUtils;
  7. import cn.com.lzt.budget.data.util.SpreadJson;
  8. import cn.com.lzt.budget.dimfunction.entity.BudgetDimFunctionEntity;
  9. import cn.com.lzt.budget.entity.entity.BudgetEntity;
  10. import cn.com.lzt.budget.measure.entity.BudgetMeasureEntity;
  11. import cn.com.lzt.budget.period.entity.BudgetPeriodEntity;
  12. import cn.com.lzt.budget.perioddetail.entity.BudgetPeriodDetailEntity;
  13. import cn.com.lzt.budget.perioddetail.service.BudgetPeriodDetailServiceI;
  14. import cn.com.lzt.budget.tempsheet.entity.BudgetTempSheetEntity;
  15. import cn.com.lzt.budget.tempsheet.service.BudgetTempSheetServiceI;
  16. import cn.com.lzt.budget.tempsheetentity.entity.BudgetTempSheetEntityMap;
  17. import cn.com.lzt.budget.tempsheetmeasure.entity.BudgetTempSheetMeasureEntity;
  18. import cn.com.lzt.budget.tempsheetmeasure.service.BudgetTempSheetMeasureServiceI;
  19. import cn.com.lzt.budget.writeback.service.BudgetWriteBackServiceI;
  20. import cn.com.lzt.common.util.DictUtil;
  21. import cn.com.lzt.goodscategory.entity.TBGoodsCategoryEntity;
  22. import org.apache.commons.lang.xwork.StringUtils;
  23. import org.apache.commons.lang3.math.NumberUtils;
  24. import org.apache.log4j.Logger;
  25. import org.apache.poi.xwpf.usermodel.*;
  26. import org.codehaus.jackson.map.ObjectMapper;
  27. import org.jeecgframework.core.beanvalidator.BeanValidators;
  28. import org.jeecgframework.core.common.controller.BaseController;
  29. import org.jeecgframework.core.common.exception.BusinessException;
  30. import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
  31. import org.jeecgframework.core.common.model.json.AjaxJson;
  32. import org.jeecgframework.core.common.model.json.DataGrid;
  33. import org.jeecgframework.core.constant.Globals;
  34. import org.jeecgframework.core.util.*;
  35. import org.jeecgframework.poi.excel.ExcelImportUtil;
  36. import org.jeecgframework.poi.excel.entity.ExportParams;
  37. import org.jeecgframework.poi.excel.entity.ImportParams;
  38. import org.jeecgframework.poi.excel.entity.vo.NormalExcelConstants;
  39. import org.jeecgframework.tag.core.easyui.TagUtil;
  40. import org.jeecgframework.web.system.pojo.base.TSType;
  41. import org.jeecgframework.web.system.service.SystemService;
  42. import org.springframework.beans.factory.annotation.Autowired;
  43. import org.springframework.http.HttpHeaders;
  44. import org.springframework.http.HttpStatus;
  45. import org.springframework.http.MediaType;
  46. import org.springframework.http.ResponseEntity;
  47. import org.springframework.stereotype.Controller;
  48. import org.springframework.ui.ModelMap;
  49. import org.springframework.web.bind.annotation.*;
  50. import org.springframework.web.multipart.MultipartFile;
  51. import org.springframework.web.multipart.MultipartHttpServletRequest;
  52. import org.springframework.web.servlet.ModelAndView;
  53. import org.springframework.web.util.UriComponentsBuilder;
  54. import javax.servlet.http.HttpServletRequest;
  55. import javax.servlet.http.HttpServletResponse;
  56. import javax.validation.ConstraintViolation;
  57. import javax.validation.Validator;
  58. import java.io.FileInputStream;
  59. import java.io.IOException;
  60. import java.io.OutputStream;
  61. import java.math.BigDecimal;
  62. import java.net.URI;
  63. import java.util.*;
  64. import java.util.regex.Matcher;
  65. import java.util.regex.Pattern;
  66. /**
  67. * @Title: Controller
  68. * @Description: 预算样表
  69. * @author onlineGenerator
  70. * @date 2020-07-04 13:51:06
  71. * @version V1.0
  72. *
  73. */
  74. @Controller
  75. @RequestMapping("/budgetTempSheetController")
  76. public class BudgetTempSheetController extends BaseController {
  77. /**
  78. * Logger for this class
  79. */
  80. private static final Logger logger = Logger.getLogger(BudgetTempSheetController.class);
  81. @Autowired
  82. private BudgetTempSheetServiceI budgetTempSheetService;
  83. @Autowired
  84. private BudgetTempSheetMeasureServiceI budgetTempSheetMeasureService;
  85. @Autowired
  86. private SystemService systemService;
  87. @Autowired
  88. private Validator validator;
  89. @Autowired
  90. private BudgetDataService budgetDataService;
  91. @Autowired
  92. private BudgetPeriodDetailServiceI budgetPeriodDetailService;
  93. /**
  94. * 预算样表列表 页面跳转
  95. *
  96. * @return
  97. */
  98. @RequestMapping(params = "list")
  99. public ModelAndView list(HttpServletRequest request) {
  100. return new ModelAndView("cn/com/lzt/budget/tempsheet/budgetTempSheetList");
  101. }
  102. @RequestMapping(params = "goMeasure")
  103. public ModelAndView goMeasure(HttpServletRequest request) {
  104. String id = request.getParameter("id");
  105. BudgetTempSheetEntity tempSheet = systemService.getEntity(BudgetTempSheetEntity.class, id);
  106. request.setAttribute("tempSheet", tempSheet);
  107. setGoMeasure(tempSheet,request);
  108. String type = tempSheet.getType();
  109. String periodYear = tempSheet.getPeriodYear();
  110. Integer actual = tempSheet.getActual();
  111. if(actual==1){
  112. /*List<BudgetControlEntity> controlEntityList =systemService.getList(BudgetControlEntity.class);
  113. request.setAttribute("controlEntityList", controlEntityList);*/
  114. }
  115. if(StringUtils.startsWith(type, BudgetConstant.TEMPSHEET_TYPE_NORMAL)){
  116. return new ModelAndView("cn/com/lzt/budget/tempsheet/budgetTempSheet-measure");
  117. }else if(StringUtils.equals(type, BudgetConstant.TEMPSHEET_TYPE_ORDERS)){
  118. String orderKind = tempSheet.getOrdersKind();
  119. String ordersKindTxt = DictUtil.formatToTypeName(orderKind,"budget_orders_kind");
  120. request.setAttribute("ordersKindTxt", ordersKindTxt);
  121. BudgetConstant.OrdersKindType ordersKindType=tempSheet.getOrdersKindType();
  122. if(ordersKindType==BudgetConstant.OrdersKindType.dizhi){
  123. List<TBGoodsCategoryEntity> subCategoryList =budgetDataService.getGoodsCategoryByParent(tempSheet.getOrdersCategoryCode());
  124. request.setAttribute("subCategoryList", subCategoryList);
  125. }
  126. BudgetPeriodDetailEntity defaultPeriodDetailEntity = budgetPeriodDetailService.findMaxDataDetail(periodYear);
  127. request.setAttribute("defaultPeriodDetailEntity", defaultPeriodDetailEntity);
  128. return new ModelAndView("cn/com/lzt/budget/tempsheet/budgetTempSheet-measure4Orders");
  129. }else if(StringUtils.equals(type, BudgetConstant.TEMPSHEET_TYPE_HR)){
  130. BudgetPeriodDetailEntity defaultPeriodDetailEntity = budgetPeriodDetailService.findMaxDataDetail(periodYear);
  131. request.setAttribute("defaultPeriodDetailEntity", defaultPeriodDetailEntity);
  132. List<TSType> postTypeList =systemService.getTypesByGroupcodeFromCache("budget_cost_post_type");
  133. request.setAttribute("postTypeList",postTypeList );
  134. return new ModelAndView("cn/com/lzt/budget/tempsheet/budgetTempSheet-measure4Hr");
  135. }else if(StringUtils.equals(type, BudgetConstant.TEMPSHEET_TYPE_ACTUAL_STA)){//执行统计表
  136. if(StringUtils.isBlank(tempSheet.getJson())){
  137. List<BudgetEntity> entityList = systemService.findHql("from BudgetEntity where id<>? order by entityName",BudgetConstant.COMPANY_ENTITY_ID);
  138. request.setAttribute("entityList", entityList);
  139. }
  140. if(StringUtils.equals(tempSheet.getActualStaKind(),BudgetConstant.TEMPSHEET_TYPE_LEADER)){
  141. return new ModelAndView("cn/com/lzt/budget/tempsheet/budgetTempSheet-leader");
  142. }
  143. return new ModelAndView("cn/com/lzt/budget/tempsheet/budgetTempSheet-measure4ActualSta");
  144. }
  145. return null;
  146. }
  147. @RequestMapping(params = "doAddMeasure")
  148. @ResponseBody
  149. public AjaxJson doAddMeasure(BudgetTempSheetEntity tempSheet, BudgetTempSheetEntityMap tempSheetDept, HttpServletRequest request) {
  150. AjaxJson j = new AjaxJson();
  151. String message = "保存成功";
  152. try {
  153. String tempSheetId = tempSheet.getId();
  154. // Integer headRowCount = tempSheet.getHeadRowCount();
  155. // Integer goalsColIndex =tempSheet.getGoalsColIndex();
  156. String json = request.getParameter("json");
  157. BudgetTempSheetEntity entity = systemService.getEntity(BudgetTempSheetEntity.class, tempSheetId);
  158. BudgetPeriodEntity periodEntity = systemService.findUniqueByProperty(BudgetPeriodEntity.class, "periodYear", entity.getPeriodYear());
  159. String periodId = periodEntity.getId();
  160. //预算指标
  161. String deltempSheetMeasureSql = " truncate "+entity.getTempsheetMeasureTable();
  162. systemService.executeSql(deltempSheetMeasureSql);
  163. SpreadJson spreadJson = new SpreadJson(json);
  164. Map<String, Object> dataMap = spreadJson.data(1);
  165. Map<String, Object> dataTable = (Map<String, Object>) dataMap.get("dataTable");
  166. List<BudgetTempSheetMeasureEntity> tempSheetMeasureEntityList = new ArrayList<>();
  167. List<String> usedMeasureIdList = new ArrayList<>();
  168. for(Map.Entry<String,Object> entry :dataTable.entrySet()){
  169. String row = entry.getKey();
  170. Map<String, Object> rowCols = (Map<String, Object>) entry.getValue();
  171. for(Map.Entry<String,Object> rowColsEntry :rowCols.entrySet()) {
  172. String col = rowColsEntry.getKey();
  173. Map<String, Object> cellMap = (Map<String, Object>) rowColsEntry.getValue();
  174. Map<String, Object> cellTag = (Map<String, Object>) cellMap.get("tag");
  175. if(cellTag==null) continue;
  176. Boolean dataCell = (Boolean) cellTag.get("dataCell");
  177. if(dataCell== null ||!dataCell) continue;
  178. Object value = cellMap.get("value");
  179. Object formula = cellMap.get("formula");
  180. String measureDataType = oConvertUtils.getString(cellTag.get("datatype"));
  181. boolean isFormula = formula!=null;
  182. BudgetTempSheetMeasureEntity tempSheetMeasureEntity = new BudgetTempSheetMeasureEntity();
  183. String entityId = oConvertUtils.getString(cellTag.get("entityId"));
  184. tempSheetMeasureEntity.setEntityId(entityId);
  185. String effectCell = oConvertUtils.getString(cellTag.get("effectCell"));
  186. tempSheetMeasureEntity.setEffectCell(effectCell);
  187. String showType = oConvertUtils.getString(cellTag.get("showType"));
  188. tempSheetMeasureEntity.setShowType(showType);
  189. tempSheetMeasureEntity.setTempSheetId(tempSheetId);
  190. if(StringUtils.equals(measureDataType,"txt")){//文本说明保存到txt字段
  191. tempSheetMeasureEntity.setDataTxt(value==null?null:value.toString());
  192. }else {
  193. if(value!=null){
  194. if(NumberUtils.isNumber(value.toString())){
  195. tempSheetMeasureEntity.setDataValue(BudgetUtils.measureShowValueToDb(new BigDecimal(value.toString()),measureDataType,showType));
  196. }else {//非空非数字
  197. cellMap.put("value", null);
  198. }
  199. }
  200. }
  201. tempSheetMeasureEntity.setDataFormula(formula==null?null:formula.toString());
  202. String measureId = oConvertUtils.getString(cellTag.get("measureId"));
  203. String measureName = oConvertUtils.getString(cellTag.get("measureName"));
  204. if(StringUtils.isNotBlank(measureId)) {
  205. tempSheetMeasureEntity.setMeasureId(measureId);
  206. tempSheetMeasureEntity.setMeasureName(measureName);
  207. usedMeasureIdList.add(measureId);
  208. }
  209. tempSheetMeasureEntity.setPeriodId(periodEntity.getId());
  210. tempSheetMeasureEntity.setPeriodType(periodEntity.getPeriodType());
  211. tempSheetMeasureEntity.setPeriodYear(periodEntity.getPeriodYear());
  212. String periodDetailId = oConvertUtils.getString(cellTag.get("periodDetailId"));
  213. if(StringUtils.isNotBlank(periodDetailId)) {
  214. BudgetPeriodDetailEntity periodDetail = systemService.getEntity(BudgetPeriodDetailEntity.class, periodDetailId);
  215. tempSheetMeasureEntity.setPeriodDetailId(periodDetail.getId());
  216. tempSheetMeasureEntity.setPeriodDetailStart(periodDetail.getStart());
  217. tempSheetMeasureEntity.setPeriodDetailEnd(periodDetail.getEnd());
  218. }
  219. String periodDetailName = oConvertUtils.getString(cellTag.get("periodDetailName"));
  220. tempSheetMeasureEntity.setPeriodDetailName(periodDetailName);
  221. String includeTax = oConvertUtils.getString(cellTag.get("includeTax"),Globals.YES);
  222. if(StringUtils.isNotBlank(includeTax)) tempSheetMeasureEntity.setIncludeTax(Integer.valueOf(includeTax));
  223. Integer expect = oConvertUtils.getInt(cellTag.get("expect"), BudgetConstant.EXPECT_YES);
  224. cellTag.put("expect", expect);
  225. tempSheetMeasureEntity.setExpect(expect);
  226. /*if(isFormula) {
  227. measureDataType="formula";
  228. cellTag.put("datatype", measureDataType);
  229. }*/
  230. String functionId = oConvertUtils.getString(cellTag.get("functionId"));
  231. if(StringUtils.isNotBlank(functionId)) {
  232. tempSheetMeasureEntity.setFunctionId(functionId);
  233. isFormula=true;//维度公式必须是公式
  234. BudgetDimFunctionEntity dimFunctionEntity = systemService.getEntity(BudgetDimFunctionEntity.class, functionId);
  235. dimFunctionEntity.setFunctionFrom("tempsheet");
  236. dimFunctionEntity.setCurrentMeasureId(measureId);
  237. dimFunctionEntity.setCurrentPeriodDetailId(periodDetailId);
  238. dimFunctionEntity.setTempsheetId(tempSheetId);
  239. dimFunctionEntity.setRow(Integer.valueOf(row));
  240. dimFunctionEntity.setCol(Integer.valueOf(col));
  241. systemService.updateEntitie(dimFunctionEntity);
  242. }
  243. if(StringUtils.isNotBlank(measureDataType)) tempSheetMeasureEntity.setDataType(measureDataType);
  244. //goalsDataTypeName
  245. String controlFlag = oConvertUtils.getString(cellTag.get("controlFlag"));
  246. if(StringUtils.isNotBlank(controlFlag)) tempSheetMeasureEntity.setControlFlag(controlFlag);
  247. String readOnly = oConvertUtils.getString(cellTag.get("readOnly"));
  248. if(isFormula){
  249. readOnly = "1";
  250. cellTag.put("readOnly",1);
  251. }
  252. Map<String,Object> dataRules = new HashMap<>();
  253. if(StringUtils.isNotBlank(readOnly)) dataRules.put("readOnly", readOnly);
  254. ObjectMapper mapper = new ObjectMapper();
  255. tempSheetMeasureEntity.setDataRules(mapper.writeValueAsString(dataRules));
  256. tempSheetMeasureEntity.setRow(Integer.valueOf(row));
  257. tempSheetMeasureEntity.setCol(Integer.valueOf(col));
  258. tempSheetMeasureEntity.setSheetIndex(1);
  259. String findKey = oConvertUtils.getString(cellTag.get("findKey"));
  260. tempSheetMeasureEntity.setFindKey(findKey);
  261. tempSheetMeasureEntityList.add(tempSheetMeasureEntity);
  262. }
  263. }
  264. budgetTempSheetMeasureService.batchSave(tempSheetMeasureEntityList);
  265. ObjectMapper mapper = new ObjectMapper();
  266. String alterJson=mapper.writeValueAsString(spreadJson.getJsonMap());
  267. entity.setJson(alterJson);
  268. systemService.saveOrUpdate(entity);
  269. //标志指标已被使用
  270. for(String measureId:usedMeasureIdList){
  271. BudgetMeasureEntity measureEntity = systemService.getEntity(BudgetMeasureEntity.class,measureId);
  272. systemService.saveOrUpdate(measureEntity);
  273. }
  274. }catch (Exception e){
  275. e.printStackTrace();
  276. j.setSuccess(false);
  277. message="保存失败";
  278. }
  279. j.setMsg(message);
  280. return j;
  281. }
  282. /**
  283. * easyui AJAX请求数据
  284. *
  285. * @param request
  286. * @param response
  287. * @param dataGrid
  288. */
  289. @RequestMapping(params = "datagrid")
  290. public void datagrid(BudgetTempSheetEntity tempSheet, HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
  291. CriteriaQuery cq = new CriteriaQuery(BudgetTempSheetEntity.class, dataGrid);
  292. //查询条件组装器
  293. if(StringUtils.isNotBlank(tempSheet.getName())){
  294. cq.like("name","%"+tempSheet.getName()+"%");
  295. tempSheet.setName(null);
  296. }
  297. if(StringUtils.isNotBlank(tempSheet.getOfficeName())){
  298. cq.like("officeName","%"+tempSheet.getOfficeName()+"%");
  299. tempSheet.setOfficeName(null);
  300. }
  301. org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, tempSheet, request.getParameterMap());
  302. try{
  303. //自定义追加查询条件
  304. }catch (Exception e) {
  305. throw new BusinessException(e.getMessage());
  306. }
  307. cq.add();
  308. this.budgetTempSheetService.getDataGridReturn(cq, true);
  309. TagUtil.datagrid(response, dataGrid);
  310. }
  311. /**
  312. * 删除预算样表
  313. *
  314. * @return
  315. */
  316. @RequestMapping(params = "doDel")
  317. @ResponseBody
  318. public AjaxJson doDel(BudgetTempSheetEntity tempSheet, HttpServletRequest request) {
  319. String message = null;
  320. AjaxJson j = new AjaxJson();
  321. tempSheet = systemService.getEntity(BudgetTempSheetEntity.class, tempSheet.getId());
  322. message = "预算样表删除成功";
  323. try{
  324. budgetTempSheetService.delete(tempSheet);
  325. systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
  326. }catch(Exception e){
  327. e.printStackTrace();
  328. message = "预算样表删除失败";
  329. throw new BusinessException(e.getMessage());
  330. }
  331. j.setMsg(message);
  332. return j;
  333. }
  334. /**
  335. * 批量删除预算样表
  336. *
  337. * @return
  338. */
  339. @RequestMapping(params = "doBatchDel")
  340. @ResponseBody
  341. public AjaxJson doBatchDel(String ids,HttpServletRequest request){
  342. String message = null;
  343. AjaxJson j = new AjaxJson();
  344. message = "预算样表删除成功";
  345. try{
  346. for(String id:ids.split(",")){
  347. BudgetTempSheetEntity tempSheet = systemService.getEntity(BudgetTempSheetEntity.class,
  348. id
  349. );
  350. budgetTempSheetService.delete(tempSheet);
  351. systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
  352. }
  353. }catch(Exception e){
  354. e.printStackTrace();
  355. message = "预算样表删除失败";
  356. throw new BusinessException(e.getMessage());
  357. }
  358. j.setMsg(message);
  359. return j;
  360. }
  361. /**
  362. * 添加预算样表
  363. *
  364. * @return
  365. */
  366. @RequestMapping(params = "doAdd")
  367. @ResponseBody
  368. public AjaxJson doAdd(BudgetTempSheetEntity tempSheet, HttpServletRequest request) {
  369. if(StringUtils.isNotBlank(tempSheet.getId())){
  370. return doUpdate(tempSheet,request);
  371. }
  372. String message = null;
  373. AjaxJson j = new AjaxJson();
  374. message = "预算样表添加成功";
  375. try{
  376. Integer actual = tempSheet.getActual();
  377. String type = tempSheet.getType();
  378. String periodYear = tempSheet.getPeriodYear();
  379. if(actual==1 &&StringUtil.equals(type,BudgetConstant.TEMPSHEET_TYPE_ACTUAL_STA)){//执行统计表 判断唯一性
  380. String sql = "select 1 from t_b_budget_tempsheet where period_year=? and actual=? and actual_sta_kind=? ";
  381. List<?> list = systemService.findForJdbc(sql,periodYear,1,tempSheet.getActualStaKind());
  382. if(!list.isEmpty()){
  383. j.setSuccess(false);
  384. j.setMsg("已存在同类型统计表");
  385. return j;
  386. }
  387. }
  388. tempSheet.setStatus(BudgetTempSheetEntity.STATUS_UNPUBLISH);
  389. BudgetPeriodEntity periodEntity = systemService.findUniqueByProperty(BudgetPeriodEntity.class, "periodYear",periodYear);
  390. if(periodEntity==null){
  391. j.setMsg(periodYear+"年无预算周期设置");
  392. j.setSuccess(false);
  393. return j;
  394. }
  395. tempSheet.setPeriodId(periodEntity.getId());
  396. budgetTempSheetService.save(tempSheet);
  397. systemService.addLog(message, Globals.Log_Type_INSERT, Globals.Log_Leavel_INFO);
  398. }catch(Exception e){
  399. e.printStackTrace();
  400. message = "预算样表添加失败";
  401. throw new BusinessException(e.getMessage());
  402. }
  403. j.setMsg(message);
  404. return j;
  405. }
  406. /**
  407. * 更新预算样表
  408. *
  409. * @return
  410. */
  411. @RequestMapping(params = "doUpdate")
  412. @ResponseBody
  413. public AjaxJson doUpdate(BudgetTempSheetEntity tempSheet, HttpServletRequest request) {
  414. String message = null;
  415. AjaxJson j = new AjaxJson();
  416. message = "预算样表更新成功";
  417. Integer oldWriteback = tempSheet.getWriteBack();
  418. BudgetTempSheetEntity t = budgetTempSheetService.get(BudgetTempSheetEntity.class, tempSheet.getId());
  419. Integer newWriteback = t.getWriteBack();
  420. try {
  421. MyBeanUtils.copyBeanNotNull2Bean(tempSheet, t);
  422. budgetTempSheetService.saveOrUpdate(t);
  423. if(newWriteback!=null&&(oldWriteback==null || !newWriteback.equals(oldWriteback))){
  424. budgetTempSheetService.changeMeasureInstMap(t);
  425. }
  426. systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
  427. } catch (Exception e) {
  428. e.printStackTrace();
  429. message = "预算样表更新失败";
  430. throw new BusinessException(e.getMessage());
  431. }
  432. j.setMsg(message);
  433. return j;
  434. }
  435. /**
  436. * 预算样表新增页面跳转
  437. *
  438. * @return
  439. */
  440. @RequestMapping(params = "goAdd")
  441. public ModelAndView goAdd(BudgetTempSheetEntity tempSheet, HttpServletRequest req) {
  442. if (StringUtil.isNotEmpty(tempSheet.getId())) {
  443. tempSheet = budgetTempSheetService.getEntity(BudgetTempSheetEntity.class, tempSheet.getId());
  444. req.setAttribute("tempSheetPage", tempSheet);
  445. }
  446. return new ModelAndView("cn/com/lzt/budget/tempsheet/budgetTempSheet-add");
  447. }
  448. /**
  449. * 预算样表编辑页面跳转
  450. *
  451. * @return
  452. */
  453. @RequestMapping(params = "goUpdate")
  454. public ModelAndView goUpdate(BudgetTempSheetEntity tempSheet, HttpServletRequest req) {
  455. if (StringUtil.isNotEmpty(tempSheet.getId())) {
  456. tempSheet = budgetTempSheetService.getEntity(BudgetTempSheetEntity.class, tempSheet.getId());
  457. req.setAttribute("entity", tempSheet);
  458. }
  459. return new ModelAndView("cn/com/lzt/budget/tempsheet/budgetTempSheet-add");
  460. }
  461. /**
  462. * 导入功能跳转
  463. *
  464. * @return
  465. */
  466. @RequestMapping(params = "upload")
  467. public ModelAndView upload(HttpServletRequest req) {
  468. req.setAttribute("controller_name","budgetTempSheetController");
  469. return new ModelAndView("common/upload/pub_excel_upload");
  470. }
  471. /**
  472. * 导出excel
  473. *
  474. * @param request
  475. * @param response
  476. */
  477. @RequestMapping(params = "exportXls")
  478. public String exportXls(BudgetTempSheetEntity tempSheet, HttpServletRequest request, HttpServletResponse response
  479. , DataGrid dataGrid, ModelMap modelMap) {
  480. CriteriaQuery cq = new CriteriaQuery(BudgetTempSheetEntity.class, dataGrid);
  481. org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, tempSheet, request.getParameterMap());
  482. List<BudgetTempSheetEntity> tempSheets = this.budgetTempSheetService.getListByCriteriaQuery(cq,false);
  483. modelMap.put(NormalExcelConstants.FILE_NAME,"预算样表");
  484. modelMap.put(NormalExcelConstants.CLASS, BudgetTempSheetEntity.class);
  485. modelMap.put(NormalExcelConstants.PARAMS,new ExportParams("预算样表列表", "导出人:"+ResourceUtil.getSessionUser().getRealName(),
  486. "导出信息"));
  487. modelMap.put(NormalExcelConstants.DATA_LIST,tempSheets);
  488. return NormalExcelConstants.JEECG_EXCEL_VIEW;
  489. }
  490. /**
  491. * 导出excel 使模板
  492. *
  493. * @param request
  494. * @param response
  495. */
  496. @RequestMapping(params = "exportXlsByT")
  497. public String exportXlsByT(BudgetTempSheetEntity tempSheet, HttpServletRequest request, HttpServletResponse response
  498. , DataGrid dataGrid, ModelMap modelMap) {
  499. modelMap.put(NormalExcelConstants.FILE_NAME,"预算样表");
  500. modelMap.put(NormalExcelConstants.CLASS, BudgetTempSheetEntity.class);
  501. modelMap.put(NormalExcelConstants.PARAMS,new ExportParams("预算样表列表", "导出人:"+ResourceUtil.getSessionUser().getRealName(),
  502. "导出信息"));
  503. modelMap.put(NormalExcelConstants.DATA_LIST,new ArrayList());
  504. return NormalExcelConstants.JEECG_EXCEL_VIEW;
  505. }
  506. @SuppressWarnings("unchecked")
  507. @RequestMapping(params = "importExcel", method = RequestMethod.POST)
  508. @ResponseBody
  509. public AjaxJson importExcel(HttpServletRequest request, HttpServletResponse response) {
  510. AjaxJson j = new AjaxJson();
  511. MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
  512. Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
  513. for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
  514. MultipartFile file = entity.getValue();// 获取上传文件对象
  515. ImportParams params = new ImportParams();
  516. params.setTitleRows(2);
  517. params.setHeadRows(1);
  518. params.setNeedSave(true);
  519. try {
  520. List<BudgetTempSheetEntity> listBudgetTempSheetEntities = ExcelImportUtil.importExcel(file.getInputStream(), BudgetTempSheetEntity.class,params);
  521. for (BudgetTempSheetEntity tempSheet : listBudgetTempSheetEntities) {
  522. budgetTempSheetService.save(tempSheet);
  523. }
  524. j.setMsg("文件导入成功!");
  525. } catch (Exception e) {
  526. j.setMsg("文件导入失败!");
  527. logger.error(ExceptionUtil.getExceptionMessage(e));
  528. }finally{
  529. try {
  530. file.getInputStream().close();
  531. } catch (IOException e) {
  532. e.printStackTrace();
  533. }
  534. }
  535. }
  536. return j;
  537. }
  538. @RequestMapping(method = RequestMethod.GET)
  539. @ResponseBody
  540. public List<BudgetTempSheetEntity> list() {
  541. List<BudgetTempSheetEntity> listtempSheets=budgetTempSheetService.getList(BudgetTempSheetEntity.class);
  542. return listtempSheets;
  543. }
  544. @RequestMapping(value = "/{id}", method = RequestMethod.GET)
  545. @ResponseBody
  546. public ResponseEntity<?> get(@PathVariable("id") String id) {
  547. BudgetTempSheetEntity task = budgetTempSheetService.get(BudgetTempSheetEntity.class, id);
  548. if (task == null) {
  549. return new ResponseEntity(HttpStatus.NOT_FOUND);
  550. }
  551. return new ResponseEntity(task, HttpStatus.OK);
  552. }
  553. @RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
  554. @ResponseBody
  555. public ResponseEntity<?> create(@RequestBody BudgetTempSheetEntity tempSheet, UriComponentsBuilder uriBuilder) {
  556. //调用JSR303 Bean Validator进行校验,如果出错返回含400错误码及json格式的错误信息.
  557. Set<ConstraintViolation<BudgetTempSheetEntity>> failures = validator.validate(tempSheet);
  558. if (!failures.isEmpty()) {
  559. return new ResponseEntity(BeanValidators.extractPropertyAndMessage(failures), HttpStatus.BAD_REQUEST);
  560. }
  561. //保存
  562. try{
  563. budgetTempSheetService.save(tempSheet);
  564. } catch (Exception e) {
  565. e.printStackTrace();
  566. return new ResponseEntity(HttpStatus.NO_CONTENT);
  567. }
  568. //按照Restful风格约定,创建指向新任务的url, 也可以直接返回id或对象.
  569. String id = tempSheet.getId();
  570. URI uri = uriBuilder.path("/rest/budgetTempSheetController/" + id).build().toUri();
  571. HttpHeaders headers = new HttpHeaders();
  572. headers.setLocation(uri);
  573. return new ResponseEntity(headers, HttpStatus.CREATED);
  574. }
  575. @RequestMapping(value = "/{id}", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE)
  576. public ResponseEntity<?> update(@RequestBody BudgetTempSheetEntity tempSheet) {
  577. //调用JSR303 Bean Validator进行校验,如果出错返回含400错误码及json格式的错误信息.
  578. Set<ConstraintViolation<BudgetTempSheetEntity>> failures = validator.validate(tempSheet);
  579. if (!failures.isEmpty()) {
  580. return new ResponseEntity(BeanValidators.extractPropertyAndMessage(failures), HttpStatus.BAD_REQUEST);
  581. }
  582. //保存
  583. try{
  584. budgetTempSheetService.saveOrUpdate(tempSheet);
  585. } catch (Exception e) {
  586. e.printStackTrace();
  587. return new ResponseEntity(HttpStatus.NO_CONTENT);
  588. }
  589. //按Restful约定,返回204状态码, 无内容. 也可以返回200状态码.
  590. return new ResponseEntity(HttpStatus.NO_CONTENT);
  591. }
  592. @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
  593. @ResponseStatus(HttpStatus.NO_CONTENT)
  594. public void delete(@PathVariable("id") String id) {
  595. budgetTempSheetService.deleteEntityById(BudgetTempSheetEntity.class, id);
  596. }
  597. @RequestMapping(params = "budgetactualdemo")
  598. public ModelAndView budgetactualdemo(HttpServletRequest request) {
  599. return new ModelAndView("cn/com/lzt/budget/tempsheet/budgetActualspreadjsdemo");
  600. }
  601. @RequestMapping(params = "budgetSubDemo")
  602. public ModelAndView budgetSubDemo(HttpServletRequest request) {
  603. return new ModelAndView("cn/com/lzt/budget/tempsheet/budgetSubspreadjsdemo");
  604. }
  605. @RequestMapping(params = "budgetActualSubspreadjsdemo")
  606. public ModelAndView budgetActualSubspreadjsdemo(HttpServletRequest request) {
  607. return new ModelAndView("cn/com/lzt/budget/tempsheet/budgetActualSubspreadjsdemo");
  608. }
  609. @RequestMapping(params = "budgetToolsDemo")
  610. public ModelAndView budgetToolsDemo(HttpServletRequest request) {
  611. return new ModelAndView("cn/com/lzt/budget/tempsheet/budgetToolsDemo");
  612. }
  613. @RequestMapping(params = "downloadword")
  614. public void downloadword(HttpServletRequest request,HttpServletResponse response)throws Exception {
  615. String fileName ="项目管理方案.docx";
  616. fileName = new String(fileName.getBytes("UTF-8"), "iso-8859-1");
  617. response.setHeader("Content-disposition", "attachment; filename="+fileName);
  618. response.setContentType("application/msword");
  619. String path = request.getSession().getServletContext().getRealPath("/userfiles/files/demoTemplate.docx");//获取模板路径
  620. String tempPath = request.getSession().getServletContext().getRealPath("/userfiles/files/demoTemplate1.docx");//获取模板路径
  621. OutputStream outputStream = null;
  622. try {
  623. FileInputStream is = new FileInputStream(path);//读入流中
  624. FileInputStream tempIs = new FileInputStream(tempPath);
  625. XWPFDocument xdf;
  626. xdf = new XWPFDocument(is);
  627. //新建一个word文档
  628. XWPFDocument tempXdf = new XWPFDocument(tempIs);
  629. //变量
  630. Map<String, Object> params = new HashMap<String, Object>();
  631. params.put("year", request.getParameter("year"));
  632. params.put("manangercount", request.getParameter("manangercount"));
  633. params.put("totalcount", request.getParameter("totalcount"));
  634. params.put("sucuritycount", request.getParameter("sucuritycount"));
  635. params.put("cleanercount", request.getParameter("cleanercount"));
  636. params.put("maintenancecount", request.getParameter("maintenancecount"));
  637. params.put("meetingcount", request.getParameter("meetingcount"));
  638. params.put("totalwageclass", request.getParameter("totalwageclass"));
  639. params.put("wageclassmemo", request.getParameter("wageclassmemo"));
  640. params.put("apartcount", request.getParameter("apartcount"));
  641. params.put("totalwage", request.getParameter("totalwage"));
  642. params.put("sucuritywage", request.getParameter("sucuritywage"));
  643. params.put("cleanerwage", request.getParameter("cleanerwage"));
  644. params.put("maintenancewage", request.getParameter("maintenancewage"));
  645. params.put("meetingwage", request.getParameter("meetingwage"));
  646. params.put("apartwage", request.getParameter("apartwage"));
  647. params.put("managerwage", request.getParameter("managerwage"));
  648. this.replaceText(xdf,params);//替换word中的变量。
  649. this.setStyle(tempXdf,xdf);//tempXdf-模板的格式,xdf-模板
  650. outputStream = response.getOutputStream();
  651. xdf.write(outputStream);
  652. response.flushBuffer();
  653. } catch (Exception e) {
  654. logger.info("--通过流的方式获取文件异常--" + e.getMessage());
  655. }finally{
  656. if(outputStream != null){
  657. outputStream.close();
  658. }
  659. }
  660. }
  661. /**
  662. * 替换文档中的参数(word)
  663. * @param doc
  664. * @param params
  665. */
  666. private void replaceText(XWPFDocument doc, Map<String, Object> params) {
  667. Iterator<XWPFParagraph> iterator = doc.getParagraphsIterator();
  668. XWPFParagraph para;
  669. while (iterator.hasNext()) {
  670. para = iterator.next();
  671. this.replaceInPara(para, params);
  672. }
  673. //处理table
  674. List<XWPFTable> tableList = doc.getTables();
  675. if(tableList.size()>0) {
  676. for (XWPFTable xwpfTable : tableList) {
  677. for (XWPFTableRow row : xwpfTable.getRows()) {
  678. for(XWPFTableCell cell : row.getTableCells() ) {
  679. for(XWPFParagraph cellPara : cell.getParagraphs()) {
  680. this.replaceInPara(cellPara, params);
  681. }
  682. }
  683. }
  684. }
  685. }
  686. }
  687. /**
  688. * 替换段落中的参数(word)
  689. * @param para
  690. * @param params
  691. */
  692. private void replaceInPara(XWPFParagraph para, Map<String, Object> params) {
  693. List<XWPFRun> runs;
  694. Matcher matcher;
  695. if (matcher(para.getParagraphText()).find()) {
  696. runs = para.getRuns();
  697. for (int i=0; i<runs.size(); i++) {
  698. XWPFRun run = runs.get(i);
  699. String runText = run.toString();
  700. matcher = matcher(runText);
  701. if (matcher.find()) {
  702. while ((matcher = matcher(runText)).find()) {
  703. runText = matcher.replaceFirst(String.valueOf(params.get(matcher.group(1))));
  704. }
  705. para.removeRun(i);
  706. //重新插入run里内容格式可能与原来模板的格式不一致
  707. para.insertNewRun(i).setText(runText);
  708. }
  709. }
  710. }
  711. }
  712. /**
  713. * 处理文档替换内容的格式问题(word)
  714. * @param tempDoc
  715. * @param doc
  716. */
  717. private void setStyle(XWPFDocument tempDoc, XWPFDocument doc) {
  718. Iterator<XWPFParagraph> iterator = tempDoc.getParagraphsIterator();
  719. Iterator<XWPFParagraph> iterator2 = doc.getParagraphsIterator();
  720. XWPFParagraph para ;
  721. XWPFParagraph para2;
  722. while (iterator.hasNext()&&iterator2.hasNext()) {
  723. para = iterator.next();
  724. para2 = iterator2.next();
  725. this.setStyleInPara(para,para2);
  726. }
  727. }
  728. /**
  729. * 处理段落替换内容的格式问题(word)
  730. * @param para
  731. * @param para2
  732. */
  733. private void setStyleInPara(XWPFParagraph para, XWPFParagraph para2) {
  734. List<XWPFRun> runs;
  735. List<XWPFRun> runs2;
  736. Matcher matcher;
  737. if (matcher(para.getParagraphText()).find()) {
  738. runs = para.getRuns();
  739. runs2 = para2.getRuns();
  740. for (int i=0; i<runs.size(); i++) {
  741. XWPFRun run = runs.get(i);
  742. XWPFRun run2 = runs2.get(i);
  743. String runText = run.toString();
  744. matcher = matcher(runText);
  745. if (matcher.find()) {
  746. //按模板文件格式设置格式
  747. run2.getCTR().setRPr(run.getCTR().getRPr());
  748. }
  749. }
  750. }
  751. }
  752. private Matcher matcher(String str) {
  753. Pattern pattern = Pattern.compile("\\$\\{(.+?)\\}", Pattern.CASE_INSENSITIVE);
  754. Matcher matcher = pattern.matcher(str);
  755. return matcher;
  756. }
  757. @RequestMapping(params = "publish")
  758. @ResponseBody
  759. public AjaxJson publish(BudgetTempSheetEntity tempSheet, HttpServletRequest request) {
  760. String message = null;
  761. AjaxJson j = new AjaxJson();
  762. message = "下发成功";
  763. try{
  764. tempSheet = systemService.getEntity(BudgetTempSheetEntity.class, tempSheet.getId());
  765. if(StringUtils.isBlank(tempSheet.getJson())){
  766. j.setSuccess(false);
  767. j.setMsg("样表没填写,下发失败.");
  768. return j;
  769. }
  770. String entityIds=request.getParameter("entityIds");
  771. String[] entityIdArray = StringUtil.splitString(entityIds, ",");
  772. List<BudgetEntity> entityList = new ArrayList<>(entityIdArray.length);
  773. for(String entityId:entityIdArray){
  774. BudgetEntity budgetEntity = systemService.getEntity(BudgetEntity.class,entityId);
  775. if(StringUtil.equals(tempSheet.getDirection(),"up" )){
  776. if(budgetEntity.getEntityUserId()==null) {
  777. j.setSuccess(false);
  778. j.setMsg("请填写预算实体:"+budgetEntity.getEntityName()+"的负责人。");
  779. return j;
  780. }
  781. }
  782. entityList.add(budgetEntity);
  783. }
  784. budgetTempSheetService.publish(tempSheet,entityList);
  785. }catch(Exception e){
  786. e.printStackTrace();
  787. message = "下发失败";
  788. j.setSuccess(false);
  789. // throw new BusinessException(e.getMessage());
  790. }
  791. j.setMsg(message);
  792. return j;
  793. }
  794. @RequestMapping(params = "cancel")
  795. @ResponseBody
  796. public AjaxJson cancel(BudgetTempSheetEntity tempSheet, HttpServletRequest request) {
  797. String message = null;
  798. AjaxJson j = new AjaxJson();
  799. message = "撤销成功";
  800. try{
  801. String entityIds=request.getParameter("entityIds");
  802. budgetTempSheetService.cancel(tempSheet.getId(),entityIds);
  803. }catch(Exception e){
  804. e.printStackTrace();
  805. message = "撤销失败";
  806. throw new BusinessException(e.getMessage());
  807. }
  808. j.setMsg(message);
  809. return j;
  810. }
  811. private void setGoMeasure(BudgetTempSheetEntity tempSheet, HttpServletRequest request){
  812. String tempSheetId = tempSheet.getId();
  813. String periodYear = tempSheet.getPeriodYear();
  814. BudgetPeriodEntity periodEntity = systemService.findUniqueByProperty(BudgetPeriodEntity.class, "periodYear", periodYear);
  815. request.setAttribute("periodEntity", periodEntity);
  816. List<BudgetPeriodDetailEntity> periodDetailList = systemService.findByProperty(BudgetPeriodDetailEntity.class, "periodId", periodEntity.getId());
  817. request.setAttribute("periodDetailList", periodDetailList);
  818. String tempSheetDepSql = "select group_concat(dept.entity_name) as entityNames,group_concat(dept.id) as entityIds from t_b_budget_tempsheet_entity dd ,t_b_budget_entity dept " +
  819. " where dd.entity_id=dept.id and dd.tempsheet_id=? ";
  820. Map tempSheetEntity =systemService.findOneForJdbc(tempSheetDepSql,tempSheetId);
  821. request.setAttribute("tempSheetEntity",tempSheetEntity );
  822. List<TSType> measureDataTypeList =systemService.getTypesByGroupcodeFromCache("budget_measure_datatype");
  823. request.setAttribute("measureDataTypeList",measureDataTypeList );
  824. }
  825. @RequestMapping(params = "select")
  826. public ModelAndView select(HttpServletRequest request) {
  827. return new ModelAndView("cn/com/lzt/budget/tempsheet/budgetTempSheet-select");
  828. }
  829. @RequestMapping(params = "selectDatagrid")
  830. public void selectDatagrid(BudgetTempSheetEntity tempSheet, HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
  831. CriteriaQuery cq = new CriteriaQuery(BudgetTempSheetEntity.class, dataGrid);
  832. //查询条件组装器
  833. org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, tempSheet, request.getParameterMap());
  834. try{
  835. cq.isNotNull("json");
  836. //自定义追加查询条件
  837. }catch (Exception e) {
  838. throw new BusinessException(e.getMessage());
  839. }
  840. cq.add();
  841. this.budgetTempSheetService.getDataGridReturn(cq, true);
  842. TagUtil.datagrid(response, dataGrid);
  843. }
  844. @RequestMapping(params = "goAuth")
  845. public ModelAndView goAuth(BudgetTempSheetEntity tempSheet, HttpServletRequest req) {
  846. if (StringUtil.isNotEmpty(tempSheet.getId())) {
  847. tempSheet = budgetTempSheetService.getEntity(BudgetTempSheetEntity.class, tempSheet.getId());
  848. req.setAttribute("entity", tempSheet);
  849. }
  850. return new ModelAndView("cn/com/lzt/budget/tempsheet/budgetTempSheet-auth");
  851. }
  852. @RequestMapping(params = "doSaveAuth")
  853. @ResponseBody
  854. public AjaxJson doSaveAuth(BudgetTempSheetEntity tempSheet, HttpServletRequest request) {
  855. String message = null;
  856. AjaxJson j = new AjaxJson();
  857. message = "预算样表权限更新成功";
  858. BudgetTempSheetEntity t = budgetTempSheetService.get(BudgetTempSheetEntity.class, tempSheet.getId());
  859. try {
  860. MyBeanUtils.copyBeanNotNull2Bean(tempSheet, t);
  861. budgetTempSheetService.saveOrUpdate(t);
  862. systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
  863. } catch (Exception e) {
  864. e.printStackTrace();
  865. message = "预算样表权限更新失败";
  866. throw new BusinessException(e.getMessage());
  867. }
  868. j.setMsg(message);
  869. return j;
  870. }
  871. @RequestMapping(params = "dimMatch")
  872. @ResponseBody
  873. public AjaxJson dimMatch(BudgetDimMatchDto dto, HttpServletRequest request) {
  874. AjaxJson j = new AjaxJson();
  875. try {
  876. budgetDataService.dimMatch(dto);
  877. j.setObj(dto);
  878. }catch (Exception e){
  879. j.setSuccess(false);
  880. e.printStackTrace();
  881. }
  882. return j;
  883. }
  884. }