seller.go 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316
  1. package census
  2. import (
  3. "bytes"
  4. "fmt"
  5. "github.com/gin-gonic/gin"
  6. "github.com/go-playground/validator/v10"
  7. "github.com/shopspring/decimal"
  8. "github.com/tealeg/xlsx"
  9. "hongze/fms_api/controller/resp"
  10. "hongze/fms_api/global"
  11. "hongze/fms_api/models/base"
  12. "hongze/fms_api/models/crm"
  13. "hongze/fms_api/models/fms"
  14. crmService "hongze/fms_api/services/crm"
  15. "hongze/fms_api/utils"
  16. "net/http"
  17. "sort"
  18. "strconv"
  19. "time"
  20. )
  21. // SellerController 销售统计
  22. type SellerController struct{}
  23. // GroupInvoiceList
  24. // @Title 销售组开票统计列表-弃用
  25. // @Description 销售组开票统计列表-弃用
  26. // @Param StartDate query string false "开始日期"
  27. // @Param EndDate query string false "结束日期"
  28. // @Param SortField query int false "排序字段: 1-开票金额; 2-组别占比"
  29. // @Param SortType query int false "排序方式: 1-正序; 2-倒序"
  30. // @Param IsExport query int false "是否导出: 0-否; 1-是"
  31. // @Success 200 {object} fms.CensusSellerGroupInvoiceItem
  32. // @router /census/seller/group_invoice_list [get]
  33. //func (ct *SellerController) GroupInvoiceList(c *gin.Context) {
  34. // var req fms.CensusSellerGroupInvoiceListReq
  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. // outCond := ` department_id = %d AND parent_id = 0 `
  57. // outCond = fmt.Sprintf(outCond, departmentId)
  58. // cond := ` (invoice_type = %d OR invoice_type = %d) AND a.is_deleted = 0 AND a.seller_id != 0 `
  59. // cond = fmt.Sprintf(cond, fms.ContractInvoiceTypeMake, fms.ContractInvoiceTypePreMake)
  60. //
  61. // //adminCond := ` (invoice_type = %d OR invoice_type = %d) AND is_deleted = 0 AND seller_group_id != 0 `
  62. // //adminCond = fmt.Sprintf(cond, fms.ContractInvoiceTypeMake, fms.ContractInvoiceTypePreMake)
  63. // pars := make([]interface{}, 0)
  64. // // 开票日期
  65. // if req.StartDate != "" && req.EndDate != "" {
  66. // st := fmt.Sprint(req.StartDate, " 00:00:00")
  67. // ed := fmt.Sprint(req.EndDate, " 23:59:59")
  68. // cond += ` AND (invoice_time BETWEEN '%s' AND '%s')`
  69. // cond = fmt.Sprintf(cond, st, ed)
  70. //
  71. // //adminCond += ` AND (invoice_time BETWEEN '%s' AND '%s')`
  72. // //adminCond = fmt.Sprintf(cond, st, ed)
  73. // }
  74. //
  75. // if req.CompanyType == 1 {
  76. // cond += ` AND b.contract_type = 1 `
  77. // //historyCond += ` AND new_company = 1 `
  78. // } else if req.CompanyType == 2 {
  79. // cond += ` AND b.contract_type IN (2,3,4) `
  80. // //historyCond += ` AND new_company = 0 `
  81. // }
  82. //
  83. // page := new(base.Page)
  84. // page.SetPageSize(req.PageSize)
  85. // page.SetCurrent(req.Current)
  86. // // 排序, 默认开票金额倒序
  87. // sortFieldMap := map[int]string{0: "invoice_amount", 1: "invoice_amount", 2: "group_rate"}
  88. // sortTypeMap := map[int]bool{0: false, 1: true, 2: false}
  89. // page.AddOrderItem(base.OrderItem{Column: sortFieldMap[req.SortField], Asc: sortTypeMap[req.SortType]})
  90. // if req.IsExport == 1 {
  91. // page.SetPageSize(10000)
  92. // page.SetCurrent(1)
  93. // }
  94. //
  95. // // 查询开票金额总和(减少子查询)
  96. // invOB := new(fms.ContractInvoice)
  97. // sumCond := cond
  98. // sumPars := make([]interface{}, 0)
  99. // invSum, e := invOB.Sum("amount", sumCond, sumPars)
  100. // if e != nil {
  101. // resp.FailMsg("获取失败", "获取开票金额总和失败, Err: "+e.Error(), c)
  102. // return
  103. // }
  104. //
  105. // // 查询列表
  106. // groupOB := new(crm.SysGroup)
  107. // //totalCond := outCond
  108. // //totalPars := make([]interface{}, 0)
  109. // //total, e := groupOB.Count(totalCond, totalPars)
  110. // //if e != nil {
  111. // // resp.FailMsg("获取失败", "获取销售组开票统计列表总数失败, Err: "+e.Error(), c)
  112. // // return
  113. // //}
  114. // //list, e := fms.GetCensusSellerGroupInvoicePageList(page, cond, outCond, pars, invSum)
  115. // //if e != nil {
  116. // // resp.FailMsg("获取失败", "获取销售组开票统计列表失败, Err: "+e.Error(), c)
  117. // // return
  118. // //}
  119. // groupCond := ` department_id = %d AND parent_id = 0 `
  120. // groupCond = fmt.Sprintf(groupCond, departmentId)
  121. // groupPars := make([]interface{}, 0)
  122. // groupList, e := groupOB.List(groupCond, groupPars)
  123. // if e != nil {
  124. // resp.FailMsg("获取失败", "获取组别列表失败, Err: "+e.Error(), c)
  125. // return
  126. // }
  127. // //total := len(groupList)
  128. // groupMap := make(map[int]*crm.SysGroup)
  129. // groupIdSlice := make([]string, 0)
  130. // for i := range groupList {
  131. // groupMap[groupList[i].GroupId] = groupList[i]
  132. // groupIdSlice = append(groupIdSlice, strconv.Itoa(groupList[i].GroupId))
  133. // }
  134. //
  135. // groupStr := strings.Join(groupIdSlice, ",")
  136. // total, list, e := fms.GetCensusSellerGroupInvoicePageListV2(page, groupStr, cond, pars, invSum)
  137. // if e != nil {
  138. // resp.FailMsg("获取失败", "获取销售组开票统计列表失败, Err: "+e.Error(), c)
  139. // return
  140. // }
  141. // for _, v := range list {
  142. // if group, ok := groupMap[v.GroupId]; ok {
  143. // v.GroupName = group.GroupName
  144. // }
  145. // }
  146. // // 处理百分比, 乘100并保留两位小数
  147. // mulNum := decimal.NewFromFloat(100)
  148. // for i := range list {
  149. // d := decimal.NewFromFloat(list[i].GroupRate)
  150. // d = d.Mul(mulNum).Round(2)
  151. // a, _ := d.Float64()
  152. // list[i].GroupRate = a
  153. // }
  154. //
  155. // // 是否导出
  156. // if req.IsExport == 1 {
  157. // ExportGroupInvoiceList(c, list, req)
  158. // return
  159. // }
  160. // page.SetTotal(int64(total))
  161. // baseData := new(base.BaseData)
  162. // baseData.SetPage(page)
  163. // baseData.SetList(list)
  164. // resp.OkData("获取成功", baseData, c)
  165. //}
  166. // ExportGroupInvoiceList 导出销售组开票统计列表
  167. func ExportGroupInvoiceList(c *gin.Context, list []*fms.CensusSellerGroupInvoiceItem, req fms.CensusSellerGroupInvoiceListReq) {
  168. // 生成Excel文件
  169. xlsxFile := xlsx.NewFile()
  170. style := xlsx.NewStyle()
  171. alignment := xlsx.Alignment{
  172. Horizontal: "center",
  173. Vertical: "center",
  174. WrapText: true,
  175. }
  176. style.Alignment = alignment
  177. style.ApplyAlignment = true
  178. sheetName := "销售组开票统计"
  179. sheet, err := xlsxFile.AddSheet(sheetName)
  180. if err != nil {
  181. resp.FailData("新增Sheet失败", "Err:"+err.Error(), c)
  182. return
  183. }
  184. // 存在筛选则前两行显示时间信息
  185. if req.StartDate != "" && req.EndDate != "" {
  186. timeData := fmt.Sprintf("时间:%s至%s", req.StartDate, req.EndDate)
  187. rowA := sheet.AddRow()
  188. cellAA := rowA.AddCell()
  189. cellAA.SetString("销售统计表")
  190. cellAA.SetStyle(style)
  191. rowB := sheet.AddRow()
  192. rowB.AddCell().SetString(timeData)
  193. // 第三行空出
  194. sheet.AddRow()
  195. }
  196. // 数据表头
  197. rowTitle := []string{"排名", "销售组别", "收入金额(元)", "组别占比"}
  198. titleRow := sheet.AddRow()
  199. for i := range rowTitle {
  200. v := titleRow.AddCell()
  201. v.SetString(rowTitle[i])
  202. v.SetStyle(style)
  203. }
  204. // 填充数据
  205. for k, v := range list {
  206. dataRow := sheet.AddRow()
  207. dataRow.AddCell().SetString(fmt.Sprint(k + 1)) // 排名
  208. dataRow.AddCell().SetString(v.GroupName) // 销售组别
  209. dataRow.AddCell().SetString(fmt.Sprint(v.InvoiceAmount)) // 开票金额
  210. dataRow.AddCell().SetString(fmt.Sprint(v.GroupRate, "%")) // 组别占比
  211. }
  212. // 输出文件
  213. var buffer bytes.Buffer
  214. _ = xlsxFile.Write(&buffer)
  215. content := bytes.NewReader(buffer.Bytes())
  216. randStr := time.Now().Format(utils.FormatDateTimeUnSpace)
  217. fileName := sheetName + randStr + ".xlsx"
  218. c.Writer.Header().Add("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, fileName))
  219. c.Writer.Header().Add("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
  220. http.ServeContent(c.Writer, c.Request, fileName, time.Now(), content)
  221. }
  222. // InvoiceList
  223. // @Title 销售开票统计列表-弃用
  224. // @Description 销售开票统计列表-弃用
  225. // @Param GroupId query int false "组别ID"
  226. // @Param StartDate query string false "开始日期"
  227. // @Param EndDate query string false "结束日期"
  228. // @Param SortField query int false "排序字段: 1-开票金额; 2-组别占比; 3-全员占比"
  229. // @Param SortType query int false "排序方式: 1-正序; 2-倒序"
  230. // @Param IsExport query int false "是否导出: 0-否; 1-是"
  231. // @Success 200 {object} fms.CensusSellerInvoiceListReq
  232. // @router /census/seller/invoice_list [get]
  233. //func (ct *SellerController) InvoiceList(c *gin.Context) {
  234. // var req fms.CensusSellerInvoiceListReq
  235. // if e := c.BindQuery(&req); e != nil {
  236. // err, ok := e.(validator.ValidationErrors)
  237. // if !ok {
  238. // resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  239. // return
  240. // }
  241. // resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  242. // return
  243. // }
  244. // var departmentId int
  245. // if req.SellerType == 1 {
  246. // departmentId = crm.SellerDepartmentId
  247. // } else if req.SellerType == 2 {
  248. // departmentId = crm.RaiSellerDepartmentId
  249. // } else if req.SellerType == 0 {
  250. // resp.Fail("请选择销售类型", c)
  251. // return
  252. // } else {
  253. // resp.Fail("请选择正确的销售类型", c)
  254. // return
  255. // }
  256. //
  257. // pars := make([]interface{}, 0)
  258. // adminPars := make([]interface{}, 0)
  259. // outCond := ` a.department_id = %d `
  260. // outCond = fmt.Sprintf(outCond, departmentId)
  261. // adminCond := ` department_id = %d `
  262. // adminCond = fmt.Sprintf(adminCond, departmentId)
  263. // totalCond := ` department_id = %d `
  264. // totalCond = fmt.Sprintf(totalCond, departmentId)
  265. // totalPars := make([]interface{}, 0)
  266. //
  267. // if !req.ShowResign {
  268. // adminCond += ` AND enabled = 1 `
  269. // }
  270. // if req.GroupId > 0 {
  271. // // 筛选组别时, 查询当前组别的下级组(因为admin表存的group_id, 有三级的存的是子ID, 只有二级的存的才是父ID =_=!)
  272. // groupCond := `parent_id = ?`
  273. // groupPars := make([]interface{}, 0)
  274. // groupPars = append(groupPars, req.GroupId)
  275. // groupOB := new(crm.SysGroup)
  276. // groupList, e := groupOB.List(groupCond, groupPars)
  277. // if e != nil {
  278. // resp.FailMsg("获取失败", "获取组别下级组列表失败, Err: "+e.Error(), c)
  279. // return
  280. // }
  281. // groupIds := make([]int, 0)
  282. // groupIds = append(groupIds, req.GroupId)
  283. // for i := range groupList {
  284. // groupIds = append(groupIds, groupList[i].GroupId)
  285. // }
  286. // outCond += ` AND a.group_id IN (?) `
  287. // pars = append(pars, groupIds)
  288. // adminCond += ` AND group_id IN (?) `
  289. // adminPars = append(adminPars, groupIds)
  290. // totalCond += ` AND group_id IN (?) `
  291. // totalPars = append(totalPars, groupIds)
  292. // }
  293. //
  294. // sumCond := ` (invoice_type = ? OR invoice_type = ? ) AND a.is_deleted = 0 AND a.seller_id != 0 `
  295. // sumPars := make([]interface{}, 0)
  296. // sumPars = append(sumPars, fms.ContractInvoiceTypeMake, fms.ContractInvoiceTypePreMake)
  297. // cond := ` (c.invoice_type = %d OR c.invoice_type = %d) AND c.is_deleted = 0 AND c.seller_id != 0 `
  298. // inCond := ` (invoice_type = %d OR invoice_type = %d) AND is_deleted = 0 AND seller_id != 0 `
  299. // cond = fmt.Sprintf(cond, fms.ContractInvoiceTypeMake, fms.ContractInvoiceTypePreMake)
  300. // inCond = fmt.Sprintf(inCond, fms.ContractInvoiceTypeMake, fms.ContractInvoiceTypePreMake)
  301. //
  302. // if req.CompanyType == 1 {
  303. // cond += ` AND b.contract_type = 1 `
  304. // //historyCond += ` AND new_company = 1 `
  305. // } else if req.CompanyType == 2 {
  306. // cond += ` AND b.contract_type IN (2,3,4) `
  307. // //historyCond += ` AND new_company = 0 `
  308. // }
  309. //
  310. // // 开票日期
  311. // if req.StartDate != "" && req.EndDate != "" {
  312. // st := fmt.Sprint(req.StartDate, " 00:00:00")
  313. // ed := fmt.Sprint(req.EndDate, " 23:59:59")
  314. // cond += ` AND (c.invoice_time BETWEEN '%s' AND '%s')`
  315. // inCond += ` AND (invoice_time BETWEEN '%s' AND '%s')`
  316. // cond = fmt.Sprintf(cond, st, ed)
  317. // inCond = fmt.Sprintf(inCond, st, ed)
  318. // sumCond += ` AND (invoice_time BETWEEN ? AND ?)`
  319. // sumPars = append(sumPars, st, ed)
  320. // }
  321. //
  322. // page := new(base.Page)
  323. // page.SetPageSize(req.PageSize)
  324. // page.SetCurrent(req.Current)
  325. // // 排序, 默认开票金额倒序
  326. // sortFieldMap := map[int]string{0: "invoice_amount", 1: "invoice_amount", 2: "group_rate", 3: "seller_rate"}
  327. // sortTypeMap := map[int]bool{0: false, 1: true, 2: false}
  328. // page.AddOrderItem(base.OrderItem{Column: sortFieldMap[req.SortField], Asc: sortTypeMap[req.SortType]})
  329. // if req.IsExport == 1 {
  330. // page.SetPageSize(10000)
  331. // page.SetCurrent(1)
  332. // }
  333. //
  334. // // 查询开票金额总和(减少子查询)
  335. // invOB := new(fms.ContractInvoice)
  336. // invSum, e := invOB.Sum("amount", sumCond, sumPars)
  337. // if e != nil {
  338. // resp.FailMsg("获取失败", "获取开票金额总和失败, Err: "+e.Error(), c)
  339. // return
  340. // }
  341. //
  342. // // 查询列表
  343. // adminOB := new(crm.Admin)
  344. // //total, e := adminOB.Count(totalCond, totalPars)
  345. // //if e != nil {
  346. // // resp.FailMsg("获取失败", "获取销售开票统计列表总数失败, Err: "+e.Error(), c)
  347. // // return
  348. // //}
  349. // //list, e := fms.GetCensusSellerInvoicePageList(page, cond, outCond, pars, invSum)
  350. // //if e != nil {
  351. // // resp.FailMsg("获取失败", "获取销售开票统计列表失败, Err: "+e.Error(), c)
  352. // // return
  353. // //}
  354. //
  355. // adminList, e := adminOB.List(adminCond, adminPars)
  356. // if e != nil {
  357. // resp.FailMsg("获取失败", "获取销售列表失败, Err: "+e.Error(), c)
  358. // return
  359. // }
  360. // //total := len(adminList)
  361. // adminIdSlice := make([]string, 0)
  362. // sellerMap := make(map[int]*crm.Admin)
  363. // for i := range adminList {
  364. // sellerMap[adminList[i].AdminId] = adminList[i]
  365. // adminIdSlice = append(adminIdSlice, strconv.Itoa(adminList[i].AdminId))
  366. // }
  367. //
  368. // adminStr := strings.Join(adminIdSlice, ",")
  369. //
  370. // total, list, e := fms.GetCensusSellerInvoicePageListV2(page, adminStr, inCond, cond, pars, invSum)
  371. // if e != nil {
  372. // resp.FailMsg("获取失败", "获取销售开票统计列表失败, Err: "+e.Error(), c)
  373. // return
  374. // }
  375. //
  376. // for _, v := range list {
  377. // if admin, ok := sellerMap[v.SellerId]; ok {
  378. // v.GroupName = admin.GroupName
  379. // v.SellerName = admin.RealName
  380. // v.GroupId = admin.GroupId
  381. // }
  382. // }
  383. // // 分组信息, list的groupId可能是三级的ID, 要转为对应的二级
  384. // groupMap, e := crmService.GetSellerTeamGroupMap()
  385. // if e != nil {
  386. // resp.FailMsg("获取失败", "获取组别对应信息失败, Err: "+e.Error(), c)
  387. // return
  388. // }
  389. //
  390. // mulNum := decimal.NewFromFloat(100)
  391. // for i := range list {
  392. // g := groupMap[list[i].GroupId]
  393. // if g != nil {
  394. // list[i].GroupId = g.GroupId
  395. // list[i].GroupName = g.GroupName
  396. // }
  397. // // 处理百分比, 乘100并保留两位小数
  398. // d := decimal.NewFromFloat(list[i].GroupRate)
  399. // d = d.Mul(mulNum).Round(2)
  400. // a, _ := d.Float64()
  401. // list[i].GroupRate = a
  402. // d2 := decimal.NewFromFloat(list[i].SellerRate)
  403. // d2 = d2.Mul(mulNum).Round(2)
  404. // a2, _ := d2.Float64()
  405. // list[i].SellerRate = a2
  406. // }
  407. //
  408. // // 是否导出
  409. // if req.IsExport == 1 {
  410. // ExportInvoiceList(c, list, req)
  411. // return
  412. // }
  413. // page.SetTotal(int64(total))
  414. // baseData := new(base.BaseData)
  415. // baseData.SetPage(page)
  416. // baseData.SetList(list)
  417. // resp.OkData("获取成功", baseData, c)
  418. //}
  419. func (this *SellerController) InvoiceListV2(c *gin.Context) {
  420. var req fms.CensusSellerInvoiceListReq
  421. if e := c.BindQuery(&req); e != nil {
  422. err, ok := e.(validator.ValidationErrors)
  423. if !ok {
  424. resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  425. return
  426. }
  427. resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  428. return
  429. }
  430. page := new(base.Page)
  431. page.SetPageSize(req.PageSize)
  432. page.SetCurrent(req.Current)
  433. if req.IsExport == 1 {
  434. page.SetPageSize(10000)
  435. page.SetCurrent(1)
  436. }
  437. //if req.SellerIds != "" {
  438. // sellerIds := strings.Split(req.SellerIds, ",")
  439. // cond += ` AND (a.seller_id in ? ) `
  440. // historyCond += ` AND (seller_id in ? ) `
  441. // pars = append(pars, sellerIds)
  442. // historyPars = append(historyPars, sellerIds)
  443. //}
  444. var departmentId int
  445. if req.SellerType == 1 {
  446. departmentId = crm.SellerDepartmentId
  447. } else if req.SellerType == 2 {
  448. departmentId = crm.RaiSellerDepartmentId
  449. } else if req.SellerType == 0 {
  450. resp.Fail("请选择销售类型", c)
  451. return
  452. } else {
  453. resp.Fail("请选择正确的销售类型", c)
  454. return
  455. }
  456. var totalMoneySlice []float64
  457. historyTime, _ := time.Parse(utils.FormatDate, "2023-04-01")
  458. // 累计值
  459. var accumulate float64
  460. var startDate, endDate string
  461. //开始日期
  462. if req.StartDate != "" && req.EndDate != "" {
  463. startDate = fmt.Sprint(req.StartDate, " 00:00:00")
  464. endDate = fmt.Sprint(req.EndDate, " 23:59:59")
  465. }
  466. startDateTime, _ := time.Parse(utils.FormatDateTime, startDate)
  467. endDateTime, _ := time.Parse(utils.FormatDateTime, endDate)
  468. cond := ` 1 = 1 `
  469. historyCond := ` 1=1 `
  470. pars := make([]interface{}, 0)
  471. historyPars := make([]interface{}, 0)
  472. if req.CompanyType == 1 {
  473. cond += ` AND b.contract_type = 1 `
  474. historyCond += ` AND new_company = 1 `
  475. } else if req.CompanyType == 2 {
  476. cond += ` AND b.contract_type IN (2,3,4) `
  477. historyCond += ` AND new_company = 0 `
  478. }
  479. if req.GroupId > 0 {
  480. // 筛选组别时, 查询当前组别的下级组(因为admin表存的group_id, 有三级的存的是子ID, 只有二级的存的才是父ID =_=!)
  481. groupCond := `parent_id = ?`
  482. groupPars := make([]interface{}, 0)
  483. groupPars = append(groupPars, req.GroupId)
  484. groupOB := new(crm.SysGroup)
  485. groupList, e := groupOB.List(groupCond, groupPars)
  486. if e != nil {
  487. resp.FailMsg("获取失败", "获取组别下级组列表失败, Err: "+e.Error(), c)
  488. return
  489. }
  490. groupIds := make([]int, 0)
  491. groupIds = append(groupIds, req.GroupId)
  492. for i := range groupList {
  493. groupIds = append(groupIds, groupList[i].GroupId)
  494. }
  495. cond += ` AND (c.seller_group_id IN (?) OR d.seller_group_id IN (?))`
  496. pars = append(pars, groupIds, groupIds)
  497. historyCond += ` AND group_id IN (?) `
  498. historyPars = append(historyPars, groupIds)
  499. }
  500. if req.CompanyType == 1 {
  501. cond += ` AND b.contract_type = 1 `
  502. historyCond += ` AND new_company = 1 `
  503. } else if req.CompanyType == 2 {
  504. cond += ` AND b.contract_type IN (2,3,4) `
  505. historyCond += ` AND new_company = 0 `
  506. }
  507. //if req.SellerIds != "" {
  508. // sellerIds := strings.Split(req.SellerIds, ",")
  509. // cond += ` AND (c.seller_id in ? OR d.seller_id in ?)`
  510. // pars = append(pars, sellerIds, sellerIds)
  511. // prevCond += ` AND (c.seller_id in ? OR d.seller_id in ?)`
  512. // prevPars = append(prevPars, sellerIds, sellerIds)
  513. // histrtyCond += ` AND seller_id in ? `
  514. // prevHistoryCond += ` AND seller_id in ? `
  515. // historyPars = append(historyPars, sellerIds)
  516. // prevHistoryPars = append(prevHistoryPars, sellerIds)
  517. //}
  518. st := startDate
  519. ed := endDate
  520. groupInvoiceMap := make(map[int]float64)
  521. sellerInvoiceMap := make(map[int]float64)
  522. sellerGroupMap := make(map[int]int)
  523. //校验日期,分段查询
  524. if req.StartDate == "" && req.EndDate == "" {
  525. summaryIds, e := fms.GetInvoicePaymentCensusSummaryDataIds(cond, pars)
  526. if e != nil {
  527. return
  528. }
  529. // 开票到款金额合计(换算后)
  530. var amountTotal float64
  531. var amountSum float64
  532. if len(summaryIds) > 0 {
  533. amountCond := `a.id IN ? `
  534. amountPars := make([]interface{}, 0)
  535. amountPars = append(amountPars, summaryIds)
  536. //if req.SellerIds != "" {
  537. // sellerIds := strings.Split(req.SellerIds, ",")
  538. // amountCond += ` AND (( b.seller_id IN ? AND a.invoice_id <> 0 AND b.invoice_time BETWEEN ? AND ?)`
  539. // amountCond += `OR ( d.seller_id IN ? AND a.payment_id <> 0 AND a.invoice_id = 0 AND d.invoice_time BETWEEN ? AND ?)) `
  540. // amountPars = append(amountPars, sellerIds, st, ed, sellerIds, st, ed)
  541. //} else {
  542. // amountCond += ` AND ((a.invoice_id <> 0 AND b.invoice_time BETWEEN ? AND ?)`
  543. // amountCond += `OR (a.payment_id <> 0 AND a.invoice_id = 0 AND d.invoice_time BETWEEN ? AND ?))`
  544. // amountPars = append(amountPars, st, ed, st, ed)
  545. //}
  546. results, e := fms.GetContractSummaryIncomeAmount(amountCond, amountPars)
  547. if e != nil {
  548. resp.FailMsg("查询错误", fmt.Sprintf("获取汇总数据失败, Err: %s", e.Error()), c)
  549. return
  550. }
  551. //dataList = append(dataList, results...)
  552. for _, result := range results {
  553. amountSum += result.Amount
  554. fmt.Println("result.Amount:", result.Amount)
  555. if _, ok := groupInvoiceMap[result.SellerGroupId]; ok {
  556. groupInvoiceMap[result.SellerGroupId] += result.Amount
  557. } else {
  558. groupInvoiceMap[result.SellerGroupId] = result.Amount
  559. }
  560. if _, ok := sellerInvoiceMap[result.FinalSellerId]; ok {
  561. sellerInvoiceMap[result.FinalSellerId] += result.Amount
  562. } else {
  563. sellerInvoiceMap[result.FinalSellerId] = result.Amount
  564. }
  565. if _, ok := sellerGroupMap[result.FinalSellerId]; !ok {
  566. sellerGroupMap[result.FinalSellerId] = result.SellerGroupId
  567. }
  568. }
  569. amountTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", amountSum), 64)
  570. accumulate += amountTotal
  571. amountSum = 0
  572. }
  573. results, e := fms.GetIncomeHistory(historyCond, historyPars)
  574. if e != nil {
  575. resp.FailMsg("查询错误", fmt.Sprintf("获取汇总数据失败, Err: %s", e.Error()), c)
  576. return
  577. }
  578. for _, result := range results {
  579. amountSum += result.Amount
  580. if _, ok := groupInvoiceMap[result.SellerGroupId]; ok {
  581. groupInvoiceMap[result.SellerGroupId] += result.Amount
  582. } else {
  583. groupInvoiceMap[result.SellerGroupId] = result.Amount
  584. }
  585. if _, ok := sellerInvoiceMap[result.FinalSellerId]; ok {
  586. sellerInvoiceMap[result.FinalSellerId] += result.Amount
  587. } else {
  588. sellerInvoiceMap[result.FinalSellerId] = result.Amount
  589. }
  590. if _, ok := sellerGroupMap[result.FinalSellerId]; !ok {
  591. sellerGroupMap[result.FinalSellerId] = result.SellerGroupId
  592. }
  593. }
  594. amountTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", amountSum), 64)
  595. accumulate += amountTotal
  596. } else if startDateTime.After(historyTime) || startDateTime.Equal(historyTime) {
  597. //全部走新查询
  598. //fmt.Println("新查询")
  599. if st != "" && ed != "" {
  600. cond += ` AND ((c.invoice_time BETWEEN ? AND ?) or (d.invoice_time BETWEEN ? AND ?))`
  601. pars = append(pars, st, ed, st, ed)
  602. }
  603. summaryIds, e := fms.GetInvoicePaymentCensusSummaryDataIds(cond, pars)
  604. if e != nil {
  605. return
  606. }
  607. // 开票到款金额合计(换算后)
  608. var amountTotal float64
  609. if len(summaryIds) > 0 {
  610. amountCond := `a.id IN ? `
  611. amountPars := make([]interface{}, 0)
  612. amountPars = append(amountPars, summaryIds)
  613. //if req.SellerIds != "" {
  614. // sellerIds := strings.Split(req.SellerIds, ",")
  615. // amountCond += ` AND (( b.seller_id IN ? AND a.invoice_id <> 0 AND b.invoice_time BETWEEN ? AND ?)`
  616. // amountCond += `OR ( d.seller_id IN ? AND a.payment_id <> 0 AND a.invoice_id = 0 AND d.invoice_time BETWEEN ? AND ?)) `
  617. // amountPars = append(amountPars, sellerIds, st, ed, sellerIds, st, ed)
  618. //} else {
  619. // amountCond += ` AND ((a.invoice_id <> 0 AND b.invoice_time BETWEEN ? AND ?)`
  620. // amountCond += `OR (a.payment_id <> 0 AND a.invoice_id = 0 AND d.invoice_time BETWEEN ? AND ?))`
  621. // amountPars = append(amountPars, st, ed, st, ed)
  622. //}
  623. results, e := fms.GetContractSummaryIncomeAmount(amountCond, amountPars)
  624. if e != nil {
  625. resp.FailMsg("查询错误", fmt.Sprintf("获取汇总数据失败, Err: %s", e.Error()), c)
  626. return
  627. }
  628. //dataList = append(dataList, results...)
  629. var amountSum float64
  630. for _, result := range results {
  631. amountSum += result.Amount
  632. fmt.Println("result.Amount:", result.Amount)
  633. if _, ok := groupInvoiceMap[result.SellerGroupId]; ok {
  634. groupInvoiceMap[result.SellerGroupId] += result.Amount
  635. } else {
  636. groupInvoiceMap[result.SellerGroupId] = result.Amount
  637. }
  638. if _, ok := sellerInvoiceMap[result.FinalSellerId]; ok {
  639. sellerInvoiceMap[result.FinalSellerId] += result.Amount
  640. } else {
  641. sellerInvoiceMap[result.FinalSellerId] = result.Amount
  642. }
  643. if _, ok := sellerGroupMap[result.FinalSellerId]; !ok {
  644. sellerGroupMap[result.FinalSellerId] = result.SellerGroupId
  645. }
  646. }
  647. amountTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", amountSum), 64)
  648. accumulate += amountTotal
  649. }
  650. } else if endDateTime.Before(historyTime) || endDateTime.Equal(historyTime) {
  651. //全部走旧查询
  652. //fmt.Println("旧查询")
  653. //fmt.Println("st:",st)
  654. //fmt.Println("ed:",ed)
  655. if st != "" && ed != "" {
  656. historyCond += ` AND (invoice_time BETWEEN ? AND ? )`
  657. historyPars = append(historyPars, st, ed)
  658. }
  659. //fmt.Println("st:",st)
  660. //fmt.Println("ed:",ed)
  661. // 开票到款金额合计(换算后)
  662. var amountTotal float64
  663. results, e := fms.GetIncomeHistory(historyCond, historyPars)
  664. if e != nil {
  665. resp.FailMsg("查询错误", fmt.Sprintf("获取汇总数据失败, Err: %s", e.Error()), c)
  666. return
  667. }
  668. var amountSum float64
  669. //dataList = append(dataList, results...)
  670. for _, result := range results {
  671. amountSum += result.Amount
  672. if _, ok := groupInvoiceMap[result.SellerGroupId]; ok {
  673. groupInvoiceMap[result.SellerGroupId] += result.Amount
  674. } else {
  675. groupInvoiceMap[result.SellerGroupId] = result.Amount
  676. }
  677. if _, ok := sellerInvoiceMap[result.FinalSellerId]; ok {
  678. sellerInvoiceMap[result.FinalSellerId] += result.Amount
  679. } else {
  680. sellerInvoiceMap[result.FinalSellerId] = result.Amount
  681. }
  682. if _, ok := sellerGroupMap[result.FinalSellerId]; !ok {
  683. sellerGroupMap[result.FinalSellerId] = result.SellerGroupId
  684. }
  685. }
  686. amountTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", amountSum), 64)
  687. accumulate += amountTotal
  688. //fmt.Println("partAccumulate:",partAccumulate)
  689. } else {
  690. if st != "" && ed != "" {
  691. cond += ` AND ((c.invoice_time BETWEEN ? AND ?) or (d.invoice_time BETWEEN ? AND ?))`
  692. pars = append(pars, st, ed, st, ed)
  693. }
  694. summaryIds, e := fms.GetInvoicePaymentCensusSummaryDataIds(cond, pars)
  695. if e != nil {
  696. return
  697. }
  698. // 开票到款金额合计(换算后)
  699. var amountTotal float64
  700. var amountSum float64
  701. if len(summaryIds) > 0 {
  702. amountCond := `a.id IN ? `
  703. amountPars := make([]interface{}, 0)
  704. amountPars = append(amountPars, summaryIds)
  705. //if req.SellerIds != "" {
  706. // sellerIds := strings.Split(req.SellerIds, ",")
  707. // amountCond += ` AND (( b.seller_id IN ? AND a.invoice_id <> 0 AND b.invoice_time BETWEEN ? AND ?)`
  708. // amountCond += `OR ( d.seller_id IN ? AND a.payment_id <> 0 AND a.invoice_id = 0 AND d.invoice_time BETWEEN ? AND ?)) `
  709. // amountPars = append(amountPars, sellerIds, st, ed, sellerIds, st, ed)
  710. //} else {
  711. // amountCond += ` AND ((a.invoice_id <> 0 AND b.invoice_time BETWEEN ? AND ?)`
  712. // amountCond += `OR (a.payment_id <> 0 AND a.invoice_id = 0 AND d.invoice_time BETWEEN ? AND ?))`
  713. // amountPars = append(amountPars, st, ed, st, ed)
  714. //}
  715. results, e := fms.GetContractSummaryIncomeAmount(amountCond, amountPars)
  716. if e != nil {
  717. resp.FailMsg("查询错误", fmt.Sprintf("获取汇总数据失败, Err: %s", e.Error()), c)
  718. return
  719. }
  720. //dataList = append(dataList, results...)
  721. for _, result := range results {
  722. amountSum += result.Amount
  723. fmt.Println("result.Amount:", result.Amount)
  724. if _, ok := groupInvoiceMap[result.SellerGroupId]; ok {
  725. groupInvoiceMap[result.SellerGroupId] += result.Amount
  726. } else {
  727. groupInvoiceMap[result.SellerGroupId] = result.Amount
  728. }
  729. if _, ok := sellerInvoiceMap[result.FinalSellerId]; ok {
  730. sellerInvoiceMap[result.FinalSellerId] += result.Amount
  731. } else {
  732. sellerInvoiceMap[result.FinalSellerId] = result.Amount
  733. }
  734. if _, ok := sellerGroupMap[result.FinalSellerId]; !ok {
  735. sellerGroupMap[result.FinalSellerId] = result.SellerGroupId
  736. }
  737. }
  738. amountTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", amountSum), 64)
  739. accumulate += amountTotal
  740. amountSum = 0
  741. }
  742. if st != "" && ed != "" {
  743. historyCond += ` AND (invoice_time BETWEEN ? AND ? )`
  744. historyPars = append(historyPars, st, ed)
  745. }
  746. results, e := fms.GetIncomeHistory(historyCond, historyPars)
  747. if e != nil {
  748. resp.FailMsg("查询错误", fmt.Sprintf("获取汇总数据失败, Err: %s", e.Error()), c)
  749. return
  750. }
  751. for _, result := range results {
  752. amountSum += result.Amount
  753. if _, ok := groupInvoiceMap[result.SellerGroupId]; ok {
  754. groupInvoiceMap[result.SellerGroupId] += result.Amount
  755. } else {
  756. groupInvoiceMap[result.SellerGroupId] = result.Amount
  757. }
  758. if _, ok := sellerInvoiceMap[result.FinalSellerId]; ok {
  759. sellerInvoiceMap[result.FinalSellerId] += result.Amount
  760. } else {
  761. sellerInvoiceMap[result.FinalSellerId] = result.Amount
  762. }
  763. if _, ok := sellerGroupMap[result.FinalSellerId]; !ok {
  764. sellerGroupMap[result.FinalSellerId] = result.SellerGroupId
  765. }
  766. }
  767. amountTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", amountSum), 64)
  768. accumulate += amountTotal
  769. }
  770. fmt.Println("totalMoneySlice:", len(totalMoneySlice))
  771. if req.SellerType == 1 {
  772. departmentId = crm.SellerDepartmentId
  773. } else if req.SellerType == 2 {
  774. departmentId = crm.RaiSellerDepartmentId
  775. } else if req.SellerType == 0 {
  776. resp.FailMsg("查询错误", "请选择正确的销售类型", c)
  777. return
  778. } else {
  779. resp.FailMsg("查询错误", "请选择正确的销售类型", c)
  780. return
  781. }
  782. adminOB := new(crm.Admin)
  783. adminPars := make([]interface{}, 0)
  784. adminCond := ` department_id = %d `
  785. adminCond = fmt.Sprintf(adminCond, departmentId)
  786. adminList, e := adminOB.List(adminCond, adminPars)
  787. if e != nil {
  788. resp.FailMsg("获取失败", "获取销售列表失败, Err: "+e.Error(), c)
  789. return
  790. }
  791. // 获取销售分组信息
  792. sellerList, e := crmService.GetSellerDepartmentListWithEnable()
  793. if e != nil {
  794. resp.FailData("获取销售失败", "Err:"+e.Error(), c)
  795. return
  796. }
  797. sellerMap := make(map[int]*crm.SellerAdminWithGroupTeam)
  798. for i := range sellerList {
  799. sellerMap[sellerList[i].SellerId] = sellerList[i]
  800. }
  801. list := make([]*fms.CensusSellerInvoiceItem, 0)
  802. for _, v := range adminList {
  803. if vv, ok := sellerInvoiceMap[v.AdminId]; ok {
  804. rate := vv / accumulate
  805. mulNum := decimal.NewFromFloat(100)
  806. newRate := decimal.NewFromFloat(rate)
  807. newRate = newRate.Mul(mulNum).Round(2)
  808. finalRate, _ := newRate.Float64()
  809. groupId := sellerGroupMap[v.AdminId]
  810. groupRate := vv / groupInvoiceMap[groupId]
  811. newGroupRate := decimal.NewFromFloat(groupRate)
  812. newGroupRate = newGroupRate.Mul(mulNum).Round(2)
  813. finalGroupRate, _ := newGroupRate.Float64()
  814. amuont, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", vv), 64)
  815. var sellerName, groupName string
  816. if seller, ok := sellerMap[v.AdminId]; ok {
  817. sellerName = seller.SellerName
  818. groupName = seller.GroupName
  819. }
  820. list = append(list, &fms.CensusSellerInvoiceItem{
  821. SellerId: v.AdminId,
  822. SellerName: sellerName,
  823. GroupId: groupId,
  824. GroupName: groupName,
  825. InvoiceAmount: amuont,
  826. GroupRate: finalGroupRate,
  827. SellerRate: finalRate,
  828. })
  829. }
  830. }
  831. var respList fms.CensusSellerList
  832. respList = list
  833. sort.Sort(respList)
  834. // 是否导出
  835. if req.IsExport == 1 {
  836. ExportInvoiceList(c, respList, req)
  837. return
  838. }
  839. page.SetTotal(int64(len(list)))
  840. baseData := new(base.BaseData)
  841. baseData.SetPage(page)
  842. if req.Current*req.PageSize > int64(len(list)) {
  843. baseData.SetList(respList[(req.Current-1)*req.PageSize : len(list)])
  844. } else {
  845. baseData.SetList(respList[(req.Current-1)*req.PageSize : req.Current*req.PageSize])
  846. }
  847. resp.OkData("获取成功", baseData, c)
  848. }
  849. // ExportInvoiceList 导出销售开票统计列表
  850. func ExportInvoiceList(c *gin.Context, list []*fms.CensusSellerInvoiceItem, req fms.CensusSellerInvoiceListReq) {
  851. // 生成Excel文件
  852. xlsxFile := xlsx.NewFile()
  853. style := xlsx.NewStyle()
  854. alignment := xlsx.Alignment{
  855. Horizontal: "center",
  856. Vertical: "center",
  857. WrapText: true,
  858. }
  859. style.Alignment = alignment
  860. style.ApplyAlignment = true
  861. sheetName := "销售开票统计"
  862. sheet, err := xlsxFile.AddSheet(sheetName)
  863. if err != nil {
  864. resp.FailData("新增Sheet失败", "Err:"+err.Error(), c)
  865. return
  866. }
  867. // 存在筛选则前两行显示时间信息
  868. if req.StartDate != "" && req.EndDate != "" {
  869. timeData := fmt.Sprintf("时间:%s至%s", req.StartDate, req.EndDate)
  870. rowA := sheet.AddRow()
  871. cellAA := rowA.AddCell()
  872. cellAA.SetString("销售统计表")
  873. cellAA.SetStyle(style)
  874. rowB := sheet.AddRow()
  875. rowB.AddCell().SetString(timeData)
  876. // 第三行空出
  877. sheet.AddRow()
  878. }
  879. // 数据表头
  880. rowTitle := []string{"排名", "销售员", "销售组别", "收入金额(元)", "小组占比", "全员占比"}
  881. titleRow := sheet.AddRow()
  882. for i := range rowTitle {
  883. v := titleRow.AddCell()
  884. v.SetString(rowTitle[i])
  885. v.SetStyle(style)
  886. }
  887. // 填充数据
  888. for k, v := range list {
  889. dataRow := sheet.AddRow()
  890. dataRow.AddCell().SetString(fmt.Sprint(k + 1)) // 排名
  891. dataRow.AddCell().SetString(v.SellerName) // 销售员
  892. dataRow.AddCell().SetString(v.GroupName) // 销售组别
  893. dataRow.AddCell().SetString(fmt.Sprint(v.InvoiceAmount)) // 开票金额
  894. dataRow.AddCell().SetString(fmt.Sprint(v.GroupRate, "%")) // 小组占比
  895. dataRow.AddCell().SetString(fmt.Sprint(v.SellerRate, "%")) // 全员占比
  896. }
  897. // 输出文件
  898. var buffer bytes.Buffer
  899. _ = xlsxFile.Write(&buffer)
  900. content := bytes.NewReader(buffer.Bytes())
  901. randStr := time.Now().Format(utils.FormatDateTimeUnSpace)
  902. fileName := sheetName + randStr + ".xlsx"
  903. c.Writer.Header().Add("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, fileName))
  904. c.Writer.Header().Add("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
  905. http.ServeContent(c.Writer, c.Request, fileName, time.Now(), content)
  906. }
  907. // GroupInvoiceList
  908. // @Title 销售组开票统计列表
  909. // @Description 销售组开票统计列表
  910. // @Param StartDate query string false "开始日期"
  911. // @Param EndDate query string false "结束日期"
  912. // @Param SortField query int false "排序字段: 1-开票金额; 2-组别占比"
  913. // @Param SortType query int false "排序方式: 1-正序; 2-倒序"
  914. // @Param IsExport query int false "是否导出: 0-否; 1-是"
  915. // @Success 200 {object} fms.CensusSellerGroupInvoiceItem
  916. // @router /census/seller/group_invoice_list [get]
  917. func (this *SellerController) GroupInvoiceListV2(c *gin.Context) {
  918. var req fms.CensusSellerGroupInvoiceListReq
  919. if e := c.BindQuery(&req); e != nil {
  920. err, ok := e.(validator.ValidationErrors)
  921. if !ok {
  922. resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  923. return
  924. }
  925. resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  926. return
  927. }
  928. //if req.SellerIds != "" {
  929. // sellerIds := strings.Split(req.SellerIds, ",")
  930. // cond += ` AND (a.seller_id in ? ) `
  931. // historyCond += ` AND (seller_id in ? ) `
  932. // pars = append(pars, sellerIds)
  933. // historyPars = append(historyPars, sellerIds)
  934. //}
  935. var totalMoneySlice []float64
  936. historyTime, _ := time.Parse(utils.FormatDate, "2023-04-01")
  937. // 累计值
  938. var accumulate float64
  939. //dataList := make([]*fms.IncomeSummaryItem, 0)
  940. //historydataList := make([]*fms.IncomeSummaryItem, 0)
  941. //timeNow, _ := time.Parse("2006-01", time.Now().Format("2006-01"))
  942. var startDate, endDate string
  943. //开始日期
  944. if req.StartDate != "" && req.EndDate != "" {
  945. startDate = fmt.Sprint(req.StartDate, " 00:00:00")
  946. endDate = fmt.Sprint(req.EndDate, " 23:59:59")
  947. }
  948. startDateTime, _ := time.Parse(utils.FormatDate, req.StartDate)
  949. endDateTime, _ := time.Parse(utils.FormatDate, req.EndDate)
  950. cond := `1 = 1`
  951. histortyCond := `1 = 1`
  952. pars := make([]interface{}, 0)
  953. historyPars := make([]interface{}, 0)
  954. if req.CompanyType == 1 {
  955. cond += ` AND b.contract_type = 1 `
  956. histortyCond += ` AND new_company = 1 `
  957. } else if req.CompanyType == 2 {
  958. cond += ` AND b.contract_type IN (2,3,4) `
  959. histortyCond += ` AND new_company = 0 `
  960. }
  961. //if req.SellerIds != "" {
  962. // sellerIds := strings.Split(req.SellerIds, ",")
  963. // cond += ` AND (c.seller_id in ? OR d.seller_id in ?)`
  964. // pars = append(pars, sellerIds, sellerIds)
  965. // prevCond += ` AND (c.seller_id in ? OR d.seller_id in ?)`
  966. // prevPars = append(prevPars, sellerIds, sellerIds)
  967. // histrtyCond += ` AND seller_id in ? `
  968. // prevHistoryCond += ` AND seller_id in ? `
  969. // historyPars = append(historyPars, sellerIds)
  970. // prevHistoryPars = append(prevHistoryPars, sellerIds)
  971. //}
  972. st := fmt.Sprint(startDate, " 00:00:00")
  973. ed := fmt.Sprint(endDate, " 23:59:59")
  974. groupInvoiceMap := make(map[int]float64)
  975. //校验日期,分段查询
  976. if req.StartDate == "" && req.EndDate == "" {
  977. summaryIds, e := fms.GetInvoicePaymentCensusSummaryDataIds(cond, pars)
  978. if e != nil {
  979. return
  980. }
  981. // 开票到款金额合计(换算后)
  982. var amountTotal float64
  983. var amountSum float64
  984. if len(summaryIds) > 0 {
  985. amountCond := `a.id IN ? `
  986. amountPars := make([]interface{}, 0)
  987. amountPars = append(amountPars, summaryIds)
  988. //if req.SellerIds != "" {
  989. // sellerIds := strings.Split(req.SellerIds, ",")
  990. // amountCond += ` AND (( b.seller_id IN ? AND a.invoice_id <> 0 AND b.invoice_time BETWEEN ? AND ?)`
  991. // amountCond += `OR ( d.seller_id IN ? AND a.payment_id <> 0 AND a.invoice_id = 0 AND d.invoice_time BETWEEN ? AND ?)) `
  992. // amountPars = append(amountPars, sellerIds, st, ed, sellerIds, st, ed)
  993. //} else {
  994. // amountCond += ` AND ((a.invoice_id <> 0 AND b.invoice_time BETWEEN ? AND ?)`
  995. // amountCond += `OR (a.payment_id <> 0 AND a.invoice_id = 0 AND d.invoice_time BETWEEN ? AND ?))`
  996. // amountPars = append(amountPars, st, ed, st, ed)
  997. //}
  998. results, e := fms.GetContractSummaryIncomeAmount(amountCond, amountPars)
  999. if e != nil {
  1000. resp.FailMsg("查询错误", fmt.Sprintf("获取汇总数据失败, Err: %s", e.Error()), c)
  1001. return
  1002. }
  1003. //dataList = append(dataList, results...)
  1004. for _, result := range results {
  1005. amountSum += result.Amount
  1006. fmt.Println("result.Amount:", result.Amount)
  1007. if _, ok := groupInvoiceMap[result.SellerGroupId]; ok {
  1008. groupInvoiceMap[result.SellerGroupId] += result.Amount
  1009. } else {
  1010. groupInvoiceMap[result.SellerGroupId] = result.Amount
  1011. }
  1012. }
  1013. amountTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", amountSum), 64)
  1014. accumulate += amountTotal
  1015. amountSum = 0
  1016. }
  1017. //var amountTotal float64
  1018. results, e := fms.GetIncomeHistory(histortyCond, historyPars)
  1019. if e != nil {
  1020. resp.FailMsg("查询错误", fmt.Sprintf("获取汇总数据失败, Err: %s", e.Error()), c)
  1021. return
  1022. }
  1023. //dataList = append(dataList, results...)
  1024. for _, result := range results {
  1025. amountSum += result.Amount
  1026. if _, ok := groupInvoiceMap[result.SellerGroupId]; ok {
  1027. groupInvoiceMap[result.SellerGroupId] += result.Amount
  1028. } else {
  1029. groupInvoiceMap[result.SellerGroupId] = result.Amount
  1030. }
  1031. }
  1032. amountTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", amountSum), 64)
  1033. accumulate += amountTotal
  1034. } else if startDateTime.After(historyTime) || startDateTime.Equal(historyTime) {
  1035. //全部走新查询
  1036. //fmt.Println("新查询")
  1037. if st != "" && ed != "" {
  1038. cond += ` AND ((c.invoice_time BETWEEN ? AND ?) or (d.invoice_time BETWEEN ? AND ?))`
  1039. pars = append(pars, st, ed, st, ed)
  1040. }
  1041. summaryIds, e := fms.GetInvoicePaymentCensusSummaryDataIds(cond, pars)
  1042. if e != nil {
  1043. return
  1044. }
  1045. // 开票到款金额合计(换算后)
  1046. var amountTotal float64
  1047. if len(summaryIds) > 0 {
  1048. amountCond := `a.id IN ? `
  1049. amountPars := make([]interface{}, 0)
  1050. amountPars = append(amountPars, summaryIds)
  1051. //if req.SellerIds != "" {
  1052. // sellerIds := strings.Split(req.SellerIds, ",")
  1053. // amountCond += ` AND (( b.seller_id IN ? AND a.invoice_id <> 0 AND b.invoice_time BETWEEN ? AND ?)`
  1054. // amountCond += `OR ( d.seller_id IN ? AND a.payment_id <> 0 AND a.invoice_id = 0 AND d.invoice_time BETWEEN ? AND ?)) `
  1055. // amountPars = append(amountPars, sellerIds, st, ed, sellerIds, st, ed)
  1056. //} else {
  1057. // amountCond += ` AND ((a.invoice_id <> 0 AND b.invoice_time BETWEEN ? AND ?)`
  1058. // amountCond += `OR (a.payment_id <> 0 AND a.invoice_id = 0 AND d.invoice_time BETWEEN ? AND ?))`
  1059. // amountPars = append(amountPars, st, ed, st, ed)
  1060. //}
  1061. results, e := fms.GetContractSummaryIncomeAmount(amountCond, amountPars)
  1062. if e != nil {
  1063. resp.FailMsg("查询错误", fmt.Sprintf("获取汇总数据失败, Err: %s", e.Error()), c)
  1064. return
  1065. }
  1066. //dataList = append(dataList, results...)
  1067. var amountSum float64
  1068. for _, result := range results {
  1069. amountSum += result.Amount
  1070. fmt.Println("result.Amount:", result.Amount)
  1071. if _, ok := groupInvoiceMap[result.SellerGroupId]; ok {
  1072. groupInvoiceMap[result.SellerGroupId] += result.Amount
  1073. } else {
  1074. groupInvoiceMap[result.SellerGroupId] = result.Amount
  1075. }
  1076. }
  1077. amountTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", amountSum), 64)
  1078. accumulate += amountTotal
  1079. }
  1080. } else if endDateTime.Before(historyTime) || endDateTime.Equal(historyTime) {
  1081. //全部走旧查询
  1082. //fmt.Println("旧查询")
  1083. //fmt.Println("st:",st)
  1084. //fmt.Println("ed:",ed)
  1085. if st != "" && ed != "" {
  1086. histortyCond += ` AND (invoice_time BETWEEN ? AND ? )`
  1087. historyPars = append(historyPars, st, ed)
  1088. }
  1089. //fmt.Println("st:",st)
  1090. //fmt.Println("ed:",ed)
  1091. // 开票到款金额合计(换算后)
  1092. var amountTotal float64
  1093. results, e := fms.GetIncomeHistory(histortyCond, historyPars)
  1094. if e != nil {
  1095. resp.FailMsg("查询错误", fmt.Sprintf("获取汇总数据失败, Err: %s", e.Error()), c)
  1096. return
  1097. }
  1098. var amountSum float64
  1099. //dataList = append(dataList, results...)
  1100. for _, result := range results {
  1101. amountSum += result.Amount
  1102. if _, ok := groupInvoiceMap[result.SellerGroupId]; ok {
  1103. groupInvoiceMap[result.SellerGroupId] += result.Amount
  1104. } else {
  1105. groupInvoiceMap[result.SellerGroupId] = result.Amount
  1106. }
  1107. }
  1108. amountTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", amountSum), 64)
  1109. accumulate += amountTotal
  1110. //fmt.Println("partAccumulate:",partAccumulate)
  1111. } else {
  1112. // 有时间,分段查询
  1113. // 新查询
  1114. if st != "" && ed != "" {
  1115. cond += ` AND ((c.invoice_time BETWEEN ? AND ?) or (d.invoice_time BETWEEN ? AND ?))`
  1116. pars = append(pars, st, ed, st, ed)
  1117. }
  1118. summaryIds, e := fms.GetInvoicePaymentCensusSummaryDataIds(cond, pars)
  1119. if e != nil {
  1120. return
  1121. }
  1122. // 开票到款金额合计(换算后)
  1123. var amountTotal float64
  1124. var amountSum float64
  1125. if len(summaryIds) > 0 {
  1126. amountCond := `a.id IN ? `
  1127. amountPars := make([]interface{}, 0)
  1128. amountPars = append(amountPars, summaryIds)
  1129. //if req.SellerIds != "" {
  1130. // sellerIds := strings.Split(req.SellerIds, ",")
  1131. // amountCond += ` AND (( b.seller_id IN ? AND a.invoice_id <> 0 AND b.invoice_time BETWEEN ? AND ?)`
  1132. // amountCond += `OR ( d.seller_id IN ? AND a.payment_id <> 0 AND a.invoice_id = 0 AND d.invoice_time BETWEEN ? AND ?)) `
  1133. // amountPars = append(amountPars, sellerIds, st, ed, sellerIds, st, ed)
  1134. //} else {
  1135. // amountCond += ` AND ((a.invoice_id <> 0 AND b.invoice_time BETWEEN ? AND ?)`
  1136. // amountCond += `OR (a.payment_id <> 0 AND a.invoice_id = 0 AND d.invoice_time BETWEEN ? AND ?))`
  1137. // amountPars = append(amountPars, st, ed, st, ed)
  1138. //}
  1139. results, e := fms.GetContractSummaryIncomeAmount(amountCond, amountPars)
  1140. if e != nil {
  1141. resp.FailMsg("查询错误", fmt.Sprintf("获取汇总数据失败, Err: %s", e.Error()), c)
  1142. return
  1143. }
  1144. //dataList = append(dataList, results...)
  1145. for _, result := range results {
  1146. amountSum += result.Amount
  1147. fmt.Println("result.Amount:", result.Amount)
  1148. if _, ok := groupInvoiceMap[result.SellerGroupId]; ok {
  1149. groupInvoiceMap[result.SellerGroupId] += result.Amount
  1150. } else {
  1151. groupInvoiceMap[result.SellerGroupId] = result.Amount
  1152. }
  1153. }
  1154. amountTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", amountSum), 64)
  1155. accumulate += amountTotal
  1156. amountSum = 0
  1157. //var amountTotal float64
  1158. // 旧查询
  1159. }
  1160. if st != "" && ed != "" {
  1161. histortyCond += ` AND (invoice_time BETWEEN ? AND ? )`
  1162. historyPars = append(historyPars, st, ed)
  1163. }
  1164. results, e := fms.GetIncomeHistory(histortyCond, historyPars)
  1165. if e != nil {
  1166. resp.FailMsg("查询错误", fmt.Sprintf("获取汇总数据失败, Err: %s", e.Error()), c)
  1167. return
  1168. }
  1169. //dataList = append(dataList, results...)
  1170. for _, result := range results {
  1171. amountSum += result.Amount
  1172. if _, ok := groupInvoiceMap[result.SellerGroupId]; ok {
  1173. groupInvoiceMap[result.SellerGroupId] += result.Amount
  1174. } else {
  1175. groupInvoiceMap[result.SellerGroupId] = result.Amount
  1176. }
  1177. }
  1178. amountTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", amountSum), 64)
  1179. accumulate += amountTotal
  1180. }
  1181. fmt.Println("totalMoneySlice:", len(totalMoneySlice))
  1182. var departmentId int
  1183. if req.SellerType == 1 {
  1184. departmentId = crm.SellerDepartmentId
  1185. } else if req.SellerType == 2 {
  1186. departmentId = crm.RaiSellerDepartmentId
  1187. } else if req.SellerType == 0 {
  1188. resp.FailMsg("查询错误", "请选择正确的销售类型", c)
  1189. return
  1190. } else {
  1191. resp.FailMsg("查询错误", "请选择正确的销售类型", c)
  1192. return
  1193. }
  1194. groupCond := ` department_id = %d AND parent_id = 0 `
  1195. groupCond = fmt.Sprintf(groupCond, departmentId)
  1196. groupPars := make([]interface{}, 0)
  1197. groupOB := new(crm.SysGroup)
  1198. groupList, e := groupOB.List(groupCond, groupPars)
  1199. if e != nil {
  1200. resp.FailMsg("查询错误", fmt.Sprintf("获取组别列表失败, Err: %s", e.Error()), c)
  1201. return
  1202. }
  1203. //total := len(groupList)
  1204. groupMap := make(map[int]*crm.SysGroup)
  1205. for i := range groupList {
  1206. groupMap[groupList[i].GroupId] = groupList[i]
  1207. }
  1208. list := make([]*fms.CensusSellerGroupInvoiceItem, 0)
  1209. for _, group := range groupList {
  1210. if v, ok := groupInvoiceMap[group.GroupId]; ok {
  1211. rate := v / accumulate
  1212. mulNum := decimal.NewFromFloat(100)
  1213. newRate := decimal.NewFromFloat(rate)
  1214. newRate = newRate.Mul(mulNum).Round(2)
  1215. finalRate, _ := newRate.Float64()
  1216. amuont, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", v), 64)
  1217. groupName := ""
  1218. if groupItem, ok := groupMap[group.GroupId]; ok {
  1219. groupName = groupItem.GroupName
  1220. }
  1221. list = append(list, &fms.CensusSellerGroupInvoiceItem{
  1222. GroupId: group.GroupId,
  1223. GroupName: groupName,
  1224. InvoiceAmount: amuont,
  1225. GroupRate: finalRate,
  1226. })
  1227. }
  1228. }
  1229. var respList fms.CensusSellerGroupList
  1230. respList = list
  1231. sort.Sort(respList)
  1232. // 是否导出
  1233. if req.IsExport == 1 {
  1234. ExportGroupInvoiceList(c, respList, req)
  1235. return
  1236. }
  1237. resp.OkData("获取成功", respList, c)
  1238. }