123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- <?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 + -
- 80 => '旅游预定',//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',
- 'travel_order' => 'travel_wallet',
- ];
- /**
- * 主表订单对应各自店铺表
- */
- public const LOG_TABLE = [
- 'hotel_wallet' => 'hotel',
- 'hotel_canteen_wallet' => 'hotel_canteen',
- 'university_event_wallet' => 'university_event',
- 'offline_shop_wallet' => 'offline_shop',
- 'travel_wallet' => 'travel',
- ];
- // 操作钱包余额类型
- const money_type = [
- 'money' => '40期收益',
- 'balance' => '余额',
- 'points' => '让利积分',
- ];
- protected string $walletName = '';
- public function __construct($orderName) {
- $this->walletName = self::WALLET_TABLE[$orderName];
- }
- //获取钱包名称
- 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,array_keys(self::money_type))){
- $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->walletName)->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->walletName)->where(['shop_id' => $shop_id])->update([$accountType => $data['remain']]);
- if ($upWallet === false) {
- return $this->error("更新账户余额失败!");
- }
- // 插入日志
- $left_table = self::LOG_TABLE[$this->walletName];
- $logTableName = "{$left_table}_{$accountType}_log";
- if (!Db::name($logTableName)->insertGetId($data)) {
- return $this->error("更新财务记录失败!");
- }
- return $this->success("账户余额已更新!");
- }
- }
|