作者 tangxuhui

导出数据 功能优化

@@ -143,3 +143,16 @@ func GetAuditTemplateSort(companyId int64, chanceTypeId int) (v *ChanceType, err @@ -143,3 +143,16 @@ func GetAuditTemplateSort(companyId int64, chanceTypeId int) (v *ChanceType, err
143 } 143 }
144 return 144 return
145 } 145 }
  146 +
  147 +// GetAuditTemplateByIds retrieves AuditTemplate by Id.
  148 +func GetAuditTemplateByIds(ids []int64) (data []AuditTemplate, err error) {
  149 + data = []AuditTemplate{}
  150 + if len(ids) == 0 {
  151 + return data, nil
  152 + }
  153 + o := orm.NewOrm()
  154 + _, err = o.QueryTable(&AuditTemplate{}).
  155 + Filter("id__in", ids).
  156 + All(&data)
  157 + return data, err
  158 +}
@@ -54,6 +54,19 @@ func GetChanceReserveTypeById(id int) (m *ChanceReserveType, err error) { @@ -54,6 +54,19 @@ func GetChanceReserveTypeById(id int) (m *ChanceReserveType, err error) {
54 err = o.Read(m) 54 err = o.Read(m)
55 return m, err 55 return m, err
56 } 56 }
  57 +
  58 +func GetChanceReserveTypeByIds(ids []int) (data []ChanceReserveType, err error) {
  59 + data = []ChanceReserveType{}
  60 + if len(ids) == 0 {
  61 + return data, nil
  62 + }
  63 + o := orm.NewOrm()
  64 + _, err = o.QueryTable(&ChanceReserveType{}).
  65 + Filter("id__in", ids).
  66 + All(&data)
  67 + return data, err
  68 +}
  69 +
57 func HasChanceReserveTypeName(name string, companyid int64, notId int) bool { 70 func HasChanceReserveTypeName(name string, companyid int64, notId int) bool {
58 var bl bool 71 var bl bool
59 o := orm.NewOrm() 72 o := orm.NewOrm()
@@ -111,3 +111,17 @@ func GetChanceTypeMaxSort(companyId int64) (v *ChanceType, err error) { @@ -111,3 +111,17 @@ func GetChanceTypeMaxSort(companyId int64) (v *ChanceType, err error) {
111 } 111 }
112 return 112 return
113 } 113 }
  114 +
  115 +// GetChanceTypeById retrieves ChanceType by Id. Returns error if
  116 +// Id doesn't exist
  117 +func GetChanceTypeByIds(ids []int) (data []ChanceType, err error) {
  118 + data = []ChanceType{}
  119 + if len(ids) == 0 {
  120 + return data, nil
  121 + }
  122 + o := orm.NewOrm()
  123 + _, err = o.QueryTable(&ChanceType{}).
  124 + Filter("id__in", ids).
  125 + All(&data)
  126 + return data, err
  127 +}
