UserScoreDetail.php 7.1 KB

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