s_local_life.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. package db
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/sirupsen/logrus"
  6. "reflect"
  7. "youngee_b_api/model/common_model"
  8. "youngee_b_api/model/gorm_model"
  9. "youngee_b_api/util"
  10. )
  11. // GetFullLocalLifeList 商单广场-公开本地生活列表
  12. func GetFullLocalLifeList(ctx context.Context, pageSize, pageNum int32, condition *common_model.SLocalLifeCondition) ([]*gorm_model.YounggeeLocalLifeInfo, int64, error) {
  13. db := GetReadDB(ctx)
  14. // 根据带货任务状态过滤
  15. db = db.Debug().Model(gorm_model.YounggeeLocalLifeInfo{}).Where("task_status = 4 and local_type = 1")
  16. // 根据Project条件过滤
  17. conditionType := reflect.TypeOf(condition).Elem()
  18. conditionValue := reflect.ValueOf(condition).Elem()
  19. for i := 0; i < conditionType.NumField(); i++ {
  20. field := conditionType.Field(i)
  21. tag := field.Tag.Get("condition")
  22. value := conditionValue.FieldByName(field.Name)
  23. if (tag == "local_id" || tag == "local_name") && !util.IsBlank(value) {
  24. db = db.Where(fmt.Sprintf("local_id like '%%%v%%' or local_name like '%%%v%%'", value.Interface(), value.Interface()))
  25. } else if tag == "updated_at" && value.Interface() != "0" {
  26. db = db.Where(fmt.Sprintf("updated_at like '%s%%'", value.Interface()))
  27. } else if !util.IsBlank(value) && tag != "updated_at" {
  28. db = db.Where(fmt.Sprintf("%s = ?", tag), value.Interface())
  29. }
  30. }
  31. // 查询总数
  32. var total int64
  33. var fullLocals []*gorm_model.YounggeeLocalLifeInfo
  34. if err := db.Count(&total).Error; err != nil {
  35. logrus.WithContext(ctx).Errorf("[GetFullProjectList] error query mysql total, err:%+v", err)
  36. return nil, 0, err
  37. }
  38. // 查询该页数据
  39. limit := pageSize
  40. offset := pageSize * pageNum // assert pageNum start with 0
  41. err := db.Order("updated_at desc").Limit(int(limit)).Offset(int(offset)).Find(&fullLocals).Error
  42. if err != nil {
  43. logrus.WithContext(ctx).Errorf("[GetFullProjectList] error query mysql total, err:%+v", err)
  44. return nil, 0, err
  45. }
  46. return fullLocals, total, nil
  47. }
  48. // GetLocalLifeDetail 根据本地生活ID查询本地生活详情
  49. func GetLocalLifeDetail(ctx context.Context, localLifeId string) (*gorm_model.YounggeeLocalLifeInfo, error) {
  50. db := GetReadDB(ctx)
  51. var localLifeInfo *gorm_model.YounggeeLocalLifeInfo
  52. whereCondition := gorm_model.YounggeeLocalLifeInfo{LocalId: localLifeId}
  53. err := db.Model(gorm_model.YounggeeLocalLifeInfo{}).Where(whereCondition).Find(&localLifeInfo).Error
  54. if err != nil {
  55. return nil, err
  56. }
  57. return localLifeInfo, nil
  58. }
  59. // GetSLocalLifeDetail 服务商本地生活查询
  60. func GetSLocalLifeDetail(ctx context.Context, sLocalLifeId int) (*gorm_model.YounggeeSLocalLifeInfo, error) {
  61. db := GetReadDB(ctx)
  62. var sLocalLifeInfo *gorm_model.YounggeeSLocalLifeInfo
  63. whereCondition := gorm_model.YounggeeSLocalLifeInfo{SLocalId: sLocalLifeId}
  64. err := db.Model(gorm_model.YounggeeSLocalLifeInfo{}).Where(whereCondition).Find(&sLocalLifeInfo).Error
  65. if err != nil {
  66. return nil, err
  67. }
  68. return sLocalLifeInfo, nil
  69. }
  70. // CreateSLocalLife 创建服务商加入商单后的本地生活详情
  71. func CreateSLocalLife(ctx context.Context, newSLocal *gorm_model.YounggeeSLocalLifeInfo) (int, error) {
  72. db := GetWriteDB(ctx)
  73. err := db.Create(&newSLocal).Error
  74. if err != nil {
  75. return 0, err
  76. }
  77. return newSLocal.SLocalId, nil
  78. }
  79. // FindBriefByLocalId 根据localId查找Brief
  80. func FindBriefByLocalId(ctx context.Context, localId string) ([]gorm_model.LocalLifeBrief, error) {
  81. db := GetReadDB(ctx)
  82. var sProjectInfo []gorm_model.LocalLifeBrief
  83. // 1. 根据服务商种草任务id过滤
  84. err := db.Model(gorm_model.LocalLifeBrief{}).Where("local_id = ?", localId).Find(&sProjectInfo).Error
  85. if err != nil {
  86. logrus.WithContext(ctx).Errorf("[GetSProjectDetail] error query mysql, err:%+v", err)
  87. return nil, err
  88. }
  89. return sProjectInfo, nil
  90. }
  91. // FindMaterialByLocalId 根据localId查找Material
  92. func FindMaterialByLocalId(ctx context.Context, localId string) ([]gorm_model.LocalLifeMaterial, error) {
  93. db := GetReadDB(ctx)
  94. var sProjectInfo []gorm_model.LocalLifeMaterial
  95. // 1. 根据服务商种草任务id过滤
  96. err := db.Model(gorm_model.LocalLifeMaterial{}).Where("local_id = ?", localId).Find(&sProjectInfo).Error
  97. if err != nil {
  98. logrus.WithContext(ctx).Errorf("[GetSProjectDetail] error query mysql, err:%+v", err)
  99. return nil, err
  100. }
  101. return sProjectInfo, nil
  102. }
  103. // FindSLocalByLocalIdAndSupplierId 根据LocalId和SupplierId查找SLocal数量
  104. func FindSLocalByLocalIdAndSupplierId(ctx context.Context, localId string, supplierId int) (int64, error) {
  105. db := GetWriteDB(ctx)
  106. whereCondition := gorm_model.YounggeeSLocalLifeInfo{
  107. LocalId: localId,
  108. SupplierId: supplierId,
  109. }
  110. var total int64
  111. err := db.Debug().Model(gorm_model.YounggeeSLocalLifeInfo{}).Where(whereCondition).Count(&total).Error
  112. if err != nil {
  113. return 0, err
  114. }
  115. return total, nil
  116. }
  117. // GetFullSLocalLifeList 商单管理-公开本地生活列表
  118. func GetFullSLocalLifeList(ctx context.Context, pageSize, pageNum int32, condition *common_model.SLocalLifeCondition) ([]*gorm_model.YounggeeSLocalLifeInfo, int64, error) {
  119. db := GetReadDB(ctx)
  120. // 根据带货任务状态过滤
  121. db = db.Debug().Model(gorm_model.YounggeeSLocalLifeInfo{}).Where("task_status = 4 and local_type = 1")
  122. // 根据条件过滤
  123. conditionType := reflect.TypeOf(condition).Elem()
  124. conditionValue := reflect.ValueOf(condition).Elem()
  125. for i := 0; i < conditionType.NumField(); i++ {
  126. field := conditionType.Field(i)
  127. tag := field.Tag.Get("condition")
  128. value := conditionValue.FieldByName(field.Name)
  129. if (tag == "local_id" || tag == "local_name") && !util.IsBlank(value) {
  130. db = db.Where(fmt.Sprintf("local_id like '%%%v%%' or local_name like '%%%v%%'", value.Interface(), value.Interface()))
  131. } else if tag == "updated_at" && value.Interface() != "0" {
  132. db = db.Where(fmt.Sprintf("updated_at like '%s%%'", value.Interface()))
  133. } else if !util.IsBlank(value) && tag != "updated_at" {
  134. db = db.Where(fmt.Sprintf("%s = ?", tag), value.Interface())
  135. }
  136. }
  137. // 查询总数
  138. var total int64
  139. var fullLocals []*gorm_model.YounggeeSLocalLifeInfo
  140. if err := db.Count(&total).Error; err != nil {
  141. logrus.WithContext(ctx).Errorf("[GetFullProjectList] error query mysql total, err:%+v", err)
  142. return nil, 0, err
  143. }
  144. // 查询该页数据
  145. limit := pageSize
  146. offset := pageSize * pageNum // assert pageNum start with 0
  147. err := db.Order("create_time desc").Limit(int(limit)).Offset(int(offset)).Find(&fullLocals).Error
  148. if err != nil {
  149. logrus.WithContext(ctx).Errorf("[GetFullProjectList] error query mysql total, err:%+v", err)
  150. return nil, 0, err
  151. }
  152. return fullLocals, total, nil
  153. }
  154. // GetSpecialLocalLifeList 商单广场-定向本地生活列表
  155. func GetSpecialLocalLifeList(ctx context.Context, pageSize, pageNum int32, condition *common_model.SSpecialLocalLifeCondition) ([]*gorm_model.YounggeeSLocalLifeInfo, int64, error) {
  156. db := GetReadDB(ctx)
  157. // 根据带货任务状态过滤
  158. db = db.Debug().Model(gorm_model.YounggeeSLocalLifeInfo{}).Where("task_status = 4 and local_type = 2")
  159. // 根据Project条件过滤
  160. conditionType := reflect.TypeOf(condition).Elem()
  161. conditionValue := reflect.ValueOf(condition).Elem()
  162. for i := 0; i < conditionType.NumField(); i++ {
  163. field := conditionType.Field(i)
  164. tag := field.Tag.Get("condition")
  165. value := conditionValue.FieldByName(field.Name)
  166. if (tag == "local_id" || tag == "local_name") && !util.IsBlank(value) {
  167. db = db.Where(fmt.Sprintf("local_id like '%%%v%%' or local_name like '%%%v%%'", value.Interface(), value.Interface()))
  168. } else if tag == "updated_at" && value.Interface() != "0" {
  169. db = db.Where(fmt.Sprintf("updated_at like '%s%%'", value.Interface()))
  170. } else if !util.IsBlank(value) && tag != "updated_at" {
  171. db = db.Where(fmt.Sprintf("%s = ?", tag), value.Interface())
  172. }
  173. }
  174. // 查询总数
  175. var total int64
  176. var fullLocals []*gorm_model.YounggeeSLocalLifeInfo
  177. if err := db.Count(&total).Error; err != nil {
  178. logrus.WithContext(ctx).Errorf("[GetFullProjectList] error query mysql total, err:%+v", err)
  179. return nil, 0, err
  180. }
  181. // 查询该页数据
  182. limit := pageSize
  183. offset := pageSize * pageNum // assert pageNum start with 0
  184. err := db.Order("create_time desc").Limit(int(limit)).Offset(int(offset)).Find(&fullLocals).Error
  185. if err != nil {
  186. logrus.WithContext(ctx).Errorf("[GetFullProjectList] error query mysql total, err:%+v", err)
  187. return nil, 0, err
  188. }
  189. return fullLocals, total, nil
  190. }
  191. // UpdateSLocal 更新SLocal
  192. func UpdateSLocal(ctx context.Context, sLocalInfo *gorm_model.YounggeeSLocalLifeInfo) error {
  193. db := GetWriteDB(ctx)
  194. whereCondition := gorm_model.YounggeeSLocalLifeInfo{SLocalId: sLocalInfo.SLocalId}
  195. err := db.Model(&gorm_model.YounggeeSLocalLifeInfo{}).Where(whereCondition).Updates(sLocalInfo).Error
  196. if err != nil {
  197. return err
  198. }
  199. return nil
  200. }
  201. func GetFullSLocalBillList(ctx context.Context, pageSize, pageNum int32, supplierId int) ([]*gorm_model.YounggeeSLocalLifeInfo, int64, error) {
  202. db := GetReadDB(ctx)
  203. // 根据带货任务状态过滤
  204. db = db.Debug().Model(gorm_model.YounggeeSLocalLifeInfo{}).Where("task_status = 4 and local_type = 1")
  205. // 查询总数
  206. var total int64
  207. var fullLocals []*gorm_model.YounggeeSLocalLifeInfo
  208. if err := db.Count(&total).Error; err != nil {
  209. logrus.WithContext(ctx).Errorf("[GetFullProjectList] error query mysql total, err:%+v", err)
  210. return nil, 0, err
  211. }
  212. // 查询该页数据
  213. limit := pageSize
  214. offset := pageSize * pageNum // assert pageNum start with 0
  215. err := db.Order("s_local_id desc").Limit(int(limit)).Offset(int(offset)).Find(&fullLocals).Error
  216. if err != nil {
  217. logrus.WithContext(ctx).Errorf("[GetFullProjectList] error query mysql total, err:%+v", err)
  218. return nil, 0, err
  219. }
  220. return fullLocals, total, nil
  221. }