ProjectController.java 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. package com.jeeplus.modules.sg.project.web;
  2. import com.jeeplus.common.json.AjaxJson;
  3. import com.jeeplus.common.utils.DateUtils;
  4. import com.jeeplus.common.utils.StringUtils;
  5. import com.jeeplus.core.web.BaseController;
  6. import com.jeeplus.modules.sg.overheadline.entity.JkxlDetailFee;
  7. import com.jeeplus.modules.sg.overheadline.service.OverheadLineService;
  8. import com.jeeplus.modules.sg.project.entity.WbsItem;
  9. import com.jeeplus.modules.sg.project.entity.WbsProject;
  10. import com.jeeplus.modules.sg.project.entity.WbsSelection;
  11. import com.jeeplus.modules.sg.project.service.ProjectService;
  12. import com.jeeplus.modules.sg.project.util.ExportUtil;
  13. import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
  14. import org.springframework.beans.factory.annotation.Autowired;
  15. import org.springframework.stereotype.Controller;
  16. import org.springframework.web.bind.annotation.ModelAttribute;
  17. import org.springframework.web.bind.annotation.RequestMapping;
  18. import org.springframework.web.bind.annotation.RequestParam;
  19. import org.springframework.web.bind.annotation.ResponseBody;
  20. import org.springframework.web.multipart.MultipartFile;
  21. import org.springframework.web.servlet.mvc.support.RedirectAttributes;
  22. import org.springframework.ui.Model;
  23. import javax.servlet.http.HttpServletRequest;
  24. import javax.servlet.http.HttpServletResponse;
  25. import java.io.IOException;
  26. import java.text.SimpleDateFormat;
  27. import java.util.ArrayList;
  28. import java.util.Date;
  29. import java.util.List;
  30. import java.util.Random;
  31. @Controller
  32. @RequestMapping("${adminPath}/project")
  33. public class ProjectController extends BaseController {
  34. @Autowired
  35. private HttpServletRequest request;
  36. @Autowired
  37. private ProjectService projectService;
  38. @Autowired
  39. private OverheadLineService overheadLineService;
  40. @ModelAttribute
  41. public WbsProject get(@RequestParam(required=false) String id) {
  42. WbsProject entity = null;
  43. if (StringUtils.isNotBlank(id)){
  44. entity = projectService.get(id);
  45. }
  46. if (entity == null){
  47. entity = new WbsProject();
  48. }
  49. return entity;
  50. }
  51. @RequestMapping(value = {"list", ""})
  52. public String list(WbsSelection wbsSelection, HttpServletRequest request, HttpServletResponse response, Model model) {
  53. List<WbsProject> list = projectService.findList(wbsSelection);
  54. model.addAttribute("list",list);
  55. return "modules/sg/project/management";
  56. }
  57. @RequestMapping("/form")
  58. public String form(WbsProject wbsProject, Model model) {
  59. WbsProject wbsProject1 = projectService.get(wbsProject.getId());
  60. List<WbsProject> list = new ArrayList<>();
  61. list.add(wbsProject1);
  62. model.addAttribute("list", list);
  63. String view = "updateItemForm";
  64. String tabId = request.getParameter("tabId");
  65. if("1".equals(tabId)){
  66. view = "lookItemForm";
  67. }
  68. return "modules/sg/project/"+view;
  69. }
  70. //项目保存
  71. @ResponseBody
  72. @RequestMapping("/save")
  73. public AjaxJson save(WbsProject wbsProject, Model model, RedirectAttributes redirectAttributes){
  74. AjaxJson j = new AjaxJson();
  75. //数据验证
  76. String errMsg = beanValidator(wbsProject);
  77. if (StringUtils.isNotBlank(errMsg)){
  78. j.setSuccess(false);
  79. j.setMsg(errMsg);
  80. return j;
  81. }
  82. //新增或编辑表单保存
  83. projectService.save(wbsProject);//保存
  84. j.setSuccess(true);
  85. addMessage(redirectAttributes, "保存成功");
  86. return j;
  87. }
  88. /**
  89. *数据导入
  90. */
  91. @RequestMapping("/import")
  92. public String importFile(MultipartFile file, RedirectAttributes redirectAttributes, HttpServletRequest request, Model model) {
  93. String projectName = request.getParameter("name"); //项目名称
  94. String projectId = request.getParameter("projectId"); //工程ID
  95. String type = request.getParameter("type"); //工程类型
  96. String id = getId();
  97. try {
  98. //保存数据到数据库
  99. if(type.equals("1")) {
  100. overheadLineService.saveJk(file, projectName, id, projectId, type);
  101. }
  102. if (type.equals("2")) {
  103. overheadLineService.saveDl(file, projectName, id, projectId, type);
  104. }
  105. } catch (RuntimeException e) {
  106. addMessage(redirectAttributes, "导入失败,"+e.getMessage());
  107. return "redirect:/a/project/list";
  108. } catch (Exception e){
  109. addMessage(redirectAttributes, "导入失败");
  110. return "redirect:/a/project/list";
  111. }
  112. addMessage(redirectAttributes, "导入成功");
  113. return "redirect:/a/jkxl/list?id="+id+"&type="+type;
  114. }
  115. //导出
  116. @RequestMapping("/export")
  117. public String toList(HttpServletRequest request,Model model,HttpServletResponse response) throws IOException {
  118. String id = request.getParameter("id");
  119. WbsProject wbsProject = projectService.get(id);
  120. String fileName = wbsProject.getProjectName()+ DateUtils.getDate("yyyyMMddHHmmss")+".xlsx";
  121. List<WbsItem> wbsItems = wbsProject.getWbsItems();
  122. String[] sts = new String[wbsItems.size()+1];
  123. sts[0] = "合计";
  124. for (int i=0;i<wbsItems.size();i++){
  125. sts[i+1] = wbsItems.get(i).getItemName();
  126. }
  127. List<JkxlDetailFee> list = new ArrayList<>();
  128. for(WbsItem wbsItem :wbsItems){
  129. List<JkxlDetailFee> jkxlDetailFees = overheadLineService.findList(wbsItem.getId(),wbsItem.getType());
  130. list.addAll(jkxlDetailFees);
  131. }
  132. List<JkxlDetailFee> list1 = getNewList(list);
  133. ExportUtil exportUtil = new ExportUtil(null, JkxlDetailFee.class,sts);
  134. exportUtil.setDataList(list1,0);
  135. for(int j=0;j<wbsItems.size();j++){
  136. List<JkxlDetailFee> jkxlDetailFees = overheadLineService.findList(wbsItems.get(j).getId(),wbsItems.get(j).getType());
  137. exportUtil.setDataList(jkxlDetailFees,j+1);
  138. }
  139. exportUtil.write(response,fileName).dispose();
  140. return "redirect:/a/project/list";
  141. }
  142. private List<JkxlDetailFee> getNewList(List<JkxlDetailFee> list) {
  143. List<JkxlDetailFee> list1 = new ArrayList<>();
  144. for(JkxlDetailFee jkxlDetailFee: list){
  145. String wbsCode = jkxlDetailFee.getWbsCode();
  146. if(!list1.contains(jkxlDetailFee)){
  147. list1.add(jkxlDetailFee);
  148. }else {
  149. for(JkxlDetailFee jkxlDetailFee1 : list1){
  150. if(jkxlDetailFee1.getWbsCode().equals(wbsCode)){
  151. double azFee = jkxlDetailFee1.getAzFee()!=null? jkxlDetailFee1.getAzFee():0.00;
  152. double azFee1 = jkxlDetailFee.getAzFee()!=null? jkxlDetailFee.getAzFee():0.00;
  153. double qtFee = jkxlDetailFee1.getQtFee()!=null? jkxlDetailFee1.getQtFee():0.00;
  154. double qtFee1 = jkxlDetailFee.getQtFee()!=null? jkxlDetailFee.getQtFee():0.00;
  155. double totalFee = jkxlDetailFee1.getTotalFee()!=null? jkxlDetailFee1.getTotalFee():0.00;
  156. double totalFee1 = jkxlDetailFee.getTotalFee()!=null? jkxlDetailFee.getTotalFee():0.00;
  157. double jsFee = jkxlDetailFee1.getJsFee()!=null? jkxlDetailFee1.getJsFee():0.00;
  158. double jsFee1 = jkxlDetailFee.getJsFee()!=null? jkxlDetailFee.getJsFee():0.00;
  159. double cFee = jkxlDetailFee1.getcFee()!=null? jkxlDetailFee1.getcFee():0.00;
  160. double cFee1 = jkxlDetailFee.getcFee()!=null? jkxlDetailFee.getcFee():0.00;
  161. jkxlDetailFee1.setAzFee(azFee+azFee1);
  162. jkxlDetailFee1.setQtFee(qtFee+qtFee1);
  163. jkxlDetailFee1.setTotalFee(totalFee+totalFee1);
  164. jkxlDetailFee1.setJsFee(jsFee+jsFee1);
  165. jkxlDetailFee1.setcFee(cFee+cFee1);
  166. }
  167. }
  168. }
  169. // for(JkxlDetailFee jkxlDetailFee1 : list1){
  170. // if(wbsCode.equals(jkxlDetailFee1.getWbsCode())){
  171. // jkxlDetailFee1.setAzFee(jkxlDetailFee1.getAzFee()+jkxlDetailFee.getAzFee());
  172. // jkxlDetailFee1.setQtFee(jkxlDetailFee1.getQtFee()+jkxlDetailFee.getQtFee());
  173. // jkxlDetailFee1.setTotalFee(jkxlDetailFee1.getTotalFee()+jkxlDetailFee.getTotalFee());
  174. // jkxlDetailFee1.setJsFee(jkxlDetailFee1.getJsFee()+jkxlDetailFee.getJsFee());
  175. // jkxlDetailFee1.setcFee(jkxlDetailFee1.getcFee()+jkxlDetailFee.getcFee());
  176. // }else {
  177. // list1.add(jkxlDetailFee);
  178. // }
  179. // }
  180. }
  181. return list1;
  182. }
  183. //项目删除
  184. @ResponseBody
  185. @RequestMapping(value = "delete")
  186. public AjaxJson delete(WbsProject wbsProject) {
  187. AjaxJson j = new AjaxJson();
  188. projectService.delete(wbsProject);
  189. j.setMsg("删除项目成功");
  190. return j;
  191. }
  192. @RequestMapping(value = "tolist")
  193. public String toList(){
  194. return "redirect:/a/project/list";
  195. }
  196. /**
  197. * 创建项目ID
  198. */
  199. public String getId() {
  200. SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
  201. String newDate=sdf.format(new Date());
  202. String result="";
  203. Random random=new Random();
  204. for(int i=0;i<3;i++){
  205. result+=random.nextInt(10);
  206. }
  207. return newDate+result;
  208. }
  209. }