diff --git a/app/common/dao/store/order/StoreOrderDao.php b/app/common/dao/store/order/StoreOrderDao.php index 1c229cc0..650ccc59 100644 --- a/app/common/dao/store/order/StoreOrderDao.php +++ b/app/common/dao/store/order/StoreOrderDao.php @@ -495,20 +495,26 @@ class StoreOrderDao extends BaseDao return ($this->getModel()::getDB())->whereIn('reconciliation_id', $reconciliation_id)->update(['reconciliation_id' => 0]); } - public function dayOrderNum($day, $merId = null) + public function dayOrderNum($day, $merId = null, $saleType = 1) { return StoreOrder::getDB()->where('paid', 1)->when($merId, function ($query, $merId) { $query->where('mer_id', $merId); })->when($day, function ($query, $day) { getModelTime($query, $day, 'pay_time'); + })->when($saleType > 0, function ($query) use ($saleType) { + $query->where('sale_type', $saleType); })->count(); } - public function dayOrderPrice($day, $merId = null) + public function dayOrderPrice($day, $merId = null, $saleType = 1) { - return getModelTime(StoreOrder::getDB()->where('paid', 1)->when($merId, function ($query, $merId) { - $query->where('mer_id', $merId); - }), $day, 'pay_time')->sum('pay_price'); + return getModelTime(StoreOrder::getDB()->where('paid', 1) + ->when($merId, function ($query, $merId) { + $query->where('mer_id', $merId); + }), $day, 'pay_time') + ->when($saleType > 0, function ($query) use ($saleType) { + $query->where('sale_type', $saleType); + })->sum('pay_price'); } public function dateOrderPrice($date, $merId = null) @@ -529,12 +535,14 @@ class StoreOrderDao extends BaseDao })->count(); } - public function dayOrderUserNum($day, $merId = null) + public function dayOrderUserNum($day, $merId = null, $saleType = 1) { return StoreOrder::getDB()->where('paid', 1)->when($merId, function ($query, $merId) { $query->where('mer_id', $merId); })->when($day, function ($query, $day) { getModelTime($query, $day, 'pay_time'); + })->when($saleType > 0, function ($query) use ($saleType) { + $query->where('sale_type', $saleType); })->group('uid')->count(); } diff --git a/app/common/model/store/coupon/StoreCoupon.php b/app/common/model/store/coupon/StoreCoupon.php index e675ecc0..6f2f94ce 100644 --- a/app/common/model/store/coupon/StoreCoupon.php +++ b/app/common/model/store/coupon/StoreCoupon.php @@ -22,6 +22,9 @@ use app\common\repositories\store\product\SpuRepository; class StoreCoupon extends BaseModel { + protected $json = ['config']; + protected $jsonAssoc = true; + /** * @return string * @author xaboy diff --git a/app/common/model/store/coupon/StoreCouponUser.php b/app/common/model/store/coupon/StoreCouponUser.php index 4d2b6ebe..3fc0462d 100644 --- a/app/common/model/store/coupon/StoreCouponUser.php +++ b/app/common/model/store/coupon/StoreCouponUser.php @@ -28,6 +28,11 @@ use app\common\repositories\store\coupon\StoreCouponRepository; class StoreCouponUser extends BaseModel { + const STATUS_UN_RECEIVE = -2; //未领取 + const STATUS_REPEAL = -1; //已作废 + const STATUS_UNUSED = 0; //未使用 + const STATUS_USED = 1; //已使用 + /** * @return string * @author xaboy diff --git a/app/common/repositories/store/coupon/StoreCouponRepository.php b/app/common/repositories/store/coupon/StoreCouponRepository.php index acad5beb..2accd0a7 100644 --- a/app/common/repositories/store/coupon/StoreCouponRepository.php +++ b/app/common/repositories/store/coupon/StoreCouponRepository.php @@ -50,6 +50,7 @@ class StoreCouponRepository extends BaseRepository const TYPE_STORE_PRODUCT = 1; //店铺现金抵扣券,可结余 const TYPE_STORE_COUPON = 2; + const TYPE_SALE_SUBSIDY = 3; //平台券 const TYPE_PLATFORM_ALL = 10; //平台分类券 @@ -59,8 +60,9 @@ class StoreCouponRepository extends BaseRepository //平台购物卡,可全额抵扣,可结余 const TYPE_PLATFORM_CARD = 13; const TYPE_MAP = [ - self::TYPE_STORE_COUPON => '春耕采购余额', - self::TYPE_PLATFORM_CARD => '补贴', + self::TYPE_STORE_COUPON => '补贴', + self::TYPE_SALE_SUBSIDY => '补贴', + self::TYPE_PLATFORM_CARD => '春耕采购余额', ]; //获取方式 diff --git a/app/common/repositories/store/order/StoreOrderRepository.php b/app/common/repositories/store/order/StoreOrderRepository.php index d5a6206b..ca79269f 100644 --- a/app/common/repositories/store/order/StoreOrderRepository.php +++ b/app/common/repositories/store/order/StoreOrderRepository.php @@ -92,6 +92,9 @@ class StoreOrderRepository extends BaseRepository const TYPE_SN_USER_RECHARGE = 'wxu'; const TYPE_SN_SERVER_ORDER = 'cs'; const TYPE_SN_REFUND = 'rwx'; + + public $saleType; + /** * StoreOrderRepository constructor. * @param StoreOrderDao $dao @@ -853,6 +856,9 @@ class StoreOrderRepository extends BaseRepository public function OrderTitleNumber(?int $merId, ?int $orderType) { $where = []; + if ($this->saleType > 0) { + $where['sale_type'] = $this->saleType; + } $sysDel = $merId ? 0 : null; //商户删除 if ($merId) $where['mer_id'] = $merId; //商户订单 if ($orderType === 0) $where['order_type'] = 0; //普通订单 diff --git a/app/controller/api/server/Store.php b/app/controller/api/server/Store.php new file mode 100644 index 00000000..d19048d6 --- /dev/null +++ b/app/controller/api/server/Store.php @@ -0,0 +1,192 @@ +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 = StoreCoupon::where('type', StoreCouponRepository::TYPE_PLATFORM_CARD)->value('coupon_id'); + $userCoupon = StoreCouponUser::where('uid', $uid) + ->where('coupon_id', $couponId) + ->where('status', '<>', StoreCouponUser::STATUS_REPEAL) + ->field('coupon_price,origin_price') + ->find(); + if (!empty($userCoupon)) { + $couponId = StoreCoupon::where('type', StoreCouponRepository::TYPE_STORE_COUPON)->value('coupon_id'); + $subsidyAmount = StoreCouponUser::where('uid', $uid) + ->where('coupon_id', $couponId) + ->value('origin_price'); + $saleTarget = $userCoupon['origin_price']; + $purchaseTarget = $userCoupon['origin_price']; + $merchant['purchase_amount'] = bcsub($userCoupon['origin_price'], $userCoupon['coupon_price'], 2); + $merchant['balance'] = $userCoupon['coupon_price']; + } + } else { + //增收销售补贴 + $coupon = StoreCoupon::where('type', StoreCouponRepository::TYPE_SALE_SUBSIDY)->find(); + if ($coupon) { + $userCoupon = StoreCouponUser::where('uid', $uid) + ->where('coupon_id', $coupon['coupon_id']) + ->where('status', '<>', StoreCouponUser::STATUS_REPEAL) + ->field('coupon_price,origin_price,status') + ->find(); + $subsidyAmount = $userCoupon['origin_price'] ?? '0.00'; + $merchant['balance'] = $userCoupon['coupon_price'] ?? '0.00'; + foreach ($coupon['config'] as $k => $item) { + if (empty($userCoupon) && in_array($merchant['type_id'], $item['type_id'])) { + $saleTarget = $item['amount']; + $subsidyAmount = $item['subsidy']; + $purchaseTarget = bcmul($saleTarget, 0.5, 2); + break; + } + if (!empty($userCoupon) && in_array($merchant['type_id'], $item['type_id'])) { + $nextItem = $coupon['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, + 'balance' => $merchant['balance'] ?? '0.00', + ]); + } + + /** + * 红包获取记录 + * @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 = StoreCouponRepository::TYPE_STORE_COUPON; + } else { + $couponType = StoreCouponRepository::TYPE_SALE_SUBSIDY; + } + $couponId = StoreCoupon::where('type', $couponType)->value('coupon_id'); + $totalAmount = StoreCouponUser::where('uid', $userId) + ->where('coupon_id', $couponId) + ->where('status', StoreCouponUser::STATUS_UNUSED) + ->sum('coupon_price'); + $couponUserId = StoreCouponUser::where('uid', $userId) + ->where('coupon_id', $couponId) + ->where('status', '<>', StoreCouponUser::STATUS_REPEAL) + ->column('coupon_user_id'); + $query = StoreCouponDetail::field('amount coupon_price,create_time,pay_price') + ->whereIn('coupon_user_id', $couponUserId) + ->where('type', StoreCouponDetail::TYPE_INCOME); + $count = $query->count(); + $record = $query->page($page)->limit($limit)->select()->toArray(); + foreach ($record as &$item) { + $item['order_amount'] = bcadd($item['coupon_price'], $item['pay_price'], 2); + } + $result = ['total_amount' => $totalAmount, 'count' => $count, 'record' => $record]; + return app('json')->success($result); + } + +} diff --git a/app/controller/api/server/StoreOrder.php b/app/controller/api/server/StoreOrder.php index 63eb2ec3..bc4960ea 100644 --- a/app/controller/api/server/StoreOrder.php +++ b/app/controller/api/server/StoreOrder.php @@ -35,13 +35,19 @@ class StoreOrder extends BaseController public function orderStatistics($merId, StoreOrderRepository $repository) { + $repository->saleType = $this->request->param('sale_type', 1); $order = $repository->OrderTitleNumber($merId, null); - $order['refund'] = app()->make(StoreRefundOrderRepository::class)->getWhereCount(['is_system_del' => 0, 'mer_id' => $merId]); + $where = ['is_system_del' => 0, 'mer_id' => $merId]; + if ($repository->saleType > 0) { + $where['sale_type'] = $repository->saleType; + } + $order['refund'] = app()->make(StoreRefundOrderRepository::class)->getWhereCount($where); + /** @var Common $common */ $common = app()->make(Common::class); $data = []; - $data['today'] = $common->mainGroup('today', $merId); - $data['yesterday'] = $common->mainGroup('yesterday', $merId); - $data['month'] = $common->mainGroup('month', $merId); + $data['today'] = $common->mainGroup('today', $merId, $repository->saleType); + $data['yesterday'] = $common->mainGroup('yesterday', $merId, $repository->saleType); + $data['month'] = $common->mainGroup('month', $merId, $repository->saleType); return app('json')->success(compact('order', 'data')); } diff --git a/app/controller/merchant/Common.php b/app/controller/merchant/Common.php index 2fdd8041..4eabe1fd 100644 --- a/app/controller/merchant/Common.php +++ b/app/controller/merchant/Common.php @@ -82,18 +82,22 @@ class Common extends BaseController /** * @param $date * @param $merId + * @param $saleType * @return array * @author xaboy * @day 2020/6/25 */ - public function mainGroup($date, $merId) + public function mainGroup($date, $merId, $saleType = 1) { + /** @var UserVisitRepository $userVisitRepository */ $userVisitRepository = app()->make(UserVisitRepository::class); + /** @var StoreOrderRepository $repository */ $repository = app()->make(StoreOrderRepository::class); + /** @var UserRelationRepository $relationRepository */ $relationRepository = app()->make(UserRelationRepository::class); - $orderNum = (float)$repository->dayOrderNum($date, $merId); - $payPrice = (float)$repository->dayOrderPrice($date, $merId); - $payUser = (float)$repository->dayOrderUserNum($date, $merId); + $orderNum = (float)$repository->dayOrderNum($date, $merId, $saleType); + $payPrice = (float)$repository->dayOrderPrice($date, $merId, $saleType); + $payUser = (float)$repository->dayOrderUserNum($date, $merId, $saleType); $visitNum = (float)$userVisitRepository->dateVisitUserNum($date, $merId); $likeStore = (float)$relationRepository->dayLikeStore($date, $merId); return compact('orderNum', 'payPrice', 'payUser', 'visitNum', 'likeStore'); diff --git a/route/api.php b/route/api.php index 3e3fd6c1..5b278254 100644 --- a/route/api.php +++ b/route/api.php @@ -295,6 +295,8 @@ Route::group('api/', function () { Route::post('attr/delete', 'StoreProductAttrTemplate/batchDelete'); Route::get('attr/detail/:id', 'StoreProductAttrTemplate/detail'); Route::get('attr/list', 'StoreProductAttrTemplate/getlist'); + Route::get('subsidy', 'Store/subsidy'); + Route::get('subsidyRecord', 'Store/subsidyRecord'); })->prefix('api.server.')->middleware(\app\common\middleware\MerchantServerMiddleware::class, 1); //管理员订单