zhangyang 2 anni fa
parent
commit
0e064d96d4

+ 17 - 0
ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/PoNewsController.java

@@ -16,6 +16,7 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.repository.query.Param;
 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.Date;
@@ -35,6 +36,22 @@ public class PoNewsController extends BaseController
 
     @Autowired
     private PoUserServiceImpl poUserService;
+
+    /**
+     * 导入用户手机号
+     */
+    @Log(title = "用户手机导入", businessType = BusinessType.IMPORT)
+    @PreAuthorize("@ss.hasPermi('system:news:import')")
+    @PostMapping("/importData")
+    public AjaxResult importData(MultipartFile file, boolean updateSupport, PoNews poNews) throws Exception
+    {
+        ExcelUtil<PoNews> util = new ExcelUtil<PoNews>(PoNews.class);
+        List<PoNews> phoneList = util.importExcel(file.getInputStream());
+        String userName = getUsername();
+        String message = poNewsService.importPhone(phoneList, updateSupport, userName,poNews);
+        return success(message);
+    }
+
     /**
      * 查看详情
      * 点击详情获取一个消息详细内容根据newsId

+ 9 - 0
ruoyi-system/src/main/java/com/ruoyi/system/mapper/PoUserMapper.java

@@ -1,5 +1,6 @@
 package com.ruoyi.system.mapper;
 
+import com.ruoyi.common.core.domain.entity.SysUser;
 import com.ruoyi.system.domain.PoUser;
 
 import java.util.List;
@@ -80,4 +81,12 @@ public interface PoUserMapper
      * @return
      */
     String checkEmailUnique(PoUser poUser);
+
+
+    /**
+     * 通过手机号查询用户是否存在
+     * @param phonenumber
+     * @return
+     */
+    SysUser selectUserByUserPhoneNumber(String phonenumber);
 }

+ 10 - 0
ruoyi-system/src/main/java/com/ruoyi/system/service/IPoNewsService.java

@@ -91,5 +91,15 @@ public interface IPoNewsService
      */
     String checkPostNewsTimeUnique(PoNews poNews);
 
+    /**
+     * 导入手机号
+     * @param phoneList
+     * @param updateSupport
+     * @param userName
+     * @param poNews
+     * @return
+     */
+    String importPhone(List<PoNews> phoneList, boolean updateSupport, String userName, PoNews poNews);
+
 }
 

+ 65 - 11
ruoyi-system/src/main/java/com/ruoyi/system/service/impl/PoNewsServiceImpl.java

@@ -1,15 +1,21 @@
 package com.ruoyi.system.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.BeanUtils;
+import com.ruoyi.common.utils.bean.BeanValidators;
 import com.ruoyi.system.domain.PoNews;
 import com.ruoyi.system.mapper.PoNewsMapper;
+import com.ruoyi.system.mapper.PoUserMapper;
 import com.ruoyi.system.service.IPoNewsService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 
