admin
2021-01-04 aa6ef62aef83e277d4171df1d9f0803f91738216
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package com.newvideo.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.newvideo.dao.LoginUserDao;
import com.newvideo.dao.shop.ShopItemCollectDao;
import com.newvideo.dao.shop.ShopItemCommentDao;
import com.newvideo.domain.LoginUser;
import com.newvideo.domain.entity.ReturnResult;
import com.newvideo.domain.shop.ShopGoodsItem;
import com.newvideo.domain.shop.ShopItemCollect;
import com.newvideo.domain.shop.ShopItemComment;
import com.newvideo.domain.shop.TaoBaoItem;
import com.newvideo.service.imp.UserService;
import com.newvideo.util.Constant;
import com.newvideo.util.HttpUtil;
import com.newvideo.util.StringUtil;
 
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
 
@Service
public class ShopService {
    final static String HOST = "http://111.231.250.166:8087//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/mugua/itemlist?page=" + page, "ISO-8859-1");
    }
 
    public List<ShopGoodsItem> getGoodsList(final String uid, int page) {
        final List<ShopGoodsItem> list = new ArrayList<ShopGoodsItem>();
        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<ShopGoodsItem>();
        String result = "";
        try {
            result = HttpUtil.get(String.format(HOST + "api/mugua/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/mugua/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<ShopGoodsItem>();
        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/mugua/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/mugua/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/mugua/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) {
                    if (session.getTransaction().isActive())
                        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/mugua/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/mugua/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/mugua/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, int pageIndex) {
        List<ShopItemCollect> list = shopItemCollectDao.list(
                String.format("from ShopItemCollect sm where sm.user.id=%s", uid), (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));
    }
 
}