contract_allocation.go 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288
  1. package cygx
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/rdlucklib/rdluck_tools/paging"
  6. "github.com/tealeg/xlsx"
  7. "hongze/hz_crm_api/controllers"
  8. "hongze/hz_crm_api/models"
  9. "hongze/hz_crm_api/models/company"
  10. "hongze/hz_crm_api/models/contract"
  11. "hongze/hz_crm_api/models/cygx"
  12. "hongze/hz_crm_api/models/system"
  13. cygxService "hongze/hz_crm_api/services/cygx"
  14. "hongze/hz_crm_api/utils"
  15. "math"
  16. "os"
  17. "path/filepath"
  18. "strconv"
  19. "strings"
  20. "time"
  21. )
  22. // ContractAllocationController 权益合同派单
  23. type ContractAllocationController struct {
  24. controllers.BaseAuthController
  25. }
  26. // getQueryParams 获取基础查询信息
  27. func getQueryParams(condition string, pars []interface{}, sysUser *system.Admin, tableAlias string) (newCondition string, newPars []interface{}) {
  28. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN {
  29. condition += " AND " + tableAlias + "product_id=?"
  30. pars = append(pars, 1)
  31. } else if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_ADMIN {
  32. condition += " AND " + tableAlias + "product_id=?"
  33. pars = append(pars, 2)
  34. } else if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN || sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FINANCE {
  35. //超级管理员账户,不做条件限制
  36. } else {
  37. //如果不是研究员,那么去找对应的 部门、小组、销售
  38. if sysUser.Authority == 0 {
  39. //普通用户
  40. condition += " AND " + tableAlias + "seller_id=?"
  41. pars = append(pars, sysUser.AdminId)
  42. } else if sysUser.Authority == 1 {
  43. //部门主管
  44. condition += " AND " + tableAlias + "department_id=?"
  45. pars = append(pars, sysUser.DepartmentId)
  46. } else if sysUser.Authority == 2 && sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_GROUP {
  47. //权益小组负责人
  48. condition += " AND " + tableAlias + "group_id=?"
  49. pars = append(pars, sysUser.GroupId)
  50. } else if sysUser.Authority == 2 && sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_GROUP {
  51. //ficc销售主管
  52. pid, err := company.GetParentIdFromGroup(sysUser.GroupId)
  53. if err != nil {
  54. fmt.Println(err.Error())
  55. return
  56. }
  57. var ids []*string
  58. if pid != nil && *pid != 0 {
  59. ids, err = company.GetGroupIdsByParentId(*pid)
  60. if err != nil {
  61. fmt.Println(err.Error())
  62. }
  63. } else {
  64. ids, err = company.GetGroupIdsByParentId(sysUser.GroupId)
  65. if err != nil {
  66. fmt.Println(err.Error())
  67. }
  68. }
  69. var idSlice []string
  70. var sid string
  71. for _, id := range ids {
  72. idSlice = append(idSlice, *id)
  73. }
  74. //加入父级groupId
  75. if *pid > 0 {
  76. idSlice = append(idSlice, strconv.Itoa(*pid))
  77. } else {
  78. idSlice = append(idSlice, strconv.Itoa(sysUser.GroupId))
  79. }
  80. sid = strings.Join(idSlice, ",")
  81. condition += " AND " + tableAlias + `group_id IN (` + sid + `) `
  82. fmt.Println("condition:", condition)
  83. //pars = append(pars, sysUser.GroupId)
  84. } else if sysUser.Authority == 4 {
  85. //ficc小组负责人
  86. condition += " AND " + tableAlias + "group_id=?"
  87. pars = append(pars, sysUser.GroupId)
  88. } else {
  89. //不知道什么类型的用户(后面新增的位置类型客户)
  90. condition += " AND " + tableAlias + "seller_id=?"
  91. pars = append(pars, sysUser.AdminId)
  92. }
  93. }
  94. newCondition = condition
  95. newPars = pars
  96. return
  97. }
  98. // @Title 合同列表
  99. // @Description 合同列表接口
  100. // @Param PageSize query int true "每页数据条数"
  101. // @Param CurrentIndex query int true "当前页页码,从1开始"
  102. // @Param Keyword query string true "客户名称"
  103. // @Param AdminId query string true "销售id,多个用英文逗号隔开,空字符串为全部"
  104. // @Param StartDate query string false "开始日期"
  105. // @Param EndDate query string false "结束日期"
  106. // @Param ContractType query string false "合同类型,枚举值:“,`新签合同`,`续约合同`,`补充协议`"
  107. // @Param FormalType query string false "转正类型,枚举值:“,`标准`,`非标`"
  108. // @Param IsExport query bool false "是否导出excel,默认是false"
  109. // @Param ResearcherRealName query string false "研究员姓名"
  110. // @Param IsAllocation query int false "派点状态: -1-默认全部; 0-未派点; 1-已派点"
  111. // @Success 200 {object} cygx.CompanyContractListResp
  112. // @router /allocation/company_contract_list [get]
  113. func (this *ContractAllocationController) CompanyContractList() {
  114. br := new(models.BaseResponse).Init()
  115. defer func() {
  116. this.Data["json"] = br
  117. this.ServeJSON()
  118. }()
  119. sysUser := this.SysUser
  120. if sysUser == nil {
  121. br.Msg = "请登录"
  122. br.ErrMsg = "请登录,SysUser Is Empty"
  123. br.Ret = 408
  124. return
  125. }
  126. pageSize, _ := this.GetInt("PageSize")
  127. currentIndex, _ := this.GetInt("CurrentIndex")
  128. adminId := this.GetString("AdminId")
  129. formalType := this.GetString("FormalType")
  130. contractType := this.GetString("ContractType")
  131. keyword := this.GetString("Keyword")
  132. startDate := this.GetString("StartDate")
  133. endDate := this.GetString("EndDate")
  134. researcherRealName := this.GetString("ResearcherRealName")
  135. isAllocation, _ := this.GetInt("IsAllocation", -1) // CRM 13.9
  136. if startDate == "" {
  137. startDate = "2015-01-01"
  138. }
  139. var startSize int
  140. if pageSize <= 0 {
  141. pageSize = utils.PageSize20
  142. }
  143. if currentIndex <= 0 {
  144. currentIndex = 1
  145. }
  146. startSize = utils.StartIndex(currentIndex, pageSize)
  147. //是否导出报表
  148. isExport, _ := this.GetBool("IsExport")
  149. if isExport {
  150. pageSize = 10000
  151. currentIndex = 1
  152. }
  153. var condition string
  154. var pars []interface{}
  155. if endDate != "" {
  156. condition += ` AND a.start_date >= ? AND a.start_date <= ? `
  157. pars = append(pars, startDate+" 00:00:01", endDate+" 23:59:59")
  158. }
  159. //条件
  160. if adminId != "" {
  161. condition += ` AND c.seller_id in (` + adminId + `) `
  162. //pars = append(pars, adminId)
  163. }
  164. //权益申请销售只能看到自己名下的客户的申请
  165. companyIds, err := cygxService.GetAdminLookUserCompanyIdsBySelf(sysUser)
  166. if err != nil {
  167. br.Msg = "获取失败"
  168. br.ErrMsg = "获取失败,GetAdminLookUserCompanyIds Err:" + err.Error()
  169. return
  170. }
  171. lencompanyIds := len(companyIds)
  172. if lencompanyIds > 0 {
  173. condition += ` AND c.company_id IN (` + utils.GetOrmInReplace(lencompanyIds) + `)`
  174. pars = append(pars, companyIds)
  175. }
  176. //关键字搜索
  177. if keyword != "" {
  178. condition += ` and b.company_name like "%` + keyword + `%" `
  179. }
  180. //是否派点
  181. if isAllocation != -1 {
  182. condition += ` AND a.is_allocation = ? `
  183. pars = append(pars, isAllocation)
  184. }
  185. // 标准非标查询
  186. switch formalType {
  187. case "标准":
  188. condition += ` AND a.source = ? `
  189. pars = append(pars, "系统合同")
  190. case "非标":
  191. condition += ` AND a.source = ? `
  192. pars = append(pars, "上传附件")
  193. }
  194. if contractType != "" {
  195. condition += ` AND a.contract_type = ? `
  196. pars = append(pars, contractType)
  197. }
  198. //默认只查询权益 2023-06-01 之后的合同
  199. condition += ` AND c.product_id = ? AND a.start_date > ? `
  200. pars = append(pars, 2, "2023-05-31")
  201. mapMoneyPoint := make(map[int]float64)
  202. //研究员姓名查询
  203. if researcherRealName != "" {
  204. var conditionAllocation string
  205. var parsAllocation []interface{}
  206. conditionAllocation = " AND real_name = ? AND money != 0 "
  207. parsAllocation = append(parsAllocation, researcherRealName)
  208. allocationCompanyContractList, err := cygx.GetCygxAllocationCompanyContractList(conditionAllocation, parsAllocation)
  209. if err != nil {
  210. br.Msg = "获取失败"
  211. br.ErrMsg = "获取失败,GetCygxAllocationCompanyContractListErr:" + err.Error()
  212. return
  213. }
  214. var companyContractIds []int
  215. for _, v := range allocationCompanyContractList {
  216. companyContractIds = append(companyContractIds, v.CompanyContractId)
  217. mapMoneyPoint[v.CompanyContractId] = v.Money
  218. }
  219. lenCon := len(companyContractIds)
  220. if lenCon == 0 {
  221. condition += ` AND a.company_contract_id = 0 `
  222. } else {
  223. condition += ` AND a.company_contract_id IN (` + utils.GetOrmInReplace(lenCon) + `)`
  224. pars = append(pars, companyContractIds)
  225. }
  226. }
  227. var list []*cygx.CompanyContractResp
  228. total, err := cygx.GetCompanyContractCountJoinCompany(condition, pars)
  229. if err != nil && err.Error() != utils.ErrNoRow() {
  230. br.Msg = "获取失败"
  231. br.ErrMsg = "获取失败,Err:" + err.Error()
  232. return
  233. }
  234. //列表页数据
  235. list, err = cygx.GetCompanyContractListJoinCompany(condition, pars, startSize, pageSize)
  236. if err != nil {
  237. br.Msg = "获取失败"
  238. br.ErrMsg = "获取失败,Err:" + err.Error()
  239. return
  240. }
  241. //mapCompamy := make(map[int]string)
  242. listLen := len(list)
  243. if listLen == 0 {
  244. list = make([]*cygx.CompanyContractResp, 0)
  245. } else {
  246. var contractCodes []string
  247. var companyContractIds []int
  248. for _, v := range list {
  249. switch v.Source {
  250. case "系统合同":
  251. v.FormalType = "标准"
  252. case "上传附件":
  253. v.FormalType = "非标"
  254. }
  255. contractCodes = append(contractCodes, v.ContractCode)
  256. companyContractIds = append(companyContractIds, v.CompanyContractId)
  257. //mapCompamy[v.CompanyId] = strconv.Itoa(v.CompanyContractId)
  258. }
  259. lencontractCodes := len(contractCodes)
  260. if lencontractCodes > 0 {
  261. //获取标准合同的ID,这里上面的查询已经关联了三张表,拆分吧。。。
  262. condition = ""
  263. joinStr := ""
  264. pars = make([]interface{}, 0)
  265. condition = " AND a.contract_code IN (" + utils.GetOrmInReplace(lencontractCodes) + ") "
  266. pars = append(pars, contractCodes)
  267. listContract, err := contract.GetContractList(condition, joinStr, pars, 0, lencontractCodes)
  268. if err != nil {
  269. br.Msg = "获取合同列表失败!"
  270. br.ErrMsg = "获取合同列表失败,Err:" + err.Error()
  271. return
  272. }
  273. mapContractCode := make(map[string]int)
  274. for _, v := range listContract {
  275. mapContractCode[v.ContractCode] = v.ContractId
  276. }
  277. mapIsGray, err := cygxService.GetMapIsGrayByCompanyContractIds(companyContractIds)
  278. if err != nil {
  279. br.Msg = "获取合同列表失败!"
  280. br.ErrMsg = "获取合同列表失败,GetMapIsGrayByCompanyContractIds Err:" + err.Error()
  281. return
  282. }
  283. //合并合同所对应的权限
  284. mappermissionName, err := cygxService.GetCompanyContractPermissionNameMapById(companyContractIds)
  285. if err != nil {
  286. br.Msg = "获取失败"
  287. br.ErrMsg = "获取失败,Err:" + err.Error()
  288. return
  289. }
  290. for _, v := range list {
  291. v.ContractId = mapContractCode[v.ContractCode]
  292. v.MoneyPoint = mapMoneyPoint[v.CompanyContractId]
  293. v.PermissionName = mappermissionName[v.CompanyContractId]
  294. v.IsGray = mapIsGray[v.CompanyContractId]
  295. if v.ContractType == "打分派点" {
  296. quarter, _ := utils.GetQuarterStrStartDatesInRange(v.StartDate, v.EndDate)
  297. v.Quarter = strings.Replace(quarter, ",", "+", -1)
  298. }
  299. }
  300. }
  301. }
  302. page := paging.GetPaging(currentIndex, pageSize, total)
  303. resp := cygx.CompanyContractListResp{
  304. Paging: page,
  305. List: list,
  306. }
  307. //导出excel
  308. if isExport {
  309. CompanyContractListExport(this, resp, br)
  310. return
  311. }
  312. br.Ret = 200
  313. br.Success = true
  314. br.Msg = "获取成功"
  315. br.Data = resp
  316. }
  317. // CompanyContractListExport 导出Excel
  318. func CompanyContractListExport(this *ContractAllocationController, resp cygx.CompanyContractListResp, br *models.BaseResponse) {
  319. dir, err := os.Executable()
  320. exPath := filepath.Dir(dir)
  321. downLoadnFilePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  322. xlsxFile := xlsx.NewFile()
  323. if err != nil {
  324. br.Msg = "生成文件失败"
  325. br.ErrMsg = "生成文件失败"
  326. return
  327. }
  328. style := xlsx.NewStyle()
  329. alignment := xlsx.Alignment{
  330. Horizontal: "center",
  331. Vertical: "center",
  332. WrapText: true,
  333. }
  334. style.Alignment = alignment
  335. style.ApplyAlignment = true
  336. sheel, err := xlsxFile.AddSheet("派点导出数据")
  337. if err != nil {
  338. br.Msg = "新增Sheet失败"
  339. br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
  340. return
  341. }
  342. sheel.SetColWidth(0, 0, 30)
  343. sheel.SetColWidth(1, 1, 15)
  344. sheel.SetColWidth(2, 2, 15)
  345. sheel.SetColWidth(3, 3, 18)
  346. titleRow := sheel.AddRow()
  347. cellA := titleRow.AddCell()
  348. cellA.SetStyle(style)
  349. cellA.SetValue("合同编号")
  350. cellB := titleRow.AddCell()
  351. cellB.SetStyle(style)
  352. cellB.SetValue("转正类型")
  353. cellC := titleRow.AddCell()
  354. cellC.SetStyle(style)
  355. cellC.SetValue("合同类型")
  356. cellD := titleRow.AddCell()
  357. cellD.SetStyle(style)
  358. cellD.SetValue("公司名称")
  359. cellE := titleRow.AddCell()
  360. cellE.SetStyle(style)
  361. cellE.SetValue("所属销售")
  362. cellF := titleRow.AddCell()
  363. cellF.SetStyle(style)
  364. cellF.SetValue("合同金额")
  365. cellG := titleRow.AddCell()
  366. cellG.SetStyle(style)
  367. cellG.SetValue("合同期限")
  368. cellH := titleRow.AddCell()
  369. cellH.SetStyle(style)
  370. cellH.SetValue("签约套餐")
  371. cellI := titleRow.AddCell()
  372. cellI.SetStyle(style)
  373. cellI.SetValue("状态")
  374. for _, v := range resp.List {
  375. dataRow := sheel.AddRow()
  376. dataRow.SetHeight(20)
  377. cellA := dataRow.AddCell()
  378. cellA.SetStyle(style)
  379. cellA.SetValue(v.ContractCode)
  380. cellB := dataRow.AddCell()
  381. cellB.SetStyle(style)
  382. cellB.SetValue(v.FormalType)
  383. cellC := dataRow.AddCell()
  384. cellC.SetStyle(style)
  385. cellC.SetValue(v.ContractType)
  386. cellD := dataRow.AddCell()
  387. cellD.SetStyle(style)
  388. cellD.SetValue(v.CompanyName)
  389. cellE := dataRow.AddCell()
  390. cellE.SetStyle(style)
  391. cellE.SetValue(v.SellerName)
  392. cellF := dataRow.AddCell()
  393. cellF.SetStyle(style)
  394. cellF.SetValue(v.Money)
  395. cellG := dataRow.AddCell()
  396. cellG.SetStyle(style)
  397. cellG.SetValue(fmt.Sprint(v.StartDate, " ~ ", v.EndDate))
  398. cellH := dataRow.AddCell()
  399. cellH.SetStyle(style)
  400. cellH.SetValue(v.PermissionName)
  401. cellI := dataRow.AddCell()
  402. cellI.SetStyle(style)
  403. if v.IsAllocation == 1 {
  404. cellI.SetValue("已派点")
  405. } else {
  406. cellI.SetValue("未派点")
  407. }
  408. }
  409. err = xlsxFile.Save(downLoadnFilePath)
  410. if err != nil {
  411. br.Msg = "保存文件失败"
  412. br.ErrMsg = "保存文件失败"
  413. return
  414. }
  415. randStr := time.Now().Format(utils.FormatDateTimeUnSpace)
  416. downloadFileName := "派点导出数据_" + randStr + ".xlsx"
  417. this.Ctx.Output.Download(downLoadnFilePath, downloadFileName)
  418. defer func() {
  419. os.Remove(downLoadnFilePath)
  420. }()
  421. br.Ret = 200
  422. br.Success = true
  423. br.Msg = "导出成功"
  424. }
  425. // @Title 详情
  426. // @Description 获取详情接口
  427. // @Param CompanyContractId query int true "ID"
  428. // @Param ShowDetail query bool false "是否是派点详情展示"
  429. // @Success Ret=200 {object} cygx.CygxAllocationCompanyContractDetailResp
  430. // @router /allocation/detail [get]
  431. func (this *ContractAllocationController) CompanyContracDetail() {
  432. br := new(models.BaseResponse).Init()
  433. defer func() {
  434. this.Data["json"] = br
  435. this.ServeJSON()
  436. }()
  437. AdminUser := this.SysUser
  438. if AdminUser == nil {
  439. br.Msg = "请登录"
  440. br.ErrMsg = "请登录,用户信息为空"
  441. br.Ret = 408
  442. return
  443. }
  444. resp := new(cygx.CygxAllocationCompanyContractDetailResp)
  445. companyContractId, _ := this.GetInt("CompanyContractId")
  446. if companyContractId < 1 {
  447. br.Msg = "请输入详情ID"
  448. return
  449. }
  450. showDetail, _ := this.GetBool("ShowDetail", false)
  451. contractItem, err := company.GetCompanyContractById(companyContractId)
  452. if err != nil {
  453. br.Msg = "获取信息失败"
  454. br.ErrMsg = "获取合同信息失败,Err:" + err.Error()
  455. return
  456. }
  457. total, err := cygx.GetCygxAllocationCompanyContractCountByCompanyContractId(companyContractId)
  458. if err != nil {
  459. br.Msg = "获取信息失败"
  460. br.ErrMsg = "获取合同信息失败,GetCygxAllocationCompanyContractCountByCompanyContractId Err:" + err.Error()
  461. return
  462. }
  463. companyProduct, err := company.GetCompanyProductByCompanyIdAndProductId(contractItem.CompanyId, utils.COMPANY_PRODUCT_RAI_ID)
  464. if err != nil {
  465. br.Msg = "查询客户产品信息失败"
  466. br.ErrMsg = "查询客户产品信息失败,Err:" + err.Error()
  467. return
  468. }
  469. //判断客户是否是永续(X类试用客户)
  470. if companyProduct.Status == utils.COMPANY_STATUS_FOREVER {
  471. resp.IsXClass = true
  472. }
  473. //var contractPermissionList []*company.ContractPermissionList
  474. //expMap := map[int]string{0: "(3w)", 1: "(5w)", 2: "(10w)"} // 买方研选价格
  475. hasPermissions, e := company.GetCompanyContractPermissionByCompanyContractId(companyContractId)
  476. if e != nil {
  477. br.Msg = "获取失败"
  478. br.ErrMsg = "获取合同权限信息失败,Err:" + e.Error()
  479. return
  480. }
  481. hasMap := make(map[int]*company.CompanyContractPermission)
  482. var pointMoney float64
  483. for _, p := range hasPermissions {
  484. hasMap[p.ChartPermissionId] = p
  485. if p.ChartPermissionId == utils.YAN_XUAN_KOU_DIAN_BAO_ID {
  486. pointMoney = math.Round(0.2*p.Points*100) / 100 // 将给定数字舍入到最近的整数,处理精度问题
  487. }
  488. }
  489. raiPermissions, e := company.GetPermissionLookItemsExt("2", utils.COMPANY_PRODUCT_RAI_NAME)
  490. if e != nil {
  491. br.Msg = "获取失败"
  492. br.ErrMsg = "获取权益权限列表失败, Err: " + e.Error()
  493. return
  494. }
  495. mapPermissionNameHave := make(map[string]bool) // 判断合同是否存在某一行业权限种类
  496. var expensiveYx int
  497. for _, n := range raiPermissions {
  498. match := hasMap[n.ChartPermissionId]
  499. if match == nil {
  500. continue
  501. }
  502. //研选订阅、研选扣点包 映射成买方研选
  503. if n.PermissionName == utils.CHART_PERMISSION_NAME_MF_YANXUAN || n.PermissionName == utils.YAN_XUAN_KOU_DIAN_BAO_NAME {
  504. n.PermissionName = utils.CHART_PERMISSION_NAME_MAI_FANG_YANXUAN
  505. }
  506. mapPermissionNameHave[n.PermissionName] = true
  507. // 买方研选(3w/5w/10w)
  508. if n.PermissionName == utils.CHART_PERMISSION_NAME_MF_YANXUAN {
  509. expensiveYx = match.ExpensiveYx
  510. //n.PermissionName += expMap[match.ExpensiveYx]
  511. continue
  512. }
  513. }
  514. //fmt.Println(mapPermissionNameHave)
  515. resp.Money = contractItem.Money / 10000
  516. //有研选时,对研选套餐类型做文案处理
  517. respItemYx := new(cygx.AllocationPermissionListResp) // 研选订阅
  518. respItemYxKouDian := new(cygx.AllocationPermissionListResp) // 研选扣点
  519. if mapPermissionNameHave[utils.CHART_PERMISSION_NAME_MF_YANXUAN] || mapPermissionNameHave[utils.YAN_XUAN_KOU_DIAN_BAO_NAME] {
  520. var moneyYx float64
  521. if mapPermissionNameHave[utils.CHART_PERMISSION_NAME_MF_YANXUAN] {
  522. if expensiveYx == 0 {
  523. moneyYx = 3
  524. } else if expensiveYx == 1 {
  525. moneyYx = 5
  526. } else if expensiveYx == 2 {
  527. moneyYx = 10
  528. }
  529. respItemYx.Proportion = 0
  530. respItemYx.Money = moneyYx
  531. respItemYx.ChartPermissionName = utils.CHART_PERMISSION_NAME_MF_YANXUAN
  532. respItemYx.ChartPermissionId = utils.CHART_PERMISSION_ID_YANXUAN
  533. //手动拼接研选订阅的数据类型
  534. listYxItem := new(cygx.AllocationRealNameListResp)
  535. listYxItem.RealName = utils.CHART_PERMISSION_NAME_MF_YANXUAN
  536. listYxItem.Money = moneyYx
  537. listYxItem.ChartPermissionId = utils.CHART_PERMISSION_ID_YANXUAN
  538. respItemYx.List = append(respItemYx.List, listYxItem)
  539. }
  540. if mapPermissionNameHave[utils.YAN_XUAN_KOU_DIAN_BAO_NAME] {
  541. respItemYxKouDian.Proportion = 0
  542. respItemYxKouDian.Money = pointMoney
  543. respItemYxKouDian.ChartPermissionName = utils.YAN_XUAN_KOU_DIAN_BAO_NAME
  544. respItemYxKouDian.ChartPermissionId = utils.YAN_XUAN_KOU_DIAN_BAO_ID
  545. listKouDianItem := new(cygx.AllocationRealNameListResp)
  546. listKouDianItem.RealName = utils.YAN_XUAN_KOU_DIAN_BAO_NAME
  547. listKouDianItem.Money = pointMoney
  548. listKouDianItem.ChartPermissionId = utils.YAN_XUAN_KOU_DIAN_BAO_ID
  549. respItemYxKouDian.List = append(respItemYxKouDian.List, listKouDianItem)
  550. }
  551. resp.TotalPointsContent = fmt.Sprint(resp.Money, "W,", "其中", moneyYx+pointMoney, "w默认归属买方研选,请对剩余", resp.Money-moneyYx-pointMoney, "w按照100%进行比值分配")
  552. if showDetail {
  553. resp.TotalPointsContent = fmt.Sprint(resp.Money, "W")
  554. }
  555. resp.Money = resp.Money - moneyYx
  556. } else {
  557. resp.TotalPointsContent = fmt.Sprint(resp.Money, "W")
  558. }
  559. sysUserList, err := cygx.GetAskEmailList()
  560. if err != nil {
  561. br.Msg = "获取失败"
  562. br.ErrMsg = "获取失败,GetAskEmailList Err: " + err.Error()
  563. return
  564. }
  565. mapPermissionUser := make(map[string][]*cygx.AllocationRealNameListResp)
  566. listPermission, err := cygx.GetChartPermissionAll()
  567. if err != nil {
  568. br.Msg = "获取信息失败"
  569. br.ErrMsg = "获取品种信息失败,Err:" + err.Error()
  570. return
  571. }
  572. mapPermissionId := make(map[string]int)
  573. for _, v := range listPermission {
  574. mapPermissionId[v.PermissionName] = v.ChartPermissionId
  575. }
  576. var respList []*cygx.AllocationPermissionListResp
  577. if total == 0 {
  578. for _, v := range sysUserList {
  579. if !mapPermissionNameHave[v.ChartPermissionName] {
  580. continue
  581. }
  582. item := new(cygx.AllocationRealNameListResp)
  583. item.RealName = v.Name
  584. item.ChartPermissionId = mapPermissionId[v.ChartPermissionName]
  585. mapPermissionUser[v.ChartPermissionName] = append(mapPermissionUser[v.ChartPermissionName], item)
  586. }
  587. for k, v := range mapPermissionUser {
  588. respItem := new(cygx.AllocationPermissionListResp)
  589. respItem.ChartPermissionName = k
  590. respItem.ChartPermissionId = mapPermissionId[k]
  591. respItem.List = v
  592. respList = append(respList, respItem)
  593. }
  594. ////研选订阅
  595. //if respItemYx.ChartPermissionName != "" {
  596. // respList = append(respList, respItemYx)
  597. //}
  598. ////研选扣点
  599. //if respItemYxKouDian.ChartPermissionName != "" {
  600. // respList = append(respList, respItemYxKouDian)
  601. //}
  602. } else {
  603. listUser, err := cygx.GetCygxAllocationCompanyContractListById(companyContractId)
  604. if err != nil {
  605. br.Msg = "获取失败"
  606. br.ErrMsg = "获取失败,GetCygxAllocationCompanyContractListById Err: " + err.Error()
  607. return
  608. }
  609. for _, v := range listUser {
  610. if showDetail && v.Money == 0 {
  611. continue
  612. }
  613. item := new(cygx.AllocationRealNameListResp)
  614. item.RealName = v.RealName
  615. item.Money = v.Money
  616. item.Proportion = v.Proportion
  617. item.ChartPermissionId = mapPermissionId[v.ChartPermissionName]
  618. mapPermissionUser[v.ChartPermissionName] = append(mapPermissionUser[v.ChartPermissionName], item)
  619. }
  620. listPermissionContract, err := cygx.GetCygxAllocationCompanyContractPermissionListById(companyContractId)
  621. if err != nil {
  622. br.Msg = "获取失败"
  623. br.ErrMsg = "获取失败,GetCygxAllocationCompanyContractPermissionListById Err: " + err.Error()
  624. return
  625. }
  626. for _, v := range listPermissionContract {
  627. if showDetail && v.Money == 0 {
  628. continue
  629. }
  630. respItem := new(cygx.AllocationPermissionListResp)
  631. respItem.ChartPermissionName = v.ChartPermissionName
  632. respItem.ChartPermissionId = mapPermissionId[v.ChartPermissionName]
  633. respItem.Money = v.Money
  634. respItem.Proportion = v.Proportion
  635. respItem.List = mapPermissionUser[v.ChartPermissionName]
  636. respList = append(respList, respItem)
  637. }
  638. }
  639. //处理是否置灰
  640. mapIsGray, err := cygxService.GetMapIsGrayByCompanyContractIds([]int{companyContractId})
  641. if err != nil {
  642. br.Msg = "获取合同列表失败!"
  643. br.ErrMsg = "获取合同列表失败,GetMapIsGrayByCompanyContractIds Err:" + err.Error()
  644. return
  645. }
  646. permissionNameArr := []string{"医药", "消费", "科技", "智造", "策略", "固收", utils.CHART_PERMISSION_NAME_MAI_FANG_YANXUAN}
  647. for _, v := range permissionNameArr {
  648. for _, v2 := range respList {
  649. if v2.ChartPermissionName == v {
  650. resp.List = append(resp.List, v2)
  651. }
  652. }
  653. }
  654. resp.IsGray = mapIsGray[companyContractId]
  655. //resp.List = respList
  656. resp.CompanyContractId = companyContractId
  657. resp.AllocationType = contractItem.AllocationType
  658. br.Ret = 200
  659. br.Success = true
  660. br.Msg = "获取成功"
  661. br.Data = resp
  662. }
  663. // @Title 更新派点
  664. // @Description 更新派点接口
  665. // @Param request body cygx.AddProductInteriorReq true "type json string"
  666. // @Success 200 {object} "保存成功"
  667. // @router /allocation/update [post]
  668. func (this *ContractAllocationController) CompanyContracUpdate() {
  669. br := new(models.BaseResponse).Init()
  670. defer func() {
  671. this.Data["json"] = br
  672. this.ServeJSON()
  673. }()
  674. sysUser := this.SysUser
  675. if sysUser == nil {
  676. br.Msg = "请登录"
  677. br.ErrMsg = "请登录,SysUser Is Empty"
  678. br.Ret = 408
  679. return
  680. }
  681. var req cygx.UpdateAllocationCompanyContractReq
  682. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  683. if err != nil {
  684. br.Msg = "参数解析异常!"
  685. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  686. return
  687. }
  688. companyContractId := req.CompanyContractId
  689. if companyContractId == 0 {
  690. br.Msg = "参数错误"
  691. br.ErrMsg = "参数错误,id不可为空"
  692. return
  693. }
  694. mapIsGray, err := cygxService.GetMapIsGrayByCompanyContractIds([]int{companyContractId})
  695. if err != nil {
  696. br.Msg = "获取合同列表失败!"
  697. br.ErrMsg = "获取合同列表失败,GetMapIsGrayByCompanyContractIds Err:" + err.Error()
  698. return
  699. }
  700. if mapIsGray[companyContractId] {
  701. br.Msg = "超过180天,无法修改!"
  702. br.ErrMsg = "超过180天,无法修改,companyContractId :" + strconv.Itoa(companyContractId)
  703. return
  704. }
  705. contractItem, err := company.GetCompanyContractById(companyContractId)
  706. if err != nil {
  707. br.Msg = "获取信息失败"
  708. br.ErrMsg = "获取合同信息失败,Err:" + err.Error()
  709. return
  710. }
  711. companyProduct, err := company.GetCompanyProductByCompanyIdAndProductId(contractItem.CompanyId, utils.COMPANY_PRODUCT_RAI_ID)
  712. if err != nil {
  713. br.Msg = "查询客户产品信息失败"
  714. br.ErrMsg = "查询客户产品信息失败,Err:" + err.Error()
  715. return
  716. }
  717. money := contractItem.Money / 10000 // 合同金额,万为单位
  718. var moneyAvg float64 // 行业所占合同的平均金额
  719. hasPermissions, e := company.GetCompanyContractPermissionByCompanyContractId(companyContractId)
  720. if e != nil {
  721. br.Msg = "获取失败"
  722. br.ErrMsg = "获取合同权限信息失败,Err:" + e.Error()
  723. return
  724. }
  725. hasMap := make(map[int]*company.CompanyContractPermission)
  726. for _, p := range hasPermissions {
  727. hasMap[p.ChartPermissionId] = p
  728. }
  729. raiPermissions, e := company.GetPermissionLookItemsExt("2", utils.COMPANY_PRODUCT_RAI_NAME)
  730. if e != nil {
  731. br.Msg = "获取失败"
  732. br.ErrMsg = "获取权益权限列表失败, Err: " + e.Error()
  733. return
  734. }
  735. mapPermissionNameHave := make(map[string]bool) // 判断合同是否存在某一行业权限种类
  736. for _, n := range raiPermissions {
  737. //只计算,医药、消费、科技、智造、策略、固收、买方研选的
  738. if n.PermissionName != utils.YI_YAO_NAME && n.PermissionName != utils.XIAO_FEI_NAME && n.PermissionName != utils.KE_JI_NAME && n.PermissionName != utils.ZHI_ZAO_NAME && n.PermissionName != utils.CE_LUE_NAME && n.PermissionName != utils.GU_SHOU_NAME && n.PermissionName != utils.CHART_PERMISSION_NAME_MAI_FANG_YANXUAN {
  739. continue
  740. }
  741. match := hasMap[n.ChartPermissionId]
  742. if match == nil {
  743. continue
  744. }
  745. // 买方研选(3w/5w/10W)
  746. //if n.PermissionName == utils.CHART_PERMISSION_NAME_MF_YANXUAN {
  747. // if match.ExpensiveYx == 1 {
  748. // money = money - 5
  749. // } else if match.ExpensiveYx == 2 {
  750. // money = money - 10
  751. // } else {
  752. // money = money - 3
  753. // }
  754. //} else {
  755. // mapPermissionNameHave[n.PermissionName] = true
  756. //}
  757. mapPermissionNameHave[n.PermissionName] = true
  758. }
  759. if len(mapPermissionNameHave) > 0 {
  760. moneyAvg = money / float64(len(mapPermissionNameHave))
  761. }
  762. list := req.List
  763. var items []*cygx.CygxAllocationCompanyContract
  764. var itemsPermission []*cygx.CygxAllocationCompanyContractPermission
  765. var proportionSum float64 // 校验前端传过来的占比使用
  766. for _, v := range list {
  767. itemPermission := new(cygx.CygxAllocationCompanyContractPermission)
  768. itemPermission.CompanyContractId = companyContractId
  769. itemPermission.AdminId = sysUser.AdminId
  770. itemPermission.AdminName = sysUser.RealName
  771. itemPermission.Proportion = v.Proportion
  772. itemPermission.Money = v.Money
  773. if v.ChartPermissionName != utils.YAN_XUAN_KOU_DIAN_BAO_NAME {
  774. itemPermission.MoneyAvg = moneyAvg
  775. if v.Money < moneyAvg/2 && companyProduct.Status == utils.COMPANY_STATUS_FOREVER { //永续客户不做这种限制
  776. br.Msg = "单行业占比值不得低于平均值的一半"
  777. br.ErrMsg = "单行业占比值不得低于平均值的一半,Err:" + fmt.Sprint(proportionSum)
  778. return
  779. }
  780. }
  781. itemPermission.ChartPermissionName = v.ChartPermissionName
  782. itemPermission.CreateTime = time.Now()
  783. itemPermission.ModifyTime = time.Now()
  784. itemsPermission = append(itemsPermission, itemPermission)
  785. var userProportionSum float64 // 校验前端传过来的占比使用
  786. for _, v2 := range v.List {
  787. if v2.Proportion < -20 && companyProduct.Status == utils.COMPANY_STATUS_FOREVER { //永续客户不做这种限制
  788. br.Msg = "研究员占比值不得小于总额的-20%"
  789. br.ErrMsg = "研究员占比值不得小于总额的20%,Err:" + fmt.Sprint(proportionSum)
  790. return
  791. }
  792. item := new(cygx.CygxAllocationCompanyContract)
  793. item.CompanyContractId = companyContractId
  794. item.AdminId = sysUser.AdminId
  795. item.AdminName = sysUser.RealName
  796. item.Proportion = v2.Proportion
  797. item.Money = v2.Money
  798. item.RealName = v2.RealName
  799. item.ChartPermissionName = v.ChartPermissionName
  800. item.CreateTime = time.Now()
  801. item.ModifyTime = time.Now()
  802. items = append(items, item)
  803. proportionSum += v2.Proportion
  804. userProportionSum += v2.Proportion
  805. }
  806. //先添加0.2%的精度校验
  807. if userProportionSum > (v.Proportion+0.2) || userProportionSum < (v.Proportion-0.2) {
  808. br.Msg = "单行业下的研究员比值之和不等于行业占比值"
  809. br.ErrMsg = "单行业下的研究员比值之和不等于行业占比值"
  810. return
  811. }
  812. }
  813. //先添加0.5%的精度校验
  814. if proportionSum > 100.5 || proportionSum < 99.5 {
  815. br.Msg = "行业总比值相加不等于100%"
  816. br.ErrMsg = "行业总比值相加不等于100%,Err:"
  817. return
  818. }
  819. err = cygx.AddAndUpdateCygxAllocationCompanyContract(items, itemsPermission, companyContractId)
  820. if err != nil {
  821. br.Msg = "操作失败"
  822. br.ErrMsg = "操作失败,AddAndUpdateCygxAllocationCompanyContract Err:" + err.Error()
  823. return
  824. }
  825. br.Ret = 200
  826. br.Success = true
  827. br.IsAddLog = true
  828. br.Msg = "操作成功"
  829. }
  830. // @Title 研究员派点统计
  831. // @Description 研究员派点统计接口
  832. // @Param PageSize query int true "每页数据条数"
  833. // @Param CurrentIndex query int true "当前页页码,从1开始"
  834. // @Param Keyword query string true "客户名称"
  835. // @Param StartDate query string false "开始日期"
  836. // @Param EndDate query string false "结束日期"
  837. // @Param IsExport query bool false "是否导出excel,默认是false"
  838. // @Success 200 {object} cygx.CompanyContractListResp
  839. // @router /allocation/statistics [get]
  840. func (this *ContractAllocationController) CompanyContractStatistics() {
  841. br := new(models.BaseResponse).Init()
  842. defer func() {
  843. this.Data["json"] = br
  844. this.ServeJSON()
  845. }()
  846. sysUser := this.SysUser
  847. if sysUser == nil {
  848. br.Msg = "请登录"
  849. br.ErrMsg = "请登录,SysUser Is Empty"
  850. br.Ret = 408
  851. return
  852. }
  853. keyword := this.GetString("Keyword")
  854. startDate := this.GetString("StartDate")
  855. endDate := this.GetString("EndDate")
  856. if startDate == "" {
  857. startDate = "2015-01-01"
  858. }
  859. //是否导出报表
  860. isExport, _ := this.GetBool("IsExport")
  861. var condition string
  862. var pars []interface{}
  863. if endDate != "" {
  864. condition += ` AND a.start_date >= ? AND a.start_date <= ? `
  865. pars = append(pars, startDate+" 00:00:01", endDate+" 23:59:59")
  866. }
  867. //根据当前角色来获取查询条件
  868. //condition, pars = getQueryParams(condition, pars, sysUser, "c.")
  869. companyIds, err := cygxService.GetAdminLookUserCompanyIdsBySelf(sysUser)
  870. if err != nil {
  871. br.Msg = "获取失败"
  872. br.ErrMsg = "获取失败,GetAdminLookUserCompanyIds Err:" + err.Error()
  873. return
  874. }
  875. lencompanyIds := len(companyIds)
  876. if lencompanyIds > 0 {
  877. condition += ` AND c.company_id IN (` + utils.GetOrmInReplace(lencompanyIds) + `)`
  878. pars = append(pars, companyIds)
  879. }
  880. //关键字搜索
  881. if keyword != "" {
  882. condition += ` and b.company_name like "%` + keyword + `%" `
  883. }
  884. //默认只查询权益 2023-06-01 之后的合同
  885. condition += ` AND c.product_id = ? AND a.start_date > ? `
  886. pars = append(pars, 2, "2023-05-31")
  887. //列表页数据
  888. listContract, err := cygx.GetCompanyContractListJoinCompany(condition, pars, 0, 1000)
  889. if err != nil {
  890. br.Msg = "获取失败"
  891. br.ErrMsg = "获取失败,Err:" + err.Error()
  892. return
  893. }
  894. //return
  895. var companyContractIds []int
  896. companyContractIdBool := make(map[int]bool)
  897. for _, v := range listContract {
  898. companyContractIds = append(companyContractIds, v.CompanyContractId)
  899. }
  900. lenArr := len(companyContractIds)
  901. mapUserAllocation := make(map[string]float64) // 关联合同
  902. mapUserMoney := make(map[string]float64) // 派点金额
  903. mapPermissionAllocation := make(map[string]float64)
  904. mapPermissionMoney := make(map[string]float64)
  905. mapPermissionMoneyAvg := make(map[string]float64)
  906. mapPermissionContract := make(map[string]float64) // 行业关联的合同数量
  907. mapPermissionContractKey := make(map[string]bool) // 行业关联的合同数量键值对
  908. //totalContract := lenArr //所有的关联合同
  909. var totalMoney float64 //所有的关联合同的金额 单位万
  910. if lenArr > 0 {
  911. var conditionAllocation string
  912. var parsAllocation []interface{}
  913. conditionAllocation = ` AND company_contract_id IN(` + utils.GetOrmInReplace(lenArr) + ` ) `
  914. parsAllocation = append(parsAllocation, companyContractIds)
  915. allocationCompanyContractList, err := cygx.GetCygxAllocationCompanyContractList(conditionAllocation, parsAllocation)
  916. if err != nil {
  917. br.Msg = "获取失败"
  918. br.ErrMsg = "获取失败,Err:" + err.Error()
  919. return
  920. }
  921. allocationCompanyContractPermissionList, err := cygx.GetCygxAllocationCompanyContractPermissionList(conditionAllocation, parsAllocation)
  922. if err != nil {
  923. br.Msg = "获取失败"
  924. br.ErrMsg = "获取失败,Err:" + err.Error()
  925. return
  926. }
  927. for _, v := range allocationCompanyContractList {
  928. //if v.Proportion != 0 && v.ChartPermissionName != utils.CHART_PERMISSION_NAME_MF_YANXUAN {
  929. mapUserAllocation[v.RealName] += 1
  930. mapUserMoney[v.RealName] += v.Money
  931. mapPermissionAllocation[v.ChartPermissionName] += 1
  932. mapPermissionMoney[v.ChartPermissionName] += v.Money
  933. totalMoney += v.Money
  934. companyContractIdBool[v.CompanyContractId] = true
  935. //统计单个行业所关的合同数量
  936. if !mapPermissionContractKey[fmt.Sprint("ChartPermissionName_", v.ChartPermissionName, "CompanyContractId_", v.CompanyContractId)] {
  937. mapPermissionContract[v.ChartPermissionName] += 1
  938. mapPermissionContractKey[fmt.Sprint("ChartPermissionName_", v.ChartPermissionName, "CompanyContractId_", v.CompanyContractId)] = true
  939. }
  940. //dd = decimal.NewFromFloat(v.Money).Add(dd)
  941. //}
  942. }
  943. for _, v := range allocationCompanyContractPermissionList {
  944. mapPermissionMoneyAvg[v.ChartPermissionName] += v.MoneyAvg
  945. }
  946. }
  947. sysUserList, err := cygx.GetAskEmailList()
  948. if err != nil {
  949. br.Msg = "获取失败"
  950. br.ErrMsg = "获取失败,GetAskEmailList Err: " + err.Error()
  951. return
  952. }
  953. mapPermissionUser := make(map[string][]*cygx.AllocationRealNameStatisticsListResp) //行业分组 map
  954. for _, v := range sysUserList {
  955. item := new(cygx.AllocationRealNameStatisticsListResp)
  956. item.RealName = v.Name
  957. item.TotalRelatedContract = mapUserAllocation[v.Name]
  958. item.TotalDispatchPoint = utils.SubFloatToString(mapUserMoney[v.Name], 2)
  959. if item.TotalDispatchPoint == "" {
  960. item.TotalDispatchPoint = "0"
  961. }
  962. //组内占比
  963. if mapUserMoney[v.Name] == 0 {
  964. item.GroupProportion = ""
  965. } else {
  966. item.GroupProportion = utils.SubFloatToString(mapUserMoney[v.Name]/mapPermissionMoney[v.ChartPermissionName]*100, 2)
  967. }
  968. if item.GroupProportion == "" {
  969. item.GroupProportion = "0"
  970. }
  971. item.GroupProportion += "%"
  972. //部门占比
  973. if totalMoney == 0 {
  974. item.DepartmentProportion = ""
  975. } else {
  976. item.DepartmentProportion = utils.SubFloatToString(mapUserMoney[v.Name]/totalMoney*100, 2)
  977. }
  978. if item.DepartmentProportion == "" {
  979. item.DepartmentProportion = "0"
  980. }
  981. item.DepartmentProportion += "%"
  982. mapPermissionUser[v.ChartPermissionName] = append(mapPermissionUser[v.ChartPermissionName], item)
  983. }
  984. for k, v := range mapPermissionUser {
  985. lenUser := len(v)
  986. item := new(cygx.AllocationRealNameStatisticsListResp)
  987. item.RealName = "合计"
  988. item.TotalRelatedContract = mapPermissionContract[k]
  989. item.TotalDispatchPoint = fmt.Sprint(utils.SubFloatToString(mapPermissionMoney[k], 2), "/", utils.SubFloatToString(mapPermissionMoneyAvg[k], 2))
  990. item.GroupProportion = "100%"
  991. //部门占比
  992. if totalMoney == 0 {
  993. item.DepartmentProportion = ""
  994. } else {
  995. item.DepartmentProportion = utils.SubFloatToString(mapPermissionMoney[k]/totalMoney*100, 2)
  996. }
  997. if item.DepartmentProportion == "" {
  998. item.DepartmentProportion = "0"
  999. }
  1000. item.DepartmentProportion += "%"
  1001. mapPermissionUser[k] = append(mapPermissionUser[k], item)
  1002. item = new(cygx.AllocationRealNameStatisticsListResp)
  1003. item.RealName = "平均"
  1004. item.TotalRelatedContract = utils.SubFloatToFloat(mapPermissionAllocation[k]/float64(lenUser), 2)
  1005. item.TotalDispatchPoint = utils.SubFloatToString(mapPermissionMoney[k]/float64(lenUser), 2)
  1006. //组内占比
  1007. if mapPermissionMoney[k] == 0 {
  1008. item.GroupProportion = ""
  1009. } else {
  1010. item.GroupProportion = utils.SubFloatToString(1/(float64(len(mapPermissionUser[k])-1))*100, 2)
  1011. }
  1012. if item.GroupProportion == "" {
  1013. item.GroupProportion = "0"
  1014. }
  1015. item.GroupProportion += "%"
  1016. // 部门占比
  1017. if totalMoney == 0 {
  1018. item.DepartmentProportion = ""
  1019. } else {
  1020. item.DepartmentProportion = utils.SubFloatToString(mapPermissionMoney[k]/totalMoney*100/float64(len(mapPermissionUser[k])-1), 2)
  1021. }
  1022. if item.DepartmentProportion == "" {
  1023. item.DepartmentProportion = "0"
  1024. }
  1025. item.DepartmentProportion += "%"
  1026. mapPermissionUser[k] = append(mapPermissionUser[k], item)
  1027. }
  1028. var list []*cygx.AllocationPermissionStatisticsListResp
  1029. permissionNameArr := []string{"医药", "消费", "科技", "智造", "策略", "固收", "买方研选"}
  1030. for _, v := range permissionNameArr {
  1031. item := new(cygx.AllocationPermissionStatisticsListResp)
  1032. item.ChartPermissionName = v
  1033. item.List = mapPermissionUser[v]
  1034. list = append(list, item)
  1035. }
  1036. resp := cygx.CygxAllocationCompanyContractDetailStatisticsResp{
  1037. List: list,
  1038. }
  1039. resp.TotalContract = len(companyContractIdBool)
  1040. resp.TotalMoney = utils.SubFloatToFloat(totalMoney, 2)
  1041. //导出excel
  1042. if isExport {
  1043. CompanyContractStatisticsExport(this, resp, br)
  1044. return
  1045. }
  1046. br.Ret = 200
  1047. br.Success = true
  1048. br.Msg = "获取成功"
  1049. br.Data = resp
  1050. }
  1051. // CompanyContractStatisticsExport 导出Excel
  1052. func CompanyContractStatisticsExport(this *ContractAllocationController, resp cygx.CygxAllocationCompanyContractDetailStatisticsResp, br *models.BaseResponse) {
  1053. dir, err := os.Executable()
  1054. exPath := filepath.Dir(dir)
  1055. downLoadnFilePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  1056. xlsxFile := xlsx.NewFile()
  1057. if err != nil {
  1058. br.Msg = "生成文件失败"
  1059. br.ErrMsg = "生成文件失败"
  1060. return
  1061. }
  1062. style := xlsx.NewStyle()
  1063. alignment := xlsx.Alignment{
  1064. Horizontal: "center",
  1065. Vertical: "center",
  1066. WrapText: true,
  1067. }
  1068. style.Alignment = alignment
  1069. style.ApplyAlignment = true
  1070. sheel, err := xlsxFile.AddSheet("研究员派点统计")
  1071. if err != nil {
  1072. br.Msg = "新增Sheet失败"
  1073. br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
  1074. return
  1075. }
  1076. sheel.SetColWidth(0, 0, 30)
  1077. sheel.SetColWidth(1, 1, 15)
  1078. sheel.SetColWidth(2, 2, 15)
  1079. sheel.SetColWidth(3, 3, 18)
  1080. titleRow := sheel.AddRow()
  1081. cellA := titleRow.AddCell()
  1082. cellA.SetStyle(style)
  1083. cellA.SetValue("组别")
  1084. cellB := titleRow.AddCell()
  1085. cellB.SetStyle(style)
  1086. cellB.SetValue("研究员")
  1087. cellC := titleRow.AddCell()
  1088. cellC.SetStyle(style)
  1089. cellC.SetValue("关联合同")
  1090. cellD := titleRow.AddCell()
  1091. cellD.SetStyle(style)
  1092. cellD.SetValue("总派点")
  1093. cellE := titleRow.AddCell()
  1094. cellE.SetStyle(style)
  1095. cellE.SetValue("组内占比")
  1096. cellF := titleRow.AddCell()
  1097. cellF.SetStyle(style)
  1098. cellF.SetValue("部门占比")
  1099. for _, v := range resp.List {
  1100. for k2, v2 := range v.List {
  1101. dataRow := sheel.AddRow()
  1102. dataRow.SetHeight(20)
  1103. cellA := dataRow.AddCell()
  1104. cellA.SetStyle(style)
  1105. cellA.SetValue(v.ChartPermissionName)
  1106. if k2 < len(v.List)-1 {
  1107. cellA.VMerge = 1
  1108. }
  1109. cellB := dataRow.AddCell()
  1110. cellB.SetStyle(style)
  1111. cellB.SetValue(v2.RealName)
  1112. cellC := dataRow.AddCell()
  1113. cellC.SetStyle(style)
  1114. cellC.SetValue(v2.TotalRelatedContract)
  1115. cellD := dataRow.AddCell()
  1116. cellD.SetStyle(style)
  1117. cellD.SetValue(v2.TotalDispatchPoint)
  1118. cellE := dataRow.AddCell()
  1119. cellE.SetStyle(style)
  1120. cellE.SetValue(v2.GroupProportion)
  1121. cellF := dataRow.AddCell()
  1122. cellF.SetStyle(style)
  1123. cellF.SetValue(v2.DepartmentProportion)
  1124. }
  1125. }
  1126. titleRow = sheel.AddRow()
  1127. cellA = titleRow.AddCell()
  1128. cellA.HMerge = 1
  1129. cellA.SetStyle(style)
  1130. cellA.SetValue("部门合计")
  1131. cellB = titleRow.AddCell()
  1132. cellB.SetStyle(style)
  1133. cellB.SetValue("")
  1134. cellC = titleRow.AddCell()
  1135. cellC.SetStyle(style)
  1136. cellC.SetValue(resp.TotalContract)
  1137. cellD = titleRow.AddCell()
  1138. cellD.SetStyle(style)
  1139. cellD.SetValue(resp.TotalMoney)
  1140. cellE = titleRow.AddCell()
  1141. cellE.SetStyle(style)
  1142. cellE.SetValue("-")
  1143. cellF = titleRow.AddCell()
  1144. cellF.SetStyle(style)
  1145. cellF.SetValue("100%")
  1146. err = xlsxFile.Save(downLoadnFilePath)
  1147. if err != nil {
  1148. br.Msg = "保存文件失败"
  1149. br.ErrMsg = "保存文件失败"
  1150. return
  1151. }
  1152. randStr := time.Now().Format(utils.FormatDateTimeUnSpace)
  1153. downloadFileName := "研究员派点统计_" + randStr + ".xlsx"
  1154. this.Ctx.Output.Download(downLoadnFilePath, downloadFileName)
  1155. defer func() {
  1156. os.Remove(downLoadnFilePath)
  1157. }()
  1158. br.Ret = 200
  1159. br.Success = true
  1160. br.Msg = "导出成功"
  1161. }