ShopWalletBusiness.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. ];
  20. /**
  21. * 主表订单对应各自店铺表
  22. */
  23. public const WALLET_TABLE = [
  24. 'hotel_order' => 'hotel_wallet',
  25. 'hotel_canteen_order' => 'hotel_canteen_wallet',
  26. 'university_event_apply' => 'university_event_wallet',
  27. 'offline_shop_order' => 'offline_shop_wallet',
  28. 'travel_order' => 'travel_wallet',
  29. ];
  30. /**
  31. * 主表订单对应各自店铺表
  32. */
  33. public const LOG_TABLE = [
  34. 'hotel_wallet' => 'hotel_money_log',
  35. 'hotel_canteen_wallet' => 'hotel_canteen_money_log',
  36. 'university_event_wallet' => 'university_event_money_log',
  37. 'offline_shop_wallet' => 'offline_shop_money_log',
  38. 'travel_wallet' => 'travel_money_log',
  39. ];
  40. // 操作钱包余额类型
  41. const money_type = [
  42. 'money' => '余额',
  43. ];
  44. protected string $walletName = '';
  45. public function __construct($orderName) {
  46. $this->walletName = self::WALLET_TABLE[$orderName];
  47. }
  48. //获取钱包名称
  49. public function getWalletName($name = '')
  50. {
  51. $conf = self::money_type;
  52. if ($name) {
  53. return $conf[$name] ?: $name;
  54. }
  55. return $conf;
  56. }
  57. /**
  58. * 余额变更
  59. * @param int $shop_id 店铺id
  60. * @param float $number 金额(正数进账,负数出账)
  61. * @param string $accountType 货币类型 self::money_type
  62. * @param int $log_type 日志的类型 self::log_type
  63. * @param string $remark 备注
  64. * @param string $table 来源表
  65. * @param int $table_id 表id
  66. * @return bool
  67. */
  68. public function change(int $shop_id, float $number, string $accountType = 'money', int $log_type = 0, string $remark = '', string $table = '', int $table_id = 0)
  69. {
  70. //获取小数点
  71. $point = 0;
  72. if(in_array($accountType,['money','bean'])){
  73. $point = 2;
  74. }
  75. bcscale($point);
  76. //钱包名称
  77. $wallet_name = $this->getWalletName($accountType);
  78. //检测
  79. if ($number == 0 || 0 === bccomp($number, 0)) {
  80. return $this->error('交易金额:0');
  81. }
  82. //检测
  83. if (!$wallet = Db::name($this->walletName)->lock(true)->where(['shop_id' => $shop_id])->find()) {
  84. return $this->error('不存在的用户');
  85. }
  86. if (bccomp(bcadd($wallet[$accountType], $number), 0) === -1) {
  87. return $this->error("{$wallet_name}余额不足!");
  88. }
  89. //钱币记录
  90. $data = [
  91. 'shop_id' => $shop_id,
  92. 'log_type' => $log_type,
  93. 'before' => $wallet[$accountType],
  94. 'change_value' => $number,
  95. 'remain' => bcadd($wallet[$accountType], $number),
  96. 'table' => $table,
  97. 'table_id' => $table_id,
  98. 'remark' => $remark,
  99. 'createtime' => time(),
  100. 'updatetime' => time(),
  101. ];
  102. //新的方式
  103. $upWallet = Db::name($this->walletName)->where(['shop_id' => $shop_id])->update([$accountType => $data['remain']]);
  104. if ($upWallet === false) {
  105. return $this->error("更新账户余额失败!");
  106. }
  107. $logTableName = self::LOG_TABLE[$this->walletName];
  108. if (!Db::name($logTableName)->insertGetId($data)) {
  109. return $this->error("更新财务记录失败!");
  110. }
  111. return $this->success("账户余额已更新!");
  112. }
  113. }