AppUserController.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. package com.game.business.controller;
  2. import java.math.BigDecimal;
  3. import java.util.Date;
  4. import java.util.List;
  5. import javax.servlet.http.HttpServletResponse;
  6. import cn.hutool.core.util.IdUtil;
  7. import cn.hutool.core.util.RandomUtil;
  8. import com.game.business.domain.AppChargeChannel;
  9. import com.game.business.domain.AppUsersCharge;
  10. import com.game.business.domain.FinTranRecord;
  11. import com.game.business.dto.RestPwdDto;
  12. import com.game.business.dto.UserChargeDto;
  13. import com.game.business.service.IAppChargeChannelService;
  14. import com.game.business.service.IAppUsersChargeService;
  15. import com.game.business.util.Md5Utils;
  16. import com.game.common.constant.AppSceneType;
  17. import com.game.common.constant.finance.FinTranAddedInfo;
  18. import com.game.common.constant.finance.FinTranType1;
  19. import com.game.common.constant.finance.FinTranType3;
  20. import com.game.common.constant.finance.TranCurrencyType;
  21. import com.game.common.core.domain.HttpRet;
  22. import com.game.common.core.domain.R;
  23. import com.game.common.core.redis.RedisCache;
  24. import com.game.common.utils.SecurityUtils;
  25. import lombok.extern.log4j.Log4j2;
  26. import org.apache.commons.lang3.StringUtils;
  27. import org.springframework.beans.BeanUtils;
  28. import org.springframework.security.access.prepost.PreAuthorize;
  29. import org.springframework.beans.factory.annotation.Autowired;
  30. import org.springframework.validation.annotation.Validated;
  31. import org.springframework.web.bind.annotation.GetMapping;
  32. import org.springframework.web.bind.annotation.PostMapping;
  33. import org.springframework.web.bind.annotation.PutMapping;
  34. import org.springframework.web.bind.annotation.DeleteMapping;
  35. import org.springframework.web.bind.annotation.PathVariable;
  36. import org.springframework.web.bind.annotation.RequestBody;
  37. import org.springframework.web.bind.annotation.RequestMapping;
  38. import org.springframework.web.bind.annotation.RestController;
  39. import com.game.common.annotation.Log;
  40. import com.game.common.core.controller.BaseController;
  41. import com.game.common.core.domain.AjaxResult;
  42. import com.game.common.enums.BusinessType;
  43. import com.game.common.annotation.DataSource;
  44. import com.game.common.enums.DataSourceType;
  45. import io.swagger.annotations.Api;
  46. import io.swagger.annotations.ApiOperation;
  47. import com.game.business.domain.AppUser;
  48. import com.game.business.service.IAppUserService;
  49. import com.game.common.utils.poi.ExcelUtil;
  50. import com.game.common.core.page.TableDataInfo;
  51. /**
  52. * app用户Controller
  53. *
  54. * @author game
  55. * @date 2024-06-18
  56. */
  57. @RestController
  58. @RequestMapping("/business/user")
  59. @Api(value = "AppUserController", description = "app用户接口", tags = {"app用户"})
  60. @Log4j2
  61. public class AppUserController extends BaseController
  62. {
  63. @Autowired
  64. private IAppUserService appUserService;
  65. @Autowired
  66. private IAppUsersChargeService appUsersChargeService;
  67. @Autowired
  68. private RedisCache redisCache;
  69. @Autowired
  70. private IAppChargeChannelService appChargeChannelService;
  71. /**
  72. * 查询app用户列表
  73. */
  74. @PreAuthorize("@ss.hasPermi('business:user:list')")
  75. @GetMapping("/list")
  76. @ApiOperation(value = "查询app用户列表", notes = "获取app用户列表")
  77. public TableDataInfo list(AppUser appUser)
  78. {
  79. startPage();
  80. List<AppUser> list = appUserService.selectAppUserList(appUser);
  81. return getDataTable(list);
  82. }
  83. /**
  84. * 重置密码
  85. */
  86. @PreAuthorize("@ss.hasPermi('business:user:restPwd')")
  87. @Log(title = "app用户", businessType = BusinessType.UPDATE)
  88. @PostMapping("/restPwd")
  89. @ApiOperation(value = "重置密码", notes = "重置密码")
  90. public R<String> restPwd(HttpServletResponse response,@RequestBody @Validated RestPwdDto restPwdDto)
  91. {
  92. AppUser appUser = appUserService.selectAppUserByUserid(restPwdDto.getUserId());
  93. if(null == appUser){
  94. return R.fail("重置失败,用户不存在");
  95. }
  96. AppUser updateUser = new AppUser();
  97. updateUser.setUserid(appUser.getUserid());
  98. updateUser.setSalt(appUser.getSalt());
  99. if(StringUtils.isBlank(updateUser.getSalt())){
  100. updateUser.setSalt(RandomUtil.randomString(6));
  101. }
  102. updateUser.setPassword(Md5Utils.md5(Md5Utils.md5(restPwdDto.getPassword()) + updateUser.getSalt()));
  103. appUserService.updateAppUser(updateUser);
  104. //清除用户缓存
  105. redisCache.deleteObject("U:UserInfo:".concat(String.valueOf(appUser.getUserid())));
  106. return R.ok("重置成功");
  107. }
  108. /**
  109. * 充值
  110. */
  111. @PreAuthorize("@ss.hasPermi('business:user:charge')")
  112. @Log(title = "app用户", businessType = BusinessType.UPDATE)
  113. @PostMapping("/charge")
  114. @ApiOperation(value = "充值", notes = "充值")
  115. public R<String> charge(HttpServletResponse response,@RequestBody @Validated UserChargeDto userChargeDto)
  116. {
  117. AppUser appUser = appUserService.selectAppUserByUserid(userChargeDto.getUserId());
  118. if(null == appUser){
  119. return R.fail("充值失败,用户不存在");
  120. }
  121. String remark = "后台充值";
  122. if(null == userChargeDto.getChannelId() || -1 == userChargeDto.getChannelId().longValue()){
  123. remark = "后台充值";
  124. }else{
  125. AppChargeChannel channel = appChargeChannelService.selectAppChargeChannelById(userChargeDto.getChannelId());
  126. if(null == channel){
  127. return R.fail("充值渠道错误");
  128. }
  129. userChargeDto.setRate(channel.getRate());
  130. remark = "后台充值补单,渠道:".concat(channel.getName());
  131. }
  132. /*if(StringUtils.isNotBlank(userChargeDto.getRemark())){
  133. String r = userChargeDto.getRemark().replace("后台充值","");
  134. remark = remark.concat(",").concat(r);
  135. }*/
  136. //流水 余额
  137. FinTranAddedInfo addedInfo = FinTranAddedInfo.createTranInfo(appUser.getUserid(), 0, 0, AppSceneType.Scene_None, "");
  138. FinTranRecord tran = null;
  139. if(userChargeDto.getType() == 0){
  140. tran = FinTranRecord.initFinTranRecordSomeParams(addedInfo, FinTranType3.CHARGE_IN_Back, userChargeDto.getAmount()<0?FinTranType1.U_Outcome_Coin:FinTranType1.U_Income_Coin, appUser);
  141. tran.setCoinChange(userChargeDto.getAmount());
  142. tran.setCurrencyType(TranCurrencyType.Coin.getType());
  143. }else{
  144. tran = FinTranRecord.initFinTranRecordSomeParams(addedInfo, FinTranType3.CHARGE_IN_Back, userChargeDto.getAmount()<0?FinTranType1.U_Outcome_Balance:FinTranType1.U_Income_Coin_Balance, appUser);
  145. tran.setDiamondCoinChange(userChargeDto.getAmount());
  146. tran.setCurrencyType(TranCurrencyType.Balance.getType());
  147. }
  148. tran.setRemarks(remark);
  149. try {
  150. appUserService.updateUserAmount(tran);
  151. BigDecimal rate = BigDecimal.ZERO;
  152. if(null != userChargeDto.getRate() && userChargeDto.getRate() > 0 && userChargeDto.getAmount() > 0){
  153. if(userChargeDto.getRate() > 1){
  154. return R.fail("超过最大手续费扣除");
  155. }
  156. //扣除手续费
  157. FinTranRecord rateTran = new FinTranRecord();
  158. rateTran.setId(null);
  159. BeanUtils.copyProperties(tran,rateTran);
  160. if(TranCurrencyType.Balance.getType() == rateTran.getCurrencyType()){
  161. rate = BigDecimal.valueOf(rateTran.getDiamondCoinChange() * userChargeDto.getRate()).setScale(2,BigDecimal.ROUND_HALF_UP);
  162. rateTran.setDiamondCoinChange(rate.doubleValue() * -1);
  163. }else if(TranCurrencyType.Coin.getType() == rateTran.getCurrencyType()){
  164. rate = BigDecimal.valueOf(rateTran.getCoinChange() * userChargeDto.getRate()).setScale(2,BigDecimal.ROUND_HALF_UP);
  165. rateTran.setCoinChange(rate.doubleValue() * -1);
  166. }
  167. /*rateTran.setTranType3(FinTranType3.CHARGE_OUT_RATE.getType());
  168. rateTran.setTranType2(FinTranType3.CHARGE_OUT_RATE.getTranType2().getType());
  169. rateTran.setTranType1(userChargeDto.getType() == 1?FinTranType1.U_Outcome_Balance.getType():FinTranType1.U_Outcome_Coin.getType());
  170. rateTran.setRemarks("充值手续费");
  171. appUserService.updateUserAmount(rateTran);*/
  172. }
  173. double give = 0;
  174. // if(null != userChargeDto.getGiveAmount() && userChargeDto.getGiveAmount() > 0){
  175. //今日已赠送金额
  176. double todayGiveMoney = appUsersChargeService.isChargeToday(userChargeDto.getUserId());
  177. double giveRate = todayGiveMoney>0?0.02:0.1; //当然首充10% 否则2%
  178. if(todayGiveMoney < 2000) { //当日2000封顶
  179. give = BigDecimal.valueOf(userChargeDto.getAmount().doubleValue()
  180. * giveRate).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
  181. if((give + todayGiveMoney) > 2000){
  182. give = give - (give + todayGiveMoney - 2000); //赠送金额超过2000则扣减多出的余额
  183. }
  184. //赠送
  185. FinTranRecord giveTran = new FinTranRecord();
  186. BeanUtils.copyProperties(tran, giveTran);
  187. if (TranCurrencyType.Balance.getType() == giveTran.getCurrencyType()) {
  188. giveTran.setDiamondCoinChange(give);
  189. } else if (TranCurrencyType.Coin.getType() == giveTran.getCurrencyType()) {
  190. giveTran.setCoinChange(give);
  191. }
  192. giveTran.setTranType3(FinTranType3.CHARGE_IN_SEND.getType());
  193. giveTran.setTranType2(FinTranType3.CHARGE_IN_SEND.getTranType2().getType());
  194. giveTran.setTranType1(userChargeDto.getType() == 1 ? FinTranType1.U_Income_Coin_Balance.getType() : FinTranType1.U_Income_Coin.getType());
  195. giveTran.setRemarks("充值赠送");
  196. giveTran.setId(null);
  197. appUserService.updateUserAmount(giveTran);
  198. }
  199. // }
  200. AppUser afterUser = appUserService.selectAppUserByUserid(userChargeDto.getUserId());
  201. //充值记录
  202. AppUsersCharge appUsersCharge = new AppUsersCharge();
  203. // appUsersCharge.id = charge.id;
  204. appUsersCharge.setTouid(appUser.getUserid());
  205. appUsersCharge.setUid(appUser.getUserid());
  206. appUsersCharge.setCoin(BigDecimal.valueOf(userChargeDto.getAmount()));
  207. appUsersCharge.setCoinType(Long.parseLong(String.valueOf(userChargeDto.getType())));
  208. appUsersCharge.setOrderno(IdUtil.fastUUID());
  209. // 充值成功
  210. appUsersCharge.setStatus(1L);
  211. // 人工充值
  212. appUsersCharge.setType(4L);
  213. // 人工
  214. appUsersCharge.setAmbient(1L);
  215. // 操作人
  216. appUsersCharge.setOptUser(String.valueOf(SecurityUtils.getUserId()));
  217. // 操作类型 1:充值金币 2:扣减金币
  218. appUsersCharge.setOptType(userChargeDto.getAmount()<0?2L:1L);
  219. //赠送金额
  220. appUsersCharge.setCoinGive(BigDecimal.valueOf(give));
  221. appUsersCharge.setAddtime(new Date());
  222. appUsersCharge.setPlatformService(rate.doubleValue());
  223. appUsersCharge.setPlatformServiceRate(userChargeDto.getRate());
  224. appUsersCharge.setAfterCoin(userChargeDto.getType()==0?BigDecimal.valueOf(afterUser.getCoin()):BigDecimal.valueOf(afterUser.getDiamondCoin()));
  225. appUsersChargeService.insertAppUsersCharge(appUsersCharge);
  226. } catch (Exception e) {
  227. log.info("{}充值失败:{}",userChargeDto.getUserId(),e.getMessage());
  228. return R.fail("充值失败");
  229. }
  230. return R.ok("充值成功");
  231. }
  232. /**
  233. * 导出app用户列表
  234. */
  235. @PreAuthorize("@ss.hasPermi('business:user:export')")
  236. @Log(title = "app用户", businessType = BusinessType.EXPORT)
  237. @PostMapping("/export")
  238. @ApiOperation(value = "导出app用户列表", notes = "导出app用户列表")
  239. public void export(HttpServletResponse response, AppUser appUser)
  240. {
  241. List<AppUser> list = appUserService.selectAppUserList(appUser);
  242. ExcelUtil<AppUser> util = new ExcelUtil<AppUser>(AppUser.class);
  243. util.exportExcel(response, list, "app用户数据");
  244. }
  245. /**
  246. * 获取app用户详细信息
  247. */
  248. @PreAuthorize("@ss.hasPermi('business:user:query')")
  249. @GetMapping(value = "/{userid}")
  250. @ApiOperation(value = "获取app用户详细信息", notes = "获取app用户详细信息")
  251. public AjaxResult getInfo(@PathVariable("userid") Long userid)
  252. {
  253. return success(appUserService.selectAppUserByUserid(userid));
  254. }
  255. /**
  256. * 新增app用户
  257. */
  258. @PreAuthorize("@ss.hasPermi('business:user:add')")
  259. @Log(title = "app用户", businessType = BusinessType.INSERT)
  260. @ApiOperation(value = "新增app用户", notes = "新增app用户")
  261. @PostMapping
  262. public AjaxResult add(@RequestBody AppUser appUser)
  263. {
  264. return toAjax(appUserService.insertAppUser(appUser));
  265. }
  266. /**
  267. * 修改app用户
  268. */
  269. @PreAuthorize("@ss.hasPermi('business:user:edit')")
  270. @Log(title = "app用户", businessType = BusinessType.UPDATE)
  271. @ApiOperation(value = "修改app用户", notes = "修改app用户")
  272. @PutMapping
  273. public AjaxResult edit(@RequestBody AppUser appUser)
  274. {
  275. return toAjax(appUserService.updateAppUser(appUser));
  276. }
  277. /**
  278. * 删除app用户
  279. */
  280. @PreAuthorize("@ss.hasPermi('business:user:remove')")
  281. @Log(title = "app用户", businessType = BusinessType.DELETE)
  282. @ApiOperation(value = "删除app用户", notes = "删除app用户")
  283. @DeleteMapping("/{userids}")
  284. public AjaxResult remove(@PathVariable Long[] userids)
  285. {
  286. return toAjax(appUserService.deleteAppUserByUserids(userids));
  287. }
  288. }