30], [['remark'], 'string', 'max' => 1000], ]; } /** * {@inheritdoc} */ public function attributeLabels() { return [ 'id' => 'ID', 'mall_id' => 'Mall ID', 'status' => 'Status', 'created_at' => 'Created At', 'updated_at' => 'Updated At', 'user_id' => 'User ID', 'buy_user_id' => 'Buy User ID', 'order_id' => 'Order ID', 'order_no' => 'Order No', 'order_amount' => 'Order Amount', 'remark' => 'Remark', 'settle_status' => 'Settle Status', 'commission' => 'Commission', 'commission_type' => 'Commission Type', ]; } public function getUser() { return $this->hasOne(User::class, ['id' => 'user_id'])->select('id, username, nickname, avatar_url, mobile'); } public function getSharer() { return $this->hasOne(WechatVideoShopSharer::class, ['user_id' => 'user_id']); } public function getBuyUser() { return $this->hasOne(User::class, ['id' => 'buy_user_id'])->select('id, username, nickname, avatar_url, mobile'); } public function getOrder() { return $this->hasOne(Order::class, ['id' => 'order_id']); } /** * 导出字段 */ public static function exportFieldsList($field = null) { return [ 'user_id' => '用户ID', 'nickname' => '昵称', 'mobile' => '手机号', 'created_at' => '时间', 'commission' => '佣金', 'commission_type' => '佣金类型', 'settle_status' => '结算状态', 'level' => '分销员等级', 'order_no' => '订单号', 'order_amount' => '订单金额', 'goods_name' => '商品名称' ]; } public static function fieldsList($field = null) { $fields = [ 'user_id' => '用户ID', 'nickname' => '昵称', 'mobile' => '手机号', 'created_at' => '时间', 'commission' => '佣金', 'commission_type' => '佣金类型', 'settle_status' => '结算状态', 'level' => '分销员等级', 'order_no' => '订单号', 'order_amount' => '订单金额', 'goods_name' => '商品名称' ]; 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['level'])) $model->andWhere(['=', 'level', $params['level']]); if (!empty($params['keyword'])) { $condition['where'] = [[ 'or', ['like', 'nickname', $params['keyword']], ['like', 'mobile', $params['keyword']], ['id' => $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['date_start']) { $model->andWhere(['>=', 'created_at', $params['date_start']]); } if ($params['date_end']) { $model->andWhere(['<=', 'created_at', $params['date_end']]); } if ($params['order_no']) { $order_list = Order::lists([ 'where' => [ ['mall_id' => $params['mall_id']], ['like', 'order_no', $params['order_no']] ], 'select' => 'id', ]); $model->andWhere(['order_id' => array_column($order_list, 'id') ?? []]); } $model->with(['user', 'sharer' => function($query) use($params) { $query->andWhere(['mall_id' => $params['mall_id']]); }]); $levels = WechatVideoShopLevel::lists([ 'where' => [ ['mall_id' => $params['mall_id']], ], 'index' => 'level' ]); //遍历组装数据 foreach ($model->asArray()->batch() as $list) { $order_detail_list = OrderDetail::lists([ 'where' => [ ['mall_id' => $params['mall_id']], ['order_id' => array_column($list, 'order_id') ?? []] ], 'select' => 'id, goods_name, order_id', 'index' => 'order_id', 'with' => [ 'order' => function ($query) { return $query->select('id, order_no'); } ] ]); //查询上级昵称数组 $csv->data = array_map(function ($agent) use ($fields, $order_detail_list, $params, $levels) { $row = []; //遍历所选字段 foreach ($fields as $field) { if (in_array($field, ['created_at'])) { $row[] = !empty($agent[$field]) ? date('Y-m-d H:i:s', $agent[$field]) : ''; } elseif (in_array($field, ['nickname', 'mobile'])) { $row[] = !empty($agent['user'][$field]) ? $agent['user'][$field] : ''; } elseif (in_array($field, ['level'])) { $row[] = !empty($levels[$agent['sharer']['level']]) ? $levels[$agent['sharer']['level']]['name'] : ''; } elseif (in_array($field, ['order_no'])) { $row[] = isset($order_detail_list[$agent['order_id']]) ? $order_detail_list[$agent['order_id']]['order']['order_no'] : ''; } elseif (in_array($field, ['goods_name'])) { $row[] = isset($order_detail_list[$agent['order_id']]) ? $order_detail_list[$agent['order_id']]['goods_name'] : ''; } elseif (in_array($field, ['commission'])) { $row[] = !empty($agent[$field]) ? bcmul($agent[$field], 0.01, 2) : 0; } elseif (in_array($field, ['commission_type'])) { $row[] = !empty($agent[$field]) ? WechatVideoShopEnums::getRewardTypeMap($params['mall_id'], $agent[$field]) : '未知类型'; } elseif (in_array($field, ['settle_status'])) { $row[] = !empty($agent[$field]) ? (WechatVideoShopEnums::SETTLE_STATUS_REMARKS[$agent[$field]] ?? '未知状态') : '未知状态'; } else { $row[] = !empty($agent[$field]) ? $agent[$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; } } } }