ExcelServiceImpl.java 9.1 KB

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