作者 tangxvhui

机会自查内容中筛选结果数据 构建方式优化

... ... @@ -302,15 +302,15 @@ type ReponseChanceCheckResult struct {
// ChanceCheckResultData 筛选结果详情数据
type ChanceCheckResultData struct {
Total string `json:"total"`
CheckPid int64 `json:"check_pid"`
CheckId int64 `json:"check_id"`
CheckItem string `json:"check_item"`
CustomItemData map[string]string `json:"custom_item_data"`
AnswerYes int `json:"-"`
AnswerNo int `json:"-"`
AnswerAny int `json:"-"`
Child map[int64]*ChanceCheckResultData `json:"-"`
Total string `json:"total"`
CheckPid int64 `json:"check_pid"`
CheckId int64 `json:"check_id"`
CheckItem string `json:"check_item"`
CustomItemData map[string]string `json:"custom_item_data"`
AnswerYes int `json:"-"`
AnswerNo int `json:"-"`
AnswerAny int `json:"-"`
Child []*ChanceCheckResultData `json:"-"`
}
//ResponseChanceCheckResultList 响应自查内容筛选结果列表
... ...
... ... @@ -36,6 +36,162 @@ func GetChanceCheckResultInfo(chanceId int64, companyid int64) (protocol.Reponse
}
//getChanceCheckResultData 机会自查内容筛选详情数据输出
// func getChanceCheckResultData(chanceId int64) (protocol.ReponseChanceCheckResult, error) {
// var (
// checkResultData []models.ChanceCheckResult
// err error
// )
// rsp := protocol.ReponseChanceCheckResult{
// CustomItem: []string{},
// CheckData: []protocol.ChanceCheckResultData{},
// }
// checkResultData, err = models.GetChanceCheckResultByChancce(chanceId)
// if err != nil {
// log.Error("未获得指定的机会自查内容筛选结果,err;%s", err)
// return rsp, nil
// }
// //组装响应数据
// //自查内容中的用户id
// userIds := utils.NewArraySetInt64()
// //一维自查内容项
// checkItemsLv1 := utils.NewArraySetInt64()
// checkItemsLv2 := map[int64]*utils.ArraySetInt64{}
// checkResultMap := make(map[int64]*protocol.ChanceCheckResultData)
// //处理一维自查内容数据
// for _, v := range checkResultData {
// if v.CheckPid > 0 {
// continue
// }
// if _, ok := checkResultMap[v.CheckId]; ok {
// checkResultMap[v.CheckId].CustomItemData[fmt.Sprint(v.UserCompanyId)] = v.Answer
// switch v.Answer {
// case "是":
// checkResultMap[v.CheckId].AnswerYes++
// case "否":
// checkResultMap[v.CheckId].AnswerNo++
// case "不清楚":
// checkResultMap[v.CheckId].AnswerAny++
// }
// } else {
// userAnswer := map[string]string{
// fmt.Sprint(v.UserCompanyId): v.Answer,
// }
// r := &protocol.ChanceCheckResultData{
// CheckPid: v.CheckPid,
// CheckId: v.CheckId,
// CheckItem: v.CheckItem,
// CustomItemData: userAnswer,
// }
// switch v.Answer {
// case "是":
// r.AnswerYes++
// case "否":
// r.AnswerNo++
// case "不清楚":
// r.AnswerAny++
// }
// checkResultMap[v.CheckId] = r
// userIds.Add(v.UserCompanyId)
// checkItemsLv1.Add(v.CheckId)
// }
// }
// //处理二维自查内容数据
// for _, v := range checkResultData {
// if v.CheckPid == 0 {
// continue
// }
// _, ok := checkResultMap[v.CheckPid]
// if !ok {
// continue
// }
// _, ok = checkResultMap[v.CheckPid].Child[v.CheckId]
// if ok {
// checkResultMap[v.CheckPid].Child[v.CheckId].CustomItemData[fmt.Sprint(v.UserCompanyId)] = v.Answer
// switch v.Answer {
// case "是":
// checkResultMap[v.CheckId].AnswerYes++
// case "否":
// checkResultMap[v.CheckId].AnswerNo++
// case "不清楚":
// checkResultMap[v.CheckId].AnswerAny++
// }
// } else {
// userAnswer := map[string]string{
// fmt.Sprint(v.UserCompanyId): v.Answer,
// }
// r := &protocol.ChanceCheckResultData{
// CheckPid: v.CheckPid,
// CheckId: v.CheckId,
// CheckItem: v.CheckItem,
// CustomItemData: userAnswer,
// }
// switch v.Answer {
// case "是":
// r.AnswerYes++
// case "否":
// r.AnswerNo++
// case "不清楚":
// r.AnswerAny++
// }
// checkResultMap[v.CheckPid].Child[v.CheckId] = r
// userIds.Add(v.UserCompanyId)
// if _, ok := checkItemsLv2[v.CheckPid]; ok {
// checkItemsLv2[v.CheckPid].Add(v.CheckId)
// } else {
// checkItemsLv2[v.CheckPid] = utils.NewArraySetInt64()
// checkItemsLv2[v.CheckPid].Add(v.CheckId)
// }
// }
// }
// usernameMap := make(map[string]string)
// for _, v := range userIds.ToSlice() {
// idString := fmt.Sprint(v)
// u, err := models.GetUserCompanyById(v)
// if err == nil {
// usernameMap[idString] = u.NickName
// } else {
// usernameMap[idString] = idString
// }
// rsp.CustomItem = append(rsp.CustomItem, usernameMap[idString])
// }
// for _, Lv1 := range checkItemsLv1.ToSlice() {
// // 处理一级数据
// newCustomItemData := map[string]string{}
// for uid, uname := range usernameMap {
// if answer, ok := checkResultMap[Lv1].CustomItemData[uid]; ok {
// newCustomItemData[uname] = answer
// } else {
// newCustomItemData[uname] = ""
// }
// }
// checkResultMap[Lv1].CustomItemData = newCustomItemData
// checkResultMap[Lv1].Total = fmt.Sprintf("是:%d 否:%d 不清楚:%d",
// checkResultMap[Lv1].AnswerYes, checkResultMap[Lv1].AnswerNo, checkResultMap[Lv1].AnswerAny)
// rsp.CheckData = append(rsp.CheckData, *checkResultMap[Lv1])
// if _, ok := checkItemsLv2[Lv1]; ok {
// //处理二级数据
// for _, Lv2 := range checkItemsLv2[Lv1].ToSlice() {
// newCustomItemData := map[string]string{}
// for uid, uname := range usernameMap {
// if answer, ok := checkResultMap[Lv1].Child[Lv2].CustomItemData[uid]; ok {
// newCustomItemData[uname] = answer
// } else {
// newCustomItemData[uname] = ""
// }
// }
// checkResultMap[Lv1].Child[Lv2].CustomItemData = newCustomItemData
// // checkResultMap[Lv1].Child[Lv2].Total = fmt.Sprintf("是:%d 否:%d 不清楚:%d",
// // checkResultMap[Lv1].Child[Lv2].AnswerYes, checkResultMap[Lv1].Child[Lv2].AnswerNo,
// // checkResultMap[Lv1].Child[Lv2].AnswerAny)
// rsp.CheckData = append(rsp.CheckData, *checkResultMap[Lv1].Child[Lv2])
// }
// }
// }
// return rsp, nil
// }
//getChanceCheckResultData 机会自查内容筛选详情数据输出
func getChanceCheckResultData(chanceId int64) (protocol.ReponseChanceCheckResult, error) {
var (
checkResultData []models.ChanceCheckResult
... ... @@ -50,142 +206,78 @@ func getChanceCheckResultData(chanceId int64) (protocol.ReponseChanceCheckResult
log.Error("未获得指定的机会自查内容筛选结果,err;%s", err)
return rsp, nil
}
//组装响应数据
//自查内容中的用户id
//人的id
userIds := utils.NewArraySetInt64()
//一维自查内容项
checkItemsLv1 := utils.NewArraySetInt64()
checkItemsLv2 := map[int64]*utils.ArraySetInt64{}
checkResultMap := make(map[int64]*protocol.ChanceCheckResultData)
//处理一维自查内容数据
for _, v := range checkResultData {
if v.CheckPid > 0 {
continue
}
if _, ok := checkResultMap[v.CheckId]; ok {
checkResultMap[v.CheckId].CustomItemData[fmt.Sprint(v.UserCompanyId)] = v.Answer
switch v.Answer {
case "是":
checkResultMap[v.CheckId].AnswerYes++
case "否":
checkResultMap[v.CheckId].AnswerNo++
case "不清楚":
checkResultMap[v.CheckId].AnswerAny++
}
} else {
userAnswer := map[string]string{
fmt.Sprint(v.UserCompanyId): v.Answer,
}
checkResultMap := map[int64]*protocol.ChanceCheckResultData{}
checkReusltSlice := []*protocol.ChanceCheckResultData{}
//按人区分问题的答案
userAnswer := make(map[int64]map[int64]string)
for _, resultData := range checkResultData {
if _, ok := checkResultMap[resultData.CheckId]; !ok {
r := &protocol.ChanceCheckResultData{
CheckPid: v.CheckPid,
CheckId: v.CheckId,
CheckItem: v.CheckItem,
CustomItemData: userAnswer,
CheckId: resultData.CheckId,
CheckPid: resultData.CheckPid,
CheckItem: resultData.CheckItem,
}
switch v.Answer {
case "是":
r.AnswerYes++
case "否":
r.AnswerNo++
case "不清楚":
r.AnswerAny++
}
checkResultMap[v.CheckId] = r
userIds.Add(v.UserCompanyId)
checkItemsLv1.Add(v.CheckId)
}
}
//处理二维自查内容数据
for _, v := range checkResultData {
if v.CheckPid == 0 {
continue
checkResultMap[resultData.CheckId] = r
checkReusltSlice = append(checkReusltSlice, r)
}
_, ok := checkResultMap[v.CheckPid]
if !ok {
continue
}
_, ok = checkResultMap[v.CheckPid].Child[v.CheckId]
if ok {
checkResultMap[v.CheckPid].Child[v.CheckId].CustomItemData[fmt.Sprint(v.UserCompanyId)] = v.Answer
switch v.Answer {
case "是":
checkResultMap[v.CheckId].AnswerYes++
case "否":
checkResultMap[v.CheckId].AnswerNo++
case "不清楚":
checkResultMap[v.CheckId].AnswerAny++
}
userIds.Add(resultData.UserCompanyId)
if _, ok := userAnswer[resultData.UserCompanyId]; ok {
userAnswer[resultData.UserCompanyId][resultData.CheckId] = resultData.Answer
} else {
userAnswer := map[string]string{
fmt.Sprint(v.UserCompanyId): v.Answer,
}
r := &protocol.ChanceCheckResultData{
CheckPid: v.CheckPid,
CheckId: v.CheckId,
CheckItem: v.CheckItem,
CustomItemData: userAnswer,
}
switch v.Answer {
case "是":
r.AnswerYes++
case "否":
r.AnswerNo++
case "不清楚":
r.AnswerAny++
}
checkResultMap[v.CheckPid].Child[v.CheckId] = r
userIds.Add(v.UserCompanyId)
if _, ok := checkItemsLv2[v.CheckPid]; ok {
checkItemsLv2[v.CheckPid].Add(v.CheckId)
} else {
checkItemsLv2[v.CheckPid] = utils.NewArraySetInt64()
checkItemsLv2[v.CheckPid].Add(v.CheckId)
userAnswer[resultData.UserCompanyId] = map[int64]string{
resultData.CheckId: resultData.Answer,
}
}
}
usernameMap := make(map[string]string)
usernameMap := make(map[int64]string)
customItems := []string{}
for _, v := range userIds.ToSlice() {
idString := fmt.Sprint(v)
u, err := models.GetUserCompanyById(v)
if err == nil {
usernameMap[idString] = u.NickName
usernameMap[v] = u.NickName
} else {
usernameMap[idString] = idString
usernameMap[v] = idString
}
rsp.CustomItem = append(rsp.CustomItem, usernameMap[idString])
customItems = append(customItems, usernameMap[v])
}
for _, Lv1 := range checkItemsLv1.ToSlice() {
// 处理一级数据
newCustomItemData := map[string]string{}
for uid, uname := range usernameMap {
if answer, ok := checkResultMap[Lv1].CustomItemData[uid]; ok {
newCustomItemData[uname] = answer
for i := range checkReusltSlice {
customItemsData := map[string]string{}
for uid := range userAnswer {
if answer, ok := userAnswer[uid][checkReusltSlice[i].CheckId]; ok {
switch answer {
case "是":
checkReusltSlice[i].AnswerYes++
case "否":
checkReusltSlice[i].AnswerNo++
case "不清楚":
checkReusltSlice[i].AnswerAny++
}
customItemsData[usernameMap[uid]] = answer
} else {
newCustomItemData[uname] = ""
customItemsData[usernameMap[uid]] = ""
}
}
checkResultMap[Lv1].CustomItemData = newCustomItemData
checkResultMap[Lv1].Total = fmt.Sprintf("是:%d 否:%d 不清楚:%d",
checkResultMap[Lv1].AnswerYes, checkResultMap[Lv1].AnswerNo, checkResultMap[Lv1].AnswerAny)
rsp.CheckData = append(rsp.CheckData, *checkResultMap[Lv1])
if _, ok := checkItemsLv2[Lv1]; ok {
//处理二级数据
for _, Lv2 := range checkItemsLv2[Lv1].ToSlice() {
newCustomItemData := map[string]string{}
for uid, uname := range usernameMap {
if answer, ok := checkResultMap[Lv1].Child[Lv2].CustomItemData[uid]; ok {
newCustomItemData[uname] = answer
} else {
newCustomItemData[uname] = ""
}
}
checkResultMap[Lv1].Child[Lv2].CustomItemData = newCustomItemData
// checkResultMap[Lv1].Child[Lv2].Total = fmt.Sprintf("是:%d 否:%d 不清楚:%d",
// checkResultMap[Lv1].Child[Lv2].AnswerYes, checkResultMap[Lv1].Child[Lv2].AnswerNo,
// checkResultMap[Lv1].Child[Lv2].AnswerAny)
rsp.CheckData = append(rsp.CheckData, *checkResultMap[Lv1].Child[Lv2])
}
checkReusltSlice[i].CustomItemData = customItemsData
if checkReusltSlice[i].CheckPid == 0 {
checkReusltSlice[i].Total = fmt.Sprintf("是:%d 否:%d 不清楚:%d",
checkReusltSlice[i].AnswerYes, checkReusltSlice[i].AnswerNo, checkReusltSlice[i].AnswerAny)
}
if checkReusltSlice[i].CheckPid > 0 {
pid := checkReusltSlice[i].CheckPid
checkResultMap[pid].Child = append(checkResultMap[pid].Child, checkReusltSlice[i])
}
}
rsp.CustomItem = customItems
for i := range checkReusltSlice {
if checkReusltSlice[i].CheckPid > 0 {
continue
}
rsp.CheckData = append(rsp.CheckData, *checkReusltSlice[i])
for ii := range checkReusltSlice[i].Child {
rsp.CheckData = append(rsp.CheckData, *checkReusltSlice[i].Child[ii])
}
}
return rsp, nil
... ...