logistics.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. //新增
  17. func CreateLogistics(ctx context.Context, logistics gorm_model.YoungeeTaskLogistics, RecruitStrategyID int64) (*int64, error) {
  18. db := GetReadDB(ctx)
  19. err := db.Create(&logistics).Error
  20. if err != nil {
  21. logrus.WithContext(ctx).Errorf("[logistics db] call CreateLogistics error,err:%+v", err)
  22. return nil, err
  23. }
  24. return &logistics.LogisticsID, nil
  25. }
  26. //修改接口
  27. func UpdateLogistics(ctx context.Context, logistics gorm_model.YoungeeTaskLogistics) (*int64, error) {
  28. db := GetReadDB(ctx)
  29. err := db.Model(&logistics).Where("task_id = ?", logistics.TaskID).Updates(logistics).Error
  30. if err != nil {
  31. logrus.WithContext(ctx).Errorf("[logistics db] call UpdateLogistics error,err:%+v", err)
  32. return nil, err
  33. }
  34. return &logistics.LogisticsID, nil
  35. }
  36. // 更改签收时间
  37. func SignForReceipt(ctx context.Context, taskID int64) error {
  38. db := GetReadDB(ctx)
  39. time := time.Now()
  40. err := db.Model(gorm_model.YoungeeTaskLogistics{}).Where("task_id = ?", taskID).Update("signed_time", time).Error
  41. if err != nil {
  42. logrus.WithContext(ctx).Errorf("[logistics db] call UpdateLogistics error,err:%+v", err)
  43. return err
  44. }
  45. return nil
  46. }
  47. // 查询包含物流信息的task list
  48. func GetTaskLogisticsList(ctx context.Context, projectID string, pageSize, pageNum int64, conditions *common_model.TalentConditions) ([]*http_model.TaskLogisticsInfo, int64, error) {
  49. db := GetReadDB(ctx)
  50. // 查询Task表信息
  51. db = db.Debug().Model(gorm_model.YoungeeTaskInfo{})
  52. // 根据Project条件过滤
  53. conditionType := reflect.TypeOf(conditions).Elem()
  54. conditionValue := reflect.ValueOf(conditions).Elem()
  55. var platform_nickname string = ""
  56. for i := 0; i < conditionType.NumField(); i++ {
  57. field := conditionType.Field(i)
  58. tag := field.Tag.Get("condition")
  59. value := conditionValue.FieldByName(field.Name)
  60. if !util.IsBlank(value) {
  61. if tag == "platform_nickname" {
  62. platform_nickname = fmt.Sprintf("%v", value.Interface())
  63. continue
  64. } else {
  65. db = db.Where(fmt.Sprintf("%s = ?", tag), value.Interface())
  66. }
  67. }
  68. }
  69. var taskInfos []gorm_model.YoungeeTaskInfo
  70. db = db.Model(gorm_model.YoungeeTaskInfo{})
  71. // 查询总数
  72. var totalTask int64
  73. if err := db.Count(&totalTask).Error; err != nil {
  74. logrus.WithContext(ctx).Errorf("[GetProjectTaskList] error query mysql total, err:%+v", err)
  75. return nil, 0, err
  76. }
  77. db.Order("task_id").Find(&taskInfos)
  78. // 查询任务id
  79. var taskIds []int
  80. taskMap := make(map[int]gorm_model.YoungeeTaskInfo)
  81. for _, taskInfo := range taskInfos {
  82. taskIds = append(taskIds, taskInfo.TaskID)
  83. taskMap[taskInfo.TaskID] = taskInfo
  84. }
  85. db1 := GetReadDB(ctx)
  86. db1 = db1.Debug().Model(gorm_model.YoungeeTaskLogistics{})
  87. var logisticsInfos []gorm_model.YoungeeTaskLogistics
  88. db1 = db1.Model(gorm_model.YoungeeTaskLogistics{}).Where("task_id IN ?", taskIds).Find(&logisticsInfos)
  89. logisticsMap := make(map[int]gorm_model.YoungeeTaskLogistics)
  90. for _, logisticsInfo := range logisticsInfos {
  91. logisticsMap[conv.MustInt(logisticsInfo.TaskID)] = logisticsInfo
  92. }
  93. // 查询总数
  94. var totalLogistics int64
  95. if err := db1.Count(&totalLogistics).Error; err != nil {
  96. logrus.WithContext(ctx).Errorf("[GetProjectTalentList] error query mysql total, err:%+v", err)
  97. return nil, 0, err
  98. }
  99. var misNum int64
  100. if totalLogistics > totalTask {
  101. misNum = totalLogistics - totalTask
  102. } else {
  103. misNum = totalTask - totalLogistics
  104. }
  105. logrus.Println("totalLogistics,totalTalent,misNum:", totalLogistics, totalTask, misNum)
  106. // 查询该页数据
  107. limit := pageSize + misNum
  108. offset := pageSize * pageNum // assert pageNum start with 0
  109. err := db.Order("task_id").Limit(int(limit)).Offset(int(offset)).Error
  110. if err != nil {
  111. logrus.WithContext(ctx).Errorf("[GetProjectTaskList] error query mysql total, err:%+v", err)
  112. return nil, 0, err
  113. }
  114. var TaskLogisticss []*http_model.TaskLogistics
  115. var taskLogisticss []*http_model.TaskLogisticsInfo
  116. var newTaskLogisticss []*http_model.TaskLogisticsInfo
  117. for _, taskId := range taskIds {
  118. TaskLogistics := new(http_model.TaskLogistics)
  119. TaskLogistics.Talent = taskMap[taskId]
  120. TaskLogistics.Logistics = logisticsMap[taskId]
  121. TaskLogisticss = append(TaskLogisticss, TaskLogistics)
  122. }
  123. taskLogisticss = pack.TaskLogisticsToTaskInfo(TaskLogisticss)
  124. for _, v := range taskLogisticss {
  125. if platform_nickname == "" {
  126. newTaskLogisticss = append(newTaskLogisticss, v)
  127. } else if strings.Contains(v.PlatformNickname, platform_nickname) {
  128. newTaskLogisticss = append(newTaskLogisticss, v)
  129. } else if strings.Contains(conv.MustString(v.TaskID), platform_nickname) {
  130. newTaskLogisticss = append(newTaskLogisticss, v)
  131. } else {
  132. totalTask--
  133. }
  134. }
  135. return newTaskLogisticss, totalTask, nil
  136. }
  137. // 修改任务表的物流状态
  138. func ChangeLogisticsStatus(ctx context.Context, taskIds []string) error {
  139. db := GetReadDB(ctx)
  140. err := db.Debug().Model(gorm_model.YoungeeTaskInfo{}).Where("task_id IN ?", taskIds).Update("logistics_status", 3).Error
  141. if err != nil {
  142. logrus.WithContext(ctx).Errorf("[ChangeTaskStatus] error query mysql total, err:%+v", err)
  143. return err
  144. }
  145. return nil
  146. }