rs_calendar_relation.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. package roadshow
  2. import (
  3. "github.com/rdlucklib/rdluck_tools/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.Admin, researcherList []admin.Admin) (err error) {
  82. currentStartTimer := time.Unix(int64(thirdUserCalendar.StartTime), 0)
  83. currentEndTimer := time.Unix(int64(thirdUserCalendar.EndTime), 0)
  84. o := orm.NewOrm()
  85. o.Begin()
  86. defer func() {
  87. if err != nil {
  88. o.Rollback()
  89. } else {
  90. o.Commit()
  91. }
  92. }()
  93. //路演活动表入库
  94. rsCalendar := &RsCalendar{
  95. SysUserId: createUser.AdminId,
  96. SysUserRealName: createUser.RealName,
  97. ActivityType: "路演",
  98. RoadshowType: "",
  99. RoadshowPlatform: "",
  100. CompanyId: 0,
  101. CompanyName: thirdUserCalendar.CustomerName,
  102. Province: "",
  103. ProvinceCode: "",
  104. City: "",
  105. CityCode: "",
  106. Theme: "",
  107. CooperationName: "",
  108. Title: thirdUserCalendar.Title,
  109. Source: 1, //来源,0:自系统,1:上海方的
  110. CreateTime: time.Now(),
  111. ModifyTime: time.Now(),
  112. ActivityCategory: "",
  113. }
  114. rsCalendarId, err := o.Insert(rsCalendar)
  115. if err != nil {
  116. return
  117. }
  118. rsCalendar.RsCalendarId = int(rsCalendarId)
  119. // 路演研究员入库
  120. rsCalendarResearcherList := make([]*RsCalendarResearcher, 0)
  121. for _, researcheInfo := range researcherList {
  122. rsCalendarResearcher := &RsCalendarResearcher{
  123. RsCalendarResearcherId: 0,
  124. RsCalendarId: rsCalendar.RsCalendarId,
  125. ResearcherId: researcheInfo.AdminId,
  126. ResearcherName: researcheInfo.RealName,
  127. StartDate: currentStartTimer.Format(utils.FormatDate),
  128. EndDate: currentEndTimer.Format(utils.FormatDate),
  129. StartTime: currentStartTimer.Format(utils.FormatTime),
  130. EndTime: currentEndTimer.Format(utils.FormatTime),
  131. StartWeek: utils.StrDateTimeToWeek(currentStartTimer.Weekday().String()),
  132. EndWeek: utils.StrDateTimeToWeek(currentEndTimer.Weekday().String()),
  133. CreateTime: time.Now(),
  134. ModifyTime: time.Now(),
  135. Status: 2, //1:待接受,2:已接受,3:已拒绝,4:已删除,5:已撤回,6:已结束
  136. RefuseReason: "",
  137. //RefuseTime: time.Time{},
  138. DeleteReason: "",
  139. }
  140. rsCalendarResearcherId, tmpErr := o.Insert(rsCalendarResearcher)
  141. if tmpErr != nil {
  142. err = tmpErr
  143. return
  144. }
  145. rsCalendarResearcher.RsCalendarResearcherId = int(rsCalendarResearcherId)
  146. rsCalendarResearcherList = append(rsCalendarResearcherList, rsCalendarResearcher)
  147. }
  148. selfCalendarId := 0
  149. if len(rsCalendarResearcherList) > 0 {
  150. selfCalendarId = rsCalendarResearcherList[0].RsCalendarResearcherId
  151. }
  152. //关系入库
  153. customerId, _ := strconv.Atoi(thirdUserCalendar.CustomerId)
  154. rsCalendarRelation := &RsCalendarRelation{
  155. //RelationId: 0,
  156. CalendarType: 1, //日历类型;1:路演;2:事项
  157. SelfCalendarId: selfCalendarId, //研究员与路演关系id;
  158. ThirdCalendarId: thirdUserCalendar.ID,
  159. UserId: thirdUserCalendar.UserId,
  160. UserPhone: thirdUserCalendar.UserPhone,
  161. UserName: thirdUserCalendar.UserName,
  162. ProjectName: thirdUserCalendar.ProjectName,
  163. ProjectId: thirdUserCalendar.ProjectId,
  164. CustomerId: customerId,
  165. CustomerName: thirdUserCalendar.CustomerName,
  166. CustomerSocial: thirdUserCalendar.CustomerSocial,
  167. ProjectType: thirdUserCalendar.ProjectType,
  168. ProjectFormType: thirdUserCalendar.ProjectType,
  169. Room: thirdUserCalendar.Room,
  170. StartTime: thirdUserCalendar.StartTime,
  171. EndTime: thirdUserCalendar.EndTime,
  172. Content: thirdUserCalendar.Content,
  173. FeedExpert: thirdUserCalendar.FeedExpert,
  174. Title: thirdUserCalendar.Title,
  175. ResearcherMobile: thirdUserCalendar.ResearcherMobile,
  176. ModifyTime: time.Now(),
  177. CreateTime: time.Now(),
  178. }
  179. rsCalendarRelationId, err := o.Insert(rsCalendarRelation)
  180. if err != nil {
  181. return
  182. }
  183. rsCalendarRelation.RelationId = int(rsCalendarRelationId)
  184. return
  185. }
  186. // UpdateSyncRsCalendarRelation 同步自系统路演与第三方路演关系
  187. func UpdateSyncRsCalendarRelation(thirdUserCalendar UserCalendar, rsCalendar *RsCalendar, rsCalendarRelation *RsCalendarRelation, updateRsCalendarResearcherList []*RsCalendarResearcher, delResearcherIdList []int, addResearcherList []admin.Admin) (err error) {
  188. currentStartTimer := time.Unix(int64(thirdUserCalendar.StartTime), 0)
  189. currentEndTimer := time.Unix(int64(thirdUserCalendar.EndTime), 0)
  190. //新增研究员
  191. //删除研究员
  192. //更新研究员
  193. o := orm.NewOrm()
  194. o.Begin()
  195. defer func() {
  196. if err != nil {
  197. o.Rollback()
  198. } else {
  199. o.Commit()
  200. }
  201. }()
  202. // 路演活动表修改
  203. rsCalendar.Title = thirdUserCalendar.Title
  204. rsCalendar.ModifyTime = time.Now()
  205. _, err = o.Update(rsCalendar, "Title", "ModifyTime")
  206. if err != nil {
  207. return
  208. }
  209. // 删除路演研究员
  210. if len(delResearcherIdList) > 0 {
  211. delResearcherIdStr := utils.Implode(delResearcherIdList)
  212. sql := `DELETE FROM rs_calendar_researcher WHERE rs_calendar_researcher_id in (` + delResearcherIdStr + `) `
  213. _, tmpErr := o.Raw(sql).Exec()
  214. if tmpErr != nil {
  215. err = tmpErr
  216. return
  217. }
  218. }
  219. // 修改路演研究员
  220. for _, rsCalendarResearcher := range updateRsCalendarResearcherList {
  221. rsCalendarResearcher.StartDate = currentStartTimer.Format(utils.FormatDate)
  222. rsCalendarResearcher.EndDate = currentEndTimer.Format(utils.FormatDate)
  223. rsCalendarResearcher.StartTime = currentStartTimer.Format(utils.FormatTime)
  224. rsCalendarResearcher.EndTime = currentEndTimer.Format(utils.FormatTime)
  225. rsCalendarResearcher.StartWeek = utils.StrDateTimeToWeek(currentStartTimer.Weekday().String())
  226. rsCalendarResearcher.EndWeek = utils.StrDateTimeToWeek(currentEndTimer.Weekday().String())
  227. rsCalendarResearcher.ModifyTime = time.Now()
  228. _, tmpErr := o.Update(rsCalendar, "StartDate", "EndDate", "StartTime", "EndTime", "StartWeek", "EndWeek", "ModifyTime")
  229. if tmpErr != nil {
  230. err = tmpErr
  231. return
  232. }
  233. }
  234. // 路演研究员入库
  235. rsCalendarResearcherList := make([]*RsCalendarResearcher, 0)
  236. for _, researcheInfo := range addResearcherList {
  237. rsCalendarResearcher := &RsCalendarResearcher{
  238. RsCalendarResearcherId: 0,
  239. RsCalendarId: rsCalendar.RsCalendarId,
  240. ResearcherId: researcheInfo.AdminId,
  241. ResearcherName: researcheInfo.RealName,
  242. StartDate: currentStartTimer.Format(utils.FormatDate),
  243. EndDate: currentEndTimer.Format(utils.FormatDate),
  244. StartTime: currentStartTimer.Format(utils.FormatTime),
  245. EndTime: currentEndTimer.Format(utils.FormatTime),
  246. StartWeek: utils.StrDateTimeToWeek(currentStartTimer.Weekday().String()),
  247. EndWeek: utils.StrDateTimeToWeek(currentEndTimer.Weekday().String()),
  248. CreateTime: time.Now(),
  249. ModifyTime: time.Now(),
  250. Status: 2, //1:待接受,2:已接受,3:已拒绝,4:已删除,5:已撤回,6:已结束
  251. RefuseReason: "",
  252. //RefuseTime: time.Time{},
  253. DeleteReason: "",
  254. }
  255. rsCalendarResearcherId, tmpErr := o.Insert(rsCalendarResearcher)
  256. if tmpErr != nil {
  257. err = tmpErr
  258. return
  259. }
  260. rsCalendarResearcher.RsCalendarResearcherId = int(rsCalendarResearcherId)
  261. rsCalendarResearcherList = append(rsCalendarResearcherList, rsCalendarResearcher)
  262. }
  263. // 关系表变更
  264. selfCalendarId := rsCalendarRelation.SelfCalendarId
  265. if len(updateRsCalendarResearcherList) > 0 {
  266. selfCalendarId = updateRsCalendarResearcherList[0].RsCalendarResearcherId //更新的研究员关系表id
  267. } else if len(rsCalendarResearcherList) > 0 {
  268. selfCalendarId = rsCalendarResearcherList[0].RsCalendarResearcherId //新增的研究员关系表id
  269. }
  270. //关系入库
  271. customerId, _ := strconv.Atoi(thirdUserCalendar.CustomerId)
  272. rsCalendarRelation.SelfCalendarId = selfCalendarId
  273. rsCalendarRelation.UserId = thirdUserCalendar.UserId
  274. rsCalendarRelation.UserPhone = thirdUserCalendar.UserPhone
  275. rsCalendarRelation.UserName = thirdUserCalendar.UserName
  276. rsCalendarRelation.ProjectName = thirdUserCalendar.ProjectName
  277. rsCalendarRelation.ProjectId = thirdUserCalendar.ProjectId
  278. rsCalendarRelation.CustomerId = customerId
  279. rsCalendarRelation.CustomerName = thirdUserCalendar.CustomerName
  280. rsCalendarRelation.CustomerSocial = thirdUserCalendar.CustomerSocial
  281. rsCalendarRelation.ProjectType = thirdUserCalendar.ProjectType
  282. rsCalendarRelation.ProjectFormType = thirdUserCalendar.ProjectType
  283. rsCalendarRelation.Room = thirdUserCalendar.Room
  284. rsCalendarRelation.StartTime = thirdUserCalendar.StartTime
  285. rsCalendarRelation.EndTime = thirdUserCalendar.EndTime
  286. rsCalendarRelation.Content = thirdUserCalendar.Content
  287. rsCalendarRelation.FeedExpert = thirdUserCalendar.FeedExpert
  288. rsCalendarRelation.Title = thirdUserCalendar.Title
  289. rsCalendarRelation.ResearcherMobile = thirdUserCalendar.ResearcherMobile
  290. rsCalendarRelation.ModifyTime = time.Now()
  291. _, err = o.Update(rsCalendarRelation, "SelfCalendarId", "UserId", "UserPhone", "UserName", "ProjectName", "ProjectId", "CustomerId", "CustomerName", "CustomerSocial", "ProjectType", "ProjectFormType", "Room", "StartTime", "EndTime", "Content", "FeedExpert", "Title", "ResearcherMobile", "ModifyTime")
  292. return
  293. }