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() { @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() { @Override public JsonElement serialize(ThirdAdType src, Type typeOfSrc, JsonSerializationContext context) { return src == null ? new JsonPrimitive("") : new JsonPrimitive(src.name()); } }).registerTypeAdapter(AdConfigTypeVO.class, new JsonSerializer() { @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 list = adAreaConfigService.list(detailSystem, page, 20); Set detailSystemIds = new HashSet<>(); for (AdAreaConfig ad : list) { detailSystemIds.add(ad.getDetailSystemId()); } Map 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 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)); } }