audit_check.go 2.7 KB
package models

import (
	"time"

	"github.com/astaxie/beego/orm"
)

type AuditCheck struct {
	Id         int64     `orm:"column(id);pk"`
	Pid        int64     `orm:"column(pid)"`
	TemplateId int64     `orm:"column(template_id)" description:"模板id"`
	Title      string    `orm:"column(title);size(100)" description:"标题"`
	Items      string    `orm:"column(items)" description:"选项数据json格式"`
	Enable     int8      `orm:"column(enable)" description:"是否有效【0:无效】【1:有效】"`
	CreateTime time.Time `orm:"column(create_time);type(timestamp)"`
}

func (t *AuditCheck) TableName() string {
	return "audit_check"
}

func init() {
	orm.RegisterModel(new(AuditCheck))
}

//AuditCheckItems 自查内容选项
// AuditCheck 表items存储的json结构
type AuditCheckItems struct {
	Items []AuditCheckItem `json:"items"`
}

type AuditCheckItem struct {
	Item      string `json:"item"`      //选项内容
	NeedOther int    `json:"needOther"` //是否需填写其他的内容【1:需要】【2:不需要】
}

//AuditCheck 表enable字段含义 是否有效【0:无效】【1:有效】
const (
	AUDITCHECK_ENABLE_NO  int8 = 0
	AUDITCHECK_ENABLE_YES int8 = 1
)

// AddAuditCheck insert a new AuditCheck into database and returns
// last inserted Id on success.
func AddAuditCheck(m *AuditCheck) (id int64, err error) {
	o := orm.NewOrm()
	id, err = o.Insert(m)
	return
}

// GetAuditCheckById retrieves AuditCheck by Id. Returns error if
// Id doesn't exist
func GetAuditCheckById(id int64) (v *AuditCheck, err error) {
	o := orm.NewOrm()
	v = &AuditCheck{Id: id}
	if err = o.Read(v); err == nil {
		return v, nil
	}
	return nil, err
}

// DeleteAuditCheck deletes AuditCheck by Id and returns error if
// the record to be deleted doesn't exist
// func DeleteAuditCheck(id int64) (err error) {
// 	o := orm.NewOrm()
// 	v := AuditCheck{Id: id}
// 	// ascertain id exists in the database
// 	if err = o.Read(&v); err == nil {
// 		var num int64
// 		if num, err = o.Delete(&AuditCheck{Id: id}); err == nil {
// 			fmt.Println("Number of records deleted in database:", num)
// 		}
// 	}
// 	return
// }

func GetAuditCheckByTemplate(templateId int64) ([]AuditCheck, error) {
	var (
		data []AuditCheck
		err  error
	)
	o := orm.NewOrm()
	_, err = o.QueryTable(&AuditCheck{}).
		Filter("template_id", templateId).
		Filter("enable", AUDITCHECK_ENABLE_YES).
		All(&data)
	if err == orm.ErrNoRows {
		return data, nil
	}
	if err != nil {
		return nil, err
	}
	return data, nil
}

func DeleteAuditCheckByTempelate(templateId int64, om orm.Ormer) error {
	var (
		err error
	)
	_, err = om.QueryTable(&AuditCheck{}).
		Filter("template_id", templateId).
		Filter("enable", AUDITCHECK_ENABLE_YES).
		Update(orm.Params{
			"enable": AUDITCHECK_ENABLE_NO,
		})
	return err
}