logistics.go 5.2 KB

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