logistics.go 5.3 KB

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