admin
2024-10-30 010ef2a907e66efd4702443c06cdd18f8a7ffa5b
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
package com.yeshi.buwan.controller.admin.api;
 
import com.google.gson.*;
import com.yeshi.buwan.domain.ad.AdAreaConfig;
import com.yeshi.buwan.domain.ad.ThirdAdType;
import com.yeshi.buwan.domain.system.DetailSystem;
import com.yeshi.buwan.dto.ad.AdAreaConfigDto;
import com.yeshi.buwan.job.AdJob;
import com.yeshi.buwan.service.imp.SystemService;
import com.yeshi.buwan.service.inter.ad.AdAreaConfigService;
import com.yeshi.buwan.util.JsonUtil;
import com.yeshi.buwan.util.StringUtil;
import com.yeshi.buwan.util.TimeUtil;
import com.yeshi.buwan.util.factory.ad.AdAreaConfigFactory;
import com.yeshi.buwan.vo.ad.AdConfigTypeVO;
import com.yeshi.buwan.vo.ad.AdPositionVO;
import com.yeshi.buwan.web.tag.PageEntity;
import net.sf.json.JSONArray;
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 java.io.PrintWriter;
import java.lang.reflect.Type;
import java.util.*;
 
@Controller
@RequestMapping("admin/new/api/adAreaConfig")
public class AdAreaConfigAdminController {
    @Resource
    private AdAreaConfigService adAreaConfigService;
 
    @Resource
    private SystemService systemService;
 
    private final 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:ss"));
                }
            }).registerTypeAdapter(ThirdAdType.class, new JsonSerializer<ThirdAdType>() {
 
                @Override
                public JsonElement serialize(ThirdAdType src, Type typeOfSrc, JsonSerializationContext context) {
                    return src == null ? new JsonPrimitive("") : new JsonPrimitive(src.name());
                }
            }).registerTypeAdapter(AdConfigTypeVO.class, new JsonSerializer<AdConfigTypeVO>() {
 
                @Override
                public JsonElement serialize(AdConfigTypeVO src, Type typeOfSrc, JsonSerializationContext context) {
                    if (src == null) {
                        return null;
                    } else {
                        return new JsonPrimitive(new Gson().toJson(AdPositionVO.builder().name(src.getName()).value(src.name()).build()));
                    }
                }
            })
            .create();
 
    @RequestMapping("list")
    public void list(Long detailSystem, int page, PrintWriter out) {
        if (detailSystem != null && detailSystem == 0L) {
            detailSystem = null;
        }
        List<AdAreaConfig> list = adAreaConfigService.list(detailSystem, page, 20);
        Set<Long> detailSystemIds = new HashSet<>();
        for (AdAreaConfig ad : list) {
            detailSystemIds.add(ad.getDetailSystemId());
        }
        Map<Long, DetailSystem> detailSystemMap = new HashMap<>();
        for (Long sid : detailSystemIds) {
            DetailSystem ds = systemService.getDetailSystemById(sid + "");
            DetailSystem fds = new DetailSystem();
            fds.setId(ds.getId());
            fds.setAppName(ds.getAppName());
            detailSystemMap.put(sid, fds);
        }
 
        List<AdAreaConfigDto> flist = new ArrayList<>();
        for (AdAreaConfig ad : list) {
            AdAreaConfigDto dto = AdAreaConfigFactory.createDTO(ad);
            dto.setDetailSystem(detailSystemMap.get(ad.getDetailSystemId()));
            flist.add(dto);
        }
        long count = adAreaConfigService.count(detailSystem);
        JSONObject root = new JSONObject();
        root.put("code", 0);
        root.put("pageEntity", new PageEntity(page, 20, (int) count));
        root.put("data", gson.toJson(flist));
        out.print(root.toString());
    }
 
 
 
    @RequestMapping("get")
    public void get(Long id, PrintWriter out) {
        AdAreaConfig detail = adAreaConfigService.get(id);
        DetailSystem ds = systemService.getDetailSystemById(detail.getDetailSystemId() + "");
        DetailSystem fds = new DetailSystem();
        fds.setId(ds.getId());
        fds.setAppName(ds.getAppName());
        AdAreaConfigDto dto = AdAreaConfigFactory.createDTO(detail);
        dto.setDetailSystem(fds);
        out.print(JsonUtil.loadTrueAdmin(gson.toJson(dto)));
    }
 
 
    @RequestMapping(value = "add", method = RequestMethod.POST)
    public void add(AdAreaConfig config, PrintWriter out) {
        if (config.getDetailSystemId() == null) {
            out.print(JsonUtil.loadFalseAdmin("系统编号未上传"));
            return;
        }
        if (StringUtil.isNullOrEmpty(config.getStartTime()) || StringUtil.isNullOrEmpty(config.getEndTime())) {
            out.print(JsonUtil.loadFalseAdmin("请上传生效时间"));
            return;
        }
 
        if (StringUtil.isNullOrEmpty(config.getChannel())) {
            out.print(JsonUtil.loadFalseAdmin("渠道不能为空"));
            return;
        }
        adAreaConfigService.addConfig(config);
        out.print(JsonUtil.loadTrueAdmin(""));
    }
 
 
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public void update(AdAreaConfig config, PrintWriter out) {
        if (config.getId() == null) {
            out.print(JsonUtil.loadFalseAdmin("请上传id"));
            return;
        }
        adAreaConfigService.updateConfig(config);
        out.print(JsonUtil.loadTrueAdmin(""));
    }
 
 
    @RequestMapping(value = "delete", method = RequestMethod.POST)
    public void delete(Long id, PrintWriter out) {
        if (id == null) {
            out.print(JsonUtil.loadFalseAdmin("请上传id"));
            return;
        }
        adAreaConfigService.delete(id);
        out.print(JsonUtil.loadTrueAdmin(""));
    }
 
 
    /***
     * @author hxh
     * @description 请获取广告位列表
     * @date 14:10 2024/10/28
     * @param: out
     * @return void
     **/
    @RequestMapping(value = "getPositionList", method = RequestMethod.POST)
    public void getPositionList(PrintWriter out) {
        JSONArray array = new JSONArray();
        for (AdConfigTypeVO vo : AdConfigTypeVO.values()) {
            array.add(AdPositionVO.builder().name( vo.getName()).value( vo.name()).build());
        }
        out.print(JsonUtil.loadTrueAdmin(array));
    }
}