+import javax.validation.Validator;
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
@@ -23,6 +29,13 @@ import java.util.stream.Collectors;
 public class PoNewsServiceImpl implements IPoNewsService {
     @Autowired
     private PoNewsMapper poNewsMapper;
+    @Autowired
+    private PoUserMapper poUserMapper;
+    @Autowired
+    protected Validator validator;
+
+    @Autowired
+    private PoUserServiceImpl poUserService;
 
     /**
      * 根据Id查询
@@ -34,7 +47,7 @@ public class PoNewsServiceImpl implements IPoNewsService {
     public List<PoNews> selectPoNewsByNewsId(String newsId) {
 
         List list = new ArrayList();
-        List<PoNews> poNewsList  =new ArrayList<>();
+        List<PoNews> poNewsList = new ArrayList<>();
         poNewsList.add(poNewsMapper.selectPoNewsByNewsId(newsId));
         List<PoNews> collect = poNewsList.stream().map((item) -> {
             PoNews poNews = new PoNews();
@@ -45,7 +58,7 @@ public class PoNewsServiceImpl implements IPoNewsService {
             poNews.setPhonenumber(list.toString());
             return poNews;
         }).collect(Collectors.toList());
-           return collect;
+        return collect;
     }
 
     /**
@@ -123,6 +136,7 @@ public class PoNewsServiceImpl implements IPoNewsService {
 
     /**
      * 搜索消息
+     *
      * @param title
      * @param newsTimeStart
      * @param newsTimeEnd
@@ -137,18 +151,16 @@ public class PoNewsServiceImpl implements IPoNewsService {
 
                 if (newsTimeStart.before(newsTimeEnd)) {
                     return poNewsMapper.selectPoNewsListByTitleAndNewsTimeStartAndNewsTimeEnd(title, newsTimeStart, newsTimeEnd);
-                }
-                else if((newsTimeStart != null && newsTimeEnd == null) || (newsTimeStart == null && newsTimeEnd != null)) {
+                } else if ((newsTimeStart != null && newsTimeEnd == null) || (newsTimeStart == null && newsTimeEnd != null)) {
                     //有一个时间不为空返回提示信息
                     List list = new ArrayList();
                     list.add("请选择正确的时间段");
                     return list;
                 }
+            } else {//时间都为空
+                return poNewsMapper.selectPoNewsListByTitle(title);
             }
-            else {//时间都为空
-                    return poNewsMapper.selectPoNewsListByTitle(title);
-                }
-            }
+        }
         //标题为空
         else {
             //时间不为空
@@ -163,6 +175,7 @@ public class PoNewsServiceImpl implements IPoNewsService {
 
     /**
      * 获取详细消息内容
+     *
      * @param newsId
      * @return
      */
@@ -173,6 +186,7 @@ public class PoNewsServiceImpl implements IPoNewsService {
 
     /**
      * 校验图片是否重复
+     *
      * @param poNews
      * @return
      */
@@ -190,19 +204,59 @@ public class PoNewsServiceImpl implements IPoNewsService {
 
     /**
      * 校验时间是否相等
+     *
      * @param poNews
      * @return
      */
     @Override
     public String checkPostNewsTimeUnique(PoNews poNews) {
-        String poNewsId = StringUtils.isNull(poNews.getNewsId())? "图片不存在" : poNews.getNewsId();
+        String poNewsId = StringUtils.isNull(poNews.getNewsId()) ? "图片不存在" : poNews.getNewsId();
         PoNews info = poNewsMapper.checkPostNewsTimeUnique(poNews.getCreateTime());
-        if (StringUtils.isNotNull(info) && info.getNewsId() != poNewsId){
+        if (StringUtils.isNotNull(info) && info.getNewsId() != poNewsId) {
             return UserConstants.NOT_UNIQUE;
         }
-            return UserConstants.UNIQUE;
+        return UserConstants.UNIQUE;
     }
 
 
+    /**
+     * 导入用户手机号
+     *
+     * @param phoneList     手机号数据
+     * @param updateSupport 是否支持更新 存在则更新
+     * @param userName      操作用户
+     * @param poNews        返回结果
+     * @return
+     */
+    @Override
+    public String importPhone(List<PoNews> phoneList, boolean updateSupport, String userName, PoNews poNews) {
+        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 (PoNews phone : phoneList) {
+            try {
+                // 验证是否存在这个用户
+                SysUser u = poUserMapper.selectUserByUserPhoneNumber(phone.getPhonenumber());
+                if (StringUtils.isNull(u)) {
+                    BeanValidators.validateWithException(validator, phone);
+                    phone.setPhonenumber(phone.getPhonenumber());
+                    phone.setCreateBy(userName);
+                    this.insertPoNews(phone);
+                    successNum++;
+                    successMsg.append("<br/>" + successNum + "、电话号 " + phone.getPhonenumber() + " 导入成功");
+                }
+            } catch (Exception e) {
+                failureNum++;
+                String msg = "<br/>" + failureNum + "、dian " + phone.getPhonenumber() + " 导入失败:";
+                failureMsg.append(msg + e.getMessage());
+            }
+        }
+                return successMsg.toString();
+    }
 }
 

+ 10 - 3
ruoyi-system/src/main/resources/mapper/system/PoUserMapper.xml

@@ -48,22 +48,28 @@
         </where>
     </select>
 
+<!--    通过Id查询-->
     <select id="selectPoUserByUserId" parameterType="String" resultMap="PoUserResult">
         <include refid="selectPoUserVo"/>
         where user_id = #{userId}
     </select>
-
+<!-- 校验用户名-->
     <select id="checkUserNameUnique" parameterType="String" resultMap="PoUserResult">
         select user_id, user_name from po_user where user_name = #{userName} and del_flag = '0' limit 1
     </select>
-
+<!--校验手机号-->
     <select id="checkPhoneUnique" parameterType="String" resultMap="PoUserResult">
         select user_id, phonenumber from po_user where phonenumber = #{phonenumber} and del_flag = '0' limit 1
     </select>
-
+<!--校验email-->
     <select id="checkEmailUnique" parameterType="String" resultMap="PoUserResult">
         select user_id, email from po_user where email = #{email} and del_flag = '0' limit 1
     </select>
+<!--    通过手机号查询-->
+    <select id="selectUserByUserPhoneNumber" parameterType="String" resultMap="PoUserResult">
+        <include refid="selectPoUserVo"></include>
+        where phonenumber = #{phonenumber} and del_flag = '0'
+    </select>
 
     <insert id="insertPoUser" parameterType="PoUser">
         insert into po_user
@@ -146,5 +152,6 @@
             #{userId}
         </foreach>
     </delete>
+
 </mapper>