UserScoreDetail.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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(),$link_id=0)
  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['link_id'] = $link_id;
  111. $post['money'] = (float)(new User)->where('uid', $uid)->value('score') + $money;
  112. $post['o_id'] = empty($parms["o_id"])?0:$parms["o_id"];
  113. $this->insert($post);
  114. $bool = Db::name("user")->where('uid', $uid)->inc('score', floatval($money))->inc('score_in', floatval($money))->update();
  115. if ($bool > 0) {
  116. return true;
  117. } else {
  118. return false;
  119. }
  120. } catch (DbException $db) {
  121. return false;
  122. }
  123. }
  124. /**
  125. * 消耗积分
  126. * @param type $uid
  127. * @param type $money
  128. * @param type $mono
  129. */
  130. public function payScore($uid,$money,$code,$mono,$parms=array()){
  131. try {
  132. $content = $this->TplParam($this->config[$code]['content'],compact('money', 'mono'));
  133. $post['uid'] = $uid;
  134. $post['v'] = $money;
  135. $post['code'] = $code;
  136. $post['title'] = $this->config[$code]['code'];
  137. $post['content'] = $content;
  138. $post['type'] = -1;
  139. $post['time'] = time();
  140. $post['money'] = (float)(new User)->where('uid', $uid)->value('score') - $money;
  141. $post['o_id'] = empty($parms["o_id"])?0:$parms["o_id"];
  142. $this->insert($post);
  143. $bool = Db::name("user")->where('uid', $uid)->dec('score', floatval($money))->update();
  144. if ($bool > 0) {
  145. return true;
  146. } else {
  147. return false;
  148. }
  149. } catch (DbException $db) {
  150. return false;
  151. }
  152. }
  153. /**
  154. * 消耗积分,从冻结账户扣除
  155. * @param type $uid
  156. * @param type $money
  157. * @param type $mono
  158. */
  159. public function payFreezeScore($uid,$money,$code,$mono,$parms=[]){
  160. $content = $this->TplParam($this->config[$code]['content'],compact('money', 'mono'));
  161. $post['uid'] = $uid;
  162. $post['v'] = $money;
  163. $post['code'] = $code;
  164. $post['title'] = $this->config[$code]['code'];
  165. $post['content'] = $content;
  166. $post['type'] = -1;
  167. $post['time'] = time();
  168. $post['money'] = (float)(new User)->where('uid', $uid)->value('score');
  169. $this->insert($post);
  170. $bool = Db::name("user")->where('uid', $uid)->dec('score_freeze', floatval($money))->update();
  171. if ($bool > 0) {
  172. return true;
  173. } else {
  174. return false;
  175. }
  176. }
  177. /**
  178. * 转义的模板信息
  179. */
  180. private function TplParam($content)
  181. {
  182. $data = func_get_args();
  183. foreach ($data[1] as $k => $v) {
  184. $content = str_replace('{' . $k . '}', $v, $content);
  185. }
  186. return $content;
  187. }
  188. }