package service import ( "gin-vue-admin/global" "gin-vue-admin/model" "gin-vue-admin/model/request" "strconv" ) func CreateSurveyUser(req model.SurveyUser) (error, int) { err := global.GVA_DB.Table("survey_user").Create(&req).Error return err, req.Id } func UpdateSurveyUser(req model.SurveyUser) (error, int) { err := global.GVA_DB.Table("survey_user").Where("id=?", req.Id).Updates(&req).Error return err, req.Id } func CreateSurveyUserData(req model.SurveyUserData) error { err := global.GVA_DB.Table("survey_user_data").Create(&req).Error return err } func CreateSurveyLog(req model.SurveyLog) error { err := global.GVA_DB.Table("survey_log").Create(&req).Error return err } func GetSurveyUserList(req request.GetSurveyUserListReq) (error, []request.GetSurveyUserList, int64) { pagesize := 10 page := 1 if req.PageSize != 0 { pagesize = req.PageSize } if req.Page != 0 { page = req.Page } currentpage := pagesize * (page - 1) list := make([]request.GetSurveyUserList, 0) table := " survey_user as sur " + " left join customer_user as cu on sur.user_id=cu.id " + " left join vcode as vc on sur.user_id=vc.user_id and sur.moc_id = vc.moc_id " field := " sur.id, sur.user_id, sur.moc_id, sur.code_mobile, sur.code, sur.contacts, sur.contacts_mobile" + ", sur.reference, sur.reference_mobile, sur.create_time" + ", sur.country, sur.area, sur.region, sur.city" + ", cu.worker_name, cu.worker_mobile " + ", vc.code as vcode " conditions := "" orderby := " sur.create_time desc " if req.MocId != 0 { conditions += " AND sur.moc_id = " + strconv.Itoa(req.MocId) } if req.Status != 0 { conditions += " AND sur.status = " + strconv.Itoa(req.Status) } if req.Region != "" { conditions += " AND sur.region like '%" + req.Region + "%'" } if req.City != "" { conditions += " AND sur.city like '%" + req.City + "%'" } if req.Contacts != "" { conditions += " AND sur.contacts like '%" + req.Contacts + "%'" } if req.ContactsMobile != "" { conditions += " AND sur.contacts_mobile like '%" + req.ContactsMobile + "%'" } if req.Reference != "" { conditions += " AND sur.reference like '%" + req.Reference + "%'" } if req.ReferenceMobile != "" { conditions += " AND sur.reference_mobile like '%" + req.ReferenceMobile + "%'" } if req.StartCreateTime != "" { conditions += " AND sur.create_time >'" + req.StartCreateTime + " 00:00:00'" } if req.EndCreateTime != "" { conditions += " AND sur.create_time <='" + req.EndCreateTime + " 23:59:59'" } //@@总条数,总页数 var totalItem int64 = 0 sqlStr := "SELECT count(sur.id) as totalItem FROM " + table + " where 1=1 " + conditions global.GVA_DB.Raw(sqlStr).Count(&totalItem) //获取总条数 sqlStr2 := "SELECT " + field + " FROM " + table + " WHERE 1>0 " + conditions + " ORDER BY " + orderby + " LIMIT " + strconv.Itoa(currentpage) + "," + strconv.Itoa(pagesize) global.GVA_DB.Raw(sqlStr2).Scan(&list) if global.GVA_DB.Error != nil { return global.GVA_DB.Error, nil, 0 } return nil, list, totalItem } func GetSurveyUserDataList(id int) []model.SurveyUserData { list := make([]model.SurveyUserData, 0) table := " survey_user_data " field := " survey_user_id, `option`, option_value, option_num, price " conditions := " AND survey_user_id = " + strconv.Itoa(id) sqlStr2 := "SELECT " + field + " FROM " + table + " WHERE 1>0 " + conditions global.GVA_DB.Raw(sqlStr2).Scan(&list) if global.GVA_DB.Error != nil { return []model.SurveyUserData{} } return list } func GetSurveyLogList(req request.GetSurveyLogListReq) (error, []request.GetSurveyLogList, int64) { pagesize := 10 page := 1 if req.PageSize != 0 { pagesize = req.PageSize } if req.Page != 0 { page = req.Page } currentpage := pagesize * (page - 1) list := make([]request.GetSurveyLogList, 0) table := " survey_log as sl " + " left join customer_user as cu on sur.user_id=cu.id " field := " sur.id, sur.user_id, sur.moc_id, sur.contacts, sur.contacts_mobile" + ", sur.reference, sur.reference_mobile, sur.create_time" + ", sur.country, sur.area, sur.region, sur.city" + ", cu.worker_name, cu.worker_mobile " conditions := "" orderby := " sur.create_time desc " if req.MocId != 0 { conditions += " AND sur.moc_id = " + strconv.Itoa(currentpage) } if req.Region != "" { conditions += " AND region like '%" + req.Region + "%'" } if req.City != "" { conditions += " AND city like '%" + req.City + "%'" } if req.StartCreateTime != "" { conditions += " AND sur.create_time >'" + req.StartCreateTime + "'" } if req.EndCreateTime != "" { conditions += " AND sur.create_time <='" + req.EndCreateTime + "'" } //@@总条数,总页数 var totalItem int64 = 0 sqlStr := "SELECT count(vo.id) as totalItem FROM " + table + " where 1=1 " + conditions global.GVA_DB.Raw(sqlStr).Count(&totalItem) //获取总条数 sqlStr2 := "SELECT " + field + " FROM " + table + " WHERE 1>0 " + conditions + " ORDER BY " + orderby + " LIMIT " + strconv.Itoa(currentpage) + "," + strconv.Itoa(pagesize) global.GVA_DB.Raw(sqlStr2).Scan(&list) if global.GVA_DB.Error != nil { return global.GVA_DB.Error, nil, 0 } return nil, list, totalItem } // 留资数量 func GetSurveyUserCount(req request.GetSurveyUserListReq) int64 { table := " survey_user as sur " + " left join customer_user as cu on sur.user_id=cu.id " + " left join vcode as vc on sur.user_id=vc.user_id and sur.moc_id = vc.moc_id " conditions := "" if req.MocId != 0 { conditions += " AND sur.moc_id = " + strconv.Itoa(req.MocId) } if req.Status != 0 { conditions += " AND sur.status = " + strconv.Itoa(req.Status) } if req.Region != "" { conditions += " AND sur.region like '%" + req.Region + "%'" } if req.City != "" { conditions += " AND sur.city like '%" + req.City + "%'" } if req.Contacts != "" { conditions += " AND sur.contacts like '%" + req.Contacts + "%'" } if req.ContactsMobile != "" { conditions += " AND sur.contacts_mobile like '%" + req.ContactsMobile + "%'" } if req.Reference != "" { conditions += " AND sur.reference like '%" + req.Reference + "%'" } if req.ReferenceMobile != "" { conditions += " AND sur.reference_mobile like '%" + req.ReferenceMobile + "%'" } if req.StartCreateTime != "" { conditions += " AND sur.create_time >'" + req.StartCreateTime + " 00:00:00'" } if req.EndCreateTime != "" { conditions += " AND sur.create_time <='" + req.EndCreateTime + " 23:59:59'" } if req.IsReference == 1 { conditions += " AND sur.contacts = cu.worker_mobile " } else if req.IsReference == 2 { conditions += " AND sur.contacts != cu.worker_mobile " } //@@总条数,总页数 var totalItem int64 = 0 sqlStr := "SELECT count(sur.id) as totalItem FROM " + table + " where 1=1 " + conditions global.GVA_DB.Raw(sqlStr).Count(&totalItem) //获取总条数 if global.GVA_DB.Error != nil { return 0 } return totalItem } // 点击量 func GetSurveyLogCount(req request.GetSurveyLogListReq) []request.SlData { list := make([]request.SlData, 0) table := " survey_log as sl " + " left join customer_user as cu on sl.user_id=cu.id " field := " count(sl.id) as cid, sl.page " conditions := "" if req.MocId != 0 { conditions += " AND sl.moc_id = " + strconv.Itoa(req.MocId) } if req.SlPage != 0 { conditions += " AND sl.page = " + strconv.Itoa(req.SlPage) } if req.Region != "" { conditions += " AND sl.region like '%" + req.Region + "%'" } if req.City != "" { conditions += " AND sl.city like '%" + req.City + "%'" } if req.StartCreateTime != "" { conditions += " AND sl.create_time >'" + req.StartCreateTime + " 00:00:00'" } if req.EndCreateTime != "" { conditions += " AND sl.create_time <='" + req.EndCreateTime + " 23:59:59'" } sqlStr2 := "SELECT " + field + " FROM " + table + " WHERE 1>0 " + conditions if req.Groupby != "" { sqlStr2 += " GROUP BY " + req.Groupby } global.GVA_DB.Raw(sqlStr2).Scan(&list) if global.GVA_DB.Error != nil { return nil } return list }