audit_template.go 4.0 KB
package models

import (
	"fmt"
	"time"

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

type AuditTemplate struct {
	Id            int64     `orm:"column(id);pk" description:"唯一编号"`
	CompanyId     int       `orm:"column(company_id)" description:"公司id"`
	ChanceTypeId  int       `orm:"column(chance_type_id)" description:"机会类型 chance_type.id"`
	Name          string    `orm:"column(name);size(20)" description:"子分类名称"`
	Doc           string    `orm:"column(doc);size(255)" description:"说明"`
	Icon          string    `orm:"column(icon);size(255)" description:"图标"`
	Code          string    `orm:"column(code);size(50);null" description:" 编码"`
	NoticeType    int8      `orm:"column(notice_type)" description:"通知方式"`
	NoApprover    int8      `orm:"column(no_approver)" description:"审核人空时:【1:自动通过】【2:转交给管理员】"`
	SortNum       int       `orm:"column(sort_num)" description:"自定义排序编号"`
	VisibleType   int8      `orm:"column(visible_type)" description:"可见范围  0:所有人 1:指定部门 "`
	VisibleObject string    `orm:"column(visible_object);size(1000);null" description:"可见的对象 部门 指定人 json"`
	EnableStatus  int8      `orm:"column(enable_status)" description:"是否有效 1:有效 0:无效"`
	Example       string    `orm:"column(example);null" description:"示例"`
	CreateAt      time.Time `orm:"column(create_at);type(timestamp)" description:"创建时间"`
	UpdateAt      time.Time `orm:"column(update_at);type(timestamp)" description:"更新时间"`
}

func (t *AuditTemplate) TableName() string {
	return "audit_template"
}

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

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

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

// UpdateAuditTemplate updates AuditTemplate by Id and returns error if
// the record to be updated doesn't exist
func UpdateAuditTemplateById(m *AuditTemplate) (err error) {
	o := orm.NewOrm()
	v := AuditTemplate{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
}

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

//按 1.公司编号 2.机会类型编号
//获取审核模板列表
func GetAuditTemplates(companyId int64, chanceTypeId int) (v []*AuditTemplate, err error) {
	o := orm.NewOrm()
	sql := "select * from audit_template where company_id=? and chance_type_id=? and enable_status=1 order by sort_num"
	if _, err = o.Raw(sql, companyId, chanceTypeId).QueryRows(&v); err == nil {
		return
	}
	return
}

//获取模板示例
func GetAuditTemplateExample(id int64) (v string, err error) {
	o := orm.NewOrm()
	sql := "select example from audit_template where  id=?"
	if err = o.Raw(sql, id).QueryRow(&v); err == nil {
		return
	}
	return
}

//按 1.公司编号 2.机会类型编号 3.模板编号
//获取审核模板列表
func GetAuditTemplate(companyId int64, chanceTypeId, templateId int) (v *AuditTemplate, err error) {
	o := orm.NewOrm()
	sql := "select * from audit_template where id=? and company_id=? and chance_type_id=? and enable_status=1"
	if err = o.Raw(sql, templateId, companyId, chanceTypeId).QueryRow(&v); err == nil {
		return
	}
	return
}