| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- <?php
- namespace addons\Boss\common\logic;
- use common\enums\StatusEnum;
- use common\models\statistics\UserStatisticsTotal;
- use common\models\user\User;
- /**
- * 股东权重分红条件限制
- */
- class BossBonusLogic
- {
- const ORDER_TYPE_PAY = 'order_pay';// 订单支付
- const ORDER_TYPE_FINISH = 'order_finish';// 订单完成
- /**
- * 获取股东分红条件限制
- * @param $user
- * @param $level
- * @return bool
- */
- public static function getAccordBonus($user, $level): bool
- {
- // 判断是否开启股东分红限制
- if (empty($level['bonus_restrict'])) return true;
- // 判断是否选则分红条件限制
- $keys = json_decode($level['bonus_restrict_keys'], true);
- $values = json_decode($level['bonus_restrict_values'], true);
- if (empty($keys) || empty($values)) {// 如果为选择分红条件限制,则默认通过
- return true;
- }
- foreach ($values as $value) {
- $system = $value['value'];
- if (!in_array($value['key'], $keys)) continue;
- switch ($value['key']) {
- case 0: // 直推
- $boss_result = self::directPushRestrict($user, $system);
- if ($boss_result === false) {
- return false;
- }
- break;
- case 1: // 消费满
- $boss_result = self::consumptionRestrict($user, $system);
- if ($boss_result === false) {
- return false;
- }
- break;
- default:
- return false;
- }
- }
- return true;
- }
- /**
- * 直推股东分红限制
- * @param $user
- * @param array $system
- * @return true
- */
- protected static function directPushRestrict($user, array $system): bool
- {
- try {
- // 获取直推人数
- $pushNum = User::find()
- ->where(['parent_id' => $user['user_id'], 'mall_id' => $user['mall_id'], 'status' => StatusEnum::ENABLED])
- ->Andwhere(['>=', 'level', $system['level']])
- ->count();
- } catch (\Exception $e) {
- return false;
- }
- if ($pushNum < $system['num']) {
- return false;
- }
- return true;
- }
- /**
- * 消费股东分红限制
- * @param $user
- * @param array $system
- * @return true
- */
- protected static function consumptionRestrict($user, array $system): bool
- {
- try {
- $userIds = User::find()
- ->select(['id'])
- ->where(['parent_id' => $user['user_id'], 'mall_id' => $user['mall_id'], 'status' => StatusEnum::ENABLED])
- ->column();
- if (empty($userIds)) return false;
- $userMoney = UserStatisticsTotal::find()
- ->select('sum(pay_money) as pay_money, sum(refund_order_money) as refund_order_money, sum(finish_order_money) as finish_order_money')
- ->where(['user_id' => $userIds, 'mall_id' => $user['mall_id']])
- ->asArray()
- ->one();
- } catch (\Exception $e) {
- return false;
- }
- // 订单支付
- // 支付金额等于付款金额减去退款金额
- $money = bcsub($userMoney['pay_money'], $userMoney['refund_order_money'], 2);
- if ($system['order_type'] === self::ORDER_TYPE_PAY && $money < $system['money']) {
- return false;
- }
- // 订单完成
- if ($system['order_type'] === self::ORDER_TYPE_FINISH && $userMoney['finish_order_money'] < $system['money']) {
- return false;
- }
- return true;
- }
- }
|