Parcourir la source

+ 小程序页面修改
+ 新增用户批量导入功能

chen.cheng il y a 10 mois
Parent
commit
28ad0a4de4

+ 16 - 0
ruoyi-admin/src/main/java/com/ruoyi/web/controller/cp/CpsMeetingUsrController.java

@@ -29,6 +29,7 @@ import org.springframework.web.bind.annotation.PutMapping;
 import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.multipart.MultipartFile;
 
 import javax.servlet.http.HttpServletResponse;
 import java.util.HashMap;
@@ -94,6 +95,21 @@ public class CpsMeetingUsrController extends BaseController {
         return getDataTable(list);
     }
 
+    @PostMapping("/importTemplate")
+    public void importTemplate(HttpServletResponse response) {
+        ExcelUtil<CpsMeetingUsr> util = new ExcelUtil<>(CpsMeetingUsr.class);
+        util.importTemplateExcel(response, "参会用户数据");
+    }
+
+    @PostMapping("/importData")
+    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
+        ExcelUtil<CpsMeetingUsr> util = new ExcelUtil<>(CpsMeetingUsr.class);
+        List<CpsMeetingUsr> userList = util.importExcel(file.getInputStream());
+        String operName = getUsername();
+        String message = cpsMeetingUsrService.importUser(userList, updateSupport, operName);
+        return success(message);
+    }
+
     /**
      * 导出cps_usr_wechat微信用户列表
      */

+ 6 - 12
ruoyi-system/src/main/java/com/ruoyi/system/domain/CpsMeetingUsr.java

@@ -5,6 +5,8 @@ import com.ruoyi.common.core.domain.BaseEntity;
 import org.apache.commons.lang3.builder.ToStringBuilder;
 import org.apache.commons.lang3.builder.ToStringStyle;
 
+import javax.validation.constraints.NotBlank;
+
 /**
  * cps_usr_wechat微信用户对象 cps_meeting_usr
  *
@@ -23,39 +25,31 @@ public class CpsMeetingUsr extends BaseEntity {
      * 文件路径
      */
     @Excel(name = "用户名")
+    @NotBlank(message = "用户名不能为空")
     private String usrName;
 
     /**
      * 文件名称
      */
     @Excel(name = "手机号")
+    @NotBlank(message = "手机号不能为空")
     private String tel;
 
     /**
      * 微信开放id
      */
-    @Excel(name = "微信开放id")
     private String openId;
 
-    /**
-     * 与会人员企业
-     */
-    @Excel(name = "与会人员单位")
     private Long enterpriseId;
 
-    /**
-     * 参会行程信息
-     */
-    @Excel(name = "参会行程信息")
     private Long tripId;
 
-    @Excel(name = "参会单位信息")
     private String enterpriseName;
-    @Excel(name = "行程名称")
+
     private String tripName;
 
     private String avatarUrl;
