chance_draft.go 3.9 KB
package models

import (
	"fmt"
	"opp/internal/utils"
	"time"

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

type ChanceDraft struct {
	Id                  int64     `orm:"column(id);pk" description:"id 主键"`
	UserId              int64     `orm:"column(user_id)" description:"表user_company.id  id"`
	DepartmentId        int64     `orm:"column(department_id)" description:"表department.id 部门id (提交机会指定的部门)"`
	ChanceTypeId        int       `orm:"column(chance_type_id)" description:"表chance_type.id 机会类型 "`
	CompanyId           int64     `orm:"column(company_id)" description:"表company.id 公司编号"`
	AuditTemplateId     int64     `orm:"column(audit_template_id)" description:"表audit_template.id 所属审批模板编号"`
	AuditTemplateConfig string    `orm:"column(audit_template_config);size(255);null" description:"模板配置 (存旧的配置信息,对新改动的不影响)"`
	SourceContent       string    `orm:"column(source_content)" description:"原始表单内容 json"`
	EnableStatus        int8      `orm:"column(enable_status)" description:"有效状态 0:无效 1:有效 "`
	UpdateAt            time.Time `orm:"column(update_at);type(timestamp)" description:"更新时间"`
	CreateAt            time.Time `orm:"column(create_at);type(timestamp)" description:"创建时间"`
	SelfChecks          string    `orm:"column(self_checks);size(1000);null" description:"自查内容"`
}

func (t *ChanceDraft) TableName() string {
	return "chance_draft"
}

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

var (
	DeleteAllChanceDraft = "update chance_draft set enable_status=0 where user_id=%v and enable_status=1"
	DeleteChanceDraft    = "update chance_draft set enable_status=0 where user_id=%v and id=%v and enable_status=1"
)

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

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

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

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

//草稿项机会列表
func GetDraftByChance(uid int64, offset int, pageSize int, v interface{}) (total int, err error) {
	sql := fmt.Sprintf(`select a.*,b.images,b.speechs,b.videos 
from (
	select b.id chance_id,b.user_id chance_user_id,b.source_content,b.enable_status,b.audit_template_id,
b.chance_type_id,b.create_at,b.update_at,b.department_id,b.self_checks
	from   chance_draft b 
	where b.user_id=%v and enable_status=1
)a left outer join chance_data b on a.chance_id =b.chance_id
order by a.update_at desc
limit %v,%v`, uid, offset, pageSize)

	sqlCount := fmt.Sprintf(`select count(0)
	from   chance_draft b 
	where b.user_id=%v and enable_status=1
`, uid)
	if err = utils.ExecuteQueryOne(&total, sqlCount); err != nil {
		return
	}
	if v != nil {
		if err = utils.ExecuteQueryAll(v, sql); err != nil {
			return
		}
	}
	return
}