192 lines
8.1 KiB
PHP
192 lines
8.1 KiB
PHP
<?php
|
|
|
|
namespace app\controller\api\server;
|
|
|
|
use app\common\model\store\consumption\StoreConsumption;
|
|
use app\common\model\store\consumption\StoreConsumptionUser;
|
|
use app\common\model\system\merchant\FinancialRecord;
|
|
use app\common\model\system\merchant\Merchant;
|
|
use app\common\model\user\UserBill;
|
|
use app\controller\api\Common;
|
|
use crmeb\basic\BaseController;
|
|
use think\App;
|
|
use think\db\Query;
|
|
|
|
class Store extends BaseController
|
|
{
|
|
|
|
protected $merId;
|
|
|
|
public function __construct(App $app)
|
|
{
|
|
parent::__construct($app);
|
|
$this->merId = $this->request->route('merId');
|
|
}
|
|
|
|
/**
|
|
* 生成二维码
|
|
*/
|
|
public function qrcode()
|
|
{
|
|
$common = app()->make(Common::class);
|
|
$siteUrl = systemConfig('site_url');
|
|
$data = $common->Qrcode(['code' => $siteUrl . 'download/index.html?code=mer_' . $this->merId, 'id' => $this->merId]);
|
|
return app('json')->success(['url' => $data]);
|
|
}
|
|
|
|
|
|
/**
|
|
* 邀请记录
|
|
* @return mixed
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
* @throws \think\db\exception\DbException
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
*/
|
|
public function record()
|
|
{
|
|
[$page, $limit] = $this->getPage();
|
|
$query = FinancialRecord::field('order_sn,number')
|
|
->where('mer_id', $this->merId)
|
|
->where('financial_type', 'commission_to_promoter')
|
|
->with(['orderInfo' => function (Query $query) {
|
|
$query->field('pay_price,real_name,uid,order_sn')->with(['user' => function (Query $query) {
|
|
$query->field('nickname,uid');
|
|
}]);
|
|
}]);
|
|
$count = $query->count();
|
|
$result = $query->page($page, $limit)->select()->toArray();
|
|
$list = [];
|
|
foreach ($result as $item) {
|
|
$list[] = [
|
|
'nickname' => $item['orderInfo']['user']['nickname'],
|
|
'uid' => $item['orderInfo']['uid'],
|
|
'order_amount' => $item['orderInfo']['pay_price'],
|
|
'commission' => $item['number'],
|
|
];
|
|
}
|
|
return app('json')->success(['count' => $count, 'list' => $list]);
|
|
}
|
|
|
|
/**
|
|
* 补贴进度查询
|
|
* @return mixed
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
* @throws \think\db\exception\DbException
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
*/
|
|
public function subsidy()
|
|
{
|
|
$type = $this->request->get('type', 1);
|
|
$uid = $this->request->uid();
|
|
$merchant = Merchant::where('mer_id', $this->merId)->field('sale_amount,purchase_amount,type_id')->find()->toArray();
|
|
$saleTarget = '0.00';
|
|
$purchaseTarget = '0.00';
|
|
if ($type == 1) {
|
|
//春耕采购补贴
|
|
$couponId = StoreConsumption::where('type', StoreConsumption::TYPE_PULL_CONSUMPTION)->value('coupon_id');
|
|
$userConsumption = StoreConsumptionUser::where('uid', $uid)
|
|
->where('coupon_id', $couponId)
|
|
->where('status', '<>', -1)
|
|
->field('coupon_price,balance')
|
|
->find();
|
|
if (!empty($userConsumption)) {
|
|
$couponId = StoreConsumption::where('type', StoreConsumption::TYPE_RECHARGE)->value('coupon_id');
|
|
$subsidyAmount = StoreConsumptionUser::where('uid', $uid)
|
|
->where('coupon_id', $couponId)
|
|
->value('coupon_price');
|
|
$saleTarget = $userConsumption['coupon_price'];
|
|
$purchaseTarget = $userConsumption['coupon_price'];
|
|
$merchant['purchase_amount'] = bcsub($userConsumption['coupon_price'], $userConsumption['balance'], 2);
|
|
}
|
|
} else {
|
|
//增收销售补贴
|
|
$consumption = StoreConsumption::where('type', StoreConsumption::TYPE_SALE_SUBSIDY)->find();
|
|
if ($consumption) {
|
|
$userConsumption = StoreConsumptionUser::where('uid', $uid)
|
|
->where('coupon_id', $consumption['coupon_id'])
|
|
->where('status', '<>', -1)
|
|
->field('coupon_price,balance,status')
|
|
->find();
|
|
$subsidyAmount = $userConsumption['coupon_price'] ?? '0.00';
|
|
foreach ($consumption['config'] as $k => $item) {
|
|
if (empty($userConsumption) && in_array($merchant['type_id'], $item['type_id'])) {
|
|
$saleTarget = $item['amount'];
|
|
$subsidyAmount = $item['subsidy'];
|
|
$purchaseTarget = bcmul($saleTarget, 0.5, 2);
|
|
break;
|
|
}
|
|
if (!empty($userConsumption) && in_array($merchant['type_id'], $item['type_id'])) {
|
|
$nextItem = $consumption['config'][$k + 1] ?? $item;
|
|
$saleTarget = $nextItem['amount'];
|
|
$subsidyAmount = $nextItem['subsidy'];
|
|
$purchaseTarget = bcmul($saleTarget, 0.5, 2);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
$saleFinishRate = $saleTarget <= 0 ? 0 : bcdiv($merchant['sale_amount'], $saleTarget, 2);
|
|
$saleFinishRate = bcmul(min($saleFinishRate, 1), 100, 0);
|
|
$purchaseFinishRate = $purchaseTarget <= 0 ? 0 : bcdiv($merchant['purchase_amount'], $purchaseTarget, 2);
|
|
$purchaseFinishRate = bcmul(min($purchaseFinishRate, 1), 100, 0);
|
|
$springSubsidyRate = $purchaseTarget <= 0 ? 0 : bcdiv($merchant['purchase_amount'], $purchaseTarget, 2);
|
|
$springSubsidyRate = bcmul(min($springSubsidyRate, 1), 100, 0);
|
|
return app('json')->success([
|
|
'subsidy_amount' => empty($subsidyAmount) ? '0.00' : $subsidyAmount,
|
|
'subsidy_status' => $saleFinishRate >= 100 && $purchaseFinishRate >= 100,
|
|
'sale_target' => $saleTarget,
|
|
'sale_amount' => $merchant['sale_amount'],
|
|
'sale_finish_rate' => $saleFinishRate,
|
|
'purchase_target' => $purchaseTarget,
|
|
'purchase_amount' => $merchant['purchase_amount'],
|
|
'purchase_finish_rate' => $purchaseFinishRate,
|
|
'spring_subsidy' => $saleTarget,
|
|
'spring_subsidy_amount' => $merchant['purchase_amount'],
|
|
'spring_subsidy_rate' => $springSubsidyRate,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 红包获取记录
|
|
* @return mixed
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
* @throws \think\db\exception\DbException
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
*/
|
|
public function subsidyRecord()
|
|
{
|
|
$userId = $this->request->uid();
|
|
$type = $this->request->get('type', 1);
|
|
$page = $this->request->get('page', 1);
|
|
$limit = $this->request->get('limit', 10);
|
|
if ($type == 1) {
|
|
$couponType = StoreConsumption::TYPE_RECHARGE;
|
|
} else {
|
|
$couponType = StoreConsumption::TYPE_SALE_SUBSIDY;
|
|
}
|
|
$couponId = StoreConsumption::where('type', $couponType)->value('coupon_id');
|
|
$totalAmount = StoreConsumptionUser::where('uid', $userId)
|
|
->whereIn('coupon_id', $couponId)
|
|
->where('status', StoreConsumptionUser::STATUS_UNUSED)
|
|
->sum('balance');
|
|
$query = UserBill::field('link_id,create_time,number coupon_price,mark,extra,status')
|
|
->where('uid', $userId)
|
|
->where('category', 'red_pack')
|
|
->where('status', 1)
|
|
->whereRaw("extra->'$.coupon_id'=" . $couponId);
|
|
$count = $query->count();
|
|
$record = $query->page($page)->limit($limit)->select()->toArray();
|
|
foreach ($record as &$item) {
|
|
$item['order_amount'] = 0;
|
|
if (mb_strpos($item['mark'], '订单金额:') !== false) {
|
|
$item['order_amount'] = mb_substr($item['mark'], mb_strpos($item['mark'], '订单金额:') + 5);
|
|
}
|
|
$item['extra'] = json_decode($item['extra'], true);
|
|
unset($item['mark']);
|
|
}
|
|
$result = ['total_amount' => $totalAmount, 'count' => $count, 'record' => $record];
|
|
return app('json')->success($result);
|
|
}
|
|
|
|
}
|