chance.go 7.9 KB
package models

import (
	"encoding/json"
	"fmt"
	"strings"
	"time"

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

type Chance 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:"模板配置 (存旧的配置信息,对新改动的不影响)"`
	Content             string    `orm:"column(content)" description:"格式化后的文本内容"`
	SourceContent       string    `orm:"column(source_content)" description:"原始表单内容 json"`
	ViewTotal           int       `orm:"column(view_total)" description:"查看总数"`
	CommentTotal        int       `orm:"column(comment_total)" description:"评论总数"`
	ZanTotal            int       `orm:"column(zan_total)" description:"点赞总数"`
	ReviewStatus        int8      `orm:"column(review_status)" description:"审核状态 0:待处理 1:待审核 2:被退回 3:已通过 "`
	EnableStatus        int8      `orm:"column(enable_status)" description:"有效状态 0:无效 1:有效 "`
	UpdateAt            string    `orm:"column(update_at)" description:"更新时间"`
	CreateAt            time.Time `orm:"column(create_at);type(timestamp);" description:"创建时间"`
	BasicScore          float64   `orm:"column(basic_score);null;digits(4);decimals(1)" description:"基础评分"`
	ExtraScore          float64   `orm:"column(extra_score);null;digits(4);decimals(1)" description:"附加评分"`
	ValueScore          float64   `orm:"column(value_score);null;digits(4);decimals(1)" description:"价值评分"`
	DiscoveryScore      float64   `orm:"column(discovery_score);null;digits(4);decimals(1)" description:"发现得分(发现得分=基础评分*系数 + 附加评分*系数 + 价值评分*系数)"`
	PublishStatus       int       `orm:"column(publish_status)" description:"公开状态 -1 未公开、1部门公开、2公司公开"`
	AuditLevel          int       `orm:"column(audit_level)" description:"当前审批步骤"`
	ApproveData         string    `orm:"column(approve_data);size(500);null" description:"公开数据 (公开状态 公开对象)"`
	Code                string    `orm:"column(code)" description:"机会编码"`
	Status              int8      `orm:"column(status)" description:"机会状态 1:开启 2:关闭"`
	SelfChecks          string    `orm:"column(self_checks)" description:"自查内容"`
	CheckResultStatus   int8      ` orm:"column(check_result_status)"`
	CheckTime           time.Time `orm:"column(check_time);type(timestamp);"`
	CheckResult         string    `orm:"column(check_result)" description:"自查内容筛选结果"`
}

func (t *Chance) TableName() string {
	return "chance"
}

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

// 公开状态 0未设置、1部门公开、2公司公开
const (
	ChancePublishStatus0 int = 0
	ChancePublishStatus1 int = 1
	ChancePublishStatus2 int = 2
)

var ChancePublishStatusMap = map[int]string{
	ChancePublishStatus0: "未设置",
	ChancePublishStatus1: "部门公开",
	ChancePublishStatus2: "公司公开",
}

// 审核状态 0:待处理 1:待审核 2:被退回 3:已通过
const (
	// ChanceReviewStatusWait int = 0
	ChanceReviewStatusIng int8 = 1
	ChanceReviewStatusNo  int8 = 2
	ChanceReviewStatusYes int8 = 3
)

var ChanceReviewStatusMap = map[int8]string{
	// ChanceReviewStatusWait: "待处理",
	ChanceReviewStatusIng: "待审核",
	ChanceReviewStatusNo:  "被退回",
	ChanceReviewStatusYes: "已通过",
}

//有效状态 0:无效 1:有效
const (
	ChanceEnableStatusYes int8 = 1
	ChanceEnableStatusNo  int8 = 0
)

var ChanceEnableStatusMap = map[int8]string{
	ChanceEnableStatusNo:  "无效",
	ChanceEnableStatusYes: "有效",
}

//机会的开启关闭状态  1:已开启 2:已关闭
const (
	ChanceStatusAllow  int8 = 1
	ChanceStatusForbid int8 = 2
)

var ChanceStatusMap = map[int8]string{
	ChanceStatusAllow:  "已开启",
	ChanceStatusForbid: "已关闭",
}

//机会自查内容筛选状态 【0:未提交】【1:待处理未设置】【2:通过】【3:不通过】
const (
	CheckResultStatusNone   int8 = 0
	CheckResultStatusWait   int8 = 1
	CheckResultStatusPass   int8 = 2
	CheckResultStatusNOPass int8 = 3
)

var CheckResultStatusMap = map[int8]string{
	CheckResultStatusWait:   "未设置",
	CheckResultStatusPass:   "通过",
	CheckResultStatusNOPass: "未通过",
}

//ChanceSelfCheck chance表中SelfChecks字段的json结构
type ChanceSelfCheckData struct {
	CheckItem string `json:"checkItem"`
	GroupId   int64  `json:"groupId"`
	Answer    string `json::"answer"`
	Id        int64  `json:"id"`
	ParentId  int64  `json:"parentId"`
	Reason    string `json:"reason"`
}

func (m *Chance) GetSelfCheckData() []ChanceSelfCheckData {
	var data []ChanceSelfCheckData
	json.Unmarshal([]byte(m.CheckResult), &data)
	return data
}

func (m *Chance) GetCheckResultData() []ChanceSelfCheckData {
	var data []ChanceSelfCheckData
	json.Unmarshal([]byte(m.CheckResult), &data)
	return data
}

type ChanceSelfCheckList []ChanceSelfCheckData

func (s ChanceSelfCheckList) Format() ChanceSelfCheckList {
	if len(s) == 0 {
		return s
	}
	for i := range s {
		if strings.TrimSpace(s[i].Answer) == "否" {
			s[i].Reason = strings.TrimSpace(s[i].Reason)
		}
	}
	return s
}

func (s ChanceSelfCheckList) SetSelfChecksLevel1ByRule() (err error) {
	if len(s) == 0 {
		return
	}
	s.Format()
	var gIdx = -1
	for i := 0; i < len(s); i++ {
		if gIdx < 0 || s[gIdx].GroupId != s[i].GroupId {
			gIdx = i
		} else {
			continue
		}
		hasSub := false
		var (
			cntYes       = 0 //是
			cntNo        = 0 //否
			cntUncertain = 0 //不确定
		)
		for j := i + 1; j < len(s); j++ {
			if s[i].GroupId == s[j].GroupId {
				if !hasSub {
					hasSub = true
				}
			} else {
				break
			}
			answer := s[j].Answer
			if strings.EqualFold(strings.TrimSpace(answer), "是") {
				cntYes++
			} else if strings.EqualFold(strings.TrimSpace(answer), "否") {
				cntNo++
			} else if strings.EqualFold(strings.TrimSpace(answer), "不清楚") {
				cntUncertain++
			}
		}
		//只有一级维度的必须填写
		if hasSub {
			if cntYes > 0 {
				s[gIdx].Answer = "是"
			} else if cntNo > 0 {
				s[gIdx].Answer = "否"
			} else if cntUncertain > 0 {
				s[gIdx].Answer = "不清楚"
			}
		}
		if hasSub && cntYes == 0 && cntNo == 0 && cntUncertain == 0 {
			err = fmt.Errorf("未填写自查项:%v,二级维度至少需要填写一项", s[gIdx].CheckItem)
		}
		if !hasSub && len(s[gIdx].Answer) == 0 {
			err = fmt.Errorf("未填写自查项:%v", s[gIdx].CheckItem)
		}
	}
	return
}

//格式化数据结构
func (s ChanceSelfCheckList) ClearEmpty() (rsp ChanceSelfCheckList) {
	if len(s) == 0 {
		return
	}
	for i := range s {
		if len(s[i].Answer) > 0 {
			rsp = append(rsp, s[i])
		}
	}
	return
}

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

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

// UpdateChance updates Chance by Id and returns error if
// the record to be updated doesn't exist
func UpdateChanceById(m *Chance, col []string) (err error) {
	o := orm.NewOrm()
	var num int64
	if num, err = o.Update(m, col...); err == nil {
		fmt.Println("Number of records updated in database:", num)
	}
	return
}