audit_template.go 4.9 KB
package models

import (
	"fmt"
	"time"

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

type AuditTemplate struct {
	Id            int64     `orm:"column(id);auto" 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(100)" description:"子分类名称"`
	Doc           string    `orm:"column(doc);size(255)" description:"说明"`
	Icon          string    `orm:"column(icon);size(255)" description:"图标"`
	Code          string    `orm:"column(code);size(50)" 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)" description:"可见的对象 部门 指定人 json"`
	EnableStatus  int8      `orm:"column(enable_status)" description:"是否有效 1:有效 0:无效"`
	CreateAt      time.Time `orm:"column(create_at);type(timestamp)" description:"创建时间"`
	UpdateAt      time.Time `orm:"column(update_at);type(timestamp)" description:"更新时间"`
	Example       string    `orm:"column(example);null" description:"示例"`
	Videos        string    `orm:"column(videos);null" description:"示例"`
	SelfCheckNeed int8      `orm:"column(self_check_need)" description:"是否需要自查内容"`
}

const (
	VisibleTypeAll        = 0
	VisibleTypeDepartment = 1
)

const (
	VisibleObject_User       = 0
	VisibleObject_Department = 1
)

// SelfCheckNeed 是否需要自查内容
const (
	AuditSelfCheckNeedYes int8 = 1 //需要
	AuditSelfCheckNeedNo  int8 = 2 //不需要
)

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
}

func GetAuditTemplateByTypeId(chanceTypeId int) (v []*AuditTemplate, err error) {
	o := orm.NewOrm()
	//_, err = o.QueryTable(&AuditTemplate{}).
	//	Filter("chance_type_id", chanceTypeId).OrderBy("sort_num").All(&v)
	sql := `select id,company_id,chance_type_id,name,doc,icon,code,sort_num,visible_type,visible_object,enable_status 
		from audit_template where  chance_type_id=? order by sort_num`
	if _, err = o.Raw(sql, chanceTypeId).QueryRows(&v); err == nil {
		return
	}
	if err == orm.ErrNoRows {
		return v, nil
	}
	return
}

func GetAuditTemplateByCode(companyId int64, code string, chanceTypeId int) (v *ChanceType, err error) {
	o := orm.NewOrm()
	sql := "select * from audit_template where binary code=? and company_id=? and chance_type_id=?"
	if err = o.Raw(sql, code, companyId, chanceTypeId).QueryRow(&v); err == nil {
		return
	}
	return
}

//同一分类下面 模板名称不能重复
func GetAuditTemplateByName(companyId int64, name string, chanceTypeId int) (v *ChanceType, err error) {
	o := orm.NewOrm()
	sql := "select * from audit_template where  binary name=? and company_id=? and chance_type_id=?"
	if err = o.Raw(sql, name, companyId, chanceTypeId).QueryRow(&v); err == nil {
		return
	}
	return
}

func GetAuditTemplateSort(companyId int64, chanceTypeId int) (v *ChanceType, err error) {
	o := orm.NewOrm()
	sql := "select max(sort_num) sort_num  from audit_template where  company_id=? and chance_type_id=?"
	if err = o.Raw(sql, companyId, chanceTypeId).QueryRow(&v); err == nil {
		return
	}
	return
}