local_life_dao.go 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  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 LocalLifeDao struct{}
  11. // 根据localId获取localLife信息
  12. func (d LocalLifeDao) GetLocalById(localId string) (*entity.LocalLifeInfo, error) {
  13. var localLife entity.LocalLifeInfo
  14. err := Db.Model(&entity.LocalLifeInfo{}).Where("local_id = ?", localId).First(&localLife).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 &localLife, err
  23. }
  24. // 根据localId获取违约状态id
  25. func (d LocalLifeDao) GetAutoDefaultId(localId string) (*int64, error) {
  26. var autoDefaultId int64
  27. err := Db.Model(&entity.LocalLifeInfo{}).Where("local_id = ?", localId).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 LocalLifeDao) GetLocalListOfDay(enterpriseId string, date time.Time) ([]entity.LocalLifeInfo, error) {
  35. var localLifes []entity.LocalLifeInfo
  36. // 构建查询
  37. query := Db.Model(&entity.LocalLifeInfo{})
  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(&localLifes).Error
  44. return localLifes, err
  45. }
  46. // 创建本地生活任务
  47. func (d LocalLifeDao) CreateLocalLife(localLife entity.LocalLifeInfo) error {
  48. err := Db.Omit("auto_fail_at", "pay_at", "submit_at", "pass_at", "finish_at").Create(&localLife).Error
  49. if err != nil {
  50. return err
  51. }
  52. return nil
  53. }
  54. // 更新本地生活任务
  55. func (d LocalLifeDao) UpdateLocal(localLife entity.LocalLifeInfo) error {
  56. err := Db.Model(&entity.LocalLifeInfo{}).Where("local_id = ?", localLife.LocalID).Updates(localLife).Error
  57. if err != nil {
  58. return err
  59. }
  60. return nil
  61. }
  62. // 更新开票状态字段
  63. func (d LocalLifeDao) UpdateInvoiceStatus(localIDs []string) error {
  64. err := Db.Debug().Model(&entity.LocalLifeInfo{}).Where("local_id IN ?", localIDs).Updates(entity.LocalLifeInfo{InvoiceStatus: 1}).Error
  65. if err != nil {
  66. return err
  67. }
  68. return nil
  69. }
  70. // 获取本地生活任务列表
  71. func (d LocalLifeDao) GetLocalPreviews(param *vo.LocalSearchParam) ([]vo.ReLocalTaskPreview, int64, error) {
  72. var reLocalTaskPreviews []vo.ReLocalTaskPreview
  73. var localLifes []entity.LocalLifeInfo
  74. var total int64
  75. query := Db.Model(&entity.LocalLifeInfo{})
  76. // 动态添加查询条件
  77. if param.SubAccountId == 0 {
  78. if param.EnterpriseId == "" {
  79. return reLocalTaskPreviews, 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.LocalType != 0 {
  86. query = query.Where("local_type = ?", param.LocalType)
  87. }
  88. if param.LocalPlatform != 0 {
  89. query = query.Where("local_platform = ?", param.LocalPlatform)
  90. }
  91. if param.LocalStatus != 0 {
  92. query = query.Where("task_status = ?", param.LocalStatus)
  93. } else {
  94. query = query.Where("task_status not in ?", []int{1, 3})
  95. }
  96. if param.LocalForm != 0 {
  97. query = query.Where("task_form = ?", param.LocalForm)
  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 local_id = ? or local_name LIKE ?", param.Others, param.Others, "%"+param.Others+"%")
  104. }
  105. query.Count(&total)
  106. query = query.Select("enterprise_id, sub_account_id, local_id, local_platform, task_status, estimated_cost, need_pay, task_form, content_type, need_review, need_quality, need_calculate, store_id, team_buying_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(&localLifes).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(&localLifes).Error; err != nil {
  114. return nil, 0, err
  115. }
  116. }
  117. for _, localLife := range localLifes {
  118. reLocalTaskPreview := vo.ReLocalTaskPreview{
  119. EnterpriseId: localLife.EnterpriseID,
  120. SubAccountId: localLife.SubAccountID,
  121. LocalId: localLife.LocalID,
  122. LocalPlatform: localLife.LocalPlatform,
  123. LocalStatus: localLife.TaskStatus,
  124. LocalForm: localLife.TaskForm,
  125. ContentType: localLife.ContentType,
  126. NeedReview: localLife.NeedReview,
  127. NeedQuality: localLife.NeedQuality,
  128. NeedCalculate: localLife.NeedCalculate,
  129. StoreId: localLife.StoreID,
  130. TeamBuyingId: localLife.TeamBuyingId,
  131. Tools: localLife.Tools,
  132. }
  133. if localLife.TaskStatus < 6 {
  134. reLocalTaskPreview.EstimatedCost = localLife.EstimatedCost
  135. } else {
  136. reLocalTaskPreview.EstimatedCost = localLife.NeedPay
  137. }
  138. reLocalTaskPreviews = append(reLocalTaskPreviews, reLocalTaskPreview)
  139. }
  140. return reLocalTaskPreviews, total, nil
  141. }
  142. // 删除本地生活任务
  143. func (d LocalLifeDao) DeleteLocalLife(localId string) (*string, error) {
  144. if localId == "" {
  145. return &localId, nil
  146. }
  147. err := Db.Model(&entity.LocalLifeInfo{}).Where("local_id = ?", localId).Delete(&entity.LocalLifeInfo{}).Error
  148. if err != nil {
  149. return nil, err
  150. }
  151. return &localId, nil
  152. }
  153. // 获取草稿箱——本地生活任务列表
  154. func (d LocalLifeDao) GetLocalDraftList(param *vo.LocalDraftParam) ([]vo.ReLocalTaskPreview, int64, error) {
  155. var reLocalTaskPreviews []vo.ReLocalTaskPreview
  156. var localLifeInfos []entity.LocalLifeInfo
  157. var total int64
  158. query := Db.Model(&entity.LocalLifeInfo{}).Where("task_status = ?", 1)
  159. // 动态添加查询条件
  160. if param.SubAccountId == 0 {
  161. if param.EnterpriseId == "" {
  162. return reLocalTaskPreviews, 0, errors.New("enterpriseId is empty")
  163. }
  164. query = query.Where("enterprise_id = ?", param.EnterpriseId)
  165. } else {
  166. query = query.Where("sub_account_id = ?", param.SubAccountId)
  167. }
  168. if param.LocalType != 0 {
  169. query = query.Where("local_type = ?", param.LocalType)
  170. }
  171. if param.LocalPlatform != 0 {
  172. query = query.Where("local_platform = ?", param.LocalPlatform)
  173. }
  174. if param.Others != "" {
  175. query = query.Where("enterprise_id = ? or local_id = ? or local_name LIKE ?", param.Others, param.Others, "%"+param.Others+"%")
  176. }
  177. query.Count(&total)
  178. query = query.Select("enterprise_id, sub_account_id, local_id, local_platform, local_type, created_at, store_id")
  179. offset := (param.Page - 1) * param.PageSize
  180. if err := query.Order("created_at asc").Offset(offset).Limit(param.PageSize).Find(&localLifeInfos).Error; err != nil {
  181. return nil, 0, err
  182. }
  183. for _, localLifeInfo := range localLifeInfos {
  184. reLocalTaskPreview := vo.ReLocalTaskPreview{
  185. EnterpriseId: localLifeInfo.EnterpriseID,
  186. SubAccountId: localLifeInfo.SubAccountID,
  187. LocalId: localLifeInfo.LocalID,
  188. LocalPlatform: localLifeInfo.LocalPlatform,
  189. LocalType: localLifeInfo.LocalType,
  190. CreatedAt: localLifeInfo.CreatedAt.Format("2006-01-02 15:04:05"),
  191. StoreId: localLifeInfo.StoreID,
  192. }
  193. reLocalTaskPreviews = append(reLocalTaskPreviews, reLocalTaskPreview)
  194. }
  195. return reLocalTaskPreviews, total, nil
  196. }
  197. // 获取公开本地生活中全部指定状态值的项目
  198. func (d LocalLifeDao) GetLocalLifeList(value int64, fieldName string) ([]*entity.LocalLifeInfo, error) {
  199. var localLifeInfos []*entity.LocalLifeInfo
  200. err := Db.Model(entity.LocalLifeInfo{}).Where(fmt.Sprintf("local_type = ? AND %s = ? ", fieldName), 1, value).Find(&localLifeInfos).Error
  201. if err != nil {
  202. return nil, err
  203. }
  204. return localLifeInfos, nil
  205. }
  206. // 违约管理——违约公开本地生活任务列表
  207. func (d LocalLifeDao) GetLocalPublicList(param *vo.DefaultSearchParam) ([]vo.ReTaskDefaultPublic, int64, error) {
  208. var reTaskDefaultPublics []vo.ReTaskDefaultPublic
  209. var localLifeInfos []entity.LocalLifeInfo
  210. var total int64
  211. query := Db.Model(&entity.LocalLifeInfo{}).Where("local_type = ?", 1)
  212. // 动态添加查询条件
  213. if param.SubAccountId == 0 {
  214. if param.EnterpriseId == "" {
  215. return reTaskDefaultPublics, 0, errors.New("enterpriseId is empty")
  216. }
  217. query = query.Where("enterprise_id = ?", param.EnterpriseId)
  218. } else {
  219. query = query.Where("sub_account_id = ?", param.SubAccountId)
  220. }
  221. if param.Platform != 0 {
  222. query = query.Where("local_platform = ?", param.Platform)
  223. }
  224. if param.Others != "" {
  225. query = query.Where("enterprise_id = ? or local_id = ? or local_name LIKE ?", param.Others, param.Others, "%"+param.Others+"%")
  226. }
  227. query.Count(&total)
  228. query = query.Select("enterprise_id, sub_account_id, local_id, local_platform, task_form, content_type, store_id")
  229. offset := (param.Page - 1) * param.PageSize
  230. if err := query.Order("created_at asc").Offset(offset).Limit(param.PageSize).Find(&localLifeInfos).Error; err != nil {
  231. return nil, 0, err
  232. }
  233. for _, localLife := range localLifeInfos {
  234. reTaskDefaultPublic := vo.ReTaskDefaultPublic{
  235. EnterpriseId: localLife.EnterpriseID,
  236. SubAccountId: localLife.SubAccountID,
  237. TaskId: localLife.LocalID,
  238. Platform: localLife.LocalPlatform,
  239. TaskForm: localLife.TaskForm,
  240. ContentType: localLife.ContentType,
  241. TaskType: 1,
  242. StoreId: localLife.StoreID,
  243. }
  244. reTaskDefaultPublics = append(reTaskDefaultPublics, reTaskDefaultPublic)
  245. }
  246. return reTaskDefaultPublics, total, nil
  247. }
  248. // 探店本地生活列表
  249. func (d LocalLifeDao) GetLocalStoreExplorePreviews(param *vo.LocalSearchParam) ([]vo.ReLocalStoreExplorePreview, int64, error) {
  250. var reLocalStoreExplorePreviews []vo.ReLocalStoreExplorePreview
  251. var localLifes []entity.LocalLifeInfo
  252. var total int64
  253. query := Db.Model(&entity.LocalLifeInfo{})
  254. // 动态添加查询条件
  255. if param.SubAccountId == 0 {
  256. if param.EnterpriseId == "" {
  257. return reLocalStoreExplorePreviews, 0, errors.New("enterpriseId is empty")
  258. }
  259. query = query.Where("enterprise_id = ?", param.EnterpriseId)
  260. } else {
  261. query = query.Where("sub_account_id = ?", param.SubAccountId)
  262. }
  263. if param.LocalType != 0 {
  264. query = query.Where("local_type = ?", param.LocalType)
  265. }
  266. if param.LocalPlatform != 0 {
  267. query = query.Where("local_platform = ?", param.LocalPlatform)
  268. }
  269. if param.Others != "" {
  270. query = query.Where("enterprise_id = ? or local_id = ? or local_name LIKE ?", param.Others, param.Others, "%"+param.Others+"%")
  271. }
  272. query = query.Where("task_status = ? AND task_form = ?", 8, 1)
  273. query.Count(&total)
  274. query = query.Select("enterprise_id, sub_account_id, local_id, local_type, local_platform, need_reserve, need_confirm, need_explore, explored_num, store_id, team_buying_id")
  275. offset := (param.Page - 1) * param.PageSize
  276. if err := query.Order("pay_at asc").Offset(offset).Limit(param.PageSize).Find(&localLifes).Error; err != nil {
  277. return nil, 0, err
  278. }
  279. for _, localLife := range localLifes {
  280. reLocalStoreExplorePreview := vo.ReLocalStoreExplorePreview{
  281. EnterpriseId: localLife.EnterpriseID,
  282. SubAccountId: localLife.SubAccountID,
  283. LocalId: localLife.LocalID,
  284. LocalPlatform: localLife.LocalPlatform,
  285. LocalType: localLife.LocalType,
  286. NeedReserve: localLife.NeedReserve,
  287. NeedConfirm: localLife.NeedConfirm,
  288. NeedExplore: localLife.NeedExplore,
  289. ExploredNum: localLife.ExploredNum,
  290. StoreId: localLife.StoreID,
  291. TeamBuyingId: localLife.TeamBuyingId,
  292. }
  293. reLocalStoreExplorePreviews = append(reLocalStoreExplorePreviews, reLocalStoreExplorePreview)
  294. }
  295. return reLocalStoreExplorePreviews, total, nil
  296. }
  297. // 获取本地生活账单列表
  298. func (d LocalLifeDao) GetBillLocalPreviews(param *vo.LocalSearchParam) ([]vo.ReBillLocalTaskPreview, int64, error) {
  299. var reBillLocalTaskPreviews []vo.ReBillLocalTaskPreview
  300. var localLifes []entity.LocalLifeInfo
  301. var total int64
  302. query := Db.Model(&entity.LocalLifeInfo{})
  303. // 动态添加查询条件
  304. if param.SubAccountId == 0 {
  305. if param.EnterpriseId == "" {
  306. return reBillLocalTaskPreviews, 0, errors.New("enterpriseId is empty")
  307. }
  308. query = query.Where("enterprise_id = ?", param.EnterpriseId)
  309. } else {
  310. query = query.Where("sub_account_id = ?", param.SubAccountId)
  311. }
  312. if param.LocalType != 0 {
  313. query = query.Where("local_type = ?", param.LocalType)
  314. }
  315. if param.LocalPlatform != 0 {
  316. query = query.Where("local_platform = ?", param.LocalPlatform)
  317. }
  318. if param.LocalStatus != 0 {
  319. query = query.Where("task_status = ?", param.LocalStatus)
  320. }
  321. if param.Others != "" {
  322. query = query.Where("enterprise_id = ? or local_id = ? or local_name LIKE ?", param.Others, param.Others, "%"+param.Others+"%")
  323. }
  324. query.Count(&total)
  325. query = query.Select("enterprise_id, sub_account_id, local_id, local_platform, task_status, estimated_cost, need_pay, task_form, content_type, store_id, team_buying_id, settlement_amount")
  326. offset := (param.Page - 1) * param.PageSize
  327. if err := query.Order("created_at asc").Offset(offset).Limit(param.PageSize).Find(&localLifes).Error; err != nil {
  328. return nil, 0, err
  329. }
  330. for _, localLife := range localLifes {
  331. reBillLocalTaskPreview := vo.ReBillLocalTaskPreview{
  332. EnterpriseId: localLife.EnterpriseID,
  333. SubAccountId: localLife.SubAccountID,
  334. LocalId: localLife.LocalID,
  335. LocalPlatform: localLife.LocalPlatform,
  336. LocalStatus: localLife.TaskStatus,
  337. LocalForm: localLife.TaskForm,
  338. ContentType: localLife.ContentType,
  339. StoreId: localLife.StoreID,
  340. TeamBuyingId: localLife.TeamBuyingId,
  341. CashAmount: localLife.SettlementAmount,
  342. }
  343. if localLife.TaskStatus < 6 {
  344. reBillLocalTaskPreview.EstimatedCost = localLife.EstimatedCost
  345. } else {
  346. reBillLocalTaskPreview.EstimatedCost = localLife.NeedPay
  347. }
  348. reBillLocalTaskPreviews = append(reBillLocalTaskPreviews, reBillLocalTaskPreview)
  349. }
  350. return reBillLocalTaskPreviews, total, nil
  351. }
  352. // 本地生活任务待办
  353. func (d LocalLifeDao) GetLocalLifeToDo(enterpriseId string, subAccountId int64, platform int64, taskType int64) (map[string]int64, error) {
  354. resultMap := make(map[string]int64)
  355. var needReview int64 // 待审核
  356. var needPay int64
  357. var needProcess int64
  358. var needCheck int64 // 初稿待审
  359. var needQuality int64 // 链接待审
  360. var needCalculate int64 // 待结算
  361. var localInfos []entity.LocalLifeInfo
  362. //query := Db.Model(&entity.LocalLifeInfo{}).Where("enterprise_id = ? and local_platform = ? and local_type = ?", enterpriseId, platform, taskType)
  363. if subAccountId == 0 {
  364. // 待审核、待支付、达人未处理、初稿待审、链接待审、待结算
  365. query1 := Db.Model(&entity.LocalLifeInfo{}).Where("enterprise_id = ? and local_platform = ? and local_type = ?", enterpriseId, platform, taskType)
  366. query1.Where("task_status = ?", 2).Count(&needReview)
  367. query2 := Db.Model(&entity.LocalLifeInfo{}).Where("enterprise_id = ? and local_platform = ? and local_type = ?", enterpriseId, platform, taskType)
  368. query2.Where("task_status = ?", 6).Count(&needPay)
  369. query3 := Db.Model(&entity.LocalLifeInfo{}).Where("enterprise_id = ? and local_platform = ? and local_type = ?", enterpriseId, platform, taskType)
  370. err := query3.Where("task_status = ?", 8).Select("local_id").Find(&localInfos).Error
  371. if err != nil {
  372. if errors.Is(err, gorm.ErrRecordNotFound) {
  373. needProcess = 0
  374. } else {
  375. return resultMap, err
  376. }
  377. } else if len(localInfos) > 0 {
  378. var localIDs []string
  379. for _, info := range localInfos {
  380. localIDs = append(localIDs, info.LocalID)
  381. }
  382. if len(localIDs) > 0 {
  383. err1 := Db.Model(&entity.LocalLifeTaskInfo{}).Where("local_id in ? and task_status = ?", localIDs, 1).Count(&needProcess).Error // task_status=1待选
  384. if err1 != nil {
  385. needProcess = 0
  386. }
  387. }
  388. }
  389. query4 := Db.Model(&entity.LocalLifeInfo{}).Where("enterprise_id = ? and local_platform = ? and local_type = ?", enterpriseId, platform, taskType)
  390. err1 := query4.Select("need_review, need_quality, need_calculate").Find(&localInfos).Error
  391. if err1 != nil {
  392. return resultMap, err1
  393. } else if len(localInfos) > 0 {
  394. for _, info := range localInfos {
  395. needCheck += info.NeedReview
  396. needQuality += info.NeedQuality
  397. needCalculate += info.NeedCalculate
  398. }
  399. }
  400. } else {
  401. // 待审核、待支付、达人未处理
  402. query1 := Db.Model(&entity.LocalLifeInfo{}).Where("enterprise_id = ? and local_platform = ? and local_type = ?", enterpriseId, platform, taskType)
  403. query1.Where("sub_account_id = ? and task_status = ?", subAccountId, 2).Count(&needReview)
  404. query2 := Db.Model(&entity.LocalLifeInfo{}).Where("enterprise_id = ? and local_platform = ? and local_type = ?", enterpriseId, platform, taskType)
  405. query2.Where("sub_account_id = ? and task_status = ?", subAccountId, 6).Count(&needPay)
  406. query3 := Db.Model(&entity.LocalLifeInfo{}).Where("enterprise_id = ? and local_platform = ? and local_type = ?", enterpriseId, platform, taskType)
  407. err := query3.Where("sub_account_id = ? and task_status = ?", subAccountId, 8).Select("local_id").Find(&localInfos).Error
  408. if err != nil {
  409. if errors.Is(err, gorm.ErrRecordNotFound) {
  410. needProcess = 0
  411. } else {
  412. return resultMap, err
  413. }
  414. } else {
  415. var localIDs []string
  416. for _, info := range localInfos {
  417. localIDs = append(localIDs, info.LocalID)
  418. }
  419. if len(localIDs) > 0 {
  420. err1 := Db.Model(&entity.LocalLifeTaskInfo{}).Where("local_id in ? and task_status = ?", localIDs, 1).Count(&needProcess).Error // task_status=1待选
  421. if err1 != nil {
  422. needProcess = 0
  423. }
  424. }
  425. }
  426. query4 := Db.Model(&entity.LocalLifeInfo{}).Where("enterprise_id = ? and local_platform = ? and local_type = ?", enterpriseId, platform, taskType)
  427. err1 := query4.Select("need_review, need_quality, need_calculate").Find(&localInfos).Error
  428. if err1 != nil {
  429. return resultMap, err1
  430. } else if len(localInfos) > 0 {
  431. for _, info := range localInfos {
  432. needCheck += info.NeedReview
  433. needQuality += info.NeedQuality
  434. needCalculate += info.NeedCalculate
  435. }
  436. }
  437. }
  438. resultMap["needReview"] = needReview
  439. resultMap["needPay"] = needPay
  440. resultMap["needProcess"] = needProcess
  441. resultMap["needCheck"] = needCheck
  442. resultMap["needQuality"] = needQuality
  443. resultMap["needCalculate"] = needCalculate
  444. return resultMap, nil
  445. }
  446. // 探店邀约任务待办
  447. func (d LocalLifeDao) GetExploreToDo(enterpriseId string, subAccountId int64, platform int64) (map[string]int64, error) {
  448. resultMap := make(map[string]int64)
  449. var needBook int64 // 待预约探店时间
  450. var needConfirm int64 // 探店时间待确认
  451. var needExplore int64 // 达人待探店
  452. var localInfos []entity.LocalLifeInfo
  453. //query := Db.Model(&entity.LocalLifeInfo{}).Where("enterprise_id = ? and local_platform = ? and local_type = ?", enterpriseId, platform, taskType)
  454. if subAccountId == 0 {
  455. // 待预约探店时间、探店时间待确认、达人待探店
  456. query1 := Db.Model(&entity.LocalLifeInfo{}).Where("enterprise_id = ? and local_platform = ? and task_form = ?", enterpriseId, platform, 1)
  457. err := query1.Where("task_status = ?", 8).Select("local_id").Find(&localInfos).Error
  458. if err != nil {
  459. if errors.Is(err, gorm.ErrRecordNotFound) {
  460. needBook = 0
  461. needConfirm = 0
  462. needExplore = 0
  463. } else {
  464. return resultMap, err
  465. }
  466. } else if len(localInfos) > 0 {
  467. var localIDs []string
  468. for _, info := range localInfos {
  469. localIDs = append(localIDs, info.LocalID)
  470. }
  471. if len(localIDs) > 0 {
  472. err1 := Db.Model(&entity.LocalLifeTaskInfo{}).Where("local_id in ? and task_stage = ?", localIDs, 4).Count(&needBook).Error // task_stage=4待预约探店
  473. if err1 != nil {
  474. needBook = 0
  475. }
  476. err2 := Db.Model(&entity.LocalLifeTaskInfo{}).Where("local_id in ? and task_stage = ?", localIDs, 5).Count(&needConfirm).Error // task_stage=4预约确认中
  477. if err2 != nil {
  478. needConfirm = 0
  479. }
  480. err3 := Db.Model(&entity.LocalLifeTaskInfo{}).Where("local_id in ? and book_status = ?", localIDs, 5).Count(&needExplore).Error // book_status=5达人待探店
  481. if err3 != nil {
  482. needExplore = 0
  483. }
  484. }
  485. }
  486. } else {
  487. // 待预约探店时间、探店时间待确认、达人待探店
  488. query1 := Db.Model(&entity.LocalLifeInfo{}).Where("enterprise_id = ? and local_platform = ? and task_form = ?", enterpriseId, platform, 1)
  489. err := query1.Where("sub_account_id = ? and task_status = ?", subAccountId, 8).Select("local_id").Find(&localInfos).Error
  490. if err != nil {
  491. if errors.Is(err, gorm.ErrRecordNotFound) {
  492. needBook = 0
  493. needConfirm = 0
  494. needExplore = 0
  495. } else {
  496. return resultMap, err
  497. }
  498. } else if len(localInfos) > 0 {
  499. var localIDs []string
  500. for _, info := range localInfos {
  501. localIDs = append(localIDs, info.LocalID)
  502. }
  503. if len(localIDs) > 0 {
  504. err1 := Db.Model(&entity.LocalLifeTaskInfo{}).Where("local_id in ? and task_stage = ?", localIDs, 4).Count(&needBook).Error // task_stage=4待预约探店
  505. if err1 != nil {
  506. needBook = 0
  507. }
  508. err2 := Db.Model(&entity.LocalLifeTaskInfo{}).Where("local_id in ? and task_stage = ?", localIDs, 5).Count(&needConfirm).Error // task_stage=4预约确认中
  509. if err2 != nil {
  510. needConfirm = 0
  511. }
  512. err3 := Db.Model(&entity.LocalLifeTaskInfo{}).Where("local_id in ? and book_status = ?", localIDs, 5).Count(&needExplore).Error // book_status=5达人待探店
  513. if err3 != nil {
  514. needExplore = 0
  515. }
  516. }
  517. }
  518. }
  519. resultMap["needBook"] = needBook
  520. resultMap["needConfirm"] = needConfirm
  521. resultMap["needExplore"] = needExplore
  522. return resultMap, nil
  523. }
  524. // 违约管理任务待办
  525. func (d LocalLifeDao) GetDefaultToDo(enterpriseId string, subAccountId int64, platform int64, taskType int64) (map[string]int64, error) {
  526. resultMap := make(map[string]int64)
  527. var noSketch int64 // 未传初稿
  528. var noWork int64 // 未发作品
  529. var noData int64 // 未传数据
  530. var cooperateOver int64 // 终止合作
  531. var localInfos []entity.LocalLifeInfo
  532. //query := Db.Model(&entity.LocalLifeInfo{}).Where("enterprise_id = ? and local_platform = ? and local_type = ?", enterpriseId, platform, taskType)
  533. if subAccountId == 0 {
  534. // 未传初稿、未发作品、未传数据、终止合作
  535. query1 := Db.Model(&entity.LocalLifeInfo{}).Where("enterprise_id = ? and local_platform = ? and local_type = ?", enterpriseId, platform, taskType)
  536. err := query1.Where("task_status = ?", 8).Select("local_id").Find(&localInfos).Error
  537. if err != nil {
  538. if errors.Is(err, gorm.ErrRecordNotFound) {
  539. noSketch = 0
  540. noWork = 0
  541. noData = 0
  542. cooperateOver = 0
  543. } else {
  544. return resultMap, err
  545. }
  546. } else if len(localInfos) > 0 {
  547. var localIDs []string
  548. for _, info := range localInfos {
  549. localIDs = append(localIDs, info.LocalID)
  550. }
  551. if len(localIDs) > 0 {
  552. err1 := Db.Model(&entity.LocalLifeTaskInfo{}).Where("local_id in ? and cur_default_type = ?", localIDs, 4).Count(&noSketch).Error // cur_default_type=4 初稿未上传违约
  553. if err1 != nil {
  554. noSketch = 0
  555. }
  556. err2 := Db.Model(&entity.LocalLifeTaskInfo{}).Where("local_id in ? and cur_default_type = ?", localIDs, 6).Count(&noWork).Error // cur_default_type=6 链接未上传违约
  557. if err2 != nil {
  558. noWork = 0
  559. }
  560. err3 := Db.Model(&entity.LocalLifeTaskInfo{}).Where("local_id in ? and cur_default_type = ?", localIDs, 8).Count(&noData).Error // cur_default_type=8 数据未上传违约
  561. if err3 != nil {
  562. noData = 0
  563. }
  564. err4 := Db.Model(&entity.LocalLifeTaskInfo{}).Where("local_id in ? and cur_default_type = ?", localIDs, 10).Count(&cooperateOver).Error // cur_default_type=10 解约
  565. if err4 != nil {
  566. cooperateOver = 0
  567. }
  568. }
  569. }
  570. } else {
  571. // 未传初稿、未发作品、未传数据、终止合作
  572. query1 := Db.Model(&entity.LocalLifeInfo{}).Where("enterprise_id = ? and local_platform = ? and local_type = ?", enterpriseId, platform, taskType)
  573. err := query1.Where("sub_account_id = ? and task_status = ?", subAccountId, 8).Select("local_id").Find(&localInfos).Error
  574. if err != nil {
  575. if errors.Is(err, gorm.ErrRecordNotFound) {
  576. noSketch = 0
  577. noWork = 0
  578. noData = 0
  579. cooperateOver = 0
  580. } else {
  581. return resultMap, err
  582. }
  583. } else if len(localInfos) > 0 {
  584. var localIDs []string
  585. for _, info := range localInfos {
  586. localIDs = append(localIDs, info.LocalID)
  587. }
  588. if len(localIDs) > 0 {
  589. err1 := Db.Model(&entity.LocalLifeTaskInfo{}).Where("local_id in ? and cur_default_type = ?", localIDs, 4).Count(&noSketch).Error // cur_default_type=4 初稿未上传违约
  590. if err1 != nil {
  591. noSketch = 0
  592. }
  593. err2 := Db.Model(&entity.LocalLifeTaskInfo{}).Where("local_id in ? and cur_default_type = ?", localIDs, 6).Count(&noWork).Error // cur_default_type=6 链接未上传违约
  594. if err2 != nil {
  595. noWork = 0
  596. }
  597. err3 := Db.Model(&entity.LocalLifeTaskInfo{}).Where("local_id in ? and cur_default_type = ?", localIDs, 8).Count(&noData).Error // cur_default_type=8 数据未上传违约
  598. if err3 != nil {
  599. noData = 0
  600. }
  601. err4 := Db.Model(&entity.LocalLifeTaskInfo{}).Where("local_id in ? and cur_default_type = ?", localIDs, 10).Count(&cooperateOver).Error // cur_default_type=10 解约
  602. if err4 != nil {
  603. cooperateOver = 0
  604. }
  605. }
  606. }
  607. }
  608. resultMap["noSketch"] = noSketch
  609. resultMap["noWork"] = noWork
  610. resultMap["noData"] = noData
  611. resultMap["cooperateOver"] = cooperateOver
  612. return resultMap, nil
  613. }
  614. // 获取指定商家已结案的指定开票状态数据
  615. func (d LocalLifeDao) GetLocalLifeFinished(enterpriseId string, invoiceStatus int64) (float64, error) {
  616. var localLifeAmount float64
  617. err := Db.Model(&entity.LocalLifeInfo{}).Where("enterprise_id = ? and task_status = ? and invoice_status = ?", enterpriseId, 10, invoiceStatus).Select("COALESCE(SUM(settlement_amount), 0)").Find(&localLifeAmount).Error
  618. if err != nil {
  619. return 0, err
  620. }
  621. return localLifeAmount, err
  622. }
  623. // 合作待办-任务邀约
  624. func (d LocalLifeDao) GetTaskInviteToDo(enterpriseId string, subAccountId int64, platform int64) (map[string]int64, error) {
  625. resultMap := make(map[string]int64)
  626. var availInvitationNum int64 //
  627. var invitingNum int64 //
  628. var cooperatingNum int64 //
  629. //var localInfos []entity.LocalLifeInfo
  630. ////query := Db.Model(&entity.LocalLifeInfo{}).Where("enterprise_id = ? and local_platform = ? and local_type = ?", enterpriseId, platform, taskType)
  631. //if subAccountId == 0 {
  632. // // 待预约探店时间、探店时间待确认、达人待探店
  633. // query1 := Db.Model(&entity.LocalLifeInfo{}).Where("enterprise_id = ? and local_platform = ? and task_form = ?", enterpriseId, platform, 1)
  634. // err := query1.Where("task_status = ?", 8).Select("local_id").Find(&localInfos).Error
  635. // if err != nil {
  636. // if errors.Is(err, gorm.ErrRecordNotFound) {
  637. // needBook = 0
  638. // needConfirm = 0
  639. // needExplore = 0
  640. // } else {
  641. // return resultMap, err
  642. // }
  643. // } else if len(localInfos) > 0 {
  644. // var localIDs []string
  645. // for _, info := range localInfos {
  646. // localIDs = append(localIDs, info.LocalID)
  647. // }
  648. // if len(localIDs) > 0 {
  649. // err1 := Db.Model(&entity.LocalLifeTaskInfo{}).Where("local_id in ? and task_stage = ?", localIDs, 4).Count(&needBook).Error // task_stage=4待预约探店
  650. // if err1 != nil {
  651. // needBook = 0
  652. // }
  653. // err2 := Db.Model(&entity.LocalLifeTaskInfo{}).Where("local_id in ? and task_stage = ?", localIDs, 5).Count(&needConfirm).Error // task_stage=4预约确认中
  654. // if err2 != nil {
  655. // needConfirm = 0
  656. // }
  657. // err3 := Db.Model(&entity.LocalLifeTaskInfo{}).Where("local_id in ? and book_status = ?", localIDs, 5).Count(&needExplore).Error // book_status=5达人待探店
  658. // if err3 != nil {
  659. // needExplore = 0
  660. // }
  661. // }
  662. // }
  663. //} else {
  664. // // 待预约探店时间、探店时间待确认、达人待探店
  665. // query1 := Db.Model(&entity.LocalLifeInfo{}).Where("enterprise_id = ? and local_platform = ? and task_form = ?", enterpriseId, platform, 1)
  666. // err := query1.Where("sub_account_id = ? and task_status = ?", subAccountId, 8).Select("local_id").Find(&localInfos).Error
  667. // if err != nil {
  668. // if errors.Is(err, gorm.ErrRecordNotFound) {
  669. // needBook = 0
  670. // needConfirm = 0
  671. // needExplore = 0
  672. // } else {
  673. // return resultMap, err
  674. // }
  675. // } else if len(localInfos) > 0 {
  676. // var localIDs []string
  677. // for _, info := range localInfos {
  678. // localIDs = append(localIDs, info.LocalID)
  679. // }
  680. // if len(localIDs) > 0 {
  681. // err1 := Db.Model(&entity.LocalLifeTaskInfo{}).Where("local_id in ? and task_stage = ?", localIDs, 4).Count(&needBook).Error // task_stage=4待预约探店
  682. // if err1 != nil {
  683. // needBook = 0
  684. // }
  685. // err2 := Db.Model(&entity.LocalLifeTaskInfo{}).Where("local_id in ? and task_stage = ?", localIDs, 5).Count(&needConfirm).Error // task_stage=4预约确认中
  686. // if err2 != nil {
  687. // needConfirm = 0
  688. // }
  689. // err3 := Db.Model(&entity.LocalLifeTaskInfo{}).Where("local_id in ? and book_status = ?", localIDs, 5).Count(&needExplore).Error // book_status=5达人待探店
  690. // if err3 != nil {
  691. // needExplore = 0
  692. // }
  693. // }
  694. // }
  695. //}
  696. resultMap["availInvitationNum"] = availInvitationNum
  697. resultMap["invitingNum"] = invitingNum
  698. resultMap["cooperatingNum"] = cooperatingNum
  699. return resultMap, nil
  700. }