audit_form.go 4.4 KB
package models

import (
	"fmt"
	"time"

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

type AuditForm struct {
	Id              int       `orm:"column(id);auto" description:"唯一编号"`
	CompanyId       int       `orm:"column(company_id)" description:"表company.id 公司编号"`
	AuditTemplateId int       `orm:"column(audit_template_id)" description:"表audit_template.id 所属审批模板编号"`
	Section         int8      `orm:"column(section)" description:"输入项所属的版块(1.基础,2.附加)"`
	SortNum         int       `orm:"column(sort_num)" description:"排序"`
	Label           string    `orm:"column(label);size(100)" description:"标题"`
	InputType       string    `orm:"column(input_type);size(50)" description:"输入类型 text"`
	ValueList       string    `orm:"column(value_list);size(255)" description:"可选值列表json结构字符串"`
	Required        int8      `orm:"column(required)" description:"是否必填:【0:否】【1:是】"`
	CurrentValue    string    `orm:"column(current_value);size(255)" description:"实际填写的值"`
	Disable         int8      `orm:"column(disable);null" description:"显示隐藏:【0:显示】【1:隐藏】"`
	Step            string    `orm:"column(step);size(30)" description:"步进,输入类型是range时生效 "`
	Placeholder     string    `orm:"column(placeholder);size(40)" description:"输入提示"`
	CreateAt        time.Time `orm:"column(create_at);type(timestamp);auto_now_add" description:"创建时间"`
	DeleteAt        time.Time `orm:"column(delete_at);type(timestamp)" description:"更新时间"`
	EnableStatus    int8      `orm:"column(enable_status);null" description:"有效状态 0:无效 1:有效 "`
}

func (t *AuditForm) TableName() string {
	return "audit_form"
}

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

//AuditFormValueList 表audit_form中字段value_list的结构
type AuditFormValueList struct {
	Value string `json:"value"`
	Type  string `json:"type"` //输入的数据内容类型
}

//输入框类型
const (
	InputTypeText       string = "text"        //单行文本宽
	InputTypeRadio      string = "radio"       //单选框
	InputTypeImageVedio string = "image/video" // 图片或视频输入
)

//输入框输入的数据类型
const (
	InputDataTypeText  string = "text"  //单纯文本
	InputDataTypeImage string = "image" //图片的文件url
	InputDataTypeVedio string = "video" //视频的文件url
)

var (
	InputDataTypeMap map[string]bool = map[string]bool{
		InputDataTypeText:  true,
		InputDataTypeImage: true,
		InputDataTypeVedio: true,
	}
	InputTypeMap map[string]bool = map[string]bool{
		InputTypeText:       true,
		InputTypeRadio:      true,
		InputTypeImageVedio: true,
	}
)

var (
	DeleteAuditFormBy = `delete FROM audit_form where audit_template_id=? and company_id=?`
)

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

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

// UpdateAuditForm updates AuditForm by Id and returns error if
// the record to be updated doesn't exist
func UpdateAuditFormById(m *AuditForm) (err error) {
	o := orm.NewOrm()
	v := AuditForm{Id: m.Id}
	// ascertain id exists in the database
	if err = o.Read(&v); err == nil {
		var num int64
		if num, err = o.Update(m); err == nil {
			fmt.Println("Number of records updated in database:", num)
		}
	}
	return
}

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

// GetAuditFormById retrieves AuditForm by Id. Returns error if
// Id doesn't exist
func GetAuditFormByTemplateId(id int) (v []*AuditForm, err error) {
	o := orm.NewOrm()
	sql := `select * from audit_form where audit_template_id =? and enable_status=1 order by sort_num`
	if _, err = o.Raw(sql, id).QueryRows(&v); err == nil {
		return v, nil
	}
	return nil, err
}