ExcelServiceImpl.java 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. package com.fdkankan.manage.service.impl;
  2. import com.alibaba.excel.EasyExcel;
  3. import com.alibaba.excel.ExcelWriter;
  4. import com.alibaba.excel.write.metadata.WriteSheet;
  5. import com.fdkankan.manage.common.ExcelErrorUtil;
  6. import com.fdkankan.manage.common.ResultCode;
  7. import com.fdkankan.manage.entity.AgentNew;
  8. import com.fdkankan.manage.exception.BusinessException;
  9. import com.fdkankan.manage.constant.CameraOutTypeEnum;
  10. import com.fdkankan.manage.entity.Company;
  11. import com.fdkankan.manage.service.IAgentNewService;
  12. import com.fdkankan.manage.service.ICameraService;
  13. import com.fdkankan.manage.service.ICompanyService;
  14. import com.fdkankan.manage.service.IExcelService;
  15. import com.fdkankan.manage.util.ExcelUtil;
  16. import com.fdkankan.manage.vo.request.CameraInOutParam;
  17. import lombok.extern.slf4j.Slf4j;
  18. import org.apache.commons.lang3.StringUtils;
  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 javax.validation.constraints.Size;
  25. import java.io.IOException;
  26. import java.net.URLEncoder;
  27. import java.util.ArrayList;
  28. import java.util.HashMap;
  29. import java.util.List;
  30. /**
  31. * <p>
  32. * TODO
  33. * </p>
  34. *
  35. * @author dengsixing
  36. * @since 2022/6/6
  37. **/
  38. @Service
  39. @Slf4j
  40. public class ExcelServiceImpl implements IExcelService {
  41. @Autowired
  42. ICompanyService companyService;
  43. @Autowired
  44. ICameraService cameraService;
  45. @Autowired
  46. IAgentNewService agentNewService;
  47. public void commonExport(HttpServletRequest request, HttpServletResponse response,String name,List<?> result,ExcelWriter excelWriter) throws Exception {
  48. response.setContentType("application/vnd.ms-excel");
  49. response.setCharacterEncoding("utf-8");
  50. String fileName = name + ".xlsx";
  51. fileName = URLEncoder.encode(fileName, "UTF-8");
  52. response.setHeader("Content-disposition", "attachment;filename=" + fileName);
  53. WriteSheet writeSheet = EasyExcel.writerSheet(name).build();
  54. excelWriter.write(result, writeSheet);
  55. }
  56. public void commonExport(HttpServletRequest request, HttpServletResponse response,String name,List<?> result,Class<?> clazz) throws Exception {
  57. response.setContentType("application/vnd.ms-excel");
  58. response.setCharacterEncoding("utf-8");
  59. String fileName = name + ".xlsx";
  60. fileName = URLEncoder.encode(fileName, "UTF-8");
  61. response.setHeader("Content-disposition", "attachment;filename=" + fileName);
  62. ExcelWriter excelWriter = null;
  63. try {
  64. excelWriter = EasyExcel.write(response.getOutputStream(), clazz).build();
  65. WriteSheet writeSheet = EasyExcel.writerSheet(name).build();
  66. excelWriter.write(result, writeSheet);
  67. }catch (Exception e){
  68. e.printStackTrace();
  69. }finally {
  70. if(excelWriter != null){
  71. excelWriter.finish();
  72. }
  73. }
  74. }
  75. @Override
  76. public Integer uploadExcel(MultipartFile file, Integer type) {
  77. String originalFilename = file.getOriginalFilename();
  78. assert originalFilename != null;
  79. String fileType=originalFilename.substring(originalFilename.lastIndexOf(".")+1);
  80. if (!fileType.equalsIgnoreCase("xlsx")) {
  81. throw new BusinessException(ResultCode.FILE_TYPE_ERROR);
  82. }
  83. List<HashMap<Integer, String>> excelRowList = new ArrayList<>();
  84. try {
  85. excelRowList = ExcelUtil.getExcelRowList(file);
  86. }catch (Exception e){
  87. throw new BusinessException(ResultCode.TEMPLATE_TYPE_ERROR);
  88. }
  89. List<String> wifiNameList = new ArrayList<>();
  90. List<CameraInOutParam> params = new ArrayList<>();
  91. List<CameraInOutParam> companyParams = new ArrayList<>();
  92. List<Integer> errorIndex = new ArrayList<>();
  93. Integer index = 0;
  94. for (HashMap<Integer, String> map : excelRowList) {
  95. index ++;
  96. if(type == 0){ //入库
  97. if(index == 0 && !map.get(0).equals("设备入库模板")){
  98. throw new BusinessException(ResultCode.TEMPLATE_TYPE_ERROR);
  99. }
  100. if(index <4){ //从第四行开始
  101. continue;
  102. }
  103. String wifiName = map.get(0);
  104. if(StringUtils.isBlank(wifiName)){
  105. errorIndex.add(index -3);
  106. }
  107. wifiNameList.add(wifiName);
  108. }else if(type == 1){ //出库
  109. if(index == 0 && !map.get(0).equals("设备出库模板")){
  110. throw new BusinessException(ResultCode.TEMPLATE_TYPE_ERROR);
  111. }
  112. if(index <4){
  113. continue;
  114. }
  115. String snCode = map.get(0);
  116. String outTypeString = map.get(1);
  117. String companyName = map.get(2);
  118. String orderSn = map.get(3);
  119. String agentName = map.get(4);
  120. CameraOutTypeEnum outTypeEnum = CameraOutTypeEnum.getByMsg(outTypeString);
  121. if(outTypeEnum == null || StringUtils.isBlank(snCode)){
  122. log.error("outError-->出库错误:出库类型为空或snCode为空:snCode:{},outType:{},companyName:{},orderSn:{},agentName:{}"
  123. ,snCode,outTypeString,companyName,orderSn,agentName);
  124. errorIndex.add(index -3);
  125. }
  126. CameraInOutParam param = new CameraInOutParam();
  127. if(outTypeEnum != null){
  128. int outType = outTypeEnum.getCode();
  129. param.setOutType(outType);
  130. }
  131. if(param.getOutType() != null && param.getOutType() == 4 && StringUtils.isBlank(agentName)){
  132. log.error("outError-->出库错误:经销商为空错误:snCode:{},outType:{},companyName:{},orderSn:{},agentName:{}"
  133. ,snCode,outTypeString,companyName,orderSn,agentName);
  134. errorIndex.add(index -3);
  135. }
  136. if(param.getOutType() != null && param.getOutType() != 4 && StringUtils.isNotBlank(agentName)){
  137. log.error("outError-->出库错误:出库类型错误:snCode:{},outType:{},companyName:{},orderSn:{},agentName:{}"
  138. ,snCode,outTypeString,companyName,orderSn,agentName);
  139. errorIndex.add(index -3);
  140. }
  141. if(StringUtils.isNotBlank(agentName)){
  142. AgentNew agentNew = agentNewService.getByName(agentName);
  143. if(agentNew == null){
  144. log.error("outError-->出库错误:代理商不存在:snCode:{},outType:{},companyName:{},orderSn:{},agentName:{}"
  145. ,snCode,outTypeString,companyName,orderSn,agentName);
  146. errorIndex.add(index -3);
  147. }else {
  148. param.setAgentId(agentNew.getId());
  149. }
  150. }
  151. param.setCompanyName(companyName);
  152. param.setOrderSn(orderSn);
  153. param.setSnCode(snCode);
  154. params.add(param);
  155. }else if(type == 2){ //关联客户
  156. if(index == 0 && !map.get(0).equals("客户关联模板")){
  157. throw new BusinessException(ResultCode.TEMPLATE_TYPE_ERROR);
  158. }
  159. if(index <4){
  160. continue;
  161. }
  162. CameraInOutParam param = new CameraInOutParam();
  163. param.setCompanyName(map.get(0));
  164. param.setSnCode(map.get(1));
  165. if(StringUtils.isBlank(param.getSnCode()) || StringUtils.isBlank(param.getCompanyName())){
  166. errorIndex.add(index -3 );
  167. }
  168. companyParams.add(param);
  169. }
  170. }
  171. this.toExcelError(errorIndex);
  172. if(type == 0 && wifiNameList.size() <=0){
  173. throw new BusinessException(ResultCode.IN_TEMPLATE_EMPTY);
  174. }else if(type == 1 && params.size() <=0){
  175. throw new BusinessException(ResultCode.OUT_TEMPLATE_EMPTY);
  176. }else if(type == 2 && companyParams.size() <=0){
  177. throw new BusinessException(ResultCode.COMPANY_TEMPLATE_EMPTY);
  178. }
  179. if(wifiNameList.size() >0){
  180. return cameraService.ins(wifiNameList);
  181. }
  182. if(params.size() >0){
  183. return cameraService.outs(params);
  184. }
  185. if(companyParams.size() >0){
  186. return cameraService.updateCompany(companyParams);
  187. }
  188. return 0;
  189. }
  190. @Override
  191. public void toExcelError(List<Integer> errorList) {
  192. String resultIn = ExcelErrorUtil.getResultIn(errorList);
  193. if(StringUtils.isNotBlank(resultIn)){
  194. throw new BusinessException(-1,resultIn);
  195. }
  196. }
  197. }