作者 tangxuhui

导出数据 功能优化

... ... @@ -143,3 +143,16 @@ func GetAuditTemplateSort(companyId int64, chanceTypeId int) (v *ChanceType, err
}
return
}
// GetAuditTemplateByIds retrieves AuditTemplate by Id.
func GetAuditTemplateByIds(ids []int64) (data []AuditTemplate, err error) {
data = []AuditTemplate{}
if len(ids) == 0 {
return data, nil
}
o := orm.NewOrm()
_, err = o.QueryTable(&AuditTemplate{}).
Filter("id__in", ids).
All(&data)
return data, err
}
... ...
... ... @@ -54,6 +54,19 @@ func GetChanceReserveTypeById(id int) (m *ChanceReserveType, err error) {
err = o.Read(m)
return m, err
}
func GetChanceReserveTypeByIds(ids []int) (data []ChanceReserveType, err error) {
data = []ChanceReserveType{}
if len(ids) == 0 {
return data, nil
}
o := orm.NewOrm()
_, err = o.QueryTable(&ChanceReserveType{}).
Filter("id__in", ids).
All(&data)
return data, err
}
func HasChanceReserveTypeName(name string, companyid int64, notId int) bool {
var bl bool
o := orm.NewOrm()
... ...
... ... @@ -111,3 +111,17 @@ func GetChanceTypeMaxSort(companyId int64) (v *ChanceType, err error) {
}
return
}
// GetChanceTypeById retrieves ChanceType by Id. Returns error if
// Id doesn't exist
func GetChanceTypeByIds(ids []int) (data []ChanceType, err error) {
data = []ChanceType{}
if len(ids) == 0 {
return data, nil
}
o := orm.NewOrm()
_, err = o.QueryTable(&ChanceType{}).
Filter("id__in", ids).
All(&data)
return data, err
}
... ...
... ... @@ -183,17 +183,15 @@ func GetAuditListSql(param protocol.RequestAuditList, companyid int64, userid in
)
s1 := `SELECT a.id,a.department_id,a.audit_template_id,a.chance_type_id
,a.publish_status,a.create_at,a.review_status,a.status
,a.discovery_score,a.comment_total ,a.code,d.nick_name,d.id as user_id
,a.discovery_score,a.comment_total ,a.code,c.nick_name
,a.type,a.reserve_type_id,a.source_content,a.approve_time,a.check_result
,a.self_checks,a.check_result_status
FROM chance AS a
JOIN user_company AS c ON c.id = a.user_id
JOIN user AS d ON c.user_id = d.id
%s
where a.company_id=? AND a.enable_status=1 `
s2 := ` SELECT count(*) FROM chance as a
JOIN user_company AS c ON c.id = a.user_id
JOIN user AS d ON c.user_id = d.id
%s
where a.company_id=? AND a.enable_status=1 `
if companyinfo, err := models.GetCompanyById(companyid); err == nil {
... ... @@ -299,7 +297,7 @@ func GetAuditListSql(param protocol.RequestAuditList, companyid int64, userid in
func GetAuditList(param protocol.RequestAuditList, companyid int64, userid int64) (protocol.ResponseAuditList, error) {
type SqlData struct {
Id int64 `orm:"column(id)"`
UserId int64 `orm:"column(user_id)"`
// UserId int64 `orm:"column(user_id)"`
NickName string `orm:"column(nick_name)"`
DepartmentId int64 `orm:"column(department_id)"`
AuditTemplateId int64 `orm:"column(audit_template_id)"`
... ...
... ... @@ -38,13 +38,77 @@ func GetAuditListForExcel(param protocol.RequestAuditList, companyid int64, user
// FROM chance AS a
// JOIN user_company AS c ON c.id = a.user_id
// JOIN user AS d ON c.user_id = d.id
var (
reserveTypeIdMap = map[int]struct{}{}
chanceTypeIdMap = map[int]struct{}{}
auditTemplateIdMap = map[int64]struct{}{}
departmentIdMap = map[int64]struct{}{}
)
for i := range soureData {
reserveTypeIdStr := fmt.Sprint(soureData[i]["reserve_type_id"])
reserveTypeId, _ := strconv.Atoi(reserveTypeIdStr)
reserveTypeIdMap[reserveTypeId] = struct{}{}
chanceTypeIdStr := fmt.Sprint(soureData[i]["chance_type_id"])
chanceTypeId, _ := strconv.Atoi(chanceTypeIdStr)
chanceTypeIdMap[chanceTypeId] = struct{}{}
auditTemplateIdStr := fmt.Sprint(soureData[i]["audit_template_id"])
auditTemplateId, _ := strconv.ParseInt(auditTemplateIdStr, 10, 64)
auditTemplateIdMap[auditTemplateId] = struct{}{}
departmentIdStr := fmt.Sprint(soureData[i]["department_id"])
departmentId, _ := strconv.ParseInt(departmentIdStr, 10, 64)
departmentIdMap[departmentId] = struct{}{}
}
var (
reserveTypeIds []int
chanceTypeIds []int
auditTemplateIds []int64
departmentIds []int64
)
for k := range reserveTypeIdMap {
reserveTypeIds = append(reserveTypeIds, k)
}
for k := range chanceTypeIdMap {
chanceTypeIds = append(chanceTypeIds, k)
}
for k := range auditTemplateIdMap {
auditTemplateIds = append(auditTemplateIds, k)
}
for k := range departmentIdMap {
departmentIds = append(departmentIds, k)
}
var (
reserveTypes []models.ChanceReserveType
chanceTypes []models.ChanceType
auditTemplates []models.AuditTemplate
departments []models.Department
)
reserveTypes, _ = models.GetChanceReserveTypeByIds(reserveTypeIds)
chanceTypes, _ = models.GetChanceTypeByIds(chanceTypeIds)
auditTemplates, _ = models.GetAuditTemplateByIds(auditTemplateIds)
departments, _ = models.GetDepartmentByIds(departmentIds)
//进行数据整理
var (
reserveTypeCache = make(map[string]*models.ChanceReserveType)
chanceTypeCache = make(map[string]*models.ChanceType)
auditTemplateCache = make(map[string]*models.AuditTemplate)
departmentCache = make(map[string]*models.Department)
reserveTypeCache = make(map[string]models.ChanceReserveType)
chanceTypeCache = make(map[string]models.ChanceType)
auditTemplateCache = make(map[string]models.AuditTemplate)
departmentCache = make(map[string]models.Department)
)
for i := range reserveTypes {
idString := strconv.Itoa(reserveTypes[i].Id)
reserveTypeCache[idString] = reserveTypes[i]
}
for i := range chanceTypes {
idString := strconv.Itoa(chanceTypes[i].Id)
chanceTypeCache[idString] = chanceTypes[i]
}
for i := range auditTemplates {
idString := strconv.Itoa(int(auditTemplates[i].Id))
auditTemplateCache[idString] = auditTemplates[i]
}
for i := range departments {
idString := strconv.Itoa(int(departments[i].Id))
departmentCache[idString] = departments[i]
}
for i := range soureData {
storeTypeS := fmt.Sprint(soureData[i]["type"])
storeType, _ := strconv.ParseInt(storeTypeS, 10, 8)
... ... @@ -53,57 +117,62 @@ func GetAuditListForExcel(param protocol.RequestAuditList, companyid int64, user
soureData[i]["reserve_type"] = "未设置"
if reserveType, ok := reserveTypeCache[reserveTypeIdS]; ok {
soureData[i]["reserve_type"] = reserveType.Name
} else {
reserveTypeId, _ := strconv.Atoi(reserveTypeIdS)
if reserveTypeId > 0 {
reserveType, err := models.GetChanceReserveTypeById(reserveTypeId)
if err == nil {
reserveTypeCache[reserveTypeIdS] = reserveType
soureData[i]["reserve_type"] = reserveType.Name
}
}
}
// else {
// reserveTypeId, _ := strconv.Atoi(reserveTypeIdS)
// if reserveTypeId > 0 {
// reserveType, err := models.GetChanceReserveTypeById(reserveTypeId)
// if err == nil {
// reserveTypeCache[reserveTypeIdS] = reserveType
// soureData[i]["reserve_type"] = reserveType.Name
// }
// }
// }
chanceTypeIdS := fmt.Sprint(soureData[i]["chance_type_id"])
soureData[i]["chance_type"] = ""
//TODO
if chanceType, ok := chanceTypeCache[chanceTypeIdS]; ok {
soureData[i]["chance_type"] = chanceType.Name
} else {
chanceTypeId, _ := strconv.Atoi(chanceTypeIdS)
if chanceTypeId > 0 {
chanceType, err := models.GetChanceTypeById(chanceTypeId)
if err == nil {
chanceTypeCache[chanceTypeIdS] = chanceType
soureData[i]["chance_type"] = chanceType.Name
}
}
}
// else {
// chanceTypeId, _ := strconv.Atoi(chanceTypeIdS)
// if chanceTypeId > 0 {
// chanceType, err := models.GetChanceTypeById(chanceTypeId)
// if err == nil {
// chanceTypeCache[chanceTypeIdS] = chanceType
// soureData[i]["chance_type"] = chanceType.Name
// }
// }
// }
auditTemplateIdS := fmt.Sprint(soureData[i]["audit_template_id"])
soureData[i]["audit_template"] = ""
if auditTempalte, ok := auditTemplateCache[auditTemplateIdS]; ok {
soureData[i]["audit_template"] = auditTempalte.Name
} else {
auditTemplateId, _ := strconv.ParseInt(auditTemplateIdS, 10, 64)
if auditTemplateId > 0 {
auditTempalte, err := models.GetAuditTemplateById(auditTemplateId)
if err == nil {
auditTemplateCache[auditTemplateIdS] = auditTempalte
soureData[i]["audit_template"] = auditTempalte.Name
}
}
}
// else {
// auditTemplateId, _ := strconv.ParseInt(auditTemplateIdS, 10, 64)
// if auditTemplateId > 0 {
// auditTempalte, err := models.GetAuditTemplateById(auditTemplateId)
// if err == nil {
// auditTemplateCache[auditTemplateIdS] = auditTempalte
// soureData[i]["audit_template"] = auditTempalte.Name
// }
// }
// }
departmentIdS := fmt.Sprint(soureData[i]["department_id"])
if department, ok := departmentCache[departmentIdS]; ok {
soureData[i]["department"] = department.Name
} else {
departmentId, _ := strconv.ParseInt(departmentIdS, 10, 64)
if departmentId > 0 {
department, err := models.GetDepartmentById(departmentId)
if err == nil {
departmentCache[departmentIdS] = department
soureData[i]["department"] = department.Name
}
}
}
// else {
// departmentId, _ := strconv.ParseInt(departmentIdS, 10, 64)
// if departmentId > 0 {
// department, err := models.GetDepartmentById(departmentId)
// if err == nil {
// departmentCache[departmentIdS] = department
// soureData[i]["department"] = department.Name
// }
// }
// }
reviewStatusS := fmt.Sprint(soureData[i]["review_status"])
reviewStatus, _ := strconv.ParseInt(reviewStatusS, 10, 8)
soureData[i]["review_status"] = models.ChanceReviewStatusMap[int8(reviewStatus)]
... ...