| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- <?php
- namespace addons\RealAuth\common\models;
- use common\components\export\CsvTool;
- use common\enums\DownloadEnum;
- use common\models\base\User;
- use common\models\common\Download;
- use Yii;
- use yii\db\ActiveRecord;
- /**
- * This is the model class for table "{{%addons_real_auth_user}}".
- * @property int $id
- * @property int $mall_id
- * @property int $user_id
- * @property int $id_type
- * @property string $id_no
- * @property int $auth
- * @property int $created_at
- * @property int $updated_at
- * @property string $real_name
- * @property string $auth_result
- */
- class RealAuthUser extends \common\models\base\BaseActiveRecord
- {
- //认证类型 1 个人 2 企业
- const USER = 1;
- const BUSINESS = 2;
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_real_auth_user}}';
- }
- public static function fieldsList($field = null)
- {
- $fields = [
- 'user_id' => '会员ID',
- 'nickname' => '昵称',
- 'username' => '用户名',
- 'mobile' => '手机号',
- 'real_name' => '姓名',
- 'id_no' => '证件号',
- 'created_at' => '认证时间',
- ];
- if ($field === null) return $fields;
- $temp = [];
- foreach ($field as $val) {
- if (isset($fields[$val])) $temp[$val] = $fields[$val];
- }
- return $temp;
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'user_id', 'id_type', 'auth', 'created_at','updated_at','auth_type'], 'integer'],
- [['real_name','id_no'], 'string', 'max' => 64],
- [['auth_result','business_code'], 'string'],
- ];
- }
- public static function getUser($mall_id,$user_id,$auth_type=0)
- {
- $query = self::find()->where(['mall_id' => $mall_id,'user_id' =>$user_id]);
- if($auth_type){
- $query->andWhere(['=','auth_type',$auth_type]);
- }
- return $query->one();
- }
- public static function getUserByInfo($mall_id, $user_id, $real_name, $id_no, $auth_type=0)
- {
- $query = self::find()->where(['mall_id' => $mall_id,'user_id' =>$user_id, 'real_name' => trim($real_name), 'id_no' => trim($id_no)]);
- if($auth_type){
- $query->andWhere(['=','auth_type',$auth_type]);
- }
- return $query->one();
- }
- /**
- * @param array $data
- * @return int
- * @throws \Exception
- */
- public static function setData(array $data)
- {
- if(isset($data['id']) && $data['id'] > 0 ){
- $model = self::findOne([ 'id' => $data['id']]);
- if(!$model){
- throw new \Exception('记录不存在');
- }
- }else{
- $model = self::findOne(['mall_id' => $data['mall_id'], 'user_id' => $data['user_id']]);
- if(!$model){
- $model = new self();
- }
- }
- $model->attributes = $data;
- if (!$model->save()) {
- throw new \Exception($model->getErrorMessage());
- }
- return $model->id;
- }
- public static function exportHandle(array $data)
- {
- date_default_timezone_set('Asia/Shanghai');
- $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;
- $model = RealAuthUser::find();
- $model->where(['mall_id' => $params['mall_id']]);
- if (!empty($params['user_id'])) $model->andWhere(['=', 'user_id', $params['user_id']]);
- if (!empty($params['real_name'])) $model->andWhere(['=', 'real_name', $params['real_name']]);
- if (!empty($params['id_no'])) $model->andWhere(['=', 'id_no', $params['id_no']]);
- if (!empty($params['data'])) {
- list($start, $end) = $params['data'];
- $end .= ' 23:59:59';
- $model->andWhere(['between', 'created_at', strtotime($start), strtotime($end)]);
- }
- $fields = $params['fields'];
- $fields_arr = self::fieldsList();
- $have_select_field_arr = [];
- foreach ($fields as $value) {
- if (isset($fields_arr[$value])) $have_select_field_arr[] = $fields_arr[$value];
- }
- $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;
- $i = 1;
- $page_size = 1000;
- while ($list = $model
- ->offset(($i - 1) * $page_size)->limit($page_size)->orderBy('id asc')->all()) {
- $user_ids = array_column($list, 'user_id');
- $users = User::lists(['where' => [['id' => $user_ids]], 'index' => 'id']);
- $csv->data = array_map(function ($real_auth_user) use ($fields, $users) {
- $row = [];
- foreach ($fields as $field) {
- switch (true) {
- case (in_array($field, ['created_at'])):
- $row[] = !empty($real_auth_user[$field]) ? date('Y-m-d H:i:s', $real_auth_user[$field]) : '';
- break;
- case (in_array($field, ['id_no'])):
- $row[] = !empty($real_auth_user[$field]) ? $real_auth_user[$field] . "\t" : '';
- break;
- case (in_array($field, ['nickname', 'username', 'mobile'])):
- if (isset($users[$real_auth_user['user_id']])) {
- $row[] = $users[$real_auth_user['user_id']][$field] . "\t";
- } else {
- $row[] = '';
- }
- break;
- default:
- $row[] = !empty($real_auth_user[$field]) ? $real_auth_user[$field] : '';
- }
- }
- return $row;
- }, $list);
- $csv->export();
- $i++;
- }
- $csv->updateDown($download, $params['mall_id']);
- return true;
- } catch (\Exception $e) {
- $download->status = 3;
- $res = $download->save();
- \Yii::error($e->getMessage() . $e->getFile() . $e->getLine());
- self::setStaticError($e->getMessage());
- return $e->getMessage();
- }
- }
- }
- }
|