Browse Source

导入EXCEl

huianan 2 years ago
parent
commit
529b02b188

+ 17 - 0
ruoyi-admin/src/main/java/com/ruoyi/web/controller/post/PostNewsController.java

@@ -4,6 +4,7 @@ import com.ruoyi.common.annotation.Log;
 import com.ruoyi.common.constant.UserConstants;
 import com.ruoyi.common.core.controller.BaseController;
 import com.ruoyi.common.core.domain.AjaxResult;
+import com.ruoyi.common.core.domain.entity.SysUser;
 import com.ruoyi.common.core.page.TableDataInfo;
 import com.ruoyi.common.enums.BusinessType;
 import com.ruoyi.common.utils.poi.ExcelUtil;
@@ -13,6 +14,7 @@ import io.swagger.annotations.ApiOperation;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.security.access.prepost.PreAuthorize;
 import org.springframework.web.bind.annotation.*;
+import org.springframework.web.multipart.MultipartFile;
 
 import javax.servlet.http.HttpServletResponse;
 import java.util.List;
@@ -43,6 +45,21 @@ public class PostNewsController extends BaseController
         return getDataTable(list);
     }
 
+    /**
+     * 导入用户手机号
+     */
+    @Log(title = "用户手机导入", businessType = BusinessType.IMPORT)
+    @PreAuthorize("@ss.hasPermi('system:user:import')")
+    @PostMapping("/importData")
+    public AjaxResult importData(MultipartFile file, boolean updateSupport,PostNews postNews) throws Exception
+    {
+        ExcelUtil<PostNews> util = new ExcelUtil<PostNews>(PostNews.class);
+        List<PostNews> phoneList = util.importExcel(file.getInputStream());
+        String operName = getUsername();
+        String message = postNewsService.importPhone(phoneList, updateSupport, operName,postNews);
+        return success(message);
+    }
+
     /**
      * 导出消息;
      */

+ 62 - 52
ruoyi-post/src/main/java/com/ruoyi/post/domain/PostNews.java

@@ -4,122 +4,132 @@ import com.ruoyi.common.annotation.Excel;
 import com.ruoyi.common.core.domain.BaseEntity;
 import org.apache.commons.lang3.builder.ToStringBuilder;
 import org.apache.commons.lang3.builder.ToStringStyle;
+
+import java.util.List;
+
 /**
  * @Author HuiANan
  * @Date 2023/1/17 20:39
  * @Exegesis:
  */
