f62a4206bb382713d970cfb544f0762bc4d366c4.svn-base 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. package cn.com.goldenwater.dcproj.service.impl.accunt;
  2. import cn.com.goldenwater.dcproj.dao.BisInspAccuntDao;
  3. import cn.com.goldenwater.dcproj.model.BisInspAccunt;
  4. import cn.com.goldenwater.dcproj.param.BisInspAccuntParam;
  5. import cn.com.goldenwater.dcproj.service.BisInspAccuntService;
  6. import cn.com.goldenwater.core.service.AbstractCrudService;
  7. import cn.com.goldenwater.dcproj.utils.BisInspAccuntVerify;
  8. import cn.com.goldenwater.dcproj.utils.ExcelVerifyUtil;
  9. import cn.com.goldenwater.dcproj.utils.ImportUtil;
  10. import cn.com.goldenwater.dcproj.utils.StringUtils;
  11. import org.apache.poi.ss.usermodel.*;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import org.springframework.beans.factory.annotation.Value;
  14. import org.springframework.stereotype.Service;
  15. import org.springframework.transaction.annotation.Transactional;
  16. import cn.com.goldenwater.id.util.UuidUtil;
  17. import org.springframework.web.multipart.MultipartFile;
  18. import javax.servlet.http.HttpServletResponse;
  19. import java.io.*;
  20. import java.net.URLEncoder;
  21. import java.text.ParseException;
  22. import java.text.SimpleDateFormat;
  23. import java.util.List;
  24. import java.util.Date;
  25. /**
  26. * @author lhc
  27. * @date 2022-3-17
  28. */
  29. @Service
  30. @Transactional
  31. public class BisInspAccuntServiceImpl extends AbstractCrudService<BisInspAccunt, BisInspAccuntParam> implements BisInspAccuntService {
  32. @Autowired
  33. private BisInspAccuntDao bisInspAccuntDao;
  34. @Value("${export.templatePath}")
  35. private String templatePath;
  36. @Autowired
  37. BisInspAccuntVerify accuntVerify;
  38. private final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
  39. public BisInspAccuntServiceImpl(BisInspAccuntDao bisInspAccuntDao) {
  40. super(bisInspAccuntDao);
  41. this.bisInspAccuntDao = bisInspAccuntDao;
  42. }
  43. @Override
  44. public int insert(BisInspAccunt bisInspAccunt) {
  45. String uuid = UuidUtil.uuid(); // 生成uuid
  46. bisInspAccunt.setId(uuid);
  47. bisInspAccunt.setIntm(new Date());
  48. bisInspAccunt.setUptm(new Date());
  49. bisInspAccunt.setDataStat("0");
  50. return this.bisInspAccuntDao.insert(bisInspAccunt);
  51. }
  52. @Override
  53. public int update(BisInspAccunt bisInspAccunt) {
  54. bisInspAccunt.setUptm(new Date());
  55. return this.bisInspAccuntDao.update(bisInspAccunt);
  56. }
  57. @Override
  58. public int delete(String id) {
  59. return this.bisInspAccuntDao.delete(id);
  60. }
  61. @Override
  62. public void batch(List<BisInspAccunt> bisInspAccunt) {
  63. for(int i = 0;i<bisInspAccunt.size();i++){
  64. int ret = update(bisInspAccunt.get(i));
  65. }
  66. }
  67. @Override
  68. public void export(HttpServletResponse response, BisInspAccuntParam bisInspAccuntParam) {
  69. String templateFilePath = templatePath+ File.separator +"zgjgtz.xls";
  70. File file = new File(templateFilePath);
  71. if(file.exists()){
  72. try{
  73. InputStream inputStream = new FileInputStream(file);
  74. Workbook workbook = WorkbookFactory.create(inputStream);
  75. exportFile(workbook,bisInspAccuntParam);
  76. writeToResponse(response,workbook,"追责结果台账.xls");
  77. }catch(Exception e){
  78. e.printStackTrace();
  79. }
  80. }
  81. }
  82. @Override
  83. public List<String> importFile(MultipartFile file, BisInspAccunt account) throws Exception {
  84. ImportUtil<BisInspAccunt> util = new ImportUtil<BisInspAccunt>(){
  85. @Override
  86. public void before(BisInspAccunt bisInspAccunt) {
  87. bisInspAccunt.setProvince(account.getProvince());
  88. bisInspAccunt.setOrgId(account.getOrgId());
  89. bisInspAccunt.setPersId(account.getPersId());
  90. }
  91. };
  92. List<BisInspAccunt> list;
  93. try {
  94. list = util.fileToList(file, BisInspAccunt.class);
  95. } catch (Exception e) { throw new RuntimeException(e); }
  96. ExcelVerifyUtil verifyUtil = new ExcelVerifyUtil<>(list, accuntVerify);
  97. List<BisInspAccunt> rightList = verifyUtil.getRightList();
  98. rightList.forEach(a -> this.insert(a));
  99. return verifyUtil.getErrorList();
  100. }
  101. private void exportFile(Workbook xssfWorkbook, BisInspAccuntParam bisInspAccuntParam) throws ParseException {
  102. List<BisInspAccunt> list = bisInspAccuntDao.findList(bisInspAccuntParam);
  103. Sheet sheet = xssfWorkbook.getSheetAt(0);
  104. String title = "追责结果台账";
  105. if(StringUtils.isNotBlank(bisInspAccuntParam.getYear())){
  106. title = title+"("+bisInspAccuntParam.getYear()+")";
  107. }
  108. CellStyle styleColor = sheet.getRow(1).getCell(0).getCellStyle();
  109. CellStyle styleContent = xssfWorkbook.createCellStyle();
  110. styleColor.setAlignment(HorizontalAlignment.CENTER);
  111. styleColor.setVerticalAlignment(VerticalAlignment.CENTER);
  112. styleContent.setAlignment(HorizontalAlignment.CENTER);
  113. styleContent.setVerticalAlignment(VerticalAlignment.CENTER);
  114. setCellValue(sheet, 0, 1, title);
  115. if(list!=null && list.size()>0){
  116. for(int i = 0;i<=list.size();i++){
  117. Row row = sheet.createRow(2+i);
  118. int cols = 9;
  119. for(int j = 0;j<=cols;j++){
  120. Cell cell = row.createCell(j);
  121. cell.setCellStyle(styleContent);
  122. if(i==list.size()){
  123. cell.setCellStyle(styleColor);
  124. }
  125. }
  126. }
  127. for(int i = 0;i<list.size();i++){
  128. BisInspAccunt bisInspAccunt = list.get(i);
  129. setAllCell(sheet,i,bisInspAccunt);
  130. }
  131. }
  132. }
  133. private void setAllCell(Sheet sheet, int i, BisInspAccunt bisInspAccunt) {
  134. setCellValue(sheet, 2 + i, 0, String.valueOf(i + 1));
  135. setCellValue(sheet, 2 + i, 1, bisInspAccunt.getAdName());
  136. setCellValue(sheet, 2 + i, 2, bisInspAccunt.getAccuntPrp().equals("1")?"被上级追责":bisInspAccunt.getAccuntPrp().equals("2")?"对下级追责":"");
  137. setCellValue(sheet, 2 + i, 3, bisInspAccunt.getAccuntMttr());
  138. setCellValue(sheet, 2 + i, 4, bisInspAccunt.getAccuntMprp().equals("1")?"责任单位":bisInspAccunt.getAccuntMprp().equals("1")?"责任人":"");
  139. setCellValue(sheet, 2 + i, 5, bisInspAccunt.getUnitName());
  140. setCellValue(sheet, 2 + i, 6, bisInspAccunt.getAccuntName());
  141. setCellValue(sheet, 2 + i, 7, bisInspAccunt.getAccuntItem());
  142. setCellValue(sheet, 2 + i, 8, bisInspAccunt.getAccuntKind());
  143. setCellValue(sheet, 2 + i, 9, simpleDateFormat.format(bisInspAccunt.getAccuntTm()));
  144. }
  145. private void setCellValue(Sheet sheet, int rownum, int cellnum, String value) {
  146. Cell cell = sheet.getRow(rownum).getCell(cellnum);
  147. cell.setCellValue(value);
  148. }
  149. private void writeToResponse(HttpServletResponse response, Workbook workbook, String name) throws IOException {
  150. // 修改模板内容导出新模板
  151. response.setContentType("application/x-msdownload");
  152. String fileNameURL = URLEncoder.encode(name, "UTF-8");
  153. response.setHeader("Content-disposition", "attachment;filename=" + fileNameURL + ";filename*=utf-8''" + fileNameURL);
  154. OutputStream out = response.getOutputStream();
  155. workbook.write(out);
  156. out.flush();
  157. out.close();
  158. }
  159. }