logistics.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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/caixw/lib.go/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. err := db.Model(gorm_model.YoungeeTaskLogistics{}).Where("task_id = ?", taskID).Update("signed_time", time.Now()).Error
  40. if err != nil {
  41. logrus.WithContext(ctx).Errorf("[logistics db] call UpdateLogistics error,err:%+v", err)
  42. return err
  43. }
  44. return nil
  45. }
  46. // 查询包含物流信息的task list
  47. func GetTaskLogisticsList(ctx context.Context, projectID string, pageSize, pageNum int64, conditions *common_model.TalentConditions) ([]*http_model.TaskLogisticsInfo, int64, error) {
  48. db := GetReadDB(ctx)
  49. // 查询Task表信息
  50. db = db.Debug().Model(gorm_model.YoungeeTaskInfo{})
  51. // 根据Project条件过滤
  52. conditionType := reflect.TypeOf(conditions).Elem()
  53. conditionValue := reflect.ValueOf(conditions).Elem()
  54. var platform_nickname string = ""
  55. for i := 0; i < conditionType.NumField(); i++ {
  56. field := conditionType.Field(i)
  57. tag := field.Tag.Get("condition")
  58. value := conditionValue.FieldByName(field.Name)
  59. if !util.IsBlank(value) {
  60. if tag == "platform_nickname" {
  61. platform_nickname = fmt.Sprintf("%v", value.Interface())
  62. continue
  63. } else {
  64. db = db.Where(fmt.Sprintf("%s = ?", tag), value.Interface())
  65. }
  66. }
  67. }
  68. var taskInfos []gorm_model.YoungeeTaskInfo
  69. db = db.Model(gorm_model.YoungeeTaskInfo{})
  70. // 查询总数
  71. var totalTask int64
  72. if err := db.Count(&totalTask).Error; err != nil {
  73. logrus.WithContext(ctx).Errorf("[GetProjectTaskList] error query mysql total, err:%+v", err)
  74. return nil, 0, err
  75. }
  76. db.Order("task_id").Find(&taskInfos)
  77. // 查询任务id
  78. var taskIds []int
  79. taskMap := make(map[int]gorm_model.YoungeeTaskInfo)
  80. for _, taskInfo := range taskInfos {
  81. taskIds = append(taskIds, taskInfo.TaskId)
  82. taskMap[taskInfo.TaskId] = taskInfo
  83. }
  84. // 查询物流信息
  85. var logisticsInfos []gorm_model.YoungeeTaskLogistics
  86. db1 := GetReadDB(ctx)
  87. db1 = db1.Debug().Model(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, 0)] = 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. // 查询该页数据
  100. limit := pageSize
  101. offset := pageSize * pageNum // assert pageNum start with 0
  102. err := db.Order("task_id").Limit(int(limit)).Offset(int(offset)).Error
  103. if err != nil {
  104. logrus.WithContext(ctx).Errorf("[GetProjectTaskList] error query mysql total, err:%+v", err)
  105. return nil, 0, err
  106. }
  107. var TaskLogisticss []*http_model.TaskLogistics
  108. var taskLogisticss []*http_model.TaskLogisticsInfo
  109. var newTaskLogisticss []*http_model.TaskLogisticsInfo
  110. for _, taskId := range taskIds {
  111. TaskLogistics := new(http_model.TaskLogistics)
  112. TaskLogistics.Talent = taskMap[taskId]
  113. TaskLogistics.Logistics = logisticsMap[taskId]
  114. TaskLogisticss = append(TaskLogisticss, TaskLogistics)
  115. }
  116. taskLogisticss = pack.TaskLogisticsToTaskInfo(TaskLogisticss)
  117. for _, v := range taskLogisticss {
  118. if platform_nickname == "" {
  119. newTaskLogisticss = append(newTaskLogisticss, v)
  120. } else if strings.Contains(v.PlatformNickname, platform_nickname) {
  121. newTaskLogisticss = append(newTaskLogisticss, v)
  122. } else if strings.Contains(conv.MustString(v.TaskID, ""), platform_nickname) {
  123. newTaskLogisticss = append(newTaskLogisticss, v)
  124. } else {
  125. totalTask--
  126. }
  127. }
  128. return newTaskLogisticss, totalTask, nil
  129. }
  130. // 修改任务表的物流状态
  131. func ChangeLogisticsStatus(ctx context.Context, taskIds []string) error {
  132. db := GetReadDB(ctx)
  133. err := db.Debug().Model(gorm_model.YoungeeTaskInfo{}).Where("task_id IN ?", taskIds).Update("logistics_status", 3).Error
  134. if err != nil {
  135. logrus.WithContext(ctx).Errorf("[ChangeTaskStatus] error query mysql total, err:%+v", err)
  136. return err
  137. }
  138. return nil
  139. }