company_seller.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. package crm
  2. import (
  3. "fmt"
  4. "github.com/gin-gonic/gin"
  5. "github.com/go-playground/validator/v10"
  6. "hongze/fms_api/controller/resp"
  7. "hongze/fms_api/global"
  8. "hongze/fms_api/models/crm"
  9. "hongze/fms_api/models/fms"
  10. crmService "hongze/fms_api/services/crm"
  11. )
  12. // CompanySellerController CRM销售
  13. type CompanySellerController struct{}
  14. // List
  15. // @Title 销售列表
  16. // @Description 销售列表
  17. // @Success 200 {object} crm.SellerAdminWithGroupList
  18. // @router /crm/company_seller/list [get]
  19. func (rg *CompanySellerController) List(c *gin.Context) {
  20. // 此处调整为只取销售部门的人员, 而不是原先的根据角色取
  21. list, e := crmService.GetSellerDepartmentList()
  22. if e != nil {
  23. resp.FailData("获取失败", "获取销售列表失败, Err:"+e.Error(), c)
  24. return
  25. }
  26. resp.OkData("获取成功", list, c)
  27. }
  28. // GroupList
  29. // @Title 销售组别列表
  30. // @Description 销售组别列表
  31. // @Success 200 {object} crm.SysGroup
  32. // @router /crm/company_seller/group_list [get]
  33. func (rg *CompanySellerController) GroupList(c *gin.Context) {
  34. var req crm.SysGroupListReq
  35. if e := c.BindQuery(&req); e != nil {
  36. err, ok := e.(validator.ValidationErrors)
  37. if !ok {
  38. resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  39. return
  40. }
  41. resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  42. return
  43. }
  44. var departmentId int
  45. if req.SellerType == 1 {
  46. departmentId = crm.SellerDepartmentId
  47. }else if req.SellerType == 2 {
  48. departmentId = crm.RaiSellerDepartmentId
  49. }else if req.SellerType == 0 {
  50. resp.Fail("请选择销售类型", c)
  51. return
  52. }else {
  53. resp.Fail("请选择正确的销售类型", c)
  54. return
  55. }
  56. groupOB := new(crm.SysGroup)
  57. cond := `department_id = ? AND parent_id = 0`
  58. pars := make([]interface{}, 0)
  59. pars = append(pars, departmentId)
  60. list, e := groupOB.List(cond, pars)
  61. if e != nil {
  62. resp.FailData("获取失败", "获取销售组别列表失败, Err:"+e.Error(), c)
  63. return
  64. }
  65. resp.OkData("获取成功", list, c)
  66. }
  67. // List
  68. // @Title 销售列表
  69. // @Description 销售列表
  70. // @Success 200 {object} crm.SellerAdminWithGroupList
  71. // @router /crm/company_seller/all_list [get]
  72. func (rg *CompanySellerController) AllList(c *gin.Context) {
  73. ficcList, raiLits, e := crmService.GetSellerDepartmentListWithGroupAndTeamByDepartment()
  74. if e != nil {
  75. resp.FailData("获取销售失败", "Err:"+e.Error(), c)
  76. return
  77. }
  78. respItem := crm.AllSellerResp{
  79. FiccSeller: ficcList,
  80. RaiSeller: raiLits,
  81. }
  82. resp.OkData("获取成功", respItem, c)
  83. }
  84. // List
  85. // @Title 销售列表
  86. // @Description 销售列表
  87. // @Success 200 {object} crm.SellerAdminWithGroupList
  88. // @router /crm/company_seller/team_list [get]
  89. func (rg *CompanySellerController) TeamList(c *gin.Context) {
  90. // 此处调整为只取销售部门的人员, 而不是原先的根据角色取
  91. list, e := crmService.GetSellerList()
  92. if e != nil {
  93. resp.FailData("获取销售失败", "Err:"+e.Error(), c)
  94. return
  95. }
  96. resp.OkData("获取成功", list, c)
  97. }
  98. // 修复历史数据表中导入的销售seller_id
  99. func FIXIncomeHistorySellerId() (err error) {
  100. adminCond := ``
  101. adminPars := make([]interface{}, 0)
  102. adminOB := new(crm.Admin)
  103. adminList, e := adminOB.List(adminCond, adminPars)
  104. if e != nil {
  105. return
  106. }
  107. sellerMap := make(map[string]*crm.Admin)
  108. for i := range adminList {
  109. sellerMap[adminList[i].RealName] = adminList[i]
  110. }
  111. list,err := fms.GetIncomeHistoryList()
  112. if err != nil {
  113. return
  114. }
  115. for _, v := range list {
  116. fmt.Println("v:", v.IncomeId)
  117. if seller, ok := sellerMap[v.SellerName]; ok{
  118. v.SellerId = seller.AdminId
  119. if e = v.Update([]string{"SellerId"}); e != nil {
  120. fmt.Println("更新销售失败, Err: " + e.Error())
  121. return
  122. }
  123. }
  124. }
  125. return
  126. }
  127. // 修复
  128. func FixContractInvoiceSellerInfoV2() (err error) {
  129. sellerList, e := crmService.GetSellerDepartmentListWithEnable()
  130. if e != nil {
  131. fmt.Println("获取销售列表失败, Err: " + e.Error())
  132. return
  133. }
  134. sellerMap := make(map[int]*crm.SellerAdminWithGroupTeam)
  135. for i := range sellerList {
  136. sellerMap[sellerList[i].SellerId] = sellerList[i]
  137. }
  138. list,err := fms.GetInvoiceListByFix()
  139. if err != nil {
  140. return
  141. }
  142. queryRegisterIds := make([]int, 0)
  143. for i := range list {
  144. queryRegisterIds = append(queryRegisterIds, list[i].ContractRegisterId)
  145. }
  146. cond := `1 = 1`
  147. pars := make([]interface{}, 0)
  148. summaryCond := cond
  149. summaryCond += ` AND a.register_id IN ?`
  150. summaryPars := pars
  151. summaryPars = append(summaryPars, queryRegisterIds)
  152. summaryData, e := fms.GetInvoicePaymentCensusSummaryData(summaryCond, summaryPars)
  153. if e != nil {
  154. fmt.Println("err1:",e)
  155. return
  156. }
  157. contractRegisters, err := fms.GetContractInfoByRegisterIds(queryRegisterIds)
  158. if err != nil {
  159. fmt.Println("err3:",e)
  160. return
  161. }
  162. registerMap := make(map[int]fms.ContractRegister, 0)
  163. for _, v := range contractRegisters {
  164. registerMap[v.ContractRegisterId] = v
  165. }
  166. summaryMap := make(map[int]*fms.InvoicePaymentSummaryItem, 0)
  167. for _, v := range summaryData {
  168. summaryMap[v.InvoiceId] = v
  169. summaryMap[v.PaymentId] = v
  170. }
  171. for _, v := range list {
  172. paymentItem := fms.ContractInvoice{}
  173. paymentItem.ContractInvoiceId = v.ContractInvoiceId
  174. fmt.Println("paymentItem.ContractInvoiceId:", paymentItem.ContractInvoiceId)
  175. var seller *crm.SellerAdminWithGroupTeam
  176. //if v.InvoiceType == 2 || v.InvoiceType == 4 {
  177. // seller = sellerMap[v.SellerId]
  178. //} else {
  179. // seller = sellerMap[v.ContractInvoiceId]
  180. //}
  181. if item, ok := summaryMap[v.ContractInvoiceId]; ok{
  182. seller = sellerMap[item.SellerId]
  183. }
  184. if seller == nil {
  185. register := registerMap[v.ContractRegisterId]
  186. seller = sellerMap[register.SellerId]
  187. if seller == nil {
  188. seller = sellerMap[register.RaiSellerId]
  189. }
  190. }
  191. if seller != nil {
  192. fmt.Println("SellerName:", seller.SellerName)
  193. paymentItem.SellerId = seller.SellerId
  194. paymentItem.SellerName = seller.SellerName
  195. paymentItem.SellerGroupId = seller.GroupId
  196. paymentItem.SellerGroupName = seller.GroupName
  197. paymentItem.SellerTeamId = seller.TeamId
  198. paymentItem.SellerTeamName = seller.TeamName
  199. e = paymentItem.Update([]string{"SellerId", "SellerName", "SellerGroupId", "SellerGroupName", "SellerTeamId", "SellerTeamName"})
  200. if e != nil {
  201. fmt.Println("err2:",e)
  202. return
  203. }
  204. }
  205. }
  206. fmt.Println(len(summaryData))
  207. return
  208. }