report_record.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. package roadshow
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/client/orm"
  5. "hongze/hz_crm_api/utils"
  6. "strings"
  7. "time"
  8. )
  9. type RsReportRecord struct {
  10. RsReportRecordId int `orm:"column(rs_report_record_id);pk"`
  11. CompanyId int `description:"客户id"`
  12. CompanyName string `description:"客户名称"`
  13. CreditCode string `description:"社会统一信用码"`
  14. CompanyStatus string `description:"客户状态:'试用','永续','冻结','流失','正式','潜在'"`
  15. ResearcherId int `description:"研究员id"`
  16. ResearcherName string `description:"研究员名称"`
  17. ResearcherGroupId int `description:"研究员分组id"`
  18. ResearcherGroupName string `description:"研究员分组名称"`
  19. SellerId int `description:"所属销售id"`
  20. SellerName string `description:"所属销售名称"`
  21. SellerGroupId int `description:"销售分组id"`
  22. SellerGroupName string `description:"销售分组名称"`
  23. RsCalendarId int `description:"路演活动id"`
  24. RsCalendarResearcherId int `description:"路演研究员id"`
  25. StartDate string `description:"开始日期"`
  26. EndDate string `description:"结束日期"`
  27. StartTime string `description:"开始时间"`
  28. EndTime string `description:"结束时间"`
  29. StartWeek string `description:"开始日期对应周"`
  30. EndWeek string `description:"结束日期对应周"`
  31. CreateTime time.Time `description:"记录创建时间"`
  32. RsCalendarResearcherStatus int `description:"路演活动状态"`
  33. PermissionName string `description:"开通品种"`
  34. }
  35. // RsReportRecordResp 总统计信息
  36. type RsReportRecordResp struct {
  37. List []GroupReportRecord `description:"系统用户分组信息"`
  38. RsReportRecordNumList []RsReportRecordNum `description:"统计次数"`
  39. }
  40. // GroupReportRecord 分组统计信息
  41. type GroupReportRecord struct {
  42. Item []AdminReportRecord `description:"系统用户信息"`
  43. Name string `description:"分组名"`
  44. GruopId int `description:"分组id"`
  45. RsReportRecordNumList []RsReportRecordNum `description:"统计次数"`
  46. }
  47. // AdminReportRecord 系统用户统计信息
  48. type AdminReportRecord struct {
  49. Name string `description:"系统用户名"`
  50. AdminId int `description:"系统用户id"`
  51. RsReportRecordNumList []RsReportRecordNum `description:"统计次数"`
  52. }
  53. // RsReportRecordNum 系统用户统计信息
  54. type RsReportRecordNum struct {
  55. TryOutNum int `description:"试用路演次数"`
  56. FormalNum int `description:"正式路演次数"`
  57. MeetingNum int `description:"会议次数"`
  58. OnlineNum int `description:"线上路演次数"`
  59. OfflineNum int `description:"线上路演次数"`
  60. RoadShowNum int `description:"路演总次数"`
  61. SalonNum int `description:"沙龙路演次数"`
  62. StartDate string `description:"开始日期"`
  63. EndDate string `description:"结束日期"`
  64. IsLook bool `description:"是否可以查看详情"`
  65. }
  66. func DeleteRsReportRecord(rsCalendarId, rsCalendarResearcherId int) (err error) {
  67. o := orm.NewOrm()
  68. sql := ` DELETE FROM rs_report_record WHERE rs_calendar_id=? `
  69. _, err = o.Raw(sql, rsCalendarId).Exec()
  70. return
  71. }
  72. type RsReportRecordGroup struct {
  73. AdminId int `description:"系统用户id"`
  74. GroupId int `description:"系统用户分组id"`
  75. AdminName string `description:"系统用户名称"`
  76. Num int `description:"汇总次数"`
  77. }
  78. // GetGroupResearcherRecordList 获取研究员分组数据
  79. func GetGroupResearcherRecordList(condition string, pars []interface{}) (list []*RsReportRecordGroup, err error) {
  80. o := orm.NewOrm()
  81. sql := ` SELECT researcher_id as admin_id,researcher_group_id as group_id,researcher_name admin_name,count(1) num
  82. FROM rs_report_record
  83. WHERE 1=1 and rs_calendar_researcher_status = 2
  84. `
  85. if condition != "" {
  86. sql += condition
  87. }
  88. sql += ` GROUP BY researcher_id`
  89. _, err = o.Raw(sql, pars).QueryRows(&list)
  90. return
  91. }
  92. // GetGroupSellerRecordList 获取销售分组数据
  93. func GetGroupSellerRecordList(condition string, pars []interface{}) (list []*RsReportRecordGroup, err error) {
  94. o := orm.NewOrm()
  95. sql := ` SELECT seller_id as admin_id,seller_group_id as group_id,seller_name admin_name,count(1) num
  96. FROM rs_report_record
  97. WHERE 1=1 and rs_calendar_researcher_status = 2
  98. `
  99. if condition != "" {
  100. sql += condition
  101. }
  102. sql += ` GROUP BY seller_id`
  103. _, err = o.Raw(sql, pars).QueryRows(&list)
  104. return
  105. }
  106. // RsReportRecordList 路演统计详情返回数据
  107. type RsReportRecordList struct {
  108. RsReportRecordId int `orm:"column(rs_report_record_id);pk"`
  109. CompanyId int `description:"客户id"`
  110. CompanyName string `description:"客户名称"`
  111. CreditCode string `description:"社会统一信用码"`
  112. CompanyStatus string `description:"客户状态:'试用','永续','冻结','流失','正式','潜在'"`
  113. ResearcherId int `description:"研究员id"`
  114. ResearcherName string `description:"研究员名称"`
  115. SellerId int `description:"所属销售id"`
  116. SellerName string `description:"所属销售名称"`
  117. RsCalendarId int `description:"路演活动id"`
  118. RsCalendarResearcherId int `description:"路演研究员id"`
  119. StartDate string `description:"开始日期"`
  120. EndDate string `description:"结束日期"`
  121. StartTime string `description:"开始时间"`
  122. EndTime string `description:"结束时间"`
  123. StartWeek string `description:"开始日期对应周"`
  124. EndWeek string `description:"结束日期对应周"`
  125. CreateTime time.Time `description:"记录创建时间"`
  126. RsCalendarResearcherStatus int `description:"路演活动状态"`
  127. ActivityType string `description:"活动类型"`
  128. RoadshowType string `description:"路演形式"`
  129. RoadshowPlatform string `description:"路演平台"`
  130. Theme string `description:"会议主题"`
  131. Province string `description:"省"`
  132. City string `description:"市"`
  133. CooperationName string `description:"合作方名称"`
  134. }
  135. // GetRsReportRecordList 获取路演统计详情返回数据
  136. func GetRsReportRecordList(condition string, pars []interface{}) (list []*RsReportRecordList, err error) {
  137. o := orm.NewOrm()
  138. sql := ` SELECT a.*,b.theme,b.roadshow_platform,b.province,b.city,b.cooperation_name
  139. FROM rs_report_record a
  140. join rs_calendar b on a.rs_calendar_id=b.rs_calendar_id
  141. WHERE 1=1 and a.rs_calendar_researcher_status = 2
  142. `
  143. if condition != "" {
  144. sql += condition
  145. }
  146. sql += ` order BY a.start_date asc,a.start_time asc`
  147. _, err = o.Raw(sql, pars).QueryRows(&list)
  148. return
  149. }
  150. type BusinessTripResp struct {
  151. GroupList []*BusinessTripGroup
  152. BaseDate string `description:"用于查询上下两周的开始日期"`
  153. }
  154. type BusinessTripGroup struct {
  155. GroupName string
  156. GroupId int
  157. ResearcherList []*BusinessTripResearcher
  158. }
  159. type BusinessTripResearcher struct {
  160. AdminId int `description:"研究员id"`
  161. RealName string `description:"研究员名称"`
  162. GroupId int `description:"分组id"`
  163. GroupName string `description:"分组名称"`
  164. RoleTypeCode string `description:"角色编码"`
  165. Province string `description:"省"`
  166. City string `description:"市"`
  167. BusinessTripList []*BusinessTrip
  168. }
  169. type BusinessTrip struct {
  170. WeekDate string `description:"开始日期"`
  171. City string `description:"城市"`
  172. Week string `description:"周"`
  173. WeekType string `description:"当前周:current,下一周:next"`
  174. }
  175. // GetResearcherV2 获取研究员列表(冻结的也要)
  176. func GetBusinessTripResearcher() (list []*BusinessTripResearcher, err error) {
  177. o := orm.NewOrm()
  178. sql := ` SELECT * FROM admin AS a
  179. WHERE a.role_type_code IN('researcher','rai_researcher','ficc_researcher','ficc_admin')
  180. AND a.enabled=1 and admin_id !=92 `
  181. _, err = o.Raw(sql).QueryRows(&list)
  182. return
  183. }
  184. type BusinessTripResearcherList struct {
  185. Province string `description:"省"`
  186. City string `description:"市"`
  187. StartDate string `description:"路演开始日期"`
  188. EndDate string `description:"路演结束日期"`
  189. ResearcherId int `description:"研究员id"`
  190. ResearcherName string `description:"研究员姓名"`
  191. StartDateTime string `description:"研究员姓名"`
  192. }
  193. func GetBusinessTripList(adminId int, startDate, endDate string) (list []*BusinessTripResearcherList, err error) {
  194. o := orm.NewOrm()
  195. sql := ` SELECT a.province,a.city,b.start_date,b.end_date,b.researcher_id,b.researcher_name,CONCAT(b.start_date,' ',b.start_time) AS start_date_time
  196. FROM rs_calendar AS a
  197. INNER JOIN rs_calendar_researcher AS b ON a.rs_calendar_id=b.rs_calendar_id
  198. INNER JOIN admin AS c ON b.researcher_id=c.admin_id
  199. WHERE a.roadshow_type='线下'
  200. AND b.status=2
  201. AND a.city<>c.city
  202. AND b.start_date>=?
  203. AND b.start_date<=? `
  204. if adminId > 0 {
  205. sql += ` AND b.researcher_id=? `
  206. //sql += ` GROUP BY b.researcher_id,b.start_date `
  207. sql += ` ORDER BY b.researcher_id ASC,start_date_time ASC `
  208. _, err = o.Raw(sql, startDate, endDate, adminId).QueryRows(&list)
  209. } else {
  210. //sql += ` GROUP BY b.researcher_id,b.start_date `
  211. sql += ` ORDER BY b.researcher_id ASC,start_date_time ASC `
  212. _, err = o.Raw(sql, startDate, endDate).QueryRows(&list)
  213. }
  214. return
  215. }
  216. // GetOverseasGroupSellerRecordList 获取销售分组数据
  217. func _GetOverseasGroupSellerRecordList(condition string, pars []interface{}) (list []*RsReportRecordGroup, err error) {
  218. o := orm.NewOrm()
  219. sql := ` SELECT seller_id as admin_id,seller_group_id as group_id,seller_name admin_name,count(1) num
  220. FROM rs_report_record
  221. WHERE 1=1 and rs_calendar_researcher_status = 2
  222. `
  223. if condition != "" {
  224. sql += condition
  225. }
  226. sql += ` GROUP BY seller_id`
  227. _, err = o.Raw(sql, pars).QueryRows(&list)
  228. return
  229. }
  230. // OverseasAdminReportRecord 系统用户统计信息
  231. type OverseasAdminReportRecord struct {
  232. Name string `description:"系统用户名"`
  233. AdminId int `description:"系统用户id"`
  234. RsReportRecordNumList []RsOverseasReportRecordNum `description:"统计次数"`
  235. }
  236. // RsOverseasReportRecordNum 系统用户统计信息
  237. type RsOverseasReportRecordNum struct {
  238. TryOutNum int `description:"试用路演次数"`
  239. FormalNum int `description:"正式路演次数"`
  240. CloseNum int `description:"关闭客户次数"`
  241. OnlineNum int `description:"线上路演次数"`
  242. OfflineNum int `description:"线上路演次数"`
  243. StartDate string `description:"开始日期"`
  244. EndDate string `description:"结束日期"`
  245. IsLook bool `description:"是否可以查看详情"`
  246. }
  247. // RsOverseasReportRecordResp 总统计信息
  248. type RsOverseasReportRecordResp struct {
  249. List []OverseasAdminReportRecord `description:"系统用户分组信息"`
  250. RsReportRecordNumList []RsOverseasReportRecordNum `description:"统计次数"`
  251. }
  252. func GetOverseasGroupResearcherRecordList(startDate, endDate, companyStatus string) (list []*RsReportRecordGroup, err error) {
  253. var databaseName string
  254. if utils.RunMode == "debug" {
  255. databaseName = "test_v2_hongze_rddp"
  256. } else {
  257. databaseName = "hongze_rddp"
  258. }
  259. companyStatusList := make([]string, 0)
  260. if companyStatus != "" {
  261. companyStatusList = strings.Split(companyStatus, ",")
  262. }
  263. lenCompanyStatusList := len(companyStatusList)
  264. o := orm.NewOrm()
  265. pars := make([]interface{}, 0)
  266. sql1 := `SELECT a.rs_calendar_id,b.researcher_id as admin_id,b.researcher_group_id as group_id,b.researcher_name AS admin_name FROM rs_calendar AS a
  267. INNER JOIN rs_report_record AS b ON a.rs_calendar_id=b.rs_calendar_id
  268. JOIN company_product AS c ON a.company_id=c.company_id AND c.product_id=1
  269. JOIN company AS d ON c.company_id=d.company_id
  270. INNER JOIN overseas_custom_seller AS f ON c.seller_id=f.seller_id
  271. where c.is_overseas = 0 and a.english_company= 0 AND a.source = 0 AND b.rs_calendar_researcher_status=2 AND b.researcher_id != 0 `
  272. if startDate != "" {
  273. sql1 += ` AND b.start_date >= ? `
  274. pars = append(pars, startDate)
  275. }
  276. if endDate != "" {
  277. sql1 += ` AND b.end_date <= ? `
  278. pars = append(pars, endDate)
  279. }
  280. if lenCompanyStatusList > 0 {
  281. sql1 += ` AND d.overseas_status in (` + utils.GetOrmInReplace(lenCompanyStatusList) + ") "
  282. pars = append(pars, companyStatusList)
  283. }
  284. //sql1 += ` GROUP BY admin_id `
  285. sql2 := fmt.Sprintf(`SELECT aa.rs_calendar_id,bb.researcher_id as admin_id,bb.researcher_group_id as group_id,bb.researcher_name AS admin_name FROM rs_calendar AS aa
  286. INNER JOIN rs_report_record AS bb ON aa.rs_calendar_id=bb.rs_calendar_id
  287. INNER JOIN %s.english_company AS cc ON aa.company_id=cc.company_id
  288. where aa.english_company= 1 AND aa.source = 0 AND cc.is_deleted=0 AND bb.rs_calendar_researcher_status=2 AND bb.researcher_id != 0 `, databaseName)
  289. if startDate != "" {
  290. sql2 += ` AND bb.start_date >= ? `
  291. pars = append(pars, startDate)
  292. }
  293. if endDate != "" {
  294. sql2 += ` AND bb.end_date <= ? `
  295. pars = append(pars, endDate)
  296. }
  297. if lenCompanyStatusList > 0 {
  298. sql2 += ` AND cc.overseas_status in (` + utils.GetOrmInReplace(lenCompanyStatusList) + ") "
  299. pars = append(pars, companyStatusList)
  300. }
  301. //sql2 += ` GROUP BY admin_id `
  302. // 列表数据
  303. sql := `SELECT m.*,count(1) as num FROM (` + sql1 + ` UNION ALL ` + sql2
  304. sql += ` ) AS m WHERE 1=1 `
  305. sql += ` Group by admin_id `
  306. _, err = o.Raw(sql, pars).QueryRows(&list)
  307. return
  308. }
  309. func GetOverseasGroupSellerRecordList(startDate, endDate, companyStatus string) (list []*RsReportRecordGroup, err error) {
  310. var databaseName string
  311. if utils.RunMode == "debug" {
  312. databaseName = "test_v2_hongze_rddp"
  313. } else {
  314. databaseName = "hongze_rddp"
  315. }
  316. companyStatusList := make([]string, 0)
  317. if companyStatus != "" {
  318. companyStatusList = strings.Split(companyStatus, ",")
  319. }
  320. lenCompanyStatusList := len(companyStatusList)
  321. o := orm.NewOrm()
  322. pars := make([]interface{}, 0)
  323. sql1 := `SELECT COUNT(DISTINCT a.rs_calendar_id) AS num,b.seller_id as admin_id,b.seller_group_id as group_id,b.seller_name AS admin_name FROM rs_calendar AS a
  324. INNER JOIN rs_report_record AS b ON a.rs_calendar_id=b.rs_calendar_id
  325. JOIN company_product AS c ON a.company_id=c.company_id AND c.product_id=1
  326. JOIN company AS d ON c.company_id=d.company_id
  327. INNER JOIN overseas_custom_seller AS f ON c.seller_id=f.seller_id
  328. where c.is_overseas = 0 and a.english_company= 0 AND a.source = 0 AND b.rs_calendar_researcher_status=2 AND b.researcher_id != 0`
  329. if startDate != "" {
  330. sql1 += ` AND b.start_date >= ? `
  331. pars = append(pars, startDate)
  332. }
  333. if endDate != "" {
  334. sql1 += ` AND b.end_date <= ? `
  335. pars = append(pars, endDate)
  336. }
  337. if lenCompanyStatusList > 0 {
  338. sql1 += ` AND d.overseas_status in (` + utils.GetOrmInReplace(lenCompanyStatusList) + ") "
  339. pars = append(pars, companyStatusList)
  340. }
  341. sql1 += ` GROUP BY admin_id `
  342. sql2 := fmt.Sprintf(`SELECT COUNT(DISTINCT aa.rs_calendar_id) AS num,bb.seller_id as admin_id,bb.seller_group_id as group_id,bb.seller_name AS admin_name FROM rs_calendar AS aa
  343. INNER JOIN rs_report_record AS bb ON aa.rs_calendar_id=bb.rs_calendar_id
  344. INNER JOIN %s.english_company AS cc ON aa.company_id=cc.company_id
  345. where aa.english_company= 1 AND aa.source = 0 AND cc.is_deleted=0 AND bb.rs_calendar_researcher_status=2 AND bb.researcher_id != 0 `, databaseName)
  346. if startDate != "" {
  347. sql2 += ` AND bb.start_date >= ? `
  348. pars = append(pars, startDate)
  349. }
  350. if endDate != "" {
  351. sql2 += ` AND bb.end_date <= ? `
  352. pars = append(pars, endDate)
  353. }
  354. if lenCompanyStatusList > 0 {
  355. sql2 += ` AND cc.overseas_status in (` + utils.GetOrmInReplace(lenCompanyStatusList) + ") "
  356. pars = append(pars, companyStatusList)
  357. }
  358. sql2 += ` GROUP BY admin_id `
  359. // 列表数据
  360. sql := `SELECT m.*,SUM(m.num) AS num FROM (` + sql1 + ` UNION ALL ` + sql2
  361. sql += ` ) AS m WHERE 1=1 `
  362. sql += ` Group by admin_id `
  363. _, err = o.Raw(sql, pars).QueryRows(&list)
  364. return
  365. }
  366. // 根据ID获取详情
  367. func GetRsReportRecordDetailByRsReportRecordId(rsReportRecordId int) (item *RsReportRecord, err error) {
  368. o := orm.NewOrm()
  369. sql := ` SELECT * FROM rs_report_record WHERE rs_report_record_id=? `
  370. err = o.Raw(sql, rsReportRecordId).QueryRow(&item)
  371. return
  372. }
  373. // UpdateRsReportRecordPermissionMulti 批量修改
  374. func UpdateRsReportRecordPermissionMulti(items []*RsReportRecord) (err error) {
  375. o, err := orm.NewOrm().Begin()
  376. if err != nil {
  377. return
  378. }
  379. defer func() {
  380. if err == nil {
  381. o.Commit()
  382. } else {
  383. o.Rollback()
  384. }
  385. }()
  386. //批量修改
  387. p, err := o.Raw("UPDATE rs_report_record SET permission_name = ? WHERE rs_report_record_id = ?").Prepare()
  388. if err != nil {
  389. return
  390. }
  391. defer func() {
  392. _ = p.Close() // 别忘记关闭 statement
  393. }()
  394. for _, v := range items {
  395. _, err = p.Exec(v.PermissionName, v.RsReportRecordId)
  396. if err != nil {
  397. return
  398. }
  399. }
  400. return
  401. }