package com.yeshi.buwan.service.imp.shop;
|
|
import java.io.Serializable;
|
import java.io.UnsupportedEncodingException;
|
import java.lang.reflect.Type;
|
import java.net.URLEncoder;
|
import java.util.ArrayList;
|
import java.util.List;
|
|
import javax.annotation.Resource;
|
|
import org.hibernate.HibernateException;
|
import org.hibernate.Session;
|
import org.springframework.cache.annotation.Cacheable;
|
import org.springframework.orm.hibernate4.HibernateCallback;
|
import org.springframework.stereotype.Service;
|
|
import com.google.gson.Gson;
|
import com.google.gson.GsonBuilder;
|
import com.google.gson.reflect.TypeToken;
|
import com.yeshi.buwan.dao.user.LoginUserDao;
|
import com.yeshi.buwan.dao.shop.ShopItemCollectDao;
|
import com.yeshi.buwan.dao.shop.ShopItemCommentDao;
|
import com.yeshi.buwan.domain.user.LoginUser;
|
import com.yeshi.buwan.domain.entity.ReturnResult;
|
import com.yeshi.buwan.domain.shop.ShopGoodsItem;
|
import com.yeshi.buwan.domain.shop.ShopItemCollect;
|
import com.yeshi.buwan.domain.shop.ShopItemComment;
|
import com.yeshi.buwan.domain.shop.TaoBaoItem;
|
import com.yeshi.buwan.service.imp.UserService;
|
import com.yeshi.buwan.util.Constant;
|
import com.yeshi.buwan.util.HttpUtil;
|
import com.yeshi.buwan.util.StringUtil;
|
|
import net.sf.json.JSONArray;
|
import net.sf.json.JSONObject;
|
|
@Service
|
public class ShopService {
|
final static String HOST = "http://buwantb.yeshitv.com:8080/TaoBaoKe/";// 10.251.33.117
|
|
// private static final String HOST ="http://192.168.1.200:8088/TaoBaoKe/";
|
@Resource
|
private UserService userService;
|
@Resource
|
private LoginUserDao loginUserDao;
|
@Resource
|
private ShopItemCollectDao shopItemCollectDao;
|
@Resource
|
private ShopItemCommentDao shopItemCommentDao;
|
|
@Cacheable(value = "shopCache", key = "'getItemList-'+#page")
|
private String getItemList(int page) {
|
return HttpUtil.get(HOST + "api/buwan/itemlist?page=" + page, "ISO-8859-1");
|
}
|
|
public List<ShopGoodsItem> getGoodsList(final String uid, int page) {
|
final List<ShopGoodsItem> list = new ArrayList<>();
|
String result = getItemList(page);
|
System.out.println(result);
|
|
JSONObject obj = JSONObject.fromObject(result);
|
if (obj.optInt("code") == 0) {
|
JSONArray array = obj.optJSONObject("data").optJSONArray("data");
|
Gson gson = new GsonBuilder().create();
|
for (int i = 0; i < array.size(); i++) {
|
ShopGoodsItem sg = gson.fromJson(array.optJSONObject(i).toString(), ShopGoodsItem.class);
|
sg.setLoginUser(new LoginUser(array.optJSONObject(i).optLong("uid") + ""));
|
sg.setCommentcount(sg.getCommentcount() + 1);
|
list.add(sg);
|
}
|
loginUserDao.excute(new HibernateCallback<List<LoginUser>>() {
|
public List<LoginUser> doInHibernate(Session session) throws HibernateException {
|
String sql = "";
|
for (ShopGoodsItem item : list) {
|
item.setLoginUser((LoginUser) session.get(LoginUser.class, item.getLoginUser().getId()));
|
sql += String.format(
|
"(select count(*) from wk_shop_item_collect c where c.uid=%s and c.itemid=%s) union all ",
|
uid, item.getId() + "");
|
}
|
if (sql.endsWith("union all "))
|
sql = sql.substring(0, sql.length() - 10);
|
|
System.out.println(sql);
|
|
if (!StringUtil.isNullOrEmpty(sql)) {
|
List li = session.createSQLQuery(sql).list();
|
for (int i = 0; i < list.size(); i++) {
|
list.get(i).setCollect(Integer.parseInt(li.get(i) + "") > 0);
|
}
|
}
|
|
return null;
|
}
|
});
|
}
|
|
return list;
|
}
|
|
public ReturnResult getGoodsListAdmin(int page, String key, int show) {
|
final List<ShopGoodsItem> list = new ArrayList<>();
|
String result = "";
|
try {
|
result = HttpUtil.get(String.format(HOST + "api/buwan/admin/itemlist?page=%s&show=%s&key=%s", page + "",
|
show + "", StringUtil.isNullOrEmpty(key) ? "" : URLEncoder.encode(key, "UTF-8")), "ISO-8859-1");
|
} catch (UnsupportedEncodingException e) {
|
e.printStackTrace();
|
}
|
System.out.println(result);
|
|
JSONObject obj = JSONObject.fromObject(result);
|
if (obj.optInt("code") == 0) {
|
JSONArray array = obj.optJSONObject("data").optJSONArray("data");
|
Gson gson = new GsonBuilder().create();
|
for (int i = 0; i < array.size(); i++) {
|
ShopGoodsItem sg = gson.fromJson(array.optJSONObject(i).toString(), ShopGoodsItem.class);
|
sg.setLoginUser(new LoginUser(array.optJSONObject(i).optLong("uid") + ""));
|
list.add(sg);
|
}
|
loginUserDao.excute(new HibernateCallback<List<LoginUser>>() {
|
public List<LoginUser> doInHibernate(Session session) throws HibernateException {
|
for (ShopGoodsItem item : list) {
|
item.setLoginUser((LoginUser) session.get(LoginUser.class, item.getLoginUser().getId()));
|
}
|
return null;
|
}
|
});
|
}
|
|
long count = obj.optJSONObject("data").optLong("count");
|
|
return new ReturnResult(count, list);
|
|
}
|
|
@Cacheable(value = "shopCache", key = "'getItemDetail-'+#id")
|
private String getItemDetail(long id) {
|
return HttpUtil.get(HOST + "api/buwan/itemdetail?id=" + id, "ISO-8859-1");
|
}
|
|
@Cacheable(value = "shopCache", key = "'getGoodsDetail-'+#id")
|
public ShopGoodsItem getGoodsDetail(long id) {
|
ShopGoodsItem shop = null;
|
Gson gson = new GsonBuilder().create();
|
String result = getItemDetail(id);
|
JSONObject obj = JSONObject.fromObject(result);
|
if (obj.optInt("code") == 0) {
|
JSONObject item = obj.optJSONObject("data").optJSONObject("item");
|
shop = gson.fromJson(item.toString(), ShopGoodsItem.class);
|
shop.setLoginUser(userService.getLoginUser(item.optLong("uid") + ""));
|
JSONArray recommend = obj.optJSONObject("data").optJSONArray("recommends");
|
Type type = new TypeToken<ArrayList<TaoBaoItem>>() {
|
}.getType();
|
List<TaoBaoItem> recommendList = new Gson().fromJson(recommend.toString(), type);
|
shop.setRecommendList(recommendList);
|
}
|
return shop;
|
}
|
|
@Cacheable(value = "shopCache", key = "'getGoodsSimpleDetail-'+#md5")
|
public List<ShopGoodsItem> getGoodsSimpleDetail(long[] ids, String md5) {
|
List<ShopGoodsItem> list = new ArrayList<>();
|
Gson gson = new GsonBuilder().create();
|
String idStr = "";
|
for (long id : ids) {
|
idStr += id + ",";
|
}
|
|
if (idStr.endsWith(","))
|
idStr = idStr.substring(0, idStr.length() - 1);
|
|
String result = HttpUtil.get(HOST + "api/buwan/itemsimpledetail?ids=" + idStr, "ISO-8859-1");
|
JSONObject obj = JSONObject.fromObject(result);
|
if (obj.optInt("code") == 0) {
|
JSONArray items = obj.optJSONObject("data").optJSONArray("items");
|
for (int i = 0; i < items.size(); i++) {
|
JSONObject item = items.optJSONObject(i);
|
if (item == null) {
|
continue;
|
}
|
ShopGoodsItem shop = gson.fromJson(item.toString(), ShopGoodsItem.class);
|
shop.setLoginUser(userService.getLoginUser(item.optLong("uid") + ""));
|
list.add(shop);
|
}
|
}
|
return list;
|
}
|
|
public boolean updateGoodsItem(long id, int show, int orderby, String title) {
|
String url = HOST + "api/buwan/admin/updateitem?id=" + id;
|
if (!StringUtil.isNullOrEmpty(title))
|
try {
|
url += "&title=" + URLEncoder.encode(title, "UTF-8");
|
} catch (UnsupportedEncodingException e) {
|
// TODO Auto-generated catch block
|
e.printStackTrace();
|
}
|
if (show > -1)
|
url += "&show=" + show;
|
if (orderby > -1)
|
url += "&orderby=" + orderby;
|
|
String result = HttpUtil.get(url, "ISO-8859-1");
|
JSONObject obj = JSONObject.fromObject(result);
|
if (obj.optInt("code") == 0) {
|
return true;
|
}
|
return false;
|
}
|
|
// 删除商品
|
public boolean deleteGoodsItem(final long id) {
|
final String url = HOST + "api/buwan/admin/deleteitem?id=" + id;
|
return (Boolean) shopItemCommentDao.excute(new HibernateCallback<Boolean>() {
|
public Boolean doInHibernate(Session session) throws HibernateException {
|
try {
|
String result = HttpUtil.get(url, "ISO-8859-1");
|
JSONObject obj = JSONObject.fromObject(result);
|
if (obj.optInt("code") == 0) {
|
session.getTransaction().begin();
|
session.createQuery("delete from ShopItemComment st where st.itemid=?").setParameter(0, id)
|
.executeUpdate();
|
session.createQuery("delete from ShopItemCollect st where st.itemid=?").setParameter(0, id)
|
.executeUpdate();
|
session.flush();
|
session.getTransaction().commit();
|
return true;
|
}
|
} catch (Exception e) {
|
session.getTransaction().rollback();
|
}
|
return false;
|
}
|
});
|
}
|
|
public List<ShopItemComment> shopItemCommentList(long itemid, int pageIndex) {
|
return shopItemCommentDao.list("from ShopItemComment st where st.itemid=" + itemid,
|
(pageIndex - 1) * Constant.pageCount, Constant.pageCount, null);
|
}
|
|
public long shopItemCommentCount(long itemid) {
|
return shopItemCommentDao.getCount("select count(*) from ShopItemComment st where st.itemid=" + itemid);
|
}
|
|
public void addshopItemComment(ShopItemComment item) {
|
shopItemCommentDao.create(item);
|
HttpUtil.get(HOST + "api/buwan/addcomment?id=" + item.getItemid(), "ISO-8859-1");
|
}
|
|
public List<ShopItemCollect> shopItemCollectList(long uid, int pageIndex) {
|
return shopItemCollectDao.list("from ShopItemCollect st where st.user.id=" + uid,
|
(pageIndex - 1) * Constant.pageCount, Constant.pageCount, null);
|
}
|
|
public long shopItemCollectCount(long uid) {
|
return shopItemCollectDao.getCount("select count(*) from ShopItemCollect st where st.user.id=" + uid);
|
}
|
|
public boolean addshopItemCollect(ShopItemCollect item) {
|
try {
|
shopItemCollectDao.create(item);
|
String result = HttpUtil.get(HOST + "api/buwan/addcollect?id=" + item.getItemid(), "ISO-8859-1");
|
System.out.println(result);
|
return true;
|
} catch (Exception e) {
|
return false;
|
}
|
}
|
|
// 取消收藏
|
public void cancelshopItemCollect(ShopItemCollect item) {
|
List<ShopItemCollect> list = shopItemCollectDao
|
.list(String.format("from ShopItemCollect sm where sm.user.id=%s and sm.itemid=%s",
|
item.getUser().getId(), item.getItemid() + ""));
|
for (ShopItemCollect sc : list) {
|
shopItemCollectDao.delete(sc);
|
HttpUtil.get(HOST + "api/buwan/subcollect?id=" + item.getItemid(), "ISO-8859-1");
|
}
|
}
|
|
// 是否收藏
|
public boolean isCollect(long itemid, String uid) {
|
List<ShopItemCollect> list = shopItemCollectDao
|
.list("from ShopItemCollect sm where sm.user.id=? and sm.itemid=?", new Serializable[] { uid, itemid });
|
if (list != null && list.size() > 0)
|
return true;
|
else
|
return false;
|
}
|
|
// 是否收藏列表
|
public List<ShopItemCollect> collectList(String uid, String loginUid, int pageIndex) {
|
List<ShopItemCollect> list = null;
|
if (!StringUtil.isNullOrEmpty(loginUid)) {
|
shopItemCollectDao.excute(new HibernateCallback() {
|
|
@Override
|
public Object doInHibernate(Session session) throws HibernateException {
|
session.createSQLQuery("UPDATE wk_shop_item_collect SET loginuid=? WHERE uid=?")
|
.setParameter(0, loginUid).setParameter(1, uid).executeUpdate();
|
return null;
|
}
|
});
|
list = shopItemCollectDao.list(
|
String.format("from ShopItemCollect sm where sm.loginUser.id=%s", loginUid) + " order by id desc",
|
(pageIndex - 1) * Constant.pageCount, Constant.pageCount, null);
|
} else {
|
list = shopItemCollectDao.list(
|
String.format("from ShopItemCollect sm where sm.user.id=%s", uid) + " order by id desc",
|
(pageIndex - 1) * Constant.pageCount, Constant.pageCount, null);
|
}
|
|
if (list.size() > 0) {
|
long[] sts = new long[list.size()];
|
int i = 0;
|
String md5S = "";
|
for (ShopItemCollect c : list) {
|
sts[i] = c.getItemid();
|
i++;
|
md5S += c.getItemid() + "-";
|
}
|
List<ShopGoodsItem> itemList = getGoodsSimpleDetail(sts, StringUtil.Md5(md5S));
|
for (i = 0; i < itemList.size(); i++) {
|
if (itemList.get(i) != null)
|
list.get(i).setItem(itemList.get(i));
|
}
|
}
|
for (int i = 0; i < list.size(); i++) {
|
if (list.get(i).getItem() == null) {
|
list.remove(i);
|
i--;
|
}
|
}
|
|
return list;
|
}
|
|
public long collectCount(String uid, int pageIndex) {
|
return shopItemCollectDao
|
.getCount(String.format("select count(*) from ShopItemCollect sm where sm.user.id=%s", uid));
|
}
|
|
}
|