UserScoreDetail.php 7.1 KB

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