Przeglądaj źródła

单位导入 加字段

jz.kai 5 lat temu
rodzic
commit
a0d319ab33

+ 2 - 0
common/src/main/java/com/jpsoft/smart/modules/base/dao/CompanyInfoDAO.java

@@ -35,4 +35,6 @@ public interface CompanyInfoDAO {
     List<CompanyInfo> findByCompanyCode(String code, Long personId);
 
     List<CompanyInfo> findByParentId(@Param("parentId") String parentId);
+
+	CompanyInfo findByName(String name);
 }

+ 2 - 0
common/src/main/java/com/jpsoft/smart/modules/base/dto/CompanyInfoDTO.java

@@ -18,6 +18,8 @@ public class CompanyInfoDTO {
     private Integer sortNo;
     @ApiModelProperty(value = "公司名称")
     private String name;
+    @ApiModelProperty(value = "公司简称")
+    private String shortName;
     @ApiModelProperty(value = "备注")
     private String remark;
     @ApiModelProperty(value = "创建人")

+ 2 - 0
common/src/main/java/com/jpsoft/smart/modules/base/entity/CompanyInfo.java

@@ -24,6 +24,8 @@ public class CompanyInfo {
 	private Integer sortNo;
     @ApiModelProperty(value = "公司名称")
 	private String name;
+	@ApiModelProperty(value = "公司简称")
+	private String shortName;
     @ApiModelProperty(value = "备注")
 	private String remark;
     @ApiModelProperty(value = "创建人")

+ 1 - 0
common/src/main/java/com/jpsoft/smart/modules/base/service/CompanyInfoService.java

@@ -20,4 +20,5 @@ public interface CompanyInfoService {
     int updateCode(String oldCode, String newCode);
     Boolean hasChildren(String parentId);
 	List<CompanyInfo> findByParentId(String parentId);
+	CompanyInfo findByName(String name);
 }

+ 5 - 0
common/src/main/java/com/jpsoft/smart/modules/base/service/impl/CompanyInfoServiceImpl.java

@@ -97,4 +97,9 @@ public class CompanyInfoServiceImpl implements CompanyInfoService {
 	public List<CompanyInfo> findByParentId(String parentId) {
 		return companyInfoDAO.findByParentId(parentId);
 	}
+
+	@Override
+	public CompanyInfo findByName(String name) {
+		return companyInfoDAO.findByName(name);
+	}
 }

+ 12 - 1
common/src/main/resources/mapper/base/CompanyInfo.xml

@@ -7,6 +7,7 @@
         <id property="id" column="id_"/>
         <result property="sortNo" column="sort_no"/>
         <result property="name" column="name_"/>
+        <result property="shortName" column="short_name"/>
         <result property="logo" column="logo_"/>
         <result property="remark" column="remark_"/>
         <result property="createBy" column="create_by"/>
@@ -28,7 +29,7 @@
         -->
         <![CDATA[
 		insert into base_company_info
-	    (id_,sort_no,name_,logo_,remark_,create_by,create_time,
+	    (id_,sort_no,name_,short_name,logo_,remark_,create_by,create_time,
 	    update_by,update_time,del_flag,parent_id,code_,type_,
 	    wall_url)
 		values
@@ -36,6 +37,7 @@
 			#{id,jdbcType=VARCHAR}
 			,#{sortNo,jdbcType= NUMERIC }
 			,#{name,jdbcType=VARCHAR}
+			,#{shortName,jdbcType=VARCHAR}
 			,#{logo,jdbcType=VARCHAR}
 			,#{remark,jdbcType=VARCHAR}
 			,#{createBy,jdbcType=VARCHAR}
@@ -62,6 +64,9 @@
             <if test="name!=null">
                 name_=#{name,jdbcType=VARCHAR},
             </if>
+            <if test="name!=null">
+                short_name=#{shortName,jdbcType=VARCHAR},
+            </if>
             <if test="logo!=null">
                 logo_=#{logo,jdbcType=VARCHAR},
             </if>
@@ -193,4 +198,10 @@
         </if>
         order by a.sort_no asc,a.name_ asc
     </select>
+    <select id="findByName" parameterType="string" resultMap="CompanyInfoMap">
+        select a.*,b.name_ as parent_name
+        from base_company_info a
+        left join base_company_info b on a.parent_id = b.id_
+        where a.name_=#{0} and a.del_flag = 0
+    </select>
 </mapper>

+ 128 - 0
web/src/main/java/com/jpsoft/smart/modules/base/controller/CompanyInfoController.java

@@ -1,9 +1,13 @@
 package com.jpsoft.smart.modules.base.controller;
 
 import com.github.pagehelper.Page;
+import com.jpsoft.smart.config.OSSConfig;
 import com.jpsoft.smart.modules.base.dto.CompanyInfoDTO;
 import com.jpsoft.smart.modules.base.entity.*;
 import com.jpsoft.smart.modules.base.service.*;
+import com.jpsoft.smart.modules.common.utils.CheckIdCard;
+import com.jpsoft.smart.modules.common.utils.OSSUtil;
+import com.jpsoft.smart.modules.common.utils.POIUtils;
 import com.jpsoft.smart.modules.common.utils.PojoUtils;
 import com.jpsoft.smart.modules.common.dto.Sort;
 import com.jpsoft.smart.modules.common.dto.MessageResult;
@@ -19,14 +23,19 @@ import io.swagger.annotations.ApiImplicitParam;
 import io.swagger.annotations.ApiImplicitParams;
 import io.swagger.annotations.ApiOperation;
 import org.apache.commons.lang3.StringUtils;
+import org.apache.poi.ss.usermodel.Sheet;
+import org.apache.poi.ss.usermodel.Workbook;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.transaction.interceptor.TransactionAspectSupport;
 import org.springframework.web.bind.annotation.*;
+import org.springframework.web.multipart.MultipartFile;
 
 import javax.servlet.http.HttpServletRequest;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
 import java.math.BigDecimal;
 import java.text.DecimalFormat;
 import java.text.SimpleDateFormat;
@@ -38,6 +47,9 @@ import java.util.*;
 public class CompanyInfoController {
     private Logger logger = LoggerFactory.getLogger(getClass());
 
+    @Autowired
+    private OSSConfig ossConfig;
+
     @Autowired
     private CompanyInfoService companyInfoService;
 
@@ -682,4 +694,120 @@ public class CompanyInfoController {
 
         return msgResult;
     }
+
+    @ApiOperation(value="导入企业")
+    @PostMapping("importXls")
+    @ApiImplicitParams({
+            @ApiImplicitParam(name = "uploadFile",value = "上传文件", required = true,paramType="form", dataType = "__file")
+    })
+    public MessageResult<String> importXls(MultipartFile uploadFile,
+                                           @RequestAttribute String subject){
+        User user = userService.get(subject);
+
+        MessageResult<String> msgResult = new MessageResult<>();
+
+        try {
+            POIUtils poiUtils = new POIUtils(uploadFile.getInputStream());
+            int sheetIndex = 0;
+            Sheet sheet1 = poiUtils.getSheetAt(sheetIndex);
+
+            int affectCount = 0;
+            int failCount = 0;
+            int validateColIndex = 10;
+
+            for(int rowIndex=1 ; rowIndex<=sheet1.getLastRowNum(); rowIndex++){
+                try {
+                    String name = (String)poiUtils.getCellValue(sheetIndex,rowIndex,1).toString().replace(" ","");
+                    String parentName = (String)poiUtils.getCellValue(sheetIndex,rowIndex,2).toString().replace(" ","");
+                    String type = (String)poiUtils.getCellValue(sheetIndex,rowIndex,3).toString().replace(" ","");
+                    String shortName = (String)poiUtils.getCellValue(sheetIndex,rowIndex,4).toString().replace(" ","");
+                    String position1 = (String)poiUtils.getCellValue(sheetIndex,rowIndex,5).toString().replace(" ","");
+                    String position2 = (String)poiUtils.getCellValue(sheetIndex,rowIndex,6).toString().replace(" ","");
+                    String position3 = (String)poiUtils.getCellValue(sheetIndex,rowIndex,7).toString().replace(" ","");
+                    String position4 = (String)poiUtils.getCellValue(sheetIndex,rowIndex,8).toString().replace(" ","");
+                    String position5 = (String)poiUtils.getCellValue(sheetIndex,rowIndex,9).toString().replace(" ","");
+
+                    if(StringUtils.isEmpty(name)){
+                        break;
+                    }
+
+                    CompanyInfo companyInfo = companyInfoService.findByName(name);
+
+                    if (companyInfo != null) {
+                        sheet1.getRow(rowIndex).createCell(validateColIndex).setCellValue("该企业已存在!");
+                        failCount++;
+                        continue;
+                    } else {
+                        companyInfo = new CompanyInfo();
+                        companyInfo.setId(UUID.randomUUID().toString());
+                        companyInfo.setName(name);
+                        if(StringUtils.isNotEmpty(parentName)) {
+                            CompanyInfo li = companyInfoService.findByName(parentName);
+                            if(li != null) {
+                                companyInfo.setParentId(li.getId());
+                            }
+                        }
+                        companyInfo.setType(type);
+                        companyInfo.setShortName(shortName);
+                        companyInfo.setDelFlag(false);
+                        companyInfo.setCreateBy(subject);
+                        companyInfo.setCreateTime(new Date());
+
+                        if (companyInfoService.insert(companyInfo) > 0) {
+                            affectCount++;
+
+                            CompanyPosition companyPosition = new CompanyPosition();
+                            companyPosition.setId(UUID.randomUUID().toString());
+                            companyPosition.setCompanyId(companyInfo.getId());
+                            companyPosition.setPosition1Name(position1);
+                            companyPosition.setPosition2Name(position2);
+                            companyPosition.setPosition3Name(position3);
+                            companyPosition.setPosition4Name(position4);
+                            companyPosition.setPosition5Name(position5);
+                            companyPosition.setDelFlag(false);
+                            companyPosition.setCreateBy(subject);
+                            companyPosition.setCreateTime(new Date());
+                            companyPositionService.insert(companyPosition);
+                        }
+                    }
+                }
+                catch(Exception innerEx){
+                    logger.error(innerEx.getMessage(),innerEx);
+                }
+            }
+
+            if (failCount>0){
+                //有导入失败的记录
+                msgResult.setResult(false);
+                msgResult.setMessage("数据成功导入" + affectCount + "条,有" + failCount + "条数据未导入成功,错误原因请查看报表。");
+
+                //todo 只保留错误数据的sheet
+                Workbook wb = poiUtils.exportErrorXls(sheetIndex,validateColIndex,1 + affectCount + failCount);
+
+                //todo 将wb保存到oss
+                ByteArrayOutputStream output = new ByteArrayOutputStream();
+                wb.write(output);
+
+                byte[] buffer = output.toByteArray();
+                ByteArrayInputStream input = new ByteArrayInputStream(buffer);
+
+                String downloadUrl = OSSUtil.upload(ossConfig,"import","error.xls",input);
+
+                //todo 返回导入失败报表下载链接
+                msgResult.setData(downloadUrl);
+            }
+            else{
+                msgResult.setResult(true);
+                msgResult.setMessage("数据成功导入" + affectCount + "条");
+            }
+        }
+        catch(Exception ex){
+            logger.error(ex.getMessage(),ex);
+
+            msgResult.setResult(false);
+            msgResult.setMessage(ex.getMessage());
+        }
+
+        return msgResult;
+    }
 }