Browse Source

删除影像文件,非结构化文件等

chenyongxing 6 years ago
parent
commit
9119ab5e9f

+ 8 - 0
src/main/java/com/yihu/ehr/resolve/common/ResolveApi.java

@ -0,0 +1,8 @@
package com.yihu.ehr.resolve.common;
public class ResolveApi {
    public class Packages {
        public static final String DeletePackages = "/packages/delete";
    }
}

+ 92 - 0
src/main/java/com/yihu/ehr/resolve/controller/PackageEndPoint.java

@ -0,0 +1,92 @@
package com.yihu.ehr.resolve.controller;
import com.yihu.ehr.constants.ApiVersion;
import com.yihu.ehr.controller.EnvelopRestEndPoint;
import com.yihu.ehr.elasticsearch.ElasticSearchUtil;
import com.yihu.ehr.fastdfs.FastDFSUtil;
import com.yihu.ehr.resolve.common.ResolveApi;
import com.yihu.ehr.resolve.service.profile.PackageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping(ApiVersion.Version1_0)
@Api(value = "PackageEndPoint", description = "档案包相关操作(主要用于清理脏数据),此操作会涉及到fastdfs,hbase同步删除", tags = {"档案相关服务"})
public class PackageEndPoint extends EnvelopRestEndPoint {
    @Autowired
    private FastDFSUtil fastDFSUtil;
    @Autowired
    private ElasticSearchUtil elasticSearchUtil;
    @Autowired
    private PackageService packageService;
    private static final String INDEX = "json_archives";
    private static final String TYPE = "info";
    @RequestMapping(value = ResolveApi.Packages.DeletePackages, method = RequestMethod.DELETE)
    @ApiOperation(value = "批量删除非结构化/影像档案包", notes = "批量删除非结构化/影像档案包,[结构化数据档案,最好手动删除,涉及到质控,太多东西了]")
    public long deletePackages(
            @ApiParam(name = "packType", value = "2:非结构化档案 ||  3:影像档案", required = true )
            @RequestParam(value = "packType",required = true) String packType,
            @ApiParam(name = "startDate", value = "接收时间(eg:2017-01-01 00:00:00)", required = false )
            @RequestParam(value = "startDate",required = false) String startDate,
            @ApiParam(name = "endDate", value = "接收时间(eg:2017-01-01 00:00:00)", required = false )
            @RequestParam(value = "endDate",required = false) String endDate,
            @ApiParam(name = "orgCode", value = "机构编码", required = false )
            @RequestParam(value = "orgCode",required = false) String orgCode,
            @ApiParam(name = "eventNo", value = "事件号", required = false )
            @RequestParam(value = "eventNo",required = false) String eventNo) throws Exception {
        String filters = "pack_type="+packType+";";
        if(StringUtils.isNotBlank(startDate)){
            filters+="receive_date>="+startDate+";";
        }
        if(StringUtils.isNotBlank(endDate)){
            filters+="receive_date<="+endDate+";";
        }
        if(StringUtils.isNotBlank(orgCode)){
            filters += "org_code="+orgCode+";";
        }
        if(StringUtils.isNotBlank(eventNo)){
            filters += "event_no="+eventNo+";";
        }
        long count = elasticSearchUtil.count(INDEX, TYPE, filters);
        Page<Map<String, Object>> result = elasticSearchUtil.page(INDEX, TYPE, filters, 1, 10000);
        while (CollectionUtils.isNotEmpty(result.getContent())) {
            List<String> idList = new ArrayList<>();
            for (Map<String, Object> temp : result) {
                String[] tokens =  String.valueOf(temp.get("remote_path")).split(":");
                String profileId = String.valueOf(temp.get("profile_id"));
                if(StringUtils.isNotBlank(profileId) && !profileId.equals("null")){
                    packageService.deleteFilePack(profileId);
                }
                //删除fastdfs档案包
                fastDFSUtil.delete(tokens[0], tokens[1]);
                idList.add(String.valueOf(temp.get("_id")));
            }
            //删除es记录
            if (idList.size() > 0) {
                String[] _id = new String[idList.size()];
                elasticSearchUtil.bulkDelete(INDEX, TYPE, idList.toArray(_id));
            }
            result = elasticSearchUtil.page(INDEX, TYPE, filters, 1, 10000);
        }
        return count;
    }
}

+ 73 - 0
src/main/java/com/yihu/ehr/resolve/service/profile/PackageService.java

@ -0,0 +1,73 @@
package com.yihu.ehr.resolve.service.profile;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yihu.ehr.fastdfs.FastDFSUtil;
import com.yihu.ehr.hbase.HBaseDao;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.csource.common.MyException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.util.List;
import java.util.Map;
@Service
public class PackageService {
    @Autowired
    private HBaseDao hbaseDao;
    @Autowired
    protected ObjectMapper objectMapper;
    @Autowired
    private FastDFSUtil fastDFSUtil;
    private static final String MasterTable = "HealthFile";
    private static final String SubTable = "HealthFileSub";
    /**
     *
     * 通过 主表的rowkey,删除hbase主细表数据,以及删除已经入库到fast dfs的文件
     * 该方法目前只对非结构化和影像档案有作用
     * @param profileId
     * @throws IOException
     */
    public void deleteFilePack(String profileId) throws IOException, MyException {
        Map<String, Object> obj = hbaseDao.getResultMap(MasterTable,profileId);
        if (obj != null) {
            if (obj.get("file_list") != null) {
                String file_list = (String) obj.get("file_list");
                List<Map<String, String>> list = objectMapper.readValue(file_list, List.class);
                //删除 fast dfs 中非结构化文档
                if(CollectionUtils.isNotEmpty(list)){
                    for(Map<String,String> map:list){
                        String url = map.get("url");
                        if(StringUtils.isNotBlank(url)){
                            int index = url.indexOf("/");
                            String groupName = url.substring(0,index);
                            String fileName = url.substring(index+1,url.length()-1);
                            //删除fastdfs档案包
                            fastDFSUtil.delete(groupName, fileName);
                        }
                    }
                }
            }
            if (obj.get("sub_rowkeys") != null) {
                String sub_rowkeys = (String) obj.get("sub_rowkeys");
                List<String> list = objectMapper.readValue(sub_rowkeys, List.class);
                //删除细表数据
                if(CollectionUtils.isNotEmpty(list)){
                    for(String subRow:list){
                        //删除主表档案
                        hbaseDao.delete(MasterTable,SubTable);
                    }
                }
            }
            //删除主表档案
            hbaseDao.delete(MasterTable,profileId);
        }
    }
}