request; if ($request->isAjax) { // 提交等级内容 if ($request->isPost) { $params = Yii::$app->request->post(); $res = Yii::$app->services->validate->validate($params, [ [['page','user_id','user_name','mobile','date_start','date_end','from_type'], 'safe'], ]); if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage()); $user_ids = []; $user_name_ids = []; $is_search = false; if(!empty($params['user_id'])) $where[] = ['=','user_id',$params['user_id']]; if(!empty($params['mobile'])) { $user_ids[] = User::find()->select("id")->where(['mall_id' => $this->mall_id,'mobile' => $params['mobile']])->scalar(); $is_search = true; } if(!empty($params['user_name'])) { $user_where[] = ['like','nickname',$params['user_name'] . '%', false]; $select = 'id,nickname'; $user_list = User::lists(['where' =>$user_where,'select' => $select],true); $user_name_ids = array_column($user_list,'id'); $is_search = true; } if($is_search){ $all_user_ids = array_merge($user_ids,$user_name_ids); if(!$all_user_ids){ $all_user_ids = [-1]; } $where[] = ['in','user_id',$all_user_ids]; } if(!empty($params['from_type'])) $where[] = ['=','from_type',$params['from_type']]; if($params['date_start'] && $params['date_end']) $where[] = ['between', 'created_at', strtotime($params['date_start']),strtotime($params['date_end'])]; $where[] = ['=','mall_id',$this->mall_id]; $where[] = ['>=','status',StatusEnum::DISABLED]; $order = 'id desc'; // $list = ScoreExpansionLog::listPage(['where' => $where,'order' => $order,'with' => ['user']],false); $list = ScoreExpansionWalletLog::listPage(['where' => $where,'order' => $order,'with' => ['user']],false); // echo '
';print_r($where);die;
                    $list['from_type'] = ScoreExpansionEnum::getFromType('', $this->mall_id);
                    $list['redpack_type'] = ScoreExpansionEnum::getRedpackText('', $this->mall_id);
                    $list['score_name'] = Yii::$app->services->setting->addons->get($this->mall_id, AddonsEnum::ADDONS_NAME_SCORE_EXPANSION, 'basic.score_name') ?? '积分';
                    return $this->success('操作成功',$list);
                }
            }
        } catch (\Exception $e) {
            return $this->error($e->getMessage(), []);
        }
        return $this->render($this->action->id);
    }

    /**
     * 积分统计
     * @Author: wu
     * @DateTime: 2023/2/21 0015 15:00
     * @Copyright: copyright (c) 2023 广东七件事集团
     * @return string|\yii\web\Response
     */
    public function actionTotal()
    {
        $integral_wallet_type = 'frozen_integral';
        $frozen_integral_add = ScoreExpansionWalletLog::find()->where(['mall_id' => $this->mall_id, 'integral_wallet_type' => $integral_wallet_type, 'change_type' => 'add'])->sum('change_num');
        $frozen_integral_sub = ScoreExpansionWalletLog::find()->where(['mall_id' => $this->mall_id, 'integral_wallet_type' => $integral_wallet_type, 'change_type' => 'sub'])->sum('change_num');
        $total_frozen_integral = ($frozen_integral_add ?? 0) - ($frozen_integral_sub ?? 0);
        $integral_wallet_type = 'integral';
        $integral_add = ScoreExpansionWalletLog::find()->where(['mall_id' => $this->mall_id, 'integral_wallet_type' => $integral_wallet_type, 'change_type' => 'add'])->sum('change_num');
        $integral_sub = ScoreExpansionWalletLog::find()->where(['mall_id' => $this->mall_id, 'integral_wallet_type' => $integral_wallet_type, 'change_type' => 'sub'])->sum('change_num');
        $total_integral = ($integral_add ?? 0) - ($integral_sub ?? 0);
        return $this->success('操作成功', ['total' => ['total_frozen_integral' => $total_frozen_integral, 'total_integral' => $total_integral]]);
    }


    public function actionGetImportLog()
    {
        $params = Yii::$app->request->get();
        $params['where'] = [
            ['mall_id' => $this->mall_id],
            ['source' => ScoreExpansionWallet::class]
        ];
        $params['order'] = 'id desc';
        $data = ImportLog::listPage($params, false, true);
        if (!empty($data['list'])) {
            foreach ($data['list'] as &$v) {
                $v['url'] = '';
                if ($v['fail_counts'])
                    $v['url'] = Yii::$app->urlManager->createUrl(['score-expansion/score-log/export-fail', 'import_log_id' => $v['id']]);
            }
        }
        return $this->success('操作成功', $data);
    }

    /**
     * @return string|void
     */
    public function actionImport()
    {
        if (!$this->request->isAjax) {
            return $this->render($this->action->id);
        }

        if (!$this->request->isPost) {
            return;
        }

        set_time_limit(0);
        $params = Yii::$app->request->post();
        $res = Yii::$app->services->validate->validate($params, [
            [['current_num'], 'required'],
            [['file'], 'file', 'extensions' => ['csv']],
            [['file_path'], 'string'],
            [['sync'], 'safe'],
        ]);
        if ($res === false) {
            return $this->error(Yii::$app->services->validate->getErrorMessage());
        }
        $params['mall_id'] = $this->mall_id;
        $res = ScoreImportLogic::importSync($params);
        if ($res === false) {
            return $this->error(ScoreImportLogic::getStaticError());
        }
        if (empty($res['fail_counts'])) {
            return $this->success('操作成功', $res);
        }
        if (!empty($res['fail_counts'])) {
            return $this->error('导入失败' . $res['fail_counts'] . '条');
        }
    }

    /**
     * 导出失败日志
     */
    public function actionExportFail()
    {
        $params = Yii::$app->request->get();
        $params['head_list'] = ScoreExpansionEnum::HEAD_LIST;
        ImportFailLog::exportFailLog($params);
    }

    /**
     * 导入的模板
     */
    public function actionImportTemplate()
    {
        $csv = new CsvExport();
        $file_name = '积分批量导入模板-' . date('YmdHis', time());
        return $csv->export([], array_column(ScoreExpansionEnum::HEAD_LIST, 'field_name'), $file_name);
    }
}