男神鹏:使用golang gin 框架 实现 个人信息完善度

今天遇到一个根据信息表单的修改实时性的修改完善度的需求,一开始是百思不得其解。

后来,沉下心来想了想,发现逻辑非常的简单,就那么一个小点把我当时给误住了。

实现逻辑就是 

首先用一个变量 = 有值的字段/总字段

因有值的字段是随时都变的,因此要设置为n

然后遍历表中的值,看看那些有那些没有(可以对数据判空或者判0)

得到之后再将它更新便可以了

以下代码详解

var n float64

var datas []string
datas = []string{contactsrelationship, countries, idpostcode, marriage, national, nowaddresspostcode, professional, workunitspostcode}

for _, value := range datas {
if value != "0" {
n++

}
}

var datass []string
datass = []string{birthaddress, contactsaddress, contactsname, contactstelphone, healthcardnum, idaddress, iddetailedaddress, nativeplace, nowaddress,
nowdetailedaddress, nowtelphone, workunits, workunitsaddress, workunitstelphone}

for _, value := range datass {
if value != "" {
n++

}
}
var nums float64
nums = (6 + n) / 28
perfect, err := strconv.ParseFloat(fmt.Sprintf("%.2f", nums), 64)

idd := c.Param("id")
IntID, errr := strconv.Atoi(idd)
if errr != nil {
appG.Response(http.StatusOK, consts.INVALID_PARAMS, nil)
return
}
var patientPersonalFiless model.PatientPersonalFile
err = patientPersonalFiless.GetPatientPersonalFile(IntID)
if err != nil {
appG.Response(http.StatusOK, consts.ERROR_NOT_ID, nil)
return
}

value := model.Values{
"id": patientPersonalFiless.PatientID,
"perfect": perfect,
}

var patient model.Patient
successs, errs := patient.UpdatePatientPerfect(value)
if errs != nil || successs == false {
appG.Response(http.StatusOK, consts.ERROR_EDIT_CONTENT, nil)
return
}

appG.Response(http.StatusOK, consts.SUCCESS, "修改成功")
return
}

猜你喜欢

转载自www.cnblogs.com/lyp0626/p/12056092.html