喻健
2018-11-13 6feaf1324e207ebee22d0e263a8f32061b8374bf
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
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.yeshi.fanli.entity.bus.clazz.ClassRecommendGoods;
import com.yeshi.fanli.exception.ExistObjectException;
import com.yeshi.fanli.exception.NotExistObjectException;
import com.yeshi.fanli.service.inter.goods.ClassRecommendGoodsService;
import com.yeshi.fanli.tag.PageEntity;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.GsonUtil;
import org.yeshi.utils.JsonUtil;
 
import net.sf.json.JSONObject;
 
@Controller
@RequestMapping("admin/new/api/v1/classgoods")
public class ClassRecommendGoodsAdminController {
    
    @Resource
    private ClassRecommendGoodsService classRecommendGoodsService;
    
    @RequestMapping(value="/addRecommendGoods",method=RequestMethod.POST)
    public void addRecommendGoods(String[] tbids,long cgid,PrintWriter out){
            List<JSONObject> list = new ArrayList<JSONObject>();
            JSONObject data=null;
            for (String tbid : tbids) {
                data=new JSONObject();
                data.put("id",tbid); 
                try {
                    classRecommendGoodsService.addRecommendGoods(tbid,cgid);
                    data.put("msg","添加成功"); 
                } catch (ExistObjectException e) {
                    data.put("msg",e.getMessage()); 
                }
                list.add(data);
            }
            JSONObject dataAll = new JSONObject();
            dataAll.put("list",list);
            out.print(JsonUtil.loadTrueResult(dataAll));
    }
    
    @RequestMapping(value="/getRecommendGoodsList",method=RequestMethod.POST)
    public void getRecommendGoodsList(int pageIndex,String key, long cgid,PrintWriter out){
            List<ClassRecommendGoods> classRecommendGoodsList = classRecommendGoodsService.getClassRecommendGoods(pageIndex-1,key,cgid);
             int count =classRecommendGoodsService.getCount(cgid,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("classRecommendGoodsList", GsonUtil.toDFJson(classRecommendGoodsList));
             out.print(JsonUtil.loadTrueResult(data));
    }
    
    @RequestMapping(value="/deleteRecommendGoods",method=RequestMethod.POST)
    public void deleteRecommendGoods(long[] cgids,PrintWriter out){
        for (long id : cgids) {
            classRecommendGoodsService.deleteRecommendGoods(id);
        }
        out.print(JsonUtil.loadTrueResult("删除成功"));
    }
    
    @RequestMapping(value="/getRecommendGoods",method=RequestMethod.POST)
    public void getRecommendGoods(long cgid,PrintWriter out){
        ClassRecommendGoods crg = classRecommendGoodsService.getRecommendGoods(cgid);
        if(crg ==null){
            out.print(JsonUtil.loadFalseResult("不存在该对象"));
            return;
        }
        out.print(JsonUtil.loadTrueResult(crg.getOrderby()));
    }
    
 
    @RequestMapping(value="/updateRecommendGoods",method=RequestMethod.POST)
    public void updateRecommendGoods(long cgid,int orderby,PrintWriter out){
        try {
            classRecommendGoodsService.updateRecommendGoods(cgid,orderby);
        } catch (NotExistObjectException e) {
            out.print(JsonUtil.loadFalseResult(e.getMessage()));
        }
        out.print(JsonUtil.loadTrueResult("修改成功"));
    }
}