liaoyongfei vor 4 Jahren
Ursprung
Commit
fa5047d6bd

+ 30 - 23
mpwechatApp/src/main/java/com/liangjian11/wx/mp/controller/WxMaterialController.java

@@ -2,10 +2,7 @@ package com.liangjian11.wx.mp.controller;
 
 import com.liangjian11.wx.mp.modle.Material;
 import com.liangjian11.wx.mp.service.MediaMaterialService;
-import com.liangjian11.wx.mp.utils.FileUtil;
-import com.liangjian11.wx.mp.utils.FiltersData;
-import com.liangjian11.wx.mp.utils.ResultInfo;
-import com.liangjian11.wx.mp.utils.ResultUtil;
+import com.liangjian11.wx.mp.utils.*;
 import me.chanjar.weixin.common.api.WxConsts;
 import me.chanjar.weixin.common.error.WxErrorException;
 import me.chanjar.weixin.mp.api.WxMpService;
@@ -29,37 +26,46 @@ import java.util.Map;
 @RequestMapping("/wx/material/{appid}")
 public class WxMaterialController {
 
-    private Map<Integer,String> mediaTypeMap;
+
     private final MediaMaterialService mediaMaterialService;
     private final WxMpService wxMpService;
 
-
     @Autowired
     public WxMaterialController(MediaMaterialService mediaMaterialService,WxMpService wxMpService){
         this.mediaMaterialService=mediaMaterialService;
         this.wxMpService=wxMpService;
-        mediaTypeMap=new HashMap<>();
-        mediaTypeMap.put(1,"image");
-        mediaTypeMap.put(2,"voice");
-        mediaTypeMap.put(3,"video");
-        mediaTypeMap.put(4,"thumb");
     }
 
 
     @CrossOrigin
     @PostMapping("/getPageList")
-    public ResultInfo getMaterialListOfPage(@RequestBody FiltersData filtersData) {
+    public ResultInfo getMaterialListOfPage(@PathVariable String appid,@RequestBody FiltersData filtersData) throws WxErrorException {
         String filterSql = " Where 1=1";
-        Integer start = 0;
-        Integer end = 20;
         if (filtersData != null && !StringUtils.isEmpty(filtersData.getFilterString())) {
             filterSql = filtersData.getFilterString();
         }
-        if (filtersData.getPageIndex() > 0) {
-            start = (filtersData.getPageIndex() - 1) * filtersData.getPageSize();
+        if (filtersData.getPageIndex() < 1) {
+            return ResultUtil.createFail(new Exception("缺少pageIndex!"));
         }
-        if (filtersData.getPageSize() > 0) {
-            end = filtersData.getPageIndex() * filtersData.getPageSize() + 1;
+        if (filtersData.getPageSize() < 1) {
+            return ResultUtil.createFail(new Exception("缺少pageSize!"));
+        }
+        Integer start  = (filtersData.getPageIndex() - 1) * filtersData.getPageSize();;
+        Integer end  = filtersData.getPageIndex() * filtersData.getPageSize() + 1;
+
+        boolean hasType=false;
+        int type=0;
+        if(filtersData.getPageIndex()==1 && filtersData.getFilters()!=null){
+            for(FiltersUtils filtersUtils:filtersData.getFilters()){
+                if(filtersUtils.getField().equals("type")){
+                    hasType=true;
+                    type=Integer.valueOf(filtersUtils.getValue()).intValue();
+                }
+            }
+            if(!hasType){
+                return ResultUtil.createFail(new Exception("缺少素材类型!"));
+            }
+            mediaMaterialService.refreshMaterialFileBatch(appid,type);
         }
         List<Material> materialList = mediaMaterialService.getMaterialListOfPage(filterSql, start, end);
         int totalCount = 0;
@@ -75,20 +81,21 @@ public class WxMaterialController {
     @CrossOrigin
     @PostMapping("/addMaterial")
     public ResultInfo addMaterial(@PathVariable String appid, @RequestPart("file") MultipartFile multipartFile,
-                                  @RequestParam("mediaType") Integer mediaType , @RequestParam("title") String title) throws IOException, WxErrorException {
+                                  @RequestParam("mediaType") Short mediaType , @RequestParam(value = "title",required=false)
+                                          String title) throws IOException, WxErrorException {
         WxMpMaterial wxMaterial = new WxMpMaterial();
         wxMaterial.setFile(FileUtil.multipartFileToFile(multipartFile));
         wxMaterial.setName(StringUtils.isEmpty(multipartFile.getOriginalFilename())?multipartFile.getName():multipartFile.getOriginalFilename());
-        if (WxConsts.MediaFileType.VIDEO.equals(mediaTypeMap.get(mediaType))) {
+        if (WxConsts.MediaFileType.VIDEO.equals(mediaMaterialService.getMediaTypeMap().get(mediaType))) {
             wxMaterial.setVideoTitle(title);
             wxMaterial.setVideoIntroduction(null);
         }
-        WxMpMaterialUploadResult wxResult= this.wxMpService.switchoverTo(appid).getMaterialService().materialFileUpload(mediaTypeMap.get(mediaType), wxMaterial);
+        WxMpMaterialUploadResult wxResult= this.wxMpService.switchoverTo(appid).getMaterialService().materialFileUpload(mediaMaterialService.getMediaTypeMap().get(mediaType), wxMaterial);
         if(wxResult.getErrCode()!=null&&wxResult.getErrCode()>200){
             return new ResultInfo(0,wxResult.getErrCode(),wxResult.getErrMsg());
         }
         Material material=new Material();
-        material.setType(mediaType);
+        material.setMaterialType(mediaType);
         material.setMediaId(wxResult.getMediaId());
         String fileName= StringUtils.isEmpty(multipartFile.getOriginalFilename())?multipartFile.getName():multipartFile.getOriginalFilename();
         String[] names=fileName.split("\\.");
@@ -98,7 +105,7 @@ public class WxMaterialController {
         material.setCreateTime(new Date());
         material.setAppId(appid);
         material.setUrl(wxResult.getUrl());
-        mediaMaterialService.insert(material);
+        mediaMaterialService.insertItem(material);
         return ResultUtil.createSuccess(200,null);
     }
 

+ 42 - 0
mpwechatApp/src/main/java/com/liangjian11/wx/mp/mapper/MaterialMapper.java

@@ -2,6 +2,8 @@ package com.liangjian11.wx.mp.mapper;
 
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 import com.liangjian11.wx.mp.modle.Material;
+import org.apache.ibatis.annotations.Delete;
+import org.apache.ibatis.annotations.Insert;
 import org.apache.ibatis.annotations.Param;
 import org.apache.ibatis.annotations.Select;
 import java.util.List;
@@ -9,10 +11,50 @@ import java.util.List;
 
 public interface MaterialMapper extends BaseMapper<Material> {
 
+
+    @Insert("insert into material(material_type,title,media_id,file_name,file_size,content_type,create_time,edit_time,url,app_id)" +
+        "values (#{m.materialType} ,#{m.title} ,#{m.mediaId} ,#{m.fileName} ,#{m.fileSize} ,#{m.contentType} ,#{m.createTime} " +
+        ",#{m.editTime} ,#{m.url} ,#{m.appId})")
+    Integer insertItem(@Param("m") Material material);
+    /**
+     * 查询符合条件的数量
+     * @param sqlStr 拼装sql
+     * @return 数量
+     */
     @Select("select count(1) from material ${sqlStr}")
     Integer getMaterialCount(@Param("sqlStr")String sqlStr);
 
+    /**
+     * 分页查询
+     * @param sqlStr 拼装sql
+     * @param index 偏移量
+     * @param size 页容量
+     * @return 分页数据
+     */
     @Select("SELECT m.* from material m  ${sqlStr} ORDER BY m.create_time DESC offset #{index} rows fetch next #{size} rows only")
     List<Material> getMaterialPageList(@Param("sqlStr")String sqlStr, @Param("index")Integer index, @Param("size")Integer size);
 
+    /**
+     * 按appid 和素材类型,查询素材
+     * @param materialType 素材类型
+     * @param appid appid
+     * @param index 偏移量
+     * @param size 页容量
+     * @return 素材集合
+     */
+    @Select("SELECT m.* from material m where m.material_type=#{materialType} and m.app_id=#{appid} order by m.create_time desc offset #{index} rows " +
+        "fetch next #{size} rows only")
+    List<Material> getMaterialList(@Param("materialType") Integer materialType,@Param("appid") String appid,@Param("index")Integer index, @Param("size")Integer size);
+
+    /**
+     * 按appid 和素材类型,查询素材数量
+     * @param materialType 素材类型
+     * @param appid appid
+     * @return 数量
+     */
+    @Select("select count(1) from material m where m.material_type=#{materialType} and m.app_id=#{appid} ")
+    int getMaterialCountSecond(@Param("materialType") Integer materialType,@Param("appid") String appid);
+
+    @Delete("delete from material where app_id=#{appid} and material_type=#{materialType}")
+    int deleteMaterial(@Param("appid") String appid,@Param("materialType")Integer materialType);
 }

+ 0 - 1
mpwechatApp/src/main/java/com/liangjian11/wx/mp/modle/Fans.java

@@ -5,7 +5,6 @@ import com.baomidou.mybatisplus.annotation.IdType;
 import com.baomidou.mybatisplus.annotation.TableField;
 import com.baomidou.mybatisplus.annotation.TableId;
 import com.baomidou.mybatisplus.annotation.TableName;
-
 import java.io.Serializable;
 import java.util.Date;
 

+ 27 - 6
mpwechatApp/src/main/java/com/liangjian11/wx/mp/modle/Material.java

@@ -1,10 +1,11 @@
 package com.liangjian11.wx.mp.modle;
 
-import com.baomidou.mybatisplus.annotations.TableField;
-import com.baomidou.mybatisplus.annotations.TableId;
-import com.baomidou.mybatisplus.annotations.TableName;
-import com.baomidou.mybatisplus.enums.IdType;
+import com.baomidou.mybatisplus.annotation.IdType;
+import com.baomidou.mybatisplus.annotation.TableField;
+import com.baomidou.mybatisplus.annotation.TableId;
+import com.baomidou.mybatisplus.annotation.TableName;
 import lombok.Data;
+import org.springframework.format.annotation.DateTimeFormat;
 
 import java.io.Serializable;
 import java.util.Date;
@@ -13,11 +14,13 @@ import java.util.Date;
 @Data
 public class Material implements Serializable {
 
+    private static final long serialVersionUID = 1L;
+
     @TableId(value="id",type = IdType.AUTO)
     private Integer id;
 
-    @TableField("type")
-    private int type;
+    @TableField("material_type")
+    private short materialType;
 
     @TableField("title")
     private String title;
@@ -35,11 +38,29 @@ public class Material implements Serializable {
     private String contentType;
 
     @TableField("create_time")
+    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
     private Date createTime;
 
     @TableField("edit_time")
+    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
     private Date editTime ;
 
+    public Date getCreateTime() {
+        return createTime;
+    }
+
+    public void setCreateTime(Date createTime) {
+        this.createTime = createTime;
+    }
+
+    public Date getEditTime() {
+        return editTime;
+    }
+
+    public void setEditTime(Date editTime) {
+        this.editTime = editTime;
+    }
+
     @TableField("url")
     private String url;
 

+ 11 - 0
mpwechatApp/src/main/java/com/liangjian11/wx/mp/service/MediaMaterialService.java

@@ -1,12 +1,19 @@
 package com.liangjian11.wx.mp.service;
 
 import com.liangjian11.wx.mp.modle.Material;
+import me.chanjar.weixin.common.error.WxErrorException;
+import me.chanjar.weixin.mp.bean.material.WxMpMaterialCountResult;
+import me.chanjar.weixin.mp.bean.material.WxMpMaterialFileBatchGetResult;
 
 import java.util.List;
+import java.util.Map;
 
 
 public interface MediaMaterialService {
 
+    Integer insertItem(Material material);
+
+    Map<Integer, String> getMediaTypeMap();
     /**
      * 分页查询
      * @param filterSql
@@ -30,4 +37,8 @@ public interface MediaMaterialService {
      */
     Integer insert(Material material);
 
+    int getMaterialCount(Integer type,String appid) ;
+
+    void refreshMaterialFileBatch(String appid,int type) throws WxErrorException;
+
 }

+ 84 - 6
mpwechatApp/src/main/java/com/liangjian11/wx/mp/service/impl/MediaMaterialServiceImpl.java

@@ -1,27 +1,54 @@
 package com.liangjian11.wx.mp.service.impl;
 
-import com.baomidou.mybatisplus.service.impl.ServiceImpl;
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.liangjian11.wx.mp.mapper.FansMapper;
 import com.liangjian11.wx.mp.mapper.MaterialMapper;
+import com.liangjian11.wx.mp.modle.Fans;
 import com.liangjian11.wx.mp.modle.Material;
-import com.liangjian11.wx.mp.modle.Message;
 import com.liangjian11.wx.mp.service.MediaMaterialService;
 import lombok.extern.slf4j.Slf4j;
+import me.chanjar.weixin.common.error.WxErrorException;
 import me.chanjar.weixin.mp.api.WxMpService;
+import me.chanjar.weixin.mp.bean.material.WxMpMaterialCountResult;
+import me.chanjar.weixin.mp.bean.material.WxMpMaterialFileBatchGetResult;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
+import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
 
 @Service
 @Slf4j
-public class MediaMaterialServiceImpl implements MediaMaterialService {
+public class MediaMaterialServiceImpl extends ServiceImpl<MaterialMapper, Material> implements MediaMaterialService {
+
 
-    @Autowired
     private WxMpService wxMpService;
+    private MaterialMapper materialMapper;
+    private Map<Integer,String> mediaTypeMap;
 
     @Autowired
-    private MaterialMapper materialMapper;
+    public MediaMaterialServiceImpl(WxMpService wxMpService,MaterialMapper materialMapper){
+        this.wxMpService=wxMpService;
+        this.materialMapper=materialMapper;
+        mediaTypeMap=new HashMap<>();
+        mediaTypeMap.put(1,"image");
+        mediaTypeMap.put(2,"voice");
+        mediaTypeMap.put(3,"video");
+        mediaTypeMap.put(4,"thumb");
+    }
+
+    @Override
+    public Integer insertItem(Material material) {
+        return this.materialMapper.insertItem(material);
+    }
+
+    @Override
+    public Map<Integer, String> getMediaTypeMap() {
+        return mediaTypeMap;
+    }
 
     @Override
     public List<Material> getMaterialListOfPage(String filterSql,int start,int end){
@@ -37,8 +64,59 @@ public class MediaMaterialServiceImpl implements MediaMaterialService {
 
     @Override
     public Integer insert(Material material) {
-        log.info("------------"+material.toString());
         return materialMapper.insert(material);
     }
 
+    @Override
+    public int getMaterialCount(Integer type,String appid)  {
+         return materialMapper.getMaterialCountSecond(type,appid);
+    }
+
+    @Override
+    public void refreshMaterialFileBatch(String appid,int type) throws WxErrorException {
+        /*WxMpMaterialCountResult materialCountResult=wxMpService.switchoverTo(appid).getMaterialService().materialCount();
+        int typeCount=0;
+        if(type==1){
+            typeCount=materialCountResult.getImageCount();
+        }else if(type==2){
+            typeCount=materialCountResult.getVoiceCount();
+        }else if(type==3){
+            typeCount=materialCountResult.getVideoCount();
+        }*/
+
+        int count=20;
+        WxMpMaterialFileBatchGetResult materialFileBatchGetResult=wxMpService.switchoverTo(appid).getMaterialService()
+            .materialFileBatchGet(mediaTypeMap.get(type),0,count);
+        //List<Material>materialList=new ArrayList<>(count);
+        this.loopInsert(materialFileBatchGetResult);
+        //剩余总数量
+        int typeCount=materialFileBatchGetResult.getTotalCount()-materialFileBatchGetResult.getItemCount();
+        count=typeCount>20?20:typeCount;
+        for(int offset=materialFileBatchGetResult.getItemCount();count<=0;){
+            WxMpMaterialFileBatchGetResult materialFileBatch=wxMpService.switchoverTo(appid).getMaterialService()
+                .materialFileBatchGet(mediaTypeMap.get(type),offset,count);
+            this.loopInsert(materialFileBatch);
+            offset+=count;
+            typeCount-=count;
+            count=typeCount>20?20:typeCount;
+        }
+    }
+
+    /**
+     * 批量插入
+     * @param materialFileBatchGetResult 微信素材查询结果集
+     */
+    private void loopInsert(WxMpMaterialFileBatchGetResult materialFileBatchGetResult){
+        for(int i=0;i<materialFileBatchGetResult.getItemCount();i++){
+            Material material=new Material();
+            material.setFileName(materialFileBatchGetResult.getItems().get(i).getName());
+            material.setMediaId(materialFileBatchGetResult.getItems().get(i).getMediaId());
+            material.setUrl(materialFileBatchGetResult.getItems().get(i).getUrl());
+            material.setCreateTime(materialFileBatchGetResult.getItems().get(i).getUpdateTime());
+            materialMapper.insert(material);
+        }
+    }
+
+
+
 }