AdTheatreController.java 9.4 KB

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