local_life_dao.go 26 KB

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