AdTheatreController.java 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. package com.ylcm.sys.controller;
  2. import com.alibaba.excel.EasyExcel;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.baomidou.mybatisplus.core.metadata.IPage;
  5. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  6. import com.ylcm.sys.common.Constants;
  7. import com.ylcm.sys.common.ExcelUtil;
  8. import com.ylcm.sys.domain.AdMedia;
  9. import com.ylcm.sys.domain.AdPlatform;
  10. import com.ylcm.sys.domain.AdProduct;
  11. import com.ylcm.sys.domain.AdTheatre;
  12. import com.ylcm.sys.domain.AdTheatreTv;
  13. import com.ylcm.sys.domain.AdTv;
  14. import com.ylcm.sys.excel.listener.BaseExcelListener;
  15. import com.ylcm.sys.excel.model.AdTheatreExcelDTO;
  16. import com.ylcm.sys.form.AdTheatreForm;
  17. import com.ylcm.sys.service.*;
  18. import com.ylcm.sys.vo.AdTheatreVO;
  19. import lombok.extern.slf4j.Slf4j;
  20. import org.apache.commons.collections.CollectionUtils;
  21. import org.apache.commons.lang.StringUtils;
  22. import org.springframework.beans.BeanUtils;
  23. import org.springframework.core.task.TaskExecutor;
  24. import org.springframework.stereotype.Controller;
  25. import org.springframework.ui.ModelMap;
  26. import org.springframework.web.bind.annotation.RequestMapping;
  27. import org.springframework.web.multipart.MultipartFile;
  28. import javax.annotation.Resource;
  29. import javax.servlet.http.HttpServletRequest;
  30. import javax.servlet.http.HttpServletResponse;
  31. import java.util.ArrayList;
  32. import java.util.Comparator;
  33. import java.util.List;
  34. import java.util.Map;
  35. import java.util.concurrent.CompletableFuture;
  36. import java.util.stream.Collectors;
  37. /**
  38. * 剧场管理
  39. *
  40. * @author liuyu
  41. */
  42. @Slf4j
  43. @Controller
  44. @RequestMapping("/ad-theatre")
  45. public class AdTheatreController extends BaseController{
  46. @Resource
  47. private AdTheatreService adTheatreService;
  48. @Resource
  49. private AdTvService adTvService;
  50. @Resource
  51. private AdTheatreTvService adTheatreTvService;
  52. @Resource
  53. private AdProductService adProductService;
  54. @Resource
  55. private AdPlatformService adPlatformService;
  56. @Resource
  57. private AdMediaService adMediaService;
  58. @Resource
  59. private AdTheatreTvStatService adTheatreTvStatService;
  60. @Resource
  61. private TaskExecutor taskExecutor;
  62. /**
  63. * to 列表页面
  64. * */
  65. @RequestMapping(value = "/index.do")
  66. public String index(ModelMap model) {
  67. // 菜单选中,和权限对应
  68. model.put("menu", "ad_theatre");
  69. model.put("parentMenu", "ad_placement");
  70. return "adtheatre/index";
  71. }
  72. /**
  73. * 分页获取列表数据
  74. * @param form 参数
  75. */
  76. @RequestMapping(value = "/ajax_list.do")
  77. public void mediaList(AdTheatreForm form, HttpServletRequest request, HttpServletResponse response) {
  78. JSONObject result = new JSONObject();
  79. result.put("code", Constants.CODE_SUCCESS);
  80. result.put("msg", "操作成功");
  81. IPage<AdTheatreVO> vos = adTheatreService.page(new Page<AdTheatre>(form.getPageNo(), form.getPageSize()), form);
  82. result.put("list", vos);
  83. returnResult(request, response, result);
  84. }
  85. @RequestMapping(value = "/detail.do")
  86. public String detail(Integer id, ModelMap model) {
  87. AdTheatre adTheatre = adTheatreService.getById(id);
  88. model.put("map", adTheatre);
  89. return "adtheatre/detail";
  90. }
  91. @RequestMapping(value = "/del.do")
  92. public void del(Integer id, HttpServletRequest request, HttpServletResponse response) {
  93. JSONObject result = new JSONObject();
  94. result.put("code", Constants.CODE_SUCCESS);
  95. result.put("msg", "服务器异常");
  96. String error = adTheatreService.deleteById(id);
  97. if (error == null) {
  98. result.put("code", Constants.CODE_SUCCESS);
  99. result.put("msg", "操作成功");
  100. } else {
  101. result.put("msg", error);
  102. }
  103. returnResult(request, response, result);
  104. }
  105. @RequestMapping(value = "/batch_del.do")
  106. public void batchDel(String ids, HttpServletRequest request, HttpServletResponse response) {
  107. JSONObject result = new JSONObject();
  108. result.put("code", Constants.CODE_SUCCESS);
  109. result.put("msg", "服务器异常");
  110. String[] idArray = ids.split(",");
  111. for (String id : idArray) {
  112. adTheatreService.deleteById(Integer.parseInt(id));
  113. }
  114. returnResult(request, response, result);
  115. }
  116. @RequestMapping(value = "/save.do")
  117. public void save(AdTheatreVO vo, HttpServletRequest request, HttpServletResponse response) {
  118. JSONObject result = new JSONObject();
  119. result.put("code", Constants.CODE_SUCCESS);
  120. result.put("msg", "服务器异常");
  121. AdTheatre adTheatre = vo.toAdTheatre();
  122. if (vo.getId() != null) {
  123. // 更新
  124. AdTheatre old = adTheatreService.getUniqu(adTheatre);
  125. if (old != null && !old.getId().equals(adTheatre.getId())) {
  126. result.put("code", Constants.CODE_FAIL);
  127. result.put("msg", "数据重复");
  128. returnResult(request, response, result);
  129. return;
  130. }
  131. adTheatreService.saveOrUpd(adTheatre);
  132. result.put("msg", "操作成功");
  133. } else {
  134. // 新增
  135. AdTheatre old = adTheatreService.getUniqu(adTheatre);
  136. if (old != null) {
  137. result.put("code", Constants.CODE_FAIL);
  138. result.put("msg", "数据重复");
  139. returnResult(request, response, result);
  140. return;
  141. }
  142. adTheatreService.saveOrUpd(adTheatre);
  143. result.put("msg", "操作成功");
  144. }
  145. returnResult(request, response, result);
  146. }
  147. @RequestMapping(value = "/export.do")
  148. public void export(AdTheatreForm form, HttpServletRequest request, HttpServletResponse response) {
  149. ExcelUtil.exportExcelToWebsite(response,"剧场数据",new AdTheatreExcelDTO(),(currentPage, pageSize) -> {
  150. return toExport(adTheatreService.page(new Page<AdTheatre>(currentPage, pageSize), form).getRecords());
  151. });
  152. }
  153. private List<AdTheatreExcelDTO> toExport(List<AdTheatreVO> list) {
  154. if (CollectionUtils.isEmpty(list)){
  155. return null;
  156. }
  157. List<AdTheatreExcelDTO> dtoList = new ArrayList<AdTheatreExcelDTO>();
  158. list.forEach(x ->{
  159. // 电视剧收视数据
  160. AdTheatreTv tv = adTheatreTvService.getByTheatreId(x.getId());
  161. // 整合
  162. dtoList.add(new AdTheatreExcelDTO.AdTheatreExcelDTOBuilder().adTheatre(x).adTheatreTv(tv).build());
  163. });
  164. return dtoList;
  165. }
  166. @RequestMapping(value = "import_excel.do")
  167. public String importExcel(HttpServletRequest request, HttpServletResponse response, MultipartFile excelfile) {
  168. Integer code = Constants.CODE_FAIL;
  169. String msg = "服务器异常";
  170. String returnHtml = "common/import_callback";
  171. try {
  172. BaseExcelListener<AdTheatreExcelDTO> listener = new BaseExcelListener<>();
  173. EasyExcel.read(excelfile.getInputStream(), AdTheatreExcelDTO.class,listener).sheet(1).doRead();
  174. List<AdTheatreExcelDTO> dtos = listener.getDataList();
  175. if(CollectionUtils.isEmpty(dtos)) {
  176. request.setAttribute("code", Constants.CODE_FAIL);
  177. request.setAttribute("msg", "表格数据为空!");
  178. return "common/import_callback";
  179. }
  180. // 表格实际从第二行开始导入
  181. int index = 2;
  182. // 待入库
  183. List<AdTheatre> list = new ArrayList<>();
  184. List<AdTheatreTv> tvList = new ArrayList<>();
  185. for (AdTheatreExcelDTO dto: dtos) {
  186. index++;
  187. /*
  188. * “待入库”的剧场数据
  189. */
  190. // 基本校验
  191. String errorMsg = dto.check();
  192. if (StringUtils.isNotBlank(errorMsg)){
  193. request.setAttribute("code", Constants.CODE_FAIL);
  194. request.setAttribute("msg", "第"+ index + "行" + errorMsg);
  195. return "common/import_callback";
  196. }
  197. AdTheatre adTheatre = dto.toAdTheatre();
  198. // 获取剧的体裁、细分体裁
  199. AdTv tv = adTvService.getByName(dto.getName());
  200. if (tv == null){
  201. request.setAttribute("code", Constants.CODE_FAIL);
  202. request.setAttribute("msg", "第"+ index + "行的剧不存在,请先到电视剧管理中添加该剧");
  203. return "common/import_callback";
  204. }
  205. adTheatre.setFirstTheme(tv.getFirstTheme());
  206. adTheatre.setSecondTheme(tv.getSecondTheme());
  207. // 媒体是否存在
  208. AdMedia media = adMediaService.getByName(dto.getMediaName());
  209. if (media == null){
  210. request.setAttribute("code", Constants.CODE_FAIL);
  211. request.setAttribute("msg", "第"+ index + "行的媒体不存在,请先到媒体管理中添加");
  212. return "common/import_callback";
  213. }
  214. // 产品or类目是否存在
  215. if (StringUtils.isBlank(adTheatre.getTypeName())){
  216. // 产品/类目名称可以为空
  217. } else if (Integer.valueOf(0).equals(adTheatre.getType())){
  218. // 产品是否存在
  219. AdProduct product = adProductService.getByName(adTheatre.getTypeName());
  220. if (product == null){
  221. request.setAttribute("msg", "第"+ index + "行产品不存在,请先到产品管理中添加");
  222. return "common/import_callback";
  223. }
  224. } else if (Integer.valueOf(1).equals(adTheatre.getType())){
  225. // 栏目是否存在
  226. AdPlatform platform = adPlatformService.getByName(adTheatre.getTypeName());
  227. if (platform == null){
  228. request.setAttribute("msg", "第"+ index + "行栏目不存在,请先到栏目管理中添加");
  229. return "common/import_callback";
  230. }
  231. }
  232. // 状态默认为“正常”
  233. adTheatre.setStatus(0);
  234. // 存入“待入库”列表
  235. list.add(adTheatre);
  236. /*
  237. * “待入库”的电视剧数据
  238. */
  239. tvList.add(dto.toAdTheatreTv());
  240. }
  241. // 入库
  242. List<AdTheatre> adTheatres = adTheatreService.batchAdd(list, tvList);
  243. //电视剧分组
  244. Map<String,List<AdTheatre>> map = list.stream().collect(Collectors.groupingBy(b -> b.getName()));
  245. for (Map.Entry<String, List<AdTheatre>> entry : map.entrySet()) {
  246. if(CollectionUtils.isNotEmpty(entry.getValue())){
  247. CompletableFuture[] completableFutures = entry.getValue().stream().map(x -> CompletableFuture.runAsync(() -> {
  248. adTheatreTvStatService.saveStatByTheatreTv(x);
  249. }, taskExecutor)).toArray(CompletableFuture[]::new);
  250. CompletableFuture.allOf(completableFutures).join();
  251. }
  252. }
  253. // 判断执行结果
  254. if (adTheatres != null) {
  255. request.setAttribute("repeat", 0);
  256. request.setAttribute("code", Constants.CODE_SUCCESS);
  257. request.setAttribute("msg", msg);
  258. return "common/import_callback";
  259. }
  260. } catch (Exception e) {
  261. log.error(e.getMessage(), e);
  262. if (e.getMessage().contains("数据重复")){
  263. msg = e.getMessage();
  264. }
  265. }
  266. // 返回结果
  267. request.setAttribute("code", code);
  268. request.setAttribute("msg", msg);
  269. return returnHtml;
  270. }
  271. }