company_statistic.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. package services
  2. import (
  3. "context"
  4. "fmt"
  5. "hongze/hongze_task/models"
  6. "hongze/hongze_task/utils"
  7. "time"
  8. )
  9. // StackCompanyStatisticOld 存量客户数据统计(2021-10-26 14:12:27 过期失效)
  10. func StackCompanyStatisticOld(cont context.Context) (err error) {
  11. defer func() {
  12. if err != nil {
  13. go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "存量客户数据统计 ErrMsg:"+err.Error(), utils.EmailSendToUsers)
  14. }
  15. }()
  16. dayStr := time.Now().AddDate(0, 0, -1).Format(utils.FormatDate) //截止到昨天的数据
  17. //查询昨天的数据有没有生成,如果没有生成的话,那么重新生成
  18. count, err := models.GetStackCompanyCount(dayStr)
  19. if err != nil {
  20. fmt.Println("查询昨天的数据是否生成语句 执行异常:", err.Error())
  21. return
  22. }
  23. //昨天数据有生成,那么就不往下执行了
  24. if count > 0 {
  25. fmt.Println("昨天的数据已经生成,不允许重复生成")
  26. return
  27. }
  28. total, list, err := models.GetStackCompanyListV1()
  29. if err != nil {
  30. fmt.Println("查询新签客户数(存量客户)异常:", err.Error())
  31. return
  32. }
  33. fmt.Println("total:", total)
  34. //fmt.Println(list)
  35. for _, company := range list {
  36. item := models.StackCompanyStatistic{
  37. CompanyId: company.CompanyId,
  38. CompanyName: company.CompanyName,
  39. ProductId: company.ProductId,
  40. ProductName: company.ProductName,
  41. ContractNum: company.Count,
  42. SellerId: company.SellerId,
  43. SellerName: company.SellerName,
  44. GroupId: company.GroupId,
  45. DepartmentId: company.DepartmentId,
  46. Date: dayStr, //截止到昨天的数据
  47. StartDate: company.StartDate,
  48. EndDate: company.EndDate,
  49. RegionType: company.RegionType,
  50. CreateTime: time.Now(),
  51. }
  52. if company.ProductStatus == "正式" {
  53. //正式客户
  54. if company.Count == 1 {
  55. //新签客户数
  56. item.Type = "新签客户"
  57. } else {
  58. item.Type = "续约客户"
  59. }
  60. } else {
  61. //未续约客户
  62. item.Type = "未续约客户"
  63. continue
  64. }
  65. addErr := models.AddStackCompanyStatistic(&item)
  66. if addErr != nil {
  67. fmt.Println("存量客户数据统计,插入数据异常:", addErr)
  68. }
  69. }
  70. total, notRenewalCompanyList, err := models.GetNotRenewalCompanyTotalV1(time.Now().Format(utils.FormatDateTime))
  71. if err != nil {
  72. fmt.Println("查询未续约客户数(存量客户)异常:", err.Error())
  73. return
  74. }
  75. fmt.Println("total:", total)
  76. //fmt.Println(list)
  77. for _, company := range notRenewalCompanyList {
  78. endDateTime, err := time.Parse(utils.FormatDateTime, company.CreateTime)
  79. if err != nil {
  80. fmt.Println("查询未续约客户数,插入数据异常:", err)
  81. }
  82. item := models.StackCompanyStatistic{
  83. CompanyId: company.CompanyId,
  84. CompanyName: company.CompanyName,
  85. ProductId: company.ProductId,
  86. ProductName: company.ProductName,
  87. //ContractNum: company.Count,
  88. SellerId: company.SellerId,
  89. SellerName: company.SellerName,
  90. GroupId: company.GroupId,
  91. DepartmentId: company.DepartmentId,
  92. Date: dayStr, //截止到昨天的数据
  93. StartDate: company.StartDate,
  94. EndDate: endDateTime.Format(utils.FormatDate),
  95. RegionType: company.RegionType,
  96. CreateTime: time.Now(),
  97. Type: "未续约客户",
  98. }
  99. addErr := models.AddStackCompanyStatistic(&item)
  100. if addErr != nil {
  101. fmt.Println("存量客户数据统计,插入数据异常:", addErr)
  102. }
  103. }
  104. return
  105. }
  106. // StackCompanyStatistic 存量客户数据统计
  107. func StackCompanyStatistic(cont context.Context) (err error) {
  108. defer func() {
  109. if err != nil {
  110. go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "存量客户数据统计 ErrMsg:"+err.Error(), utils.EmailSendToUsers)
  111. }
  112. }()
  113. dayStr := time.Now().AddDate(0, 0, -1).Format(utils.FormatDate) //截止到昨天的数据
  114. //查询昨天的数据有没有生成,如果没有生成的话,那么重新生成
  115. count, err := models.GetStackCompanyCount(dayStr)
  116. if err != nil {
  117. fmt.Println("查询昨天的数据是否生成语句 执行异常:", err.Error())
  118. return
  119. }
  120. //昨天数据有生成,那么就不往下执行了
  121. if count > 0 {
  122. fmt.Println("昨天的数据已经生成,不允许重复生成")
  123. return
  124. }
  125. //新签客户
  126. {
  127. var condition1 string
  128. var pars1 []interface{}
  129. condition1 += ` AND a.start_date <= ? AND a.end_date >= ? `
  130. pars1 = append(pars1, dayStr, dayStr)
  131. condition1 += ` AND a.contract_type = ? `
  132. pars1 = append(pars1, "新签合同")
  133. list, countErr := models.GetTodayStackCompanyList(condition1, pars1)
  134. if countErr != nil {
  135. err = countErr
  136. return
  137. }
  138. for _, companyInfo := range list {
  139. item := models.StackCompanyStatistic{
  140. CompanyId: companyInfo.CompanyId,
  141. CompanyName: companyInfo.CompanyName,
  142. ProductId: companyInfo.ProductId,
  143. ProductName: companyInfo.ProductName,
  144. ContractId: companyInfo.CompanyContractId,
  145. SellerId: companyInfo.SellerId,
  146. SellerName: companyInfo.SellerName,
  147. GroupId: companyInfo.GroupId,
  148. DepartmentId: companyInfo.DepartmentId,
  149. Date: dayStr, //截止到昨天的数据
  150. StartDate: companyInfo.StartDate,
  151. EndDate: companyInfo.EndDate,
  152. RegionType: companyInfo.RegionType,
  153. CreateTime: time.Now(),
  154. Type: "新签客户",
  155. }
  156. addErr := models.AddStackCompanyStatistic(&item)
  157. if addErr != nil {
  158. fmt.Println("存量新签客户数据统计,插入数据异常:", addErr)
  159. }
  160. }
  161. }
  162. //续约客户
  163. {
  164. var condition1 string
  165. var pars1 []interface{}
  166. condition1 += ` AND a.start_date <= ? AND a.end_date >= ? `
  167. pars1 = append(pars1, dayStr, dayStr)
  168. condition1 += ` AND a.contract_type = ? `
  169. pars1 = append(pars1, "续约合同")
  170. //额外条件(续约合同的起始日期包含在所选时间段内且不包含在新签合同存续期内的客户)
  171. pars1 = append(pars1, dayStr)
  172. list, countErr := models.GetTodayStackCompanyListV2(condition1, pars1)
  173. if countErr != nil {
  174. err = countErr
  175. return
  176. }
  177. for _, companyInfo := range list {
  178. item := models.StackCompanyStatistic{
  179. CompanyId: companyInfo.CompanyId,
  180. CompanyName: companyInfo.CompanyName,
  181. ProductId: companyInfo.ProductId,
  182. ProductName: companyInfo.ProductName,
  183. ContractId: companyInfo.CompanyContractId,
  184. SellerId: companyInfo.SellerId,
  185. SellerName: companyInfo.SellerName,
  186. GroupId: companyInfo.GroupId,
  187. DepartmentId: companyInfo.DepartmentId,
  188. Date: dayStr, //截止到昨天的数据
  189. StartDate: companyInfo.StartDate,
  190. EndDate: companyInfo.EndDate,
  191. RegionType: companyInfo.RegionType,
  192. CreateTime: time.Now(),
  193. Type: "续约客户",
  194. }
  195. addErr := models.AddStackCompanyStatistic(&item)
  196. if addErr != nil {
  197. fmt.Println("存量续约客户数据统计,插入数据异常:", addErr)
  198. }
  199. }
  200. }
  201. //未续约客户
  202. {
  203. var condition1 string
  204. var pars1 []interface{}
  205. condition1 += ` AND c.status not in ("永续","正式") AND a.create_time <= ? `
  206. pars1 = append(pars1, time.Now().Format(utils.FormatDateTime))
  207. condition1 += ` AND a.operation = 'try_out' `
  208. list, countErr := models.GetIncrementalCompanyListByOperationRecord(condition1, pars1)
  209. if countErr != nil {
  210. err = countErr
  211. return
  212. }
  213. for _, companyInfo := range list {
  214. item := models.StackCompanyStatistic{
  215. CompanyId: companyInfo.CompanyId,
  216. CompanyName: companyInfo.CompanyName,
  217. ProductId: companyInfo.ProductId,
  218. ProductName: companyInfo.ProductName,
  219. ContractId: companyInfo.CompanyOperationRecordId,
  220. SellerId: companyInfo.SellerId,
  221. SellerName: companyInfo.SellerName,
  222. GroupId: companyInfo.GroupId,
  223. DepartmentId: companyInfo.DepartmentId,
  224. Date: dayStr, //截止到昨天的数据
  225. StartDate: companyInfo.StartDate,
  226. EndDate: companyInfo.EndDate,
  227. RegionType: companyInfo.RegionType,
  228. CreateTime: time.Now(),
  229. Type: "未续约客户",
  230. }
  231. addErr := models.AddStackCompanyStatistic(&item)
  232. if addErr != nil {
  233. fmt.Println("存量未续约客户数据统计,插入数据异常:", addErr)
  234. }
  235. }
  236. }
  237. return
  238. }