admin
2021-09-24 f788607ff771a47bc60d6a86e00b3433c40f3d2c
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
package com.yeshi.buwan.controller.admin.api;
 
import com.google.gson.*;
import com.google.gson.reflect.TypeToken;
import com.yeshi.buwan.domain.live.*;
import com.yeshi.buwan.domain.web.DetailSystemSelect;
import com.yeshi.buwan.exception.live.TVLiveCategoryException;
import com.yeshi.buwan.service.imp.SystemService;
import com.yeshi.buwan.service.inter.live.TVLiveCategoryChannelService;
import com.yeshi.buwan.service.inter.live.TVLiveChannelResourceService;
import com.yeshi.buwan.service.inter.live.TVLiveChannelService;
import com.yeshi.buwan.util.*;
import com.yeshi.buwan.vo.tvlive.TVLiveChannelResourceMapAdminVO;
import com.yeshi.buwan.web.tag.PageEntity;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.PrintWriter;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
 
@Controller
@RequestMapping("admin/new/api/tvlive/channel")
public class TVLiveChannelAdminController {
 
    @Resource
    private TVLiveChannelService tvLiveChannelService;
 
    @Resource
    private TVLiveCategoryChannelService tvLiveCategoryChannelService;
 
    @Resource
    private SystemService systemService;
 
    @Resource
    private TVLiveChannelResourceService tvLiveChannelResourceService;
 
