AdService.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package com.qxgmat.service.inline;
  2. import com.github.pagehelper.Page;
  3. import com.nuliji.tools.AbstractService;
  4. import com.nuliji.tools.exception.ParameterException;
  5. import com.nuliji.tools.exception.SystemException;
  6. import com.nuliji.tools.mybatis.Example;
  7. import com.qxgmat.data.constants.enums.status.DirectionStatus;
  8. import com.qxgmat.data.dao.AdMapper;
  9. import com.qxgmat.data.dao.entity.Ad;
  10. import org.slf4j.Logger;
  11. import org.slf4j.LoggerFactory;
  12. import org.springframework.stereotype.Service;
  13. import org.springframework.transaction.annotation.Transactional;
  14. import javax.annotation.Resource;
  15. import java.util.Collection;
  16. import java.util.Date;
  17. import java.util.List;
  18. @Service
  19. public class AdService extends AbstractService {
  20. private static final Logger logger = LoggerFactory.getLogger(AdService.class);
  21. @Resource
  22. private AdMapper adMapper;
  23. public Page<Ad> listAdmin(int page, int size, String channel, String position, String order, DirectionStatus direction){
  24. Example example = new Example(Ad.class);
  25. if (channel != null)
  26. example.and(
  27. example.createCriteria()
  28. .andEqualTo("channel", channel)
  29. );
  30. if (position != null)
  31. example.and(
  32. example.createCriteria()
  33. .andEqualTo("position", position)
  34. );
  35. if(order == null || order.isEmpty()) order = "id";
  36. switch(direction){
  37. case ASC:
  38. example.orderBy(order).asc();
  39. break;
  40. case DESC:
  41. default:
  42. example.orderBy(order).desc();
  43. }
  44. return select(adMapper, example, page, size);
  45. }
  46. public List<Ad> all(String channel){
  47. Example example = new Example(Ad.class);
  48. Date day = new Date();
  49. example.and(
  50. example.createCriteria()
  51. .orGreaterThanOrEqualTo("startTime", day)
  52. .orIsNull("startTime")
  53. );
  54. example.and(
  55. example.createCriteria()
  56. .orLessThan("endTime", day)
  57. .orIsNull("endTime")
  58. );
  59. example.and(
  60. example.createCriteria()
  61. .andEqualTo("channel", channel)
  62. );
  63. example.orderBy("position").asc();
  64. return select(adMapper, example);
  65. }
  66. public Ad getAd(String channel, String place){
  67. Example example = new Example(Ad.class);
  68. Date day = new Date();
  69. example.and(
  70. example.createCriteria()
  71. .andEqualTo("channel", channel)
  72. .andEqualTo("place", place)
  73. );
  74. return one(adMapper, example);
  75. }
  76. @Transactional
  77. public Ad addAd(Ad entity){
  78. Ad in = getAd(entity.getChannel(), entity.getPlace());
  79. if (in != null){
  80. throw new ParameterException("广告位已设置");
  81. }
  82. return add(entity);
  83. }
  84. @Transactional
  85. public Ad editAd(Ad entity){
  86. Ad in = getAd(entity.getChannel(), entity.getPlace());
  87. if (in != null && !in.getId().equals(entity.getId())){
  88. throw new ParameterException("广告位已设置");
  89. }
  90. return edit(entity);
  91. }
  92. public Ad add(Ad ad){
  93. int result = insert(adMapper, ad);
  94. ad = one(adMapper, ad.getId());
  95. if(ad == null){
  96. throw new SystemException("广告添加失败");
  97. }
  98. return ad;
  99. }
  100. public Ad edit(Ad ad){
  101. Ad in = one(adMapper, ad.getId());
  102. if(in == null){
  103. throw new ParameterException("广告不存在");
  104. }
  105. int result = update(adMapper, ad);
  106. return ad;
  107. }
  108. public boolean delete(Number id){
  109. Ad in = one(adMapper, id);
  110. if(in == null){
  111. throw new ParameterException("广告不存在");
  112. }
  113. int result = delete(adMapper, id);
  114. return result > 0;
  115. }
  116. public Ad get(Number id){
  117. Ad in = one(adMapper, id);
  118. if(in == null){
  119. throw new ParameterException("广告不存在");
  120. }
  121. return in;
  122. }
  123. public Page<Ad> select(int page, int pageSize){
  124. return select(adMapper, page, pageSize);
  125. }
  126. public Page<Ad> select(Integer[] ids){
  127. return page(()->select(adMapper, ids), 1, ids.length);
  128. }
  129. public List<Ad> select(Collection ids){
  130. return select(adMapper, ids);
  131. }
  132. }