Administrator
2018-10-30 7bf6a0582c7c62c90ee2ed8a88654f11d0479092
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
package com.yeshi.fanli.controller.admin;
 
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import javax.annotation.Resource;
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
 
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.yeshi.fanli.entity.bus.recommend.RecommendSectionGoods;
import com.yeshi.fanli.entity.taobao.PidUser;
import com.yeshi.fanli.entity.taobao.SearchFilter;
import com.yeshi.fanli.entity.taobao.TaoBaoSearchResult;
import com.yeshi.fanli.entity.taobao.TaoBaoUnionConfig;
import com.yeshi.fanli.exception.ExistObjectException;
import com.yeshi.fanli.exception.NotExistObjectException;
import com.yeshi.fanli.service.inter.goods.RecommendSectionGoodsService;
import com.yeshi.fanli.service.inter.taobao.TaoBaoUnionConfigService;
import com.yeshi.fanli.tag.PageEntity;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.GsonUtil;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.taobao.TaoBaoUtil;
import org.yeshi.utils.JsonUtil;
 
import net.sf.json.JSONObject;
 
@Controller
@RequestMapping("admin/new/api/v1/sectiongoods")
public class RecommendSectionGoodsAdminController {
 
    @Resource
    private RecommendSectionGoodsService recommendSectionGoodsService;
 
    @Resource
    private TaoBaoUnionConfigService taoBaoUnionConfigService;
 
    @RequestMapping(value = "/getSectionGoodsList", method = RequestMethod.POST)
    public void getSectionGoodsList(int pageIndex, long rsid, String key, PrintWriter out) {
 
        List<RecommendSectionGoods> recommendSections = recommendSectionGoodsService
                .getRecommendSectionGoods(pageIndex - 1, rsid, key);
        int count = recommendSectionGoodsService.getCount(rsid, key);
        int totalPage = count % Constant.PAGE_SIZE == 0 ? count / Constant.PAGE_SIZE : count / Constant.PAGE_SIZE + 1;
        PageEntity pe = new PageEntity(pageIndex, Constant.PAGE_SIZE, count, totalPage);
        Map<String, String> map = new HashMap<String, String>();
        map.put("key", key);
        pe.setParams(map);
        JSONObject data = new JSONObject();
        data.put("pe", pe);
        data.put("recommendSectionGoodsList", GsonUtil.toDFJson(recommendSections));
        out.print(JsonUtil.loadTrueResult(data));
        return;
 
    }
 
    @RequestMapping(value = "/deleteSectionGoods", method = RequestMethod.POST)
    public void deleteSectionGoods(long[] rsgids, PrintWriter out) {
        recommendSectionGoodsService.deleteSectionGoods(rsgids);
        out.print(JsonUtil.loadTrueResult("删除成功"));
    }
 
    @RequestMapping(value = "/searchTaoBaoGoodsList")
    public void searchTaoBaoGoodsList(String callback, int pageIndex, int type, String key, PrintWriter out) {
        List<TaoBaoUnionConfig> config = taoBaoUnionConfigService.getConfigByTypeCache(PidUser.TYPE_FANLI_ANDROID);
        SearchFilter searchFilter = new SearchFilter();
        searchFilter.setPage(pageIndex);
        searchFilter.setKey(key);
        searchFilter.setType(type);
        searchFilter.setQuan(1);
        TaoBaoSearchResult search = TaoBaoUtil.search2(searchFilter, config.get(0));
        JSONObject data = new JSONObject();
        Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
        String json = gson.toJson(search.getTaoBaoGoodsBriefs());
        if ("[]".equals(json)) {
            out.print(JsonUtil.loadFalseResult("暂无数据"));
            return;
        }
        PageEntity pe = search.getPageEntity();
        pe.setPageIndex(pageIndex);
        String pejson = gson.toJson(search.getPageEntity());
        data.put("taoBaoGoodsList", json);
        data.put("pe", pejson);
        if (StringUtil.isNullOrEmpty(callback))
            out.print(JsonUtil.loadTrueResult(data));
        else
            out.print(callback + "(" + JsonUtil.loadTrueResult(data) + ")");
    }
 
    @RequestMapping(value = "/addSectionGoods", method = RequestMethod.POST)
    public void addSectionGoods(long rsid, String[] tbgids, PrintWriter out) {
        List<JSONObject> list = new ArrayList<JSONObject>();
        JSONObject data = null;
        for (String tbgid : tbgids) {
            data = new JSONObject();
            try {
                recommendSectionGoodsService.addSectionGoods(rsid, tbgid);
                data.put("id", tbgid);
                data.put("msg", "添加成功");
            } catch (ExistObjectException e) {
                data.put("id", tbgid);
                data.put("msg", e.getMessage());
            }
            list.add(data);
        }
        JSONObject dataAll = new JSONObject();
        dataAll.put("list", list);
        out.print(JsonUtil.loadTrueResult(dataAll));
    }
 
    @RequestMapping(value = "/getSectionGoods", method = RequestMethod.POST)
    public void getSectionGoods(long id, PrintWriter out) {
        RecommendSectionGoods rsg = recommendSectionGoodsService.getRecommendSectionGoods(id);
        JSONObject data = new JSONObject();
        if (rsg == null) {
            out.print(JsonUtil.loadFalseResult("不存在RecommendSectionGoods"));
            return;
        }
        data.put("orderby", rsg.getOrderby());
        out.print(JsonUtil.loadTrueResult(data));
    }
 
    @RequestMapping(value = "/updateSectionGoods", method = RequestMethod.POST)
    public void updateSectionGoods(RecommendSectionGoods rsg, PrintWriter out) {
        try {
            recommendSectionGoodsService.updateRecommendSectionGoods(rsg);
            out.print(JsonUtil.loadTrueResult("修改成功"));
        } catch (NotExistObjectException e) {
            out.print(JsonUtil.loadFalseResult(e.getMessage()));
        }
    }
}