yujian
2019-03-14 055f90601735a59395ebe50fdbccc9a702cd50b4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package com.yeshi.fanli.service.impl;
 
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Serializable;
import java.util.List;
 
import javax.annotation.Resource;
 
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import com.yeshi.fanli.dao.MaskKeyDao;
import com.yeshi.fanli.entity.bus.recommend.MaskKey;
import com.yeshi.fanli.service.MaskKeyService;
import com.yeshi.fanli.util.Constant;
 
@Service
public class MaskKeyServiceImpl implements MaskKeyService {
 
    @Resource
    private MaskKeyDao dao;
 
    @Resource
    private MaskKeyService maskKeyService;
 
    public void addMaskKeys(String path) {
        BufferedReader bufferedReader = null;
        try {
            File file = new File(path);
            bufferedReader = new BufferedReader(new FileReader(file));
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                MaskKey mk = new MaskKey();
                mk.setContent(line);
                dao.create(mk);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
 
    }
 
    public List<MaskKey> getMaskKeys(String key, int page) {
        int start = (page - 1) * Constant.PAGE_SIZE;
        String hql = "from MaskKey mk where mk.content like ? order by mk.id desc ";
        return dao.list(hql, start, Constant.PAGE_SIZE, new Serializable[] { "%" + key + "%" });
    }
 
    public int getCount(String key) {
        return (int) dao.getCount("select count(*) from MaskKey mk where mk.content like ? ",
                new Serializable[] { "%" + key + "%" });
    }
 
    @Transactional
    public long addMaskKey(String key) {
        return (Long) dao.save(new MaskKey(key));
    }
 
    @Transactional
    public void delete(long id) {
        dao.delete(new MaskKey(id));
    }
 
    @Cacheable(value = "maskKeyCache")
    public List<MaskKey> getAll() {
 
        return dao.list("from MaskKey");
    }
 
    public String maskAction(String content) {
 
        List<MaskKey> maskKeys = maskKeyService.getAll();
        String xin = "";
        for (MaskKey maskKey : maskKeys) {
            xin = "";
            String key = maskKey.getContent();
            int length = key.length();
            for (int i = 0; i < length; i++) {
                xin = xin + "*";
            }
            content = content.replace(key, xin);
        }
        return content;
 
    }
 
}