WorkOrder.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. package WorkOrder
  2. import (
  3. "Cold_WorkOrder/conf"
  4. "Cold_WorkOrder/lib"
  5. "Cold_WorkOrder/logs"
  6. "Cold_WorkOrder/models/Account"
  7. "encoding/json"
  8. "fmt"
  9. "github.com/beego/beego/v2/adapter/orm"
  10. orm2 "github.com/beego/beego/v2/client/orm"
  11. _ "github.com/go-sql-driver/mysql"
  12. "time"
  13. )
  14. // 模板
  15. type WorkOrder struct {
  16. Id int `orm:"column(ID);size(11);auto;pk"`
  17. T_uuid string `orm:"size(50);null"` // 提交人uuid
  18. T_pid int `orm:"index;size(256);null"` // Account.Company 绑定公司
  19. T_title string `orm:"size(256);null"` // 标题
  20. T_text string `orm:"type(text);null"` // 详情
  21. T_deal_with string `orm:"type(text);null"` // 工单处理信息 处理信息,处理人,处理时间|处理信息,处理人,处理时间|
  22. T_callback string `orm:"type(text);null"` // 工单回访信息 回访信息,回访人,回访时间|回访信息,回访人,回访时间|
  23. T_State int `orm:"size(2);default(1)"` // 0 删除 1 已处理 2 待处理 3 已回访
  24. CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间
  25. UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now 每次 model 保存时都会对时间自动更新
  26. }
  27. type WorkOrder_R struct {
  28. Id int // ID
  29. T_name string // 用户名
  30. T_phone string // 电话
  31. T_Company_name string // 公司名
  32. T_title string // 标题
  33. T_text string // 详情
  34. T_State int // 0 删除 1 已处理 2 待处理 3 已回访
  35. T_deal_with []WorkOrderInfo // 处理信息
  36. T_callback []WorkOrderInfo // 回访信息
  37. CreateTime string // 创建时间
  38. UpdateTime string // 更新时间
  39. }
  40. type WorkOrder_ struct {
  41. Id int // ID
  42. T_uuid string // 用户uuid
  43. T_name string // 用户名
  44. T_phone string // 电话
  45. T_Company_name string // 公司名
  46. T_title string // 标题
  47. T_text string // 详情
  48. T_State int // 状态 0 删除 1 已处理 2 待处理 3 已回访
  49. T_deal_with []WorkOrderInfo // 处理信息
  50. T_callback []WorkOrderInfo // 回访信息
  51. CreateTime string // 创建时间
  52. UpdateTime string // 更新时间
  53. }
  54. func (t *WorkOrder) TableName() string {
  55. return "work_order" // 数据库名称 // ************** 替换 FormulaList **************
  56. }
  57. func init() {
  58. //注册模型
  59. orm.RegisterModel(new(WorkOrder))
  60. }
  61. type WorkOrderInfo struct {
  62. T_text string // 处理信息
  63. T_uuid string // 处理人
  64. T_name string // 处理人名称
  65. T_Company_name string // 处理人公司
  66. T_time string // 处理时间
  67. }
  68. func UpdateWorkOrderInfoToString(oldInfo, T_info, T_uuid string) string {
  69. var list []WorkOrderInfo
  70. _ = json.Unmarshal([]byte(oldInfo), &list)
  71. workOrderInfo := WorkOrderInfo{T_text: T_info, T_uuid: T_uuid, T_time: time.Now().Format("2006-01-02 15:04:05")}
  72. list = append(list, workOrderInfo)
  73. listJson, _ := json.Marshal(list)
  74. return string(listJson)
  75. }
  76. func StringToWorkOrderInfoList(info string) []WorkOrderInfo {
  77. var list []WorkOrderInfo
  78. _ = json.Unmarshal([]byte(info), &list)
  79. for i := 0; i < len(list); i++ {
  80. list[i].T_name = Account.Read_User_T_name_Get(list[i].T_uuid)
  81. pid := Account.Read_User_T_pid_Get(list[i].T_uuid)
  82. if pid == 0 {
  83. list[i].T_Company_name = "宝智达"
  84. } else {
  85. list[i].T_Company_name = Account.Read_Company_T_name_Get(pid)
  86. }
  87. }
  88. return list
  89. }
  90. func WorkOrderToWorkOrder_R(t WorkOrder) (r WorkOrder_R) {
  91. r.T_Company_name = Account.Read_Company_T_name_Get(t.T_pid)
  92. r.T_name = Account.Read_User_T_name_Get(t.T_uuid)
  93. r.T_phone = Account.Read_User_T_phone_Get(t.T_uuid)
  94. r.Id = t.Id
  95. r.T_title = t.T_title
  96. r.T_text = t.T_text
  97. r.T_State = t.T_State
  98. r.T_deal_with = StringToWorkOrderInfoList(t.T_deal_with)
  99. r.T_callback = StringToWorkOrderInfoList(t.T_callback)
  100. r.CreateTime = t.CreateTime.Format("2006-01-02 15:04:05")
  101. r.UpdateTime = t.UpdateTime.Format("2006-01-02 15:04:05")
  102. return
  103. }
  104. func WorkOrderToWorkOrder_(t WorkOrder) (r WorkOrder_) {
  105. r.T_Company_name = Account.Read_Company_T_name_Get(t.T_pid)
  106. r.T_name = Account.Read_User_T_name_Get(t.T_uuid)
  107. r.T_phone = Account.Read_User_T_phone_Get(t.T_uuid)
  108. r.Id = t.Id
  109. r.T_uuid = t.T_uuid
  110. r.T_title = t.T_title
  111. r.T_text = t.T_text
  112. r.T_State = t.T_State
  113. r.T_deal_with = StringToWorkOrderInfoList(t.T_deal_with)
  114. r.T_callback = StringToWorkOrderInfoList(t.T_callback)
  115. r.CreateTime = t.CreateTime.Format("2006-01-02 15:04:05")
  116. r.UpdateTime = t.UpdateTime.Format("2006-01-02 15:04:05")
  117. return
  118. }
  119. // ---------------- 特殊方法 -------------------
  120. // 获取 ById
  121. func Read_WorkOrder_ById(id int) (r WorkOrder, err error) {
  122. o := orm.NewOrm()
  123. r = WorkOrder{Id: id}
  124. err = o.Read(&r) // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
  125. if err != nil {
  126. logs.Error("Read_WorkOrder_ById", err)
  127. return r, err
  128. }
  129. if r.T_State == 0 {
  130. return r, orm.ErrNoRows
  131. }
  132. return r, nil
  133. }
  134. // 添加
  135. func Add_WorkOrder(m WorkOrder) (id int64, err error) {
  136. o := orm.NewOrm()
  137. id, err = o.Insert(&m)
  138. if err != nil {
  139. logs.Error("Add_WorkOrder", err)
  140. }
  141. return id, err
  142. }
  143. // 修改
  144. func Update_WorkOrder(r WorkOrder, cols ...string) bool {
  145. o := orm.NewOrm()
  146. num, err := o.Update(&r, cols...)
  147. if err != nil {
  148. logs.Error("Update_WorkOrder", err)
  149. return false
  150. }
  151. fmt.Println("Number of records updated in database:", num)
  152. return true
  153. }
  154. // 删除
  155. func Delete_WorkOrder_ById(r WorkOrder) (err error) {
  156. r.T_State = 0
  157. o := orm.NewOrm()
  158. _, err = o.Update(&r, "T_State")
  159. if err != nil {
  160. logs.Error("Update_WorkOrder", err)
  161. }
  162. return
  163. }
  164. // 删除
  165. func Delete_WorkOrder(m WorkOrder) (err error) {
  166. o := orm.NewOrm()
  167. if num, err := o.Delete(&m); err == nil {
  168. fmt.Println("Number of records deleted in database:", num)
  169. }
  170. return
  171. }
  172. // 获取列表
  173. func Read_WorkOrder_List(T_handle int, page int, page_z int, CompanyIds []int) (t []WorkOrder_, cnt int64) {
  174. o := orm.NewOrm()
  175. // 也可以直接使用 Model 结构体作为表名
  176. qs := o.QueryTable(new(WorkOrder))
  177. var offset int64
  178. if page_z == 0 {
  179. page_z = conf.Page_size
  180. }
  181. var r []WorkOrder
  182. if page <= 1 {
  183. offset = 0
  184. } else {
  185. offset = int64((page - 1) * page_z)
  186. }
  187. cond := orm.NewCondition()
  188. cond1 := cond.And("T_State__gt", 0)
  189. if T_handle == 1 {
  190. cond1 = cond1.And("T_State", 1)
  191. }
  192. if T_handle == 2 {
  193. cond1 = cond1.And("T_State", 2)
  194. }
  195. if len(CompanyIds) > 0 {
  196. cond1 = cond1.And("T_pid__in", CompanyIds)
  197. }
  198. _, err := qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&r)
  199. if err != nil {
  200. logs.Error("Read_WorkOrder_List", err)
  201. return
  202. }
  203. cnt, err = qs.SetCond((*orm2.Condition)(cond1)).Count()
  204. if err != nil {
  205. logs.Error("Read_WorkOrder_List Count", err)
  206. return
  207. }
  208. for _, v := range r {
  209. t = append(t, WorkOrderToWorkOrder_(v))
  210. }
  211. return t, cnt
  212. }
  213. // 获取用户列表
  214. func Read_UserWorkOrder_List(admin_r Account.User, T_pid int, page int, page_z int) (t []WorkOrder_R, cnt int64) {
  215. o := orm.NewOrm()
  216. // 也可以直接使用 Model 结构体作为表名
  217. qs := o.QueryTable(new(WorkOrder))
  218. var r []WorkOrder
  219. var offset int64
  220. if page_z == 0 {
  221. page_z = conf.Page_size
  222. }
  223. if page <= 1 {
  224. offset = 0
  225. } else {
  226. offset = int64((page - 1) * page_z)
  227. }
  228. cond := orm.NewCondition()
  229. cond1 := cond.And("T_State__gt", 0)
  230. if admin_r.T_pid > 0 {
  231. // 用户进入
  232. cond1 = cond1.And("T_uuid", admin_r.T_uuid)
  233. } else {
  234. //管理员进入
  235. cond1 = cond1.And("T_pid", T_pid)
  236. }
  237. _, err := qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&r)
  238. if err != nil {
  239. logs.Error("Read_WorkOrder_List", err)
  240. return
  241. }
  242. cnt, err = qs.SetCond((*orm2.Condition)(cond1)).Count()
  243. if err != nil {
  244. logs.Error("Read_UserWorkOrder_List Count", err)
  245. return
  246. }
  247. for _, v := range r {
  248. t = append(t, WorkOrderToWorkOrder_R(v))
  249. }
  250. return t, cnt
  251. }
  252. func Read_WorkOrder_ALL_T_State_Count(T_pids string, T_handle int, Time_start string) (cnt int64) {
  253. o := orm.NewOrm()
  254. qs := o.QueryTable(new(WorkOrder))
  255. cond := orm.NewCondition()
  256. cond1 := cond.And("T_State__gt", 0)
  257. if len(Time_start) > 0 {
  258. cond1 = cond1.And("CreateTime__gte", Time_start)
  259. }
  260. if len(T_pids) > 0 && T_pids != "*" {
  261. list := lib.SplitStringIds(T_pids, "P")
  262. cond1 = cond1.And("T_pid__in", list)
  263. }
  264. //不填或0:所有 1:已处理 2:未处理
  265. if T_handle == 1 {
  266. cond1 = cond1.And("T_State", 1)
  267. }
  268. if T_handle == 2 {
  269. cond1 = cond1.And("T_State", 2)
  270. }
  271. cnt, err := qs.SetCond((*orm2.Condition)(cond1)).Count()
  272. if err != nil {
  273. logs.Error("Read_WorkOrder_ALL_T_State_Count Count", err)
  274. return
  275. }
  276. return cnt
  277. }