Wallet.php 7.8 KB

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