ShopWalletBusiness.php 3.9 KB

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