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
package com.newvideo.controller.admin.api;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
 
import net.sf.json.JSONObject;
 
import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
 
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.newvideo.adapter.HotTypeAdminAdapter;
import com.newvideo.domain.DetailSystem;
import com.newvideo.domain.HotVideoType;
import com.newvideo.domain.SuperHotType;
import com.newvideo.domain.SuperVideoType;
import com.newvideo.domain.VideoType;
import com.newvideo.domain.web.HotTypeAdmin;
import com.newvideo.service.imp.ClassService;
import com.newvideo.service.imp.HotVideoTypeService;
import com.newvideo.service.imp.SystemService;
import com.newvideo.util.Constant;
import com.newvideo.util.StringUtil;
import com.newvideo.web.tag.PageEntity;
 
@Controller
@RequestMapping("admin/new/api/videoType")
public class VideoTypeAdminController {
    
    @Resource
    private ClassService classService;
    
    @Resource
    private SystemService systemService;
    
    @Resource
    private HotVideoTypeService hotVideoTypeService;
    
    private static final String IMGPATH="/upload/class";
    
    @RequestMapping("videoTypeList")
    public void videoTypeList(int pageIndex,int detailsystem,int pid,PrintWriter out) {
        if (pageIndex == 0)
            pageIndex = 1;
 
        List<com.newvideo.domain.web.VideoTypeAdmin> list = classService.getVideoTypeAdmin(detailsystem, pid,
                pageIndex);
 
        long count = classService.getVideoTypeAdminCount(detailsystem, pid);
        PageEntity pe = new PageEntity();
        pe.setPageIndex(pageIndex);
        pe.setPageSize(Constant.pageCount);
        Map<String, String> map = new HashMap<String, String>();
        map.put("pid", pid + "");
        map.put("detailsystem", detailsystem + "");
        pe.setParams(map);
        pe.setTotalCount((int) count);
        JSONObject json=new JSONObject();
        json.put("code", "0");
        json.put("pageEntity", pe);
        Gson gson =new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
        json.put("videoTypeAdminList",gson.toJson(list));
        out.print(json);
        return ;
    }
    
    @RequestMapping("setSuperVideoType")
    public void set(String type,String id,String icon,String detailSystem,PrintWriter out){
        
        if("add".equals(type)){
            SuperVideoType svt = new SuperVideoType();
            svt.setCreatetime(System.currentTimeMillis() + "");
            svt.setDetailSystem(new DetailSystem(detailSystem));
            svt.setPicture(icon.trim());
            svt.setType(new VideoType(Long.parseLong(id)));
            classService.addSuperVideoType(svt);
            out.print("yes");
        }else if("delete".equals(type)){
            classService.deleteVideoTypeAdmin(id, detailSystem);
            out.print("yes");
        }
    }
    
    @RequestMapping("getVideoType")
    public void getVideoType(String id,PrintWriter out) {
        VideoType vt = classService.getType(Long.parseLong(id));
        JSONObject json = new JSONObject();
        json.put("code", "0");
        json.put("videoType", vt);
        out.print(json);
        return ;
    }
    
    @RequestMapping("updateVideoType")
    public void updateVideoType(String id,String orderby,String name,String beizhu,String iconFile,String iconUrl,PrintWriter out) {
        
        System.out.println("id="+id);
        System.out.println("orderby="+orderby);
        System.out.println("name="+name);
        System.out.println("beizhu="+beizhu);
        System.out.println("iconUrl= "+iconUrl);
        
        VideoType vt = classService.getType(Long.parseLong(id));
        vt.setOrderby(Integer.parseInt(orderby));
        if (iconFile != null && !"".equals(iconFile.trim())) {// 保存图片
                vt.setIcon(iconFile);
        } else {
            if (!StringUtil.isNullOrEmpty(iconUrl))
                vt.setIcon(iconUrl);
        }
        vt.setName(name);
        vt.setBeizhu(beizhu);
        System.out.println("vt.getIcon()--"+vt.getIcon());
        classService.updateType(vt);
        out.print("yes");
        return ;
    }
    
