company_seller.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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[int]*crm.Admin)
  108. //for i := range adminList {
  109. // sellerMap[adminList[i].AdminId] = adminList[i]
  110. //}
  111. list,err := fms.GetIncomeHistoryList()
  112. if err != nil {
  113. return
  114. }
  115. sellerList, e := crmService.GetSellerDepartmentListWithEnable()
  116. if e != nil {
  117. resp.FailData("获取销售失败", "Err:"+e.Error(), nil)
  118. return
  119. }
  120. sellerMap := make(map[int]*crm.SellerAdminWithGroupTeam)
  121. for i := range sellerList {
  122. sellerMap[sellerList[i].SellerId] = sellerList[i]
  123. }
  124. for _, v := range list {
  125. fmt.Println("v:", v.IncomeId)
  126. if seller, ok := sellerMap[v.SellerId]; ok{
  127. fmt.Println("更新销售:",v.SellerName)
  128. v.SellerId = seller.SellerId
  129. v.GroupId = seller.GroupId
  130. v.GroupName = seller.GroupName
  131. v.DepartmentId = seller.DepartmentId
  132. if e = v.Update([]string{"GroupId","GroupName","DepartmentId"}); e != nil {
  133. fmt.Println("更新销售失败, Err: " + e.Error())
  134. return
  135. }
  136. }
  137. }
  138. return
  139. }
  140. // 修复
  141. func FixContractInvoiceSellerInfoV2() (err error) {
  142. sellerList, e := crmService.GetSellerDepartmentListWithEnable()
  143. if e != nil {
  144. fmt.Println("获取销售列表失败, Err: " + e.Error())
  145. return
  146. }
  147. sellerMap := make(map[int]*crm.SellerAdminWithGroupTeam)
  148. for i := range sellerList {
  149. sellerMap[sellerList[i].SellerId] = sellerList[i]
  150. }
  151. list,err := fms.GetInvoiceListByFix()
  152. if err != nil {
  153. return
  154. }
  155. queryRegisterIds := make([]int, 0)
  156. for i := range list {
  157. queryRegisterIds = append(queryRegisterIds, list[i].ContractRegisterId)
  158. }
  159. cond := `1 = 1`
  160. pars := make([]interface{}, 0)
  161. summaryCond := cond
  162. summaryCond += ` AND a.register_id IN ?`
  163. summaryPars := pars
  164. summaryPars = append(summaryPars, queryRegisterIds)
  165. summaryData, e := fms.GetInvoicePaymentCensusSummaryData(summaryCond, summaryPars)
  166. if e != nil {
  167. fmt.Println("err1:",e)
  168. return
  169. }
  170. contractRegisters, err := fms.GetContractInfoByRegisterIds(queryRegisterIds)
  171. if err != nil {
  172. fmt.Println("err3:",e)
  173. return
  174. }
  175. registerMap := make(map[int]fms.ContractRegister, 0)
  176. for _, v := range contractRegisters {
  177. registerMap[v.ContractRegisterId] = v
  178. }
  179. summaryMap := make(map[int]*fms.InvoicePaymentSummaryItem, 0)
  180. for _, v := range summaryData {
  181. summaryMap[v.InvoiceId] = v
  182. summaryMap[v.PaymentId] = v
  183. }
  184. for _, v := range list {
  185. paymentItem := fms.ContractInvoice{}
  186. paymentItem.ContractInvoiceId = v.ContractInvoiceId
  187. fmt.Println("paymentItem.ContractInvoiceId:", paymentItem.ContractInvoiceId)
  188. var seller *crm.SellerAdminWithGroupTeam
  189. //if v.InvoiceType == 2 || v.InvoiceType == 4 {
  190. // seller = sellerMap[v.SellerId]
  191. //} else {
  192. // seller = sellerMap[v.ContractInvoiceId]
  193. //}
  194. if item, ok := summaryMap[v.ContractInvoiceId]; ok{
  195. seller = sellerMap[item.SellerId]
  196. }
  197. if seller == nil {
  198. register := registerMap[v.ContractRegisterId]
  199. seller = sellerMap[register.SellerId]
  200. if seller == nil {
  201. seller = sellerMap[register.RaiSellerId]
  202. }
  203. }
  204. if seller != nil {
  205. fmt.Println("SellerName:", seller.SellerName)
  206. paymentItem.SellerId = seller.SellerId
  207. paymentItem.SellerName = seller.SellerName
  208. paymentItem.SellerGroupId = seller.GroupId
  209. paymentItem.SellerGroupName = seller.GroupName
  210. paymentItem.SellerTeamId = seller.TeamId
  211. paymentItem.SellerTeamName = seller.TeamName
  212. e = paymentItem.Update([]string{"SellerId", "SellerName", "SellerGroupId", "SellerGroupName", "SellerTeamId", "SellerTeamName"})
  213. if e != nil {
  214. fmt.Println("err2:",e)
  215. return
  216. }
  217. }
  218. }
  219. fmt.Println(len(summaryData))
  220. return
  221. }