Wallet.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php
  2. namespace app\common\model;
  3. use think\Model;
  4. use think\Db;
  5. /**
  6. * 货币模型
  7. */
  8. class Wallet extends Model
  9. {
  10. // 日志变动类型
  11. const log_type = [
  12. 1 => '系统调节',//money + -
  13. 10 => '用户充值',//money + -
  14. 20 => '老年大学活动报名',//money + -
  15. //善豆
  16. 101 => '签到', //bean +
  17. ];
  18. // 操作钱包余额类型
  19. const money_type = [
  20. 'money' => '余额',
  21. 'bean' => '善豆',
  22. ];
  23. protected $message = '';
  24. protected $data = [];
  25. /**
  26. * 获取交易类型配置
  27. * @return mixed
  28. */
  29. public function getLogType($type = '')
  30. {
  31. $conf = self::log_type;
  32. if ($type) {
  33. return $conf[$type] ?: $type;
  34. }
  35. return $conf;
  36. }
  37. //获取钱包名称
  38. public function getWalletName($name = '')
  39. {
  40. $conf = self::money_type;
  41. if ($name) {
  42. return $conf[$name] ?: $name;
  43. }
  44. return $conf;
  45. }
  46. /**
  47. * 获取钱包余额
  48. * @param int $user_id 用户编号
  49. * @param string $wallet_name 指定钱包名
  50. * @return array|float
  51. */
  52. public function getWalletBak($user_id = 0, $wallet_name = '')
  53. {
  54. //所有钱包余额
  55. if (!$wallet = Db::name('user_wallet')->where(['user_id' => $user_id])->find()) {
  56. abort(500, '钱包余额获取失败');
  57. }
  58. if ($wallet_name) { //返回指定钱包
  59. return isset($wallet[$wallet_name]) ? $wallet[$wallet_name] : 0;
  60. } else { //返回所有钱包
  61. return $wallet;
  62. }
  63. }
  64. public function getWallet($user_id = 0, $wallet_name = 'money')
  65. {
  66. return Db::name('user')->where('id',$user_id)->value($wallet_name);
  67. }
  68. /**
  69. *
  70. * @param floatval $number 金额(正数进账,负数出账)
  71. * @param $accountType 货币类型,money,score
  72. * @param $logtype 日志的类型
  73. * @param $remark 备注
  74. * @param $user_id 用户id
  75. * @param $table 来源表
  76. * @param $data_id 表id
  77. * @param $isAdmin 是否是管理员处理
  78. * @return array
  79. * @return array[status]
  80. * @return array[msg]
  81. * @return array[log_table]
  82. * @return array[log_id]
  83. */
  84. public function lockChangeAccountRemain($user_id, $accountType = 'money', $number, $logtype = '', $remark = '', $table = '', $table_id = 0, $isAdmin = false)
  85. {
  86. //初始化
  87. $result = array(
  88. 'status' => false,
  89. 'msg' => '',
  90. 'log_table' => '',
  91. 'log_id' => '',
  92. );
  93. //获取小数点
  94. $point = 0;
  95. if(in_array($accountType,['money','bean'])){
  96. $point = 2;
  97. }
  98. bcscale($point);
  99. //钱包名称
  100. $wallet_name = $this->getWalletName($accountType);
  101. //检测
  102. $number = floatval($number);
  103. if ($number == 0) {
  104. $result['msg'] = '交易金额:0';
  105. return $result;
  106. }
  107. if (0 === bccomp($number, 0)) {
  108. $result['msg'] = '交易金额:0';
  109. return $result;
  110. }
  111. //检测
  112. $wallet = Db::name('user_wallet')->lock(true)->where(['user_id' => $user_id])->find();
  113. if (!$wallet) {
  114. $result['msg'] = '不存在的用户';
  115. return $result;
  116. }
  117. if (bccomp(bcadd($wallet[$accountType], $number), 0) === -1) {
  118. $result['msg'] = $wallet_name . '余额不足!';
  119. return $result;
  120. } else {
  121. if (0 !== bccomp($number, 0)) {
  122. //钱币记录
  123. $data = array();
  124. $data['user_id'] = $user_id;
  125. $data['log_type'] = $logtype;
  126. // $data['money_type'] = $accountType;
  127. $data['before'] = $wallet[$accountType];
  128. $data['change_value'] = $number;
  129. $data['remain'] = bcadd($wallet[$accountType], $number);
  130. $data['table'] = $table;
  131. $data['table_id'] = $table_id;
  132. $data['remark'] = $remark;
  133. $data['createtime'] = time();
  134. $data['updatetime'] = time();
  135. //新的方式
  136. $rs1 = Db::name('user_wallet')->where(['user_id' => $user_id])->update([$accountType => $data['remain']]);
  137. /////////////
  138. $log_table = 'user_' . $accountType . '_log';
  139. $rs2_id = Db::name($log_table)->insertGetId($data);
  140. if ($rs1 === false || $rs2_id === false) {
  141. $result['msg'] = '更新财务记录失败!';
  142. return $result;
  143. }
  144. if ($rs1 !== false && $rs2_id !== false) {
  145. $result['status'] = true;
  146. $result['msg'] = '账户余额已更新!';
  147. $result['log_table'] = $log_table;
  148. $result['log_id'] = $rs2_id;
  149. return $result;
  150. } else {
  151. $result['msg'] = '更新财务记录失败!';
  152. return $result;
  153. }
  154. } else {
  155. $result['msg'] = '金额不足0.01';
  156. return $result;
  157. }
  158. }
  159. }
  160. /**
  161. * 余额变更
  162. * @param int $user_id 用户id
  163. * @param float $number 金额(正数进账,负数出账)
  164. * @param string $accountType 货币类型 self::money_type
  165. * @param int $log_type 日志的类型 self::log_type
  166. * @param string $remark 备注
  167. * @param string $table 来源表
  168. * @param int $table_id 表id
  169. * @return bool
  170. */
  171. public function change(int $user_id, float $number, string $accountType = 'money', int $log_type = 0, string $remark = '', string $table = '', int $table_id = 0)
  172. {
  173. //获取小数点
  174. $point = 0;
  175. if(in_array($accountType,['money','bean'])){
  176. $point = 2;
  177. }
  178. bcscale($point);
  179. //钱包名称
  180. $wallet_name = $this->getWalletName($accountType);
  181. //检测
  182. if ($number == 0 || 0 === bccomp($number, 0)) {
  183. return $this->error('交易金额:0');
  184. }
  185. //检测
  186. if (!$wallet = Db::name('user_wallet')->lock(true)->where(['user_id' => $user_id])->find()) {
  187. return $this->error('不存在的用户');
  188. }
  189. if (bccomp(bcadd($wallet[$accountType], $number), 0) === -1) {
  190. return $this->error("{$wallet_name}余额不足!");
  191. }
  192. //钱币记录
  193. $data = [
  194. 'user_id' => $user_id,
  195. 'log_type' => $log_type,
  196. 'before' => $wallet[$accountType],
  197. 'change_value' => $number,
  198. 'remain' => bcadd($wallet[$accountType], $number),
  199. 'table' => $table,
  200. 'table_id' => $table_id,
  201. 'remark' => $remark,
  202. 'createtime' => time(),
  203. 'updatetime' => time(),
  204. ];
  205. //新的方式
  206. $upWallet = Db::name('user_wallet')->where(['user_id' => $user_id])->update([$accountType => $data['remain']]);
  207. if ($upWallet === false) {
  208. return $this->error("更新账户余额失败!");
  209. }
  210. if (!Db::name("user_{$accountType}_log")->insertGetId($data)) {
  211. return $this->error("更新财务记录失败!");
  212. }
  213. return $this->success("账户余额已更新!");
  214. }
  215. protected function success($message = '', $data = [])
  216. {
  217. $this->message = $message;
  218. $this->data = $data;
  219. return true;
  220. }
  221. protected function error($message = '', $data = [])
  222. {
  223. $this->message = $message;
  224. $this->data = $data;
  225. return false;
  226. }
  227. public function getMessage()
  228. {
  229. return $this->message;
  230. }
  231. public function getData($name = null)
  232. {
  233. $data = $this->data;
  234. !empty($name) && $data = $this->data[$name];
  235. return $data;
  236. }
  237. }