logistics.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. }
  148. // GetSpecialTaskLogisticsList 查询专项包含物流信息的task list
  149. func GetSpecialTaskLogisticsList(ctx context.Context, projectID string, pageSize, pageNum int64, conditions *common_model.TalentConditions) ([]*http_model.SpecialTaskLogisticsInfo, int64, error) {
  150. db := GetReadDB(ctx)
  151. // 查询Task表信息
  152. db = db.Debug().Model(gorm_model.YoungeeTaskInfo{}).Where("task_status = 2 and project_id = ?", projectID)
  153. // 根据Project条件过滤
  154. conditionType := reflect.TypeOf(conditions).Elem()
  155. conditionValue := reflect.ValueOf(conditions).Elem()
  156. var platformNickname string = ""
  157. for i := 0; i < conditionType.NumField(); i++ {
  158. field := conditionType.Field(i)
  159. tag := field.Tag.Get("condition")
  160. value := conditionValue.FieldByName(field.Name)
  161. if !util.IsBlank(value) {
  162. if tag == "platform_nickname" {
  163. platformNickname = fmt.Sprintf("%v", value.Interface())
  164. continue
  165. } else {
  166. db = db.Where(fmt.Sprintf("%s = ?", tag), value.Interface())
  167. }
  168. }
  169. }
  170. var taskInfos []gorm_model.YoungeeTaskInfo
  171. db = db.Model(gorm_model.YoungeeTaskInfo{})
  172. // 查询总数
  173. var totalTask int64
  174. if err := db.Count(&totalTask).Error; err != nil {
  175. logrus.WithContext(ctx).Errorf("[GetSpecialTaskLogisticsList] error query mysql total, err:%+v", err)
  176. return nil, 0, err
  177. }
  178. db.Order("task_id").Find(&taskInfos)
  179. // 查询任务id
  180. var taskIds []string
  181. taskMap := make(map[string]gorm_model.YoungeeTaskInfo)
  182. for _, taskInfo := range taskInfos {
  183. taskIds = append(taskIds, taskInfo.TaskId)
  184. taskMap[taskInfo.TaskId] = taskInfo
  185. }
  186. db1 := GetReadDB(ctx)
  187. db1 = db1.Debug().Model(gorm_model.YoungeeTaskLogistics{})
  188. var logisticsInfos []gorm_model.YoungeeTaskLogistics
  189. db1 = db1.Model(gorm_model.YoungeeTaskLogistics{}).Where("task_id IN ?", taskIds).Find(&logisticsInfos)
  190. logisticsMap := make(map[string]gorm_model.YoungeeTaskLogistics)
  191. for _, logisticsInfo := range logisticsInfos {
  192. logisticsMap[logisticsInfo.TaskID] = logisticsInfo
  193. }
  194. // 查询总数
  195. var totalLogistics int64
  196. if err := db1.Count(&totalLogistics).Error; err != nil {
  197. logrus.WithContext(ctx).Errorf("[GetSpecialTaskLogisticsList] error query mysql total, err:%+v", err)
  198. return nil, 0, err
  199. }
  200. // 查询该页数据
  201. limit := pageSize
  202. offset := pageSize * pageNum // assert pageNum start with 0
  203. err := db.Order("task_id").Limit(int(limit)).Offset(int(offset)).Error
  204. if err != nil {
  205. logrus.WithContext(ctx).Errorf("[GetSpecialTaskLogisticsList] error query mysql total, err:%+v", err)
  206. return nil, 0, err
  207. }
  208. var TaskLogisticss []*http_model.SpecialTaskLogistics
  209. var taskLogisticss []*http_model.SpecialTaskLogisticsInfo
  210. var newTaskLogisticss []*http_model.SpecialTaskLogisticsInfo
  211. for _, taskId := range taskIds {
  212. TaskLogistics := new(http_model.SpecialTaskLogistics)
  213. TaskLogistics.Talent = taskMap[taskId]
  214. TaskLogistics.Logistics = logisticsMap[taskId]
  215. TalentPostAddrSnap := TaskLogistics.Talent.TalentPostAddrSnap
  216. regionCode, _ := strconv.Atoi(conv.MustString(gjson.Get(TalentPostAddrSnap, "region_code"), ""))
  217. TaskLogistics.Region = GetRegion(ctx, regionCode)
  218. TaskLogisticss = append(TaskLogisticss, TaskLogistics)
  219. }
  220. taskLogisticss = pack.SpecialTaskLogisticsToTaskInfo(TaskLogisticss)
  221. for _, v := range taskLogisticss {
  222. if platformNickname == "" {
  223. newTaskLogisticss = append(newTaskLogisticss, v)
  224. } else if strings.Contains(v.PlatformNickname, platformNickname) {
  225. newTaskLogisticss = append(newTaskLogisticss, v)
  226. } else if strings.Contains(v.TaskID, platformNickname) {
  227. newTaskLogisticss = append(newTaskLogisticss, v)
  228. } else {
  229. totalTask--
  230. }
  231. }
  232. return newTaskLogisticss, totalTask, nil
  233. }
  234. func GetSecTaskLogisticsList(ctx context.Context, secTaskList []*http_model.SecTaskInfo, taskStage int, productType int) ([]*http_model.SecTaskInfo, error) {
  235. db := GetReadDB(ctx)
  236. println("taskstage: ", taskStage)
  237. if taskStage != 6 { // 已发货
  238. // 查询youngee_task_logistics中物流信息
  239. var taskIds []string
  240. taskMap := make(map[string]*http_model.SecTaskInfo)
  241. for _, secTask := range secTaskList {
  242. taskIds = append(taskIds, secTask.SecTaskId)
  243. println("taskid: ", secTask.SecTaskId)
  244. taskMap[secTask.SecTaskId] = secTask
  245. }
  246. var logisticsInfos []gorm_model.YoungeeTaskLogistics
  247. err := db.Model(gorm_model.YoungeeTaskLogistics{}).Where("task_id IN ?", taskIds).Find(&logisticsInfos).Error
  248. if err != nil {
  249. logrus.WithContext(ctx).Errorf("[GetProjectTaskList] error query mysql total, err:%+v", err)
  250. return nil, err
  251. }
  252. logisticsMap := make(map[string]gorm_model.YoungeeTaskLogistics)
  253. for _, logisticsInfo := range logisticsInfos {
  254. logisticsMap[logisticsInfo.TaskID] = logisticsInfo
  255. }
  256. // 融合信息
  257. for i, secTask := range secTaskList {
  258. taskID := secTask.SecTaskId
  259. secTaskList[i].CompanyName = logisticsMap[taskID].CompanyName
  260. secTaskList[i].LogisticsNumber = logisticsMap[taskID].LogisticsNumber
  261. secTaskList[i].ExplorestorePeriod = logisticsMap[taskID].ExplorestorePeriod
  262. secTaskList[i].ExplorestoreStarttime = logisticsMap[taskID].ExplorestoreStarttime
  263. secTaskList[i].ExplorestoreEndtime = logisticsMap[taskID].ExplorestoreEndtime
  264. }
  265. }
  266. return secTaskList, nil
  267. }
  268. func GetLogistics(ctx context.Context, secTaskId string) (*gorm_model.YoungeeTaskLogistics, error) {
  269. db := GetWriteDB(ctx)
  270. logistics := gorm_model.YoungeeTaskLogistics{}
  271. err := db.Model(gorm_model.YoungeeTaskLogistics{}).Where("task_id", secTaskId).First(&logistics).Error
  272. if err != nil {
  273. return nil, err
  274. }
  275. return &logistics, nil
  276. }