report_record.go 16 KB

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