AdTheatreController.java 9.0 KB

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