100], [['address','remark'], 'string', 'max' => 255], [['mobile'], 'string', 'max' => 20], ]; } /** * {@inheritdoc} */ public function attributeLabels() { return [ 'id' => 'ID', 'user_id' => 'User ID', 'mall_id' => 'Mall ID', 'order_id' => 'Order ID', 'order_no' => 'Order No', 'pay_money' => 'Pay Money', 'type' => 'Type', 'head_type' => 'Head Type', 'head_name' => 'Head Name', 'account' => 'Account', 'email' => 'Email', 'address' => 'Address', 'mobile' => 'Mobile', 'tax_no' => 'Tax No', 'status' => 'Status', 'created_at' => 'Created At', 'updated_at' => 'Updated At', ]; } public function getUser(): \yii\db\ActiveQuery { return $this->hasOne(User::class, ['id' => 'user_id']); } /** * 保存数据 * @param array $params * @return bool|UserWallet */ public static function setData(array $params){ try{ if(!empty($params['id'])){ $model = self::findOne(['and',['id' => $params['id'],'mall_id' => $params['mall_id']],['<>','status',StatusEnum::DELETE]]); if(empty($model)) throw new \Exception('数据不存在或已删除'); }else{ $model = new self(); $model->mall_id = $params['mall_id']; $model->loadDefaultValues(); } if(!$model->load($params,'') || !$model->save()){ throw new \Exception($model->getErrorMessage()); } return $model->toArray(); }catch(\Exception $e){ self::$static_error = $e->getMessage(); return false; } } /** * 导出字段 */ public static function exportFieldsList($field = null) { $fields = [ 'user_id' => '用户ID', 'nickname' => '昵称', 'mobile' => '手机号', 'order_no' => '订单编号', 'pay_money' => '发票金额', 'type' => '发票类型', 'head_type' => '抬头类型', 'head_name' => '发票抬头', 'bank_name' => '开户银行', 'account' => '开户账户', 'tax_no' => '税号', 'email' => '邮箱号', 'address' => '企业地址', 'mobile' => '电话', 'invoice_status' => '开票状态', 'created_at' => '申请时间', 'remark' => '备注', ]; return $fields; } /** * 导出字段 */ public static function fieldsList($field = null) { $fields = [ 'user_id' => '用户ID', 'nickname' => '昵称', 'mobile' => '手机号', 'order_no' => '订单编号', 'pay_money' => '发票金额', 'type' => '发票类型', 'head_type' => '抬头类型', 'head_name' => '发票抬头', 'bank_name' => '开户银行', 'account' => '开户账户', 'tax_no' => '税号', 'email' => '邮箱号', 'address' => '企业地址', 'mobile' => '电话', 'invoice_status' => '开票状态', 'created_at' => '申请时间', 'remark' => '备注', ]; if ($field === null) return $fields; $temp = []; foreach ($field as $val) { if (isset($fields[$val])) $temp[$val] = $fields[$val]; } return $temp; } /** * @name: * @msg: 处理导出数据,这个方法是异步队列处理时使用的,请别修改 * @param {array} $data * @return bool */ 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; $fields = $params['fields']; $fields_arr = self::fieldsList(); $have_select_field_arr = []; foreach ($fields as $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; $model = self::find(); $model->where(['mall_id' => $params['mall_id']]); $model->andWhere(['>=', 'status', StatusEnum::DISABLED]); $user_id_arr = []; //拼接查询条件 if (!empty($params['keyword'])) { $condition['where'] = [[ 'or', ['like', 'nickname', $params['keyword']], ['like', 'mobile', $params['keyword']], ]]; $user_list = User::lists($condition); $user_id_arr = array_column($user_list, 'id'); } if ($user_id_arr) { $model->andWhere(['in', 'user_id', $user_id_arr]); } if ($params['user_id']) { $model->andWhere(['=', 'user_id', $params['user_id']]); } if ($params['order_no']) { $model->andWhere(['=', 'order_no', $params['order_no']]); } if ($params['head_type'] != -1) { $model->andWhere(['=', 'head_type', $params['head_type']]); } if ($params['invoice_status'] != -1) { $model->andWhere(['=', 'invoice_status', $params['invoice_status']]); } $model->with(['user'])->orderBy('created_at desc'); //遍历组装数据 foreach ($model->asArray()->batch() as $list) { //查询上级昵称数组 $csv->data = array_map(function ($model) use ($fields) { $row = []; //遍历所选字段 foreach ($fields as $field) { if (in_array($field, ['created_at'])) { $row[] = !empty($model[$field]) ? date('Y-m-d H:i:s', $model[$field]) : ''; } elseif (in_array($field, ['nickname', 'mobile'])) { $row[] = !empty($model['user'][$field]) ? $model['user'][$field] : ''; } elseif (in_array($field, ['type'])) { $text = ''; if($model['type'] == 1) $text = '增值税电子普通发票'; $row[] = $text; }elseif (in_array($field, ['head_type'])) { $text = ''; if($model['head_type'] == 1) $text = '个人/事业单位'; if($model['head_type'] == 2) $text = '企业'; $row[] = $text; }elseif (in_array($field, ['invoice_status'])) { $text = ''; if($model['invoice_status'] == 0) $text = '待开票'; if($model['invoice_status'] == 1) $text = '已开票'; if($model['invoice_status'] == 2) $text = '已驳回'; $row[] = $text; } else { $row[] = !empty($model[$field]) ? $model[$field] : ''; } } return $row; }, $list); $csv->export(); } $csv->updateDown($download, $params['mall_id']); return true; } catch (\Exception $e) { $download->status = 3; $res = $download->save(); var_dump($e->getMessage()); self::setStaticError($e->getMessage()); return false; } } } }