123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- package cygx
- import (
- "github.com/beego/beego/v2/client/orm"
- "github.com/rdlucklib/rdluck_tools/paging"
- "strconv"
- "strings"
- "time"
- )
- type CygxQuestionnaire struct {
- QuestionnaireId int `orm:"column(questionnaire_id);pk"`
- ActivityTypeId int `description:"活动类型id"`
- ActivityTypeName string `description:"活动名称"`
- Content string `description:"内容"`
- EndTime time.Time `description:"截止日期"`
- CreateTime time.Time `description:"创建时间"`
- ModifyTime time.Time `description:"更新时间"`
- PublishTime time.Time `description:"发布时间"`
- AdminId int `description:"管理员ID"`
- MaxChooseTotal int `description:"最多可选几项"`
- OtherThemeTotal int `description:"其余主题投票数量"`
- }
- type CygxQuestionnaireTheme struct {
- QuestionnaireThemeId int `orm:"column(questionnaire_theme_id);pk"`
- QuestionnaireId int `description:"问卷ID"`
- ActivityTheme string `description:"活动主题"`
- CreateTime time.Time `description:"创建时间"`
- ModifyTime time.Time `description:"更新时间"`
- }
- type AddCygxQuestionnaireReq struct {
- QuestionnaireId int `description:"ID"`
- ActivityTypeId int `description:"活动类型id"`
- Content string `description:"内容"`
- EndTime string `description:"截止日期"`
- MaxChooseTotal int `description:"最多可选几项"`
- ListTheme []*AddCygxQuestionnaireThemeReq
- }
- type AddCygxQuestionnaireThemeReq struct {
- QuestionnaireThemeId int `description:"问卷主题ID"`
- ActivityTheme string `description:"活动主题"`
- VoteTotal int `description:"票数"`
- }
- type CygxQuestionnaireResp struct {
- QuestionnaireId int `orm:"column(questionnaire_id);pk"`
- ActivityTypeId int `description:"活动类型id"`
- ActivityTypeName string `description:"活动类型名称"`
- Content string `description:"内容"`
- EndTime string `description:"截止日期"`
- CreateTime string `description:"创建时间"`
- ModifyTime string `description:"更新时间"`
- PublishTime string `description:"发布时间"`
- AdminId int `description:"管理员ID"`
- MaxChooseTotal int `description:"最多可选几项"`
- State string `description:"状态"`
- OtherThemeTotal int `description:"其余主题投票数量"`
- ListTheme []*AddCygxQuestionnaireThemeReq
- }
- type GetCygxQuestionnaireResp struct {
- Paging *paging.PagingItem `description:"分页数据"`
- List []*CygxQuestionnaireResp
- }
- // 添加
- func AddCygxQuestionnaire(item *CygxQuestionnaire, listTheme []*AddCygxQuestionnaireThemeReq) (newId int64, err error) {
- o := orm.NewOrmUsingDB("hz_cygx")
- to, err := o.Begin()
- if err != nil {
- return
- }
- defer func() {
- if err != nil {
- _ = to.Rollback()
- } else {
- _ = to.Commit()
- }
- }()
- newId, err = to.Insert(item)
- if err != nil {
- return
- }
- //添加主题与问卷ID的关联
- for _, v := range listTheme {
- itemTheme := new(CygxQuestionnaireTheme)
- itemTheme.QuestionnaireId = int(newId)
- itemTheme.ActivityTheme = v.ActivityTheme
- itemTheme.CreateTime = time.Now()
- itemTheme.ModifyTime = time.Now()
- _, err = to.Insert(itemTheme)
- if err != nil {
- return
- }
- }
- return
- }
- // 编辑
- func EditCygxQuestionnaire(item *CygxQuestionnaire, listTheme []*AddCygxQuestionnaireThemeReq) (err error) {
- o := orm.NewOrmUsingDB("hz_cygx")
- to, err := o.Begin()
- if err != nil {
- return
- }
- defer func() {
- if err != nil {
- _ = to.Rollback()
- } else {
- _ = to.Commit()
- }
- }()
- updateParams := make(map[string]interface{})
- updateParams["ActivityTypeId"] = item.ActivityTypeId
- updateParams["ActivityTypeName"] = item.ActivityTypeName
- updateParams["EndTime"] = item.EndTime
- updateParams["ModifyTime"] = time.Now()
- updateParams["MaxChooseTotal"] = item.MaxChooseTotal
- updateParams["Content"] = item.Content
- ptrStructOrTableName := "cygx_questionnaire"
- whereParam := map[string]interface{}{"questionnaire_id": item.QuestionnaireId}
- qs := to.QueryTable(ptrStructOrTableName)
- for expr, exprV := range whereParam {
- qs = qs.Filter(expr, exprV)
- }
- _, err = qs.Update(updateParams)
- if err != nil {
- return
- }
- //如果有减少的就进行删除
- var questionnaireThemeIds []string
- for _, v := range listTheme {
- questionnaireThemeIds = append(questionnaireThemeIds, strconv.Itoa(v.QuestionnaireThemeId))
- }
- sql := ` DELETE FROM cygx_questionnaire_theme WHERE questionnaire_id = ? AND questionnaire_theme_id NOT IN (` + strings.Join(questionnaireThemeIds, ",") + `)`
- _, err = to.Raw(sql, item.QuestionnaireId).Exec()
- if err != nil {
- return
- }
- //添加主题与问卷ID的关联
- for _, v := range listTheme {
- if v.QuestionnaireThemeId == 0 {
- itemTheme := new(CygxQuestionnaireTheme)
- itemTheme.QuestionnaireId = item.QuestionnaireId
- itemTheme.ActivityTheme = v.ActivityTheme
- itemTheme.CreateTime = time.Now()
- itemTheme.ModifyTime = time.Now()
- _, err = to.Insert(itemTheme)
- if err != nil {
- return
- }
- } else {
- updateParams := make(map[string]interface{})
- updateParams["ActivityTheme"] = v.ActivityTheme
- updateParams["ModifyTime"] = time.Now()
- ptrStructOrTableName := "cygx_questionnaire_theme"
- whereParam := map[string]interface{}{"questionnaire_theme_id": v.QuestionnaireThemeId}
- qs := to.QueryTable(ptrStructOrTableName)
- for expr, exprV := range whereParam {
- qs = qs.Filter(expr, exprV)
- }
- _, err = qs.Update(updateParams)
- if err != nil {
- return
- }
- }
- }
- return
- }
- // 获取数量
- func GetCygxQuestionnaireCount(condition string, pars []interface{}) (count int, err error) {
- sqlCount := ` SELECT COUNT(1) AS count FROM cygx_questionnaire as art WHERE 1= 1 `
- if condition != "" {
- sqlCount += condition
- }
- o := orm.NewOrmUsingDB("hz_cygx")
- err = o.Raw(sqlCount, pars).QueryRow(&count)
- return
- }
- // 列表
- func GetCygxQuestionnaireList(condition string, pars []interface{}, startSize, pageSize int) (items []*CygxQuestionnaireResp, err error) {
- o := orm.NewOrmUsingDB("hz_cygx")
- sql := `SELECT * FROM cygx_questionnaire as art WHERE 1= 1 `
- if condition != "" {
- sql += condition
- }
- sql += ` LIMIT ?,? `
- _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
- return
- }
- type GetCygxQuestionnaireDetailResp struct {
- Detail *CygxQuestionnaireResp
- }
- // 通过ID获取详情
- func GetCygxQuestionnaireDetail(questionnaireId int) (item *CygxQuestionnaireResp, err error) {
- o := orm.NewOrmUsingDB("hz_cygx")
- sql := `SELECT * FROM cygx_questionnaire WHERE questionnaire_id=? `
- err = o.Raw(sql, questionnaireId).QueryRow(&item)
- return
- }
- // 列表
- func GetCygxQuestionnaireThemeListByQuestionnaireId(questionnaireId int) (items []*AddCygxQuestionnaireThemeReq, err error) {
- o := orm.NewOrmUsingDB("hz_cygx")
- sql := `SELECT * FROM cygx_questionnaire_theme as art WHERE questionnaire_id = ? `
- _, err = o.Raw(sql, questionnaireId).QueryRows(&items)
- return
- }
|