@@ -183,17 +183,15 @@ func GetAuditListSql(param protocol.RequestAuditList, companyid int64, userid in @@ -183,17 +183,15 @@ func GetAuditListSql(param protocol.RequestAuditList, companyid int64, userid in
183 ) 183 )
184 s1 := `SELECT a.id,a.department_id,a.audit_template_id,a.chance_type_id 184 s1 := `SELECT a.id,a.department_id,a.audit_template_id,a.chance_type_id
185 ,a.publish_status,a.create_at,a.review_status,a.status 185 ,a.publish_status,a.create_at,a.review_status,a.status
186 - ,a.discovery_score,a.comment_total ,a.code,d.nick_name,d.id as user_id 186 + ,a.discovery_score,a.comment_total ,a.code,c.nick_name
187 ,a.type,a.reserve_type_id,a.source_content,a.approve_time,a.check_result 187 ,a.type,a.reserve_type_id,a.source_content,a.approve_time,a.check_result
188 ,a.self_checks,a.check_result_status 188 ,a.self_checks,a.check_result_status
189 FROM chance AS a 189 FROM chance AS a
190 JOIN user_company AS c ON c.id = a.user_id 190 JOIN user_company AS c ON c.id = a.user_id
191 - JOIN user AS d ON c.user_id = d.id  
192 %s 191 %s
193 where a.company_id=? AND a.enable_status=1 ` 192 where a.company_id=? AND a.enable_status=1 `
194 s2 := ` SELECT count(*) FROM chance as a 193 s2 := ` SELECT count(*) FROM chance as a
195 JOIN user_company AS c ON c.id = a.user_id 194 JOIN user_company AS c ON c.id = a.user_id
196 - JOIN user AS d ON c.user_id = d.id  
197 %s 195 %s
198 where a.company_id=? AND a.enable_status=1 ` 196 where a.company_id=? AND a.enable_status=1 `
199 if companyinfo, err := models.GetCompanyById(companyid); err == nil { 197 if companyinfo, err := models.GetCompanyById(companyid); err == nil {
@@ -298,8 +296,8 @@ func GetAuditListSql(param protocol.RequestAuditList, companyid int64, userid in @@ -298,8 +296,8 @@ func GetAuditListSql(param protocol.RequestAuditList, companyid int64, userid in
298 296
299 func GetAuditList(param protocol.RequestAuditList, companyid int64, userid int64) (protocol.ResponseAuditList, error) { 297 func GetAuditList(param protocol.RequestAuditList, companyid int64, userid int64) (protocol.ResponseAuditList, error) {
300 type SqlData struct { 298 type SqlData struct {
301 - Id int64 `orm:"column(id)"`  
302 - UserId int64 `orm:"column(user_id)"` 299 + Id int64 `orm:"column(id)"`
  300 + // UserId int64 `orm:"column(user_id)"`
303 NickName string `orm:"column(nick_name)"` 301 NickName string `orm:"column(nick_name)"`
304 DepartmentId int64 `orm:"column(department_id)"` 302 DepartmentId int64 `orm:"column(department_id)"`
305 AuditTemplateId int64 `orm:"column(audit_template_id)"` 303 AuditTemplateId int64 `orm:"column(audit_template_id)"`
@@ -38,13 +38,77 @@ func GetAuditListForExcel(param protocol.RequestAuditList, companyid int64, user @@ -38,13 +38,77 @@ func GetAuditListForExcel(param protocol.RequestAuditList, companyid int64, user
38 // FROM chance AS a 38 // FROM chance AS a
39 // JOIN user_company AS c ON c.id = a.user_id 39 // JOIN user_company AS c ON c.id = a.user_id
40 // JOIN user AS d ON c.user_id = d.id 40 // JOIN user AS d ON c.user_id = d.id
  41 + var (
  42 + reserveTypeIdMap = map[int]struct{}{}
  43 + chanceTypeIdMap = map[int]struct{}{}
  44 + auditTemplateIdMap = map[int64]struct{}{}
  45 + departmentIdMap = map[int64]struct{}{}
  46 + )
  47 + for i := range soureData {
  48 + reserveTypeIdStr := fmt.Sprint(soureData[i]["reserve_type_id"])
  49 + reserveTypeId, _ := strconv.Atoi(reserveTypeIdStr)
  50 + reserveTypeIdMap[reserveTypeId] = struct{}{}
  51 + chanceTypeIdStr := fmt.Sprint(soureData[i]["chance_type_id"])
  52 + chanceTypeId, _ := strconv.Atoi(chanceTypeIdStr)
  53 + chanceTypeIdMap[chanceTypeId] = struct{}{}
  54 + auditTemplateIdStr := fmt.Sprint(soureData[i]["audit_template_id"])
  55 + auditTemplateId, _ := strconv.ParseInt(auditTemplateIdStr, 10, 64)
  56 + auditTemplateIdMap[auditTemplateId] = struct{}{}
  57 + departmentIdStr := fmt.Sprint(soureData[i]["department_id"])
  58 + departmentId, _ := strconv.ParseInt(departmentIdStr, 10, 64)
  59 + departmentIdMap[departmentId] = struct{}{}
  60 + }
  61 + var (
  62 + reserveTypeIds []int
  63 + chanceTypeIds []int
  64 + auditTemplateIds []int64
  65 + departmentIds []int64
  66 + )
  67 + for k := range reserveTypeIdMap {
  68 + reserveTypeIds = append(reserveTypeIds, k)
  69 + }
  70 + for k := range chanceTypeIdMap {
  71 + chanceTypeIds = append(chanceTypeIds, k)
  72 + }
  73 + for k := range auditTemplateIdMap {
  74 + auditTemplateIds = append(auditTemplateIds, k)
  75 + }
  76 + for k := range departmentIdMap {
  77 + departmentIds = append(departmentIds, k)
  78 + }
  79 + var (
  80 + reserveTypes []models.ChanceReserveType
  81 + chanceTypes []models.ChanceType
  82 + auditTemplates []models.AuditTemplate
  83 + departments []models.Department
  84 + )
  85 + reserveTypes, _ = models.GetChanceReserveTypeByIds(reserveTypeIds)
  86 + chanceTypes, _ = models.GetChanceTypeByIds(chanceTypeIds)
  87 + auditTemplates, _ = models.GetAuditTemplateByIds(auditTemplateIds)
  88 + departments, _ = models.GetDepartmentByIds(departmentIds)
41 //进行数据整理 89 //进行数据整理
42 var ( 90 var (
43 - reserveTypeCache = make(map[string]*models.ChanceReserveType)  
44 - chanceTypeCache = make(map[string]*models.ChanceType)  
45 - auditTemplateCache = make(map[string]*models.AuditTemplate)  
46 - departmentCache = make(map[string]*models.Department) 91 + reserveTypeCache = make(map[string]models.ChanceReserveType)
  92 + chanceTypeCache = make(map[string]models.ChanceType)
  93 + auditTemplateCache = make(map[string]models.AuditTemplate)
  94 + departmentCache = make(map[string]models.Department)
47 ) 95 )
  96 + for i := range reserveTypes {
  97 + idString := strconv.Itoa(reserveTypes[i].Id)
  98 + reserveTypeCache[idString] = reserveTypes[i]
  99 + }
  100 + for i := range chanceTypes {
  101 + idString := strconv.Itoa(chanceTypes[i].Id)
  102 + chanceTypeCache[idString] = chanceTypes[i]
  103 + }
  104 + for i := range auditTemplates {
  105 + idString := strconv.Itoa(int(auditTemplates[i].Id))
  106 + auditTemplateCache[idString] = auditTemplates[i]
  107 + }
  108 + for i := range departments {
  109 + idString := strconv.Itoa(int(departments[i].Id))
  110 + departmentCache[idString] = departments[i]
  111 + }
48 for i := range soureData { 112 for i := range soureData {
49 storeTypeS := fmt.Sprint(soureData[i]["type"]) 113 storeTypeS := fmt.Sprint(soureData[i]["type"])
50 storeType, _ := strconv.ParseInt(storeTypeS, 10, 8) 114 storeType, _ := strconv.ParseInt(storeTypeS, 10, 8)
@@ -53,57 +117,62 @@ func GetAuditListForExcel(param protocol.RequestAuditList, companyid int64, user @@ -53,57 +117,62 @@ func GetAuditListForExcel(param protocol.RequestAuditList, companyid int64, user
53 soureData[i]["reserve_type"] = "未设置" 117 soureData[i]["reserve_type"] = "未设置"
54 if reserveType, ok := reserveTypeCache[reserveTypeIdS]; ok { 118 if reserveType, ok := reserveTypeCache[reserveTypeIdS]; ok {
55 soureData[i]["reserve_type"] = reserveType.Name 119 soureData[i]["reserve_type"] = reserveType.Name
56 - } else {  
57 - reserveTypeId, _ := strconv.Atoi(reserveTypeIdS)  
58 - if reserveTypeId > 0 {  
59 - reserveType, err := models.GetChanceReserveTypeById(reserveTypeId)  
60 - if err == nil {  
61 - reserveTypeCache[reserveTypeIdS] = reserveType  
62 - soureData[i]["reserve_type"] = reserveType.Name  
63 - }  
64 - }  
65 } 120 }
  121 + // else {
  122 + // reserveTypeId, _ := strconv.Atoi(reserveTypeIdS)
  123 + // if reserveTypeId > 0 {
  124 + // reserveType, err := models.GetChanceReserveTypeById(reserveTypeId)
  125 + // if err == nil {
  126 + // reserveTypeCache[reserveTypeIdS] = reserveType
  127 + // soureData[i]["reserve_type"] = reserveType.Name
  128 + // }
  129 + // }
  130 + // }
66 chanceTypeIdS := fmt.Sprint(soureData[i]["chance_type_id"]) 131 chanceTypeIdS := fmt.Sprint(soureData[i]["chance_type_id"])
67 soureData[i]["chance_type"] = "" 132 soureData[i]["chance_type"] = ""
  133 + //TODO
68 if chanceType, ok := chanceTypeCache[chanceTypeIdS]; ok { 134 if chanceType, ok := chanceTypeCache[chanceTypeIdS]; ok {
69 soureData[i]["chance_type"] = chanceType.Name 135 soureData[i]["chance_type"] = chanceType.Name
70 - } else {  
71 - chanceTypeId, _ := strconv.Atoi(chanceTypeIdS)  
72 - if chanceTypeId > 0 {  
73 - chanceType, err := models.GetChanceTypeById(chanceTypeId)  
74 - if err == nil {  
75 - chanceTypeCache[chanceTypeIdS] = chanceType  
76 - soureData[i]["chance_type"] = chanceType.Name  
77 - }  
78 - }  
79 } 136 }
  137 + // else {
  138 + // chanceTypeId, _ := strconv.Atoi(chanceTypeIdS)
  139 + // if chanceTypeId > 0 {
  140 + // chanceType, err := models.GetChanceTypeById(chanceTypeId)
  141 + // if err == nil {
  142 + // chanceTypeCache[chanceTypeIdS] = chanceType
  143 + // soureData[i]["chance_type"] = chanceType.Name
  144 + // }
  145 + // }
  146 + // }
80 auditTemplateIdS := fmt.Sprint(soureData[i]["audit_template_id"]) 147 auditTemplateIdS := fmt.Sprint(soureData[i]["audit_template_id"])
81 soureData[i]["audit_template"] = "" 148 soureData[i]["audit_template"] = ""
82 if auditTempalte, ok := auditTemplateCache[auditTemplateIdS]; ok { 149 if auditTempalte, ok := auditTemplateCache[auditTemplateIdS]; ok {
83 soureData[i]["audit_template"] = auditTempalte.Name 150 soureData[i]["audit_template"] = auditTempalte.Name
84 - } else {  
85 - auditTemplateId, _ := strconv.ParseInt(auditTemplateIdS, 10, 64)  
86 - if auditTemplateId > 0 {  
87 - auditTempalte, err := models.GetAuditTemplateById(auditTemplateId)  
88 - if err == nil {  
89 - auditTemplateCache[auditTemplateIdS] = auditTempalte  
90 - soureData[i]["audit_template"] = auditTempalte.Name  
91 - }  
92 - }  
93 } 151 }
  152 + // else {
  153 + // auditTemplateId, _ := strconv.ParseInt(auditTemplateIdS, 10, 64)
  154 + // if auditTemplateId > 0 {
  155 + // auditTempalte, err := models.GetAuditTemplateById(auditTemplateId)
  156 + // if err == nil {
  157 + // auditTemplateCache[auditTemplateIdS] = auditTempalte
  158 + // soureData[i]["audit_template"] = auditTempalte.Name
  159 + // }
  160 + // }
  161 + // }
94 departmentIdS := fmt.Sprint(soureData[i]["department_id"]) 162 departmentIdS := fmt.Sprint(soureData[i]["department_id"])
95 if department, ok := departmentCache[departmentIdS]; ok { 163 if department, ok := departmentCache[departmentIdS]; ok {
96 soureData[i]["department"] = department.Name 164 soureData[i]["department"] = department.Name
97 - } else {  
98 - departmentId, _ := strconv.ParseInt(departmentIdS, 10, 64)  
99 - if departmentId > 0 {  
100 - department, err := models.GetDepartmentById(departmentId)  
101 - if err == nil {  
102 - departmentCache[departmentIdS] = department  
103 - soureData[i]["department"] = department.Name  
104 - }  
105 - }  
106 } 165 }
  166 + // else {
  167 + // departmentId, _ := strconv.ParseInt(departmentIdS, 10, 64)
  168 + // if departmentId > 0 {
  169 + // department, err := models.GetDepartmentById(departmentId)
  170 + // if err == nil {
  171 + // departmentCache[departmentIdS] = department
  172 + // soureData[i]["department"] = department.Name
  173 + // }
  174 + // }
  175 + // }
107 reviewStatusS := fmt.Sprint(soureData[i]["review_status"]) 176 reviewStatusS := fmt.Sprint(soureData[i]["review_status"])
108 reviewStatus, _ := strconv.ParseInt(reviewStatusS, 10, 8) 177 reviewStatus, _ := strconv.ParseInt(reviewStatusS, 10, 8)
109 soureData[i]["review_status"] = models.ChanceReviewStatusMap[int8(reviewStatus)] 178 soureData[i]["review_status"] = models.ChanceReviewStatusMap[int8(reviewStatus)]