ShopWalletBusiness.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 int $userId = 0;
  45. protected string $tableName = '';
  46. public function __construct($tableName) {
  47. $this->tableName = $tableName;
  48. }
  49. //获取钱包名称
  50. public function getWalletName($name = '')
  51. {
  52. $conf = self::money_type;
  53. if ($name) {
  54. return $conf[$name] ?: $name;
  55. }
  56. return $conf;
  57. }
  58. /**
  59. * 余额变更
  60. * @param int $shop_id 店铺id
  61. * @param float $number 金额(正数进账,负数出账)
  62. * @param string $accountType 货币类型 self::money_type
  63. * @param int $log_type 日志的类型 self::log_type
  64. * @param string $remark 备注
  65. * @param string $table 来源表
  66. * @param int $table_id 表id
  67. * @return bool
  68. */
  69. public function change(int $shop_id, float $number, string $accountType = 'money', int $log_type = 0, string $remark = '', string $table = '', int $table_id = 0)
  70. {
  71. //获取小数点
  72. $point = 0;
  73. if(in_array($accountType,['money','bean'])){
  74. $point = 2;
  75. }
  76. bcscale($point);
  77. //钱包名称
  78. $wallet_name = $this->getWalletName($accountType);
  79. //检测
  80. if ($number == 0 || 0 === bccomp($number, 0)) {
  81. return $this->error('交易金额:0');
  82. }
  83. //检测
  84. if (!$wallet = Db::name($this->tableName)->lock(true)->where(['shop_id' => $shop_id])->find()) {
  85. return $this->error('不存在的用户');
  86. }
  87. if (bccomp(bcadd($wallet[$accountType], $number), 0) === -1) {
  88. return $this->error("{$wallet_name}余额不足!");
  89. }
  90. //钱币记录
  91. $data = [
  92. 'shop_id' => $shop_id,
  93. 'log_type' => $log_type,
  94. 'before' => $wallet[$accountType],
  95. 'change_value' => $number,
  96. 'remain' => bcadd($wallet[$accountType], $number),
  97. 'table' => $table,
  98. 'table_id' => $table_id,
  99. 'remark' => $remark,
  100. 'createtime' => time(),
  101. 'updatetime' => time(),
  102. ];
  103. //新的方式
  104. $upWallet = Db::name($this->tableName)->where(['shop_id' => $shop_id])->update([$accountType => $data['remain']]);
  105. if ($upWallet === false) {
  106. return $this->error("更新账户余额失败!");
  107. }
  108. $logTableName = self::LOG_TABLE[$this->tableName];
  109. if (!Db::name($logTableName)->insertGetId($data)) {
  110. return $this->error("更新财务记录失败!");
  111. }
  112. return $this->success("账户余额已更新!");
  113. }
  114. }