TextbookLibraryService.java 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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.dao.AdMapper;
  8. import com.qxgmat.data.dao.TextbookLibraryMapper;
  9. import com.qxgmat.data.dao.entity.Ad;
  10. import com.qxgmat.data.dao.entity.TextbookLibrary;
  11. import com.qxgmat.data.dao.entity.TextbookLibraryHistory;
  12. import org.slf4j.Logger;
  13. import org.slf4j.LoggerFactory;
  14. import org.springframework.stereotype.Service;
  15. import org.springframework.transaction.annotation.Transactional;
  16. import javax.annotation.Resource;
  17. import java.text.SimpleDateFormat;
  18. import java.util.Collection;
  19. import java.util.Date;
  20. import java.util.List;
  21. @Service
  22. public class TextbookLibraryService extends AbstractService {
  23. private static final Logger logger = LoggerFactory.getLogger(TextbookLibraryService.class);
  24. @Resource
  25. private TextbookLibraryMapper textbookLibraryMapper;
  26. @Resource
  27. private TextbookLibraryHistoryService textbookLibraryHistoryService;
  28. /**
  29. * 后台查看机经换库表
  30. * @param page
  31. * @param size
  32. * @param keyword
  33. * @return
  34. */
  35. public Page<TextbookLibrary> listAdmin(int page, int size, String keyword){
  36. Example example = new Example(TextbookLibrary.class);
  37. if(keyword != null)
  38. example.and(
  39. example.createCriteria()
  40. .orLike("startDate", "%"+keyword+"%")
  41. .orLike("endDate", "%"+keyword+"%")
  42. );
  43. example.orderBy("id").desc();
  44. return page(()->select(textbookLibraryMapper, example), page, size);
  45. }
  46. /**
  47. * 获取往期:最后第二期
  48. * @return
  49. */
  50. public TextbookLibrary getSecond(){
  51. Example example = new Example(TextbookLibrary.class);
  52. example.orderBy("id").desc();
  53. List<TextbookLibrary> list = select(textbookLibraryMapper, example, 2, 1);
  54. return list.get(0);
  55. }
  56. /**
  57. * 获取最后一期
  58. * @return
  59. */
  60. public TextbookLibrary getLatest(){
  61. Example example = new Example(TextbookLibrary.class);
  62. example.orderBy("id").desc();
  63. return one(textbookLibraryMapper, example);
  64. }
  65. /**
  66. * 按时间获取换库表
  67. * @param startTime
  68. * @param endTime
  69. * @return
  70. */
  71. public List<TextbookLibrary> listByTime(Date startTime, Date endTime){
  72. Example example = new Example(TextbookLibrary.class);
  73. example.and(
  74. example.createCriteria()
  75. .andGreaterThanOrEqualTo("startDate", startTime)
  76. .andLessThan("startDate", endTime)
  77. );
  78. example.orderBy("id").asc();
  79. return select(textbookLibraryMapper, example);
  80. }
  81. /**
  82. * 添加一次发布
  83. * @param history
  84. * @return
  85. */
  86. @Transactional
  87. public TextbookLibrary addHistory(TextbookLibraryHistory history){
  88. Example example = new Example(TextbookLibrary.class);
  89. example.and(
  90. example.createCriteria()
  91. .andEqualTo("id", history.getLibraryId())
  92. );
  93. example.orderBy("id").desc();
  94. TextbookLibrary library = one(textbookLibraryMapper, example);
  95. if (library == null){
  96. throw new ParameterException("机经不存在");
  97. }
  98. Date now = new Date();
  99. if (!history.getQuant().isEmpty()){
  100. library.setQuant(history.getQuant());
  101. library.setQuantVersion(library.getQuantVersion() + 1);
  102. library.setQuantTime(now);
  103. library.setQuantDescription(history.getQuantDescription());
  104. history.setQuantVersion(library.getQuantVersion() + 1);
  105. }
  106. if (!history.getIr().isEmpty()){
  107. library.setIr(history.getIr());
  108. library.setIrVersion(library.getIrVersion() + 1);
  109. library.setIrTime(now);
  110. library.setIrDescription(history.getIrDescription());
  111. history.setIrVersion(library.getIrVersion() + 1);
  112. }
  113. if (!history.getRc().isEmpty()){
  114. library.setRc(history.getRc());
  115. library.setRcVersion(library.getRcVersion() + 1);
  116. library.setRcTime(now);
  117. library.setRcDescription(history.getRcDescription());
  118. history.setRcVersion(library.getRcVersion() + 1);
  119. }
  120. library.setHistoryNumber(library.getHistoryNumber() + 1);
  121. history = textbookLibraryHistoryService.add(history);
  122. return edit(library);
  123. }
  124. public TextbookLibrary add(TextbookLibrary ad){
  125. int result = insert(textbookLibraryMapper, ad);
  126. ad = one(textbookLibraryMapper, ad.getId());
  127. if(ad == null){
  128. throw new SystemException("换库添加失败");
  129. }
  130. return ad;
  131. }
  132. public TextbookLibrary edit(TextbookLibrary ad){
  133. TextbookLibrary in = one(textbookLibraryMapper, ad.getId());
  134. if(in == null){
  135. throw new ParameterException("换库不存在");
  136. }
  137. int result = update(textbookLibraryMapper, ad);
  138. return ad;
  139. }
  140. public boolean delete(Number id){
  141. TextbookLibrary in = one(textbookLibraryMapper, id);
  142. if(in == null){
  143. throw new ParameterException("换库不存在");
  144. }
  145. int result = delete(textbookLibraryMapper, id);
  146. return result > 0;
  147. }
  148. public TextbookLibrary get(Number id){
  149. TextbookLibrary in = one(textbookLibraryMapper, id);
  150. if(in == null){
  151. throw new ParameterException("换库不存在");
  152. }
  153. return in;
  154. }
  155. public Page<TextbookLibrary> select(int page, int pageSize){
  156. return select(textbookLibraryMapper, page, pageSize);
  157. }
  158. public Page<TextbookLibrary> select(Integer[] ids){
  159. return page(()-> select(textbookLibraryMapper, ids), 1, ids.length);
  160. }
  161. public List<TextbookLibrary> select(Collection ids){
  162. return select(textbookLibraryMapper, ids);
  163. }
  164. }