s_project.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. package db
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/sirupsen/logrus"
  6. "reflect"
  7. "time"
  8. "youngee_b_api/model/common_model"
  9. "youngee_b_api/model/gorm_model"
  10. "youngee_b_api/model/http_model"
  11. "youngee_b_api/util"
  12. )
  13. // CreateSProject 新建服务商加入商单后的种草任务
  14. func CreateSProject(ctx context.Context, newSProject gorm_model.SProjectInfo) (int, error) {
  15. db := GetWriteDB(ctx)
  16. err := db.Create(&newSProject).Error
  17. if err != nil {
  18. return 0, err
  19. }
  20. return newSProject.SProjectId, nil
  21. }
  22. // GetSProjectList 根据服务商ID和其他附加条件查询种草任务列表
  23. func GetSProjectList(ctx context.Context, supplierId int, pageSize, pageNum int32, condition *common_model.SProjectCondition) ([]*gorm_model.SProjectInfo, int64, error) {
  24. db := GetReadDB(ctx)
  25. // 1. 根据服务商id过滤
  26. db = db.Debug().Model(gorm_model.SProjectInfo{}).Where("supplier_id = ? and project_status <> 1", supplierId)
  27. // 2. 根据SProjectCondition条件过滤
  28. conditionType := reflect.TypeOf(condition).Elem()
  29. conditionValue := reflect.ValueOf(condition).Elem()
  30. for i := 0; i < conditionType.NumField(); i++ {
  31. field := conditionType.Field(i)
  32. tag := field.Tag.Get("condition")
  33. value := conditionValue.FieldByName(field.Name)
  34. if (tag == "project_id" || tag == "project_name") && !util.IsBlank(value) {
  35. db = db.Where(fmt.Sprintf("project_id like '%%%v%%' or project_name like '%%%v%%'", value.Interface(), value.Interface()))
  36. } else if !util.IsBlank(value) && tag != "updated_at" {
  37. db = db.Where(fmt.Sprintf("%s = ?", tag), value.Interface())
  38. }
  39. }
  40. // 3. 确定查询总数和返回当前页数据
  41. var total int64
  42. var SProjects []*gorm_model.SProjectInfo
  43. if err := db.Count(&total).Error; err != nil {
  44. logrus.WithContext(ctx).Errorf("[GetFullProjectList] error query mysql total, err:%+v", err)
  45. return nil, 0, err
  46. }
  47. limit := pageSize
  48. offset := pageSize * pageNum // assert pageNum start with 0
  49. err := db.Order("s_project_id desc").Limit(int(limit)).Offset(int(offset)).Find(&SProjects).Error
  50. if err != nil {
  51. logrus.WithContext(ctx).Errorf("[GetFullProjectList] error query mysql total, err:%+v", err)
  52. return nil, 0, err
  53. }
  54. return SProjects, total, nil
  55. }
  56. // GetSProjectDetail 查询服务商种草任务详情
  57. func GetSProjectDetail(ctx context.Context, sProjectId int) (*gorm_model.SProjectInfo, error) {
  58. db := GetReadDB(ctx)
  59. var sProjectInfo *gorm_model.SProjectInfo
  60. // 1. 根据服务商种草任务id过滤
  61. err := db.Model(gorm_model.SProjectInfo{}).Where("s_project_id = ?", sProjectId).Find(&sProjectInfo).Error
  62. if err != nil {
  63. logrus.WithContext(ctx).Errorf("[GetSProjectDetail] error query mysql, err:%+v", err)
  64. return nil, err
  65. }
  66. return sProjectInfo, nil
  67. }
  68. // GetSpecialProjectList 根据服务商ID和其他附加条件查询定向种草任务列表
  69. func GetSpecialProjectList(ctx context.Context, supplierId int, pageSize, pageNum int32, condition *common_model.SpecialSProjectCondition, tag int) ([]*gorm_model.SProjectInfo, int64, error) {
  70. db := GetReadDB(ctx)
  71. // 1. 根据服务商id过滤
  72. if tag == 1 {
  73. db = db.Debug().Model(gorm_model.SProjectInfo{}).Where("supplier_id = ? and project_status = 8 and project_type = 2", supplierId)
  74. } else {
  75. db = db.Debug().Model(gorm_model.SProjectInfo{}).Where("supplier_id = ? and project_type = 2 and s_project_status = 2", supplierId)
  76. }
  77. // 2. 根据SProjectCondition条件过滤
  78. conditionType := reflect.TypeOf(condition).Elem()
  79. conditionValue := reflect.ValueOf(condition).Elem()
  80. for i := 0; i < conditionType.NumField(); i++ {
  81. field := conditionType.Field(i)
  82. tag := field.Tag.Get("condition")
  83. value := conditionValue.FieldByName(field.Name)
  84. if (tag == "project_id" || tag == "project_name") && !util.IsBlank(value) {
  85. db = db.Where(fmt.Sprintf("project_id like '%%%v%%' or project_name like '%%%v%%'", value.Interface(), value.Interface()))
  86. } else if !util.IsBlank(value) && tag != "updated_at" {
  87. db = db.Where(fmt.Sprintf("%s = ?", tag), value.Interface())
  88. }
  89. }
  90. // 3. 确定查询总数和返回当前页数据
  91. var total int64
  92. var SProjects []*gorm_model.SProjectInfo
  93. if err := db.Count(&total).Error; err != nil {
  94. logrus.WithContext(ctx).Errorf("[GetFullProjectList] error query mysql total, err:%+v", err)
  95. return nil, 0, err
  96. }
  97. limit := pageSize
  98. offset := pageSize * pageNum // assert pageNum start with 0
  99. err := db.Order("s_project_id desc").Limit(int(limit)).Offset(int(offset)).Find(&SProjects).Error
  100. if err != nil {
  101. logrus.WithContext(ctx).Errorf("[GetFullProjectList] error query mysql total, err:%+v", err)
  102. return nil, 0, err
  103. }
  104. return SProjects, total, nil
  105. }
  106. // UpdateSProjectStatus 更新SProject信息
  107. func UpdateSProjectStatus(ctx context.Context, newSProject *http_model.SpecialSProjectAddToListRequest) error {
  108. db := GetWriteDB(ctx)
  109. var operatorType int
  110. if newSProject.SubAccountId == 0 {
  111. operatorType = 1
  112. } else {
  113. operatorType = 2
  114. }
  115. var currentTime time.Time
  116. currentTime = time.Now()
  117. whereCondition := gorm_model.SProjectInfo{SProjectId: newSProject.SProjectId}
  118. sProjectInfo := gorm_model.SProjectInfo{
  119. SProjectStatus: newSProject.SProjectStatus,
  120. SubAccountId: newSProject.SubAccountId,
  121. OperatorType: operatorType,
  122. AgreeTime: &currentTime,
  123. }
  124. err := db.Model(&gorm_model.SProjectInfo{}).Where(whereCondition).Updates(sProjectInfo).Error
  125. if err != nil {
  126. return err
  127. }
  128. return nil
  129. }
  130. // CreateSpecialStrategy 创建定向种草任务服务商修改后的招募策略
  131. func CreateSpecialStrategy(ctx context.Context, recruitStrategy []gorm_model.RecruitStrategy) error {
  132. db := GetWriteDB(ctx)
  133. fmt.Println("CreateSpecialStrategy", recruitStrategy)
  134. err := db.Create(&recruitStrategy).Error
  135. if err != nil {
  136. return err
  137. }
  138. return nil
  139. }
  140. // UpdateSProjectStrategyStatus 修改SProject的招募策略修改标志
  141. func UpdateSProjectStrategyStatus(ctx context.Context, req *http_model.SpecialAddStrategyRequest) error {
  142. db := GetWriteDB(ctx)
  143. whereCondition := gorm_model.SProjectInfo{SProjectId: req.SProjectId}
  144. var createStrategyId int
  145. var createStrategyType int
  146. if req.SubAccountId == 0 {
  147. createStrategyId = req.SupplierId
  148. createStrategyType = 1
  149. } else {
  150. createStrategyType = 2
  151. createStrategyId = req.SubAccountId
  152. }
  153. sProjectInfo := gorm_model.SProjectInfo{
  154. CreateStrategyId: createStrategyId,
  155. StrategyStatus: 1,
  156. CreateStrategyType: createStrategyType,
  157. }
  158. err := db.Model(&gorm_model.SProjectInfo{}).Where(whereCondition).Updates(sProjectInfo).Error
  159. if err != nil {
  160. return err
  161. }
  162. return nil
  163. }
  164. // GetFullSProjectBillList 服务商种草任务账单
  165. func GetFullSProjectBillList(ctx context.Context, supplierId int, projectType int, ProjectPlatform int, projectStatus int, pageSize, pageNum int32) ([]*gorm_model.SProjectInfo, int64, error) {
  166. db := GetReadDB(ctx).Model(gorm_model.SProjectInfo{}).Where("supplier_id = ? and project_type = ?", supplierId, projectType)
  167. // 仅当 ProjectPlatform 非零时才过滤
  168. if ProjectPlatform != 0 {
  169. db = db.Where("project_platform = ?", ProjectPlatform)
  170. }
  171. // 仅当 projectStatus 非零时才过滤
  172. if projectStatus != 0 {
  173. db = db.Where("project_status = ?", projectStatus)
  174. }
  175. // 查询总数
  176. var total int64
  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. var SProjects []*gorm_model.SProjectInfo
  183. limit := pageSize
  184. offset := pageSize * pageNum // 假设 pageNum 从 0 开始
  185. err := db.Order("s_project_id desc").Limit(int(limit)).Offset(int(offset)).Find(&SProjects).Error
  186. if err != nil {
  187. logrus.WithContext(ctx).Errorf("[GetFullProjectList] error query mysql, err:%+v", err)
  188. return nil, 0, err
  189. }
  190. return SProjects, total, nil
  191. }
  192. // FindSProjectByProjectIdAndSupplierId 根据种草任务ID和服务商ID查找种草任务是否加入商单
  193. func FindSProjectByProjectIdAndSupplierId(ctx context.Context, projectId string, supplierId int) (int64, error) {
  194. db := GetWriteDB(ctx)
  195. whereCondition := gorm_model.SProjectInfo{
  196. ProjectId: projectId,
  197. SupplierId: supplierId,
  198. }
  199. var total int64
  200. err := db.Debug().Model(gorm_model.SProjectInfo{}).Where(whereCondition).Count(&total).Error
  201. if err != nil {
  202. return 0, err
  203. }
  204. return total, nil
  205. }
  206. func GetSProjectTaskList(ctx context.Context, sProjectId int, condition string, pageSize, pageNum int32) ([]*gorm_model.YoungeeTaskInfo, int64, error) {
  207. db := GetReadDB(ctx)
  208. // 基础查询条件 - 必须有s_project_id
  209. db = db.Debug().Model(gorm_model.YoungeeTaskInfo{}).Where("s_project_id = ?", sProjectId)
  210. // 如果condition不为空
  211. if condition != "" {
  212. // db = db.Where("talent_id = ? OR talent_name LIKE ?", condition, "%"+condition+"%")
  213. db = db.Where("talent_id = ?", condition)
  214. }
  215. var taskInfos []*gorm_model.YoungeeTaskInfo
  216. // 查询总数
  217. var totalTask int64
  218. if err := db.Count(&totalTask).Error; err != nil {
  219. logrus.WithContext(ctx).Errorf("[GetProjectTaskList] error query mysql total, err:%+v", err)
  220. return nil, 0, err
  221. }
  222. // 查询该页数据
  223. limit := pageSize
  224. offset := pageSize * (pageNum - 1) // 假设pageNum从1开始
  225. err := db.Order("task_id").Limit(int(limit)).Offset(int(offset)).Find(&taskInfos).Error
  226. if err != nil {
  227. logrus.WithContext(ctx).Errorf("[GetProjectTaskList] error query mysql data, err:%+v", err)
  228. return nil, 0, err
  229. }
  230. return taskInfos, totalTask, nil
  231. }
  232. // GetUnifiedTaskList 获取统一的账单列表(合并本地生活和种草任务)
  233. func GetUnifiedTaskList(ctx context.Context, supplierId int, pageSize, pageNum int32, status int, condition string) ([]*http_model.SupplierAmountBillListData, int64, error) {
  234. db := GetReadDB(ctx).Debug()
  235. var statusCondition string
  236. if status == 10 {
  237. statusCondition = "= 10"
  238. } else {
  239. statusCondition = "< 10"
  240. }
  241. localLifeSQL := fmt.Sprintf(`
  242. SELECT
  243. s_local_id AS s_id,
  244. local_id AS id,
  245. local_name AS name,
  246. 3 AS type,
  247. local_platform AS platform,
  248. apply_num,
  249. recruit_num,
  250. settle_num,
  251. enterprise_id,
  252. supplier_id,
  253. service_charge,
  254. create_time,
  255. finish_time,
  256. store_id AS product_id
  257. FROM younggee_s_local_life_info
  258. WHERE supplier_id = ? AND task_status %s
  259. `, statusCondition)
  260. projectSQL := fmt.Sprintf(`
  261. SELECT
  262. s_project_id AS s_id,
  263. project_id AS id,
  264. project_name AS name,
  265. 1 AS type,
  266. project_platform AS platform,
  267. apply_num,
  268. recruit_num,
  269. settle_num,
  270. enterprise_id,
  271. supplier_id,
  272. service_charge,
  273. create_time,
  274. finish_time,
  275. product_id
  276. FROM younggee_s_project_info
  277. WHERE supplier_id = ? AND project_status %s
  278. `, statusCondition)
  279. var total int64
  280. countSQL := "SELECT COUNT(*) FROM ((" + localLifeSQL + ") UNION ALL (" + projectSQL + ")) AS combined"
  281. if err := db.Raw(countSQL, supplierId, supplierId).Scan(&total).Error; err != nil {
  282. logrus.WithContext(ctx).Errorf("[GetUnifiedTaskList] error query total count, err:%+v", err)
  283. return nil, 0, err
  284. }
  285. offset := pageSize * pageNum
  286. paginatedSQL := `
  287. SELECT * FROM ((` + localLifeSQL + `) UNION ALL (` + projectSQL + `)) AS combined
  288. ORDER BY create_time DESC
  289. LIMIT ? OFFSET ?
  290. `
  291. var tasks []*http_model.SupplierAmountBillListData
  292. if err := db.Raw(paginatedSQL, supplierId, supplierId, pageSize, offset).Scan(&tasks).Error; err != nil {
  293. logrus.WithContext(ctx).Errorf("[GetUnifiedTaskList] error query data, err:%+v", err)
  294. return nil, 0, err
  295. }
  296. return tasks, total, nil
  297. }