rs_calendar_relation.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. package roadshow
  2. import (
  3. "github.com/beego/beego/v2/client/orm"
  4. "hongze/hongze_mobile_admin/models/tables/admin"
  5. "hongze/hongze_mobile_admin/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 admin.AdminItem, researcherList []admin.AdminItem) (err error) {
  82. currentStartTimer := time.Unix(int64(thirdUserCalendar.StartTime), 0)
  83. currentEndTimer := time.Unix(int64(thirdUserCalendar.EndTime), 0)
  84. o := orm.NewOrm()
  85. tx, err := o.Begin()
  86. if err != nil {
  87. return
  88. }
  89. defer func() {
  90. if err != nil {
  91. _ = tx.Rollback()
  92. } else {
  93. _ = tx.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. }
  117. rsCalendarId, err := tx.Insert(rsCalendar)
  118. if err != nil {
  119. return
  120. }
  121. rsCalendar.RsCalendarId = int(rsCalendarId)
  122. // 路演研究员入库
  123. rsCalendarResearcherList := make([]*RsCalendarResearcher, 0)
  124. for _, researcheInfo := range researcherList {
  125. rsCalendarResearcher := &RsCalendarResearcher{
  126. RsCalendarResearcherId: 0,
  127. RsCalendarId: rsCalendar.RsCalendarId,
  128. ResearcherId: researcheInfo.AdminId,
  129. ResearcherName: researcheInfo.RealName,
  130. StartDate: currentStartTimer.Format(utils.FormatDate),
  131. EndDate: currentEndTimer.Format(utils.FormatDate),
  132. StartTime: currentStartTimer.Format(utils.FormatTime),
  133. EndTime: currentEndTimer.Format(utils.FormatTime),
  134. StartWeek: utils.StrDateTimeToWeek(currentStartTimer.Weekday().String()),
  135. EndWeek: utils.StrDateTimeToWeek(currentEndTimer.Weekday().String()),
  136. CreateTime: time.Now(),
  137. ModifyTime: time.Now(),
  138. Status: 2, //1:待接受,2:已接受,3:已拒绝,4:已删除,5:已撤回,6:已结束
  139. RefuseReason: "",
  140. //RefuseTime: time.Time{},
  141. DeleteReason: "",
  142. }
  143. rsCalendarResearcherId, tmpErr := tx.Insert(rsCalendarResearcher)
  144. if tmpErr != nil {
  145. err = tmpErr
  146. return
  147. }
  148. rsCalendarResearcher.RsCalendarResearcherId = int(rsCalendarResearcherId)
  149. rsCalendarResearcherList = append(rsCalendarResearcherList, rsCalendarResearcher)
  150. }
  151. selfCalendarId := 0
  152. if len(rsCalendarResearcherList) > 0 {
  153. selfCalendarId = rsCalendarResearcherList[0].RsCalendarResearcherId
  154. }
  155. //关系入库
  156. customerId, _ := strconv.Atoi(thirdUserCalendar.CustomerId)
  157. rsCalendarRelation := &RsCalendarRelation{
  158. //RelationId: 0,
  159. CalendarType: 1, //日历类型;1:路演;2:事项
  160. SelfCalendarId: selfCalendarId, //研究员与路演关系id;
  161. ThirdCalendarId: thirdUserCalendar.ID,
  162. UserId: thirdUserCalendar.UserId,
  163. UserPhone: thirdUserCalendar.UserPhone,
  164. UserName: thirdUserCalendar.UserName,
  165. ProjectName: thirdUserCalendar.ProjectName,
  166. ProjectId: thirdUserCalendar.ProjectId,
  167. CustomerId: customerId,
  168. CustomerName: thirdUserCalendar.CustomerName,
  169. CustomerSocial: thirdUserCalendar.CustomerSocial,
  170. ProjectType: thirdUserCalendar.ProjectType,
  171. ProjectFormType: thirdUserCalendar.ProjectType,
  172. Room: thirdUserCalendar.Room,
  173. StartTime: thirdUserCalendar.StartTime,
  174. EndTime: thirdUserCalendar.EndTime,
  175. Content: thirdUserCalendar.Content,
  176. FeedExpert: thirdUserCalendar.FeedExpert,
  177. Title: thirdUserCalendar.Title,
  178. ResearcherMobile: thirdUserCalendar.ResearcherMobile,
  179. ModifyTime: time.Now(),
  180. CreateTime: time.Now(),
  181. }
  182. rsCalendarRelationId, err := tx.Insert(rsCalendarRelation)
  183. if err != nil {
  184. return
  185. }
  186. rsCalendarRelation.RelationId = int(rsCalendarRelationId)
  187. return
  188. }
  189. // UpdateSyncRsCalendarRelation 同步自系统路演与第三方路演关系
  190. func UpdateSyncRsCalendarRelation(thirdUserCalendar UserCalendar, rsCalendar *RsCalendar, rsCalendarRelation *RsCalendarRelation, updateRsCalendarResearcherList []*RsCalendarResearcher, delResearcherIdList []int, addResearcherList []admin.AdminItem) (err error) {
  191. currentStartTimer := time.Unix(int64(thirdUserCalendar.StartTime), 0)
  192. currentEndTimer := time.Unix(int64(thirdUserCalendar.EndTime), 0)
  193. //新增研究员
  194. //删除研究员
  195. //更新研究员
  196. o := orm.NewOrm()
  197. tx, err := o.Begin()
  198. if err != nil {
  199. return
  200. }
  201. defer func() {
  202. if err != nil {
  203. _ = tx.Rollback()
  204. } else {
  205. _ = tx.Commit()
  206. }
  207. }()
  208. // 路演活动表修改
  209. rsCalendar.Title = thirdUserCalendar.Title
  210. rsCalendar.ModifyTime = time.Now()
  211. _, err = tx.Update(rsCalendar, "Title", "ModifyTime")
  212. if err != nil {
  213. return
  214. }
  215. // 删除路演研究员
  216. if len(delResearcherIdList) > 0 {
  217. delResearcherIdStr := utils.Implode(delResearcherIdList)
  218. sql := `DELETE FROM rs_calendar_researcher WHERE rs_calendar_researcher_id in (` + delResearcherIdStr + `) `
  219. _, tmpErr := tx.Raw(sql).Exec()
  220. if tmpErr != nil {
  221. err = tmpErr
  222. return
  223. }
  224. }
  225. // 修改路演研究员
  226. for _, rsCalendarResearcher := range updateRsCalendarResearcherList {
  227. rsCalendarResearcher.StartDate = currentStartTimer.Format(utils.FormatDate)
  228. rsCalendarResearcher.EndDate = currentEndTimer.Format(utils.FormatDate)
  229. rsCalendarResearcher.StartTime = currentStartTimer.Format(utils.FormatTime)
  230. rsCalendarResearcher.EndTime = currentEndTimer.Format(utils.FormatTime)
  231. rsCalendarResearcher.StartWeek = utils.StrDateTimeToWeek(currentStartTimer.Weekday().String())
  232. rsCalendarResearcher.EndWeek = utils.StrDateTimeToWeek(currentEndTimer.Weekday().String())
  233. rsCalendarResearcher.Status = 2 // 已接受
  234. rsCalendarResearcher.ModifyTime = time.Now()
  235. _, tmpErr := tx.Update(rsCalendarResearcher, "StartDate", "EndDate", "StartTime", "EndTime", "StartWeek", "EndWeek","Status", "ModifyTime")
  236. if tmpErr != nil {
  237. err = tmpErr
  238. return
  239. }
  240. }
  241. // 路演研究员入库
  242. rsCalendarResearcherList := make([]*RsCalendarResearcher, 0)
  243. for _, researcheInfo := range addResearcherList {
  244. rsCalendarResearcher := &RsCalendarResearcher{
  245. RsCalendarResearcherId: 0,
  246. RsCalendarId: rsCalendar.RsCalendarId,
  247. ResearcherId: researcheInfo.AdminId,
  248. ResearcherName: researcheInfo.RealName,
  249. StartDate: currentStartTimer.Format(utils.FormatDate),
  250. EndDate: currentEndTimer.Format(utils.FormatDate),
  251. StartTime: currentStartTimer.Format(utils.FormatTime),
  252. EndTime: currentEndTimer.Format(utils.FormatTime),
  253. StartWeek: utils.StrDateTimeToWeek(currentStartTimer.Weekday().String()),
  254. EndWeek: utils.StrDateTimeToWeek(currentEndTimer.Weekday().String()),
  255. CreateTime: time.Now(),
  256. ModifyTime: time.Now(),
  257. Status: 2, //1:待接受,2:已接受,3:已拒绝,4:已删除,5:已撤回,6:已结束
  258. RefuseReason: "",
  259. //RefuseTime: time.Time{},
  260. DeleteReason: "",
  261. }
  262. rsCalendarResearcherId, tmpErr := tx.Insert(rsCalendarResearcher)
  263. if tmpErr != nil {
  264. err = tmpErr
  265. return
  266. }
  267. rsCalendarResearcher.RsCalendarResearcherId = int(rsCalendarResearcherId)
  268. rsCalendarResearcherList = append(rsCalendarResearcherList, rsCalendarResearcher)
  269. }
  270. // 关系表变更
  271. selfCalendarId := rsCalendarRelation.SelfCalendarId
  272. if len(updateRsCalendarResearcherList) > 0 {
  273. selfCalendarId = updateRsCalendarResearcherList[0].RsCalendarResearcherId //更新的研究员关系表id
  274. } else if len(rsCalendarResearcherList) > 0 {
  275. selfCalendarId = rsCalendarResearcherList[0].RsCalendarResearcherId //新增的研究员关系表id
  276. }
  277. //关系入库
  278. customerId, _ := strconv.Atoi(thirdUserCalendar.CustomerId)
  279. rsCalendarRelation.SelfCalendarId = selfCalendarId
  280. rsCalendarRelation.UserId = thirdUserCalendar.UserId
  281. rsCalendarRelation.UserPhone = thirdUserCalendar.UserPhone
  282. rsCalendarRelation.UserName = thirdUserCalendar.UserName
  283. rsCalendarRelation.ProjectName = thirdUserCalendar.ProjectName
  284. rsCalendarRelation.ProjectId = thirdUserCalendar.ProjectId
  285. rsCalendarRelation.CustomerId = customerId
  286. rsCalendarRelation.CustomerName = thirdUserCalendar.CustomerName
  287. rsCalendarRelation.CustomerSocial = thirdUserCalendar.CustomerSocial
  288. rsCalendarRelation.ProjectType = thirdUserCalendar.ProjectType
  289. rsCalendarRelation.ProjectFormType = thirdUserCalendar.ProjectType
  290. rsCalendarRelation.Room = thirdUserCalendar.Room
  291. rsCalendarRelation.StartTime = thirdUserCalendar.StartTime
  292. rsCalendarRelation.EndTime = thirdUserCalendar.EndTime
  293. rsCalendarRelation.Content = thirdUserCalendar.Content
  294. rsCalendarRelation.FeedExpert = thirdUserCalendar.FeedExpert
  295. rsCalendarRelation.Title = thirdUserCalendar.Title
  296. rsCalendarRelation.ResearcherMobile = thirdUserCalendar.ResearcherMobile
  297. rsCalendarRelation.ModifyTime = time.Now()
  298. _, err = tx.Update(rsCalendarRelation, "SelfCalendarId", "UserId", "UserPhone", "UserName", "ProjectName", "ProjectId", "CustomerId", "CustomerName", "CustomerSocial", "ProjectType", "ProjectFormType", "Room", "StartTime", "EndTime", "Content", "FeedExpert", "Title", "ResearcherMobile", "ModifyTime")
  299. return
  300. }
  301. // RsCalendarResearcherRelationInfo
  302. type RsCalendarResearcherRelationInfo struct {
  303. RsCalendarResearcherId int `orm:"column(rs_calendar_researcher_id);pk"`
  304. CalendarType int8 `description:"日历类型;1:路演;2:事项"`
  305. ThirdCalendarId int `description:"第三方路演id"`
  306. RsCalendarId int `description:"日历活动id"`
  307. ResearcherId int `description:"研究员id"`
  308. ResearcherName string `description:"研究员名称"`
  309. StartDate string `description:"开始日期"`
  310. EndDate string `description:"结束日期"`
  311. StartTime string `description:"开始时间"`
  312. EndTime string `description:"结束时间"`
  313. StartWeek string `description:"开始日期对应周"`
  314. EndWeek string `description:"结束日期对应周"`
  315. CreateTime time.Time
  316. ModifyTime time.Time
  317. Status int `description:"状态:1:待接受,2:已接受,3:已拒绝,4:已删除,5:已撤回,6:已结束"`
  318. RefuseReason string `description:"拒绝理由"`
  319. RefuseTime time.Time `description:"拒绝时间"`
  320. DeleteReason string `description:"删除理由"`
  321. DeleteTime time.Time `description:"删除时间"`
  322. ApproveTime time.Time `description:"接受时间"`
  323. IsSynced int `description:"是否与上海同步 0:未同步 1:已同步"`
  324. ResearcherSort int `description:"研究员新增排序"`
  325. }
  326. // GetRsCalendarResearcherInfoIByResearcherIdAndDate 根据研究员和开始/结束日期获取上海的活动路演
  327. func GetRsCalendarResearcherInfoIByResearcherIdAndDate(researcherId int, startDate, endDate string) (items []*RsCalendarResearcherRelationInfo, err error) {
  328. o := orm.NewOrm()
  329. // 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
  330. //join rs_calendar_relation c on a.rs_calendar_researcher_id=c.self_calendar_id
  331. //WHERE b.source=1 and a.status=2 and c.calendar_type=1 and a.researcher_id=? and start_date>=? and end_date <= ? `
  332. //杭州创建的路演活动,如果上海被删除了,那么也要同步删除杭州的(所以相对于上面的逻辑,下面移除了来源的where条件)
  333. sql := `SELECT a.*,c.third_calendar_id,c.calendar_type FROM rs_calendar_researcher a
  334. join rs_calendar b on a.rs_calendar_id=b.rs_calendar_id
  335. join rs_calendar_relation c on a.rs_calendar_researcher_id=c.self_calendar_id
  336. WHERE a.status=2 and c.calendar_type=1 and a.researcher_id=? and start_date>=? and end_date <= ? `
  337. _, err = o.Raw(sql, researcherId, startDate, endDate).QueryRows(&items)
  338. return
  339. }