admin
2022-01-28 cd7767932dddeaf6d9c73a83d4a9b38f0341b77f
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package com.yeshi.buwan.service.imp;
 
import java.util.List;
import java.util.Map;
 
import javax.annotation.Resource;
 
import com.yeshi.buwan.domain.system.DetailSystem;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate4.HibernateCallback;
import org.springframework.stereotype.Service;
 
import com.yeshi.buwan.dao.ImageInfoDao;
import com.yeshi.buwan.dao.WeiXinImageDao;
import com.yeshi.buwan.dao.WeiXinInfoDao;
import com.yeshi.buwan.domain.ImageInfo;
import com.yeshi.buwan.domain.WeiXinImage;
import com.yeshi.buwan.domain.WeiXinInfo;
import com.yeshi.buwan.util.Constant;
import com.yeshi.buwan.util.LogUtil;
import com.yeshi.buwan.util.StringUtil;
 
@Service
public class WeiXinService {
    @Resource
    private WeiXinInfoDao weiXinInfoDao;
    @Resource
    private WeiXinImageDao weiXinImageDao;
    @Resource
    private ImageService imageService;
    @Resource
    private ImageInfoDao imageInfoDao;
 
 
    public void addWeiXinImage(WeiXinInfo info, String lastPic) {
        if (info.getType().equalsIgnoreCase("1")) {// 个人号
            WeiXinImage img = new WeiXinImage();
            img.setCreatetime(System.currentTimeMillis() + "");
            img.setImageInfo(new ImageInfo(1 + ""));
            img.setWeiXinInfo(info);
            weiXinImageDao.create(img);
            img = new WeiXinImage();
            img.setCreatetime(System.currentTimeMillis() + "");
            img.setImageInfo(new ImageInfo(2 + ""));
            img.setWeiXinInfo(info);
            weiXinImageDao.create(img);
            img = new WeiXinImage();
            img.setCreatetime(System.currentTimeMillis() + "");
            img.setImageInfo(new ImageInfo(3 + ""));
            img.setWeiXinInfo(info);
            weiXinImageDao.create(img);
            img = new WeiXinImage();
            img.setCreatetime(System.currentTimeMillis() + "");
            img.setImageInfo(new ImageInfo(4 + ""));
            img.setWeiXinInfo(info);
            weiXinImageDao.create(img);
        } else {// 公众号
            ImageInfo lastInfo = null;
 
            List<ImageInfo> infoList = imageInfoDao.list("from ImageInfo info where info.url=?",
                    new String[]{lastPic});
            if (infoList.size() > 0)
                lastInfo = infoList.get(0);
            else {
                lastInfo = new ImageInfo();
                lastInfo.setCreatetime(System.currentTimeMillis() + "");
                lastInfo.setUrl(lastPic);
                lastInfo.setId(imageInfoDao.save(lastInfo).toString());
            }
            WeiXinImage img = new WeiXinImage();
            img.setCreatetime(System.currentTimeMillis() + "");
            img.setImageInfo(new ImageInfo(5 + ""));
            img.setWeiXinInfo(info);
            weiXinImageDao.create(img);
            img = new WeiXinImage();
            img.setCreatetime(System.currentTimeMillis() + "");
            img.setImageInfo(new ImageInfo(6 + ""));
            img.setWeiXinInfo(info);
            weiXinImageDao.create(img);
            img = new WeiXinImage();
            img.setCreatetime(System.currentTimeMillis() + "");
            img.setImageInfo(lastInfo);
            img.setWeiXinInfo(info);
            weiXinImageDao.create(img);
        }
    }
 
    public WeiXinInfo addWeiXinInfo(WeiXinInfo info) {
        weiXinInfoDao.create(info);
        return weiXinInfoDao.list("from WeiXinInfo w where w.createtime=" + info.getCreatetime()).get(0);
    }
 
    public List<WeiXinInfo> getWeiXinList() {
        return weiXinInfoDao.list("from WeiXinInfo");
    }
 
    public List<WeiXinInfo> getWeiXinList(String key, int pageIndex) {
        return weiXinInfoDao.list("from WeiXinInfo w where w.nickName like ?", (pageIndex - 1) * Constant.pageCount,
                Constant.pageCount, new String[]{"%" + key + "%"});
    }
 
