logistics.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. package db
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/caixw/lib.go/conv"
  6. "github.com/sirupsen/logrus"
  7. "reflect"
  8. "strings"
  9. "time"
  10. "youngee_m_api/model/common_model"
  11. "youngee_m_api/model/gorm_model"
  12. "youngee_m_api/model/http_model"
  13. "youngee_m_api/pack"
  14. "youngee_m_api/util"
  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. //logrus.Println("tag: ", tag)
  61. if tag == "platform_nickname" {
  62. platform_nickname = fmt.Sprintf("%v", value.Interface())
  63. continue
  64. } else if tag == "project_id" {
  65. db = db.Where(fmt.Sprintf("%s = ?", tag), value.Interface())
  66. } else if tag == "strategy_ids" {
  67. strategyIds := strings.Split(fmt.Sprintf("%v", value.Interface()), ",")
  68. var strategyIdList []int
  69. for _, strategyId := range strategyIds {
  70. strategyIdList = append(strategyIdList, conv.MustInt(strategyId, 0))
  71. }
  72. db = db.Where("strategy_id in ?", strategyIdList)
  73. } else {
  74. db = db.Where(fmt.Sprintf("%s like '%%%v%%'", tag, value.Interface()))
  75. }
  76. }
  77. }
  78. var taskInfos []gorm_model.YoungeeTaskInfo
  79. db = db.Model(gorm_model.YoungeeTaskInfo{})
  80. // 查询总数
  81. var totalTask int64
  82. if err := db.Count(&totalTask).Error; err != nil {
  83. logrus.WithContext(ctx).Errorf("[GetProjectTaskList] error query mysql total, err:%+v", err)
  84. return nil, 0, err
  85. }
  86. db.Order("task_id").Find(&taskInfos)
  87. // 查询任务id
  88. var taskIds []int
  89. taskMap := make(map[int]gorm_model.YoungeeTaskInfo)
  90. for _, taskInfo := range taskInfos {
  91. taskIds = append(taskIds, taskInfo.TaskId)
  92. taskMap[taskInfo.TaskId] = taskInfo
  93. }
  94. db1 := GetReadDB(ctx)
  95. db1 = db1.Debug().Model(gorm_model.YoungeeTaskLogistics{})
  96. // 根据Project条件过滤
  97. conditionType2 := reflect.TypeOf(conditions).Elem()
  98. conditionValue2 := reflect.ValueOf(conditions).Elem()
  99. for i := 0; i < conditionType2.NumField(); i++ {
  100. field := conditionType2.Field(i)
  101. tag := field.Tag.Get("condition")
  102. value := conditionValue2.FieldByName(field.Name)
  103. //fmt.Printf("过滤展示 %+v %+v \n", value, tag)
  104. if !util.IsBlank(value) && tag == "platform_nickname" { // input:1 taskIdsbase:1,2,3 string
  105. db1 = db1.Where(fmt.Sprintf("%s = ?", tag), value.Interface())
  106. }
  107. }
  108. var logisticsInfos []gorm_model.YoungeeTaskLogistics
  109. db1 = db1.Model(gorm_model.YoungeeTaskLogistics{}).Where("task_id IN ?", taskIds).Find(&logisticsInfos)
  110. logisticsMap := make(map[int]gorm_model.YoungeeTaskLogistics)
  111. for _, logisticsInfo := range logisticsInfos {
  112. logisticsMap[conv.MustInt(logisticsInfo.TaskID, 0)] = logisticsInfo
  113. }
  114. // 查询总数
  115. var totalLogistics int64
  116. if err := db1.Count(&totalLogistics).Error; err != nil {
  117. logrus.WithContext(ctx).Errorf("[GetProjectTalentList] error query mysql total, err:%+v", err)
  118. return nil, 0, err
  119. }
  120. // 查询该页数据
  121. limit := pageSize
  122. offset := pageSize * pageNum // assert pageNum start with 0
  123. err := db.Order("task_id").Limit(int(limit)).Offset(int(offset)).Error
  124. if err != nil {
  125. logrus.WithContext(ctx).Errorf("[GetProjectTaskList] error query mysql total, err:%+v", err)
  126. return nil, 0, err
  127. }
  128. var TaskLogisticss []*http_model.TaskLogistics
  129. var taskLogisticss []*http_model.TaskLogisticsInfo
  130. var newTaskLinks []*http_model.TaskLogisticsInfo
  131. for _, taskId := range taskIds {
  132. TaskLogistics := new(http_model.TaskLogistics)
  133. TaskLogistics.Talent = taskMap[taskId]
  134. TaskLogistics.Logistics = logisticsMap[taskId]
  135. TaskLogisticss = append(TaskLogisticss, TaskLogistics)
  136. }
  137. taskLogisticss = pack.TaskLogisticsToTaskInfo(TaskLogisticss)
  138. for _, v := range taskLogisticss {
  139. if platform_nickname == "" {
  140. newTaskLinks = append(newTaskLinks, v)
  141. } else if strings.Contains(v.PlatformNickname, platform_nickname) {
  142. newTaskLinks = append(newTaskLinks, v)
  143. } else {
  144. totalTask--
  145. }
  146. }
  147. // return fulltaskLogisticss, total, nil
  148. return newTaskLinks, totalTask, nil
  149. }
  150. // 修改任务表的物流状态
  151. func ChangeLogisticsStatus(ctx context.Context, taskIds []string) error {
  152. db := GetReadDB(ctx)
  153. err := db.Debug().Model(gorm_model.YoungeeTaskInfo{}).Where("task_id IN ?", taskIds).Update("logistics_status", 3).Error
  154. if err != nil {
  155. logrus.WithContext(ctx).Errorf("[ChangeTaskStatus] error query mysql total, err:%+v", err)
  156. return err
  157. }
  158. return nil
  159. }