| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- <?php
- namespace addons\BossAchievement\common\logic;
- use addons\BossAchievement\common\enums\BossAchievementEnum;
- use addons\BossAchievement\common\models\BossAchievementLevelSetting;
- use addons\BossAchievement\common\models\BossAchievementMonth;
- use addons\BossAchievement\common\models\BossAchievementTeam;
- use addons\BossAchievement\common\models\BossAchievementUser;
- use common\enums\StatusEnum;
- use common\enums\UserWalletEnum;
- use common\models\mall\Mall;
- use common\models\user\UserPartitionRelationship;
- use common\traits\ErrorTrait;
- use Exception;
- use services\common\UserWalletService;
- use yii;
- use yii\helpers\Json;
- class AchievementLogic
- {
- use ErrorTrait;
- /**
- * 统计处理
- * @Author: lun
- * @DateTime: 2022/7/10 0010 16:03
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $order
- */
- public function statistics($order)
- {
- $trans = Yii::$app->db->beginTransaction();
- try {
- // 获取用户所有上级
- $parent_ids = UserPartitionRelationship::getParentIdArr($order['user_id']);
- array_push($parent_ids, $order['user_id']);
- // 检查用户上级是否存在插件中
- $boss_parent_ids = BossAchievementUser::find()->select('user_id')->where(['user_id' => $parent_ids, 'status' => StatusEnum::ENABLED])->column();
- if (empty($boss_parent_ids)) return true;
- // 获取所有的业绩商品
- $goods_ids = BossAchievementLevelSetting::getAllAchievementGoodsIds($order['mall_id']);
- $month = date('Ym', time());
- foreach ($order['detail'] as $item) {
- // 判断订单商品是否在业绩商品中
- if (in_array($item['goods_id'], $goods_ids)) {
- // 增加个人消费业绩记录
- $res = BossAchievementTeam::setData(['mall_id' => $order['mall_id'], 'user_id' => $order['user_id'], 'goods_id' => $item['goods_id'], 'goods_price' => $item['goods_price']]);
- if ($res === false) throw new Exception(BossAchievementTeam::getStaticError());
- // 增加上级所有团队业绩当月业绩
- $res = BossAchievementMonth::batchSetData($boss_parent_ids, ['mall_id' => $order['mall_id'], 'month' => $month, 'goods_price' => $item['goods_price']]);
- if ($res === false) throw new Exception(BossAchievementMonth::getStaticError());
- }
- }
- $trans->commit();
- return true;
- } catch (Exception $e) {
- $trans->rollBack();
- return false;
- }
- }
- /**
- * 定时任务执行
- * @Author: lun
- * @DateTime: 2022/7/10 0010 18:19
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return bool
- */
- public function exec()
- {
- try {
- $mall_list = Mall::getEnableMallList(true);
- if(!empty($mall_list)){
- foreach ($mall_list as $mall){
- $res = $this->settlement($mall['id']);
- if ($res === false) throw new Exception($this->getError());
- }
- }
- return true;
- } catch (Exception $e) {
- $this->setError($e->getMessage());
- return false;
- }
- }
- /**
- * 业绩结算
- * @Author: lun
- * @DateTime: 2022/7/10 0010 16:33
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param int $mall_id
- * @param bool $month 执行指定月份
- */
- public function settlement($mall_id, $month = '')
- {
- $trans = Yii::$app->db->beginTransaction();
- try {
- $day = date('d', time());
- if ($day != 1 && empty($month)) return true;
- // 上个月月份
- $month = $month ?: date("Ym", strtotime("-1 month"));
- $insert_wallet = [];
- // 获取所有等级设置
- $level_arr = BossAchievementLevelSetting::find()->where(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED])->indexBy('level')->asArray()->all();
- // 获取每个等级对应的所有商品
- $achievement_goods_ids = [];
- foreach ($level_arr as $item) {
- $achievement_goods_ids[$item['level']] = Json::decode($item['achievement_goods_ids'], true);
- }
- foreach ($achievement_goods_ids as $level => $goods_ids) {
- // 获取对应等级下的总商品业绩
- $settlement_money = BossAchievementTeam::find()->select('sum(finish_order_money) as total_money')->where(['mall_id' => $mall_id, 'goods_id' => $goods_ids, 'month' => $month, 'is_settlement' => StatusEnum::DISABLED, 'status' => StatusEnum::ENABLED])->scalar();
- if (empty($settlement_money)) continue;// 结算金额为0直接跳过
- // 获取该等级结算的用户
- $users = BossAchievementUser::find()->where(['mall_id' => $mall_id, 'level' => $level, 'status' => StatusEnum::ENABLED])->indexBy('user_id')->asArray()->all();
- if (!empty($users)) {
- // 获取达到业绩的用户
- $all_user_ids = array_column($users, 'user_id');
- $standard_user_ids = BossAchievementMonth::find()->select('user_id')
- ->where(['mall_id' => $mall_id, 'user_id' => $all_user_ids, 'month' => $month, 'status' => StatusEnum::ENABLED])
- ->andWhere(['>=', 'achievement', $level_arr[$level]['standard_money']])
- ->column();
- // 该等级结算人数
- $settlement_num = count($standard_user_ids);
- // 计算平分的金额,保留两位小数向下取整
- $percent = $level_arr[$level]['prize'];
- if (empty($settlement_num) || empty($percent)) {
- $bouns = 0;
- } else {
- $bouns = $settlement_money * $percent / 100 / $settlement_num;
- $bouns = sprintf("%.2f",substr(sprintf("%.3f", $bouns), 0, -1));
- }
- // 用户结算
- foreach ($users as $user_id => $item) {
- if (in_array($user_id, $standard_user_ids)) {
- // 已达标用户处理
- $res = BossAchievementMonth::setData([
- 'mall_id' => $mall_id,
- 'user_id' => $user_id,
- 'month' => $month,
- 'bonus' => $bouns,
- 'settlement_money' => $settlement_money,
- 'settlement_num' => $settlement_num,
- 'standard_money' => $level_arr[$level]['standard_money'],
- 'is_settlement' => StatusEnum::ENABLED
- ]);
- if ($res === false) throw new Exception(BossAchievementMonth::getStaticError());
- // 修改用户累计分红
- $res = BossAchievementUser::updateAllCounters(['commission' => $bouns], ['user_id' => $user_id, 'status' => StatusEnum::ENABLED]);
- if ($res == false) throw new Exception(BossAchievementMonth::getStaticError());
- $insert_wallet[] = [
- 'mall_id' => $mall_id,
- 'user_id' => $user_id,
- 'is_frozen' => false,
- 'wallet_type' => 'commission',
- 'money' => $bouns,
- 'desc' => '股东业绩分红',
- 'source_table' => 'addons_boss_achievement_month',
- 'source_table_id' => '0',
- 'from_type' => BossAchievementEnum::ADDONS_NAME,
- 'capital_type' => UserWalletEnum::BOUNS,
- ];
- } else {
- // 未达标用户处理
- $res = BossAchievementMonth::setData([
- 'mall_id' => $mall_id,
- 'user_id' => $user_id,
- 'month' => $month,
- 'standard_money' => $level_arr[$level]['standard_money'],
- 'is_settlement' => StatusEnum::ENABLED
- ]);
- if ($res === false) throw new Exception(BossAchievementMonth::getStaticError());
- }
- }
- }
- // 将当月的商品改为已结算
- $res = BossAchievementTeam::updateAll(['is_settlement' => StatusEnum::ENABLED], 'goods_id in('.implode(',', $goods_ids).') and month='.$month);
- if ($res === false) throw new Exception(BossAchievementTeam::getStaticError());
- }
- // 插入分红队列
- if ($insert_wallet) {
- $res = UserWalletService::batchHandleByQueue($insert_wallet);
- if (empty($res)) throw new \Exception(UserWalletService::getStaticError());
- }
- $trans->commit();
- return true;
- } catch (Exception $e) {
- $trans->rollBack();
- Yii::error('股东业绩执行失败:' . $e->getMessage());
- return false;
- }
- }
- }
|