IntegralServiceImpl.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. package com.willalp.integral.service.impl;
  2. import cn.hutool.core.collection.CollectionUtil;
  3. import com.baomidou.mybatisplus.core.toolkit.IdWorker;
  4. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  5. import com.willalp.common.core.domain.ReturnEntity;
  6. import com.willalp.common.core.domain.entity.SysDictData;
  7. import com.willalp.common.core.domain.entity.SysUser;
  8. import com.willalp.common.enums.RechargeEnum;
  9. import com.willalp.common.enums.RechargeTypeEnum;
  10. import com.willalp.common.enums.ReturnEnum;
  11. import com.willalp.common.exception.base.BaseException;
  12. import com.willalp.common.utils.DateUtils;
  13. import com.willalp.common.utils.ObjectToTypes;
  14. import com.willalp.common.utils.SecurityUtils;
  15. import com.willalp.common.utils.StringUtils;
  16. import com.willalp.files.domain.HsOrgPersonnelFiles;
  17. import com.willalp.files.mapper.HsOrgPersonnelFilesMapper;
  18. import com.willalp.integral.domain.Integral;
  19. import com.willalp.integral.domain.excelVo.IntegralVo;
  20. import com.willalp.integral.mapper.IntegralMapper;
  21. import com.willalp.integral.request.IntegralGroupQuery;
  22. import com.willalp.integral.service.IIntegralService;
  23. import com.willalp.system.domain.SysCardInfo;
  24. import com.willalp.system.domain.SysUserOrganization;
  25. import com.willalp.system.mapper.SysCardInfoMapper;
  26. import com.willalp.system.mapper.SysUserMapper;
  27. import com.willalp.system.mapper.SysUserOrganizationMapper;
  28. import com.willalp.system.service.ISysDictTypeService;
  29. import org.springframework.beans.factory.annotation.Autowired;
  30. import org.springframework.stereotype.Service;
  31. import org.springframework.transaction.annotation.Transactional;
  32. import javax.annotation.Resource;
  33. import java.math.BigDecimal;
  34. import java.util.ArrayList;
  35. import java.util.List;
  36. import java.util.Map;
  37. import java.util.stream.Collectors;
  38. /**
  39. * 积分管理Service业务层处理
  40. *
  41. * @author willalp
  42. * @date 2021-12-02
  43. */
  44. @Service
  45. public class IntegralServiceImpl extends ServiceImpl<IntegralMapper, Integral> implements IIntegralService {
  46. @Autowired
  47. private IntegralMapper integralMapper;
  48. @Autowired
  49. private SysUserMapper sysUserMapper;
  50. @Autowired
  51. private SysCardInfoMapper sysCardInfoMapper;
  52. @Autowired
  53. private SysUserOrganizationMapper sysUserOrganizationMapper;
  54. @Resource
  55. private HsOrgPersonnelFilesMapper opFilesMapper;
  56. /**
  57. * 查询积分管理列表
  58. *
  59. * @param integral 积分管理
  60. * @return 积分管理
  61. */
  62. @Override
  63. public List<Integral> selectIntegralList(Integral integral) {
  64. return integralMapper.selectIntegralList(integral);
  65. }
  66. @Override
  67. @Transactional(rollbackFor = Exception.class)
  68. public int saveInfo(Integral integral) {
  69. List<SysUserOrganization> list = sysUserOrganizationMapper.queryOrganizationByLoginName(SecurityUtils.getUsername());
  70. if (!list.isEmpty()) {
  71. list = list.stream().filter(e -> e.getIsDefault() != null && e.getIsDefault() == 1).collect(Collectors.toList());
  72. if (!list.isEmpty()) {
  73. integral.setOrganizationCode(list.get(0).getOrganizationCode());
  74. }
  75. }
  76. SysCardInfo cardInfo = new SysCardInfo();
  77. cardInfo.setUserId(integral.getLoginName());
  78. List<SysCardInfo> cardInfoList = sysCardInfoMapper.selectSysCardInfoList(cardInfo);
  79. if (!cardInfoList.isEmpty()) {
  80. integral.setCardNumber(cardInfoList.get(0).getCardCode());
  81. integral.setCardName(cardInfoList.get(0).getCardUsername());
  82. }
  83. while (true) {
  84. String idStr = IdWorker.getIdStr();
  85. Integral byId = integralMapper.selectById(idStr);
  86. if (null == byId) {
  87. integral.setId(idStr);
  88. break;
  89. }
  90. }
  91. integral.setIsDelete(0);
  92. integral.setRechargeTime(DateUtils.getNowDate());
  93. integral.setRechargeId(SecurityUtils.getLoginUser().getUserId());
  94. //账户
  95. SysUserOrganization organization = new SysUserOrganization();
  96. organization.setLoginName(integral.getLoginName());
  97. organization.setOrganizationCode(integral.getOrganizationCode());
  98. List<SysUserOrganization> organs = sysUserOrganizationMapper.selectSysUserOrganizationList(organization);
  99. if (organs.size() > 0) {
  100. organization = organs.get(0);
  101. } else {
  102. throw new BaseException("当前用户不存在机构权限");
  103. }
  104. //账户增加余额
  105. if (integral.getMoneyType() == Integer.valueOf(RechargeEnum.ORGANRECHARGE.getValue())
  106. || integral.getMoneyType() == Integer.valueOf(RechargeEnum.PERSONRECHARGE.getValue())) {
  107. organization.setIntegral(organization.getIntegral().add(integral.getRechargeIntegral()));
  108. } else if (integral.getMoneyType() == Integer.valueOf(RechargeEnum.KEEPRECHARGE.getValue())) {//记账
  109. organization.setQuota(organization.getQuota().add(integral.getRechargeIntegral()));
  110. } else if (integral.getMoneyType() == Integer.valueOf(RechargeEnum.DEPOSITRECHARGE.getValue())) {//押金
  111. organization.setDeposit(organization.getDeposit().add(integral.getRechargeIntegral()));
  112. } else if (integral.getMoneyType() == Integer.valueOf(ReturnEnum.RETURN.getValue())) {//余额退还
  113. if (organization.getIntegral().compareTo(integral.getRechargeIntegral()) < 0) {
  114. throw new BaseException("余额不足");
  115. }
  116. organization.setIntegral(organization.getIntegral().subtract(integral.getRechargeIntegral()));
  117. integral.setRechargeIntegral(integral.getRechargeIntegral().negate());
  118. } else if (integral.getMoneyType() == Integer.valueOf(ReturnEnum.KEEPRETURN.getValue())) {//记账额度退还
  119. if (organization.getQuota().compareTo(integral.getRechargeIntegral()) < 0) {
  120. throw new BaseException("记账额度不足");
  121. }
  122. organization.setQuota(organization.getQuota().subtract(integral.getRechargeIntegral()));
  123. integral.setRechargeIntegral(integral.getRechargeIntegral().negate());
  124. } else if (integral.getMoneyType() == Integer.valueOf(ReturnEnum.DEPOSITRETURN.getValue())) {//押金
  125. if (organization.getDeposit().compareTo(integral.getRechargeIntegral()) < 0) {
  126. throw new BaseException("押金不足");
  127. }
  128. organization.setDeposit(organization.getDeposit().subtract(integral.getRechargeIntegral()));
  129. integral.setRechargeIntegral(integral.getRechargeIntegral().negate());
  130. } else if (integral.getMoneyType() == Integer.valueOf(ReturnEnum.RETURN.getValue())) {//因错退还
  131. }
  132. sysUserOrganizationMapper.updateById(organization);
  133. return integralMapper.insert(integral);
  134. }
  135. @Override
  136. public void returnPoints(List<IntegralGroupQuery> list) {
  137. List<String> depositList = list.stream().filter(e -> e.getMoneyType() == 1).map(o -> o.getLoginName()).collect(Collectors.toList());
  138. List<String> balanceList = list.stream().filter(e -> e.getMoneyType() == 2).map(o -> o.getLoginName()).collect(Collectors.toList());
  139. IntegralGroupQuery integralGroupQuery = new IntegralGroupQuery();
  140. List<Integral> returnList = new ArrayList<>();
  141. if (!depositList.isEmpty()) {
  142. integralGroupQuery.setLoginNameList(depositList);
  143. integralGroupQuery.setMoneyType(1);
  144. returnList.addAll(integralMapper.queryIntegralSum(integralGroupQuery));
  145. }
  146. if (!balanceList.isEmpty()) {
  147. integralGroupQuery.setLoginNameList(balanceList);
  148. integralGroupQuery.setMoneyType(2);
  149. returnList.addAll(integralMapper.queryIntegralSum(integralGroupQuery));
  150. }
  151. returnList.forEach(item -> {
  152. if (item.getMoneyType() != 1) {
  153. SysUserOrganization organization = new SysUserOrganization();
  154. organization.setLoginName(item.getLoginName());
  155. organization.setOrganizationCode(item.getOrganizationCode());
  156. List<SysUserOrganization> organs = sysUserOrganizationMapper.selectSysUserOrganizationList(organization);
  157. if (CollectionUtil.isNotEmpty(organs)) {
  158. organization = organs.get(0);
  159. if (organization.getIntegral().compareTo(item.getRechargeIntegral()) >= 0) {
  160. organization.setIntegral(organization.getIntegral().subtract(item.getRechargeIntegral()));
  161. sysUserOrganizationMapper.updateById(organization);
  162. } else {
  163. throw new BaseException(item.getNickName() + "积分余额不足,不能作废");
  164. }
  165. } else {
  166. throw new BaseException(item.getNickName() + "存在用户信息");
  167. }
  168. }
  169. while (true) {
  170. String idStr = IdWorker.getIdStr();
  171. Integral byId = integralMapper.selectById(idStr);
  172. if (null == byId) {
  173. item.setId(idStr);
  174. break;
  175. }
  176. }
  177. if (item.getMoneyType() != null && item.getMoneyType() != 1) {
  178. item.setMoneyType(RechargeTypeEnum.returnPoints.getCode());
  179. }
  180. item.setRechargeIntegral(item.getRechargeIntegral().multiply(new BigDecimal(-1)));
  181. item.setIsDelete(0);
  182. item.setRechargeTime(DateUtils.getNowDate());
  183. item.setRechargeId(SecurityUtils.getLoginUser().getUserId());
  184. });
  185. integralMapper.saveBatchIntegral(returnList);
  186. }
  187. @Override
  188. public String importData(List<Integral> list) {
  189. int successNum = 0;
  190. int failureNum = 0;
  191. StringBuilder failureMsg = new StringBuilder();
  192. for (Integral integral : list) {
  193. try {
  194. SysUser user = sysUserMapper.checkPhoneUnique(integral.getPhonenumber());
  195. if (user == null) {
  196. failureNum++;
  197. failureMsg.append(integral.getPhonenumber() + "不存在用户信息");
  198. continue;
  199. }
  200. integral.setNickName(user.getNickName());
  201. integral.setLoginName(user.getUserName());
  202. integral.setMoneyType(RechargeTypeEnum.deposit.getCode());
  203. int count = saveInfo(integral);
  204. if (count > 0) {
  205. successNum++;
  206. }
  207. } catch (Exception e) {
  208. failureNum++;
  209. log.error(e.getMessage(), e);
  210. failureMsg.append(e.getMessage());
  211. }
  212. }
  213. ReturnEntity<String> res = new ReturnEntity<>();
  214. if (failureNum > 0) {
  215. res.setReturnMessage("pns.integral.errorCode1", new String[]{
  216. ObjectToTypes.parseString(failureNum), failureMsg.toString()
  217. });
  218. } else {
  219. res.setReturnMessage("pns.integral.successCode1", new String[]{
  220. ObjectToTypes.parseString(successNum)
  221. });
  222. }
  223. return res.getMessage();
  224. }
  225. @Override
  226. public List<Integral> queryIntegralSum(IntegralGroupQuery integralGroupQuery) {
  227. return integralMapper.userBalances(integralGroupQuery);
  228. }
  229. @Override
  230. public List<Map<String, Object>> getIntegralReportList(Map<String, Object> req) {
  231. return integralMapper.getIntegralReportList(req);
  232. }
  233. @Resource
  234. private ISysDictTypeService dictTypeService;
  235. @Override
  236. public List<IntegralVo> rechargeTemplate() {
  237. List<HsOrgPersonnelFiles> hsOrgPersonnelFiles = opFilesMapper.templateHsOrgPersonnelFilesList();
  238. List<SysDictData> userTypes = dictTypeService.selectDictDataByType("user_type");
  239. List<IntegralVo> list = new ArrayList<>();
  240. if (0 != hsOrgPersonnelFiles.size()) {
  241. hsOrgPersonnelFiles.forEach(item -> {
  242. IntegralVo integral = new IntegralVo();
  243. integral.setNickName(item.getUserName());
  244. if (StringUtils.isNotBlank(item.getUserType())) {
  245. SysDictData sysDictData = userTypes.get(Integer.parseInt(item.getUserType()) - 1);
  246. if (sysDictData != null) {
  247. integral.setUserType(sysDictData.getDictLabel());
  248. } else {
  249. integral.setUserType("未设置");
  250. }
  251. }
  252. integral.setPhonenumber(item.getPhone());
  253. list.add(integral);
  254. });
  255. }
  256. return list;
  257. }
  258. }