package members import ( base_ctl "SCRM/controllers" "SCRM/enums" "SCRM/models" "SCRM/service/member_service" "SCRM/utils" "encoding/json" "fmt" "reflect" "time" "github.com/astaxie/beego" ) func MemberCtlRegistRouters() { beego.Router("/api/members", &MembersAPIController{}, "get:GetMembers") beego.Router("/api/members/all", &MembersAPIController{}, "get:GetAllMembers") beego.Router("/api/member/create", &MembersAPIController{}, "Post:CreateMember") beego.Router("/api/member/edit", &MembersAPIController{}, "Put:EditMember") beego.Router("/api/members/delete", &MembersAPIController{}, "Delete:DeleteMembers") } type MembersAPIController struct { base_ctl.BaseAuthAPIController } func (c *MembersAPIController) GetMembers() { page, _ := c.GetInt64("page", 1) limit, _ := c.GetInt64("limit", 10) startTime := c.GetString("start_time", "") endTime := c.GetString("end_time", "") searchKey := c.GetString("search", "") level, _ := c.GetInt64("level", 0) source, _ := c.GetInt64("source", 0) tag, _ := c.GetInt64("tag", 0) init, _ := c.GetInt64("init", 0) fmt.Println("level", level, "source", source) if page <= 0 { page = 1 } if limit <= 0 { limit = 10 } adminUserInfo := c.GetAdminUserInfo() timeLayout := "2006-01-02 15:04:05" var isStartTime bool var theStartTIme int64 if len(startTime) > 0 { theTime, err := utils.ParseTimeStringToTime(timeLayout, startTime+" 00:00:00") if err != nil { c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "开始时间错误") return } isStartTime = true theStartTIme = theTime.Unix() } var isEndTime bool var theEndtTIme int64 if len(endTime) > 0 { theTime, err := utils.ParseTimeStringToTime(timeLayout, endTime+" 23:59:59") if err != nil { c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "结束时间错误") return } isEndTime = true theEndtTIme = theTime.Unix() } members, total, err := member_service.GetMemberList(adminUserInfo.CurrentOrgId, page, limit, level, source, tag, theStartTIme, theEndtTIme, isStartTime, isEndTime, searchKey) if err != nil { c.ServeFailJsonSend(enums.ErrorCodeDataException, "获取会员列表失败") return } returnData := make(map[string]interface{}, 0) if init == 1 { cards, err := member_service.GetCardList(adminUserInfo.CurrentOrgId) if err != nil { c.ServeFailJsonSend(enums.ErrorCodeDataException, "获取会员卡列表失败") return } returnData["cards"] = cards tags, err := member_service.GetTagList(adminUserInfo.CurrentOrgId) if err != nil { c.ServeFailJsonSend(enums.ErrorCodeDataException, "获取标签列表失败") return } returnData["tags"] = tags illness, err := member_service.GetIllnessList() if err != nil { c.ServeFailJsonSend(enums.ErrorCodeDataException, "获取病种列表失败") return } returnData["illness"] = illness } returnData["members"] = members returnData["total"] = total c.ServeSuccessJSON(returnData) return } func (c *MembersAPIController) GetAllMembers() { adminUserInfo := c.GetAdminUserInfo() members, err := member_service.GetAllMemberList(adminUserInfo.CurrentOrgId) fmt.Println("members是什么",members) fmt.Println("资讯管理错误是什么",err) if err != nil { c.ServeFailJsonSend(enums.ErrorCodeDataException, "获取列表失败") return } returnData := make(map[string]interface{}, 0) returnData["members"] = members c.ServeSuccessJSON(returnData) return } func (c *MembersAPIController) CreateMember() { adminUserInfo := c.GetAdminUserInfo() timeNow := time.Now().Unix() dataBody := make(map[string]interface{}, 0) err := json.Unmarshal(c.Ctx.Input.RequestBody, &dataBody) if err != nil { utils.ErrorLog(err.Error()) c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "参数错误") return } var member member_service.Members if dataBody["name"] == nil || reflect.TypeOf(dataBody["name"]).String() != "string" { c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "缺少参数:姓名") return } name, _ := dataBody["name"].(string) if len(name) == 0 { c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "姓名不能为空") return } member.Name = name if dataBody["mobile"] == nil || reflect.TypeOf(dataBody["mobile"]).String() != "string" { c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "缺少参数:手机号") return } mobile, _ := dataBody["mobile"].(string) if len(mobile) == 0 { c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "手机号不能为空") return } if !utils.CheckMobile(mobile) { c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "手机号格式不正确") return } member.Mobile = mobile if dataBody["gender"] == nil || reflect.TypeOf(dataBody["gender"]).String() != "float64" { c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "缺少参数:性别") return } gender := int64(dataBody["gender"].(float64)) if gender != 1 && gender != 2 { c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "性别选择不正确") return } member.Gender = gender if dataBody["birthday"] == nil || reflect.TypeOf(dataBody["birthday"]).String() != "string" { c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "缺少参数:生日") return } birthday, _ := dataBody["birthday"].(string) if len(birthday) == 0 { c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "生日格式不正确") return } timeLayout := "2006-01-02 15:04:05" theTime, err := utils.ParseTimeStringToTime(timeLayout, birthday+" 00:00:00") if err != nil { c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "生日格式不正确") return } member.Birthday = theTime.Unix() if dataBody["province_id"] == nil || reflect.TypeOf(dataBody["province_id"]).String() != "float64" { c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "缺少参数:地区(省)") return } provinceID := int64(dataBody["province_id"].(float64)) if provinceID <= 0 { c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "请选择地区") return } member.ProvinceId = provinceID if dataBody["city_id"] == nil || reflect.TypeOf(dataBody["city_id"]).String() != "float64" { c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "缺少参数:地区(市)") return } cityID := int64(dataBody["city_id"].(float64)) if cityID <= 0 { c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "请选择地区") return } member.CityId = cityID if dataBody["district_id"] == nil || reflect.TypeOf(dataBody["district_id"]).String() != "float64" { c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "缺少参数:地区(区/县)") return } districtID := int64(dataBody["district_id"].(float64)) if districtID <= 0 { c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "请选择地区") return } member.DistrictId = districtID if dataBody["illness"] == nil || reflect.TypeOf(dataBody["illness"]).String() != "[]interface {}" { c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "缺少参数:病种") return } illnessInters := dataBody["illness"].([]interface{}) if len(illnessInters) <= 0 { c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "请选择病种") return } member.Illness = make([]*member_service.CustomerIllness, 0) for _, illnessInter := range illnessInters { id := int64(illnessInter.(float64)) var illItem member_service.CustomerIllness illItem.UserOrgId = adminUserInfo.CurrentOrgId illItem.IllnessId = id illItem.Status = 1 illItem.UpdatedTime = timeNow illItem.CreatedTime = timeNow member.Illness = append(member.Illness, &illItem) } if dataBody["ill_date"] == nil || reflect.TypeOf(dataBody["ill_date"]).String() != "string" { c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "缺少参数:患病时间") return } illDate, _ := dataBody["ill_date"].(string) if len(illDate) == 0 { c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "患病时间格式不正确") return } illDateTime, err := utils.ParseTimeStringToTime(timeLayout, illDate+" 00:00:00") if err != nil { c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "患病时间格式不正确") return } member.IllDate = illDateTime.Unix() if dataBody["treat_type"] == nil || reflect.TypeOf(dataBody["treat_type"]).String() != "float64" { c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "缺少参数:治疗方式") return } treatType := int64(dataBody["treat_type"].(float64)) if treatType <= 0 { c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "请选择治疗方式") return } member.TreatType = treatType if dataBody["tags"] != nil && reflect.TypeOf(dataBody["tags"]).String() == "[]interface {}" { tagsInters := dataBody["tags"].([]interface{}) if len(tagsInters) > 0 { member.UserTags = make([]*models.UserTagLinks, 0) for _, tagsInter := range tagsInters { id := int64(tagsInter.(float64)) var tagItem models.UserTagLinks tagItem.TagId = id tagItem.UserOrgId = adminUserInfo.CurrentOrgId tagItem.Status = 1 tagItem.UpdatedTime = timeNow tagItem.CreatedTime = timeNow member.UserTags = append(member.UserTags, &tagItem) } } } if dataBody["remark"] != nil && reflect.TypeOf(dataBody["remark"]).String() == "string" { remark, _ := dataBody["remark"].(string) if len(remark) > 0 { member.Remark = remark } } if dataBody["avatar"] != nil && reflect.TypeOf(dataBody["avatar"]).String() == "string" { avatar, _ := dataBody["avatar"].(string) if len(avatar) > 0 { member.Avatar = avatar } } customer, err := member_service.GetCustomerByMobile(adminUserInfo.CurrentOrgId, member.Mobile) if err != nil { c.ServeFailJsonSend(enums.ErrorCodeDBCreate, "添加会员失败:("+err.Error()+")") return } if customer != nil { c.ServeFailJsonSend(enums.ErrorCodeDBCreate, "添加会员失败:(已经存在该手机号的客户)") return } member.UserOrgId = adminUserInfo.CurrentOrgId member.Status = 1 member.CreatedTime = timeNow member.UpdatedTime = timeNow member.Sources = 7 user, err := member_service.GetUserByMobile(member.Mobile) if err != nil { c.ServeFailJsonSend(enums.ErrorCodeDBCreate, "添加会员失败:("+err.Error()+")") return } if user != nil { member.UserId = user.ID err = member_service.CreateMember(&member) if err != nil { c.ServeFailJsonSend(enums.ErrorCodeDBCreate, "添加会员失败:("+err.Error()+")") return } } else { var userData models.User userData.Avatar = member.Avatar userData.AvatarThumb = member.Avatar userData.Username = member.Name userData.Remarkname = member.Name userData.Mobile = member.Mobile userData.Gender = member.Gender userData.ProvinceId = member.ProvinceId userData.CityId = member.CityId userData.Address = member.Address userData.Birthday = member.Birthday userData.TreatType = member.TreatType userData.Relationship = member.Relationship userData.IllnessId = member.Illness[0].IllnessId userData.Sources = member.Sources userData.Status = 1 userData.CreatedTime = timeNow userData.UpdatedTime = timeNow err = member_service.CreateMemberWithUser(&member, &userData) if err != nil { c.ServeFailJsonSend(enums.ErrorCodeDBCreate, "添加会员失败:("+err.Error()+")") return } } returnData := make(map[string]interface{}, 0) returnData["member"] = member c.ServeSuccessJSON(returnData) return } func (c *MembersAPIController) EditMember() { id, _ := c.GetInt64("id", 0) if id <= 0 { c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "参数错误:id") return } adminUserInfo := c.GetAdminUserInfo() member, err := member_service.GetOnlyMemberByID(adminUserInfo.CurrentOrgId, id) if err != nil { c.ServeFailJsonSend(enums.ErrorCodeDBUpdate, "编辑会员失败:("+err.Error()+")") return } if member == nil { c.ServeFailJsonSend(enums.ErrorCodeDBUpdate, "编辑会员失败:(会员记录不存在)") return } timeNow := time.Now().Unix() dataBody := make(map[string]interface{}, 0) err = json.Unmarshal(c.Ctx.Input.RequestBody, &dataBody) if err != nil { utils.ErrorLog(err.Error()) c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "参数错误") return } if dataBody["name"] == nil || reflect.TypeOf(dataBody["name"]).String() != "string" { c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "缺少参数:姓名") return } name, _ := dataBody["name"].(string) if len(name) == 0 { c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "姓名不能为空") return } member.Name = name if dataBody["mobile"] == nil || reflect.TypeOf(dataBody["mobile"]).String() != "string" { c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "缺少参数:手机号") return } mobile, _ := dataBody["mobile"].(string) if len(mobile) == 0 { c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "手机号不能为空") return } if !utils.CheckMobile(mobile) { c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "手机号格式不正确") return } changeMobile := true if member.Mobile == mobile { changeMobile = false } member.Mobile = mobile if dataBody["gender"] == nil || reflect.TypeOf(dataBody["gender"]).String() != "float64" { c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "缺少参数:性别") return } gender := int64(dataBody["gender"].(float64)) if gender != 1 && gender != 2 { c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "性别选择不正确") return } member.Gender = gender if dataBody["birthday"] == nil || reflect.TypeOf(dataBody["birthday"]).String() != "string" { c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "缺少参数:生日") return } birthday, _ := dataBody["birthday"].(string) if len(birthday) == 0 { c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "生日格式不正确") return } timeLayout := "2006-01-02 15:04:05" theTime, err := utils.ParseTimeStringToTime(timeLayout, birthday+" 00:00:00") if err != nil { c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "生日格式不正确") return } member.Birthday = theTime.Unix() if dataBody["province_id"] == nil || reflect.TypeOf(dataBody["province_id"]).String() != "float64" { c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "缺少参数:地区(省)") return } provinceID := int64(dataBody["province_id"].(float64)) if provinceID <= 0 { c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "请选择地区") return } member.ProvinceId = provinceID if dataBody["city_id"] == nil || reflect.TypeOf(dataBody["city_id"]).String() != "float64" { c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "缺少参数:地区(市)") return } cityID := int64(dataBody["city_id"].(float64)) if cityID <= 0 { c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "请选择地区") return } member.CityId = cityID if dataBody["district_id"] == nil || reflect.TypeOf(dataBody["district_id"]).String() != "float64" { c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "缺少参数:地区(区/县)") return } districtID := int64(dataBody["district_id"].(float64)) if districtID <= 0 { c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "请选择地区") return } member.DistrictId = districtID if dataBody["illness"] == nil || reflect.TypeOf(dataBody["illness"]).String() != "[]interface {}" { c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "缺少参数:病种") return } illnessInters := dataBody["illness"].([]interface{}) if len(illnessInters) <= 0 { c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "请选择病种") return } member.Illness = make([]*member_service.CustomerIllness, 0) illnessIDS := make([]int64, 0) illnessIDMap := make(map[int64]int64, 0) for _, illnessInter := range illnessInters { id := int64(illnessInter.(float64)) illnessIDS = append(illnessIDS, id) illnessIDMap[id] = id } allIllness, err := member_service.GetMemberAllIllness(adminUserInfo.CurrentOrgId, member.ID) if err != nil { c.ServeFailJsonSend(enums.ErrorCodeDBUpdate, "编辑会员失败:("+err.Error()+")") return } oldIllIDMap := make(map[int64]int64, 0) for _, illItem := range allIllness { item := *illItem item.Status = 1 if _, exist := illnessIDMap[item.IllnessId]; !exist { item.Status = 0 item.UpdatedTime = timeNow } member.Illness = append(member.Illness, &item) oldIllIDMap[item.IllnessId] = item.IllnessId } for _, id := range illnessIDMap { if _, exist := oldIllIDMap[id]; exist { continue } var illItem member_service.CustomerIllness illItem.UserOrgId = adminUserInfo.CurrentOrgId illItem.IllnessId = id illItem.Status = 1 illItem.UpdatedTime = timeNow illItem.CreatedTime = timeNow member.Illness = append(member.Illness, &illItem) } if dataBody["ill_date"] == nil || reflect.TypeOf(dataBody["ill_date"]).String() != "string" { c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "缺少参数:患病时间") return } illDate, _ := dataBody["ill_date"].(string) if len(illDate) == 0 { c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "患病时间格式不正确") return } illDateTime, err := utils.ParseTimeStringToTime(timeLayout, illDate+" 00:00:00") if err != nil { c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "患病时间格式不正确") return } member.IllDate = illDateTime.Unix() if dataBody["treat_type"] == nil || reflect.TypeOf(dataBody["treat_type"]).String() != "float64" { c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "缺少参数:治疗方式") return } treatType := int64(dataBody["treat_type"].(float64)) if treatType <= 0 { c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "请选择治疗方式") return } member.TreatType = treatType if dataBody["remark"] != nil && reflect.TypeOf(dataBody["remark"]).String() == "string" { remark, _ := dataBody["remark"].(string) if len(remark) > 0 { member.Remark = remark } } if dataBody["avatar"] != nil && reflect.TypeOf(dataBody["avatar"]).String() == "string" { avatar, _ := dataBody["avatar"].(string) if len(avatar) > 0 { member.Avatar = avatar } } if changeMobile { customer, err := member_service.GetCustomerByMobile(adminUserInfo.CurrentOrgId, member.Mobile) if err != nil { c.ServeFailJsonSend(enums.ErrorCodeDBUpdate, "编辑会员失败:("+err.Error()+")") return } if customer != nil && customer.ID != member.ID { c.ServeFailJsonSend(enums.ErrorCodeDBUpdate, "编辑会员失败:(已经存在该手机号的客户)") return } } member.UpdatedTime = timeNow err = member_service.EditMember(member) if err != nil { c.ServeFailJsonSend(enums.ErrorCodeDBUpdate, "编辑会员失败:("+err.Error()+")") return } illness := member.Illness member.Illness = make([]*member_service.CustomerIllness, 0) for _, ill := range illness { illItem := *ill if illItem.Status == 0 { continue } member.Illness = append(member.Illness, &illItem) } returnData := make(map[string]interface{}, 0) returnData["member"] = member c.ServeSuccessJSON(returnData) return } func (c *MembersAPIController) DeleteMembers() { adminUserInfo := c.GetAdminUserInfo() dataBody := make(map[string]interface{}, 0) err := json.Unmarshal(c.Ctx.Input.RequestBody, &dataBody) if err != nil { utils.ErrorLog(err.Error()) c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "参数错误") return } idsInters := dataBody["ids"].([]interface{}) if len(idsInters) == 0 { if err != nil { c.ServeFailJsonSend(enums.ErrorCodeDBDelete, "删除会员失败:(没有选择会员)") return } } ids := make([]int64, 0) for _, idsInter := range idsInters { id := int64(idsInter.(float64)) ids = append(ids, id) } err = member_service.DeleteMembers(adminUserInfo.CurrentOrgId, ids) if err != nil { c.ServeFailJsonSend(enums.ErrorCodeDBDelete, "删除会员失败:("+err.Error()+")") return } returnData := make(map[string]interface{}, 0) returnData["msg"] = "ok" c.ServeSuccessJSON(returnData) return }