package models
|
|
|
|
import (
|
|
"SSW_WebPlatform/db"
|
|
"SSW_WebPlatform/utils"
|
|
"strings"
|
|
"xorm.io/core"
|
|
)
|
|
|
|
type Subject struct {
|
|
Cid int `json:"cid" xorm:"not null pk INT(11)"`
|
|
Subjectid string `json:"subjectid" xorm:"not null pk comment('题目ID') VARCHAR(20)"`
|
|
Subjecttext string `json:"subjecttext" xorm:"not null comment('题目内容') VARCHAR(255)"`
|
|
Subjectpicture string `json:"subjectpicture" xorm:"default 'NULL' comment('题目图片') VARCHAR(255)"`
|
|
Subjectvideo string `json:"subjectvideo" xorm:"default 'NULL' comment('题目视频') VARCHAR(255)"`
|
|
Subjecttype string `json:"subjecttype" xorm:"default 'NULL' comment('问题类型,单选多选...') VARCHAR(10)"`
|
|
SubjectCategoryid string `json:"subject_categoryid" xorm:"not null comment('问题类别') VARCHAR(10)"`
|
|
Level int `json:"level" xorm:"not null comment('级别') INT(10)"`
|
|
Fsubjectid string `json:"fsubjectid" xorm:"not null comment('父类题目') VARCHAR(10)"`
|
|
Condition string `json:"condition" xorm:"not null comment('条件') VARCHAR(10)"`
|
|
Createtime string `json:"createtime" xorm:"default 'NULL' comment('创建时间') VARCHAR(40)"`
|
|
Lastmodifytime string `json:"lastmodifytime" xorm:"default 'NULL' comment('最近更新时间') VARCHAR(40)"`
|
|
Lastmodifyby string `json:"lastmodifyby" xorm:"default 'NULL' comment('修改人员') VARCHAR(40)"`
|
|
Valst []SubjectOptionlst `json:"valst" xorm:"-"`
|
|
}
|
|
|
|
func (t *Subject) TableName() string {
|
|
return "subject"
|
|
}
|
|
|
|
// 清除string字段的右侧空格
|
|
func (t *Subject) Clipped() {
|
|
utils.TrimStruct(t, *t)
|
|
}
|
|
|
|
//增
|
|
func (t *Subject) Add() error {
|
|
e := db.MasterEngine()
|
|
snr := new(Snrtab)
|
|
snr.Cid = t.Cid
|
|
id, err := snr.GetNextSnr("Subjectid")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
t.Subjectid = id
|
|
_, err = e.Table("subject").Insert(t)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
//批量删除
|
|
_, err = e.Table("subject_optionlst").Where("cid = ? and subjectid = ?", t.Cid, t.Subjectid).Delete(&SubjectOptionlst{})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
//批量插入
|
|
alldata := make([]SubjectOptionlst, 0)
|
|
for _, v := range t.Valst {
|
|
onedata := SubjectOptionlst{}
|
|
onedata.Cid = t.Cid
|
|
onedata.Subjectid = t.Subjectid
|
|
onedata.Optionid = v.Optionid
|
|
onedata.Optiontext = v.Optiontext
|
|
onedata.Optionpicture = v.Optionpicture
|
|
onedata.Optionvideo = v.Optionvideo
|
|
onedata.Optionvalue = v.Optionvalue
|
|
onedata.Optionfactor = v.Optionfactor
|
|
onedata.Createtime = t.Createtime
|
|
onedata.Lastmodifytime = t.Lastmodifytime
|
|
onedata.Optiontype = v.Optiontype
|
|
onedata.Optioninput = v.Optioninput
|
|
onedata.Lastmodifyby = t.Lastmodifyby
|
|
alldata = append(alldata, onedata)
|
|
}
|
|
_, err = e.Table("subject_optionlst").Insert(&alldata)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
//删
|
|
func (t *Subject) Del() bool {
|
|
e := db.MasterEngine()
|
|
_, err := e.ID(core.PK{t.Cid, t.Subjectid}).Delete(&Subject{})
|
|
if err != nil {
|
|
return false
|
|
}
|
|
countrole := new(SubjectOptionlst)
|
|
affw, _ := e.Table("subject_optionlst").Where("cid = ? and subjectid = ?", t.Cid, t.Subjectid).Count(countrole)
|
|
if affw > 0 {
|
|
_, err = e.Where("cid = ? and subjectid = ?", t.Cid, t.Subjectid).Delete(&SubjectOptionlst{})
|
|
if err != nil {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
//改
|
|
func (t *Subject) Update() bool {
|
|
e := db.MasterEngine()
|
|
|
|
_, err := e.ID(core.PK{t.Cid, t.Subjectid}).Cols("lastmodifytime", "createtime", "condition", "subjecttext", "subjectpicture", "subjectvideo", "subjecttype", "subject_categoryid", "level", "fsubjectid").Update(t)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
//批量删除
|
|
_, err = e.Table("subject_optionlst").Where("cid = ? and subjectid = ?", t.Cid, t.Subjectid).Delete(&SubjectOptionlst{})
|
|
if err != nil {
|
|
return false
|
|
}
|
|
//批量插入
|
|
alldata := make([]SubjectOptionlst, 0)
|
|
for _, v := range t.Valst {
|
|
onedata := SubjectOptionlst{}
|
|
onedata.Cid = t.Cid
|
|
onedata.Subjectid = t.Subjectid
|
|
onedata.Optionid = v.Optionid
|
|
onedata.Optiontext = v.Optiontext
|
|
onedata.Optionpicture = v.Optionpicture
|
|
onedata.Optionvideo = v.Optionvideo
|
|
onedata.Optionvalue = v.Optionvalue
|
|
onedata.Optionfactor = v.Optionfactor
|
|
onedata.Optiontype = v.Optiontype
|
|
onedata.Optioninput = v.Optioninput
|
|
onedata.Createtime = t.Createtime
|
|
onedata.Lastmodifytime = t.Lastmodifytime
|
|
onedata.Lastmodifyby = t.Lastmodifyby
|
|
alldata = append(alldata, onedata)
|
|
}
|
|
_, err = e.Table("subject_optionlst").Insert(&alldata)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
//查
|
|
func (t *Subject) SelectOne() (Subject, error) {
|
|
e := db.MasterEngine()
|
|
var data Subject
|
|
_, err := e.ID(core.PK{t.Cid, t.Subjectid}).Get(&data)
|
|
if err != nil {
|
|
return data, err
|
|
}
|
|
var datalist []SubjectOptionlst
|
|
err = e.Where("cid = ? and subjectid = ?", t.Cid, t.Subjectid).Find(&datalist)
|
|
if err != nil {
|
|
return data, err
|
|
}
|
|
data.Valst = datalist
|
|
return data, nil
|
|
}
|
|
|
|
//分页
|
|
func (t *Subject) GetPage(pageSize int, pageIndex int, ids string) ([]Subject, int, error) {
|
|
data := make([]Subject, 0)
|
|
e := db.MasterEngine()
|
|
query := e.Table("subject").Where("cid = ? ", t.Cid)
|
|
table := e.Table("subject").Where("cid = ? ", t.Cid)
|
|
if !utils.ValueIsEmpty(ids) {
|
|
sids := strings.Split(ids, ",")
|
|
query = query.NotIn("subjectid", sids)
|
|
table = table.NotIn("subjectid", sids)
|
|
}
|
|
if !utils.ValueIsEmpty(t.Subjectid) {
|
|
query = query.And("subjectid = ?", t.Subjectid)
|
|
table = table.And("subjectid = ?", t.Subjectid)
|
|
}
|
|
if !utils.ValueIsEmpty(t.SubjectCategoryid) {
|
|
query = query.And("subject_categoryid = ?", t.SubjectCategoryid)
|
|
table = table.And("subject_categoryid = ?", t.SubjectCategoryid)
|
|
}
|
|
if !utils.ValueIsEmpty(t.Subjecttext) {
|
|
Subjecttext := "%" + t.Subjecttext + "%"
|
|
query = query.And("subjecttext like ?", Subjecttext)
|
|
table = table.And("subjecttext like ?", Subjecttext)
|
|
}
|
|
if t.Level > 0 {
|
|
query = query.And("level != ?", 1)
|
|
table = table.And("level != ?", 1)
|
|
}
|
|
Offset := (pageIndex - 1) * pageSize
|
|
err := query.Limit(pageSize, Offset).Desc("createtime").Find(&data)
|
|
pcount := new(Subject)
|
|
count, err := table.Count(pcount)
|
|
if err != nil {
|
|
return data, 0, err
|
|
}
|
|
for k, v := range data {
|
|
datalist := make([]SubjectOptionlst, 0)
|
|
err := e.Table("subject_optionlst").Where("cid = ? and subjectid = ?", v.Cid, v.Subjectid).Find(&datalist)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
data[k].Valst = datalist
|
|
}
|
|
return data, int(count), nil
|
|
}
|
|
|
|
//查询基本信息筛选条件
|
|
func (t *Subject) SelectArr() ([]Subject, error) {
|
|
data := make([]Subject, 0)
|
|
e := db.MasterEngine()
|
|
query := e.Table("subject").Where("cid = ? ", t.Cid)
|
|
if !utils.ValueIsEmpty(t.SubjectCategoryid) {
|
|
query = query.And("subject_categoryid = ?", t.SubjectCategoryid)
|
|
}
|
|
if t.Level > 0 {
|
|
query = query.And("level != ?", 1)
|
|
}
|
|
err := query.Desc("createtime").Find(&data)
|
|
if err != nil {
|
|
return data, err
|
|
}
|
|
for k, v := range data {
|
|
datalist := make([]SubjectOptionlst, 0)
|
|
err := e.Table("subject_optionlst").Where("cid = ? and subjectid = ?", v.Cid, v.Subjectid).Find(&datalist)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
data[k].Valst = datalist
|
|
}
|
|
return data, nil
|
|
}
|