    public long getWeiXinListCount(String key) {
        return weiXinInfoDao.getCount("select count(*)  from WeiXinInfo w where w.nickName like ?",
                new String[]{"%" + key + "%"});
    }
 
    public WeiXinInfo getWeiXinInfo(String id) {
        return weiXinInfoDao.find(WeiXinInfo.class, id);
    }
 
    // 更新微信
    public void updateWeiXinInfo(WeiXinInfo info) {
        weiXinInfoDao.update(info);
    }
 
    public void updateWeiXinInfo(WeiXinInfo info, String lastPic) {
        if (!StringUtil.isNullOrEmpty(lastPic) && info.getType().equalsIgnoreCase("2")) {// 公众号
            ImageInfo imgInfo = null;
            imgInfo = imageService.getImageByUrl(lastPic);
            if (imgInfo == null) {
                imgInfo = new ImageInfo();
                imgInfo.setCreatetime(System.currentTimeMillis() + "");
                imgInfo.setUrl(lastPic);
                imgInfo.setId(imageService.saveImage(imgInfo).toString());
            }
 
            List<WeiXinImage> wxiList = weiXinImageDao
                    .list("from WeiXinImage wi where wi.weiXinInfo.id=" + info.getId());
            if (wxiList != null && wxiList.size() > 0)
                weiXinImageDao.delete(wxiList.get(2));
            WeiXinImage wxi = new WeiXinImage();
            wxi.setCreatetime(System.currentTimeMillis() + "");
            wxi.setImageInfo(imgInfo);
            wxi.setWeiXinInfo(info);
            weiXinImageDao.create(wxi);
        }
        weiXinInfoDao.update(info);
    }
 
    public void deleteWeiXinInfo(final WeiXinInfo info) {
        weiXinInfoDao.delete(info);
 
        weiXinInfoDao.excute(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException {
                try {
                    session.getTransaction().begin();
                    session.delete(info);
                    session.createQuery("delete from WeiXinImage w where w.weiXinInfo.id=" + info.getId())
                            .executeUpdate();
                    session.flush();
                    session.getTransaction().commit();
                } catch (Exception e) {
                    e.printStackTrace();
                    session.getTransaction().rollback();
                }
                return null;
            }
        });
 
    }
 
 
    @Resource
    private DetailSystemConfigService configService;
 
    private WeiXinInfo getWeiXin(List<WeiXinInfo> list) {
        OtherService service = new OtherService();
        Map<String, String> map = configService.getConfigAsMap(new DetailSystem(44 + ""), 1);
        String time = map.get("weixin_change_time");
        int miniute = (int) ((System.currentTimeMillis() / (1000 * 60L)) % (24 * 60));
        int size = list.size();
        int position = miniute % (size * Integer.parseInt(time)) / Integer.parseInt(time);
        LogUtil.i(position + "");
        if (position >= size)
            position = size - 1;
        if (position < 0)
            position = 0;
 
        list.get(position).setImageList(imageInfoDao
                .list("select w.imageInfo from WeiXinImage w where w.weiXinInfo.id=" + list.get(position).getId()));
 
        return list.get(position);
    }
 
    public List<ImageInfo> getWeiXinImageList(String id) {
        return imageInfoDao.list("select wx.imageInfo from WeiXinImage wx where wx.weiXinInfo.id=" + id);
    }
 
    public WeiXinInfo getWeiXinInfoById(String id) {
        List<WeiXinInfo> list = null;
        if (StringUtil.isNullOrEmpty(id)) {
            list = weiXinInfoDao.list("from WeiXinInfo w where w.weight>0 order by w.id");
            return getWeiXin(list);
        } else {
            WeiXinInfo info = weiXinInfoDao.find(WeiXinInfo.class, id);
            info.setImageList(getWeiXinImageList(id));
            return info;
        }
    }
 
    // 获取需要推广的微信号列表
    public List<WeiXinInfo> getRandomWeiXinList() {
        return weiXinInfoDao.list("from WeiXinInfo w where w.weight=-1 order by w.id");
    }
}