UserScoreDetail.php 6.7 KB

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