report_record.go 16 KB

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