UserScoreDetail.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. declare (strict_types=1);
  3. namespace app\model\api;
  4. use app\model\api\User as UserModel;
  5. use library\basic\BaseModel;
  6. use think\facade\Db;
  7. use think\Model;
  8. /**
  9. * @mixin \think\Model
  10. */
  11. class UserScoreDetail extends BaseModel
  12. {
  13. private $config = [
  14. 'admin_score_add' => ["code" => "后台补加积分", "content" => "后台补加积分:{money}。"],
  15. 'admin_score_jdd' => ["code" => "后台补扣积分", "content" => "后台补扣积分:{money}。"],
  16. 'income_score' => ["code" => "商城下单赠送积分", "content" => "用户收入{money}积分"],
  17. 'subInfo_score' => ["code" => "推荐用户生成名片", "content" => "上级用户收入{money}积分"],
  18. 'outcome_score' => ["code" => "商城购买积分抵扣支出", "content" => "{mono},支出{money}积分"],
  19. ];
  20. /**
  21. * 后台充值积分余额
  22. * @param $uid
  23. * @param $money
  24. * @param $admin_id
  25. * @return bool
  26. * @throws \think\db\exception\DbException
  27. */
  28. public function adminAddMoney($uid, $money,$admin_id,$mono="")
  29. {
  30. try {
  31. self::beginTrans();
  32. $post['uid'] = $uid;
  33. $post['v'] = floatval($money);
  34. $post['code'] = 'admin_score_add';
  35. $post['title'] = $this->config['admin_score_add']['code'];
  36. $post['content'] = $this->TplParam($this->config['admin_score_add']['content'],compact('money'));
  37. $post['type'] = 1;
  38. $post['money'] = (float)(new UserModel)->where('uid', $uid)->value('score') + floatval($money);
  39. $post['time'] = time();
  40. $post['admin_id'] = $admin_id;
  41. $post['othen'] = $mono;
  42. $this->insert($post);
  43. $bool = Db::name("user")->where('uid', $uid)->inc('score', floatval($money))->update();
  44. self::commitTrans();
  45. if ($bool > 0) {
  46. return true;
  47. } else {
  48. return false;
  49. }
  50. } catch (DbException $db) {
  51. self::rollbackTrans();
  52. return false;
  53. }
  54. }
  55. /**
  56. * 后台扣除积分余额
  57. * @param $uid
  58. * @param $money
  59. * @param $admin_id
  60. * @return bool
  61. * @throws \think\db\exception\DbException
  62. */
  63. public function adminCutMoney($uid, $money,$admin_id,$mono="")
  64. {
  65. try {
  66. self::beginTrans();
  67. $post['uid'] = $uid;
  68. $post['v'] = floatval($money);
  69. $post['code'] = 'admin_score_jdd';
  70. $post['title'] = $this->config['admin_score_jdd']['code'];
  71. $post['content'] = $this->TplParam($this->config['admin_score_jdd']['content'],compact('money'));
  72. $post['type'] = -1;
  73. $post['money'] = (float)(new UserModel)->where('uid', $uid)->value('score') - floatval($money);
  74. $post['time'] = time();
  75. $post['admin_id'] = $admin_id;
  76. $post['othen'] = $mono;
  77. $this->insert($post);
  78. $bool = Db::name("user")->where('uid', $uid)->dec('score', floatval($money))->update();
  79. self::commitTrans();
  80. if ($bool > 0) {
  81. return true;
  82. } else {
  83. return false;
  84. }
  85. } catch (DbException $db) {
  86. self::rollbackTrans();
  87. return false;
  88. }
  89. }
  90. /**
  91. * 增加用户积分
  92. * @param $uid
  93. * @param $money
  94. * @param $orderSn
  95. * @return bool
  96. * @throws \think\db\exception\DbException
  97. */
  98. public function incomeScore($uid, $money, $orderSn="",$code="income_score",$parms=array())
  99. {
  100. try {
  101. $content = $this->TplParam($this->config[$code]['content'],compact('money'));
  102. $post['uid'] = $uid;
  103. $post['v'] = $money;
  104. $post['code'] = $code;
  105. $post['title'] = $this->config[$code]['code'];
  106. $post['content'] = $content;
  107. $post['type'] = 1;
  108. $post['time'] = time();
  109. $post['order_sn'] = $orderSn;
  110. $post['money'] = (float)(new User)->where('uid', $uid)->value('score') + $money;
  111. $post['o_id'] = empty($parms["o_id"])?0:$parms["o_id"];
  112. $this->insert($post);
  113. $bool = Db::name("user")->where('uid', $uid)->inc('score', floatval($money))->inc('score_in', floatval($money))->update();
  114. if ($bool > 0) {
  115. return true;
  116. } else {
  117. return false;
  118. }
  119. } catch (DbException $db) {
  120. return false;
  121. }
  122. }
  123. /**
  124. * 消耗积分
  125. * @param type $uid
  126. * @param type $money
  127. * @param type $mono
  128. */
  129. public function payScore($uid,$money,$code,$mono,$parms=array()){
  130. try {
  131. $content = $this->TplParam($this->config[$code]['content'],compact('money', 'mono'));
  132. $post['uid'] = $uid;
  133. $post['v'] = $money;
  134. $post['code'] = $code;
  135. $post['title'] = $this->config[$code]['code'];
  136. $post['content'] = $content;
  137. $post['type'] = -1;
  138. $post['time'] = time();
  139. $post['money'] = (float)(new User)->where('uid', $uid)->value('score') - $money;
  140. $post['o_id'] = empty($parms["o_id"])?0:$parms["o_id"];
  141. $this->insert($post);
  142. $bool = Db::name("user")->where('uid', $uid)->dec('score', floatval($money))->update();
  143. if ($bool > 0) {
  144. return true;
  145. } else {
  146. return false;
  147. }
  148. } catch (DbException $db) {
  149. return false;
  150. }
  151. }
  152. /**
  153. * 消耗积分,从冻结账户扣除
  154. * @param type $uid
  155. * @param type $money
  156. * @param type $mono
  157. */
  158. public function payFreezeScore($uid,$money,$code,$mono,$parms=[]){
  159. $content = $this->TplParam($this->config[$code]['content'],compact('money', 'mono'));
  160. $post['uid'] = $uid;
  161. $post['v'] = $money;
  162. $post['code'] = $code;
  163. $post['title'] = $this->config[$code]['code'];
  164. $post['content'] = $content;
  165. $post['type'] = -1;
  166. $post['time'] = time();
  167. $post['money'] = (float)(new User)->where('uid', $uid)->value('score');
  168. $this->insert($post);
  169. $bool = Db::name("user")->where('uid', $uid)->dec('score_freeze', floatval($money))->update();
  170. if ($bool > 0) {
  171. return true;
  172. } else {
  173. return false;
  174. }
  175. }
  176. /**
  177. * 转义的模板信息
  178. */
  179. private function TplParam($content)
  180. {
  181. $data = func_get_args();
  182. foreach ($data[1] as $k => $v) {
  183. $content = str_replace('{' . $k . '}', $v, $content);
  184. }
  185. return $content;
  186. }
  187. }