RenwuMachineModel.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | EasyAdmin
  4. // +----------------------------------------------------------------------
  5. // | PHP交流群: 763822524
  6. // +----------------------------------------------------------------------
  7. // | 开源协议 https://mit-license.org
  8. // +----------------------------------------------------------------------
  9. // | github开源项目:https://github.com/zhongshaofa/EasyAdmin
  10. // +----------------------------------------------------------------------
  11. namespace app\common\model;
  12. use app\common\model\TimeModel;
  13. use think\db\builder\Oracle;
  14. use think\Exception;
  15. use think\facade\Db;
  16. class RenwuMachineModel extends TimeModel
  17. {
  18. protected $name = 'renwu_machine';
  19. public function exchangeRenwuMacheine($uid, $rid){
  20. $info = self::where('id', $rid)->find();
  21. if (!$info){
  22. throw new Exception('参数错误');
  23. }
  24. if ($info['score'] < 1){
  25. throw new Exception('参数错误');
  26. }
  27. $m_user = new UserModel();
  28. $m_mlist = new RenwuMachineListModel();
  29. $user = $m_user->getUserInfo(['id'=>$uid], 'score,id,is_auth');
  30. if ($user['is_auth'] != 1){
  31. throw new Exception('还未进行实名认证');
  32. }
  33. //============================================================
  34. // $have_count = $m_mlist->where('uid', $uid)->where('rid', $rid)->where('state', 1)->count();
  35. // if ($have_count == $info['max_have']){
  36. // throw new Exception('超过最大持有量,购买失败');
  37. // }
  38. // 2022、/04、/23 改为用户只能买一次
  39. $has = $m_mlist->where('uid', $uid)->where('rid', $rid)->find();
  40. if ($has){
  41. throw new Exception('超过最大持有量,购买失败');
  42. }
  43. //============================================================
  44. if ($user['score'] < $info['score']){
  45. throw new Exception('积分不足');
  46. }
  47. $time = time();
  48. $m_mlist->insert([
  49. 'rid'=>$rid,
  50. 'create_time'=>sr_getcurtime($time),
  51. 'expire_time'=> sr_getcurtime(strtotime("+ ".$info['valid_day']." days")),
  52. 'back_count'=>$info['cycle'],
  53. // 'once_back'=>number_format($info['total_score']/$info['cycle'], '4', '.', ''),
  54. 'once_back'=>$info['once_back'],
  55. 'over_count'=>$info['cycle'],
  56. 'uid'=>$uid
  57. ]);
  58. edit_user_score(13, $uid, $info['score']);
  59. }
  60. public function getRenwuMachineAward($uid, $id){
  61. $m_list = new RenwuMachineListModel();
  62. $m_log = new RenwuMachineGetrecordModel();
  63. $info_list = $m_list->where('id', $id)->find();
  64. if (!$info_list){
  65. throw new Exception('参数错误');
  66. }
  67. if ($info_list['state'] != 1 || $info_list['over_count'] < 1){
  68. throw new Exception('已过期');
  69. }
  70. if ($info_list['uid'] != $uid){
  71. throw new Exception('请求非法');
  72. }
  73. getActionBefore(3);
  74. $get_info = $m_log->whereDay('create_time', 'today')->where('uid', $uid)->where('rid', $id)->find();
  75. if ($get_info){
  76. throw new Exception('今日已领取');
  77. }
  78. if ($info_list['get_count'] == $info_list['back_count']-1){
  79. $m_list->where('id', $id)->save(['state'=>2]);
  80. }
  81. $m_log->insert(['create_time'=>sr_getcurtime(time()), 'rid'=>$id, 'uid'=>$uid, 'num'=>$info_list['once_back']]);
  82. $m_list->where('id', $id)->inc('get_count', 1)->dec('over_count', 1)->update();
  83. edit_user_renwusocre(1, $uid, $info_list['once_back']);
  84. }
  85. }