    @RequestMapping("addVideoType")
    public void addVideoType(String pid,String orderby,String name,String beizhu,String iconFile,String iconUrl,PrintWriter out) {
        
        System.out.println("pid="+pid);
        System.out.println("orderby="+orderby);
        System.out.println("name="+name);
        System.out.println("beizhu="+beizhu);
        System.out.println("iconUrl= "+iconUrl);
        
        VideoType vt = new VideoType();
        if (iconFile != null && !"".equals(iconFile.trim())) {// 保存图片
            vt.setIcon(iconFile);
    } else {
        if (!StringUtil.isNullOrEmpty(iconUrl))
            vt.setIcon(iconUrl);
    }
        vt.setName(name);
        vt.setBeizhu(beizhu);
        vt.setOrderby(Integer.parseInt(orderby));
        vt.setCreatetime(String.valueOf(new Date().getTime()));
        vt.setShow("1");
        if (!StringUtil.isNullOrEmpty(pid) && !pid.equalsIgnoreCase("0")) {
            vt.setParent(new VideoType(Long.parseLong(pid)));
        }
        System.out.println("vt.getIcon()--"+vt.getIcon());
        classService.createType(vt);
        out.print("yes");
        return ;
    }
    
    @RequestMapping(value="updateVideoTypeFile",method=RequestMethod.POST)
    public void updateVideoTypeFile(@RequestParam("file") CommonsMultipartFile file,HttpServletRequest request,PrintWriter out){
        ServletContext servletContext = request.getSession().getServletContext();
        JSONObject json=new JSONObject();
        if(file != null){
            String root=servletContext.getRealPath(IMGPATH);
            if (!new File(root).exists()){
                new File(root).mkdirs();
            }
             File newFile=new File(root+"/"+file.getOriginalFilename());
            //通过CommonsMultipartFile的方法直接写文件(注意这个时候)
             try {
                file.transferTo(newFile);
                 json.put("code", "0");
                 json.put("path", IMGPATH+"/"+newFile.getName());
            } catch (IllegalStateException e) {
                e.printStackTrace();
                json.put("code", "2");
            } catch (IOException e) {
                e.printStackTrace();
                json.put("code", "2");
            }finally{
                out.print(json);
            }
        }else{
             json.put("code", "1");
             out.print(json);
        }
    }
    
    @RequestMapping("deleteVideoType")
    public void deleteVideoType(String ids,PrintWriter out) {
        String[] idArr = ids.split(",");
        for (String id : idArr) {
            System.out.println("id=="+id);
            classService.deleteVideoType(id);
        }
        out.print("yes");
        return;
    }
    
    @RequestMapping("/getHotVideoTypeList")
    public void getHotVideoTypeList(int page,int detailSystem,String key,PrintWriter out){
        List<HotTypeAdmin> list = hotVideoTypeService.getHotTypeAdmin(detailSystem,key,page);
        int count = hotVideoTypeService.getCount(detailSystem,key);
        PageEntity pe = new PageEntity();
        pe.setTotalCount(count);
        pe.setPageIndex(page);
        pe.setPageSize(Constant.pageCount);
        Gson gson=new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
        JSONObject root = new JSONObject();
        root.put("code", "0");
        root.put("pageEntity", pe);
        root.put("list", gson.toJson(list));
        out.print(root);
    }
    
    @RequestMapping("/addHotVideoType")
    public void addHotVideoType(HotVideoType ht,PrintWriter out){
        if(ht.getType().getId()==0){
            JSONObject root = new JSONObject();
            root.put("code", "1");
            root.put("msg", "视频分类不能为空");
            out.print(root);
            return;
        }
        ht.setAddType(1);
        hotVideoTypeService.addHotType(ht);
        JSONObject root = new JSONObject();
        root.put("code", "0");
        out.print(root);
    }
    
    @RequestMapping("/setSystemOfHotVideoType")
    public void setSystemOfHotVideoType(int type,SuperHotType sht,PrintWriter out){
        if(type == 0){
            hotVideoTypeService.addSuperHotType(sht);
        }else{
            hotVideoTypeService.deleteSuperHotType(sht);
        }
        JSONObject root = new JSONObject();
        root.put("code", "0");
        out.print(root);
    }
    
    @RequestMapping("/deleteHotVideoType")
    public void deleteHotVideoType(String[] ids,PrintWriter out){
        for (String id : ids) {
            hotVideoTypeService.deleteHotType(new HotVideoType(id));
        }
        JSONObject root = new JSONObject();
        root.put("code", "0");
        out.print(root);
    }
    
}