    private Gson gson = new GsonBuilder().registerTypeAdapter(Date.class, new JsonSerializer<Date>() {
 
        @Override
        public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) {
            return src == null ? new JsonPrimitive("") : new JsonPrimitive(TimeUtil.getGernalTime(src.getTime(), "yyyy.MM.dd HH:mm"));
        }
    }).create();
 
    @RequestMapping(value = "/getChannelList", method = RequestMethod.POST)
    public void getChannelList(HttpSession session, String categoryId, String key, Integer state, int page, PrintWriter out) {
 
        List<TVLiveChannel> channelList;
        long count;
        if (StringUtil.isNullOrEmpty(categoryId)) {
            channelList = tvLiveChannelService.list(key, state, page, Constant.pageCount);
            count = tvLiveChannelService.count(key, state);
        } else {
            channelList = tvLiveCategoryChannelService.listChannelByCid(categoryId, key, page, Constant.pageCount);
            count = tvLiveCategoryChannelService.countChannelByCid(categoryId, key);
        }
        JSONObject data = new JSONObject();
        data.put("pageEntity", new PageEntity(page, Constant.pageCount, (int) count));
        data.put("data", gson.toJson(channelList));
        out.print(JsonUtil.loadTrueAdmin(data.toString()));
    }
 
    @RequestMapping(value = "/addChannel", method = RequestMethod.POST)
    public void addChannel(TVLiveChannel channel, HttpSession session, PrintWriter out) {
        if (StringUtil.isNullOrEmpty(channel.getName()) || StringUtil.isNullOrEmpty(channel.gethPicture())) {
            out.print(JsonUtil.loadFalseAdmin("数据不完整"));
            return;
        }
        channel.setState(TVLiveChannel.STATE_NORMAL);
        try {
            tvLiveChannelService.addChannel(channel);
            out.print(JsonUtil.loadTrueAdmin(""));
        } catch (Exception e) {
            out.print(JsonUtil.loadFalseAdmin(e.getMessage()));
            return;
        }
 
 
    }
 
 
    @RequestMapping(value = "/addCategoryChannel", method = RequestMethod.POST)
    public void addCategoryChannel(String categoryId, String channelIds, Integer weight, HttpSession session, PrintWriter out) {
 
        if (StringUtil.isNullOrEmpty(categoryId) || StringUtil.isNullOrEmpty(channelIds)) {
            out.print(JsonUtil.loadFalseAdmin("数据不完整"));
            return;
        }
 
        String[] channelIdArray = channelIds.split(",");
        for (String channelId : channelIdArray) {
            TVLiveCategoryChannelMap map = new TVLiveCategoryChannelMap();
            map.setWeight(weight);
            map.setCategoryId(categoryId);
            map.setChannelId(channelId);
            tvLiveCategoryChannelService.addMap(map);
        }
 
        out.print(JsonUtil.loadTrueAdmin(""));
    }
 
    @RequestMapping(value = "/deleteCategoryChannel", method = RequestMethod.POST)
    public void deleteCategoryChannel(String categoryId, String channelIds, HttpSession session, PrintWriter out) {
 
        if (StringUtil.isNullOrEmpty(categoryId) || StringUtil.isNullOrEmpty(channelIds)) {
            out.print(JsonUtil.loadFalseAdmin("数据不完整"));
            return;
        }
 
        String[] channelIdArray = channelIds.split(",");
        for (String channelId : channelIdArray) {
            TVLiveCategoryChannelMap map = tvLiveCategoryChannelService.selectByCategoryIdAndChannelId(categoryId, channelId);
            if (map != null)
                tvLiveCategoryChannelService.deleteByPrimaryKey(map.getId());
        }
        out.print(JsonUtil.loadTrueAdmin(""));
    }
 
 
    @RequestMapping(value = "/updateCategoryChannelWeight", method = RequestMethod.POST)
    public void updateCategoryChannelWeight(String categoryId, String channelId, Integer weight, HttpSession session, PrintWriter out) {
        TVLiveCategoryChannelMap oldMap = tvLiveCategoryChannelService.selectByCategoryIdAndChannelId(categoryId, channelId);
        if (oldMap == null) {
            out.print(JsonUtil.loadFalseAdmin("分类的频道不存在"));
            return;
        }
 
        TVLiveCategoryChannelMap map = new TVLiveCategoryChannelMap();
        map.setId(oldMap.getId());
        map.setWeight(weight);
        tvLiveCategoryChannelService.update(map);
        out.print(JsonUtil.loadTrueAdmin(""));
    }
 
    //获取频道资源
 
    /**
     * 获取频道的播放资源
     *
     * @param session
     * @param id
     * @param out
     */
    @RequestMapping(value = "/getChannelResourceList", method = RequestMethod.POST)
    public void getChannelResourceList(HttpSession session, String id, PrintWriter out) {
 
        if (StringUtil.isNullOrEmpty(id)) {
            out.print(JsonUtil.loadFalseAdmin("频道ID不能为空"));
            return;
        }
 
 
        List<TVLiveChannelResourceMap> mapList = tvLiveChannelResourceService.listByChannelId(id);
        List<TVLiveChannelResourceMapAdminVO> voList = new ArrayList<>();
        if (mapList != null)
            for (TVLiveChannelResourceMap map : mapList) {
                voList.add(TVLiveChannelResourceMapAdminVO.create(map));
            }
 
        JSONObject data = new JSONObject();
        data.put("data", gson.toJson(voList));
        data.put("channelId", id);
        out.print(JsonUtil.loadTrueAdmin(data.toString()));
    }
 
 
    /**
     * 更新频道播放资源
     *
     * @param session
     * @param id
     * @param resources
     * @param out
     */
    @RequestMapping(value = "/updateChannelResources", method = RequestMethod.POST)
    public void updateChannelResources(HttpSession session, String id, String resources, PrintWriter out) {
 
        if (StringUtil.isNullOrEmpty(id)) {
            out.print(JsonUtil.loadFalseAdmin("频道ID不能为空"));
            return;
        }
 
        Type type = new TypeToken<List<TVLiveChannelResourceMapAdminVO>>() {
        }.getType();
 
        List<TVLiveChannelResourceMapAdminVO> voList = gson.fromJson(resources, type);
 
        List<TVLiveChannelResourceMap> list = new ArrayList<>();
 
        for (TVLiveChannelResourceMapAdminVO vo : voList) {
            TVLiveChannelResourceMap map = new TVLiveChannelResourceMap();
            map.setId(vo.getId());
            map.setWeight(vo.getWeight());
            map.setChannelId(id);
            map.setPlayUrl(vo.getPlayUrl());
            map.setSubResourceName(vo.getName());
            list.add(map);
        }
        List<TVLiveChannelResourceMap> originMapList = tvLiveChannelResourceService.listByChannelId(id);
        if (originMapList == null)
            originMapList = new ArrayList<>();
 
        List<String> originMapIds = new ArrayList<>();
        for (TVLiveChannelResourceMap map : originMapList) {
            originMapIds.add(map.getId());
        }
 
        //更新需要更新的
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getId() != null && originMapIds.contains(list.get(i).getId())) {
                originMapIds.remove(list.get(i).getId());
                tvLiveChannelResourceService.update(list.get(i));
                list.remove(i--);
            }
        }
        //删除不需要更新的
        for (String mid : originMapIds) {
            tvLiveChannelResourceService.delete(mid);
        }
 
        //添加新的
        for (TVLiveChannelResourceMap map : list) {
            //咪咕不能手动添加
            map.setResource(TVLiveResource.other);
            tvLiveChannelResourceService.add(map);
        }
        out.print(JsonUtil.loadTrueAdmin(""));
    }
 
 
}