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");
|
}
|
}
|