UserInvoiceService.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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.UserInvoiceMapper;
  9. import com.qxgmat.data.dao.entity.UserInvoice;
  10. import org.slf4j.Logger;
  11. import org.slf4j.LoggerFactory;
  12. import org.springframework.stereotype.Service;
  13. import javax.annotation.Resource;
  14. import java.util.Arrays;
  15. import java.util.Collection;
  16. import java.util.List;
  17. import java.util.stream.Collectors;
  18. @Service
  19. public class UserInvoiceService extends AbstractService {
  20. private static final Logger logger = LoggerFactory.getLogger(UserInvoice.class);
  21. @Resource
  22. private UserInvoiceMapper userInvoiceMapper;
  23. public UserInvoice getByOrder(Integer userId, Integer orderId){
  24. Example example = new Example(UserInvoice.class);
  25. if(userId != null){
  26. example.and(
  27. example.createCriteria()
  28. .andEqualTo("userId", userId)
  29. );
  30. }
  31. if(orderId != null){
  32. example.and(
  33. example.createCriteria()
  34. .andEqualTo("orderId", orderId)
  35. );
  36. }
  37. return one(userInvoiceMapper, example);
  38. }
  39. public List<UserInvoice> listByOrder(Integer userId, Collection orderIds){
  40. Example example = new Example(UserInvoice.class);
  41. if(userId != null){
  42. example.and(
  43. example.createCriteria()
  44. .andEqualTo("userId", userId)
  45. );
  46. }
  47. if(orderIds != null){
  48. example.and(
  49. example.createCriteria()
  50. .andIn("orderId", orderIds)
  51. );
  52. }
  53. return select(userInvoiceMapper, example);
  54. }
  55. public Page<UserInvoice> listAdmin(int page, int size, Integer userId, Boolean isDownload, Boolean isFinish, String order, DirectionStatus direction){
  56. Example example = new Example(UserInvoice.class);
  57. if(userId != null){
  58. example.and(
  59. example.createCriteria()
  60. .andEqualTo("userId", userId)
  61. );
  62. }
  63. if(isDownload != null){
  64. example.and(
  65. example.createCriteria()
  66. .andEqualTo("isDownload", isDownload ? 1 : 0)
  67. );
  68. }
  69. if(isFinish != null){
  70. example.and(
  71. example.createCriteria()
  72. .andEqualTo("isFinish", isFinish ? 1 : 0)
  73. );
  74. }
  75. if(order == null || order.isEmpty()) order = "id";
  76. switch(direction){
  77. case ASC:
  78. example.orderBy(order).asc();
  79. break;
  80. case DESC:
  81. default:
  82. example.orderBy(order).desc();
  83. }
  84. return select(userInvoiceMapper, example, page, size);
  85. }
  86. public void finish(Integer[] ids){
  87. Example example = new Example(UserInvoice.class);
  88. example.and(
  89. example.createCriteria().andIn("id", Arrays.stream(ids).collect(Collectors.toList()))
  90. );
  91. update(userInvoiceMapper, example, UserInvoice.builder().isFinish(1).build());
  92. }
  93. public void download(Integer[] ids){
  94. Example example = new Example(UserInvoice.class);
  95. example.and(
  96. example.createCriteria().andIn("id", Arrays.stream(ids).collect(Collectors.toList()))
  97. );
  98. update(userInvoiceMapper, example, UserInvoice.builder().isDownload(1).build());
  99. }
  100. public UserInvoice add(UserInvoice invoice){
  101. int result = insert(userInvoiceMapper, invoice);
  102. invoice = one(userInvoiceMapper, invoice.getId());
  103. if(invoice == null){
  104. throw new SystemException("发票添加失败");
  105. }
  106. return invoice;
  107. }
  108. public UserInvoice edit(UserInvoice invoice){
  109. UserInvoice in = one(userInvoiceMapper, invoice.getId());
  110. if(in == null){
  111. throw new ParameterException("发票不存在");
  112. }
  113. int result = update(userInvoiceMapper, invoice);
  114. return invoice;
  115. }
  116. public boolean delete(Number id){
  117. UserInvoice in = one(userInvoiceMapper, id);
  118. if(in == null){
  119. throw new ParameterException("发票不存在");
  120. }
  121. int result = delete(userInvoiceMapper, id);
  122. return result > 0;
  123. }
  124. public UserInvoice get(Number id){
  125. UserInvoice in = one(userInvoiceMapper, id);
  126. if(in == null){
  127. throw new ParameterException("发票不存在");
  128. }
  129. return in;
  130. }
  131. public Page<UserInvoice> select(int page, int pageSize){
  132. return select(userInvoiceMapper, page, pageSize);
  133. }
  134. public Page<UserInvoice> select(Integer[] ids){
  135. return page(()->select(userInvoiceMapper, ids), 1, ids.length);
  136. }
  137. public List<UserInvoice> select(Collection ids){
  138. return select(userInvoiceMapper, ids);
  139. }
  140. }