You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

458 lines
14 KiB

<?php
namespace App\Service;
use App\Model\FinancialRecord;
use App\Model\Store;
use App\Model\UserBalance;
use Hyperf\Di\Annotation\Inject;
class FinancialRecordService implements FinancialRecordServiceInterface
{
/**
* @Inject
* @var SmsServiceInterface
*/
protected $smsAliService;
public function ledgerAccounts($source_id, $money, $source_type, $money_type, $desc, $comment='')
{
return $this->record(
FinancialRecord::ACCOUNT_LEDGER,
[
'user_id' => FinancialRecord::ACCOUNT_LEDGER,
'user_type' => FinancialRecord::USER_TYPE_LEDGER,
'money' => $money,
'money_type' => $money_type,
'source_id' => $source_id,
'source_type' => $source_type,
'desc' => $desc,
'comment' => $comment,
'status' => FinancialRecord::STATUS_NORMAL,
],
true
);
}
public function record($user_id, $record, $isLedger=false)
{
$financialRecord = new FinancialRecord();
if (!$isLedger) {
$mod = bcmod((string)$user_id, '5', 0);
$financialRecord->suffix($mod);
}
return $financialRecord->fill(
[
'user_id' => $user_id,
'user_type' => $record['user_type'],
'money' => $record['money'],
'money_type' => $record['money_type'],
'source_id' => $record['source_id'],
'source_type' => $record['source_type'],
'desc' => $record['desc'],
'comment' => $record['comment'],
'status' => $record['status'],
]
)->save();
}
/**
* @inheritDoc
*/
public function communityAwardByPlatNewUser(
$user_id,
$source_id,
$money,
$desc='新用户奖励',
$comment='社区服务点',
$user_type=FinancialRecord::USER_TYPE_CS,
$source_type=FinancialRecord::SOURCE_TYPE_ORDER,
$money_type=FinancialRecord::MONEY_TYPE_CS_PLAT_NEW_USER
)
{
$this->recordAll($user_id, $source_id, $money, $user_type, $source_type, $money_type, $desc, $comment);
// 维护社区服务点余额
$balance = UserBalance::firstOrNew([
'user_type' => $user_type,
'source_id' => $user_id
]);
$balance->balance = bcadd($balance->balance, $money, 2);
$balance->save();
// 发送短信
$this->smsAliService->sendForCommunityFinancial($user_id, $money);
}
/**
* @inheritDoc
*/
public function communityAwardByPlatNewUserFirstOLOrder(
$user_id,
$source_id,
$money,
$desc='新用户首单奖励',
$comment='社区服务点',
$user_type=FinancialRecord::USER_TYPE_CS,
$source_type=FinancialRecord::SOURCE_TYPE_ORDER,
$money_type=FinancialRecord::MONEY_TYPE_CS_FIRST_ORDER
)
{
$this->recordAll($user_id, $source_id, $money, $user_type, $source_type, $money_type, $desc, $comment);
// 维护社区服务点余额
$balance = UserBalance::firstOrNew([
'user_type' => $user_type,
'source_id' => $user_id
]);
$balance->balance = bcadd($balance->balance, $money, 2);
$balance->save();
// 发送短信
$this->smsAliService->sendForCommunityFinancial($user_id, $money);
}
/**
* @inheritDoc
*/
public function communitySeparateAccountsByOrderComp(
$user_id,
$source_id,
$money,
$desc='用户订单分成',
$comment='社区服务点',
$user_type=FinancialRecord::USER_TYPE_CS,
$source_type=FinancialRecord::SOURCE_TYPE_ORDER,
$money_type=FinancialRecord::MONEY_TYPE_CS_OL_ORDER
)
{
$this->recordAll($user_id, $source_id, $money, $user_type, $source_type, $money_type, $desc, $comment);
// 维护社区服务点余额
$balance = UserBalance::firstOrNew([
'user_type' => $user_type,
'source_id' => $user_id
]);
$balance->balance = bcadd($balance->balance, $money,2);
$balance->save();
// 发送短信
$this->smsAliService->sendForCommunityFinancial($user_id, $money);
}
/**
* @inheritDoc
*/
public function storeAwardByPlatNewUserOFLOrder(
$user_id,
$source_id,
$money,
$desc='新用户下单奖励',
$comment='用户当面付商户奖励',
$user_type=FinancialRecord::USER_TYPE_STORE,
$source_type=FinancialRecord::SOURCE_TYPE_ORDER,
$money_type=FinancialRecord::MONEY_TYPE_STORE_PLAT_NEW_USER
)
{
$this->recordAll($user_id, $source_id, $money, $user_type, $source_type, $money_type, $desc, $comment);
// 同时维护钱包
$store = Store::query()->where(['user_id' => $user_id])->first();
$store->award_money = bcadd($store->award_money, $money, 2);
$store->save();
// 维护余额
$balance = UserBalance::firstOrNew([
'user_type' => $user_type,
'source_id' => $store->id
]);
$balance->balance = bcadd($balance->balance, $money, 2);
$balance->save();
}
/**
* @inheritDoc
*/
public function storeAwardByTodayFirstOFLOrder(
$user_id,
$source_id,
$money,
$desc='用户店铺首单奖励',
$comment='用户当面付商户奖励',
$user_type=FinancialRecord::USER_TYPE_STORE,
$source_type=FinancialRecord::SOURCE_TYPE_ORDER,
$money_type=FinancialRecord::MONEY_TYPE_STORE_FIRST_ORDER
)
{
$this->recordAll($user_id, $source_id, $money, $user_type, $source_type, $money_type, $desc, $comment);
// 同时维护钱包
$store = Store::query()->where(['user_id' => $user_id])->first();
$store->award_money = bcadd($store->award_money, $money, 2);
$store->save();
// 维护余额
$balance = UserBalance::firstOrNew([
'user_type' => $user_type,
'source_id' => $store->id
]);
$balance->balance = bcadd($balance->balance, $money, 2);
$balance->save();
}
public function recordAll($user_id, $source_id, $money, $user_type=1, $source_type=0, $money_type=0, $desc='', $comment='') {
$this->record(
$user_id,
[
'user_id' => $user_id,
'user_type' => $user_type,
'money' => $money,
'money_type' => $money_type,
'source_id' => $source_id,
'source_type' => $source_type,
'desc' => $desc,
'comment' => $comment,
'status' => FinancialRecord::STATUS_NORMAL,
]
);
$this->ledgerAccounts($source_id, $money, $source_type, $money_type, $desc, $comment);
}
/**
* @inheritDoc
*/
public function userByOFLOrderPaid(
$user_id,
$source_id,
$money,
$desc='用户下单(线下)',
$comment='用户下单',
$user_type=FinancialRecord::USER_TYPE_USER,
$source_type=FinancialRecord::SOURCE_TYPE_ORDER,
$money_type=FinancialRecord::MONEY_TYPE_USER_OFL_ORDER
)
{
$this->recordAll($user_id, $source_id, $money, $user_type, $source_type, $money_type, $desc, $comment);
}
/**
* @inheritDoc
*/
public function userByOLOrderPaid(
$user_id,
$source_id,
$money,
$desc='用户下单(线上)',
$comment='用户下单',
$user_type=FinancialRecord::USER_TYPE_USER,
$source_type=FinancialRecord::SOURCE_TYPE_ORDER,
$money_type=FinancialRecord::MONEY_TYPE_USER_OL_ORDER
)
{
$this->recordAll($user_id, $source_id, $money, $user_type, $source_type, $money_type, $desc, $comment);
}
/**
* @inheritDoc
*/
public function storeByOLOrderComp(
$user_id,
$source_id,
$money,
$desc = '线上外卖订单收入',
$comment = '用户订单完成',
$user_type = FinancialRecord::USER_TYPE_STORE,
$source_type = FinancialRecord::SOURCE_TYPE_ORDER,
$money_type = FinancialRecord::MONEY_TYPE_STORE_OL_ORDER_COMP
)
{
$this->recordAll($user_id, $source_id, $money, $user_type, $source_type, $money_type, $desc, $comment);
// 同时维护钱包
$store = Store::query()->where(['user_id' => $user_id])->first();
$store->store_wallet = bcadd($store->store_wallet, $money, 2);
$store->save();
// 维护余额
$balance = UserBalance::firstOrNew([
'user_type' => $user_type,
'source_id' => $store->id
]);
$balance->balance = bcadd($balance->balance, $money, 2);
$balance->save();
}
/**
* @inheritDoc
*/
public function storeByOFLOrderComp(
$user_id,
$source_id,
$money,
$desc = '线下当面付订单收入',
$comment = '用户订单完成',
$user_type = FinancialRecord::USER_TYPE_STORE,
$source_type = FinancialRecord::SOURCE_TYPE_ORDER,
$money_type = FinancialRecord::MONEY_TYPE_STORE_OFL_ORDER_COMP
)
{
$this->recordAll($user_id, $source_id, $money, $user_type, $source_type, $money_type, $desc, $comment);
// 同时维护钱包
$store = Store::query()->where(['user_id' => $user_id])->first();
$store->store_wallet = bcadd($store->store_wallet, $money, 2);
$store->save();
// 维护余额
$balance = UserBalance::firstOrNew([
'user_type' => $user_type,
'source_id' => $store->id
]);
$balance->balance = bcadd($balance->balance, $money, 2);
$balance->save();
}
/**
* @inheritDoc
* 订单退款(线上)
*/
public function userByOLOrderRefund(
$user_id,
$source_id,
$money,
$desc = '线上订单退款',
$comment = '线上订单退款到微信',
$user_type = FinancialRecord::USER_TYPE_USER,
$source_type = FinancialRecord::SOURCE_TYPE_ORDER,
$money_type = FinancialRecord::MONEY_TYPE_USER_OL_ORDER_REFUND
)
{
$this->recordAll($user_id, $source_id, $money, $user_type, $source_type, $money_type, $desc, $comment);
}
/**
* @inheritDoc
*/
public function mmAwardByPlatNewUser(
$user_id,
$source_id,
$money,
$desc = '发展新用户',
$comment = '市场经理发展新用户奖励',
$user_type = FinancialRecord::USER_TYPE_MM,
$source_type = FinancialRecord::SOURCE_TYPE_ORDER,
$money_type = FinancialRecord::MONEY_TYPE_MM_PLAT_NEW_USER
)
{
$this->recordAll($user_id, $source_id, $money, $user_type, $source_type, $money_type, $desc, $comment);
// 维护余额
$balance = UserBalance::firstOrNew([
'user_type' => $user_type,
'source_id' => $user_id
]);
$balance->balance = bcadd($balance->balance, $money, 2);
$balance->save();
}
/**
* @inheritDoc
*/
public function mmAwardByNewStore(
$user_id,
$source_id,
$money,
$desc = '发展新商户',
$comment = '市场经理发展新商户奖励',
$user_type = FinancialRecord::USER_TYPE_MM,
$source_type = FinancialRecord::SOURCE_TYPE_ORDER,
$money_type = FinancialRecord::MONEY_TYPE_MM_PLAT_NEW_STORE
)
{
$this->recordAll($user_id, $source_id, $money, $user_type, $source_type, $money_type, $desc, $comment);
// 维护余额
$balance = UserBalance::firstOrNew([
'user_type' => $user_type,
'source_id' => $user_id
]);
$balance->balance = bcadd($balance->balance, $money, 2);
$balance->save();
}
/**
* @inheritDoc
*/
public function mpAwardByPlatNewUser(
$user_id,
$source_id,
$money,
$desc = '服务商发展新用户',
$comment = '服务商发展新用户奖励',
$user_type = FinancialRecord::USER_TYPE_MP,
$source_type = FinancialRecord::SOURCE_TYPE_ORDER,
$money_type = FinancialRecord::MONEY_TYPE_MP_PLAT_NEW_USER
)
{
$this->recordAll($user_id, $source_id, $money, $user_type, $source_type, $money_type, $desc, $comment);
// 维护余额
$balance = UserBalance::firstOrNew([
'user_type' => $user_type,
'source_id' => $user_id
]);
$balance->balance = bcadd($balance->balance, $money, 2);
$balance->save();
}
/**
* @inheritDoc
*/
public function mpAwardByNewStore(
$user_id,
$source_id,
$money,
$desc = '服务商发展新商户',
$comment = '服务商发展新商户奖励',
$user_type = FinancialRecord::USER_TYPE_MP,
$source_type = FinancialRecord::SOURCE_TYPE_ORDER,
$money_type = FinancialRecord::MONEY_TYPE_MP_PLAT_NEW_STORE
)
{
$this->recordAll($user_id, $source_id, $money, $user_type, $source_type, $money_type, $desc, $comment);
// 维护余额
$balance = UserBalance::firstOrNew([
'user_type' => $user_type,
'source_id' => $user_id
]);
$balance->balance = bcadd($balance->balance, $money, 2);
$balance->save();
}
/**
* @inheritDoc
*/
public function mpSeparateAccountByOLOrderComp(
$user_id,
$source_id,
$money,
$desc = '服务商线上订单分账',
$comment = '服务商用户线上订单完成后分账',
$user_type = FinancialRecord::USER_TYPE_MP,
$source_type = FinancialRecord::SOURCE_TYPE_ORDER,
$money_type = FinancialRecord::MONEY_TYPE_MP_OL_ORDER
)
{
$this->recordAll($user_id, $source_id, $money, $user_type, $source_type, $money_type, $desc, $comment);
// 维护余额
$balance = UserBalance::firstOrNew([
'user_type' => $user_type,
'source_id' => $user_id
]);
$balance->balance = bcadd($balance->balance, $money, 2);
$balance->save();
}
}