|
@@ -0,0 +1,128 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace app\common\business;
|
|
|
+
|
|
|
+use app\common\model\BillModel;
|
|
|
+use app\common\model\HotelCanteenOrderModel;
|
|
|
+use app\common\model\HotelOrderModel;
|
|
|
+use app\common\model\OfflineShopOrderModel;
|
|
|
+use app\common\model\UniversityEventApplyModel;
|
|
|
+use app\utils\Common;
|
|
|
+use think\Db;
|
|
|
+
|
|
|
+class ShopWalletBusiness extends BusinessResult
|
|
|
+{
|
|
|
+ // 日志变动类型
|
|
|
+ const log_type = [
|
|
|
+ 1 => '系统调节',//money + -
|
|
|
+ 40 => '酒店预定',//money + -
|
|
|
+ 50 => '餐厅预定',//money + -
|
|
|
+ 60 => '老年大学活动报名',//money + -
|
|
|
+ 70 => '线下消费',//money + -
|
|
|
+ ];
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 主表订单对应各自店铺表
|
|
|
+ */
|
|
|
+ public const WALLET_TABLE = [
|
|
|
+ 'hotel_order' => 'hotel_wallet',
|
|
|
+ 'hotel_canteen_order' => 'hotel_canteen_wallet',
|
|
|
+ 'university_event_apply' => 'university_event_wallet',
|
|
|
+ 'offline_shop_order' => 'offline_shop_wallet',
|
|
|
+ ];
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 主表订单对应各自店铺表
|
|
|
+ */
|
|
|
+ public const LOG_TABLE = [
|
|
|
+ 'hotel_wallet' => 'hotel_money_log',
|
|
|
+ 'hotel_canteen_wallet' => 'hotel_canteen_money_log',
|
|
|
+ 'university_event_wallet' => 'university_event_money_log',
|
|
|
+ 'offline_shop_wallet' => 'offline_shop_money_log',
|
|
|
+ ];
|
|
|
+
|
|
|
+ // 操作钱包余额类型
|
|
|
+ const money_type = [
|
|
|
+ 'money' => '余额',
|
|
|
+ ];
|
|
|
+
|
|
|
+ protected int $userId = 0;
|
|
|
+ protected string $tableName = '';
|
|
|
+
|
|
|
+ public function __construct($tableName) {
|
|
|
+ $this->tableName = $tableName;
|
|
|
+ }
|
|
|
+
|
|
|
+ //获取钱包名称
|
|
|
+ public function getWalletName($name = '')
|
|
|
+ {
|
|
|
+ $conf = self::money_type;
|
|
|
+ if ($name) {
|
|
|
+ return $conf[$name] ?: $name;
|
|
|
+ }
|
|
|
+ return $conf;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 余额变更
|
|
|
+ * @param int $shop_id 店铺id
|
|
|
+ * @param float $number 金额(正数进账,负数出账)
|
|
|
+ * @param string $accountType 货币类型 self::money_type
|
|
|
+ * @param int $log_type 日志的类型 self::log_type
|
|
|
+ * @param string $remark 备注
|
|
|
+ * @param string $table 来源表
|
|
|
+ * @param int $table_id 表id
|
|
|
+ * @return bool
|
|
|
+ */
|
|
|
+ public function change(int $shop_id, float $number, string $accountType = 'money', int $log_type = 0, string $remark = '', string $table = '', int $table_id = 0)
|
|
|
+ {
|
|
|
+ //获取小数点
|
|
|
+ $point = 0;
|
|
|
+ if(in_array($accountType,['money','bean'])){
|
|
|
+ $point = 2;
|
|
|
+ }
|
|
|
+ bcscale($point);
|
|
|
+
|
|
|
+ //钱包名称
|
|
|
+ $wallet_name = $this->getWalletName($accountType);
|
|
|
+
|
|
|
+ //检测
|
|
|
+ if ($number == 0 || 0 === bccomp($number, 0)) {
|
|
|
+ return $this->error('交易金额:0');
|
|
|
+ }
|
|
|
+
|
|
|
+ //检测
|
|
|
+ if (!$wallet = Db::name($this->tableName)->lock(true)->where(['shop_id' => $shop_id])->find()) {
|
|
|
+ return $this->error('不存在的用户');
|
|
|
+ }
|
|
|
+
|
|
|
+ if (bccomp(bcadd($wallet[$accountType], $number), 0) === -1) {
|
|
|
+ return $this->error("{$wallet_name}余额不足!");
|
|
|
+ }
|
|
|
+
|
|
|
+ //钱币记录
|
|
|
+ $data = [
|
|
|
+ 'shop_id' => $shop_id,
|
|
|
+ 'log_type' => $log_type,
|
|
|
+ 'before' => $wallet[$accountType],
|
|
|
+ 'change_value' => $number,
|
|
|
+ 'remain' => bcadd($wallet[$accountType], $number),
|
|
|
+ 'table' => $table,
|
|
|
+ 'table_id' => $table_id,
|
|
|
+ 'remark' => $remark,
|
|
|
+ 'createtime' => time(),
|
|
|
+ 'updatetime' => time(),
|
|
|
+ ];
|
|
|
+ //新的方式
|
|
|
+ $upWallet = Db::name($this->tableName)->where(['shop_id' => $shop_id])->update([$accountType => $data['remain']]);
|
|
|
+ if ($upWallet === false) {
|
|
|
+ return $this->error("更新账户余额失败!");
|
|
|
+ }
|
|
|
+ $logTableName = self::LOG_TABLE[$this->tableName];
|
|
|
+ if (!Db::name($logTableName)->insertGetId($data)) {
|
|
|
+ return $this->error("更新财务记录失败!");
|
|
|
+ }
|
|
|
+
|
|
|
+ return $this->success("账户余额已更新!");
|
|
|
+ }
|
|
|
+}
|