project_dao.go 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  1. package dao
  2. import (
  3. "errors"
  4. "fmt"
  5. "gorm.io/gorm"
  6. "time"
  7. "youngee_b_api/app/entity"
  8. "youngee_b_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").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_platform, project_status, estimated_cost, 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. ProjectPlatform: project.ProjectPlatform,
  126. ProjectStatus: project.ProjectStatus,
  127. EstimatedCost: project.EstimatedCost,
  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. reProjectTaskPreviews = append(reProjectTaskPreviews, reProjectTaskPreview)
  141. }
  142. return reProjectTaskPreviews, total, nil
  143. }
  144. // 删除种草任务
  145. func (d ProjectDAO) DeleteProject(projectId string) (*string, error) {
  146. if projectId == "" {
  147. return &projectId, nil
  148. }
  149. err := Db.Model(&entity.Project{}).Where("project_id = ?", projectId).Delete(&entity.Project{}).Error
  150. if err != nil {
  151. return nil, err
  152. }
  153. return &projectId, nil
  154. }
  155. // 获取草稿箱——种草任务列表
  156. func (d ProjectDAO) GetProjectDraftList(param *vo.ProjectDraftParam) ([]vo.ReProjectTaskPreview, int64, error) {
  157. var reProjectTaskPreviews []vo.ReProjectTaskPreview
  158. var projects []entity.Project
  159. var total int64
  160. query := Db.Model(&entity.Project{}).Where("project_status = ?", 1)
  161. // 动态添加查询条件
  162. if param.SubAccountId == 0 {
  163. if param.EnterpriseId == "" {
  164. return reProjectTaskPreviews, 0, errors.New("enterpriseId is empty")
  165. }
  166. query = query.Where("enterprise_id = ?", param.EnterpriseId)
  167. } else {
  168. query = query.Where("sub_account_id = ?", param.SubAccountId)
  169. }
  170. if param.ProjectType != 0 {
  171. query = query.Where("project_type = ?", param.ProjectType)
  172. }
  173. if param.ProjectPlatform != 0 {
  174. query = query.Where("project_platform = ?", param.ProjectPlatform)
  175. }
  176. if param.Others != "" {
  177. query = query.Where("enterprise_id = ? or project_id = ? or project_name LIKE ?", param.Others, param.Others, "%"+param.Others+"%")
  178. }
  179. query.Count(&total)
  180. query = query.Select("enterprise_id, sub_account_id, project_id, project_platform, project_type, created_at, product_id")
  181. offset := (param.Page - 1) * param.PageSize
  182. if err := query.Order("created_at asc").Offset(offset).Limit(param.PageSize).Find(&projects).Error; err != nil {
  183. return nil, 0, err
  184. }
  185. for _, project := range projects {
  186. reProjectTaskPreview := vo.ReProjectTaskPreview{
  187. EnterpriseId: project.EnterpriseID,
  188. SubAccountId: project.SubAccountId,
  189. ProjectId: project.ProjectId,
  190. ProjectPlatform: project.ProjectPlatform,
  191. ProjectType: project.ProjectType,
  192. CreatedAt: project.CreatedAt.Format("2006-01-02 15:04:05"),
  193. ProductId: project.ProductID,
  194. }
  195. reProjectTaskPreviews = append(reProjectTaskPreviews, reProjectTaskPreview)
  196. }
  197. return reProjectTaskPreviews, total, nil
  198. }
  199. // 获取公开种草中全部指定状态值的项目
  200. func (d ProjectDAO) GetProjectList(value int64, fieldName string) ([]*entity.Project, error) {
  201. var projectInfos []*entity.Project
  202. err := Db.Model(entity.Project{}).Where(fmt.Sprintf("project_type = ? AND %s = ? ", fieldName), 1, value).Find(&projectInfos).Error
  203. if err != nil {
  204. return nil, err
  205. }
  206. return projectInfos, nil
  207. }
  208. // 违约管理——违约公开种草任务列表
  209. func (d ProjectDAO) GetProjectPublicList(param *vo.DefaultSearchParam) ([]vo.ReTaskDefaultPublic, int64, error) {
  210. var reTaskDefaultPublics []vo.ReTaskDefaultPublic
  211. var projects []entity.Project
  212. var total int64
  213. query := Db.Model(&entity.Project{}).Where("project_type = ?", 1)
  214. // 动态添加查询条件
  215. if param.SubAccountId == 0 {
  216. if param.EnterpriseId == "" {
  217. return reTaskDefaultPublics, 0, errors.New("enterpriseId is empty")
  218. }
  219. query = query.Where("enterprise_id = ?", param.EnterpriseId)
  220. } else {
  221. query = query.Where("sub_account_id = ?", param.SubAccountId)
  222. }
  223. if param.Platform != 0 {
  224. query = query.Where("project_platform = ?", param.Platform)
  225. }
  226. if param.TaskId != "" {
  227. query = query.Where("project_id = ?", param.TaskId)
  228. }
  229. query.Count(&total)
  230. query = query.Select("enterprise_id, sub_account_id, project_id, project_platform, project_form, content_type, product_id")
  231. offset := (param.Page - 1) * param.PageSize
  232. if err := query.Order("created_at asc").Offset(offset).Limit(param.PageSize).Find(&projects).Error; err != nil {
  233. return nil, 0, err
  234. }
  235. for _, project := range projects {
  236. reTaskDefaultPublic := vo.ReTaskDefaultPublic{
  237. EnterpriseId: project.EnterpriseID,
  238. SubAccountId: project.SubAccountId,
  239. TaskId: project.ProjectId,
  240. Platform: project.ProjectPlatform,
  241. TaskForm: project.ProjectForm,
  242. ContentType: project.ContentType,
  243. TaskType: 1,
  244. ProductId: project.ProductID,
  245. }
  246. reTaskDefaultPublics = append(reTaskDefaultPublics, reTaskDefaultPublic)
  247. }
  248. return reTaskDefaultPublics, total, nil
  249. }
  250. // 违约管理——违约定向种草任务列表
  251. func (d ProjectDAO) GetProjectTargetList(param *vo.DefaultSearchParam) ([]vo.ReTaskDefaultTarget, int64, error) {
  252. var reTaskDefaultTargets []vo.ReTaskDefaultTarget
  253. var projects []entity.Project
  254. var total int64
  255. query := Db.Model(&entity.Project{}).Where("project_type = ?", 2)
  256. // 动态添加查询条件
  257. if param.SubAccountId == 0 {
  258. if param.EnterpriseId == "" {
  259. return reTaskDefaultTargets, 0, errors.New("enterpriseId is empty")
  260. }
  261. query = query.Where("enterprise_id = ?", param.EnterpriseId)
  262. } else {
  263. query = query.Where("sub_account_id = ?", param.SubAccountId)
  264. }
  265. if param.Platform != 0 {
  266. query = query.Where("project_platform = ?", param.Platform)
  267. }
  268. if param.TaskId != "" {
  269. query = query.Where("project_id = ?", param.TaskId)
  270. }
  271. query.Count(&total)
  272. query = query.Select("enterprise_id, sub_account_id, project_id, project_platform, tools, content_type, product_id")
  273. offset := (param.Page - 1) * param.PageSize
  274. if err := query.Order("created_at asc").Offset(offset).Limit(param.PageSize).Find(&projects).Error; err != nil {
  275. return nil, 0, err
  276. }
  277. for _, project := range projects {
  278. reTaskDefaultPublic := vo.ReTaskDefaultTarget{
  279. EnterpriseId: project.EnterpriseID,
  280. SubAccountId: project.SubAccountId,
  281. TaskId: project.ProjectId,
  282. Platform: project.ProjectPlatform,
  283. Tools: project.Tools,
  284. ContentType: project.ContentType,
  285. TaskType: 1,
  286. ProductId: project.ProductID,
  287. }
  288. reTaskDefaultTargets = append(reTaskDefaultTargets, reTaskDefaultPublic)
  289. }
  290. return reTaskDefaultTargets, total, nil
  291. }
  292. // 获取品牌种草冻结中的任务
  293. func (d ProjectDAO) GetProjectFrozenList(enterpriseId string) ([]*entity.Project, error) {
  294. var projects []*entity.Project
  295. query := Db.Debug().Model(entity.Project{})
  296. query.Select("project_id, product_id, enterprise_id, sub_account_id, project_platform, payment_amount, pay_at") // 冻结金额:payment_amount
  297. err := query.Where(fmt.Sprintf("enterprise_id = ? AND (project_status between 7 and 8) "), enterpriseId).Find(&projects).Error
  298. if err != nil {
  299. if errors.Is(err, gorm.ErrRecordNotFound) {
  300. return projects, nil
  301. } else {
  302. return nil, err
  303. }
  304. }
  305. return projects, nil
  306. }
  307. // 获取品牌种草冻结解除的任务
  308. func (d ProjectDAO) GetProjectFrozenCancelList(enterpriseId string) ([]*entity.Project, error) {
  309. var projects []*entity.Project
  310. query := Db.Debug().Model(entity.Project{})
  311. query.Select("project_id, product_id, enterprise_id, sub_account_id, project_platform, settlement_amount, pay_at") // 解冻金额:settlement_amount
  312. err := query.Where(fmt.Sprintf("enterprise_id = ? AND (project_status between 9 and 10) "), enterpriseId).Find(&projects).Error
  313. if err != nil {
  314. if errors.Is(err, gorm.ErrRecordNotFound) {
  315. return projects, nil
  316. } else {
  317. return nil, err
  318. }
  319. }
  320. return projects, nil
  321. }
  322. // 获取品牌种草账单列表
  323. func (d ProjectDAO) GetBillProjectPreviews(param *vo.ProjectSearchParam) ([]vo.ReBillProjectTaskPreview, int64, error) {
  324. var reBillProjectTaskPreviews []vo.ReBillProjectTaskPreview
  325. var projects []entity.Project
  326. var total int64
  327. query := Db.Model(&entity.Project{})
  328. // 动态添加查询条件
  329. if param.SubAccountId == 0 {
  330. if param.EnterpriseId == "" {
  331. return reBillProjectTaskPreviews, 0, errors.New("enterpriseId is empty")
  332. }
  333. query = query.Where("enterprise_id = ?", param.EnterpriseId)
  334. } else {
  335. query = query.Where("sub_account_id = ?", param.SubAccountId)
  336. }
  337. if param.ProjectType != 0 {
  338. query = query.Where("project_type = ?", param.ProjectType)
  339. }
  340. if param.ProjectPlatform != 0 {
  341. query = query.Where("project_platform = ?", param.ProjectPlatform)
  342. }
  343. if param.ProjectStatus != 0 {
  344. query = query.Where("project_status = ?", param.ProjectStatus)
  345. }
  346. if param.Others != "" {
  347. query = query.Where("enterprise_id = ? or project_id = ? or project_name LIKE ?", param.Others, param.Others, "%"+param.Others+"%")
  348. }
  349. query.Count(&total)
  350. query = query.Select("enterprise_id, sub_account_id, project_id, project_platform, project_status, estimated_cost, project_form, content_type, product_id, settlement_amount")
  351. offset := (param.Page - 1) * param.PageSize
  352. if err := query.Order("created_at asc").Offset(offset).Limit(param.PageSize).Find(&projects).Error; err != nil {
  353. return nil, 0, err
  354. }
  355. for _, project := range projects {
  356. reBillProjectTaskPreview := vo.ReBillProjectTaskPreview{
  357. EnterpriseId: project.EnterpriseID,
  358. SubAccountId: project.SubAccountId,
  359. ProjectId: project.ProjectId,
  360. ProjectPlatform: project.ProjectPlatform,
  361. ProjectStatus: project.ProjectStatus,
  362. ProjectForm: project.ProjectForm,
  363. ContentType: project.ContentType,
  364. ProductId: project.ProductID,
  365. EstimatedCost: project.EstimatedCost, // 应付金额
  366. CashAmount: project.SettlementAmount, //实际结算金额
  367. }
  368. reBillProjectTaskPreviews = append(reBillProjectTaskPreviews, reBillProjectTaskPreview)
  369. }
  370. return reBillProjectTaskPreviews, total, nil
  371. }
  372. // 种草任务待办
  373. func (d ProjectDAO) GetProjectToDo(enterpriseId string, subAccountId int64, platform int64, taskType int64) (map[string]int64, error) {
  374. resultMap := make(map[string]int64)
  375. var needReview int64 // 待审核
  376. var needPay int64
  377. var needProcess int64
  378. var needCheck int64 // 初稿待审
  379. var needQuality int64 // 链接待审
  380. var needCalculate int64 // 待结算
  381. var projectInfos []entity.Project
  382. //query := Db.Model(&entity.Project{}).Where("enterprise_id = ? and project_platform = ? and project_type = ?", enterpriseId, platform, taskType)
  383. if subAccountId == 0 {
  384. // 待审核、待支付、达人未处理、初稿待审、链接待审、待结算
  385. query1 := Db.Model(&entity.Project{}).Where("enterprise_id = ? and project_platform = ? and project_type = ?", enterpriseId, platform, taskType)
  386. query1.Where("project_status = ?", 2).Count(&needReview)
  387. query2 := Db.Model(&entity.Project{}).Where("enterprise_id = ? and project_platform = ? and project_type = ?", enterpriseId, platform, taskType)
  388. query2.Debug().Where("project_status = ?", 6).Count(&needPay)
  389. query3 := Db.Model(&entity.Project{}).Where("enterprise_id = ? and project_platform = ? and project_type = ?", enterpriseId, platform, taskType)
  390. err := query3.Where("project_status = ?", 8).Select("project_id").Find(&projectInfos).Error
  391. if err != nil {
  392. if errors.Is(err, gorm.ErrRecordNotFound) {
  393. needProcess = 0
  394. } else {
  395. return resultMap, err
  396. }
  397. } else if len(projectInfos) > 0 {
  398. var projectIDs []string
  399. for _, info := range projectInfos {
  400. projectIDs = append(projectIDs, info.ProjectId)
  401. }
  402. if len(projectIDs) > 0 {
  403. err1 := Db.Model(&entity.ProjectTaskInfo{}).Where("project_id in ? and task_status = ?", projectIDs, 1).Count(&needProcess).Error // task_status=1待选
  404. if err1 != nil {
  405. needProcess = 0
  406. }
  407. }
  408. }
  409. query4 := Db.Model(&entity.Project{}).Where("enterprise_id = ? and project_platform = ? and project_type = ?", enterpriseId, platform, taskType)
  410. err1 := query4.Select("need_review, need_quality, need_calculate").Find(&projectInfos).Error
  411. if err1 != nil {
  412. return resultMap, err1
  413. } else if len(projectInfos) > 0 {
  414. for _, info := range projectInfos {
  415. needCheck += info.NeedReview
  416. needQuality += info.NeedQuality
  417. needCalculate += info.NeedCalculate
  418. }
  419. }
  420. } else {
  421. // 待审核、待支付、达人未处理、初稿待审、链接待审、待结算
  422. query1 := Db.Model(&entity.Project{}).Where("enterprise_id = ? and project_platform = ? and project_type = ?", enterpriseId, platform, taskType)
  423. query1.Where("sub_account_id = ? and project_status = ?", subAccountId, 2).Count(&needReview)
  424. query2 := Db.Model(&entity.Project{}).Where("enterprise_id = ? and project_platform = ? and project_type = ?", enterpriseId, platform, taskType)
  425. query2.Where("sub_account_id = ? and project_status = ?", subAccountId, 6).Count(&needPay)
  426. query3 := Db.Model(&entity.Project{}).Where("enterprise_id = ? and project_platform = ? and project_type = ?", enterpriseId, platform, taskType)
  427. err := query3.Where("sub_account_id = ? and project_status = ?", subAccountId, 8).Select("project_id").Find(&projectInfos).Error
  428. if err != nil {
  429. if errors.Is(err, gorm.ErrRecordNotFound) {
  430. needProcess = 0
  431. } else {
  432. return resultMap, err
  433. }
  434. } else {
  435. var projectIDs []string
  436. for _, info := range projectInfos {
  437. projectIDs = append(projectIDs, info.ProjectId)
  438. }
  439. if len(projectIDs) > 0 {
  440. err1 := Db.Model(&entity.ProjectTaskInfo{}).Where("project_id in ? and task_status = ?", projectIDs, 1).Count(&needProcess).Error // task_status=1待选
  441. if err1 != nil {
  442. needProcess = 0
  443. }
  444. }
  445. }
  446. query4 := Db.Model(&entity.Project{}).Where("enterprise_id = ? and project_platform = ? and project_type = ?", enterpriseId, platform, taskType)
  447. err1 := query4.Select("need_review, need_quality, need_calculate").Find(&projectInfos).Error
  448. if err1 != nil {
  449. return resultMap, err1
  450. } else if len(projectInfos) > 0 {
  451. for _, info := range projectInfos {
  452. needCheck += info.NeedReview
  453. needQuality += info.NeedQuality
  454. needCalculate += info.NeedCalculate
  455. }
  456. }
  457. }
  458. resultMap["needReview"] = needReview
  459. resultMap["needPay"] = needPay
  460. resultMap["needProcess"] = needProcess
  461. resultMap["needCheck"] = needCheck
  462. resultMap["needQuality"] = needQuality
  463. resultMap["needCalculate"] = needCalculate
  464. return resultMap, nil
  465. }
  466. // 寄样物流任务待办
  467. func (d ProjectDAO) GetLogisticsToDo(enterpriseId string, subAccountId int64, platform int64) (map[string]int64, error) {
  468. resultMap := make(map[string]int64)
  469. var needDelivery int64
  470. var needReceive int64
  471. var projectInfos []entity.Project
  472. //query := Db.Model(&entity.Project{}).Where("enterprise_id = ? and project_platform = ? and project_type = ?", enterpriseId, platform, taskType)
  473. if subAccountId == 0 {
  474. // 待发货、待签收
  475. query1 := Db.Model(&entity.Project{}).Where("enterprise_id = ? and project_platform = ? and project_form = ?", enterpriseId, platform, 1)
  476. err := query1.Where("project_status = ?", 8).Select("project_id").Find(&projectInfos).Error
  477. if err != nil {
  478. if errors.Is(err, gorm.ErrRecordNotFound) {
  479. needDelivery = 0
  480. needReceive = 0
  481. } else {
  482. return resultMap, err
  483. }
  484. } else if len(projectInfos) > 0 {
  485. var projectIDs []string
  486. for _, info := range projectInfos {
  487. projectIDs = append(projectIDs, info.ProjectId)
  488. }
  489. if len(projectIDs) > 0 {
  490. err1 := Db.Model(&entity.ProjectTaskInfo{}).Where("project_id in ? and task_stage = ?", projectIDs, 4).Count(&needDelivery).Error // task_stage=4待发货
  491. if err1 != nil {
  492. needDelivery = 0
  493. }
  494. err2 := Db.Model(&entity.ProjectTaskInfo{}).Where("project_id in ? and task_stage = ?", projectIDs, 5).Count(&needReceive).Error // task_stage=5已发货
  495. if err2 != nil {
  496. needReceive = 0
  497. }
  498. }
  499. }
  500. } else {
  501. // 待发货、待签收
  502. query1 := Db.Model(&entity.Project{}).Where("enterprise_id = ? and project_platform = ? and project_form = ?", enterpriseId, platform, 1)
  503. err := query1.Where("sub_account_id = ? and project_status = ?", subAccountId, 8).Select("project_id").Find(&projectInfos).Error
  504. if err != nil {
  505. if errors.Is(err, gorm.ErrRecordNotFound) {
  506. needDelivery = 0
  507. needReceive = 0
  508. } else {
  509. return resultMap, err
  510. }
  511. } else {
  512. var projectIDs []string
  513. for _, info := range projectInfos {
  514. projectIDs = append(projectIDs, info.ProjectId)
  515. }
  516. if len(projectIDs) > 0 {
  517. err1 := Db.Model(&entity.ProjectTaskInfo{}).Where("project_id in ? and task_stage = ?", projectIDs, 4).Count(&needDelivery).Error // task_stage=4待发货
  518. if err1 != nil {
  519. needDelivery = 0
  520. }
  521. err2 := Db.Model(&entity.ProjectTaskInfo{}).Where("project_id in ? and task_stage = ?", projectIDs, 5).Count(&needReceive).Error // task_stage=5已发货
  522. if err2 != nil {
  523. needReceive = 0
  524. }
  525. }
  526. }
  527. }
  528. resultMap["needDelivery"] = needDelivery
  529. resultMap["needReceive"] = needReceive
  530. return resultMap, nil
  531. }
  532. // 违约管理任务待办
  533. func (d ProjectDAO) GetDefaultToDo(enterpriseId string, subAccountId int64, platform int64, taskType int64) (map[string]int64, error) {
  534. resultMap := make(map[string]int64)
  535. var noSketch int64 // 未传初稿
  536. var noWork int64 // 未发作品
  537. var noData int64 // 未传数据
  538. var cooperateOver int64 // 终止合作
  539. var projectInfos []entity.Project
  540. //query := Db.Model(&entity.Project{}).Where("enterprise_id = ? and project_platform = ? and project_type = ?", enterpriseId, platform, taskType)
  541. if subAccountId == 0 {
  542. // 未传初稿、未发作品、未传数据、终止合作
  543. query1 := Db.Model(&entity.Project{}).Where("enterprise_id = ? and project_platform = ? and project_type = ?", enterpriseId, platform, taskType)
  544. err := query1.Where("project_status >= ?", 8).Select("project_id").Find(&projectInfos).Error
  545. if err != nil {
  546. if errors.Is(err, gorm.ErrRecordNotFound) {
  547. noSketch = 0
  548. noWork = 0
  549. noData = 0
  550. cooperateOver = 0
  551. } else {
  552. return resultMap, err
  553. }
  554. } else if len(projectInfos) > 0 {
  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 cur_default_type = ?", projectIDs, 4).Count(&noSketch).Error // cur_default_type=4 初稿未上传违约
  561. if err1 != nil {
  562. noSketch = 0
  563. }
  564. err2 := Db.Model(&entity.ProjectTaskInfo{}).Where("project_id in ? and cur_default_type = ?", projectIDs, 6).Count(&noWork).Error // cur_default_type=6 链接未上传违约
  565. if err2 != nil {
  566. noWork = 0
  567. }
  568. err3 := Db.Model(&entity.ProjectTaskInfo{}).Where("project_id in ? and cur_default_type = ?", projectIDs, 8).Count(&noData).Error // cur_default_type=8 数据未上传违约
  569. if err3 != nil {
  570. noData = 0
  571. }
  572. err4 := Db.Model(&entity.ProjectTaskInfo{}).Where("project_id in ? and cur_default_type = ?", projectIDs, 10).Count(&cooperateOver).Error // cur_default_type=10 解约
  573. if err4 != nil {
  574. cooperateOver = 0
  575. }
  576. }
  577. }
  578. } else {
  579. // 未传初稿、未发作品、未传数据、终止合作
  580. query1 := Db.Model(&entity.Project{}).Where("enterprise_id = ? and project_platform = ? and project_type = ?", enterpriseId, platform, taskType)
  581. err := query1.Where("sub_account_id = ? and project_status >= ?", subAccountId, 8).Select("project_id").Find(&projectInfos).Error
  582. if err != nil {
  583. if errors.Is(err, gorm.ErrRecordNotFound) {
  584. noSketch = 0
  585. noWork = 0
  586. noData = 0
  587. cooperateOver = 0
  588. } else {
  589. return resultMap, err
  590. }
  591. } else if len(projectInfos) > 0 {
  592. var projectIDs []string
  593. for _, info := range projectInfos {
  594. projectIDs = append(projectIDs, info.ProjectId)
  595. }
  596. if len(projectIDs) > 0 {
  597. err1 := Db.Model(&entity.ProjectTaskInfo{}).Where("project_id in ? and cur_default_type = ?", projectIDs, 4).Count(&noSketch).Error // cur_default_type=4 初稿未上传违约
  598. if err1 != nil {
  599. noSketch = 0
  600. }
  601. err2 := Db.Model(&entity.ProjectTaskInfo{}).Where("project_id in ? and cur_default_type = ?", projectIDs, 6).Count(&noWork).Error // cur_default_type=6 链接未上传违约
  602. if err2 != nil {
  603. noWork = 0
  604. }
  605. err3 := Db.Model(&entity.ProjectTaskInfo{}).Where("project_id in ? and cur_default_type = ?", projectIDs, 8).Count(&noData).Error // cur_default_type=8 数据未上传违约
  606. if err3 != nil {
  607. noData = 0
  608. }
  609. err4 := Db.Model(&entity.ProjectTaskInfo{}).Where("project_id in ? and cur_default_type = ?", projectIDs, 10).Count(&cooperateOver).Error // cur_default_type=10 解约
  610. if err4 != nil {
  611. cooperateOver = 0
  612. }
  613. }
  614. }
  615. }
  616. resultMap["noSketch"] = noSketch
  617. resultMap["noWork"] = noWork
  618. resultMap["noData"] = noData
  619. resultMap["cooperateOver"] = cooperateOver
  620. return resultMap, nil
  621. }
  622. // 获取指定商家已结案的指定开票状态数据
  623. func (d ProjectDAO) GetProjectFinished(enterpriseId string, invoiceStatus int64) (float64, error) {
  624. var projectAmount float64
  625. 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
  626. if err != nil {
  627. return 0, err
  628. }
  629. return projectAmount, err
  630. }
  631. // 寄样物流任务待办
  632. func (d ProjectDAO) GetTaskInviteToDo(enterpriseId string, subAccountId int64, platform int64) (map[string]int64, error) {
  633. resultMap := make(map[string]int64)
  634. var availInvitationNum int64
  635. var invitingNum int64
  636. var cooperatingNum int64
  637. //var projectInfos []entity.Project
  638. ////query := Db.Model(&entity.Project{}).Where("enterprise_id = ? and project_platform = ? and project_type = ?", enterpriseId, platform, taskType)
  639. //if subAccountId == 0 {
  640. // // 待发货、待签收
  641. // query1 := Db.Model(&entity.Project{}).Where("enterprise_id = ? and project_platform = ? and project_form = ?", enterpriseId, platform, 1)
  642. // err := query1.Where("project_status = ?", 8).Select("project_id").Find(&projectInfos).Error
  643. // if err != nil {
  644. // if errors.Is(err, gorm.ErrRecordNotFound) {
  645. // needDelivery = 0
  646. // needReceive = 0
  647. // } else {
  648. // return resultMap, err
  649. // }
  650. // } else if len(projectInfos) > 0 {
  651. // var projectIDs []string
  652. // for _, info := range projectInfos {
  653. // projectIDs = append(projectIDs, info.ProjectId)
  654. // }
  655. // if len(projectIDs) > 0 {
  656. // err1 := Db.Model(&entity.ProjectTaskInfo{}).Where("project_id in ? and task_stage = ?", projectIDs, 4).Count(&needDelivery).Error // task_stage=4待发货
  657. // if err1 != nil {
  658. // needDelivery = 0
  659. // }
  660. // err2 := Db.Model(&entity.ProjectTaskInfo{}).Where("project_id in ? and task_stage = ?", projectIDs, 5).Count(&needReceive).Error // task_stage=5已发货
  661. // if err2 != nil {
  662. // needReceive = 0
  663. // }
  664. // }
  665. // }
  666. //} else {
  667. // // 待发货、待签收
  668. // query1 := Db.Model(&entity.Project{}).Where("enterprise_id = ? and project_platform = ? and project_form = ?", enterpriseId, platform, 1)
  669. // err := query1.Where("sub_account_id = ? and project_status = ?", subAccountId, 8).Select("project_id").Find(&projectInfos).Error
  670. // if err != nil {
  671. // if errors.Is(err, gorm.ErrRecordNotFound) {
  672. // needDelivery = 0
  673. // needReceive = 0
  674. // } else {
  675. // return resultMap, err
  676. // }
  677. // } else {
  678. // var projectIDs []string
  679. // for _, info := range projectInfos {
  680. // projectIDs = append(projectIDs, info.ProjectId)
  681. // }
  682. // if len(projectIDs) > 0 {
  683. // err1 := Db.Model(&entity.ProjectTaskInfo{}).Where("project_id in ? and task_stage = ?", projectIDs, 4).Count(&needDelivery).Error // task_stage=4待发货
  684. // if err1 != nil {
  685. // needDelivery = 0
  686. // }
  687. // err2 := Db.Model(&entity.ProjectTaskInfo{}).Where("project_id in ? and task_stage = ?", projectIDs, 5).Count(&needReceive).Error // task_stage=5已发货
  688. // if err2 != nil {
  689. // needReceive = 0
  690. // }
  691. // }
  692. // }
  693. //}
  694. resultMap["availInvitationNum"] = availInvitationNum
  695. resultMap["invitingNum"] = invitingNum
  696. resultMap["cooperatingNum"] = cooperatingNum
  697. return resultMap, nil
  698. }