package WorkOrder import ( "Cold_WorkOrder/conf" "Cold_WorkOrder/lib" "Cold_WorkOrder/logs" "Cold_WorkOrder/models/Account" "encoding/json" "fmt" "github.com/beego/beego/v2/adapter/orm" orm2 "github.com/beego/beego/v2/client/orm" _ "github.com/go-sql-driver/mysql" "time" ) // 模板 type WorkOrder struct { Id int `orm:"column(ID);size(11);auto;pk"` T_uuid string `orm:"size(50);null"` // 提交人uuid T_pid int `orm:"index;size(256);null"` // Account.Company 绑定公司 T_title string `orm:"size(256);null"` // 标题 T_text string `orm:"type(text);null"` // 详情 T_deal_with string `orm:"type(text);null"` // 工单处理信息 处理信息,处理人,处理时间|处理信息,处理人,处理时间| T_callback string `orm:"type(text);null"` // 工单回访信息 回访信息,回访人,回访时间|回访信息,回访人,回访时间| T_State int `orm:"size(2);default(1)"` // 0 删除 1 已处理 2 待处理 3 已回访 CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间 UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now 每次 model 保存时都会对时间自动更新 } type WorkOrder_R struct { Id int // ID T_name string // 用户名 T_phone string // 电话 T_Company_name string // 公司名 T_title string // 标题 T_text string // 详情 T_State int // 0 删除 1 已处理 2 待处理 3 已回访 T_deal_with []WorkOrderInfo // 处理信息 T_callback []WorkOrderInfo // 回访信息 CreateTime string // 创建时间 UpdateTime string // 更新时间 } type WorkOrder_ struct { Id int // ID T_uuid string // 用户uuid T_name string // 用户名 T_phone string // 电话 T_Company_name string // 公司名 T_title string // 标题 T_text string // 详情 T_State int // 状态 0 删除 1 已处理 2 待处理 3 已回访 T_deal_with []WorkOrderInfo // 处理信息 T_callback []WorkOrderInfo // 回访信息 CreateTime string // 创建时间 UpdateTime string // 更新时间 } func (t *WorkOrder) TableName() string { return "work_order" // 数据库名称 // ************** 替换 FormulaList ************** } func init() { //注册模型 orm.RegisterModel(new(WorkOrder)) } type WorkOrderInfo struct { T_text string // 处理信息 T_uuid string // 处理人 T_name string // 处理人名称 T_Company_name string // 处理人公司 T_time string // 处理时间 } func UpdateWorkOrderInfoToString(oldInfo, T_info, T_uuid string) string { var list []WorkOrderInfo _ = json.Unmarshal([]byte(oldInfo), &list) workOrderInfo := WorkOrderInfo{T_text: T_info, T_uuid: T_uuid, T_time: time.Now().Format("2006-01-02 15:04:05")} list = append(list, workOrderInfo) listJson, _ := json.Marshal(list) return string(listJson) } func StringToWorkOrderInfoList(info string) []WorkOrderInfo { var list []WorkOrderInfo _ = json.Unmarshal([]byte(info), &list) for i := 0; i < len(list); i++ { list[i].T_name = Account.Read_User_T_name_Get(list[i].T_uuid) pid := Account.Read_User_T_pid_Get(list[i].T_uuid) if pid == 0 { list[i].T_Company_name = "宝智达" } else { list[i].T_Company_name = Account.Read_Company_T_name_Get(pid) } } return list } func WorkOrderToWorkOrder_R(t WorkOrder) (r WorkOrder_R) { r.T_Company_name = Account.Read_Company_T_name_Get(t.T_pid) r.T_name = Account.Read_User_T_name_Get(t.T_uuid) r.T_phone = Account.Read_User_T_phone_Get(t.T_uuid) r.Id = t.Id r.T_title = t.T_title r.T_text = t.T_text r.T_State = t.T_State r.T_deal_with = StringToWorkOrderInfoList(t.T_deal_with) r.T_callback = StringToWorkOrderInfoList(t.T_callback) r.CreateTime = t.CreateTime.Format("2006-01-02 15:04:05") r.UpdateTime = t.UpdateTime.Format("2006-01-02 15:04:05") return } func WorkOrderToWorkOrder_(t WorkOrder) (r WorkOrder_) { r.T_Company_name = Account.Read_Company_T_name_Get(t.T_pid) r.T_name = Account.Read_User_T_name_Get(t.T_uuid) r.T_phone = Account.Read_User_T_phone_Get(t.T_uuid) r.Id = t.Id r.T_uuid = t.T_uuid r.T_title = t.T_title r.T_text = t.T_text r.T_State = t.T_State r.T_deal_with = StringToWorkOrderInfoList(t.T_deal_with) r.T_callback = StringToWorkOrderInfoList(t.T_callback) r.CreateTime = t.CreateTime.Format("2006-01-02 15:04:05") r.UpdateTime = t.UpdateTime.Format("2006-01-02 15:04:05") return } // ---------------- 特殊方法 ------------------- // 获取 ById func Read_WorkOrder_ById(id int) (r WorkOrder, err error) { o := orm.NewOrm() r = WorkOrder{Id: id} err = o.Read(&r) // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名 if err != nil { logs.Error("Read_WorkOrder_ById", err) return r, err } if r.T_State == 0 { return r, orm.ErrNoRows } return r, nil } // 添加 func Add_WorkOrder(m WorkOrder) (id int64, err error) { o := orm.NewOrm() id, err = o.Insert(&m) if err != nil { logs.Error("Add_WorkOrder", err) } return id, err } // 修改 func Update_WorkOrder(r WorkOrder, cols ...string) bool { o := orm.NewOrm() num, err := o.Update(&r, cols...) if err != nil { logs.Error("Update_WorkOrder", err) return false } fmt.Println("Number of records updated in database:", num) return true } // 删除 func Delete_WorkOrder_ById(r WorkOrder) (err error) { r.T_State = 0 o := orm.NewOrm() _, err = o.Update(&r, "T_State") if err != nil { logs.Error("Update_WorkOrder", err) } return } // 删除 func Delete_WorkOrder(m WorkOrder) (err error) { o := orm.NewOrm() if num, err := o.Delete(&m); err == nil { fmt.Println("Number of records deleted in database:", num) } return } // 获取列表 func Read_WorkOrder_List(T_handle int, page int, page_z int, CompanyIds []int) (t []WorkOrder_, cnt int64) { o := orm.NewOrm() // 也可以直接使用 Model 结构体作为表名 qs := o.QueryTable(new(WorkOrder)) var offset int64 if page_z == 0 { page_z = conf.Page_size } var r []WorkOrder if page <= 1 { offset = 0 } else { offset = int64((page - 1) * page_z) } cond := orm.NewCondition() cond1 := cond.And("T_State__gt", 0) if T_handle == 1 { cond1 = cond1.And("T_State", 1) } if T_handle == 2 { cond1 = cond1.And("T_State", 2) } if len(CompanyIds) > 0 { cond1 = cond1.And("T_pid__in", CompanyIds) } _, err := qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&r) if err != nil { logs.Error("Read_WorkOrder_List", err) return } cnt, err = qs.SetCond((*orm2.Condition)(cond1)).Count() if err != nil { logs.Error("Read_WorkOrder_List Count", err) return } for _, v := range r { t = append(t, WorkOrderToWorkOrder_(v)) } return t, cnt } // 获取用户列表 func Read_UserWorkOrder_List(admin_r Account.User, T_pid int, page int, page_z int) (t []WorkOrder_R, cnt int64) { o := orm.NewOrm() // 也可以直接使用 Model 结构体作为表名 qs := o.QueryTable(new(WorkOrder)) var r []WorkOrder var offset int64 if page_z == 0 { page_z = conf.Page_size } if page <= 1 { offset = 0 } else { offset = int64((page - 1) * page_z) } cond := orm.NewCondition() cond1 := cond.And("T_State__gt", 0) if admin_r.T_pid > 0 { // 用户进入 cond1 = cond1.And("T_uuid", admin_r.T_uuid) } else { //管理员进入 cond1 = cond1.And("T_pid", T_pid) } _, err := qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&r) if err != nil { logs.Error("Read_WorkOrder_List", err) return } cnt, err = qs.SetCond((*orm2.Condition)(cond1)).Count() if err != nil { logs.Error("Read_UserWorkOrder_List Count", err) return } for _, v := range r { t = append(t, WorkOrderToWorkOrder_R(v)) } return t, cnt } func Read_WorkOrder_ALL_T_State_Count(T_pids string, T_handle int, Time_start string) (cnt int64) { o := orm.NewOrm() qs := o.QueryTable(new(WorkOrder)) cond := orm.NewCondition() cond1 := cond.And("T_State__gt", 0) if len(Time_start) > 0 { cond1 = cond1.And("CreateTime__gte", Time_start) } if len(T_pids) > 0 && T_pids != "*" { list := lib.SplitStringIds(T_pids, "P") cond1 = cond1.And("T_pid__in", list) } //不填或0:所有 1:已处理 2:未处理 if T_handle == 1 { cond1 = cond1.And("T_State", 1) } if T_handle == 2 { cond1 = cond1.And("T_State", 2) } cnt, err := qs.SetCond((*orm2.Condition)(cond1)).Count() if err != nil { logs.Error("Read_WorkOrder_ALL_T_State_Count Count", err) return } return cnt }