-    @Excel(name = "性别")
+    @Excel(name = "性别", dictType = "sys_user_sex")
     private String sex;
 
     public void setId(Long id) {

+ 2 - 0
ruoyi-system/src/main/java/com/ruoyi/system/service/ICpsMeetingUsrService.java

@@ -74,5 +74,7 @@ public interface ICpsMeetingUsrService {
 
     List<CpsContentInfo> qryUsrCollectContent(String openId, String keyword);
 
+    String importUser(List<CpsMeetingUsr> userList, Boolean isUpdateSupport, String operName);
+
     CpsMeetingEnterpriseTripInfo qryUsrTripContent(String openId);
 }

+ 54 - 1
ruoyi-system/src/main/java/com/ruoyi/system/service/impl/CpsMeetingUsrServiceImpl.java

@@ -10,6 +10,8 @@ import com.ruoyi.common.exception.ServiceException;
 import com.ruoyi.common.utils.DateUtils;
 import com.ruoyi.common.utils.SecurityUtils;
 import com.ruoyi.common.utils.StreamUtils;
+import com.ruoyi.common.utils.StringUtils;
+import com.ruoyi.common.utils.bean.BeanValidators;
 import com.ruoyi.system.domain.CpsContentInfo;
 import com.ruoyi.system.domain.CpsMeetingEnterpriseTripInfo;
 import com.ruoyi.system.domain.CpsMeetingUsr;
@@ -22,9 +24,12 @@ import com.ruoyi.system.service.ICpsMeetingUsrService;
 import io.foldright.cffu.CompletableFutureUtils;
 import org.apache.commons.collections4.CollectionUtils;
 import org.apache.commons.lang3.ObjectUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
+import javax.validation.Validator;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
@@ -40,6 +45,8 @@ import java.util.concurrent.ScheduledExecutorService;
  */
 @Service
 public class CpsMeetingUsrServiceImpl implements ICpsMeetingUsrService {
+    private static final Logger log = LoggerFactory.getLogger(CpsMeetingUsrServiceImpl.class);
+
     @Autowired
     private CpsMeetingUsrMapper cpsMeetingUsrMapper;
 
@@ -58,6 +65,10 @@ public class CpsMeetingUsrServiceImpl implements ICpsMeetingUsrService {
     @Autowired
     private AppCfg appCfg;
 
+
+    @Autowired
+    protected Validator validator;
+
     /**
      * 查询cps_usr_wechat微信用户
      *
@@ -161,7 +172,7 @@ public class CpsMeetingUsrServiceImpl implements ICpsMeetingUsrService {
         user.setReviewState(ReviewStatus.WAIT.getCode());
         user.setApplyType(type);
         user.setReviewMsg("");
-        WeChatUser loginUser = (WeChatUser)SecurityUtils.getLoginUser();
+        WeChatUser loginUser = (WeChatUser) SecurityUtils.getLoginUser();
         user.setUpdateBy(loginUser.getOpenid());
         CpsContentInfo cpsContentInfo = contentInfoMapper.selectCpsContentInfoById(user.getContentId());
         user.setDeptId(cpsContentInfo.getDeptId());
@@ -227,6 +238,48 @@ public class CpsMeetingUsrServiceImpl implements ICpsMeetingUsrService {
     }
 
     @Override
+    public String importUser(List<CpsMeetingUsr> userList, Boolean isUpdateSupport, String operName) {
+        if (StringUtils.isNull(userList) || userList.size() == 0) {
+            throw new ServiceException("导入用户数据不能为空!");
+        }
+        int successNum = 0;
+        int failureNum = 0;
+        StringBuilder successMsg = new StringBuilder();
+        StringBuilder failureMsg = new StringBuilder();
+        for (CpsMeetingUsr user : userList) {
+            try {
+                // 验证是否存在这个用户
+                Integer cnt = cpsMeetingUsrMapper.exitUser(user.getTel());
+                if (cnt < 1) {
+                    BeanValidators.validateWithException(validator, user);
+                    user.setCreateBy(operName);
+                    cpsMeetingUsrMapper.insertCpsMeetingUsr(user);
+                    successNum++;
+                } else if (isUpdateSupport) {
+                    BeanValidators.validateWithException(validator, user);
+                    user.setUpdateBy(operName);
+                    cpsMeetingUsrMapper.updateCpsMeetingUsrByTel(user);
+                    successNum++;
+                } else {
+                    failureNum++;
+                    failureMsg.append("<br/>" + failureNum + "、账号 " + user.getTel() + " 已存在");
+                }
+            } catch (Exception e) {
+                failureNum++;
+                log.info("导入用户失败:%s", e.getMessage());
+                failureMsg.append(e.getMessage());
+            }
+        }
+        if (failureNum > 0) {
+            failureMsg.insert(0, "很抱歉,导入失败!共 " + failureNum + " 条数据格式不正确,错误如下:");
+            throw new ServiceException(failureMsg.toString());
+        } else {
+            successMsg.insert(0, "恭喜您,数据已全部导入成功!共 " + successNum + " 条");
+        }
+        return successMsg.toString();
+    }
+
+    @Override
     public CpsMeetingEnterpriseTripInfo qryUsrTripContent(String openId) {
         CpsMeetingUsr cpsMeetingUsr = cpsMeetingUsrMapper.selectCpsMeetingUsrByOpenId(openId);
         if (ObjectUtils.isEmpty(cpsMeetingUsr)) {

+ 81 - 1
ruoyi-ui/src/views/cp/usr/index.vue

@@ -65,6 +65,15 @@
       </el-col>
       <el-col :span="1.5">
         <el-button
+            type="info"
+            plain
+            icon="el-icon-upload2"
+            size="mini"
+            @click="handleImport"
+        >导入</el-button>
+      </el-col>
+      <el-col :span="1.5">
+        <el-button
           type="warning"
           plain
           icon="el-icon-download"
@@ -151,11 +160,41 @@
         <el-button @click="cancel">取 消</el-button>
       </div>
     </el-dialog>
+    <!-- 用户导入对话框 -->
+    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
+      <el-upload
+          ref="upload"
+          :limit="1"
+          accept=".xlsx, .xls"
+          :headers="upload.headers"
+          :action="upload.url + '?updateSupport=' + upload.updateSupport"
+          :disabled="upload.isUploading"
+          :on-progress="handleFileUploadProgress"
+          :on-success="handleFileSuccess"
+          :auto-upload="false"
+          drag
+      >
+        <i class="el-icon-upload"></i>
+        <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
+        <div class="el-upload__tip text-center" slot="tip">
+          <div class="el-upload__tip" slot="tip">
+            <el-checkbox v-model="upload.updateSupport" /> 是否更新已经存在的用户数据
+          </div>
+          <span>仅允许导入xls、xlsx格式文件。</span>
+          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;" @click="importTemplate">下载模板</el-link>
+        </div>
+      </el-upload>
+      <div slot="footer" class="dialog-footer">
+        <el-button type="primary" @click="submitFileForm">确 定</el-button>
+        <el-button @click="upload.open = false">取 消</el-button>
+      </div>
+    </el-dialog>
   </div>
 </template>
 
 <script>
 import { listUsr, getUsr, delUsr, addUsr, updateUsr } from "@/api/cp/usr";
+import { getToken } from '@/utils/auth';
 
 export default {
   name: "Usr",
@@ -192,13 +231,54 @@ export default {
       form: {},
       // 表单校验
       rules: {
-      }
+      },
+      // 用户导入参数
+      upload: {
+        // 是否显示弹出层(用户导入)
+        open: false,
+        // 弹出层标题(用户导入)
+        title: "",
+        // 是否禁用上传
+        isUploading: false,
+        // 是否更新已经存在的用户数据
+        updateSupport: 0,
+        // 设置上传的请求头部
+        headers: { Authorization: "Bearer " + getToken() },
+        // 上传的地址
+        url: process.env.VUE_APP_BASE_API + "/cp/usr/importData"
+      },
     };
   },
   created() {
     this.getList();
   },
   methods: {
+    // 提交上传文件
+    submitFileForm() {
+      this.$refs.upload.submit();
+    },
+    /** 导入按钮操作 */
+    handleImport() {
+      this.upload.title = "参会用户导入";
+      this.upload.open = true;
+    },
+    /** 下载模板操作 */
+    importTemplate() {
+      this.download('cp/usr/importTemplate', {
+      }, `user_template_${new Date().getTime()}.xlsx`)
+    },
+    // 文件上传中处理
+    handleFileUploadProgress(event, file, fileList) {
+      this.upload.isUploading = true;
+    },
+    // 文件上传成功处理
+    handleFileSuccess(response, file, fileList) {
+      this.upload.open = false;
+      this.upload.isUploading = false;
+      this.$refs.upload.clearFiles();
+      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
+      this.getList();
+    },
     /** 查询cps_usr_wechat微信用户列表 */
     getList() {
       this.loading = true;