company_todo.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. package statistic_report
  2. import (
  3. "hongze/hz_crm_api/models/company"
  4. "hongze/hz_crm_api/models/company_todo"
  5. "hongze/hz_crm_api/utils"
  6. "time"
  7. )
  8. // CompanyTodoReportDataMap 待办的数据结构体(客户状态)
  9. type CompanyTodoReportDataMap struct {
  10. TryOut CompanyTodoDataMap `description:"试用"`
  11. Formal CompanyTodoDataMap `description:"正式"`
  12. Stat CompanyTodoDataMap `description:"统计数据"`
  13. StartDate string `description:"开始日期"`
  14. EndDate string `description:"结束日期"`
  15. }
  16. // CompanyTodoDataMap 待办的数据结构体(任务状态)
  17. type CompanyTodoDataMap struct {
  18. AddMap map[int]int `description:"新增客户数"`
  19. AddIdMap map[int]string `description:"新增客户ids"`
  20. ApproveMap map[int]int `description:"完成客户数"`
  21. ApproveIdMap map[int]string `description:"完成客户ids"`
  22. }
  23. // GetCompanyTodoWeekData 销售待办周度数据
  24. func GetCompanyTodoWeekData(productId int, nowWeekMonday time.Time, sellerIds string) (adminDataList []CompanyTodoReportDataMap, err error) {
  25. //nowWeekMonday := utils.GetNowWeekMonday() //本周周一
  26. //fmt.Println(nowWeekMonday)
  27. nowWeekSunday := nowWeekMonday.AddDate(0, 0, 7).Add(-1 * time.Second) //本周周日
  28. //fmt.Println(nowWeekSunday)
  29. lastWeekMonday := nowWeekMonday.AddDate(0, 0, -7) //上周周一
  30. //fmt.Println(lastWeekMonday)
  31. lastWeekSunday := nowWeekSunday.AddDate(0, 0, -7) //上周周日
  32. //fmt.Println(lastWeekSunday)
  33. ch1 := make(chan CompanyTodoReportDataMap, 0)
  34. ch2 := make(chan CompanyTodoReportDataMap, 0)
  35. go getSectionCompanyTodoData(lastWeekMonday, lastWeekSunday, productId, ch1, sellerIds)
  36. go getSectionCompanyTodoData(nowWeekMonday, nowWeekSunday, productId, ch2, sellerIds)
  37. var nowWeekCompanyTodoReportDataMap, lastWeekCompanyTodoReportDataMap CompanyTodoReportDataMap
  38. lastWeekCompanyTodoReportDataMap = <-ch1
  39. close(ch1)
  40. nowWeekCompanyTodoReportDataMap = <-ch2
  41. close(ch2)
  42. adminDataList = make([]CompanyTodoReportDataMap, 0)
  43. adminDataList = append(adminDataList, nowWeekCompanyTodoReportDataMap, lastWeekCompanyTodoReportDataMap)
  44. return
  45. }
  46. // GetCompanyTodoWeekDataByWeekNum 销售待办周度数据(周度数据数量)
  47. func GetCompanyTodoWeekDataByWeekNum(productId int, nowWeekMonday time.Time, sellerIds string, weekNum int) (adminDataList []CompanyTodoReportDataMap, err error) {
  48. //nowWeekMonday := utils.GetNowWeekMonday() //本周周一
  49. //fmt.Println(nowWeekMonday)
  50. nowWeekSunday := nowWeekMonday.AddDate(0, 0, 7).Add(-1 * time.Second) //本周周日
  51. //fmt.Println(nowWeekSunday)
  52. //lastWeekMonday := nowWeekMonday.AddDate(0, 0, -7) //上周周一
  53. //lastWeekSunday := nowWeekSunday.AddDate(0, 0, -7) //上周周日
  54. chanList := make([]chan CompanyTodoReportDataMap, 0)
  55. for i := 0; i < weekNum; i++ {
  56. tmpCh := make(chan CompanyTodoReportDataMap, 0)
  57. chanList = append(chanList, tmpCh)
  58. lastWeekMonday := nowWeekMonday.AddDate(0, 0, -7*i)
  59. lastWeekSunday := nowWeekSunday.AddDate(0, 0, -7*i)
  60. go getSectionCompanyTodoData(lastWeekMonday, lastWeekSunday, productId, tmpCh, sellerIds)
  61. }
  62. adminDataList = make([]CompanyTodoReportDataMap, 0)
  63. for _, vChan := range chanList {
  64. var weekCompanyTodoReportDataMap CompanyTodoReportDataMap
  65. weekCompanyTodoReportDataMap = <-vChan
  66. close(vChan)
  67. adminDataList = append(adminDataList, weekCompanyTodoReportDataMap)
  68. }
  69. return
  70. }
  71. // GetCompanyTodoWeekDataByWeekNumV2 销售待办周度数据(周度数据数量,创建/完成,不区分客户状态)
  72. func GetCompanyTodoWeekDataByWeekNumV2(productId int, nowWeekMonday time.Time, sellerIds string, weekNum int) (adminDataList []CompanyTodoReportDataMap, err error) {
  73. //nowWeekMonday := utils.GetNowWeekMonday() //本周周一
  74. //fmt.Println(nowWeekMonday)
  75. nowWeekSunday := nowWeekMonday.AddDate(0, 0, 7).Add(-1 * time.Second) //本周周日
  76. //fmt.Println(nowWeekSunday)
  77. //lastWeekMonday := nowWeekMonday.AddDate(0, 0, -7) //上周周一
  78. //lastWeekSunday := nowWeekSunday.AddDate(0, 0, -7) //上周周日
  79. chanList := make([]chan CompanyTodoReportDataMap, 0)
  80. for i := 0; i < weekNum; i++ {
  81. tmpCh := make(chan CompanyTodoReportDataMap, 0)
  82. chanList = append(chanList, tmpCh)
  83. lastWeekMonday := nowWeekMonday.AddDate(0, 0, -7*i)
  84. lastWeekSunday := nowWeekSunday.AddDate(0, 0, -7*i)
  85. go getSectionCompanyTodoDataV2(lastWeekMonday, lastWeekSunday, productId, tmpCh, sellerIds)
  86. }
  87. adminDataList = make([]CompanyTodoReportDataMap, 0)
  88. for _, vChan := range chanList {
  89. var weekCompanyTodoReportDataMap CompanyTodoReportDataMap
  90. weekCompanyTodoReportDataMap = <-vChan
  91. close(vChan)
  92. adminDataList = append(adminDataList, weekCompanyTodoReportDataMap)
  93. }
  94. return
  95. }
  96. // GetCompanyTodoMonthDataByNum 销售待办月度数据(月度数据数量,创建/完成,不区分客户状态)
  97. func GetCompanyTodoMonthDataByNum(productId int, nowMonthFirstDay time.Time, sellerIds string, monthNum int) (adminDataList []CompanyTodoReportDataMap, err error) {
  98. chanList := make([]chan CompanyTodoReportDataMap, 0)
  99. for i := 0; i < monthNum; i++ {
  100. tmpCh := make(chan CompanyTodoReportDataMap, 0)
  101. chanList = append(chanList, tmpCh)
  102. lastMonthFirstDay := nowMonthFirstDay.AddDate(0, -1*i, 0) //上个月第一天
  103. lastMonthLastDay := nowMonthFirstDay.AddDate(0, -1*(i-1), 0).Add(-1 * time.Second) //上个月最后一天
  104. // 第一个开始日期
  105. //nowMonthFirstDay = lastMonthFirstDay
  106. go getSectionCompanyTodoDataV2(lastMonthFirstDay, lastMonthLastDay, productId, tmpCh, sellerIds)
  107. }
  108. adminDataList = make([]CompanyTodoReportDataMap, 0)
  109. for _, vChan := range chanList {
  110. var weekCompanyTodoReportDataMap CompanyTodoReportDataMap
  111. weekCompanyTodoReportDataMap = <-vChan
  112. close(vChan)
  113. adminDataList = append(adminDataList, weekCompanyTodoReportDataMap)
  114. }
  115. return
  116. }
  117. // GetCompanyTodoTimeIntervalDataByNum 销售待办月度数据(月度数据数量,创建/完成,不区分客户状态)
  118. func GetCompanyTodoTimeIntervalDataByNum(productId int, startDate, endDate, sellerIds string) (adminDataList []CompanyTodoReportDataMap, err error) {
  119. startDateTimer, _ := time.ParseInLocation(utils.FormatDate, startDate, time.Local)
  120. endDateTimer, _ := time.ParseInLocation(utils.FormatDate, endDate, time.Local)
  121. endDateTimer = endDateTimer.AddDate(0, 0, 1).Add(-1 * time.Second) //本月最后一天
  122. ch1 := make(chan CompanyTodoReportDataMap, 0)
  123. go getSectionCompanyTodoDataV2(startDateTimer, endDateTimer, productId, ch1, sellerIds)
  124. adminDataList = make([]CompanyTodoReportDataMap, 0)
  125. var weekCompanyTodoReportDataMap CompanyTodoReportDataMap
  126. weekCompanyTodoReportDataMap = <-ch1
  127. close(ch1)
  128. adminDataList = append(adminDataList, weekCompanyTodoReportDataMap)
  129. return
  130. }
  131. // GetDoingTodoExpiredNum 统计截止日期小于等于当前时间的未完成的任务数(已逾期)
  132. func GetDoingTodoExpiredNum(productId int, sellerIds string) (doingMap map[int]int, doingIdsMap map[int]string, err error) {
  133. var condition string
  134. var pars []interface{}
  135. todayDate := time.Now().Format(utils.FormatDate)
  136. condition = ` and a.product_id=? and a.status = "进行中" and (a.end_time <= ? or a.end_time is null or a.end_time="") and a.seller_id in` + sellerIds
  137. pars = append(pars, productId, todayDate)
  138. doingMap = make(map[int]int)
  139. doingIdsMap = make(map[int]string)
  140. data, tmpErr := company_todo.GetDoingGroupCompanyReportRecordGroupList(condition, pars)
  141. if tmpErr != nil {
  142. err = tmpErr
  143. return
  144. }
  145. for _, v := range data {
  146. doingMap[v.AdminId] = v.Num
  147. doingIdsMap[v.AdminId] = v.CompanyIds
  148. }
  149. return
  150. }
  151. // GetTryStatusCompanyBySellerIds 获取当前销售的客户ID
  152. func GetTryStatusCompanyBySellerIds(productId int, sellerIds string) (adminIdsMap map[int]string, err error) {
  153. adminIdsMap = make(map[int]string)
  154. if sellerIds == "" {
  155. return
  156. }
  157. list, err := company.GetTryCompanyIdsBySellerIds(productId, sellerIds)
  158. if err != nil {
  159. return
  160. }
  161. for _, v := range list {
  162. adminIdsMap[v.SellerId] = v.CompanyIds
  163. }
  164. return
  165. }
  166. // getSectionCompanyTodoData 获取周期数据
  167. func getSectionCompanyTodoData(startDate, endDate time.Time, productId int, ch chan CompanyTodoReportDataMap, sellerIds string) (dataMap CompanyTodoReportDataMap, err error) {
  168. defer func() {
  169. ch <- dataMap
  170. }()
  171. //TryOut: CompanyTodoDataMap{},
  172. // Formal: CompanyTodoDataMap{},
  173. //试用
  174. tryOutMap := CompanyTodoDataMap{
  175. AddMap: make(map[int]int), //新增任务
  176. AddIdMap: make(map[int]string), //新增任务客户id
  177. ApproveMap: make(map[int]int), //完成任务
  178. ApproveIdMap: make(map[int]string), //完成任务客户id
  179. }
  180. //正式
  181. formalMap := CompanyTodoDataMap{
  182. AddMap: make(map[int]int), //新增任务
  183. AddIdMap: make(map[int]string), //新增任务客户id
  184. ApproveMap: make(map[int]int), //完成任务
  185. ApproveIdMap: make(map[int]string), //完成任务客户id
  186. }
  187. //正式客户
  188. {
  189. addMap := make(map[int]int) //新增任务
  190. addIdMap := make(map[int]string) //新增任务客户id
  191. approveMap := make(map[int]int) //完成任务
  192. approveIdMap := make(map[int]string) //完成任务客户id
  193. //新增任务
  194. {
  195. var condition string
  196. var pars []interface{}
  197. condition = ` and a.product_id=? and a.created_company_status="正式" and a.status in ("进行中","已完成") and a.seller_id in ` + sellerIds + ` AND b.seller_id in ` + sellerIds + ` and a.create_time >= ? and a.create_time <= ? `
  198. pars = append(pars, productId, startDate, endDate)
  199. data, tmpErr := company_todo.GetDoingGroupCompanyReportRecordGroupList(condition, pars)
  200. if tmpErr != nil {
  201. err = tmpErr
  202. return
  203. }
  204. for _, v := range data {
  205. addMap[v.AdminId] = v.Num
  206. addIdMap[v.AdminId] = v.CompanyIds
  207. }
  208. }
  209. //完成任务
  210. {
  211. var condition string
  212. var pars []interface{}
  213. condition = ` and a.product_id=? and a.approved_company_status="正式" and a.status = "已完成" and a.approved_seller_id in ` + sellerIds + ` AND b.seller_id in ` + sellerIds + ` and a.approve_time >= ? and a.approve_time <= ? `
  214. pars = append(pars, productId, startDate, endDate)
  215. data, tmpErr := company_todo.GetFinishedGroupCompanyReportRecordGroupList(condition, pars)
  216. if tmpErr != nil {
  217. err = tmpErr
  218. return
  219. }
  220. for _, v := range data {
  221. approveMap[v.AdminId] = v.Num
  222. approveIdMap[v.AdminId] = v.CompanyIds
  223. }
  224. }
  225. formalMap = CompanyTodoDataMap{
  226. AddMap: addMap, //新增任务
  227. AddIdMap: addIdMap, //新增任务客户id
  228. ApproveMap: approveMap, //完成任务
  229. ApproveIdMap: approveIdMap, //完成任务客户id
  230. }
  231. }
  232. //试用客户
  233. {
  234. addMap := make(map[int]int) //新增任务
  235. addIdMap := make(map[int]string) //新增任务客户id
  236. approveMap := make(map[int]int) //完成任务
  237. approveIdMap := make(map[int]string) //完成任务客户id
  238. //新增任务
  239. {
  240. var condition string
  241. var pars []interface{}
  242. condition = ` and a.product_id=? and a.created_company_status="试用" and a.status in ("进行中","已完成") and a.seller_id in ` + sellerIds + ` AND b.seller_id in ` + sellerIds + ` and a.create_time >= ? and a.create_time <= ? `
  243. pars = append(pars, productId, startDate, endDate)
  244. data, tmpErr := company_todo.GetDoingGroupCompanyReportRecordGroupList(condition, pars)
  245. if tmpErr != nil {
  246. err = tmpErr
  247. return
  248. }
  249. for _, v := range data {
  250. addMap[v.AdminId] = v.Num
  251. addIdMap[v.AdminId] = v.CompanyIds
  252. }
  253. }
  254. //完成任务
  255. {
  256. var condition string
  257. var pars []interface{}
  258. condition = ` and a.product_id=? and a.approved_company_status="试用" and a.approved_seller_id in ` + sellerIds + ` AND b.seller_id in ` + sellerIds + ` and a.status = "已完成" and a.approve_time >= ? and a.approve_time <= ? `
  259. pars = append(pars, productId, startDate, endDate)
  260. data, tmpErr := company_todo.GetFinishedGroupCompanyReportRecordGroupList(condition, pars)
  261. if tmpErr != nil {
  262. err = tmpErr
  263. return
  264. }
  265. for _, v := range data {
  266. approveMap[v.AdminId] = v.Num
  267. approveIdMap[v.AdminId] = v.CompanyIds
  268. }
  269. }
  270. tryOutMap = CompanyTodoDataMap{
  271. AddMap: addMap, //新增任务
  272. AddIdMap: addIdMap, //新增任务客户id
  273. ApproveMap: approveMap, //完成任务
  274. ApproveIdMap: approveIdMap, //完成任务客户id
  275. }
  276. }
  277. dataMap = CompanyTodoReportDataMap{
  278. TryOut: tryOutMap,
  279. Formal: formalMap,
  280. StartDate: startDate.Format(utils.FormatDate),
  281. EndDate: endDate.Format(utils.FormatDate),
  282. }
  283. return
  284. }
  285. // getSectionCompanyTodoDataV2 获取周期数据(创建/完成,不区分客户状态)
  286. func getSectionCompanyTodoDataV2(startDate, endDate time.Time, productId int, ch chan CompanyTodoReportDataMap, sellerIds string) (dataMap CompanyTodoReportDataMap, err error) {
  287. defer func() {
  288. ch <- dataMap
  289. }()
  290. //客户统计
  291. addMap := make(map[int]int) //新增任务
  292. addIdMap := make(map[int]string) //新增任务客户id
  293. approveMap := make(map[int]int) //完成任务
  294. approveIdMap := make(map[int]string) //完成任务客户id
  295. //新增任务
  296. {
  297. var condition string
  298. var pars []interface{}
  299. condition = ` and a.product_id=? and a.status in ("进行中","已完成") and a.seller_id in ` + sellerIds + ` AND b.status in ("试用","正式","冻结") AND b.seller_id in ` + sellerIds + ` and a.create_time >= ? and a.create_time <= ? `
  300. pars = append(pars, productId, startDate, endDate)
  301. data, tmpErr := company_todo.GetDoingGroupCompanyReportRecordGroupList(condition, pars)
  302. if tmpErr != nil {
  303. err = tmpErr
  304. return
  305. }
  306. for _, v := range data {
  307. addMap[v.AdminId] = v.Num
  308. addIdMap[v.AdminId] = v.CompanyIds
  309. }
  310. }
  311. //完成任务
  312. {
  313. var condition string
  314. var pars []interface{}
  315. condition = ` and a.product_id=? and a.status = "已完成" and a.approved_seller_id in ` + sellerIds + ` AND b.status in ("试用","正式","冻结") AND b.seller_id in ` + sellerIds + ` and a.approve_time >= ? and a.approve_time <= ? `
  316. pars = append(pars, productId, startDate, endDate)
  317. data, tmpErr := company_todo.GetFinishedGroupCompanyReportRecordGroupList(condition, pars)
  318. if tmpErr != nil {
  319. err = tmpErr
  320. return
  321. }
  322. for _, v := range data {
  323. approveMap[v.AdminId] = v.Num
  324. approveIdMap[v.AdminId] = v.CompanyIds
  325. }
  326. }
  327. statMap := CompanyTodoDataMap{
  328. AddMap: addMap, //新增任务
  329. AddIdMap: addIdMap, //新增任务客户id
  330. ApproveMap: approveMap, //完成任务
  331. ApproveIdMap: approveIdMap, //完成任务客户id
  332. }
  333. dataMap = CompanyTodoReportDataMap{
  334. Stat: statMap,
  335. StartDate: startDate.Format(utils.FormatDate),
  336. EndDate: endDate.Format(utils.FormatDate),
  337. }
  338. return
  339. }
  340. // GetFinishTodoNum 统计已完成的任务数
  341. func GetFinishTodoNum(productId int, sellerIds string) (doingMap map[int]int, doingIdsMap map[int]string, err error) {
  342. var condition string
  343. var pars []interface{}
  344. condition = ` and a.product_id=? and a.status = "已完成" and a.approved_seller_id in` + sellerIds + ` AND b.seller_id in ` + sellerIds
  345. pars = append(pars, productId)
  346. doingMap = make(map[int]int)
  347. doingIdsMap = make(map[int]string)
  348. data, tmpErr := company_todo.GetFinishedGroupCompanyReportRecordGroupList(condition, pars)
  349. if tmpErr != nil {
  350. err = tmpErr
  351. return
  352. }
  353. for _, v := range data {
  354. doingMap[v.AdminId] = v.Num
  355. doingIdsMap[v.AdminId] = v.CompanyIds
  356. }
  357. return
  358. }
  359. // GetDoingTodoNum 统计进行中的任务数
  360. func GetDoingTodoNum(productId int, sellerIds string) (doingMap map[int]int, doingIdsMap map[int]string, err error) {
  361. var condition string
  362. var pars []interface{}
  363. todayDate := time.Now().Format(utils.FormatDate)
  364. condition = ` and a.product_id=? and a.status = "进行中" and a.end_time > ? and a.seller_id in` + sellerIds
  365. pars = append(pars, productId, todayDate)
  366. doingMap = make(map[int]int)
  367. doingIdsMap = make(map[int]string)
  368. data, tmpErr := company_todo.GetDoingGroupCompanyReportRecordGroupList(condition, pars)
  369. if tmpErr != nil {
  370. err = tmpErr
  371. return
  372. }
  373. for _, v := range data {
  374. doingMap[v.AdminId] = v.Num
  375. doingIdsMap[v.AdminId] = v.CompanyIds
  376. }
  377. return
  378. }