-public class PostNews extends BaseEntity
-{
+public class PostNews extends BaseEntity {
     private static final long serialVersionUID = 1L;
 
-    /** 消息ID */
+    /**
+     * 消息ID
+     */
     private Integer newsId;
 
-    /** 消息标题 */
+    /**
+     * 消息标题
+     */
     @Excel(name = "消息标题")
     private String newsTitle;
 
-    /** 消息内容 */
+    /**
+     * 消息内容
+     */
     @Excel(name = "消息内容")
     private String newsContent;
 
-    /** 消息状态(0正常 1关闭) */
+    /**
+     * 消息状态(0正常 1关闭)
+     */
     @Excel(name = "消息状态", readConverterExp = "0=正常,1=关闭")
     private String status;
 
-    /** 发行方ID */
+    /**
+     * 发行方ID
+     */
     @Excel(name = "发行方ID")
     private Integer publisherId;
 
-    /** 发行方名称 */
+    /**
+     * 发行方名称
+     */
     @Excel(name = "发行方名称")
     private String publisherBy;
 
-    /** 用户ID */
+    /**
+     * 用户ID
+     */
     @Excel(name = "用户ID")
     private Integer userId;
 
-    /** 用户电话 */
+
+    /**
+     * 用户电话
+     */
     @Excel(name = "用户电话")
-    private String userPhone;
+    private List<String> userPhone;
 
-    public void setNewsId(Integer newsId)
-    {
+    public void setNewsId(Integer newsId) {
         this.newsId = newsId;
     }
 
-    public Integer getNewsId()
-    {
+    public Integer getUserId() {
+        return userId;
+    }
+
+    public List<String> getUserPhone() {
+        return userPhone;
+    }
+
+    public void setUserPhone(List<String> userPhone) {
+        userPhone.add(String.valueOf(this.userPhone));
+    }
+
+    public Integer getNewsId() {
         return newsId;
     }
-    public void setNewsTitle(String newsTitle)
-    {
+
+    public void setNewsTitle(String newsTitle) {
         this.newsTitle = newsTitle;
     }
 
-    public String getNewsTitle()
-    {
+    public String getNewsTitle() {
         return newsTitle;
     }
-    public void setNewsContent(String newsContent)
-    {
+
+    public void setNewsContent(String newsContent) {
         this.newsContent = newsContent;
     }
 
-    public String getNewsContent()
-    {
+    public String getNewsContent() {
         return newsContent;
     }
-    public void setStatus(String status)
-    {
+
+    public void setStatus(String status) {
         this.status = status;
     }
 
-    public String getStatus()
-    {
+    public String getStatus() {
         return status;
     }
-    public void setPublisherId(Integer publisherId)
-    {
+
+    public void setPublisherId(Integer publisherId) {
         this.publisherId = publisherId;
     }
 
-    public Integer getPublisherId()
-    {
+    public Integer getPublisherId() {
         return publisherId;
     }
-    public void setPublisherBy(String publisherBy)
-    {
+
+    public void setPublisherBy(String publisherBy) {
         this.publisherBy = publisherBy;
     }
 
-    public String getPublisherBy()
-    {
+    public String getPublisherBy() {
         return publisherBy;
     }
-    public void setUserId(Integer userId)
-    {
-        this.userId = userId;
-    }
 
-    public Integer getUserId()
-    {
-        return userId;
-    }
-    public void setUserPhone(String userPhone)
-    {
-        this.userPhone = userPhone;
-    }
-
-    public String getUserPhone()
-    {
-        return userPhone;
+    public void setUserId(Integer userId) {
+        this.userId = userId;
     }
 
     @Override
     public String toString() {
-        return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE)
+        return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
                 .append("newsId", getNewsId())
                 .append("newsTitle", getNewsTitle())
                 .append("newsContent", getNewsContent())

+ 3 - 0
ruoyi-post/src/main/java/com/ruoyi/post/domain/PostUser.java

@@ -4,6 +4,9 @@ import com.ruoyi.common.annotation.Excel;
 import com.ruoyi.common.core.domain.BaseEntity;
 import org.apache.commons.lang3.builder.ToStringBuilder;
 import org.apache.commons.lang3.builder.ToStringStyle;
+
+import java.util.List;
+
 /**
  * @Author HuiANan
  * @Date 2023/1/19 19:19

+ 34 - 2
ruoyi-post/src/main/java/com/ruoyi/post/mapper/PostUserMapper.java

@@ -1,5 +1,6 @@
 package com.ruoyi.post.mapper;
 
+import com.ruoyi.common.core.domain.entity.SysUser;
 import com.ruoyi.post.domain.PostUser;
 
 import java.util.List;
@@ -9,8 +10,7 @@ import java.util.List;
  * @Date 2023/1/25 20:20
  * @Exegesis:
  */
-public interface PostUserMapper
-{
+public interface PostUserMapper {
     /**
      * 查询邮贝用户信息;
      *
@@ -58,4 +58,36 @@ public interface PostUserMapper
      * @return 结果
      */
     public int deletePostUserByUserIds(Long[] userIds);
+
+    /**
+     * 校验用户名称是否唯一
+     *
+     * @param userName 用户名称
+     * @return 结果
+     */
+    public SysUser checkUserNameUnique(String userName);
+
+    /**
+     * 校验手机号码是否唯一
+     *
+     * @param phonenumber 手机号码
+     * @return 结果
+     */
+    public PostUser checkPhoneUnique(String phonenumber);
+
+    /**
+     * 校验email是否唯一
+     *
+     * @param email 用户邮箱
+     * @return 结果
+     */
+    public PostUser checkEmailUnique(String email);
+
+    /**
+     * 通过用户电话查询用户
+     *
+     * @param userPhone 用户电话
+     * @return 用户对象信息
+     */
+    public SysUser selectUserByUserPhone(String userPhone);
 }

+ 16 - 0
ruoyi-post/src/main/java/com/ruoyi/post/service/IPostNewsService.java

@@ -74,4 +74,20 @@ public interface IPostNewsService
      * 查看详情
      */
     public String checkContent(PostNews postNews);
+
+    /**
+     *
+     * @param phoneList 手机号码列表
+     * @param isUpdateSupport
+     * @param operName
+     * @return
+     */
+    public String importPhone(List<PostNews> phoneList, Boolean isUpdateSupport, String operName,PostNews postNews);
+
+    /**
+     * 新增消息电话增加
+     * @param postNews
+     * @return
+     */
+    public int insertBulletin(PostNews postNews);
 }

+ 16 - 23
ruoyi-post/src/main/java/com/ruoyi/post/service/impl/PostBulletinServiceImpl.java

@@ -2,16 +2,24 @@ package com.ruoyi.post.service.impl;
 
 import com.ruoyi.common.constant.UserConstants;
 import com.ruoyi.common.core.domain.entity.SysUser;
+import com.ruoyi.common.exception.ServiceException;
 import com.ruoyi.common.utils.DateUtils;
+import com.ruoyi.common.utils.SecurityUtils;
 import com.ruoyi.common.utils.StringUtils;
+import com.ruoyi.common.utils.bean.BeanValidators;
 import com.ruoyi.post.domain.PostBulletin;
 import com.ruoyi.post.domain.PostNews;
+import com.ruoyi.post.domain.PostUser;
 import com.ruoyi.post.mapper.PostBulletinMapper;
 import com.ruoyi.post.mapper.PostNewsMapper;
+import com.ruoyi.post.mapper.PostUserMapper;
 import com.ruoyi.post.service.IPostBulletinService;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 
 import javax.annotation.Resource;
+import javax.validation.Validator;
 import java.util.List;
 
 
@@ -101,36 +109,21 @@ public class PostBulletinServiceImpl implements IPostBulletinService {
 
     @Override
     public String checkTitle(PostBulletin postBulletin) {
-       Long noticeId=StringUtils.isNull(postBulletin.getNoticeId()) ? -1l :postBulletin.getNoticeId();
-       PostBulletin info=postBulletinMapper.checkTitleUnique(postBulletin.getNoticeTitle());
-       if(StringUtils.isNotNull(info)&&info.getNoticeId().longValue()!=noticeId.longValue())
-        {
+        Long noticeId = StringUtils.isNull(postBulletin.getNoticeId()) ? -1l : postBulletin.getNoticeId();
+        PostBulletin info = postBulletinMapper.checkTitleUnique(postBulletin.getNoticeTitle());
+        if (StringUtils.isNotNull(info) && info.getNoticeId().longValue() != noticeId.longValue()) {
             return UserConstants.NOT_UNIQUE;
         }
-       return UserConstants.UNIQUE;
+        return UserConstants.UNIQUE;
     }
 
     @Override
     public String checkContent(PostBulletin postBulletin) {
-        Long noticeId=StringUtils.isNull(postBulletin.getNoticeId()) ? -1l :postBulletin.getNoticeId();
-        PostBulletin info=postBulletinMapper.checkContentUnique(postBulletin.getNoticeContent());
-        if(StringUtils.isNotNull(info)&&info.getNoticeId().longValue()!=noticeId.longValue())
-        {
+        Long noticeId = StringUtils.isNull(postBulletin.getNoticeId()) ? -1l : postBulletin.getNoticeId();
+        PostBulletin info = postBulletinMapper.checkContentUnique(postBulletin.getNoticeContent());
+        if (StringUtils.isNotNull(info) && info.getNoticeId().longValue() != noticeId.longValue()) {
             return UserConstants.NOT_UNIQUE;
         }
         return UserConstants.UNIQUE;
     }
-
-
-//    public String checkUserNameUnique(SysUser user)
-//    {
-//        Long userId = StringUtils.isNull(user.getUserId()) ? -1L : user.getUserId();
-//        SysUser info = userMapper.checkUserNameUnique(user.getUserName());
-//        if (StringUtils.isNotNull(info) && info.getUserId().longValue() != userId.longValue())
-//        {
-//            return UserConstants.NOT_UNIQUE;
-//        }
-//        return UserConstants.UNIQUE;
-//    }
-
-}
+}

+ 67 - 1
ruoyi-post/src/main/java/com/ruoyi/post/service/impl/PostNewsServiceImpl.java

@@ -2,15 +2,24 @@ package com.ruoyi.post.service.impl;
 
 
 import com.ruoyi.common.constant.UserConstants;
+import com.ruoyi.common.core.domain.entity.SysUser;
+import com.ruoyi.common.exception.ServiceException;
 import com.ruoyi.common.utils.DateUtils;
 import com.ruoyi.common.utils.StringUtils;
+import com.ruoyi.common.utils.bean.BeanValidators;
 import com.ruoyi.post.domain.PostNews;
 import com.ruoyi.post.domain.PostNews;
+import com.ruoyi.post.domain.PostUser;
 import com.ruoyi.post.mapper.PostNewsMapper;
+import com.ruoyi.post.mapper.PostUserMapper;
 import com.ruoyi.post.service.IPostNewsService;
+import com.sun.deploy.net.DownloadException;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 
 import javax.annotation.Resource;
+import javax.validation.Validator;
+import java.util.Collections;
 import java.util.List;
 
 /**
@@ -24,6 +33,11 @@ public class PostNewsServiceImpl implements IPostNewsService
     @Resource
     private PostNewsMapper postNewsMapper;
 
+    @Resource
+    private PostUserMapper postUserMapper;
+
+    @Resource
+    protected Validator validator;
     /**
      * 查询通知消息管理;
      *
@@ -124,4 +138,56 @@ public class PostNewsServiceImpl implements IPostNewsService
         }
         return UserConstants.UNIQUE;
     }
-}
+    /**
+     * 导入用户数据
+     *
+     * @param phoneList 手机号数据列表
+     * @param isUpdateSupport 是否更新支持,如果已存在,则进行更新数据
+     * @param operName 操作用户
+     * @return 结果
+     */
+    @Override
+    public String importPhone(List<PostNews> phoneList, Boolean isUpdateSupport, String operName,PostNews postNews) {
+        if (StringUtils.isNull(phoneList) || phoneList.size() == 0) {
+            throw new ServiceException("导入用户电话数据不能为空!");
+        }
+        int successNum = 0;
+        int failureNum = 0;
+        StringBuilder successMsg = new StringBuilder();
+        StringBuilder failureMsg = new StringBuilder();
+
+        for (PostNews phone : phoneList) {
+            try {
+                // 验证是否存在这个用户
+                SysUser u = postUserMapper.selectUserByUserPhone(String.valueOf(phone.getUserPhone()));
+                if (StringUtils.isNull(u)) {
+                    BeanValidators.validateWithException(validator, phone);
+                    postNews.setUserPhone(postNews.getUserPhone());
+                    this.insertBulletin(postNews);
+                    successNum++;
+                    successMsg.append("<br/>" + successNum + "、电话号 " + postNews.getUserPhone() + " 导入成功");
+                }}catch(Exception e)
+                {
+                    failureNum++;
+                    String msg = "<br/>" + failureNum + "、dian " +postNews.getUserPhone()+ " 导入失败:";
+                    failureMsg.append(msg + e.getMessage());
+                }
+            }
+
+            return successMsg.toString();
+        }
+    /**
+     * 新增保存用户电话信息
+     *
+     * @param postNews 用户信息
+     * @return 结果
+     */
+    @Override
+    @Transactional
+    public int insertBulletin(PostNews postNews)
+    {
+        // 新增用户电话信息
+        int rows = postNewsMapper.insertPostNews(postNews);
+        return rows;
+    }
+}

+ 50 - 9
ruoyi-post/src/main/resources/mapper/PostUserMapper.xml

@@ -18,16 +18,29 @@
         <result property="updateTime" column="update_time"/>
         <result property="remark" column="remark"/>
     </resultMap>
-    <sql id="selectPostUserVo"> select user_id, user_name, nick_name, email, phonenumber, sex, status, del_flag, create_by, create_time, update_by, update_time, remark from post_user </sql>
+    <sql id="selectPostUserVo"> select user_id,
+                                       user_name,
+                                       nick_name,
+                                       email,
+                                       phonenumber,
+                                       sex,
+                                       status,
+                                       del_flag,
+                                       create_by,
+                                       create_time,
+                                       update_by,
+                                       update_time,
+                                       remark
+                                from post_user </sql>
     <select id="selectPostUserList" parameterType="PostUser" resultMap="PostUserResult">
         <include refid="selectPostUserVo"/>
         <where>
-            <if test="userName != null and userName != ''"> and user_name like concat('%', #{userName}, '%')</if>
-            <if test="nickName != null and nickName != ''"> and nick_name like concat('%', #{nickName}, '%')</if>
-            <if test="email != null and email != ''"> and email = #{email}</if>
-            <if test="phonenumber != null and phonenumber != ''"> and phonenumber = #{phonenumber}</if>
-            <if test="sex != null and sex != ''"> and sex = #{sex}</if>
-            <if test="status != null and status != ''"> and status = #{status}</if>
+            <if test="userName != null and userName != ''">and user_name like concat('%', #{userName}, '%')</if>
+            <if test="nickName != null and nickName != ''">and nick_name like concat('%', #{nickName}, '%')</if>
+            <if test="email != null and email != ''">and email = #{email}</if>
+            <if test="phonenumber != null and phonenumber != ''">and phonenumber = #{phonenumber}</if>
+            <if test="sex != null and sex != ''">and sex = #{sex}</if>
+            <if test="status != null and status != ''">and status = #{status}</if>
         </where>
     </select>
     <select id="selectPostUserByUserId" parameterType="Long" resultMap="PostUserResult">
@@ -83,9 +96,37 @@
         </trim>
         where user_id = #{userId}
     </update>
-    <delete id="deletePostUserByUserId" parameterType="Long"> delete from post_user where user_id = #{userId} </delete>
+    <delete id="deletePostUserByUserId" parameterType="Long"> delete
+                                                              from post_user
+                                                              where user_id = #{userId} </delete>
     <delete id="deletePostUserByUserIds" parameterType="String">
         delete from post_user where user_id in
-        <foreach item="userId" collection="array" open="(" separator="," close=")"> #{userId} </foreach>
+        <foreach item="userId" collection="array" open="(" separator="," close=")">#{userId}</foreach>
     </delete>
+    
+    <select id="checkUserNameUnique" parameterType="String" resultMap="PostUserResult">
+        select user_id, user_name
+        from post_user
+        where user_name = #{userName}
+          and del_flag = '0' limit 1
+    </select>
+
+    <select id="checkPhoneUnique" parameterType="String" resultMap="PostUserResult">
+        select user_id, phonenumber
+        from post_user
+        where phonenumber = #{phonenumber}
+          and del_flag = '0' limit 1
+    </select>
+
+    <select id="checkEmailUnique" parameterType="String" resultMap="PostUserResult">
+        select user_id, email
+        from post_user
+        where email = #{email}
+          and del_flag = '0' limit 1
+    </select>
+
+    <select id="selectUserByUserPhone" parameterType="String" resultMap="PostUserResult">
+        <include refid="selectPostUserVo"/>
+        where phonenumber = #{phonenumber} and del_flag = '0'
+    </select>
 </mapper>