project.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. package db
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/issue9/conv"
  6. "reflect"
  7. "strconv"
  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. "gorm.io/gorm"
  15. )
  16. func CreateProject(ctx context.Context, projectInfo gorm_model.ProjectInfo) (*int64, error) {
  17. db := GetWriteDB(ctx)
  18. err := db.Create(&projectInfo).Error
  19. if err != nil {
  20. return nil, err
  21. }
  22. return &projectInfo.ProjectID, nil
  23. }
  24. func UpdateProject(ctx context.Context, project gorm_model.ProjectInfo) (*int64, error) {
  25. db := GetReadDB(ctx)
  26. err := db.Model(&project).Updates(project).Error
  27. if err != nil {
  28. return nil, err
  29. }
  30. return &project.ProjectID, nil
  31. }
  32. func GetFullProjectList(ctx context.Context, enterpriseID int64, pageSize, pageNum int32, condition *common_model.ProjectCondition) ([]*gorm_model.ProjectInfo, int64, error) {
  33. db := GetReadDB(ctx)
  34. // 根据企业id过滤
  35. db = db.Debug().Model(gorm_model.ProjectInfo{}).Where("enterprise_id = ?", enterpriseID)
  36. // 根据Project条件过滤
  37. conditionType := reflect.TypeOf(condition).Elem()
  38. conditionValue := reflect.ValueOf(condition).Elem()
  39. for i := 0; i < conditionType.NumField(); i++ {
  40. field := conditionType.Field(i)
  41. tag := field.Tag.Get("condition")
  42. value := conditionValue.FieldByName(field.Name)
  43. if tag == "project_status" && util.IsBlank(value) {
  44. db = db.Where(fmt.Sprintf("project_status != 1"))
  45. }
  46. if !util.IsBlank(value) && tag != "updated_at" && tag != "project_name" {
  47. db = db.Where(fmt.Sprintf("%s = ?", tag), value.Interface())
  48. }
  49. if tag == "updated_at" && value.Interface() != "0" {
  50. db = db.Where(fmt.Sprintf("%s > ?", tag), value.Interface())
  51. }
  52. if tag == "project_name" && !util.IsBlank(value) {
  53. db = db.Where(fmt.Sprintf("%s = ?", tag), value.Interface())
  54. }
  55. }
  56. // 查询总数
  57. var total int64
  58. var fullProjects []*gorm_model.ProjectInfo
  59. if err := db.Count(&total).Error; err != nil {
  60. logrus.WithContext(ctx).Errorf("[GetFullProjectList] error query mysql total, err:%+v", err)
  61. return nil, 0, err
  62. }
  63. // 查询该页数据
  64. limit := pageSize
  65. offset := pageSize * pageNum // assert pageNum start with 0
  66. err := db.Order("project_id").Limit(int(limit)).Offset(int(offset)).Find(&fullProjects).Error
  67. if err != nil {
  68. logrus.WithContext(ctx).Errorf("[GetFullProjectList] error query mysql total, err:%+v", err)
  69. return nil, 0, err
  70. }
  71. return fullProjects, total, nil
  72. }
  73. func GetProjectTaskList(ctx context.Context, projectID string, pageSize, pageNum int64, conditions *common_model.TaskConditions) ([]*http_model.ProjectTaskInfo, int64, error) {
  74. db := GetReadDB(ctx)
  75. // 查询task表信息
  76. db = db.Debug().Model(gorm_model.YoungeeTaskInfo{})
  77. // 根据Project条件过滤
  78. conditionType := reflect.TypeOf(conditions).Elem()
  79. conditionValue := reflect.ValueOf(conditions).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 !util.IsBlank(value) && tag != "platform_nickname" {
  85. db = db.Where(fmt.Sprintf("%s = ?", tag), value.Interface())
  86. } else if tag == "platform_nickname" {
  87. continue
  88. }
  89. }
  90. var taskInfos []gorm_model.YoungeeTaskInfo
  91. db = db.Model(gorm_model.YoungeeTaskInfo{})
  92. // 查询总数
  93. var totalTask int64
  94. if err := db.Count(&totalTask).Error; err != nil {
  95. logrus.WithContext(ctx).Errorf("[GetProjectTaskList] error query mysql total, err:%+v", err)
  96. return nil, 0, err
  97. }
  98. db.Order("task_id").Find(&taskInfos)
  99. // 查询账号id
  100. var accountIds []int
  101. taskMap := make(map[int]gorm_model.YoungeeTaskInfo)
  102. for _, taskInfo := range taskInfos {
  103. accountIds = append(accountIds, taskInfo.AccountID)
  104. taskMap[taskInfo.AccountID] = taskInfo
  105. }
  106. db1 := GetReadDB(ctx)
  107. db1 = db1.Debug().Model(gorm_model.YoungeePlatformAccountInfo{})
  108. // 根据Project条件过滤
  109. conditionType2 := reflect.TypeOf(conditions).Elem()
  110. conditionValue2 := reflect.ValueOf(conditions).Elem()
  111. for i := 0; i < conditionType2.NumField(); i++ {
  112. field := conditionType2.Field(i)
  113. tag := field.Tag.Get("condition")
  114. value := conditionValue2.FieldByName(field.Name)
  115. if !util.IsBlank(value) && tag == "platform_nickname" { // input:1 database:1,2,3 string
  116. db1 = db1.Where(fmt.Sprintf("%s = ?", tag), value.Interface())
  117. }
  118. }
  119. var accountInfos []gorm_model.YoungeePlatformAccountInfo
  120. db1 = db1.Model(gorm_model.YoungeePlatformAccountInfo{}).Where("account_id IN ?", accountIds).Find(&accountInfos)
  121. // 查询总数
  122. var totalAccount int64
  123. if err := db1.Count(&totalAccount).Error; err != nil {
  124. logrus.WithContext(ctx).Errorf("[GetProjectTaskList] error query mysql total, err:%+v", err)
  125. return nil, 0, err
  126. }
  127. var misNum int64
  128. if totalAccount > totalTask {
  129. misNum = totalAccount - totalTask
  130. } else {
  131. misNum = totalTask - totalAccount
  132. }
  133. logrus.Println("totalAccount,totalTask,misNum:", totalAccount, totalTask, misNum)
  134. // 查询该页数据
  135. limit := pageSize + misNum
  136. offset := pageSize * pageNum // assert pageNum start with 0
  137. err := db.Order("task_id").Limit(int(limit)).Offset(int(offset)).Error
  138. if err != nil {
  139. logrus.WithContext(ctx).Errorf("[GetProjectTaskList] error query mysql total, err:%+v", err)
  140. return nil, 0, err
  141. }
  142. accountMap := make(map[int]gorm_model.YoungeePlatformAccountInfo)
  143. for _, accountInfo := range accountInfos {
  144. accountMap[accountInfo.AccountID] = accountInfo
  145. }
  146. var taskAccounts []*http_model.TaskAccount
  147. var projectTasks []*http_model.ProjectTaskInfo
  148. for _, accountId := range accountIds {
  149. taskAccount := new(http_model.TaskAccount)
  150. _, ok := taskMap[accountId]
  151. _, ok2 := accountMap[accountId]
  152. if ok && ok2 {
  153. taskAccount.Task = taskMap[accountId]
  154. taskAccount.Account = accountMap[accountId]
  155. }
  156. taskAccounts = append(taskAccounts, taskAccount)
  157. }
  158. projectTasks = pack.TaskAccountToTaskInfo(taskAccounts)
  159. var fullProjectTasks []*http_model.ProjectTaskInfo
  160. // 删除只存在于一个表中的元素
  161. for i := 0; i < len(projectTasks); i++ {
  162. if projectTasks[i].TaskID != 0 {
  163. fullProjectTasks = append(fullProjectTasks, projectTasks[i])
  164. }
  165. }
  166. var total int64
  167. if totalTask > totalAccount {
  168. total = totalAccount
  169. } else {
  170. total = totalTask
  171. }
  172. return fullProjectTasks, total, nil
  173. }
  174. func GetProjectDetail(ctx context.Context, projectID int64) (*gorm_model.ProjectInfo, error) {
  175. db := GetReadDB(ctx)
  176. var ProjectDetail *gorm_model.ProjectInfo
  177. err := db.Where("project_id = ?", projectID).First(&ProjectDetail).Error
  178. if err != nil {
  179. if err == gorm.ErrRecordNotFound {
  180. return nil, nil
  181. } else {
  182. return nil, err
  183. }
  184. }
  185. return ProjectDetail, nil
  186. }
  187. func GetProjectPhoto(ctx context.Context, ProjectID int64) ([]gorm_model.ProjectPhoto, error) {
  188. db := GetReadDB(ctx)
  189. ProjectPhoto := []gorm_model.ProjectPhoto{}
  190. err := db.Where("project_id=?", ProjectID).Find(&ProjectPhoto).Error
  191. if err != nil {
  192. return nil, err
  193. }
  194. return ProjectPhoto, nil
  195. }
  196. func GetRecruitStrategys(ctx context.Context, ProjectID int64) ([]gorm_model.RecruitStrategy, error) {
  197. db := GetReadDB(ctx)
  198. RecruitStrategys := []gorm_model.RecruitStrategy{}
  199. err := db.Where("project_id=?", ProjectID).Find(&RecruitStrategys).Error
  200. if err != nil {
  201. return nil, err
  202. }
  203. return RecruitStrategys, nil
  204. }
  205. func ChangeTaskStatus(ctx context.Context, data []string, taskStatus string) error {
  206. db := GetReadDB(ctx)
  207. taskInfo := gorm_model.YoungeeTaskInfo{}
  208. taskSta, err := strconv.Atoi(taskStatus)
  209. if err != nil {
  210. return err
  211. }
  212. if err := db.Debug().Model(&taskInfo).Where("task_id IN ?", data).
  213. Updates(gorm_model.YoungeeTaskInfo{TaskStatus: taskSta}).Error; err != nil {
  214. logrus.WithContext(ctx).Errorf("[ChangeTaskStatus] error query mysql total, err:%+v", err)
  215. return err
  216. }
  217. return nil
  218. }
  219. func GetProjectTalentList(ctx context.Context, projectID string, pageSize, pageNum int64, conditions *common_model.TaskConditions) ([]*http_model.ProjectTalentInfo, int64, error) {
  220. db := GetReadDB(ctx)
  221. // 查询Task表信息
  222. db = db.Debug().Model(gorm_model.YoungeeTaskInfo{})
  223. // 根据Project条件过滤
  224. conditionType := reflect.TypeOf(conditions).Elem()
  225. conditionValue := reflect.ValueOf(conditions).Elem()
  226. for i := 0; i < conditionType.NumField(); i++ {
  227. field := conditionType.Field(i)
  228. tag := field.Tag.Get("condition")
  229. value := conditionValue.FieldByName(field.Name)
  230. if !util.IsBlank(value) && tag != "platform_nickname" {
  231. db = db.Where(fmt.Sprintf("%s = ?", tag), value.Interface())
  232. } else if tag == "platform_nickname" {
  233. continue
  234. }
  235. }
  236. var taskInfos []gorm_model.YoungeeTaskInfo
  237. db = db.Model(gorm_model.YoungeeTaskInfo{})
  238. // 查询总数
  239. var totalTask int64
  240. if err := db.Count(&totalTask).Error; err != nil {
  241. logrus.WithContext(ctx).Errorf("[GetProjectTaskList] error query mysql total, err:%+v", err)
  242. return nil, 0, err
  243. }
  244. db.Order("task_id").Find(&taskInfos)
  245. // 查询账号id
  246. var taskIds []int
  247. taskMap := make(map[int]gorm_model.YoungeeTaskInfo)
  248. for _, taskInfo := range taskInfos {
  249. taskIds = append(taskIds, taskInfo.TaskID)
  250. taskMap[taskInfo.TaskID] = taskInfo
  251. }
  252. db1 := GetReadDB(ctx)
  253. db1 = db1.Debug().Model(gorm_model.YoungeeTaskLogistics{})
  254. // 根据Project条件过滤
  255. conditionType2 := reflect.TypeOf(conditions).Elem()
  256. conditionValue2 := reflect.ValueOf(conditions).Elem()
  257. for i := 0; i < conditionType2.NumField(); i++ {
  258. field := conditionType2.Field(i)
  259. tag := field.Tag.Get("condition")
  260. value := conditionValue2.FieldByName(field.Name)
  261. if !util.IsBlank(value) && tag == "platform_nickname" { // input:1 database:1,2,3 string
  262. db1 = db1.Where(fmt.Sprintf("%s = ?", tag), value.Interface())
  263. }
  264. }
  265. var logisticsInfos []gorm_model.YoungeeTaskLogistics
  266. db1 = db1.Model(gorm_model.YoungeeTaskLogistics{}).Where("task_id IN ?", taskIds).Find(&logisticsInfos)
  267. logisticsMap := make(map[int]gorm_model.YoungeeTaskLogistics)
  268. for _, logisticsInfo := range logisticsInfos {
  269. logisticsMap[conv.MustInt(logisticsInfo.TaskID)] = logisticsInfo
  270. }
  271. /*
  272. db1 := GetReadDB(ctx)
  273. db1 = db1.Debug().Model(gorm_model.YoungeePlatformAccountInfo{})
  274. // 根据Project条件过滤
  275. conditionType2 := reflect.TypeOf(conditions).Elem()
  276. conditionValue2 := reflect.ValueOf(conditions).Elem()
  277. for i := 0; i < conditionType2.NumField(); i++ {
  278. field := conditionType2.Field(i)
  279. tag := field.Tag.Get("condition")
  280. value := conditionValue2.FieldByName(field.Name)
  281. if !util.IsBlank(value) && tag == "platform_nickname" { // input:1 database:1,2,3 string
  282. db1 = db1.Where(fmt.Sprintf("%s = ?", tag), value.Interface())
  283. }
  284. }
  285. var accountInfos []gorm_model.YoungeePlatformAccountInfo
  286. db1 = db1.Model(gorm_model.YoungeePlatformAccountInfo{}).Where("account_id IN ?", accountIds).Find(&accountInfos)
  287. */
  288. // 查询总数
  289. var totalAccount int64
  290. if err := db1.Count(&totalAccount).Error; err != nil {
  291. logrus.WithContext(ctx).Errorf("[GetProjectTalentList] error query mysql total, err:%+v", err)
  292. return nil, 0, err
  293. }
  294. var misNum int64
  295. if totalAccount > totalTask {
  296. misNum = totalAccount - totalTask
  297. } else {
  298. misNum = totalTask - totalAccount
  299. }
  300. logrus.Println("totalAccount,totalTalent,misNum:", totalAccount, totalTask, misNum)
  301. // 查询该页数据
  302. limit := pageSize + misNum
  303. offset := pageSize * pageNum // assert pageNum start with 0
  304. err := db.Order("task_id").Limit(int(limit)).Offset(int(offset)).Error
  305. if err != nil {
  306. logrus.WithContext(ctx).Errorf("[GetProjectTaskList] error query mysql total, err:%+v", err)
  307. return nil, 0, err
  308. }
  309. /*
  310. accountMap := make(map[int]gorm_model.YoungeePlatformAccountInfo)
  311. for _, accountInfo := range accountInfos {
  312. accountMap[accountInfo.AccountID] = accountInfo
  313. }
  314. */
  315. var talentAccounts []*http_model.TalentAccount
  316. var projectTalents []*http_model.ProjectTalentInfo
  317. for _, taskId := range taskIds {
  318. talentAccount := new(http_model.TalentAccount)
  319. _, ok := taskMap[taskId]
  320. _, ok2 := logisticsMap[taskId]
  321. if ok && ok2 {
  322. talentAccount.Talent = taskMap[taskId]
  323. talentAccount.Logistics = logisticsMap[taskId]
  324. }
  325. talentAccounts = append(talentAccounts, talentAccount)
  326. }
  327. projectTalents = pack.TalentAccountToTaskInfo(talentAccounts)
  328. var fullProjectTalents []*http_model.ProjectTalentInfo
  329. // 删除只存在于一个表中的元素
  330. for i := 0; i < len(projectTalents); i++ {
  331. if projectTalents[i].TaskID != 0 {
  332. fullProjectTalents = append(fullProjectTalents, projectTalents[i])
  333. }
  334. }
  335. var total int64
  336. if totalTask > totalAccount {
  337. total = totalAccount
  338. } else {
  339. total = totalTask
  340. }
  341. return fullProjectTalents, total, nil
  342. }