Wallet.php 8.1 KB

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