logistics.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. err := db.Model(gorm_model.YoungeeTaskLogistics{}).Where("task_id = ?", taskID).Updates(gorm_model.YoungeeTaskLogistics{
  42. Status: 1,
  43. SignedTime: time.Now(),
  44. }).Error
  45. if err != nil {
  46. logrus.WithContext(ctx).Errorf("[logistics db] call UpdateLogistics error,err:%+v", err)
  47. return err
  48. }
  49. return nil
  50. }
  51. // GetTaskLogisticsList 查询包含物流信息的task list
  52. func GetTaskLogisticsList(ctx context.Context, projectID string, pageSize, pageNum int64, conditions *common_model.TalentConditions) ([]*http_model.TaskLogisticsInfo, int64, error) {
  53. db := GetReadDB(ctx)
  54. // 查询Task表信息
  55. db = db.Debug().Model(gorm_model.YoungeeTaskInfo{}).Where("task_status = 2")
  56. // 根据Project条件过滤
  57. conditionType := reflect.TypeOf(conditions).Elem()
  58. conditionValue := reflect.ValueOf(conditions).Elem()
  59. var platform_nickname string = ""
  60. for i := 0; i < conditionType.NumField(); i++ {
  61. field := conditionType.Field(i)
  62. tag := field.Tag.Get("condition")
  63. value := conditionValue.FieldByName(field.Name)
  64. if !util.IsBlank(value) {
  65. if tag == "platform_nickname" {
  66. platform_nickname = fmt.Sprintf("%v", value.Interface())
  67. continue
  68. } else {
  69. db = db.Where(fmt.Sprintf("%s = ?", tag), value.Interface())
  70. }
  71. }
  72. }
  73. var taskInfos []gorm_model.YoungeeTaskInfo
  74. db = db.Model(gorm_model.YoungeeTaskInfo{})
  75. // 查询总数
  76. var totalTask int64
  77. if err := db.Count(&totalTask).Error; err != nil {
  78. logrus.WithContext(ctx).Errorf("[GetProjectTaskList] error query mysql total, err:%+v", err)
  79. return nil, 0, err
  80. }
  81. db.Order("task_id").Find(&taskInfos)
  82. // 查询任务id
  83. var taskIds []string
  84. taskMap := make(map[string]gorm_model.YoungeeTaskInfo)
  85. for _, taskInfo := range taskInfos {
  86. taskIds = append(taskIds, taskInfo.TaskId)
  87. taskMap[taskInfo.TaskId] = taskInfo
  88. }
  89. db1 := GetReadDB(ctx)
  90. db1 = db1.Debug().Model(gorm_model.YoungeeTaskLogistics{})
  91. var logisticsInfos []gorm_model.YoungeeTaskLogistics
  92. db1 = db1.Model(gorm_model.YoungeeTaskLogistics{}).Where("task_id IN ?", taskIds).Find(&logisticsInfos)
  93. logisticsMap := make(map[string]gorm_model.YoungeeTaskLogistics)
  94. for _, logisticsInfo := range logisticsInfos {
  95. logisticsMap[logisticsInfo.TaskID] = logisticsInfo
  96. }
  97. // 查询总数
  98. var totalLogistics int64
  99. if err := db1.Count(&totalLogistics).Error; err != nil {
  100. logrus.WithContext(ctx).Errorf("[GetProjectTalentList] error query mysql total, err:%+v", err)
  101. return nil, 0, err
  102. }
  103. // 查询该页数据
  104. limit := pageSize
  105. offset := pageSize * pageNum // assert pageNum start with 0
  106. err := db.Order("task_id").Limit(int(limit)).Offset(int(offset)).Error
  107. if err != nil {
  108. logrus.WithContext(ctx).Errorf("[GetProjectTaskList] error query mysql total, err:%+v", err)
  109. return nil, 0, err
  110. }
  111. var TaskLogisticss []*http_model.TaskLogistics
  112. var taskLogisticss []*http_model.TaskLogisticsInfo
  113. var newTaskLogisticss []*http_model.TaskLogisticsInfo
  114. for _, taskId := range taskIds {
  115. TaskLogistics := new(http_model.TaskLogistics)
  116. TaskLogistics.Talent = taskMap[taskId]
  117. TaskLogistics.Logistics = logisticsMap[taskId]
  118. TalentPostAddrSnap := TaskLogistics.Talent.TalentPostAddrSnap
  119. regionCode, _ := strconv.Atoi(conv.MustString(gjson.Get(TalentPostAddrSnap, "region_code"), ""))
  120. TaskLogistics.Region = GetRegion(ctx, regionCode)
  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(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. }