rs_calendar_relation.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. package roadshow
  2. import (
  3. "github.com/beego/beego/v2/client/orm"
  4. "hongze/hz_crm_api/models/system"
  5. "hongze/hz_crm_api/utils"
  6. "strconv"
  7. "time"
  8. )
  9. // RsCalendarRelation 自系统路演与第三方路演关系表
  10. type RsCalendarRelation struct {
  11. RelationId int `orm:"column(relation_id);pk" description:"关系id"`
  12. CalendarType int8 `description:"日历类型;1:路演;2:事项"`
  13. SelfCalendarId int `description:"系统内部的日历id,可以是研究员与路演活动的关系id,也可以是事项id"`
  14. ThirdCalendarId int `description:"第三方路演id"`
  15. UserId int `description:"关系id"`
  16. UserPhone string `description:"创建人手机号"`
  17. UserName string `description:"创建人昵称"`
  18. ProjectName string `description:"活动名称"`
  19. ProjectId int `description:"活动id"`
  20. CustomerId int `description:"客户id"`
  21. CustomerName string `description:"客户名称"`
  22. CustomerSocial string `description:"客户社会信用码"`
  23. ProjectType int `description:"活动类型:1=沙龙,2=路演,3=专家需求,4=研究需求,5=电话,6=面谈,7=专题需求,8=线下沙龙,9=公司调研"`
  24. ProjectFormType int `description:"服务形式:1=沙龙,2=路演,3=专家需求,4=研究需求,5=电话,6=面谈,7=专题需求"`
  25. Room int `description:"会议室id"`
  26. StartTime int `description:"开始时间戳"`
  27. EndTime int `description:"结束时间戳"`
  28. Content string `description:"活动内容"`
  29. FeedExpert string `description:"邀请的专家"`
  30. Title string `description:"日历显示的标题"`
  31. ResearcherMobile string `description:"研究员+协同人员手机号(多个使用逗号拼接)"`
  32. ModifyTime time.Time `description:"更新时间"`
  33. CreateTime time.Time `description:"关系建立时间"`
  34. }
  35. func GetRelationByPars(condition string, pars []interface{}) (items *RsCalendarRelation, err error) {
  36. o := orm.NewOrm()
  37. sql := `SELECT * FROM rs_calendar_relation WHERE 1=1 `
  38. if condition != "" {
  39. sql += condition
  40. }
  41. err = o.Raw(sql, pars).QueryRow(&items)
  42. return
  43. }
  44. func GetPhoneFromResearcher(calendarResearcherId int) (items *string, err error) {
  45. o := orm.NewOrm()
  46. sql := `SELECT mobile FROM admin AS a INNER JOIN rs_calendar_researcher AS b ON a.admin_id=b.researcher_id WHERE b.rs_calendar_researcher_id=?`
  47. err = o.Raw(sql, calendarResearcherId).QueryRow(&items)
  48. return
  49. }
  50. func GetPhoneFromRsCalendarById(rsCalendarId int) (item *string, err error) {
  51. o := orm.NewOrm()
  52. sql := `SELECT mobile FROM admin AS a INNER JOIN rs_calendar AS b ON a.admin_id=b.sys_user_id WHERE b.rs_calendar_id=? `
  53. err = o.Raw(sql, rsCalendarId).QueryRow(&item)
  54. return
  55. }
  56. // GetRsCalendarRelationListByThirdIds 根据第三方id集合获取所有的关系列表
  57. func GetRsCalendarRelationListByThirdIds(thirdCalendarIds []int) (items []*RsCalendarRelation, err error) {
  58. if len(thirdCalendarIds) <= 0 {
  59. return
  60. }
  61. thirdCalendarIdStr := utils.Implode(thirdCalendarIds)
  62. o := orm.NewOrm()
  63. sql := `SELECT * FROM rs_calendar_relation WHERE third_calendar_id in (` + thirdCalendarIdStr + `) `
  64. _, err = o.Raw(sql).QueryRows(&items)
  65. return
  66. }
  67. // AddRsCalendarRelation 添加自系统路演与第三方路演关系
  68. func AddRsCalendarRelation(item *RsCalendarRelation) (lastId int64, err error) {
  69. o := orm.NewOrm()
  70. lastId, err = o.Insert(item)
  71. return
  72. }
  73. // DeleteRsCalendarRelation 删除关联表
  74. func DeleteRsCalendarRelation(relationIdd int) (err error) {
  75. sql := `DELETE FROM rs_calendar_relation WHERE relation_id=? `
  76. o := orm.NewOrm()
  77. _, err = o.Raw(sql, relationIdd).Exec()
  78. return
  79. }
  80. // SyncRsCalendarRelation 同步自系统路演与第三方路演关系
  81. func SyncRsCalendarRelation(thirdUserCalendar UserCalendar, createUser system.AdminItem, researcherList []system.AdminItem) (err error) {
  82. currentStartTimer := time.Unix(int64(thirdUserCalendar.StartTime), 0)
  83. currentEndTimer := time.Unix(int64(thirdUserCalendar.EndTime), 0)
  84. o := orm.NewOrm()
  85. to, err := o.Begin()
  86. if err != nil {
  87. return
  88. }
  89. defer func() {
  90. if err != nil {
  91. _ = to.Rollback()
  92. } else {
  93. _ = to.Commit()
  94. }
  95. }()
  96. //路演活动表入库
  97. rsCalendar := &RsCalendar{
  98. SysUserId: createUser.AdminId,
  99. SysUserRealName: createUser.RealName,
  100. ActivityType: "路演",
  101. RoadshowType: "",
  102. RoadshowPlatform: "",
  103. CompanyId: 0,
  104. CompanyName: thirdUserCalendar.CustomerName,
  105. Province: "",
  106. ProvinceCode: "",
  107. City: "",
  108. CityCode: "",
  109. Theme: "",
  110. CooperationName: "",
  111. Title: thirdUserCalendar.Title,
  112. Source: 1, //来源,0:自系统,1:上海方的
  113. CreateTime: time.Now(),
  114. ModifyTime: time.Now(),
  115. ActivityCategory: "",
  116. IsSynced: 1,
  117. }
  118. rsCalendarId, err := to.Insert(rsCalendar)
  119. if err != nil {
  120. return
  121. }
  122. rsCalendar.RsCalendarId = int(rsCalendarId)
  123. // 路演研究员入库
  124. rsCalendarResearcherList := make([]*RsCalendarResearcher, 0)
  125. for _, researcheInfo := range researcherList {
  126. rsCalendarResearcher := &RsCalendarResearcher{
  127. RsCalendarResearcherId: 0,
  128. RsCalendarId: rsCalendar.RsCalendarId,
  129. ResearcherId: researcheInfo.AdminId,
  130. ResearcherName: researcheInfo.RealName,
  131. StartDate: currentStartTimer.Format(utils.FormatDate),
  132. EndDate: currentEndTimer.Format(utils.FormatDate),
  133. StartTime: currentStartTimer.Format(utils.FormatTime),
  134. EndTime: currentEndTimer.Format(utils.FormatTime),
  135. StartWeek: utils.EnWeekToCnWeek(currentStartTimer.Weekday().String()),
  136. EndWeek: utils.EnWeekToCnWeek(currentEndTimer.Weekday().String()),
  137. CreateTime: time.Now(),
  138. ModifyTime: time.Now(),
  139. Status: 2, //1:待接受,2:已接受,3:已拒绝,4:已删除,5:已撤回,6:已结束
  140. RefuseReason: "",
  141. //RefuseTime: time.Time{},
  142. DeleteReason: "",
  143. IsSynced: 1,
  144. }
  145. rsCalendarResearcherId, tmpErr := to.Insert(rsCalendarResearcher)
  146. if tmpErr != nil {
  147. err = tmpErr
  148. return
  149. }
  150. rsCalendarResearcher.RsCalendarResearcherId = int(rsCalendarResearcherId)
  151. rsCalendarResearcherList = append(rsCalendarResearcherList, rsCalendarResearcher)
  152. }
  153. selfCalendarId := 0
  154. if len(rsCalendarResearcherList) > 0 {
  155. selfCalendarId = rsCalendarResearcherList[0].RsCalendarResearcherId
  156. }
  157. //关系入库
  158. customerId, _ := strconv.Atoi(thirdUserCalendar.CustomerId)
  159. rsCalendarRelation := &RsCalendarRelation{
  160. //RelationId: 0,
  161. CalendarType: 1, //日历类型;1:路演;2:事项
  162. SelfCalendarId: selfCalendarId, //研究员与路演关系id;
  163. ThirdCalendarId: thirdUserCalendar.ID,
  164. UserId: thirdUserCalendar.UserId,
  165. UserPhone: thirdUserCalendar.UserPhone,
  166. UserName: thirdUserCalendar.UserName,
  167. ProjectName: thirdUserCalendar.ProjectName,
  168. ProjectId: thirdUserCalendar.ProjectId,
  169. CustomerId: customerId,
  170. CustomerName: thirdUserCalendar.CustomerName,
  171. CustomerSocial: thirdUserCalendar.CustomerSocial,
  172. ProjectType: thirdUserCalendar.ProjectType,
  173. ProjectFormType: thirdUserCalendar.ProjectType,
  174. Room: thirdUserCalendar.Room,
  175. StartTime: thirdUserCalendar.StartTime,
  176. EndTime: thirdUserCalendar.EndTime,
  177. Content: thirdUserCalendar.Content,
  178. FeedExpert: thirdUserCalendar.FeedExpert,
  179. Title: thirdUserCalendar.Title,
  180. ResearcherMobile: thirdUserCalendar.ResearcherMobile,
  181. ModifyTime: time.Now(),
  182. CreateTime: time.Now(),
  183. }
  184. rsCalendarRelationId, err := to.Insert(rsCalendarRelation)
  185. if err != nil {
  186. return
  187. }
  188. rsCalendarRelation.RelationId = int(rsCalendarRelationId)
  189. return
  190. }
  191. // UpdateSyncRsCalendarRelation 同步自系统路演与第三方路演关系
  192. func UpdateSyncRsCalendarRelation(thirdUserCalendar UserCalendar, rsCalendar *RsCalendar, rsCalendarRelation *RsCalendarRelation, updateRsCalendarResearcherList []*RsCalendarResearcher, delResearcherIdList []int, addResearcherList []system.AdminItem) (err error) {
  193. currentStartTimer := time.Unix(int64(thirdUserCalendar.StartTime), 0)
  194. currentEndTimer := time.Unix(int64(thirdUserCalendar.EndTime), 0)
  195. //新增研究员
  196. //删除研究员
  197. //更新研究员
  198. o := orm.NewOrm()
  199. to, err := o.Begin()
  200. if err != nil {
  201. return
  202. }
  203. defer func() {
  204. if err != nil {
  205. _ = to.Rollback()
  206. } else {
  207. _ = to.Commit()
  208. }
  209. }()
  210. // 路演活动表修改
  211. rsCalendar.Title = thirdUserCalendar.Title
  212. rsCalendar.ModifyTime = time.Now()
  213. _, err = to.Update(rsCalendar, "Title", "ModifyTime")
  214. if err != nil {
  215. return
  216. }
  217. // 删除路演研究员
  218. if len(delResearcherIdList) > 0 {
  219. delResearcherIdStr := utils.Implode(delResearcherIdList)
  220. sql := `DELETE FROM rs_calendar_researcher WHERE rs_calendar_researcher_id in (` + delResearcherIdStr + `) `
  221. _, tmpErr := to.Raw(sql).Exec()
  222. if tmpErr != nil {
  223. err = tmpErr
  224. return
  225. }
  226. }
  227. // 修改路演研究员
  228. for _, rsCalendarResearcher := range updateRsCalendarResearcherList {
  229. rsCalendarResearcher.StartDate = currentStartTimer.Format(utils.FormatDate)
  230. rsCalendarResearcher.EndDate = currentEndTimer.Format(utils.FormatDate)
  231. rsCalendarResearcher.StartTime = currentStartTimer.Format(utils.FormatTime)
  232. rsCalendarResearcher.EndTime = currentEndTimer.Format(utils.FormatTime)
  233. rsCalendarResearcher.StartWeek = utils.EnWeekToCnWeek(currentStartTimer.Weekday().String())
  234. rsCalendarResearcher.EndWeek = utils.EnWeekToCnWeek(currentEndTimer.Weekday().String())
  235. rsCalendarResearcher.Status = 2 // 已接受
  236. rsCalendarResearcher.ModifyTime = time.Now()
  237. rsCalendarResearcher.IsSynced = 1
  238. _, tmpErr := to.Update(rsCalendarResearcher, "StartDate", "EndDate", "StartTime", "EndTime", "StartWeek", "EndWeek", "Status", "ModifyTime", "IsSynced")
  239. if tmpErr != nil {
  240. err = tmpErr
  241. return
  242. }
  243. }
  244. // 路演研究员入库
  245. rsCalendarResearcherList := make([]*RsCalendarResearcher, 0)
  246. for _, researcheInfo := range addResearcherList {
  247. rsCalendarResearcher := &RsCalendarResearcher{
  248. RsCalendarResearcherId: 0,
  249. RsCalendarId: rsCalendar.RsCalendarId,
  250. ResearcherId: researcheInfo.AdminId,
  251. ResearcherName: researcheInfo.RealName,
  252. StartDate: currentStartTimer.Format(utils.FormatDate),
  253. EndDate: currentEndTimer.Format(utils.FormatDate),
  254. StartTime: currentStartTimer.Format(utils.FormatTime),
  255. EndTime: currentEndTimer.Format(utils.FormatTime),
  256. StartWeek: utils.EnWeekToCnWeek(currentStartTimer.Weekday().String()),
  257. EndWeek: utils.EnWeekToCnWeek(currentEndTimer.Weekday().String()),
  258. CreateTime: time.Now(),
  259. ModifyTime: time.Now(),
  260. Status: 2, //1:待接受,2:已接受,3:已拒绝,4:已删除,5:已撤回,6:已结束
  261. RefuseReason: "",
  262. //RefuseTime: time.Time{},
  263. DeleteReason: "",
  264. IsSynced: 1,
  265. }
  266. rsCalendarResearcherId, tmpErr := to.Insert(rsCalendarResearcher)
  267. if tmpErr != nil {
  268. err = tmpErr
  269. return
  270. }
  271. rsCalendarResearcher.RsCalendarResearcherId = int(rsCalendarResearcherId)
  272. rsCalendarResearcherList = append(rsCalendarResearcherList, rsCalendarResearcher)
  273. }
  274. // 关系表变更
  275. selfCalendarId := rsCalendarRelation.SelfCalendarId
  276. if len(updateRsCalendarResearcherList) > 0 {
  277. selfCalendarId = updateRsCalendarResearcherList[0].RsCalendarResearcherId //更新的研究员关系表id
  278. } else if len(rsCalendarResearcherList) > 0 {
  279. selfCalendarId = rsCalendarResearcherList[0].RsCalendarResearcherId //新增的研究员关系表id
  280. }
  281. //关系入库
  282. customerId, _ := strconv.Atoi(thirdUserCalendar.CustomerId)
  283. rsCalendarRelation.SelfCalendarId = selfCalendarId
  284. rsCalendarRelation.UserId = thirdUserCalendar.UserId
  285. rsCalendarRelation.UserPhone = thirdUserCalendar.UserPhone
  286. rsCalendarRelation.UserName = thirdUserCalendar.UserName
  287. rsCalendarRelation.ProjectName = thirdUserCalendar.ProjectName
  288. rsCalendarRelation.ProjectId = thirdUserCalendar.ProjectId
  289. rsCalendarRelation.CustomerId = customerId
  290. rsCalendarRelation.CustomerName = thirdUserCalendar.CustomerName
  291. rsCalendarRelation.CustomerSocial = thirdUserCalendar.CustomerSocial
  292. rsCalendarRelation.ProjectType = thirdUserCalendar.ProjectType
  293. rsCalendarRelation.ProjectFormType = thirdUserCalendar.ProjectType
  294. rsCalendarRelation.Room = thirdUserCalendar.Room
  295. rsCalendarRelation.StartTime = thirdUserCalendar.StartTime
  296. rsCalendarRelation.EndTime = thirdUserCalendar.EndTime
  297. rsCalendarRelation.Content = thirdUserCalendar.Content
  298. rsCalendarRelation.FeedExpert = thirdUserCalendar.FeedExpert
  299. rsCalendarRelation.Title = thirdUserCalendar.Title
  300. rsCalendarRelation.ResearcherMobile = thirdUserCalendar.ResearcherMobile
  301. rsCalendarRelation.ModifyTime = time.Now()
  302. _, err = to.Update(rsCalendarRelation, "SelfCalendarId", "UserId", "UserPhone", "UserName", "ProjectName", "ProjectId", "CustomerId", "CustomerName", "CustomerSocial", "ProjectType", "ProjectFormType", "Room", "StartTime", "EndTime", "Content", "FeedExpert", "Title", "ResearcherMobile", "ModifyTime")
  303. return
  304. }
  305. // UpdateRsCalendarRelation 更新活动关系信息
  306. func UpdateRsCalendarRelation(where, updateParams map[string]interface{}) error {
  307. o := orm.NewOrm()
  308. ptrStructOrTableName := "rs_calendar"
  309. qs := o.QueryTable(ptrStructOrTableName)
  310. for expr, exprV := range where {
  311. qs = qs.Filter(expr, exprV)
  312. }
  313. _, err := qs.Update(updateParams)
  314. return err
  315. }
  316. // RsCalendarResearcherRelationInfo
  317. type RsCalendarResearcherRelationInfo struct {
  318. RsCalendarResearcherId int `orm:"column(rs_calendar_researcher_id);pk"`
  319. CalendarType int8 `description:"日历类型;1:路演;2:事项"`
  320. ThirdCalendarId int `description:"第三方路演id"`
  321. RsCalendarId int `description:"日历活动id"`
  322. ResearcherId int `description:"研究员id"`
  323. ResearcherName string `description:"研究员名称"`
  324. StartDate string `description:"开始日期"`
  325. EndDate string `description:"结束日期"`
  326. StartTime string `description:"开始时间"`
  327. EndTime string `description:"结束时间"`
  328. StartWeek string `description:"开始日期对应周"`
  329. EndWeek string `description:"结束日期对应周"`
  330. CreateTime time.Time
  331. ModifyTime time.Time
  332. Status int `description:"状态:1:待接受,2:已接受,3:已拒绝,4:已删除,5:已撤回,6:已结束"`
  333. RefuseReason string `description:"拒绝理由"`
  334. RefuseTime time.Time `description:"拒绝时间"`
  335. DeleteReason string `description:"删除理由"`
  336. DeleteTime time.Time `description:"删除时间"`
  337. ApproveTime time.Time `description:"接受时间"`
  338. IsSynced int `description:"是否与上海同步 0:未同步 1:已同步"`
  339. ResearcherSort int `description:"研究员新增排序"`
  340. }
  341. // GetRsCalendarResearcherInfoIByResearcherIdAndDate 根据研究员和开始/结束日期获取上海的活动路演
  342. func GetRsCalendarResearcherInfoIByResearcherIdAndDate(researcherId int, startDate, endDate string) (items []*RsCalendarResearcherRelationInfo, err error) {
  343. o := orm.NewOrm()
  344. // sql := `SELECT a.*,c.third_calendar_id,c.calendar_type FROM rs_calendar_researcher a join rs_calendar b on a.rs_calendar_id=b.rs_calendar_id
  345. //join rs_calendar_relation c on a.rs_calendar_researcher_id=c.self_calendar_id
  346. //WHERE b.source=1 and a.status=2 and c.calendar_type=1 and a.researcher_id=? and start_date>=? and end_date <= ? `
  347. //杭州创建的路演活动,如果上海被删除了,那么也要同步删除杭州的(所以相对于上面的逻辑,下面移除了来源的where条件)
  348. sql := `SELECT a.*,c.third_calendar_id,c.calendar_type FROM rs_calendar_researcher a
  349. join rs_calendar b on a.rs_calendar_id=b.rs_calendar_id
  350. join rs_calendar_relation c on a.rs_calendar_researcher_id=c.self_calendar_id
  351. WHERE a.status=2 and c.calendar_type=1 and a.researcher_id=? and start_date>=? and end_date <= ? `
  352. _, err = o.Raw(sql, researcherId, startDate, endDate).QueryRows(&items)
  353. return
  354. }