ShopWalletBusiness.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace app\common\business;
  3. use app\common\model\BillModel;
  4. use app\common\model\HotelCanteenOrderModel;
  5. use app\common\model\HotelOrderModel;
  6. use app\common\model\OfflineShopOrderModel;
  7. use app\common\model\UniversityEventApplyModel;
  8. use app\utils\Common;
  9. use think\Db;
  10. class ShopWalletBusiness extends BusinessResult
  11. {
  12. // 日志变动类型
  13. const log_type = [
  14. 1 => '系统调节',//money + -
  15. 40 => '酒店预定',//money + -
  16. 50 => '餐厅预定',//money + -
  17. 60 => '老年大学活动报名',//money + -
  18. 70 => '线下消费',//money + -
  19. 80 => '旅游预定',//money + -
  20. ];
  21. /**
  22. * 主表订单对应各自店铺表
  23. */
  24. public const WALLET_TABLE = [
  25. 'hotel_order' => 'hotel_wallet',
  26. 'hotel_canteen_order' => 'hotel_canteen_wallet',
  27. 'university_event_apply' => 'university_event_wallet',
  28. 'offline_shop_order' => 'offline_shop_wallet',
  29. 'travel_order' => 'travel_wallet',
  30. ];
  31. /**
  32. * 主表订单对应各自店铺表
  33. */
  34. public const LOG_TABLE = [
  35. 'hotel_wallet' => 'hotel',
  36. 'hotel_canteen_wallet' => 'hotel_canteen',
  37. 'university_event_wallet' => 'university_event',
  38. 'offline_shop_wallet' => 'offline_shop',
  39. 'travel_wallet' => 'travel',
  40. ];
  41. // 操作钱包余额类型
  42. const money_type = [
  43. 'money' => '40期收益',
  44. 'balance' => '余额',
  45. 'points' => '让利积分',
  46. ];
  47. protected string $walletName = '';
  48. public function __construct($orderName) {
  49. $this->walletName = self::WALLET_TABLE[$orderName];
  50. }
  51. //获取钱包名称
  52. public function getWalletName($name = '')
  53. {
  54. $conf = self::money_type;
  55. if ($name) {
  56. return $conf[$name] ?: $name;
  57. }
  58. return $conf;
  59. }
  60. /**
  61. * 余额变更
  62. * @param int $shop_id 店铺id
  63. * @param float $number 金额(正数进账,负数出账)
  64. * @param string $accountType 货币类型 self::money_type
  65. * @param int $log_type 日志的类型 self::log_type
  66. * @param string $remark 备注
  67. * @param string $table 来源表
  68. * @param int $table_id 表id
  69. * @return bool
  70. */
  71. public function change(int $shop_id, float $number, string $accountType = 'money', int $log_type = 0, string $remark = '', string $table = '', int $table_id = 0)
  72. {
  73. //获取小数点
  74. $point = 0;
  75. if(in_array($accountType,array_keys(self::money_type))){
  76. $point = 2;
  77. }
  78. bcscale($point);
  79. //钱包名称
  80. $wallet_name = $this->getWalletName($accountType);
  81. //检测
  82. if ($number == 0 || 0 === bccomp($number, 0)) {
  83. return $this->error('交易金额:0');
  84. }
  85. //检测
  86. if (!$wallet = Db::name($this->walletName)->lock(true)->where(['shop_id' => $shop_id])->find()) {
  87. return $this->error('不存在的用户');
  88. }
  89. if (bccomp(bcadd($wallet[$accountType], $number), 0) === -1) {
  90. return $this->error("{$wallet_name}余额不足!");
  91. }
  92. //钱币记录
  93. $data = [
  94. 'shop_id' => $shop_id,
  95. 'log_type' => $log_type,
  96. 'before' => $wallet[$accountType],
  97. 'change_value' => $number,
  98. 'remain' => bcadd($wallet[$accountType], $number),
  99. 'table' => $table,
  100. 'table_id' => $table_id,
  101. 'remark' => $remark,
  102. 'createtime' => time(),
  103. 'updatetime' => time(),
  104. ];
  105. //新的方式
  106. $upWallet = Db::name($this->walletName)->where(['shop_id' => $shop_id])->update([$accountType => $data['remain']]);
  107. if ($upWallet === false) {
  108. return $this->error("更新账户余额失败!");
  109. }
  110. // 插入日志
  111. $left_table = self::LOG_TABLE[$this->walletName];
  112. $logTableName = "{$left_table}_{$accountType}_log";
  113. if (!Db::name($logTableName)->insertGetId($data)) {
  114. return $this->error("更新财务记录失败!");
  115. }
  116. return $this->success("账户余额已更新!");
  117. }
  118. }