project_dao.go 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  1. package dao
  2. import (
  3. "errors"
  4. "fmt"
  5. "gorm.io/gorm"
  6. "time"
  7. "youngee_m_api/app/entity"
  8. "youngee_m_api/app/vo"
  9. )
  10. type ProjectDAO struct{}
  11. // 根据projectId获取project信息
  12. func (d ProjectDAO) GetProjectById(projectId string) (*entity.Project, error) {
  13. var project entity.Project
  14. err := Db.Where("project_id = ?", projectId).First(&project).Error
  15. if err != nil {
  16. if errors.Is(err, gorm.ErrRecordNotFound) {
  17. return nil, nil
  18. } else {
  19. return nil, err
  20. }
  21. }
  22. return &project, err
  23. }
  24. // 根据projectId获取违约状态id
  25. func (d ProjectDAO) GetAutoDefaultId(projectId string) (*int64, error) {
  26. var autoDefaultId int64
  27. err := Db.Model(&entity.Project{}).Where("project_id = ?", projectId).Select("auto_default_id").Find(&autoDefaultId).Error
  28. if err != nil {
  29. return nil, nil
  30. }
  31. return &autoDefaultId, nil
  32. }
  33. // 根据enterpriseId查询指定某天的所有带货数据
  34. func (d ProjectDAO) GetProjectListOfDay(enterpriseId string, date time.Time) ([]entity.Project, error) {
  35. var projects []entity.Project
  36. // 构建查询
  37. query := Db.Model(&entity.Project{})
  38. if enterpriseId != "" {
  39. query = query.Where("enterprise_id = ?", enterpriseId)
  40. }
  41. // 将日期部分提取出来进行匹配
  42. query = query.Where("DATE(created_at) = ?", date.Format("2006-01-02"))
  43. err := query.Find(&projects).Error
  44. return projects, err
  45. }
  46. // 创建种草任务
  47. func (d ProjectDAO) CreateProject(project entity.Project) error {
  48. err := Db.Omit("auto_fail_at", "auto_script_break_at", "auto_sketch_break_at", "pay_at", "pass_at", "finish_at", "submit_at", "fail_at").Create(&project).Error
  49. if err != nil {
  50. return err
  51. }
  52. return nil
  53. }
  54. // 更新种草任务
  55. func (d ProjectDAO) UpdateProject(project entity.Project) error {
  56. err := Db.Model(&entity.Project{}).Where("project_id = ?", project.ProjectId).Updates(project).Error
  57. if err != nil {
  58. return err
  59. }
  60. return nil
  61. }
  62. // 更新开票状态字段
  63. func (d ProjectDAO) UpdateInvoiceStatus(projectIDs []string) error {
  64. err := Db.Debug().Model(&entity.Project{}).Where("project_id IN ?", projectIDs).Updates(entity.Project{InvoiceStatus: 1}).Error
  65. if err != nil {
  66. return err
  67. }
  68. return nil
  69. }
  70. // 获取种草任务列表
  71. func (d ProjectDAO) GetProjectPreviews(param *vo.ProjectSearchParam) ([]vo.ReProjectTaskPreview, int64, error) {
  72. var reProjectTaskPreviews []vo.ReProjectTaskPreview
  73. var projects []entity.Project
  74. var total int64
  75. query := Db.Model(&entity.Project{})
  76. // 动态添加查询条件
  77. //if param.SubAccountId == 0 {
  78. // if param.EnterpriseId == "" {
  79. // return reProjectTaskPreviews, 0, errors.New("enterpriseId is empty")
  80. // }
  81. // query = query.Where("enterprise_id = ?", param.EnterpriseId)
  82. //} else {
  83. // query = query.Where("sub_account_id = ?", param.SubAccountId)
  84. //}
  85. if param.ProjectType != 0 {
  86. query = query.Where("project_type = ?", param.ProjectType)
  87. }
  88. if param.ProjectPlatform != 0 {
  89. query = query.Where("project_platform = ?", param.ProjectPlatform)
  90. }
  91. if param.ProjectStatus != 0 {
  92. query = query.Where("project_status = ?", param.ProjectStatus)
  93. } else {
  94. query = query.Where("project_status not in ?", []int{1, 3})
  95. }
  96. if param.ProjectForm != 0 {
  97. query = query.Where("project_form = ?", param.ProjectForm)
  98. }
  99. if param.ContentType != 0 {
  100. query = query.Where("content_type = ?", param.ContentType)
  101. }
  102. if param.Others != "" {
  103. query = query.Where("enterprise_id = ? or project_id = ? or project_name LIKE ?", param.Others, param.Others, "%"+param.Others+"%")
  104. }
  105. query.Count(&total)
  106. query = query.Select("enterprise_id, sub_account_id, project_id, project_name, project_platform, project_status, estimated_cost, need_pay, project_form, content_type, need_review, need_quality, need_calculate, before_delivery_num, delivery_num, after_delivery_num, product_id, tools")
  107. offset := (param.Page - 1) * param.PageSize
  108. if param.Order == 1 {
  109. if err := query.Order("created_at asc").Offset(offset).Limit(param.PageSize).Find(&projects).Error; err != nil {
  110. return nil, 0, err
  111. }
  112. } else {
  113. if err := query.Order("created_at desc").Offset(offset).Limit(param.PageSize).Find(&projects).Error; err != nil {
  114. return nil, 0, err
  115. }
  116. }
  117. for _, project := range projects {
  118. // 获取种草待筛选达人数
  119. projectId := project.ProjectId
  120. needFilter := ProjectTaskInfoDao{}.CountByTaskStage(projectId, 1)
  121. reProjectTaskPreview := vo.ReProjectTaskPreview{
  122. EnterpriseId: project.EnterpriseID,
  123. SubAccountId: project.SubAccountId,
  124. ProjectId: project.ProjectId,
  125. ProjectName: project.ProjectName,
  126. ProjectPlatform: project.ProjectPlatform,
  127. ProjectStatus: project.ProjectStatus,
  128. ProjectForm: project.ProjectForm,
  129. ContentType: project.ContentType,
  130. NeedReview: project.NeedReview,
  131. NeedQuality: project.NeedQuality,
  132. NeedCalculate: project.NeedCalculate,
  133. ProductId: project.ProductID,
  134. Tools: project.Tools,
  135. NeedDelivery: project.BeforeDeliveryNum,
  136. NeedReceive: project.DeliveryNum,
  137. Received: project.AfterDeliveryNum,
  138. NeedFilter: needFilter,
  139. }
  140. if project.ProjectStatus < 6 {
  141. reProjectTaskPreview.EstimatedCost = project.EstimatedCost
  142. } else {
  143. reProjectTaskPreview.EstimatedCost = project.NeedPay
  144. }
  145. reProjectTaskPreviews = append(reProjectTaskPreviews, reProjectTaskPreview)
  146. }
  147. return reProjectTaskPreviews, total, nil
  148. }
  149. // 删除种草任务
  150. func (d ProjectDAO) DeleteProject(projectId string) (*string, error) {
  151. if projectId == "" {
  152. return &projectId, nil
  153. }
  154. err := Db.Model(&entity.Project{}).Where("project_id = ?", projectId).Delete(&entity.Project{}).Error
  155. if err != nil {
  156. return nil, err
  157. }
  158. return &projectId, nil
  159. }
  160. // 获取草稿箱——种草任务列表
  161. func (d ProjectDAO) GetProjectDraftList(param *vo.ProjectDraftParam) ([]vo.ReProjectTaskPreview, int64, error) {
  162. var reProjectTaskPreviews []vo.ReProjectTaskPreview
  163. var projects []entity.Project
  164. var total int64
  165. query := Db.Model(&entity.Project{}).Where("project_status = ?", 1)
  166. // 动态添加查询条件
  167. if param.SubAccountId == 0 {
  168. if param.EnterpriseId == "" {
  169. return reProjectTaskPreviews, 0, errors.New("enterpriseId is empty")
  170. }
  171. query = query.Where("enterprise_id = ?", param.EnterpriseId)
  172. } else {
  173. query = query.Where("sub_account_id = ?", param.SubAccountId)
  174. }
  175. if param.ProjectType != 0 {
  176. query = query.Where("project_type = ?", param.ProjectType)
  177. }
  178. if param.ProjectPlatform != 0 {
  179. query = query.Where("project_platform = ?", param.ProjectPlatform)
  180. }
  181. if param.Others != "" {
  182. query = query.Where("enterprise_id = ? or project_id = ? or project_name LIKE ?", param.Others, param.Others, "%"+param.Others+"%")
  183. }
  184. query.Count(&total)
  185. query = query.Select("enterprise_id, sub_account_id, project_id, project_name, project_platform, project_type, created_at, product_id")
  186. offset := (param.Page - 1) * param.PageSize
  187. if err := query.Order("created_at asc").Offset(offset).Limit(param.PageSize).Find(&projects).Error; err != nil {
  188. return nil, 0, err
  189. }
  190. for _, project := range projects {
  191. reProjectTaskPreview := vo.ReProjectTaskPreview{
  192. EnterpriseId: project.EnterpriseID,
  193. SubAccountId: project.SubAccountId,
  194. ProjectId: project.ProjectId,
  195. ProjectName: project.ProjectName,
  196. ProjectPlatform: project.ProjectPlatform,
  197. ProjectType: project.ProjectType,
  198. CreatedAt: project.CreatedAt.Format("2006-01-02 15:04:05"),
  199. ProductId: project.ProductID,
  200. }
  201. reProjectTaskPreviews = append(reProjectTaskPreviews, reProjectTaskPreview)
  202. }
  203. return reProjectTaskPreviews, total, nil
  204. }
  205. // 获取公开种草中全部指定状态值的项目
  206. func (d ProjectDAO) GetProjectList(value int64, fieldName string) ([]*entity.Project, error) {
  207. var projectInfos []*entity.Project
  208. err := Db.Model(entity.Project{}).Where(fmt.Sprintf("%s = ? ", fieldName), value).Find(&projectInfos).Error
  209. if err != nil {
  210. return nil, err
  211. }
  212. return projectInfos, nil
  213. }
  214. // 违约管理——违约公开种草任务列表
  215. func (d ProjectDAO) GetProjectPublicList(param *vo.DefaultSearchParam) ([]vo.ReTaskDefaultPublic, int64, error) {
  216. var reTaskDefaultPublics []vo.ReTaskDefaultPublic
  217. var projects []entity.Project
  218. var total int64
  219. query := Db.Model(&entity.Project{}).Where("project_type = ?", 1)
  220. // 动态添加查询条件
  221. //if param.SubAccountId == 0 {
  222. // if param.EnterpriseId == "" {
  223. // return reTaskDefaultPublics, 0, errors.New("enterpriseId is empty")
  224. // }
  225. // query = query.Where("enterprise_id = ?", param.EnterpriseId)
  226. //} else {
  227. // query = query.Where("sub_account_id = ?", param.SubAccountId)
  228. //}
  229. if param.Platform != 0 {
  230. query = query.Where("project_platform = ?", param.Platform)
  231. }
  232. if param.TaskId != "" {
  233. query = query.Where("project_id = ?", param.TaskId)
  234. }
  235. query.Count(&total)
  236. query = query.Select("enterprise_id, sub_account_id, project_id, project_name, project_platform, project_form, content_type, product_id")
  237. offset := (param.Page - 1) * param.PageSize
  238. if err := query.Order("created_at asc").Offset(offset).Limit(param.PageSize).Find(&projects).Error; err != nil {
  239. return nil, 0, err
  240. }
  241. for _, project := range projects {
  242. reTaskDefaultPublic := vo.ReTaskDefaultPublic{
  243. EnterpriseId: project.EnterpriseID,
  244. SubAccountId: project.SubAccountId,
  245. TaskId: project.ProjectId,
  246. TaskName: project.ProjectName,
  247. Platform: project.ProjectPlatform,
  248. TaskForm: project.ProjectForm,
  249. ContentType: project.ContentType,
  250. TaskType: 1,
  251. ProductId: project.ProductID,
  252. }
  253. reTaskDefaultPublics = append(reTaskDefaultPublics, reTaskDefaultPublic)
  254. }
  255. return reTaskDefaultPublics, total, nil
  256. }
  257. // 违约管理——违约定向种草任务列表
  258. func (d ProjectDAO) GetProjectTargetList(param *vo.DefaultSearchParam) ([]vo.ReTaskDefaultTarget, int64, error) {
  259. var reTaskDefaultTargets []vo.ReTaskDefaultTarget
  260. var projects []entity.Project
  261. var total int64
  262. query := Db.Model(&entity.Project{}).Where("project_type = ?", 2)
  263. // 动态添加查询条件
  264. //if param.SubAccountId == 0 {
  265. // if param.EnterpriseId == "" {
  266. // return reTaskDefaultTargets, 0, errors.New("enterpriseId is empty")
  267. // }
  268. // query = query.Where("enterprise_id = ?", param.EnterpriseId)
  269. //} else {
  270. // query = query.Where("sub_account_id = ?", param.SubAccountId)
  271. //}
  272. if param.Platform != 0 {
  273. query = query.Where("project_platform = ?", param.Platform)
  274. }
  275. if param.TaskId != "" {
  276. query = query.Where("project_id = ?", param.TaskId)
  277. }
  278. query.Count(&total)
  279. query = query.Select("enterprise_id, sub_account_id, project_id, project_platform, tools, content_type, product_id")
  280. offset := (param.Page - 1) * param.PageSize
  281. if err := query.Order("created_at asc").Offset(offset).Limit(param.PageSize).Find(&projects).Error; err != nil {
  282. return nil, 0, err
  283. }
  284. for _, project := range projects {
  285. reTaskDefaultPublic := vo.ReTaskDefaultTarget{
  286. EnterpriseId: project.EnterpriseID,
  287. SubAccountId: project.SubAccountId,
  288. TaskId: project.ProjectId,
  289. Platform: project.ProjectPlatform,
  290. Tools: project.Tools,
  291. ContentType: project.ContentType,
  292. TaskType: 1,
  293. ProductId: project.ProductID,
  294. }
  295. reTaskDefaultTargets = append(reTaskDefaultTargets, reTaskDefaultPublic)
  296. }
  297. return reTaskDefaultTargets, total, nil
  298. }
  299. // 获取品牌种草冻结中的任务
  300. func (d ProjectDAO) GetProjectFrozenList(enterpriseId string) ([]*entity.Project, error) {
  301. var projects []*entity.Project
  302. query := Db.Debug().Model(entity.Project{})
  303. query.Select("project_id, product_id, enterprise_id, sub_account_id, project_platform, payment_amount, pay_at") // 冻结金额:payment_amount
  304. err := query.Where(fmt.Sprintf("enterprise_id = ? AND project_type = ? AND (project_status between 7 and 8) "), enterpriseId, 1).Find(&projects).Error
  305. if err != nil {
  306. if errors.Is(err, gorm.ErrRecordNotFound) {
  307. return projects, nil
  308. } else {
  309. return nil, err
  310. }
  311. }
  312. return projects, nil
  313. }
  314. // 获取所有品牌种草冻结中的任务
  315. func (d ProjectDAO) GetProjectFrozenListAll() ([]*entity.Project, error) {
  316. var projects []*entity.Project
  317. query := Db.Debug().Model(entity.Project{})
  318. query.Select("project_id, product_id, enterprise_id, sub_account_id, project_platform, payment_amount, pay_at") // 冻结金额:payment_amount
  319. err := query.Where(fmt.Sprintf("project_type = ? AND (project_status between 7 and 8) "), 1).Find(&projects).Error
  320. if err != nil {
  321. if errors.Is(err, gorm.ErrRecordNotFound) {
  322. return projects, nil
  323. } else {
  324. return nil, err
  325. }
  326. }
  327. return projects, nil
  328. }
  329. // 获取品牌种草冻结解除的任务
  330. func (d ProjectDAO) GetProjectFrozenCancelList(enterpriseId string) ([]*entity.Project, error) {
  331. var projects []*entity.Project
  332. query := Db.Debug().Model(entity.Project{})
  333. query.Select("project_id, product_id, enterprise_id, sub_account_id, project_platform, settlement_amount, pay_at") // 解冻金额:settlement_amount
  334. err := query.Where(fmt.Sprintf("enterprise_id = ? AND project_type = ? AND (project_status between 9 and 10) "), enterpriseId, 1).Find(&projects).Error
  335. if err != nil {
  336. if errors.Is(err, gorm.ErrRecordNotFound) {
  337. return projects, nil
  338. } else {
  339. return nil, err
  340. }
  341. }
  342. return projects, nil
  343. }
  344. // 获取所有品牌种草冻结解除的任务
  345. func (d ProjectDAO) GetProjectFrozenCancelListAll() ([]*entity.Project, error) {
  346. var projects []*entity.Project
  347. query := Db.Debug().Model(entity.Project{})
  348. query.Select("project_id, product_id, enterprise_id, sub_account_id, project_platform, settlement_amount, pay_at") // 解冻金额:settlement_amount
  349. err := query.Where(fmt.Sprintf("project_type = ? AND (project_status between 9 and 10) "), 1).Find(&projects).Error
  350. if err != nil {
  351. if errors.Is(err, gorm.ErrRecordNotFound) {
  352. return projects, nil
  353. } else {
  354. return nil, err
  355. }
  356. }
  357. return projects, nil
  358. }
  359. // 获取品牌种草账单列表
  360. func (d ProjectDAO) GetBillProjectPreviews(param *vo.ProjectSearchParam) ([]vo.ReBillProjectTaskPreview, int64, error) {
  361. var reBillProjectTaskPreviews []vo.ReBillProjectTaskPreview
  362. var projects []entity.Project
  363. var total int64
  364. query := Db.Model(&entity.Project{})
  365. // 动态添加查询条件
  366. //if param.SubAccountId == 0 {
  367. // if param.EnterpriseId == "" {
  368. // return reBillProjectTaskPreviews, 0, errors.New("enterpriseId is empty")
  369. // }
  370. // query = query.Where("enterprise_id = ?", param.EnterpriseId)
  371. //} else {
  372. // query = query.Where("sub_account_id = ?", param.SubAccountId)
  373. //}
  374. if param.ProjectType != 0 {
  375. query = query.Where("project_type = ?", param.ProjectType)
  376. }
  377. if param.ProjectPlatform != 0 {
  378. query = query.Where("project_platform = ?", param.ProjectPlatform)
  379. }
  380. if param.ProjectStatus != 0 {
  381. query = query.Where("project_status = ?", param.ProjectStatus)
  382. }
  383. if param.Others != "" {
  384. query = query.Where("enterprise_id = ? or project_id = ? or project_name LIKE ?", param.Others, param.Others, "%"+param.Others+"%")
  385. }
  386. query.Count(&total)
  387. query = query.Select("enterprise_id, sub_account_id, project_id, project_name, project_platform, project_status, estimated_cost, need_pay, project_form, content_type, product_id, settlement_amount")
  388. offset := (param.Page - 1) * param.PageSize
  389. if err := query.Order("created_at asc").Offset(offset).Limit(param.PageSize).Find(&projects).Error; err != nil {
  390. return nil, 0, err
  391. }
  392. for _, project := range projects {
  393. reBillProjectTaskPreview := vo.ReBillProjectTaskPreview{
  394. EnterpriseId: project.EnterpriseID,
  395. SubAccountId: project.SubAccountId,
  396. ProjectId: project.ProjectId,
  397. ProjectName: project.ProjectName,
  398. ProjectPlatform: project.ProjectPlatform,
  399. ProjectStatus: project.ProjectStatus,
  400. ProjectForm: project.ProjectForm,
  401. ContentType: project.ContentType,
  402. ProductId: project.ProductID,
  403. EstimatedCost: project.EstimatedCost, // 应付金额
  404. CashAmount: project.SettlementAmount, //实际结算金额
  405. }
  406. if project.ProjectStatus < 6 {
  407. reBillProjectTaskPreview.EstimatedCost = project.EstimatedCost
  408. } else {
  409. reBillProjectTaskPreview.EstimatedCost = project.NeedPay
  410. }
  411. reBillProjectTaskPreviews = append(reBillProjectTaskPreviews, reBillProjectTaskPreview)
  412. }
  413. return reBillProjectTaskPreviews, total, nil
  414. }
  415. // 种草任务待办
  416. func (d ProjectDAO) GetProjectToDo(enterpriseId string, subAccountId int64, platform int64, taskType int64) (map[string]int64, error) {
  417. resultMap := make(map[string]int64)
  418. var needReview int64 // 待审核
  419. var needPay int64
  420. var needProcess int64
  421. var needCheck int64 // 初稿待审
  422. var needQuality int64 // 链接待审
  423. var needCalculate int64 // 待结算
  424. var projectInfos []entity.Project
  425. //query := Db.Model(&entity.Project{}).Where("enterprise_id = ? and project_platform = ? and project_type = ?", enterpriseId, platform, taskType)
  426. if subAccountId == 0 {
  427. // 待审核、待支付、达人未处理、初稿待审、链接待审、待结算
  428. query1 := Db.Model(&entity.Project{}).Where("enterprise_id = ? and project_platform = ? and project_type = ?", enterpriseId, platform, taskType)
  429. query1.Where("project_status = ?", 2).Count(&needReview)
  430. query2 := Db.Model(&entity.Project{}).Where("enterprise_id = ? and project_platform = ? and project_type = ?", enterpriseId, platform, taskType)
  431. query2.Debug().Where("project_status = ?", 6).Count(&needPay)
  432. query3 := Db.Model(&entity.Project{}).Where("enterprise_id = ? and project_platform = ? and project_type = ?", enterpriseId, platform, taskType)
  433. err := query3.Where("project_status = ?", 8).Select("project_id").Find(&projectInfos).Error
  434. if err != nil {
  435. if errors.Is(err, gorm.ErrRecordNotFound) {
  436. needProcess = 0
  437. } else {
  438. return resultMap, err
  439. }
  440. } else if len(projectInfos) > 0 {
  441. var projectIDs []string
  442. for _, info := range projectInfos {
  443. projectIDs = append(projectIDs, info.ProjectId)
  444. }
  445. if len(projectIDs) > 0 {
  446. err1 := Db.Model(&entity.ProjectTaskInfo{}).Where("project_id in ? and task_status = ?", projectIDs, 1).Count(&needProcess).Error // task_status=1待选
  447. if err1 != nil {
  448. needProcess = 0
  449. }
  450. }
  451. }
  452. query4 := Db.Model(&entity.Project{}).Where("enterprise_id = ? and project_platform = ? and project_type = ?", enterpriseId, platform, taskType)
  453. err1 := query4.Select("need_review, need_quality, need_calculate").Find(&projectInfos).Error
  454. if err1 != nil {
  455. return resultMap, err1
  456. } else if len(projectInfos) > 0 {
  457. for _, info := range projectInfos {
  458. needCheck += info.NeedReview
  459. needQuality += info.NeedQuality
  460. needCalculate += info.NeedCalculate
  461. }
  462. }
  463. } else {
  464. // 待审核、待支付、达人未处理、初稿待审、链接待审、待结算
  465. query1 := Db.Model(&entity.Project{}).Where("enterprise_id = ? and project_platform = ? and project_type = ?", enterpriseId, platform, taskType)
  466. query1.Where("sub_account_id = ? and project_status = ?", subAccountId, 2).Count(&needReview)
  467. query2 := Db.Model(&entity.Project{}).Where("enterprise_id = ? and project_platform = ? and project_type = ?", enterpriseId, platform, taskType)
  468. query2.Where("sub_account_id = ? and project_status = ?", subAccountId, 6).Count(&needPay)
  469. query3 := Db.Model(&entity.Project{}).Where("enterprise_id = ? and project_platform = ? and project_type = ?", enterpriseId, platform, taskType)
  470. err := query3.Where("sub_account_id = ? and project_status = ?", subAccountId, 8).Select("project_id").Find(&projectInfos).Error
  471. if err != nil {
  472. if errors.Is(err, gorm.ErrRecordNotFound) {
  473. needProcess = 0
  474. } else {
  475. return resultMap, err
  476. }
  477. } else {
  478. var projectIDs []string
  479. for _, info := range projectInfos {
  480. projectIDs = append(projectIDs, info.ProjectId)
  481. }
  482. if len(projectIDs) > 0 {
  483. err1 := Db.Model(&entity.ProjectTaskInfo{}).Where("project_id in ? and task_status = ?", projectIDs, 1).Count(&needProcess).Error // task_status=1待选
  484. if err1 != nil {
  485. needProcess = 0
  486. }
  487. }
  488. }
  489. query4 := Db.Model(&entity.Project{}).Where("enterprise_id = ? and project_platform = ? and project_type = ?", enterpriseId, platform, taskType)
  490. err1 := query4.Select("need_review, need_quality, need_calculate").Find(&projectInfos).Error
  491. if err1 != nil {
  492. return resultMap, err1
  493. } else if len(projectInfos) > 0 {
  494. for _, info := range projectInfos {
  495. needCheck += info.NeedReview
  496. needQuality += info.NeedQuality
  497. needCalculate += info.NeedCalculate
  498. }
  499. }
  500. }
  501. resultMap["needReview"] = needReview
  502. resultMap["needPay"] = needPay
  503. resultMap["needProcess"] = needProcess
  504. resultMap["needCheck"] = needCheck
  505. resultMap["needQuality"] = needQuality
  506. resultMap["needCalculate"] = needCalculate
  507. return resultMap, nil
  508. }
  509. // 寄样物流任务待办
  510. func (d ProjectDAO) GetLogisticsToDo(enterpriseId string, subAccountId int64, platform int64) (map[string]int64, error) {
  511. resultMap := make(map[string]int64)
  512. var needDelivery int64
  513. var needReceive int64
  514. var projectInfos []entity.Project
  515. //query := Db.Model(&entity.Project{}).Where("enterprise_id = ? and project_platform = ? and project_type = ?", enterpriseId, platform, taskType)
  516. if subAccountId == 0 {
  517. // 待发货、待签收
  518. query1 := Db.Model(&entity.Project{}).Where("enterprise_id = ? and project_platform = ? and project_form = ?", enterpriseId, platform, 1)
  519. err := query1.Where("project_status = ?", 8).Select("project_id").Find(&projectInfos).Error
  520. if err != nil {
  521. if errors.Is(err, gorm.ErrRecordNotFound) {
  522. needDelivery = 0
  523. needReceive = 0
  524. } else {
  525. return resultMap, err
  526. }
  527. } else if len(projectInfos) > 0 {
  528. var projectIDs []string
  529. for _, info := range projectInfos {
  530. projectIDs = append(projectIDs, info.ProjectId)
  531. }
  532. if len(projectIDs) > 0 {
  533. err1 := Db.Model(&entity.ProjectTaskInfo{}).Where("project_id in ? and task_stage = ?", projectIDs, 4).Count(&needDelivery).Error // task_stage=4待发货
  534. if err1 != nil {
  535. needDelivery = 0
  536. }
  537. err2 := Db.Model(&entity.ProjectTaskInfo{}).Where("project_id in ? and task_stage = ?", projectIDs, 5).Count(&needReceive).Error // task_stage=5已发货
  538. if err2 != nil {
  539. needReceive = 0
  540. }
  541. }
  542. }
  543. } else {
  544. // 待发货、待签收
  545. query1 := Db.Model(&entity.Project{}).Where("enterprise_id = ? and project_platform = ? and project_form = ?", enterpriseId, platform, 1)
  546. err := query1.Where("sub_account_id = ? and project_status = ?", subAccountId, 8).Select("project_id").Find(&projectInfos).Error
  547. if err != nil {
  548. if errors.Is(err, gorm.ErrRecordNotFound) {
  549. needDelivery = 0
  550. needReceive = 0
  551. } else {
  552. return resultMap, err
  553. }
  554. } else {
  555. var projectIDs []string
  556. for _, info := range projectInfos {
  557. projectIDs = append(projectIDs, info.ProjectId)
  558. }
  559. if len(projectIDs) > 0 {
  560. err1 := Db.Model(&entity.ProjectTaskInfo{}).Where("project_id in ? and task_stage = ?", projectIDs, 4).Count(&needDelivery).Error // task_stage=4待发货
  561. if err1 != nil {
  562. needDelivery = 0
  563. }
  564. err2 := Db.Model(&entity.ProjectTaskInfo{}).Where("project_id in ? and task_stage = ?", projectIDs, 5).Count(&needReceive).Error // task_stage=5已发货
  565. if err2 != nil {
  566. needReceive = 0
  567. }
  568. }
  569. }
  570. }
  571. resultMap["needDelivery"] = needDelivery
  572. resultMap["needReceive"] = needReceive
  573. return resultMap, nil
  574. }
  575. // 违约管理任务待办
  576. func (d ProjectDAO) GetDefaultToDo(enterpriseId string, subAccountId int64, platform int64, taskType int64) (map[string]int64, error) {
  577. resultMap := make(map[string]int64)
  578. var noSketch int64 // 未传初稿
  579. var noWork int64 // 未发作品
  580. var noData int64 // 未传数据
  581. var cooperateOver int64 // 终止合作
  582. var projectInfos []entity.Project
  583. //query := Db.Model(&entity.Project{}).Where("enterprise_id = ? and project_platform = ? and project_type = ?", enterpriseId, platform, taskType)
  584. if subAccountId == 0 {
  585. // 未传初稿、未发作品、未传数据、终止合作
  586. query1 := Db.Model(&entity.Project{}).Where("enterprise_id = ? and project_platform = ? and project_type = ?", enterpriseId, platform, taskType)
  587. err := query1.Where("project_status >= ?", 8).Select("project_id").Find(&projectInfos).Error
  588. if err != nil {
  589. if errors.Is(err, gorm.ErrRecordNotFound) {
  590. noSketch = 0
  591. noWork = 0
  592. noData = 0
  593. cooperateOver = 0
  594. } else {
  595. return resultMap, err
  596. }
  597. } else if len(projectInfos) > 0 {
  598. var projectIDs []string
  599. for _, info := range projectInfos {
  600. projectIDs = append(projectIDs, info.ProjectId)
  601. }
  602. if len(projectIDs) > 0 {
  603. err1 := Db.Model(&entity.ProjectTaskInfo{}).Where("project_id in ? and cur_default_type = ?", projectIDs, 4).Count(&noSketch).Error // cur_default_type=4 初稿未上传违约
  604. if err1 != nil {
  605. noSketch = 0
  606. }
  607. err2 := Db.Model(&entity.ProjectTaskInfo{}).Where("project_id in ? and cur_default_type = ?", projectIDs, 6).Count(&noWork).Error // cur_default_type=6 链接未上传违约
  608. if err2 != nil {
  609. noWork = 0
  610. }
  611. err3 := Db.Model(&entity.ProjectTaskInfo{}).Where("project_id in ? and cur_default_type = ?", projectIDs, 8).Count(&noData).Error // cur_default_type=8 数据未上传违约
  612. if err3 != nil {
  613. noData = 0
  614. }
  615. err4 := Db.Model(&entity.ProjectTaskInfo{}).Where("project_id in ? and cur_default_type = ?", projectIDs, 10).Count(&cooperateOver).Error // cur_default_type=10 解约
  616. if err4 != nil {
  617. cooperateOver = 0
  618. }
  619. }
  620. }
  621. } else {
  622. // 未传初稿、未发作品、未传数据、终止合作
  623. query1 := Db.Model(&entity.Project{}).Where("enterprise_id = ? and project_platform = ? and project_type = ?", enterpriseId, platform, taskType)
  624. err := query1.Where("sub_account_id = ? and project_status >= ?", subAccountId, 8).Select("project_id").Find(&projectInfos).Error
  625. if err != nil {
  626. if errors.Is(err, gorm.ErrRecordNotFound) {
  627. noSketch = 0
  628. noWork = 0
  629. noData = 0
  630. cooperateOver = 0
  631. } else {
  632. return resultMap, err
  633. }
  634. } else if len(projectInfos) > 0 {
  635. var projectIDs []string
  636. for _, info := range projectInfos {
  637. projectIDs = append(projectIDs, info.ProjectId)
  638. }
  639. if len(projectIDs) > 0 {
  640. err1 := Db.Model(&entity.ProjectTaskInfo{}).Where("project_id in ? and cur_default_type = ?", projectIDs, 4).Count(&noSketch).Error // cur_default_type=4 初稿未上传违约
  641. if err1 != nil {
  642. noSketch = 0
  643. }
  644. err2 := Db.Model(&entity.ProjectTaskInfo{}).Where("project_id in ? and cur_default_type = ?", projectIDs, 6).Count(&noWork).Error // cur_default_type=6 链接未上传违约
  645. if err2 != nil {
  646. noWork = 0
  647. }
  648. err3 := Db.Model(&entity.ProjectTaskInfo{}).Where("project_id in ? and cur_default_type = ?", projectIDs, 8).Count(&noData).Error // cur_default_type=8 数据未上传违约
  649. if err3 != nil {
  650. noData = 0
  651. }
  652. err4 := Db.Model(&entity.ProjectTaskInfo{}).Where("project_id in ? and cur_default_type = ?", projectIDs, 10).Count(&cooperateOver).Error // cur_default_type=10 解约
  653. if err4 != nil {
  654. cooperateOver = 0
  655. }
  656. }
  657. }
  658. }
  659. resultMap["noSketch"] = noSketch
  660. resultMap["noWork"] = noWork
  661. resultMap["noData"] = noData
  662. resultMap["cooperateOver"] = cooperateOver
  663. return resultMap, nil
  664. }
  665. // 获取指定商家已结案的指定开票状态数据
  666. func (d ProjectDAO) GetProjectFinished(enterpriseId string, invoiceStatus int64) (float64, error) {
  667. var projectAmount float64
  668. err := Db.Model(&entity.Project{}).Where("enterprise_id = ? and project_status = ? and invoice_status = ?", enterpriseId, 10, invoiceStatus).Select("COALESCE(SUM(settlement_amount), 0)").Find(&projectAmount).Error
  669. if err != nil {
  670. return 0, err
  671. }
  672. return projectAmount, err
  673. }
  674. // 寄样物流任务待办
  675. func (d ProjectDAO) GetTaskInviteToDo(enterpriseId string, subAccountId int64, platform int64) (map[string]int64, error) {
  676. resultMap := make(map[string]int64)
  677. var availInvitationNum int64
  678. var invitingNum int64
  679. var cooperatingNum int64
  680. //var projectInfos []entity.Project
  681. ////query := Db.Model(&entity.Project{}).Where("enterprise_id = ? and project_platform = ? and project_type = ?", enterpriseId, platform, taskType)
  682. //if subAccountId == 0 {
  683. // // 待发货、待签收
  684. // query1 := Db.Model(&entity.Project{}).Where("enterprise_id = ? and project_platform = ? and project_form = ?", enterpriseId, platform, 1)
  685. // err := query1.Where("project_status = ?", 8).Select("project_id").Find(&projectInfos).Error
  686. // if err != nil {
  687. // if errors.Is(err, gorm.ErrRecordNotFound) {
  688. // needDelivery = 0
  689. // needReceive = 0
  690. // } else {
  691. // return resultMap, err
  692. // }
  693. // } else if len(projectInfos) > 0 {
  694. // var projectIDs []string
  695. // for _, info := range projectInfos {
  696. // projectIDs = append(projectIDs, info.ProjectId)
  697. // }
  698. // if len(projectIDs) > 0 {
  699. // err1 := Db.Model(&entity.ProjectTaskInfo{}).Where("project_id in ? and task_stage = ?", projectIDs, 4).Count(&needDelivery).Error // task_stage=4待发货
  700. // if err1 != nil {
  701. // needDelivery = 0
  702. // }
  703. // err2 := Db.Model(&entity.ProjectTaskInfo{}).Where("project_id in ? and task_stage = ?", projectIDs, 5).Count(&needReceive).Error // task_stage=5已发货
  704. // if err2 != nil {
  705. // needReceive = 0
  706. // }
  707. // }
  708. // }
  709. //} else {
  710. // // 待发货、待签收
  711. // query1 := Db.Model(&entity.Project{}).Where("enterprise_id = ? and project_platform = ? and project_form = ?", enterpriseId, platform, 1)
  712. // err := query1.Where("sub_account_id = ? and project_status = ?", subAccountId, 8).Select("project_id").Find(&projectInfos).Error
  713. // if err != nil {
  714. // if errors.Is(err, gorm.ErrRecordNotFound) {
  715. // needDelivery = 0
  716. // needReceive = 0
  717. // } else {
  718. // return resultMap, err
  719. // }
  720. // } else {
  721. // var projectIDs []string
  722. // for _, info := range projectInfos {
  723. // projectIDs = append(projectIDs, info.ProjectId)
  724. // }
  725. // if len(projectIDs) > 0 {
  726. // err1 := Db.Model(&entity.ProjectTaskInfo{}).Where("project_id in ? and task_stage = ?", projectIDs, 4).Count(&needDelivery).Error // task_stage=4待发货
  727. // if err1 != nil {
  728. // needDelivery = 0
  729. // }
  730. // err2 := Db.Model(&entity.ProjectTaskInfo{}).Where("project_id in ? and task_stage = ?", projectIDs, 5).Count(&needReceive).Error // task_stage=5已发货
  731. // if err2 != nil {
  732. // needReceive = 0
  733. // }
  734. // }
  735. // }
  736. //}
  737. resultMap["availInvitationNum"] = availInvitationNum
  738. resultMap["invitingNum"] = invitingNum
  739. resultMap["cooperatingNum"] = cooperatingNum
  740. return resultMap, nil
  741. }