contract_allocation.go 40 KB

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