   find: (data, cb)->
     self = @
     cond = {}
-    cond.tag = data.tag
     cond.id = data.id
     cond.project_id = data.project_id
 
     #选项
     options =
       pagination: limit: data.limit, offset: data.offset
-      orderBy: timestamp: 'DESC'
+      orderBy: 'issue.status': 'desc', 'issue.timestamp': 'DESC'
       fields: (query)->
-        query.select query.knex.raw('*, (SELECT realname FROM member WHERE member.id = issue.owner) AS realname')
+        query.select ['issue.*', 'project.title AS project_name']
+        #query.select query.knex.raw('*, (SELECT realname FROM member WHERE member.id = issue.owner) AS realname')
 
       #在查询之前，对query再处理
       beforeQuery: (query)->
+        query.join('project', ()->
+          this.on 'issue.project_id', '=', 'project.id'
+        , 'left')
+
         query.limit data.limit || 10
         query.offset data.offset || 0
         #只取未完成的
         if(data.status is 'undone')
-          query.where 'status', '<>', 'done'
-          query.where 'status', '<>', 'trash'
+          query.where 'issue.status', '<>', 'done'
+          query.where 'issue.status', '<>', 'trash'
         else if data.status
-          query.where 'status', data.status
+          query.where 'issue.status', data.status
+        else
+          #默认是不获取trash的数据
+          query.where 'issue.status', '<>', 'trash'
 
         #指定标签
-        query.where 'tag', data.tag if data.tag
+        query.where 'issue.tag', data.tag if data.tag
+        #这里不查义project的tag
+        query.where 'issue.tag', '<>', 'project'
         #指定完成时间段
         #query.where 'finish_time', '>=', data.beginTime if data.beginTime
         #query.where 'finish_time', '<=', data.endTime if data.endTime
-        self.queryTimeRange query, 'finish_time', data.finish_time
-        self.queryTimeRange query, 'timestamp', data.timestamp
+        self.queryTimeRange query, 'issue.finish_time', data.finish_time
+        self.queryTimeRange query, 'issue.timestamp', data.timestamp
 
         #指定责任人
-        query.where 'owner', data.owner if data.owner
-
-        query.orderBy 'status', 'desc'
-        query.orderBy 'timestamp', 'desc'
+        query.where 'issue.owner', data.owner if data.owner isnt undefined
 
 
     super cond, options, cb


    #http://127.0.0.1:14318/api/project/1/discussion

module.exports = Issue
_AssetIssueRelation = require './asset_issue_relation'
_async = require 'async'
_common = require '../common'
_Member = require './member'
_moment = require 'moment'

#定义一个Project类
class Issue extends _BaseEntity
