data.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. package db
  2. import (
  3. "context"
  4. "fmt"
  5. "reflect"
  6. "strings"
  7. "time"
  8. "youngee_b_api/model/common_model"
  9. "youngee_b_api/model/gorm_model"
  10. "youngee_b_api/model/http_model"
  11. "youngee_b_api/pack"
  12. "youngee_b_api/util"
  13. "github.com/issue9/conv"
  14. "github.com/sirupsen/logrus"
  15. )
  16. // GetTaskDataList 查询上传数据的task list
  17. func GetTaskDataList(ctx context.Context, projectID string, pageSize, pageNum int64, conditions *common_model.TalentConditions) ([]*http_model.TaskDataInfo, int64, error) {
  18. db := GetReadDB(ctx)
  19. // 查询Task表信息
  20. db = db.Debug().Model(gorm_model.YoungeeTaskInfo{}).Where("task_status = 2")
  21. // 根据Project条件过滤
  22. conditionType := reflect.TypeOf(conditions).Elem()
  23. conditionValue := reflect.ValueOf(conditions).Elem()
  24. var platform_nickname string = ""
  25. for i := 0; i < conditionType.NumField(); i++ {
  26. field := conditionType.Field(i)
  27. tag := field.Tag.Get("condition")
  28. value := conditionValue.FieldByName(field.Name)
  29. if tag == "data_status" {
  30. fmt.Printf("Data %+v", value.Interface() == int64(0))
  31. if value.Interface() == int64(0) {
  32. db = db.Where("task_stage = 14")
  33. } else {
  34. db = db.Where("task_stage > 14 and task_stage <> 16")
  35. }
  36. continue
  37. } else if !util.IsBlank(value) {
  38. if tag == "platform_nickname" {
  39. platform_nickname = fmt.Sprintf("%v", value.Interface())
  40. continue
  41. } else if tag == "project_id" || tag == "strategy_id" {
  42. db = db.Where(fmt.Sprintf("%s = ?", tag), value.Interface())
  43. }
  44. }
  45. }
  46. var taskInfos []gorm_model.YoungeeTaskInfo
  47. db = db.Model(gorm_model.YoungeeTaskInfo{})
  48. // 查询总数
  49. var totalTask int64
  50. if err := db.Count(&totalTask).Error; err != nil {
  51. logrus.WithContext(ctx).Errorf("[GetTaskDataList] error query mysql total, err:%+v", err)
  52. return nil, 0, err
  53. }
  54. db.Order("task_id").Find(&taskInfos)
  55. // 查询任务id
  56. var taskIds []string
  57. taskMap := make(map[string]gorm_model.YoungeeTaskInfo)
  58. for _, taskInfo := range taskInfos {
  59. taskIds = append(taskIds, taskInfo.TaskID)
  60. taskMap[taskInfo.TaskID] = taskInfo
  61. }
  62. // 查询链接
  63. db1 := GetReadDB(ctx)
  64. // db1 = db1.Debug().Model(gorm_model.YounggeeDataInfo{})
  65. var LinkInfos []gorm_model.YounggeeLinkInfo
  66. db1 = db1.Model(gorm_model.YounggeeLinkInfo{}).Where("task_id IN ? AND is_submit= 1 AND is_ok = 1", taskIds)
  67. err := db1.Find(&LinkInfos).Error
  68. if err != nil {
  69. logrus.WithContext(ctx).Errorf("[GetProjectTalentList] error query mysql total, err:%+v", err)
  70. return nil, 0, err
  71. }
  72. LinkMap := make(map[string]gorm_model.YounggeeLinkInfo)
  73. for _, LinkInfo := range LinkInfos {
  74. LinkMap[LinkInfo.TaskID] = LinkInfo
  75. }
  76. db2 := GetReadDB(ctx)
  77. // db2 = db1.Debug().Model(gorm_model.YounggeeDataInfo{})
  78. var DataInfos []gorm_model.YounggeeDataInfo
  79. db2 = db2.Model(gorm_model.YounggeeDataInfo{}).Where("task_id IN ? AND is_submit = 1", taskIds)
  80. fmt.Printf("conditions %+v", conditions)
  81. if conditions.DataStatus == int64(0) {
  82. db2 = db2.Where("is_review = 0")
  83. } else {
  84. db2 = db2.Where("is_ok = 1")
  85. }
  86. err = db2.Find(&DataInfos).Error
  87. if err != nil {
  88. logrus.WithContext(ctx).Errorf("[GetProjectTalentList] error query mysql total, err:%+v", err)
  89. return nil, 0, err
  90. }
  91. DataMap := make(map[string]gorm_model.YounggeeDataInfo)
  92. for _, DataInfo := range DataInfos {
  93. DataMap[DataInfo.TaskID] = DataInfo
  94. }
  95. // 查询总数
  96. var totalData int64
  97. if err := db2.Count(&totalData).Error; err != nil {
  98. logrus.WithContext(ctx).Errorf("[GetProjectTalentList] error query mysql total, err:%+v", err)
  99. return nil, 0, err
  100. }
  101. var misNum int64
  102. if totalData > totalTask {
  103. misNum = totalData - totalTask
  104. } else {
  105. misNum = totalTask - totalData
  106. }
  107. logrus.Println("totalData,totalTalent,misNum:", totalData, totalTask, misNum)
  108. // 查询该页数据
  109. limit := pageSize + misNum
  110. offset := pageSize * pageNum // assert pageNum start with 0
  111. err = db.Order("task_id").Limit(int(limit)).Offset(int(offset)).Error
  112. if err != nil {
  113. logrus.WithContext(ctx).Errorf("[GetTaskDataList] error query mysql total, err:%+v", err)
  114. return nil, 0, err
  115. }
  116. var TaskDatas []*http_model.TaskData
  117. var taskDatas []*http_model.TaskDataInfo
  118. var newTaskDatas []*http_model.TaskDataInfo
  119. for _, taskId := range taskIds {
  120. TaskData := new(http_model.TaskData)
  121. TaskData.Talent = taskMap[taskId]
  122. TaskData.Data = DataMap[taskId]
  123. TaskData.Link = LinkMap[taskId]
  124. TaskDatas = append(TaskDatas, TaskData)
  125. }
  126. taskDatas = pack.TaskDataToTaskInfo(TaskDatas)
  127. for _, v := range taskDatas {
  128. if platform_nickname == "" {
  129. newTaskDatas = append(newTaskDatas, v)
  130. } else if strings.Contains(v.PlatformNickname, platform_nickname) {
  131. newTaskDatas = append(newTaskDatas, v)
  132. } else if strings.Contains(conv.MustString(v.TaskID), platform_nickname) {
  133. newTaskDatas = append(newTaskDatas, v)
  134. } else {
  135. totalTask--
  136. }
  137. }
  138. // return fulltaskData, total, nil
  139. return newTaskDatas, totalTask, nil
  140. }
  141. // DataOpinion 提交意见
  142. func DataOpinion(ctx context.Context, TaskID string, ReviseOpinion string, operatorid string, operatetype int, projectid string) error {
  143. db := GetReadDB(ctx)
  144. err := db.Model(gorm_model.YounggeeDataInfo{}).Where("task_id = ? and is_review = 0", TaskID).Updates(map[string]interface{}{"revise_opinion": ReviseOpinion, "reject_at": time.Now(), "is_review": 1, "b_operator": operatorid, "b_operator_type": operatetype}).Error
  145. if err != nil {
  146. logrus.WithContext(ctx).Errorf("[Data db] Update YounggeeDataInfo error,err:%+v", err)
  147. return err
  148. }
  149. var project gorm_model.ProjectInfo
  150. err = db.Model(gorm_model.ProjectInfo{}).Where("project_id = ?", projectid).Find(&project).Error
  151. if err != nil {
  152. logrus.WithContext(ctx).Errorf("[Sketch db] Find ProjectInfo error,err:%+v", err)
  153. return err
  154. }
  155. var data_default gorm_model.InfoAutoTask
  156. err = db.Model(gorm_model.InfoAutoTask{}).Where("auto_task_id = ?", project.AutoTaskID).Find(&data_default).Error
  157. if err != nil {
  158. return err
  159. }
  160. duration := time.Duration(data_default.CaseCloseDefault) * time.Hour
  161. err = db.Model(gorm_model.YoungeeTaskInfo{}).Where("task_id = ?", TaskID).Updates(gorm_model.YoungeeTaskInfo{DataStatus: 3, CurBreakAt: time.Now().Add(duration)}).Error
  162. if err != nil {
  163. logrus.WithContext(ctx).Errorf("[Data db] Update YoungeeTaskInfo error,err:%+v", err)
  164. return err
  165. }
  166. err = db.Model(gorm_model.YoungeeTaskInfo{}).Where("task_id = ?", TaskID).Updates(gorm_model.YoungeeTaskInfo{TaskStage: 13}).Error
  167. if err != nil {
  168. logrus.WithContext(ctx).Errorf("[Data db] Update YoungeeTaskInfo error,err:%+v", err)
  169. return err
  170. }
  171. return nil
  172. }
  173. // DataOpinion 提交意见
  174. func LocalDataOpinion(ctx context.Context, TaskID string, ReviseOpinion string, operatorid string, operatetype int, projectid string) error {
  175. db := GetReadDB(ctx)
  176. err := db.Model(gorm_model.YounggeeDataInfo{}).Where("task_id = ? and is_review = 0", TaskID).Updates(map[string]interface{}{"revise_opinion": ReviseOpinion, "reject_at": time.Now(), "is_review": 1, "b_operator": operatorid, "b_operator_type": operatetype}).Error
  177. if err != nil {
  178. logrus.WithContext(ctx).Errorf("[Data db] Update YounggeeDataInfo error,err:%+v", err)
  179. return err
  180. }
  181. var project gorm_model.YounggeeLocalLifeInfo
  182. err = db.Model(gorm_model.YounggeeLocalLifeInfo{}).Where("local_id = ?", projectid).Find(&project).Error
  183. if err != nil {
  184. logrus.WithContext(ctx).Errorf("[Sketch db] Find ProjectInfo error,err:%+v", err)
  185. return err
  186. }
  187. var data_default gorm_model.InfoAutoTask
  188. err = db.Model(gorm_model.InfoAutoTask{}).Where("auto_task_id = ?", project.AutoTaskId).Find(&data_default).Error
  189. if err != nil {
  190. return err
  191. }
  192. duration := time.Duration(data_default.CaseCloseDefault) * time.Hour
  193. err = db.Model(gorm_model.YoungeeLocalTaskInfo{}).Where("task_id = ?", TaskID).Updates(gorm_model.YoungeeLocalTaskInfo{DataStatus: 3, CurBreakAt: time.Now().Add(duration)}).Error
  194. if err != nil {
  195. logrus.WithContext(ctx).Errorf("[Data db] Update YoungeeLocalTaskInfo error,err:%+v", err)
  196. return err
  197. }
  198. err = db.Model(gorm_model.YoungeeLocalTaskInfo{}).Where("task_id = ?", TaskID).Updates(gorm_model.YoungeeLocalTaskInfo{TaskStage: 13}).Error
  199. if err != nil {
  200. logrus.WithContext(ctx).Errorf("[Data db] Update YoungeeLocalTaskInfo error,err:%+v", err)
  201. return err
  202. }
  203. return nil
  204. }
  205. // AcceptData 同意数据-结案
  206. func AcceptData(ctx context.Context, TaskIDs []string, operatorid string, operatetype int) error {
  207. db := GetReadDB(ctx)
  208. err := db.Model(gorm_model.YounggeeDataInfo{}).Where("task_id in ? and is_review = 0", TaskIDs).Updates(map[string]interface{}{"is_ok": 1, "is_review": 1, "agree_at": time.Now(), "b_operator": operatorid, "b_operator_type": operatetype}).Error
  209. if err != nil {
  210. logrus.WithContext(ctx).Errorf("[Data db] Update YounggeeDataInfo error,err:%+v", err)
  211. return err
  212. }
  213. return nil
  214. }
  215. // RejectData 拒绝数据-结案
  216. func RejectData(ctx context.Context, TaskIDs []string, operatorid string, operatetype int, projectid string) error {
  217. db := GetReadDB(ctx)
  218. err := db.Model(gorm_model.YounggeeDataInfo{}).Where("task_id in ? and is_review = 0", TaskIDs).Updates(map[string]interface{}{"reject_at": time.Now(), "is_review": 1, "b_operator": operatorid, "b_operator_type": operatetype}).Error
  219. if err != nil {
  220. logrus.WithContext(ctx).Errorf("[Data db] Update YounggeeDataInfo error,err:%+v", err)
  221. return err
  222. }
  223. var project gorm_model.ProjectInfo
  224. err = db.Model(gorm_model.ProjectInfo{}).Where("project_id = ?", projectid).Find(&project).Error
  225. if err != nil {
  226. logrus.WithContext(ctx).Errorf("[Sketch db] Find ProjectInfo error,err:%+v", err)
  227. return err
  228. }
  229. var data_default gorm_model.InfoAutoTask
  230. err = db.Model(gorm_model.InfoAutoTask{}).Where("auto_task_id = ?", project.AutoTaskID).Find(&data_default).Error
  231. if err != nil {
  232. return err
  233. }
  234. duration := time.Duration(data_default.CaseCloseDefault) * time.Hour
  235. err = db.Model(gorm_model.YoungeeTaskInfo{}).Where("task_id IN ?", TaskIDs).Updates(gorm_model.YoungeeTaskInfo{DataStatus: 3, CurBreakAt: time.Now().Add(duration)}).Error
  236. if err != nil {
  237. logrus.WithContext(ctx).Errorf("[Data db] Update YoungeeTaskInfo error,err:%+v", err)
  238. return err
  239. }
  240. err = db.Model(gorm_model.YoungeeTaskInfo{}).Where("task_id IN ?", TaskIDs).Updates(gorm_model.YoungeeTaskInfo{TaskStage: 13}).Error
  241. if err != nil {
  242. logrus.WithContext(ctx).Errorf("[Data db] Update YoungeeTaskInfo error,err:%+v", err)
  243. return err
  244. }
  245. return nil
  246. }
  247. // RejectData 拒绝数据-结案
  248. func LocalRejectData(ctx context.Context, TaskIDs []string, operatorid string, operatetype int, projectid string) error {
  249. db := GetReadDB(ctx)
  250. err := db.Model(gorm_model.YounggeeDataInfo{}).Where("task_id in ? and is_review = 0", TaskIDs).Updates(map[string]interface{}{"reject_at": time.Now(), "is_review": 1, "b_operator": operatorid, "b_operator_type": operatetype}).Error
  251. if err != nil {
  252. logrus.WithContext(ctx).Errorf("[Data db] Update YounggeeDataInfo error,err:%+v", err)
  253. return err
  254. }
  255. var project gorm_model.YounggeeLocalLifeInfo
  256. err = db.Model(gorm_model.YounggeeLocalLifeInfo{}).Where("local_id = ?", projectid).Find(&project).Error
  257. if err != nil {
  258. logrus.WithContext(ctx).Errorf("[Sketch db] Find ProjectInfo error,err:%+v", err)
  259. return err
  260. }
  261. var data_default gorm_model.InfoAutoTask
  262. err = db.Model(gorm_model.InfoAutoTask{}).Where("auto_task_id = ?", project.AutoTaskId).Find(&data_default).Error
  263. if err != nil {
  264. return err
  265. }
  266. duration := time.Duration(data_default.CaseCloseDefault) * time.Hour
  267. err = db.Model(gorm_model.YoungeeLocalTaskInfo{}).Where("task_id IN ?", TaskIDs).Updates(gorm_model.YoungeeLocalTaskInfo{DataStatus: 3, CurBreakAt: time.Now().Add(duration)}).Error
  268. if err != nil {
  269. logrus.WithContext(ctx).Errorf("[Data db] Update YoungeeLocalTaskInfo error,err:%+v", err)
  270. return err
  271. }
  272. err = db.Model(gorm_model.YoungeeLocalTaskInfo{}).Where("task_id IN ?", TaskIDs).Updates(gorm_model.YoungeeLocalTaskInfo{TaskStage: 13}).Error
  273. if err != nil {
  274. logrus.WithContext(ctx).Errorf("[Data db] Update YoungeeLocalTaskInfo error,err:%+v", err)
  275. return err
  276. }
  277. return nil
  278. }
  279. // GetSpecialTaskDataList 专项任务-查询上传数据的task list
  280. func GetSpecialTaskDataList(ctx context.Context, projectID string, pageSize, pageNum int64, conditions *common_model.TalentConditions) ([]*http_model.SpecialTaskDataInfo, int64, error) {
  281. db := GetReadDB(ctx)
  282. // 查询Task表信息
  283. db = db.Debug().Model(gorm_model.YoungeeTaskInfo{}).Where("task_status = 2")
  284. // 根据Project条件过滤
  285. conditionType := reflect.TypeOf(conditions).Elem()
  286. conditionValue := reflect.ValueOf(conditions).Elem()
  287. var platform_nickname string = ""
  288. for i := 0; i < conditionType.NumField(); i++ {
  289. field := conditionType.Field(i)
  290. tag := field.Tag.Get("condition")
  291. value := conditionValue.FieldByName(field.Name)
  292. if tag == "data_status" {
  293. fmt.Printf("Data %+v", value.Interface() == int64(0))
  294. if value.Interface() == int64(0) {
  295. db = db.Where("task_stage = 14")
  296. } else {
  297. db = db.Where("task_stage > 14 and task_stage <> 16")
  298. }
  299. continue
  300. } else if !util.IsBlank(value) {
  301. if tag == "platform_nickname" {
  302. platform_nickname = fmt.Sprintf("%v", value.Interface())
  303. continue
  304. } else if tag == "project_id" {
  305. db = db.Where(fmt.Sprintf("%s = ?", tag), value.Interface())
  306. }
  307. }
  308. }
  309. var taskInfos []gorm_model.YoungeeTaskInfo
  310. db = db.Model(gorm_model.YoungeeTaskInfo{})
  311. // 查询总数
  312. var totalTask int64
  313. if err := db.Count(&totalTask).Error; err != nil {
  314. logrus.WithContext(ctx).Errorf("[GetTaskDataList] error query mysql total, err:%+v", err)
  315. return nil, 0, err
  316. }
  317. db.Order("task_id").Find(&taskInfos)
  318. // 查询任务id
  319. var taskIds []string
  320. taskMap := make(map[string]gorm_model.YoungeeTaskInfo)
  321. for _, taskInfo := range taskInfos {
  322. taskIds = append(taskIds, taskInfo.TaskID)
  323. taskMap[taskInfo.TaskID] = taskInfo
  324. }
  325. // 查询链接
  326. db1 := GetReadDB(ctx)
  327. // db1 = db1.Debug().Model(gorm_model.YounggeeDataInfo{})
  328. var LinkInfos []gorm_model.YounggeeLinkInfo
  329. db1 = db1.Model(gorm_model.YounggeeLinkInfo{}).Where("task_id IN ? AND is_submit= 1 AND is_ok = 1", taskIds)
  330. err := db1.Find(&LinkInfos).Error
  331. if err != nil {
  332. logrus.WithContext(ctx).Errorf("[GetProjectTalentList] error query mysql total, err:%+v", err)
  333. return nil, 0, err
  334. }
  335. LinkMap := make(map[string]gorm_model.YounggeeLinkInfo)
  336. for _, LinkInfo := range LinkInfos {
  337. LinkMap[LinkInfo.TaskID] = LinkInfo
  338. }
  339. db2 := GetReadDB(ctx)
  340. // db2 = db1.Debug().Model(gorm_model.YounggeeDataInfo{})
  341. var DataInfos []gorm_model.YounggeeDataInfo
  342. db2 = db2.Model(gorm_model.YounggeeDataInfo{}).Where("task_id IN ? AND is_submit = 1", taskIds)
  343. fmt.Printf("conditions %+v", conditions)
  344. if conditions.DataStatus == int64(0) {
  345. db2 = db2.Where("is_review = 0")
  346. } else {
  347. db2 = db2.Where("is_ok = 1")
  348. }
  349. err = db2.Find(&DataInfos).Error
  350. if err != nil {
  351. logrus.WithContext(ctx).Errorf("[GetProjectTalentList] error query mysql total, err:%+v", err)
  352. return nil, 0, err
  353. }
  354. DataMap := make(map[string]gorm_model.YounggeeDataInfo)
  355. for _, DataInfo := range DataInfos {
  356. DataMap[DataInfo.TaskID] = DataInfo
  357. }
  358. // 查询总数
  359. var totalData int64
  360. if err := db2.Count(&totalData).Error; err != nil {
  361. logrus.WithContext(ctx).Errorf("[GetProjectTalentList] error query mysql total, err:%+v", err)
  362. return nil, 0, err
  363. }
  364. var misNum int64
  365. if totalData > totalTask {
  366. misNum = totalData - totalTask
  367. } else {
  368. misNum = totalTask - totalData
  369. }
  370. logrus.Println("totalData,totalTalent,misNum:", totalData, totalTask, misNum)
  371. // 查询该页数据
  372. limit := pageSize + misNum
  373. offset := pageSize * pageNum // assert pageNum start with 0
  374. err = db.Order("task_id").Limit(int(limit)).Offset(int(offset)).Error
  375. if err != nil {
  376. logrus.WithContext(ctx).Errorf("[GetTaskDataList] error query mysql total, err:%+v", err)
  377. return nil, 0, err
  378. }
  379. var TaskDatas []*http_model.SpecialTaskData
  380. var taskDatas []*http_model.SpecialTaskDataInfo
  381. var newTaskDatas []*http_model.SpecialTaskDataInfo
  382. for _, taskId := range taskIds {
  383. TaskData := new(http_model.SpecialTaskData)
  384. TaskData.Talent = taskMap[taskId]
  385. TaskData.Data = DataMap[taskId]
  386. TaskData.Link = LinkMap[taskId]
  387. TaskDatas = append(TaskDatas, TaskData)
  388. }
  389. taskDatas = pack.SpecialTaskDataToTaskInfo(TaskDatas)
  390. for _, v := range taskDatas {
  391. if platform_nickname == "" {
  392. newTaskDatas = append(newTaskDatas, v)
  393. } else if strings.Contains(v.PlatformNickname, platform_nickname) {
  394. newTaskDatas = append(newTaskDatas, v)
  395. } else if strings.Contains(conv.MustString(v.TaskID), platform_nickname) {
  396. newTaskDatas = append(newTaskDatas, v)
  397. } else {
  398. totalTask--
  399. }
  400. }
  401. // return fulltaskData, total, nil
  402. return newTaskDatas, totalTask, nil
  403. }