text: const Router = require('@koa/router')
  const grpc = require('grpc')
  const protoLoader = require('@grpc/proto-loader')
  const config = require('../config')
  
  const proto = grpc.loadPackageDefinition(
    protoLoader.loadSync(__dirname + '/../proto/offer.proto'), {
    keepCase: true,
    longs: String,
    enums: String,
    defaults: true,
    oneofs: true
  }
  ).offer
  
  const grpcClient = new proto.Offer(
    `${config.grpcServer.host}:${config.grpcServer.port}`,
    grpc.credentials.createInsecure()
  )
  
  
  
  const router = new Router({
    prefix: '/api/offer'
  })
  
  module.exports = router
  
  
  router
    .get('/ent/:id', async ctx => {
      const grpcFetch = body => new Promise((resolve, reject) =>
        grpcClient.entList({ data: JSON.stringify(body) }, (err, response) => {
          if (err) {
            console.error(err)
            reject(err)
            return
          } else {
            resolve(JSON.parse(response.data))
          }
        })
      )
      try {
        ctx.response.body = await grpcFetch(ctx.params)
      } catch (err) {
        console.error(err)
        ctx.response.body = { message: '服务器错误' }
      }
    })
    .get('/common/total/:id/', async ctx => {
      const grpcFetch = body => new Promise((resolve, reject) =>
        grpcClient.commonTotal({ data: JSON.stringify(body) }, (err, response) => {
          if (err) {
            console.error(err)
            reject(err)
            return
          } else {
            resolve(JSON.parse(response.data))
          }
        })
      )
      try {
        ctx.response.body = await grpcFetch(ctx.params)
      } catch (err) {
        console.error(err)
        ctx.response.body = { message: '服务器错误' }
      }
    })
    .get('/common/:id', async ctx => {
      const grpcFetch = body => new Promise((resolve, reject) =>
        grpcClient.commonList({ data: JSON.stringify(body) }, (err, response) => {
          if (err) {
            console.error(err)
            reject(err)
            return
          } else {
            resolve(JSON.parse(response.data))
          }
        })
      )
      try {
        ctx.response.body = await grpcFetch(ctx.params)
      } catch (err) {
        console.error(err)
        ctx.response.body = { message: '服务器错误' }
      }
    })
    .post('/', async ctx => {
      const grpcFetch = body => new Promise((resolve, reject) =>
        grpcClient.insert({ data: JSON.stringify(body) }, (err, response) => {
          if (err) {
            console.error(err)
            reject(err)
            return
          } else {
            resolve(JSON.parse(response.data))
          }
        })
      )
      try {
        ctx.response.body = await grpcFetch(ctx.request.body)
      } catch (err) {
        console.error(err)
        ctx.response.body = { message: '服务器错误' }
      }
    })