project.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. package db
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/issue9/conv"
  6. "reflect"
  7. "youngee_b_api/consts"
  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/pack"
  12. "youngee_b_api/util"
  13. "github.com/sirupsen/logrus"
  14. log "github.com/sirupsen/logrus"
  15. "gorm.io/gorm"
  16. )
  17. func CreateProject(ctx context.Context, projectInfo gorm_model.ProjectInfo) (*int64, error) {
  18. db := GetWriteDB(ctx)
  19. err := db.Create(&projectInfo).Error
  20. if err != nil {
  21. return nil, err
  22. }
  23. return &projectInfo.ProjectID, nil
  24. }
  25. func UpdateProject(ctx context.Context, project gorm_model.ProjectInfo) (*int64, error) {
  26. db := GetReadDB(ctx)
  27. err := db.Model(&project).Updates(project).Error
  28. if err != nil {
  29. return nil, err
  30. }
  31. return &project.ProjectID, nil
  32. }
  33. func DeleteProject(ctx context.Context, projectID int64) (*int64, error) {
  34. db := GetReadDB(ctx)
  35. err := db.Where("project_id = ?", projectID).Delete(&gorm_model.ProjectInfo{}).Error
  36. if err != nil {
  37. return nil, err
  38. }
  39. return &projectID, nil
  40. }
  41. func GetFullProjectList(ctx context.Context, enterpriseID int64, pageSize, pageNum int32, condition *common_model.ProjectCondition) ([]*gorm_model.ProjectInfo, int64, error) {
  42. db := GetReadDB(ctx)
  43. // 根据企业id过滤
  44. db = db.Debug().Model(gorm_model.ProjectInfo{}).Where("enterprise_id = ?", enterpriseID)
  45. // 根据Project条件过滤
  46. conditionType := reflect.TypeOf(condition).Elem()
  47. conditionValue := reflect.ValueOf(condition).Elem()
  48. for i := 0; i < conditionType.NumField(); i++ {
  49. field := conditionType.Field(i)
  50. tag := field.Tag.Get("condition")
  51. value := conditionValue.FieldByName(field.Name)
  52. if tag == "project_status" && util.IsBlank(value) {
  53. db = db.Where(fmt.Sprintf("project_status != 1"))
  54. }
  55. if !util.IsBlank(value) && tag != "updated_at" && tag != "project_name" {
  56. db = db.Where(fmt.Sprintf("%s = ?", tag), value.Interface())
  57. }
  58. if tag == "updated_at" && value.Interface() != "0" {
  59. db = db.Where(fmt.Sprintf("%s > ?", tag), value.Interface())
  60. }
  61. if tag == "project_name" && !util.IsBlank(value) {
  62. db = db.Where(fmt.Sprintf("%s = ?", tag), value.Interface())
  63. }
  64. }
  65. // 查询总数
  66. var total int64
  67. var fullProjects []*gorm_model.ProjectInfo
  68. if err := db.Count(&total).Error; err != nil {
  69. logrus.WithContext(ctx).Errorf("[GetFullProjectList] error query mysql total, err:%+v", err)
  70. return nil, 0, err
  71. }
  72. // 查询该页数据
  73. limit := pageSize
  74. offset := pageSize * pageNum // assert pageNum start with 0
  75. err := db.Order("project_id").Limit(int(limit)).Offset(int(offset)).Find(&fullProjects).Error
  76. if err != nil {
  77. logrus.WithContext(ctx).Errorf("[GetFullProjectList] error query mysql total, err:%+v", err)
  78. return nil, 0, err
  79. }
  80. return fullProjects, total, nil
  81. }
  82. func GetProjectDraftList(ctx context.Context, enterpriseID int64, pageSize, pageNum int32, condition *common_model.ProjectCondition) ([]*gorm_model.ProjectInfo, int64, error) {
  83. db := GetReadDB(ctx)
  84. // 根据企业id过滤
  85. db = db.Debug().Model(gorm_model.ProjectInfo{}).Where("enterprise_id = ?", enterpriseID)
  86. // 根据Project条件过滤
  87. conditionType := reflect.TypeOf(condition).Elem()
  88. conditionValue := reflect.ValueOf(condition).Elem()
  89. for i := 0; i < conditionType.NumField(); i++ {
  90. field := conditionType.Field(i)
  91. tag := field.Tag.Get("condition")
  92. value := conditionValue.FieldByName(field.Name)
  93. if tag == "project_status" && util.IsBlank(value) {
  94. db = db.Where(fmt.Sprintf("project_status = 1"))
  95. }
  96. if !util.IsBlank(value) && tag != "updated_at" && tag != "project_name" {
  97. db = db.Where(fmt.Sprintf("%s = ?", tag), value.Interface())
  98. }
  99. if tag == "updated_at" && value.Interface() != "0" {
  100. db = db.Where(fmt.Sprintf("%s > ?", tag), value.Interface())
  101. }
  102. if tag == "project_name" && !util.IsBlank(value) {
  103. db = db.Where(fmt.Sprintf("%s = ?", tag), value.Interface())
  104. }
  105. }
  106. // 查询总数
  107. var total int64
  108. var projectDrafts []*gorm_model.ProjectInfo
  109. if err := db.Count(&total).Error; err != nil {
  110. logrus.WithContext(ctx).Errorf("[GetProjectDraftList] error query mysql total, err:%+v", err)
  111. return nil, 0, err
  112. }
  113. // 查询该页数据
  114. limit := pageSize
  115. offset := pageSize * pageNum // assert pageNum start with 0
  116. err := db.Order("project_id").Limit(int(limit)).Offset(int(offset)).Find(&projectDrafts).Error
  117. if err != nil {
  118. logrus.WithContext(ctx).Errorf("[GetProjectDraftList] error query mysql total, err:%+v", err)
  119. return nil, 0, err
  120. }
  121. return projectDrafts, total, nil
  122. }
  123. func GetProjectTaskList(ctx context.Context, projectID string, pageSize, pageNum int64, conditions *common_model.TaskConditions) ([]*http_model.ProjectTaskInfo, int64, error) {
  124. db := GetReadDB(ctx)
  125. // 查询task表信息
  126. db = db.Debug().Model(gorm_model.YoungeeTaskInfo{})
  127. // 根据Project条件过滤
  128. conditionType := reflect.TypeOf(conditions).Elem()
  129. conditionValue := reflect.ValueOf(conditions).Elem()
  130. for i := 0; i < conditionType.NumField(); i++ {
  131. field := conditionType.Field(i)
  132. tag := field.Tag.Get("condition")
  133. value := conditionValue.FieldByName(field.Name)
  134. if !util.IsBlank(value) && tag != "platform_nickname" {
  135. db = db.Where(fmt.Sprintf("%s = ?", tag), value.Interface())
  136. } else if tag == "platform_nickname" {
  137. continue
  138. }
  139. }
  140. var taskInfos []gorm_model.YoungeeTaskInfo
  141. db = db.Model(gorm_model.YoungeeTaskInfo{})
  142. // 查询总数
  143. var totalTask int64
  144. if err := db.Count(&totalTask).Error; err != nil {
  145. logrus.WithContext(ctx).Errorf("[GetProjectTaskList] error query mysql total, err:%+v", err)
  146. return nil, 0, err
  147. }
  148. db.Order("task_id").Find(&taskInfos)
  149. // 查询账号id
  150. var accountIds []int
  151. taskMap := make(map[int]gorm_model.YoungeeTaskInfo)
  152. for _, taskInfo := range taskInfos {
  153. accountIds = append(accountIds, taskInfo.AccountID)
  154. taskMap[taskInfo.AccountID] = taskInfo
  155. }
  156. db1 := GetReadDB(ctx)
  157. db1 = db1.Debug().Model(gorm_model.YoungeePlatformAccountInfo{})
  158. // 根据Project条件过滤
  159. conditionType2 := reflect.TypeOf(conditions).Elem()
  160. conditionValue2 := reflect.ValueOf(conditions).Elem()
  161. for i := 0; i < conditionType2.NumField(); i++ {
  162. field := conditionType2.Field(i)
  163. tag := field.Tag.Get("condition")
  164. value := conditionValue2.FieldByName(field.Name)
  165. if !util.IsBlank(value) && tag == "platform_nickname" { // input:1 taskIdsbase:1,2,3 string
  166. db1 = db1.Where(fmt.Sprintf("%s = ?", tag), value.Interface())
  167. }
  168. }
  169. var accountInfos []gorm_model.YoungeePlatformAccountInfo
  170. db1 = db1.Model(gorm_model.YoungeePlatformAccountInfo{}).Where("account_id IN ?", accountIds).Find(&accountInfos)
  171. // 查询总数
  172. var totalAccount int64
  173. if err := db1.Count(&totalAccount).Error; err != nil {
  174. logrus.WithContext(ctx).Errorf("[GetProjectTaskList] error query mysql total, err:%+v", err)
  175. return nil, 0, err
  176. }
  177. var misNum int64
  178. if totalAccount > totalTask {
  179. misNum = totalAccount - totalTask
  180. } else {
  181. misNum = totalTask - totalAccount
  182. }
  183. logrus.Println("totalAccount,totalTask,misNum:", totalAccount, totalTask, misNum)
  184. // 查询该页数据
  185. limit := pageSize + misNum
  186. offset := pageSize * pageNum // assert pageNum start with 0
  187. err := db.Order("task_id").Limit(int(limit)).Offset(int(offset)).Error
  188. if err != nil {
  189. logrus.WithContext(ctx).Errorf("[GetProjectTaskList] error query mysql total, err:%+v", err)
  190. return nil, 0, err
  191. }
  192. accountMap := make(map[int]gorm_model.YoungeePlatformAccountInfo)
  193. for _, accountInfo := range accountInfos {
  194. accountMap[accountInfo.AccountID] = accountInfo
  195. }
  196. var taskAccounts []*http_model.TaskAccount
  197. var projectTasks []*http_model.ProjectTaskInfo
  198. for _, accountId := range accountIds {
  199. taskAccount := new(http_model.TaskAccount)
  200. _, ok := taskMap[accountId]
  201. _, ok2 := accountMap[accountId]
  202. if ok && ok2 {
  203. taskAccount.Task = taskMap[accountId]
  204. taskAccount.Account = accountMap[accountId]
  205. }
  206. taskAccounts = append(taskAccounts, taskAccount)
  207. }
  208. projectTasks = pack.TaskAccountToTaskInfo(taskAccounts)
  209. var fullProjectTasks []*http_model.ProjectTaskInfo
  210. // 删除只存在于一个表中的元素
  211. for i := 0; i < len(projectTasks); i++ {
  212. if projectTasks[i].TaskID != 0 {
  213. fullProjectTasks = append(fullProjectTasks, projectTasks[i])
  214. }
  215. }
  216. var total int64
  217. if totalTask > totalAccount {
  218. total = totalAccount
  219. } else {
  220. total = totalTask
  221. }
  222. return fullProjectTasks, total, nil
  223. }
  224. func GetProjectDetail(ctx context.Context, projectID int64) (*gorm_model.ProjectInfo, error) {
  225. db := GetReadDB(ctx)
  226. var ProjectDetail *gorm_model.ProjectInfo
  227. err := db.Where("project_id = ?", projectID).First(&ProjectDetail).Error
  228. if err != nil {
  229. if err == gorm.ErrRecordNotFound {
  230. return nil, nil
  231. } else {
  232. return nil, err
  233. }
  234. }
  235. return ProjectDetail, nil
  236. }
  237. func GetProjectPhoto(ctx context.Context, ProjectID int64) ([]gorm_model.ProjectPhoto, error) {
  238. db := GetReadDB(ctx)
  239. ProjectPhoto := []gorm_model.ProjectPhoto{}
  240. err := db.Where("project_id=?", ProjectID).Find(&ProjectPhoto).Error
  241. if err != nil {
  242. return nil, err
  243. }
  244. return ProjectPhoto, nil
  245. }
  246. func GetRecruitStrategys(ctx context.Context, ProjectID int64) ([]gorm_model.RecruitStrategy, error) {
  247. db := GetReadDB(ctx)
  248. RecruitStrategys := []gorm_model.RecruitStrategy{}
  249. err := db.Where("project_id=?", ProjectID).Find(&RecruitStrategys).Error
  250. if err != nil {
  251. return nil, err
  252. }
  253. return RecruitStrategys, nil
  254. }
  255. func UpdateProjectStatus(ctx context.Context, projectId int64, status int64) error {
  256. db := GetReadDB(ctx)
  257. err := db.Model(gorm_model.ProjectInfo{}).
  258. Where("project_id = ?", projectId).Update("project_status", status).Error
  259. if err != nil {
  260. log.Println("DB UpdateProjectStatus error :", err)
  261. return err
  262. }
  263. return nil
  264. }
  265. func GetFeeDetail(ctx context.Context, enterpriseID int64, EndTime string) (*http_model.FeeDetailPreview, error) {
  266. db := GetReadDB(ctx)
  267. // 根据企业id过滤
  268. db = db.Debug().Model(gorm_model.ProjectInfo{}).Where("enterprise_id = ? AND project_status = 10", enterpriseID)
  269. if EndTime != "" {
  270. db = db.Where("updated_at like ?", EndTime+"%")
  271. }
  272. var projectInfos []gorm_model.ProjectInfo
  273. db = db.Order("updated_at desc").Find(&projectInfos)
  274. FeeDetailPreview := http_model.FeeDetailPreview{}
  275. for _, projectInfo := range projectInfos {
  276. FeeDetailData := new(http_model.FeeDetailData)
  277. FeeDetailData.ProjectID = projectInfo.ProjectID
  278. FeeDetailData.ProjectName = projectInfo.ProjectName
  279. FeeDetailData.ProjectType = consts.GetProjectType(projectInfo.ProjectType)
  280. FeeDetailData.Payment = conv.MustString(projectInfo.PaymentAmount, "")
  281. FeeDetailData.UpdatedAt = conv.MustString(projectInfo.UpdatedAt)[0:19]
  282. FeeDetailPreview.FeeDetailData = append(FeeDetailPreview.FeeDetailData, FeeDetailData)
  283. }
  284. return &FeeDetailPreview, nil
  285. }