AllotTask.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. package AllotTask
  2. import (
  3. "ColdVerify_server/conf"
  4. "ColdVerify_server/lib"
  5. "ColdVerify_server/logs"
  6. "encoding/json"
  7. "fmt"
  8. "time"
  9. "github.com/astaxie/beego/cache"
  10. "github.com/beego/beego/v2/adapter/orm"
  11. orm2 "github.com/beego/beego/v2/client/orm"
  12. _ "github.com/go-sql-driver/mysql"
  13. )
  14. var (
  15. AllotTaskStatusWaitReceive = 1 // 待接收
  16. AllotTaskStatusReceived = 2 // 已接收
  17. AllotTaskStatusRefused = 3 // 已拒绝
  18. AllotTaskStatusSubmit = 4 // 已提交
  19. AllotTaskStateMap = map[int]string{
  20. AllotTaskStatusWaitReceive: "待接收",
  21. AllotTaskStatusReceived: "已接收",
  22. AllotTaskStatusRefused: "已拒绝",
  23. AllotTaskStatusSubmit: "已提交",
  24. }
  25. )
  26. // 模版
  27. type AllotTask struct {
  28. Id int `orm:"column(ID);size(11);auto;pk"`
  29. T_Distributor_id string `orm:"size(256);null"` // 分销商id
  30. T_allot_task_id string `orm:"size(256);null"` // 分配任务ID
  31. T_uuid string `orm:"size(256);null"` // 公司 UUID
  32. T_name string `orm:"size(256);null"` // 标题
  33. T_approach_time string `orm:"size(256);null"` // 进场时间
  34. T_scheme string `orm:"size(256);null"` // 实施方案 负责人UUID
  35. T_collection string `orm:"size(256);null"` // 数据采集 负责人UUID
  36. T_reporting string `orm:"size(256);null"` // 报告编写 负责人UUID
  37. T_delivery string `orm:"size(256);null"` // 交付审核 负责人UUID
  38. T_project string `orm:"size(256);null"` // 项目 负责人UUID
  39. T_province string `orm:"size(256);null"` // 省
  40. T_city string `orm:"size(256);null"` // 市
  41. T_district string `orm:"size(256);null"` // 区
  42. T_province_code string `orm:"size(256);null"` // 省 code
  43. T_city_code string `orm:"size(256);null"` // 市 code
  44. T_district_code string `orm:"size(256);null"` // 区 code
  45. T_category string `orm:"size(256);null"` // 类别
  46. T_attachment string `orm:"type(text);null"` // 附件
  47. T_confirm_form string `orm:"type(text);null"` // 信息确认表
  48. T_submit_time string `orm:"type(text);null"` // 信息确认表提交时间
  49. T_status int `orm:"size(2);default(0)"` // 状态 1待接收 2已接收 3已拒绝 4已提交
  50. T_state int `orm:"size(2);default(1)"` // 0 删除 1 正常
  51. T_record string `orm:"type(text);null"` // 记录
  52. T_reason string `orm:"type(text);null"` // 拒绝原因
  53. T_allot_time time.Time `orm:"type(timestamp);null;"` // 派发时间
  54. CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间
  55. UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now 每次 model 保存时都会对时间自动更新
  56. }
  57. type AllotTask_ struct {
  58. Id int
  59. T_Distributor_id string // 分销商id
  60. T_allot_task_id string // 分配任务ID
  61. T_uuid string // 公司 UUID
  62. T_user_name string // 公司 名称
  63. T_name string // 标题
  64. T_approach_time string // 进场时间
  65. T_scheme string // 实施方案 负责人UUID
  66. T_collection string // 数据采集 负责人UUID
  67. T_reporting string // 报告编写 负责人UUID
  68. T_delivery string // 交付审核 负责人UUID
  69. T_project string // 项目 负责人UUID
  70. T_scheme_name string // 实施方案 负责人姓名
  71. T_collection_name string // 数据采集 负责人姓名
  72. T_reporting_name string // 报告编写 负责人姓名
  73. T_delivery_name string // 交付审核 负责人姓名
  74. T_project_name string // 项目 负责人姓名
  75. T_province string // 省
  76. T_city string // 市
  77. T_district string // 区
  78. T_province_code string // 省 code
  79. T_city_code string // 市 code
  80. T_district_code string // 区 code
  81. T_area []string // 省市区
  82. T_area_code []string // 省市区
  83. T_category string // 类别
  84. T_attachment []string // 合同附件
  85. T_confirm_form []string // 信息确认表图片
  86. T_submit_time string // 信息确认表提交时间
  87. T_status int // 状态 1待接收 2已接收 3已拒绝
  88. T_state int // 0 删除 1 正常
  89. T_record []AllotTaskRecord
  90. T_reason string
  91. T_allot_time string //指派时间
  92. CreateTime string //auto_now_add 第一次保存时才设置时间
  93. UpdateTime string //auto_now 每次 model 保存时都会对时间自动更新
  94. }
  95. type AllotTaskRecord struct {
  96. T_collection string
  97. T_collection_name string
  98. T_status int
  99. T_reason string
  100. T_time string
  101. }
  102. func AllotTaskToAllotTask_(T AllotTask, userMap, adminMap map[string]string) (T_ AllotTask_) {
  103. T_.Id = T.Id
  104. T_.T_Distributor_id = T.T_Distributor_id
  105. T_.T_allot_task_id = T.T_allot_task_id
  106. T_.T_uuid = T.T_uuid
  107. T_.T_user_name = userMap[T.T_uuid]
  108. T_.T_name = T.T_name
  109. T_.T_approach_time = T.T_approach_time
  110. T_.T_scheme = T.T_scheme
  111. T_.T_collection = T.T_collection
  112. T_.T_reporting = T.T_reporting
  113. T_.T_delivery = T.T_delivery
  114. T_.T_project = T.T_project
  115. T_.T_scheme_name = adminMap[T.T_scheme]
  116. T_.T_collection_name = adminMap[T.T_collection]
  117. T_.T_reporting_name = adminMap[T.T_reporting]
  118. T_.T_delivery_name = adminMap[T.T_delivery]
  119. T_.T_project_name = adminMap[T.T_project]
  120. T_.T_province = T.T_province
  121. T_.T_city = T.T_city
  122. T_.T_district = T.T_district
  123. T_.T_area = []string{T.T_province, T.T_city, T.T_district}
  124. T_.T_province_code = T.T_province_code
  125. T_.T_city_code = T.T_city_code
  126. T_.T_district_code = T.T_district_code
  127. T_.T_area_code = []string{T.T_province_code, T.T_city_code, T.T_district_code}
  128. T_.T_category = T.T_category
  129. T_.T_reason = T.T_reason
  130. T_.T_state = T.T_state
  131. T_.T_status = T.T_status
  132. T_.T_attachment = lib.SplitStringSeparator(T.T_attachment, "|")
  133. T_.T_confirm_form = lib.SplitStringSeparator(T.T_confirm_form, "|")
  134. T_.T_submit_time = T.T_submit_time
  135. json.Unmarshal([]byte(T.T_record), &T_.T_record)
  136. for i, record := range T_.T_record {
  137. T_.T_record[i].T_collection_name = adminMap[record.T_collection]
  138. }
  139. if !T.T_allot_time.IsZero() {
  140. T_.T_allot_time = T.T_allot_time.Format("2006-01-02 15:04")
  141. }
  142. if !T.CreateTime.IsZero() {
  143. T_.CreateTime = T.CreateTime.Format("2006-01-02 15:04:05")
  144. }
  145. if !T.UpdateTime.IsZero() {
  146. T_.UpdateTime = T.UpdateTime.Format("2006-01-02 15:04:05")
  147. }
  148. return T_
  149. }
  150. var redisCache_AllotTask cache.Cache
  151. func (t *AllotTask) TableName() string {
  152. return "allot_task" // 数据库名称 // ************** 替换 FormulaList **************
  153. }
  154. func init() {
  155. //注册模型
  156. orm.RegisterModel(new(AllotTask))
  157. config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
  158. "redis_"+"AllotTask", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
  159. logs.Println(config)
  160. var err error
  161. redisCache_AllotTask, err = cache.NewCache("redis", config)
  162. if err != nil || redisCache_AllotTask == nil {
  163. errMsg := "failed to init redis"
  164. logs.Println(errMsg, err)
  165. }
  166. }
  167. func Redis_AllotTask_Set(key string, r AllotTask) (err error) {
  168. //json序列化
  169. str, err := json.Marshal(r)
  170. if err != nil {
  171. logs.Error(lib.FuncName(), err)
  172. return
  173. }
  174. err = redisCache_AllotTask.Put(key, str, 24*time.Hour)
  175. if err != nil {
  176. logs.Println("set key:", key, ",value:", str, err)
  177. }
  178. return
  179. }
  180. func Redis_AllotTask_Get(key string) (r AllotTask, is bool) {
  181. if redisCache_AllotTask.IsExist(key) {
  182. logs.Println("找到key:", key)
  183. v := redisCache_AllotTask.Get(key)
  184. json.Unmarshal(v.([]byte), &r)
  185. return r, true
  186. }
  187. logs.Println("没有 找到key:", key)
  188. return AllotTask{}, false
  189. }
  190. func Redis_AllotTask_DelK(key string) (err error) {
  191. err = redisCache_AllotTask.Delete(key)
  192. return
  193. }
  194. // 添加
  195. func Add_AllotTask(r AllotTask) (string, bool) {
  196. o := orm.NewOrm()
  197. // 生成编号
  198. rand_x := 0
  199. for true {
  200. r.T_allot_task_id = lib.GetRandstring(12, "abcdefghijklmnopqrstuvwxyz0123456789", int64(rand_x)) // 1,336,336
  201. err := o.Read(&r, "T_allot_task_id") // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
  202. if err != nil {
  203. break
  204. }
  205. rand_x += 1
  206. }
  207. _, err := o.Insert(&r)
  208. if err != nil {
  209. logs.Error(lib.FuncName(), err)
  210. return "", false
  211. }
  212. Redis_AllotTask_Set(r.T_allot_task_id, r)
  213. return r.T_allot_task_id, true
  214. }
  215. func Update_AllotTask(m AllotTask, cols ...string) bool {
  216. o := orm.NewOrm()
  217. if num, err := o.Update(&m, cols...); err == nil {
  218. logs.Println("Number of records updated in database:", num)
  219. Redis_AllotTask_Set(m.T_allot_task_id, m)
  220. return true
  221. } else {
  222. logs.Error(lib.FuncName(), err)
  223. }
  224. return false
  225. }
  226. // 删除
  227. func Delete_AllotTask(v AllotTask) bool {
  228. o := orm.NewOrm()
  229. v.T_state = 0
  230. if num, err := o.Update(&v, "T_state"); err == nil {
  231. logs.Println("Number of records updated in database:", num)
  232. } else {
  233. logs.Error(lib.FuncName(), err)
  234. return false
  235. }
  236. Redis_AllotTask_DelK(v.T_allot_task_id)
  237. return true
  238. }
  239. // 获取 By
  240. func Read_AllotTask(T_allot_task_id string) (r AllotTask, is bool) {
  241. if r, is = Redis_AllotTask_Get(T_allot_task_id); is == true {
  242. return r, true
  243. }
  244. o := orm.NewOrm()
  245. qs := o.QueryTable(new(AllotTask))
  246. //err := qs.Filter("T_allot_task_id", T_allot_task_id).Filter("T_State", 1).One(&r)
  247. err := qs.Filter("T_allot_task_id", T_allot_task_id).Filter("T_state", 1).One(&r)
  248. if err != nil {
  249. return r, false
  250. }
  251. Redis_AllotTask_Set(T_allot_task_id, r)
  252. return r, true
  253. }
  254. // 获取任务列表
  255. func Read_AllotTask_List(T_Distributor_id, T_admin, T_uuid, T_name, T_project, T_scheme, T_collection, T_reporting, T_delivery string, T_status int,
  256. T_company_list []string, userMap, adminMap map[string]string, page int, page_z int) ([]AllotTask_, int) {
  257. o := orm.NewOrm()
  258. qs := o.QueryTable(new(AllotTask))
  259. var r []AllotTask
  260. var offset int64
  261. if page <= 1 {
  262. offset = 0
  263. } else {
  264. offset = int64((page - 1) * page_z)
  265. }
  266. cond := orm.NewCondition()
  267. cond1 := cond.And("T_state", 1)
  268. if len(T_name) > 0 {
  269. cond1 = cond1.And("T_name__icontains", T_name)
  270. }
  271. if len(T_Distributor_id) > 0 {
  272. cond1 = cond1.And("T_Distributor_id", T_Distributor_id)
  273. }
  274. if len(T_uuid) > 0 {
  275. cond1 = cond1.And("T_uuid", T_uuid)
  276. }
  277. if len(T_admin) > 0 {
  278. cond1 = cond1.AndCond(cond.Or("T_scheme", T_admin).Or("T_collection", T_admin).
  279. Or("T_reporting", T_admin).Or("T_delivery", T_admin))
  280. }
  281. if T_status > 0 {
  282. cond1 = cond1.And("T_status", T_status)
  283. }
  284. if len(T_company_list) > 0 {
  285. cond1 = cond1.And("T_uuid__in", T_company_list)
  286. }
  287. if len(T_project) > 0 {
  288. cond1 = cond1.And("T_project", T_project)
  289. }
  290. if len(T_scheme) > 0 {
  291. cond1 = cond1.And("T_scheme", T_scheme)
  292. }
  293. if len(T_collection) > 0 {
  294. cond1 = cond1.And("T_collection", T_collection)
  295. }
  296. if len(T_reporting) > 0 {
  297. cond1 = cond1.And("T_reporting", T_reporting)
  298. }
  299. if len(T_delivery) > 0 {
  300. cond1 = cond1.And("T_delivery", T_delivery)
  301. }
  302. if page_z == 9999 {
  303. qs.SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&r)
  304. } else {
  305. qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&r)
  306. }
  307. cnt, _ := qs.SetCond((*orm2.Condition)(cond1)).Count()
  308. // 转换
  309. var AllotTaskList []AllotTask_
  310. for _, v := range r {
  311. AllotTaskList = append(AllotTaskList, AllotTaskToAllotTask_(v, userMap, adminMap))
  312. }
  313. return AllotTaskList, int(cnt)
  314. }
  315. // 获取超时未接收的任务列表 (超过24小时)
  316. func Read_AllotTask_Timeout_List() ([]AllotTask, error) {
  317. o := orm.NewOrm()
  318. qs := o.QueryTable(new(AllotTask))
  319. var r []AllotTask
  320. // 计算24小时前的时间
  321. timeout := time.Now().Add(-24 * time.Hour)
  322. // 查询CreateTime超过24小时且状态为待接收的任务
  323. cond := orm.NewCondition()
  324. cond1 := cond.And("T_state", 1). // 正常状态
  325. And("T_status", AllotTaskStatusWaitReceive). // 待接收状态
  326. And("T_allot_time__lt", timeout) // 创建时间早于24小时前
  327. _, err := qs.SetCond((*orm2.Condition)(cond1)).All(&r)
  328. if err != nil {
  329. logs.Error(lib.FuncName(), err)
  330. return nil, err
  331. }
  332. return r, nil
  333. }
  334. // 更新任务为已拒绝状态并添加拒绝记录
  335. func Update_AllotTask_To_Refused(task AllotTask, reason string) bool {
  336. o := orm.NewOrm()
  337. // 获取现有记录
  338. var existingRecords = make([]AllotTaskRecord, 0)
  339. if len(task.T_record) > 0 {
  340. json.Unmarshal([]byte(task.T_record), &existingRecords)
  341. }
  342. // 创建新的拒绝记录
  343. newRecord := AllotTaskRecord{
  344. T_collection: task.T_collection, // 系统自动拒绝
  345. T_status: AllotTaskStatusRefused,
  346. T_reason: reason,
  347. T_time: time.Now().Format("2006-01-02 15:04:05"),
  348. }
  349. existingRecords = append(existingRecords, newRecord)
  350. // 转换为JSON字符串
  351. updatedRecordsJSON, err := json.Marshal(existingRecords)
  352. if err != nil {
  353. logs.Error(lib.FuncName(), "JSON marshal error:", err)
  354. return false
  355. }
  356. // 更新任务状态和记录
  357. task.T_record = string(updatedRecordsJSON)
  358. task.T_status = AllotTaskStatusRefused
  359. task.T_reason = reason
  360. if num, err := o.Update(&task, "T_record", "T_status", "T_reason"); err == nil {
  361. logs.Println("Number of records updated in database:", num)
  362. // 更新Redis缓存
  363. Redis_AllotTask_Set(task.T_allot_task_id, task)
  364. return true
  365. } else {
  366. logs.Error(lib.FuncName(), err)
  367. }
  368. return false
  369. }
  370. // 获取项目负责人列表
  371. func Get_AllotTask_UserList(T_type string) []string {
  372. o := orm.NewOrm()
  373. var err error
  374. var pl_lists []string
  375. switch T_type {
  376. case "T_project":
  377. _, err = o.Raw("SELECT DISTINCT t_project FROM allot_task LIMIT 0,1000").QueryRows(&pl_lists)
  378. case "T_scheme":
  379. _, err = o.Raw("SELECT DISTINCT t_scheme FROM allot_task LIMIT 0,1000").QueryRows(&pl_lists)
  380. case "T_collection":
  381. _, err = o.Raw("SELECT DISTINCT t_collection FROM allot_task LIMIT 0,1000").QueryRows(&pl_lists)
  382. case "T_reporting":
  383. _, err = o.Raw("SELECT DISTINCT t_reporting FROM allot_task LIMIT 0,1000").QueryRows(&pl_lists)
  384. }
  385. if err != nil {
  386. logs.Error("获取项目负责人列表失败:", err)
  387. }
  388. return pl_lists
  389. }