Wallet.php 8.5 KB

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