| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275 |
- <?php
- namespace addons\Achievement\common\models;
- use common\enums\StatusEnum;
- use common\models\base\BaseActiveRecord;
- use common\models\mall\MallAddons;
- use common\models\user\User;
- use Yii;
- use common\models\common\Download;
- use common\enums\DownloadEnum;
- use common\components\export\CsvTool;
- /**
- * This is the model class for table "qimall_addons_achievement_award_log".
- *
- * @property int $id
- * @property int $mall_id
- * @property int $grant_id 发放规则日志id
- * @property int $user_id 用户id
- * @property float $achievement_sum 总业绩
- * @property float $team_achievement 团队业绩
- * @property float $personal_achievement 个人业绩
- * @property float $award_proportion 奖励比例
- * @property float $award_money 奖励金额
- * @property int|null $is_settlement 是否已结算打款 1:是
- * @property int|null $is_end 是否结束周期:1:是
- * @property float|null $award_sum 奖励总计
- * @property int|null $parent_team 父级团长id
- * @property float|null $proportion_money 比例结算金额
- * @property float|null $actual_money 实际应该结算金额
- * @property int $created_at
- * @property int $updated_at
- * @property int $status 状态[-1:删除;0:禁用;1启用]
- */
- class AchievementAwardLog extends BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return 'qimall_addons_achievement_award_log';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'grant_id', 'user_id', 'is_settlement', 'is_skip_settlement', 'is_end', 'parent_team', 'created_at', 'updated_at', 'status'], 'integer'],
- [['achievement_sum', 'team_achievement', 'personal_achievement', 'award_proportion', 'award_money', 'award_sum', 'proportion_money', 'actual_money','equal_first','equal_second','equal_money','team_equal_first','team_equal_second', 'sub_personal_achievement'], 'number'],
- [['equal_info'], 'string', 'max' => 255],
- [['equal_info'], 'default', 'value' => ''],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => 'Mall ID',
- 'grant_id' => '发放规则日志id',
- 'user_id' => '用户id',
- 'achievement_sum' => '总业绩',
- 'team_achievement' => '团队业绩',
- 'personal_achievement' => '个人业绩',
- 'award_proportion' => '奖励比例',
- 'award_money' => '奖励金额',
- 'is_settlement' => '是否已结算打款 1:是',
- 'is_skip_settlement' => '是否不结算 0:否 1:是',
- 'is_end' => '是否结束周期:1:是',
- 'award_sum' => '奖励总计',
- 'parent_team' => '父级团长id',
- 'proportion_money' => '比例结算金额',
- 'actual_money' => '实际应该结算金额',
- 'created_at' => 'Created At',
- 'updated_at' => 'Updated At',
- 'status' => '状态[-1:删除;0:禁用;1启用]',
- ];
- }
- // 关联模型
- public function getUser()
- {
- return $this->hasOne(User::class, ['id' => 'user_id']);
- }
- public function getGrantLog()
- {
- return $this->hasOne(AchievementGrantLog::class, ['id' => 'grant_id']);
- }
- public function getUpdateLog()
- {
- return $this->hasOne(AchievementAwardLogUpdateLog::class, ['award_id' => 'id'])->andWhere(['<>','status',StatusEnum::DELETE]);
- }
- /**
- * @desc:推广中心-调用
- * @Author: hua
- * @Date: 2021/11/12
- * @Time: 9:30
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param array $params ['user_id' => 1, 'mall_id' => 5, 'addons_name' => 'Agent']
- * @return array
- */
- public static function getIncomeInfo(array $params)
- {
- $is_install = MallAddons::isInstall($params['mall_id'], $params['addons_name']);
- $data['total_commission'] = 0;
- $data['is_install'] = $is_install;
- $basic = \Yii::$app->services->setting->addons->get($params['mall_id'], $params['addons_name'], 'basic');
- $data['is_show_at_center'] = 1;
- if(isset($basic['is_show_at_center'])) $data['is_show_at_center'] = $basic['is_show_at_center'];
- if($is_install){
- $achievement = self::find()->where(['mall_id' => $params['mall_id'], 'user_id' => $params['user_id'], 'status' => StatusEnum::ENABLED])->sum('award_money + equal_money');
- $data['total_commission'] = $achievement??0;
- }
- return $data;
- }
- public static function exportHandle(array $data)
- {
- $download_id = $data['download_id'] ?? 0;
- $download = Download::findOne(['id' => $download_id]);
- if ($download) {
- try {
- $params = json_decode($download->params, true);
- $filename = $download->name;
- $type = $download->type;
- $have_select_field_arr = [
- '周期ID',
- '用户ID',
- '用户昵称',
- '手机号',
- '发放时间',
- '总业绩',
- '团队业绩',
- '个人业绩',
- '奖励比例',
- '平级奖励',
- '结算金额',
- '现结算金额',
- '结算状态',
- ];
- $csv = new CsvTool();
- $csv->filename = $filename;
- $csv->file_dirname = DownloadEnum::getTypeStorageDirMap($type);
- $csv->export_mode = CsvTool::EXPORT_MODE_ASYNC;
- $csv->header = $have_select_field_arr;
- $model = self::find();
- $model->where(['mall_id' => $params['mall_id']]);
- $model->andWhere(['=', 'status', StatusEnum::ENABLED]);
- //拼接查询条件
- if (!empty($params['grant_id'])) {
- $model->andWhere(['=', 'grant_id', $params['grant_id']]);
- }
- $is_search = 0;
- $user_id_arr = [];
- if (!empty($params['keyword'])) {
- $is_search = 1;
- $user_list = User::lists(['where' => [['mall_id' => $params['mall_id']], [
- 'or',
- ['=', 'id', $params['keyword']],
- ['like', 'nickname', $params['keyword']],
- ['like', 'mobile', $params['keyword']]
- ]], 'select' => 'id']);
- $user_ids = array_column($user_list, 'id');
- if (!empty($user_id_arr)) {
- $user_id_arr = array_intersect($user_id_arr, $user_ids);
- } else {
- $user_id_arr = $user_ids;
- }
- }
- if ($is_search) {
- $model->andWhere(['in', 'user_id', $user_id_arr]);
- }
- $model->with(['grantLog' => function ($query) {
- }, 'user', 'updateLog']);
- $model->orderBy('id desc');
- //遍历组装数据
- foreach ($model->asArray()->batch() as $list) {
- $result = [];
- foreach ($list as $item) {
- $user = $item['user'];
- $before_award_money = $item['updateLog']['before_award_money'] ?? $item['award_money'];
- $result[] = [
- 'grant_id' => $item['grant_id'],
- 'user_id' => $item['user_id'],
- 'nickname' => $user['nickname'] ?? '-',
- 'mobile' => $user['mobile'] ?? '',
- 'settlement_time' => !empty($item['grantLog']['settlement_time']) ? date('Y-m-d H:i:s', $item['grantLog']['settlement_time']) : '',
- 'achievement_sum' => $item['achievement_sum'] ?? '0.00',
- 'team_achievement' => $item['team_achievement'] ?? '0.00',
- 'personal_achievement' => $item['personal_achievement'] ?? '0.00',
- 'award_proportion' => $item['award_proportion'],
- 'equal_money' => $item['equal_money'] < 0 ? '-' . $item['equal_money'] : $item['equal_money'],
- 'before_award_money' => $before_award_money < 0 ? '-' . $before_award_money : $before_award_money,
- 'award_money' => $item['award_money'] < 0 ? '-' . $item['award_money'] : $item['award_money'],
- 'settle_status' => $item['is_skip_settlement'] == 1 ? '不结算' : ($item['is_settlement'] == 1 ? '已结算' : '未结算')
- ];
- }
- $csv->data = $result;
- $csv->export();
- }
- $csv->updateDown($download, $params['mall_id']);
- return true;
- } catch (\Exception $e) {
- $download->status = 3;
- $res = $download->save();
- self::setStaticError($e->getMessage());
- return false;
- }
- }
- }
- public static function updateSkipSettlement($grant_id)
- {
- try {
- $list = self::find()
- ->where([
- 'grant_id' => $grant_id,
- 'is_skip_settlement' => 1,
- 'status' => StatusEnum::ENABLED
- ])
- ->all();
- if (empty($list)) {
- return true;
- }
- $insert_rows = [];
- $now = time();
- $t = \Yii::$app->db->beginTransaction();
- foreach ($list as $item) {
- $insert_rows = [
- 'mall_id' => $item['mall_id'],
- 'grant_id' => $item['grant_id'],
- 'award_id' => $item['id'],
- 'before_award_money' => $item['award_money'],
- 'after_award_money' => 0,
- ];
- $item->award_money = 0;
- if (!$item->save()) throw new \Exception($item->getErrorMessage());
- AchievementAwardLogUpdateLog::setData($insert_rows);
- }
- $t->commit();
- return true;
- } catch (\Exception $e) {
- $t->rollBack();
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- }
|