ExcelServiceImpl.java 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. package com.fdkankan.agent.service.impl;
  2. import cn.hutool.core.date.DateTime;
  3. import cn.hutool.core.date.DateUtil;
  4. import com.alibaba.excel.EasyExcel;
  5. import com.alibaba.excel.ExcelWriter;
  6. import com.alibaba.excel.write.metadata.WriteSheet;
  7. import com.fdkankan.agent.common.ResultCode;
  8. import com.fdkankan.agent.entity.AgentNewCamera;
  9. import com.fdkankan.agent.entity.Camera;
  10. import com.fdkankan.agent.entity.CameraDetail;
  11. import com.fdkankan.agent.exception.BusinessException;
  12. import com.fdkankan.agent.request.AuthModelingParam;
  13. import com.fdkankan.agent.service.*;
  14. import com.fdkankan.agent.util.ExcelErrorUtil;
  15. import com.fdkankan.agent.util.ExcelUtil;
  16. import lombok.extern.slf4j.Slf4j;
  17. import org.apache.commons.lang3.StringUtils;
  18. import org.apache.velocity.util.ArrayListWrapper;
  19. import org.springframework.beans.factory.annotation.Autowired;
  20. import org.springframework.stereotype.Service;
  21. import org.springframework.web.multipart.MultipartFile;
  22. import javax.servlet.http.HttpServletRequest;
  23. import javax.servlet.http.HttpServletResponse;
  24. import java.io.InputStream;
  25. import java.net.URLEncoder;
  26. import java.util.*;
  27. /**
  28. * <p>
  29. * TODO
  30. * </p>
  31. *
  32. * @author dengsixing
  33. * @since 2022/6/6
  34. **/
  35. @Service
  36. @Slf4j
  37. public class ExcelServiceImpl implements IExcelService {
  38. @Autowired
  39. IAgentNewCameraService agentNewCameraService;
  40. @Autowired
  41. ICameraService cameraService;
  42. @Autowired
  43. ICameraDetailService cameraDetailService;
  44. @Autowired
  45. IAgentAuthorizeModelingService agentAuthorizeModelingService;
  46. public void commonExport(HttpServletRequest request, HttpServletResponse response,String name,List<?> result,ExcelWriter excelWriter) throws Exception {
  47. response.setContentType("application/vnd.ms-excel");
  48. response.setCharacterEncoding("utf-8");
  49. String fileName = name + ".xlsx";
  50. fileName = URLEncoder.encode(fileName, "UTF-8");
  51. response.setHeader("Content-disposition", "attachment;filename=" + fileName);
  52. WriteSheet writeSheet = EasyExcel.writerSheet(name).build();
  53. excelWriter.write(result, writeSheet);
  54. }
  55. @Override
  56. public Integer uploadExcel(MultipartFile file, Integer agentId,Integer subAgentId) {
  57. String originalFilename = file.getOriginalFilename();
  58. assert originalFilename != null;
  59. String fileType=originalFilename.substring(originalFilename.lastIndexOf(".")+1);
  60. if (!fileType.equalsIgnoreCase("xlsx")) {
  61. throw new BusinessException(ResultCode.FILE_TYPE_ERROR);
  62. }
  63. List<HashMap<Integer, String>> excelRowList = new ArrayList<>();
  64. try {
  65. excelRowList = ExcelUtil.getExcelRowList(file);
  66. }catch (Exception e){
  67. log.info("uploadExcel-error:{}",e);
  68. throw new BusinessException(ResultCode.TEMPLATE_TYPE_ERROR);
  69. }
  70. List<Integer> errorIndex = new ArrayList<>();
  71. List<String> snCodes = new ArrayList<>();
  72. List<Long> cameraIds = new ArrayList<>();
  73. Integer index = 0;
  74. for (HashMap<Integer, String> map : excelRowList) {
  75. index ++;
  76. if(map.isEmpty()){
  77. continue;
  78. }
  79. if(index == 0 && !map.get(0).equals("分销商设备清单")){
  80. throw new BusinessException(ResultCode.TEMPLATE_TYPE_ERROR);
  81. }
  82. if(index <4){ //从第四行开始
  83. continue;
  84. }
  85. String snCode = map.get(0);
  86. if(StringUtils.isBlank(snCode)){
  87. errorIndex.add(index);
  88. continue;
  89. }
  90. if(StringUtils.isNotBlank(snCode)){
  91. Camera camera = cameraService.getBySnCode(snCode);
  92. if(camera == null){
  93. errorIndex.add(index);
  94. continue;
  95. }
  96. CameraDetail cameraDetail = cameraDetailService.getByCameraId(camera.getId());
  97. if(cameraDetail == null || cameraDetail.getAgentId() == null){
  98. errorIndex.add(index);
  99. continue;
  100. }
  101. if(!cameraDetail.getAgentId().equals(agentId)){
  102. List<AgentNewCamera> bySubAgent = agentNewCameraService.getBySubAgent(camera.getId(), agentId);
  103. if(bySubAgent.isEmpty()){
  104. errorIndex.add(index);
  105. continue;
  106. }
  107. }
  108. cameraIds.add(camera.getId());
  109. }
  110. snCodes.add(snCode);
  111. }
  112. this.toExcelError(errorIndex);
  113. if(!cameraIds.isEmpty()){
  114. return agentNewCameraService.giveCameraBatch(cameraIds,agentId,subAgentId);
  115. }
  116. return 0;
  117. }
  118. @Override
  119. public Integer uploadAuthModelExcel(MultipartFile file, Integer agentId) {
  120. String originalFilename = file.getOriginalFilename();
  121. assert originalFilename != null;
  122. String fileType=originalFilename.substring(originalFilename.lastIndexOf(".")+1);
  123. if (!fileType.equalsIgnoreCase("xlsx")) {
  124. throw new BusinessException(ResultCode.FILE_TYPE_ERROR);
  125. }
  126. List<HashMap<Integer, String>> excelRowList = new ArrayList<>();
  127. List<HashMap<Integer, String>> excelRowListTemplate = new ArrayList<>();
  128. try {
  129. excelRowList = ExcelUtil.getExcelRowList(file);
  130. InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("template/authModel.xlsx");
  131. excelRowListTemplate = ExcelUtil.getExcelRowList(inputStream);
  132. }catch (Exception e){
  133. log.info("uploadExcel-error:{}",e);
  134. throw new BusinessException(ResultCode.TEMPLATE_TYPE_ERROR);
  135. }
  136. HashMap<Integer, String> mapt = excelRowListTemplate.get(0);
  137. HashMap<Integer, String> map1 = excelRowList.get(0);
  138. if(!mapt.equals(map1)){
  139. throw new BusinessException(ResultCode.TEMPLATE_TYPE_ERROR);
  140. }
  141. List<Integer> errorIndex = new ArrayList<>();
  142. List<AuthModelingParam> params = new ArrayList();
  143. Integer index = 0;
  144. for (HashMap<Integer, String> map : excelRowList) {
  145. index ++;
  146. if(map.isEmpty()){
  147. continue;
  148. }
  149. if(index <2){ //从第四行开始
  150. continue;
  151. }
  152. String authSn = map.get(0);
  153. String type = map.get(1);
  154. String createDate = map.get(2);
  155. String activated = map.get(3);
  156. String activateDate = map.get(4);
  157. String disabled = map.get(5);
  158. String transfer = map.get(6);
  159. String borrow = map.get(7);
  160. String startDate = map.get(8);
  161. String endDate = map.get(9);
  162. String expirationDay = map.get(10);
  163. if(StringUtils.isBlank(authSn) ){
  164. log.info("数据错误:{}",map);
  165. continue;
  166. }
  167. AuthModelingParam param = new AuthModelingParam();
  168. if(StringUtils.isNotBlank(activateDate) && StringUtils.isNotBlank(expirationDay)){
  169. try {
  170. DateTime parse = DateUtil.parse(activateDate, "yyyy/MM/dd HH:mm:ss");
  171. DateTime dateTime = DateUtil.offsetDay(parse, Integer.parseInt(expirationDay));
  172. param.setStartTime(parse);
  173. param.setEndTime(dateTime);
  174. }catch (Exception e){
  175. log.info("解析激活时间错误:{},{}",activateDate,expirationDay);
  176. }
  177. }
  178. param.setAuthCode(authSn);
  179. param.setAgentId(agentId);
  180. params.add(param);
  181. }
  182. if(!params.isEmpty()){
  183. agentAuthorizeModelingService.addByParam(params);
  184. }
  185. this.toExcelError(errorIndex);
  186. return 0;
  187. }
  188. public void toExcelError(List<Integer> errorList) {
  189. String resultIn = ExcelErrorUtil.getResultIn(errorList);
  190. if(StringUtils.isNotBlank(resultIn)){
  191. throw new BusinessException(ResultCode.UPLOAD_EXCEL_ERROR,resultIn);
  192. }
  193. }
  194. }