contract_allocation.go 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258
  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. }
  296. }
  297. }
  298. page := paging.GetPaging(currentIndex, pageSize, total)
  299. resp := cygx.CompanyContractListResp{
  300. Paging: page,
  301. List: list,
  302. }
  303. //导出excel
  304. if isExport {
  305. CompanyContractListExport(this, resp, br)
  306. return
  307. }
  308. br.Ret = 200
  309. br.Success = true
  310. br.Msg = "获取成功"
  311. br.Data = resp
  312. }
  313. // CompanyContractListExport 导出Excel
  314. func CompanyContractListExport(this *ContractAllocationController, resp cygx.CompanyContractListResp, br *models.BaseResponse) {
  315. dir, err := os.Executable()
  316. exPath := filepath.Dir(dir)
  317. downLoadnFilePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  318. xlsxFile := xlsx.NewFile()
  319. if err != nil {
  320. br.Msg = "生成文件失败"
  321. br.ErrMsg = "生成文件失败"
  322. return
  323. }
  324. style := xlsx.NewStyle()
  325. alignment := xlsx.Alignment{
  326. Horizontal: "center",
  327. Vertical: "center",
  328. WrapText: true,
  329. }
  330. style.Alignment = alignment
  331. style.ApplyAlignment = true
  332. sheel, err := xlsxFile.AddSheet("派点导出数据")
  333. if err != nil {
  334. br.Msg = "新增Sheet失败"
  335. br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
  336. return
  337. }
  338. sheel.SetColWidth(0, 0, 30)
  339. sheel.SetColWidth(1, 1, 15)
  340. sheel.SetColWidth(2, 2, 15)
  341. sheel.SetColWidth(3, 3, 18)
  342. titleRow := sheel.AddRow()
  343. cellA := titleRow.AddCell()
  344. cellA.SetStyle(style)
  345. cellA.SetValue("合同编号")
  346. cellB := titleRow.AddCell()
  347. cellB.SetStyle(style)
  348. cellB.SetValue("转正类型")
  349. cellC := titleRow.AddCell()
  350. cellC.SetStyle(style)
  351. cellC.SetValue("合同类型")
  352. cellD := titleRow.AddCell()
  353. cellD.SetStyle(style)
  354. cellD.SetValue("公司名称")
  355. cellE := titleRow.AddCell()
  356. cellE.SetStyle(style)
  357. cellE.SetValue("所属销售")
  358. cellF := titleRow.AddCell()
  359. cellF.SetStyle(style)
  360. cellF.SetValue("合同金额")
  361. cellG := titleRow.AddCell()
  362. cellG.SetStyle(style)
  363. cellG.SetValue("合同期限")
  364. cellH := titleRow.AddCell()
  365. cellH.SetStyle(style)
  366. cellH.SetValue("签约套餐")
  367. cellI := titleRow.AddCell()
  368. cellI.SetStyle(style)
  369. cellI.SetValue("状态")
  370. for _, v := range resp.List {
  371. dataRow := sheel.AddRow()
  372. dataRow.SetHeight(20)
  373. cellA := dataRow.AddCell()
  374. cellA.SetStyle(style)
  375. cellA.SetValue(v.ContractCode)
  376. cellB := dataRow.AddCell()
  377. cellB.SetStyle(style)
  378. cellB.SetValue(v.FormalType)
  379. cellC := dataRow.AddCell()
  380. cellC.SetStyle(style)
  381. cellC.SetValue(v.ContractType)
  382. cellD := dataRow.AddCell()
  383. cellD.SetStyle(style)
  384. cellD.SetValue(v.CompanyName)
  385. cellE := dataRow.AddCell()
  386. cellE.SetStyle(style)
  387. cellE.SetValue(v.SellerName)
  388. cellF := dataRow.AddCell()
  389. cellF.SetStyle(style)
  390. cellF.SetValue(v.Money)
  391. cellG := dataRow.AddCell()
  392. cellG.SetStyle(style)
  393. cellG.SetValue(fmt.Sprint(v.StartDate, " ~ ", v.EndDate))
  394. cellH := dataRow.AddCell()
  395. cellH.SetStyle(style)
  396. cellH.SetValue(v.PermissionName)
  397. cellI := dataRow.AddCell()
  398. cellI.SetStyle(style)
  399. if v.IsAllocation == 1 {
  400. cellI.SetValue("已派点")
  401. } else {
  402. cellI.SetValue("未派点")
  403. }
  404. }
  405. err = xlsxFile.Save(downLoadnFilePath)
  406. if err != nil {
  407. br.Msg = "保存文件失败"
  408. br.ErrMsg = "保存文件失败"
  409. return
  410. }
  411. randStr := time.Now().Format(utils.FormatDateTimeUnSpace)
  412. downloadFileName := "派点导出数据_" + randStr + ".xlsx"
  413. this.Ctx.Output.Download(downLoadnFilePath, downloadFileName)
  414. defer func() {
  415. os.Remove(downLoadnFilePath)
  416. }()
  417. br.Ret = 200
  418. br.Success = true
  419. br.Msg = "导出成功"
  420. }
  421. // @Title 详情
  422. // @Description 获取详情接口
  423. // @Param CompanyContractId query int true "ID"
  424. // @Param ShowDetail query bool false "是否是派点详情展示"
  425. // @Success Ret=200 {object} cygx.CygxAllocationCompanyContractDetailResp
  426. // @router /allocation/detail [get]
  427. func (this *ContractAllocationController) CompanyContracDetail() {
  428. br := new(models.BaseResponse).Init()
  429. defer func() {
  430. this.Data["json"] = br
  431. this.ServeJSON()
  432. }()
  433. AdminUser := this.SysUser
  434. if AdminUser == nil {
  435. br.Msg = "请登录"
  436. br.ErrMsg = "请登录,用户信息为空"
  437. br.Ret = 408
  438. return
  439. }
  440. resp := new(cygx.CygxAllocationCompanyContractDetailResp)
  441. companyContractId, _ := this.GetInt("CompanyContractId")
  442. if companyContractId < 1 {
  443. br.Msg = "请输入详情ID"
  444. return
  445. }
  446. showDetail, _ := this.GetBool("ShowDetail", false)
  447. contractItem, err := company.GetCompanyContractById(companyContractId)
  448. if err != nil {
  449. br.Msg = "获取信息失败"
  450. br.ErrMsg = "获取合同信息失败,Err:" + err.Error()
  451. return
  452. }
  453. total, err := cygx.GetCygxAllocationCompanyContractCountByCompanyContractId(companyContractId)
  454. if err != nil {
  455. br.Msg = "获取信息失败"
  456. br.ErrMsg = "获取合同信息失败,GetCygxAllocationCompanyContractCountByCompanyContractId Err:" + err.Error()
  457. return
  458. }
  459. //var contractPermissionList []*company.ContractPermissionList
  460. //expMap := map[int]string{0: "(3w)", 1: "(5w)", 2: "(10w)"} // 买方研选价格
  461. hasPermissions, e := company.GetCompanyContractPermissionByCompanyContractId(companyContractId)
  462. if e != nil {
  463. br.Msg = "获取失败"
  464. br.ErrMsg = "获取合同权限信息失败,Err:" + e.Error()
  465. return
  466. }
  467. hasMap := make(map[int]*company.CompanyContractPermission)
  468. var pointMoney float64
  469. for _, p := range hasPermissions {
  470. hasMap[p.ChartPermissionId] = p
  471. if p.ChartPermissionId == utils.YAN_XUAN_KOU_DIAN_BAO_ID {
  472. pointMoney = math.Round(0.2*p.Points*100) / 100 // 将给定数字舍入到最近的整数,处理精度问题
  473. }
  474. }
  475. raiPermissions, e := company.GetPermissionLookItemsExt("2", utils.COMPANY_PRODUCT_RAI_NAME)
  476. if e != nil {
  477. br.Msg = "获取失败"
  478. br.ErrMsg = "获取权益权限列表失败, Err: " + e.Error()
  479. return
  480. }
  481. mapPermissionNameHave := make(map[string]bool) // 判断合同是否存在某一行业权限种类
  482. var expensiveYx int
  483. for _, n := range raiPermissions {
  484. match := hasMap[n.ChartPermissionId]
  485. if match == nil {
  486. continue
  487. }
  488. mapPermissionNameHave[n.PermissionName] = true
  489. // 买方研选(3w/5w/10w)
  490. if n.PermissionName == utils.CHART_PERMISSION_NAME_MF_YANXUAN {
  491. expensiveYx = match.ExpensiveYx
  492. //n.PermissionName += expMap[match.ExpensiveYx]
  493. continue
  494. }
  495. }
  496. resp.Money = contractItem.Money / 10000
  497. //有研选时,对研选套餐类型做文案处理
  498. respItemYx := new(cygx.AllocationPermissionListResp) // 研选订阅
  499. respItemYxKouDian := new(cygx.AllocationPermissionListResp) // 研选扣点
  500. if mapPermissionNameHave[utils.CHART_PERMISSION_NAME_MF_YANXUAN] || mapPermissionNameHave[utils.YAN_XUAN_KOU_DIAN_BAO_NAME] {
  501. var moneyYx float64
  502. if mapPermissionNameHave[utils.CHART_PERMISSION_NAME_MF_YANXUAN] {
  503. if expensiveYx == 0 {
  504. moneyYx = 3
  505. } else if expensiveYx == 1 {
  506. moneyYx = 5
  507. } else if expensiveYx == 2 {
  508. moneyYx = 10
  509. }
  510. respItemYx.Proportion = 0
  511. respItemYx.Money = moneyYx
  512. respItemYx.ChartPermissionName = utils.CHART_PERMISSION_NAME_MF_YANXUAN
  513. respItemYx.ChartPermissionId = utils.CHART_PERMISSION_ID_YANXUAN
  514. //手动拼接研选订阅的数据类型
  515. listYxItem := new(cygx.AllocationRealNameListResp)
  516. listYxItem.RealName = utils.CHART_PERMISSION_NAME_MF_YANXUAN
  517. listYxItem.Money = moneyYx
  518. listYxItem.ChartPermissionId = utils.CHART_PERMISSION_ID_YANXUAN
  519. respItemYx.List = append(respItemYx.List, listYxItem)
  520. }
  521. if mapPermissionNameHave[utils.YAN_XUAN_KOU_DIAN_BAO_NAME] {
  522. respItemYxKouDian.Proportion = 0
  523. respItemYxKouDian.Money = pointMoney
  524. respItemYxKouDian.ChartPermissionName = utils.YAN_XUAN_KOU_DIAN_BAO_NAME
  525. respItemYxKouDian.ChartPermissionId = utils.YAN_XUAN_KOU_DIAN_BAO_ID
  526. listKouDianItem := new(cygx.AllocationRealNameListResp)
  527. listKouDianItem.RealName = utils.YAN_XUAN_KOU_DIAN_BAO_NAME
  528. listKouDianItem.Money = pointMoney
  529. listKouDianItem.ChartPermissionId = utils.YAN_XUAN_KOU_DIAN_BAO_ID
  530. respItemYxKouDian.List = append(respItemYxKouDian.List, listKouDianItem)
  531. }
  532. resp.TotalPointsContent = fmt.Sprint(resp.Money, "W,", "其中", moneyYx+pointMoney, "w默认归属买方研选,请对剩余", resp.Money-moneyYx-pointMoney, "w按照100%进行比值分配")
  533. if showDetail {
  534. resp.TotalPointsContent = fmt.Sprint(resp.Money, "W")
  535. }
  536. resp.Money = resp.Money - moneyYx
  537. } else {
  538. resp.TotalPointsContent = fmt.Sprint(resp.Money, "W")
  539. }
  540. sysUserList, err := cygx.GetAskEmailList()
  541. if err != nil {
  542. br.Msg = "获取失败"
  543. br.ErrMsg = "获取失败,GetAskEmailList Err: " + err.Error()
  544. return
  545. }
  546. mapPermissionUser := make(map[string][]*cygx.AllocationRealNameListResp)
  547. listPermission, err := cygx.GetChartPermissionAll()
  548. if err != nil {
  549. br.Msg = "获取信息失败"
  550. br.ErrMsg = "获取品种信息失败,Err:" + err.Error()
  551. return
  552. }
  553. mapPermissionId := make(map[string]int)
  554. for _, v := range listPermission {
  555. mapPermissionId[v.PermissionName] = v.ChartPermissionId
  556. }
  557. var respList []*cygx.AllocationPermissionListResp
  558. if total == 0 {
  559. for _, v := range sysUserList {
  560. if !mapPermissionNameHave[v.ChartPermissionName] {
  561. continue
  562. }
  563. item := new(cygx.AllocationRealNameListResp)
  564. item.RealName = v.Name
  565. item.ChartPermissionId = mapPermissionId[v.ChartPermissionName]
  566. mapPermissionUser[v.ChartPermissionName] = append(mapPermissionUser[v.ChartPermissionName], item)
  567. }
  568. for k, v := range mapPermissionUser {
  569. respItem := new(cygx.AllocationPermissionListResp)
  570. respItem.ChartPermissionName = k
  571. respItem.ChartPermissionId = mapPermissionId[k]
  572. respItem.List = v
  573. respList = append(respList, respItem)
  574. }
  575. //研选订阅
  576. if respItemYx.ChartPermissionName != "" {
  577. respList = append(respList, respItemYx)
  578. }
  579. //研选扣点
  580. if respItemYxKouDian.ChartPermissionName != "" {
  581. respList = append(respList, respItemYxKouDian)
  582. }
  583. } else {
  584. listUser, err := cygx.GetCygxAllocationCompanyContractListById(companyContractId)
  585. if err != nil {
  586. br.Msg = "获取失败"
  587. br.ErrMsg = "获取失败,GetCygxAllocationCompanyContractListById Err: " + err.Error()
  588. return
  589. }
  590. for _, v := range listUser {
  591. if showDetail && v.Money == 0 {
  592. continue
  593. }
  594. item := new(cygx.AllocationRealNameListResp)
  595. item.RealName = v.RealName
  596. item.Money = v.Money
  597. item.Proportion = v.Proportion
  598. item.ChartPermissionId = mapPermissionId[v.ChartPermissionName]
  599. mapPermissionUser[v.ChartPermissionName] = append(mapPermissionUser[v.ChartPermissionName], item)
  600. }
  601. listPermission, err := cygx.GetCygxAllocationCompanyContractPermissionListById(companyContractId)
  602. if err != nil {
  603. br.Msg = "获取失败"
  604. br.ErrMsg = "获取失败,GetCygxAllocationCompanyContractPermissionListById Err: " + err.Error()
  605. return
  606. }
  607. for _, v := range listPermission {
  608. if showDetail && v.Money == 0 {
  609. continue
  610. }
  611. respItem := new(cygx.AllocationPermissionListResp)
  612. respItem.ChartPermissionName = v.ChartPermissionName
  613. respItem.ChartPermissionId = mapPermissionId[v.ChartPermissionName]
  614. respItem.Money = v.Money
  615. respItem.Proportion = v.Proportion
  616. respItem.List = mapPermissionUser[v.ChartPermissionName]
  617. respList = append(respList, respItem)
  618. }
  619. }
  620. //处理是否置灰
  621. mapIsGray, err := cygxService.GetMapIsGrayByCompanyContractIds([]int{companyContractId})
  622. if err != nil {
  623. br.Msg = "获取合同列表失败!"
  624. br.ErrMsg = "获取合同列表失败,GetMapIsGrayByCompanyContractIds Err:" + err.Error()
  625. return
  626. }
  627. permissionNameArr := []string{"医药", "消费", "科技", "智造", "策略", utils.CHART_PERMISSION_NAME_MF_YANXUAN, utils.YAN_XUAN_KOU_DIAN_BAO_NAME}
  628. for _, v := range permissionNameArr {
  629. for _, v2 := range respList {
  630. if v2.ChartPermissionName == v {
  631. resp.List = append(resp.List, v2)
  632. }
  633. }
  634. }
  635. resp.IsGray = mapIsGray[companyContractId]
  636. //resp.List = respList
  637. resp.CompanyContractId = companyContractId
  638. br.Ret = 200
  639. br.Success = true
  640. br.Msg = "获取成功"
  641. br.Data = resp
  642. }
  643. // @Title 更新派点
  644. // @Description 更新派点接口
  645. // @Param request body cygx.AddProductInteriorReq true "type json string"
  646. // @Success 200 {object} "保存成功"
  647. // @router /allocation/update [post]
  648. func (this *ContractAllocationController) CompanyContracUpdate() {
  649. br := new(models.BaseResponse).Init()
  650. defer func() {
  651. this.Data["json"] = br
  652. this.ServeJSON()
  653. }()
  654. sysUser := this.SysUser
  655. if sysUser == nil {
  656. br.Msg = "请登录"
  657. br.ErrMsg = "请登录,SysUser Is Empty"
  658. br.Ret = 408
  659. return
  660. }
  661. var req cygx.UpdateAllocationCompanyContractReq
  662. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  663. if err != nil {
  664. br.Msg = "参数解析异常!"
  665. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  666. return
  667. }
  668. companyContractId := req.CompanyContractId
  669. if companyContractId == 0 {
  670. br.Msg = "参数错误"
  671. br.ErrMsg = "参数错误,id不可为空"
  672. return
  673. }
  674. mapIsGray, err := cygxService.GetMapIsGrayByCompanyContractIds([]int{companyContractId})
  675. if err != nil {
  676. br.Msg = "获取合同列表失败!"
  677. br.ErrMsg = "获取合同列表失败,GetMapIsGrayByCompanyContractIds Err:" + err.Error()
  678. return
  679. }
  680. if mapIsGray[companyContractId] {
  681. br.Msg = "超过180天,无法修改!"
  682. br.ErrMsg = "超过180天,无法修改,companyContractId :" + strconv.Itoa(companyContractId)
  683. return
  684. }
  685. contractItem, err := company.GetCompanyContractById(companyContractId)
  686. if err != nil {
  687. br.Msg = "获取信息失败"
  688. br.ErrMsg = "获取合同信息失败,Err:" + err.Error()
  689. return
  690. }
  691. money := contractItem.Money / 10000 // 合同金额,万为单位
  692. var moneyAvg float64 // 行业所占合同的平均金额
  693. hasPermissions, e := company.GetCompanyContractPermissionByCompanyContractId(companyContractId)
  694. if e != nil {
  695. br.Msg = "获取失败"
  696. br.ErrMsg = "获取合同权限信息失败,Err:" + e.Error()
  697. return
  698. }
  699. hasMap := make(map[int]*company.CompanyContractPermission)
  700. for _, p := range hasPermissions {
  701. hasMap[p.ChartPermissionId] = p
  702. }
  703. raiPermissions, e := company.GetPermissionLookItemsExt("2", utils.COMPANY_PRODUCT_RAI_NAME)
  704. if e != nil {
  705. br.Msg = "获取失败"
  706. br.ErrMsg = "获取权益权限列表失败, Err: " + e.Error()
  707. return
  708. }
  709. mapPermissionNameHave := make(map[string]bool) // 判断合同是否存在某一行业权限种类
  710. for _, n := range raiPermissions {
  711. //只计算,医药、消费、科技、智造、策略、买方研选的
  712. 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_MF_YANXUAN {
  713. continue
  714. }
  715. match := hasMap[n.ChartPermissionId]
  716. if match == nil {
  717. continue
  718. }
  719. // 买方研选(3w/5w/10W)
  720. if n.PermissionName == utils.CHART_PERMISSION_NAME_MF_YANXUAN {
  721. if match.ExpensiveYx == 1 {
  722. money = money - 5
  723. } else if match.ExpensiveYx == 2 {
  724. money = money - 10
  725. } else {
  726. money = money - 3
  727. }
  728. } else {
  729. mapPermissionNameHave[n.PermissionName] = true
  730. }
  731. }
  732. if len(mapPermissionNameHave) > 0 {
  733. moneyAvg = money / float64(len(mapPermissionNameHave))
  734. }
  735. list := req.List
  736. var items []*cygx.CygxAllocationCompanyContract
  737. var itemsPermission []*cygx.CygxAllocationCompanyContractPermission
  738. var proportionSum float64 // 校验前端传过来的占比使用
  739. for _, v := range list {
  740. itemPermission := new(cygx.CygxAllocationCompanyContractPermission)
  741. itemPermission.CompanyContractId = companyContractId
  742. itemPermission.AdminId = sysUser.AdminId
  743. itemPermission.AdminName = sysUser.RealName
  744. itemPermission.Proportion = v.Proportion
  745. itemPermission.Money = v.Money
  746. if v.ChartPermissionName != utils.YAN_XUAN_KOU_DIAN_BAO_NAME {
  747. itemPermission.MoneyAvg = moneyAvg
  748. if v.Money < moneyAvg/2 {
  749. br.Msg = "单行业占比值不得低于平均值的一半"
  750. br.ErrMsg = "单行业占比值不得低于平均值的一半,Err:" + fmt.Sprint(proportionSum)
  751. return
  752. }
  753. }
  754. itemPermission.ChartPermissionName = v.ChartPermissionName
  755. itemPermission.CreateTime = time.Now()
  756. itemPermission.ModifyTime = time.Now()
  757. itemsPermission = append(itemsPermission, itemPermission)
  758. var userProportionSum float64 // 校验前端传过来的占比使用
  759. for _, v2 := range v.List {
  760. if v2.Proportion < -20 {
  761. br.Msg = "研究员占比值不得小于总额的-20%"
  762. br.ErrMsg = "研究员占比值不得小于总额的20%,Err:" + fmt.Sprint(proportionSum)
  763. return
  764. }
  765. item := new(cygx.CygxAllocationCompanyContract)
  766. item.CompanyContractId = companyContractId
  767. item.AdminId = sysUser.AdminId
  768. item.AdminName = sysUser.RealName
  769. item.Proportion = v2.Proportion
  770. item.Money = v2.Money
  771. item.RealName = v2.RealName
  772. item.ChartPermissionName = v.ChartPermissionName
  773. item.CreateTime = time.Now()
  774. item.ModifyTime = time.Now()
  775. items = append(items, item)
  776. proportionSum += v2.Proportion
  777. userProportionSum += v2.Proportion
  778. }
  779. //先添加0.2%的精度校验
  780. if userProportionSum > (v.Proportion+0.2) || userProportionSum < (v.Proportion-0.2) {
  781. br.Msg = "单行业下的研究员比值之和不等于行业占比值"
  782. br.ErrMsg = "单行业下的研究员比值之和不等于行业占比值"
  783. return
  784. }
  785. }
  786. //先添加0.5%的精度校验
  787. if proportionSum > 100.5 || proportionSum < 99.5 {
  788. br.Msg = "行业总比值相加不等于100%"
  789. br.ErrMsg = "行业总比值相加不等于100%,Err:"
  790. return
  791. }
  792. err = cygx.AddAndUpdateCygxAllocationCompanyContract(items, itemsPermission, companyContractId)
  793. if err != nil {
  794. br.Msg = "操作失败"
  795. br.ErrMsg = "操作失败,AddAndUpdateCygxAllocationCompanyContract Err:" + err.Error()
  796. return
  797. }
  798. br.Ret = 200
  799. br.Success = true
  800. br.IsAddLog = true
  801. br.Msg = "操作成功"
  802. }
  803. // @Title 研究员派点统计
  804. // @Description 研究员派点统计接口
  805. // @Param PageSize query int true "每页数据条数"
  806. // @Param CurrentIndex query int true "当前页页码,从1开始"
  807. // @Param Keyword query string true "客户名称"
  808. // @Param StartDate query string false "开始日期"
  809. // @Param EndDate query string false "结束日期"
  810. // @Param IsExport query bool false "是否导出excel,默认是false"
  811. // @Success 200 {object} cygx.CompanyContractListResp
  812. // @router /allocation/statistics [get]
  813. func (this *ContractAllocationController) CompanyContractStatistics() {
  814. br := new(models.BaseResponse).Init()
  815. defer func() {
  816. this.Data["json"] = br
  817. this.ServeJSON()
  818. }()
  819. sysUser := this.SysUser
  820. if sysUser == nil {
  821. br.Msg = "请登录"
  822. br.ErrMsg = "请登录,SysUser Is Empty"
  823. br.Ret = 408
  824. return
  825. }
  826. keyword := this.GetString("Keyword")
  827. startDate := this.GetString("StartDate")
  828. endDate := this.GetString("EndDate")
  829. if startDate == "" {
  830. startDate = "2015-01-01"
  831. }
  832. //是否导出报表
  833. isExport, _ := this.GetBool("IsExport")
  834. var condition string
  835. var pars []interface{}
  836. if endDate != "" {
  837. condition += ` AND a.start_date >= ? AND a.start_date <= ? `
  838. pars = append(pars, startDate+" 00:00:01", endDate+" 23:59:59")
  839. }
  840. //根据当前角色来获取查询条件
  841. //condition, pars = getQueryParams(condition, pars, sysUser, "c.")
  842. companyIds, err := cygxService.GetAdminLookUserCompanyIdsBySelf(sysUser)
  843. if err != nil {
  844. br.Msg = "获取失败"
  845. br.ErrMsg = "获取失败,GetAdminLookUserCompanyIds Err:" + err.Error()
  846. return
  847. }
  848. lencompanyIds := len(companyIds)
  849. if lencompanyIds > 0 {
  850. condition += ` AND c.company_id IN (` + utils.GetOrmInReplace(lencompanyIds) + `)`
  851. pars = append(pars, companyIds)
  852. }
  853. //关键字搜索
  854. if keyword != "" {
  855. condition += ` and b.company_name like "%` + keyword + `%" `
  856. }
  857. //默认只查询权益 2023-06-01 之后的合同
  858. condition += ` AND c.product_id = ? AND a.start_date > ? `
  859. pars = append(pars, 2, "2023-05-31")
  860. //列表页数据
  861. listContract, err := cygx.GetCompanyContractListJoinCompany(condition, pars, 0, 1000)
  862. if err != nil {
  863. br.Msg = "获取失败"
  864. br.ErrMsg = "获取失败,Err:" + err.Error()
  865. return
  866. }
  867. //return
  868. var companyContractIds []int
  869. companyContractIdBool := make(map[int]bool)
  870. for _, v := range listContract {
  871. companyContractIds = append(companyContractIds, v.CompanyContractId)
  872. }
  873. lenArr := len(companyContractIds)
  874. mapUserAllocation := make(map[string]float64) // 关联合同
  875. mapUserMoney := make(map[string]float64) // 派点金额
  876. mapPermissionAllocation := make(map[string]float64)
  877. mapPermissionMoney := make(map[string]float64)
  878. mapPermissionMoneyAvg := make(map[string]float64)
  879. mapPermissionContract := make(map[string]float64) // 行业关联的合同数量
  880. mapPermissionContractKey := make(map[string]bool) // 行业关联的合同数量键值对
  881. //totalContract := lenArr //所有的关联合同
  882. var totalMoney float64 //所有的关联合同的金额 单位万
  883. if lenArr > 0 {
  884. var conditionAllocation string
  885. var parsAllocation []interface{}
  886. conditionAllocation = ` AND company_contract_id IN(` + utils.GetOrmInReplace(lenArr) + ` ) `
  887. parsAllocation = append(parsAllocation, companyContractIds)
  888. allocationCompanyContractList, err := cygx.GetCygxAllocationCompanyContractList(conditionAllocation, parsAllocation)
  889. if err != nil {
  890. br.Msg = "获取失败"
  891. br.ErrMsg = "获取失败,Err:" + err.Error()
  892. return
  893. }
  894. allocationCompanyContractPermissionList, err := cygx.GetCygxAllocationCompanyContractPermissionList(conditionAllocation, parsAllocation)
  895. if err != nil {
  896. br.Msg = "获取失败"
  897. br.ErrMsg = "获取失败,Err:" + err.Error()
  898. return
  899. }
  900. for _, v := range allocationCompanyContractList {
  901. if v.Proportion != 0 && v.ChartPermissionName != utils.CHART_PERMISSION_NAME_MF_YANXUAN {
  902. mapUserAllocation[v.RealName] += 1
  903. mapUserMoney[v.RealName] += v.Money
  904. mapPermissionAllocation[v.ChartPermissionName] += 1
  905. mapPermissionMoney[v.ChartPermissionName] += v.Money
  906. totalMoney += v.Money
  907. companyContractIdBool[v.CompanyContractId] = true
  908. //统计单个行业所关的合同数量
  909. if !mapPermissionContractKey[fmt.Sprint("ChartPermissionName_", v.ChartPermissionName, "CompanyContractId_", v.CompanyContractId)] {
  910. mapPermissionContract[v.ChartPermissionName] += 1
  911. mapPermissionContractKey[fmt.Sprint("ChartPermissionName_", v.ChartPermissionName, "CompanyContractId_", v.CompanyContractId)] = true
  912. }
  913. }
  914. }
  915. for _, v := range allocationCompanyContractPermissionList {
  916. mapPermissionMoneyAvg[v.ChartPermissionName] += v.MoneyAvg
  917. }
  918. }
  919. sysUserList, err := cygx.GetAskEmailList()
  920. if err != nil {
  921. br.Msg = "获取失败"
  922. br.ErrMsg = "获取失败,GetAskEmailList Err: " + err.Error()
  923. return
  924. }
  925. mapPermissionUser := make(map[string][]*cygx.AllocationRealNameStatisticsListResp) //行业分组 map
  926. for _, v := range sysUserList {
  927. item := new(cygx.AllocationRealNameStatisticsListResp)
  928. item.RealName = v.Name
  929. item.TotalRelatedContract = mapUserAllocation[v.Name]
  930. item.TotalDispatchPoint = utils.SubFloatToString(mapUserMoney[v.Name], 2)
  931. if item.TotalDispatchPoint == "" {
  932. item.TotalDispatchPoint = "0"
  933. }
  934. //组内占比
  935. if mapUserMoney[v.Name] == 0 {
  936. item.GroupProportion = ""
  937. } else {
  938. item.GroupProportion = utils.SubFloatToString(mapUserMoney[v.Name]/mapPermissionMoney[v.ChartPermissionName]*100, 2)
  939. }
  940. if item.GroupProportion == "" {
  941. item.GroupProportion = "0"
  942. }
  943. item.GroupProportion += "%"
  944. //部门占比
  945. if totalMoney == 0 {
  946. item.DepartmentProportion = ""
  947. } else {
  948. item.DepartmentProportion = utils.SubFloatToString(mapUserMoney[v.Name]/totalMoney*100, 2)
  949. }
  950. if item.DepartmentProportion == "" {
  951. item.DepartmentProportion = "0"
  952. }
  953. item.DepartmentProportion += "%"
  954. mapPermissionUser[v.ChartPermissionName] = append(mapPermissionUser[v.ChartPermissionName], item)
  955. }
  956. for k, v := range mapPermissionUser {
  957. lenUser := len(v)
  958. item := new(cygx.AllocationRealNameStatisticsListResp)
  959. item.RealName = "合计"
  960. item.TotalRelatedContract = mapPermissionContract[k]
  961. item.TotalDispatchPoint = fmt.Sprint(utils.SubFloatToString(mapPermissionMoney[k], 2), "/", utils.SubFloatToString(mapPermissionMoneyAvg[k], 2))
  962. item.GroupProportion = "100%"
  963. //部门占比
  964. if totalMoney == 0 {
  965. item.DepartmentProportion = ""
  966. } else {
  967. item.DepartmentProportion = utils.SubFloatToString(mapPermissionMoney[k]/totalMoney*100, 2)
  968. }
  969. if item.DepartmentProportion == "" {
  970. item.DepartmentProportion = "0"
  971. }
  972. item.DepartmentProportion += "%"
  973. mapPermissionUser[k] = append(mapPermissionUser[k], item)
  974. item = new(cygx.AllocationRealNameStatisticsListResp)
  975. item.RealName = "平均"
  976. item.TotalRelatedContract = utils.SubFloatToFloat(mapPermissionAllocation[k]/float64(lenUser), 2)
  977. item.TotalDispatchPoint = utils.SubFloatToString(mapPermissionMoney[k]/float64(lenUser), 2)
  978. //组内占比
  979. if mapPermissionMoney[k] == 0 {
  980. item.GroupProportion = ""
  981. } else {
  982. item.GroupProportion = utils.SubFloatToString(1/(float64(len(mapPermissionUser[k])-1))*100, 2)
  983. }
  984. if item.GroupProportion == "" {
  985. item.GroupProportion = "0"
  986. }
  987. item.GroupProportion += "%"
  988. // 部门占比
  989. if totalMoney == 0 {
  990. item.DepartmentProportion = ""
  991. } else {
  992. item.DepartmentProportion = utils.SubFloatToString(mapPermissionMoney[k]/totalMoney*100/float64(len(mapPermissionUser[k])-1), 2)
  993. }
  994. if item.DepartmentProportion == "" {
  995. item.DepartmentProportion = "0"
  996. }
  997. item.DepartmentProportion += "%"
  998. mapPermissionUser[k] = append(mapPermissionUser[k], item)
  999. }
  1000. var list []*cygx.AllocationPermissionStatisticsListResp
  1001. permissionNameArr := []string{"医药", "消费", "科技", "智造", "策略"}
  1002. for _, v := range permissionNameArr {
  1003. item := new(cygx.AllocationPermissionStatisticsListResp)
  1004. item.ChartPermissionName = v
  1005. item.List = mapPermissionUser[v]
  1006. list = append(list, item)
  1007. }
  1008. resp := cygx.CygxAllocationCompanyContractDetailStatisticsResp{
  1009. List: list,
  1010. }
  1011. resp.TotalContract = len(companyContractIdBool)
  1012. resp.TotalMoney = utils.SubFloatToFloat(totalMoney, 2)
  1013. //导出excel
  1014. if isExport {
  1015. CompanyContractStatisticsExport(this, resp, br)
  1016. return
  1017. }
  1018. br.Ret = 200
  1019. br.Success = true
  1020. br.Msg = "获取成功"
  1021. br.Data = resp
  1022. }
  1023. // CompanyContractStatisticsExport 导出Excel
  1024. func CompanyContractStatisticsExport(this *ContractAllocationController, resp cygx.CygxAllocationCompanyContractDetailStatisticsResp, br *models.BaseResponse) {
  1025. dir, err := os.Executable()
  1026. exPath := filepath.Dir(dir)
  1027. downLoadnFilePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  1028. xlsxFile := xlsx.NewFile()
  1029. if err != nil {
  1030. br.Msg = "生成文件失败"
  1031. br.ErrMsg = "生成文件失败"
  1032. return
  1033. }
  1034. style := xlsx.NewStyle()
  1035. alignment := xlsx.Alignment{
  1036. Horizontal: "center",
  1037. Vertical: "center",
  1038. WrapText: true,
  1039. }
  1040. style.Alignment = alignment
  1041. style.ApplyAlignment = true
  1042. sheel, err := xlsxFile.AddSheet("研究员派点统计")
  1043. if err != nil {
  1044. br.Msg = "新增Sheet失败"
  1045. br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
  1046. return
  1047. }
  1048. sheel.SetColWidth(0, 0, 30)
  1049. sheel.SetColWidth(1, 1, 15)
  1050. sheel.SetColWidth(2, 2, 15)
  1051. sheel.SetColWidth(3, 3, 18)
  1052. titleRow := sheel.AddRow()
  1053. cellA := titleRow.AddCell()
  1054. cellA.SetStyle(style)
  1055. cellA.SetValue("组别")
  1056. cellB := titleRow.AddCell()
  1057. cellB.SetStyle(style)
  1058. cellB.SetValue("研究员")
  1059. cellC := titleRow.AddCell()
  1060. cellC.SetStyle(style)
  1061. cellC.SetValue("关联合同")
  1062. cellD := titleRow.AddCell()
  1063. cellD.SetStyle(style)
  1064. cellD.SetValue("总派点")
  1065. cellE := titleRow.AddCell()
  1066. cellE.SetStyle(style)
  1067. cellE.SetValue("组内占比")
  1068. cellF := titleRow.AddCell()
  1069. cellF.SetStyle(style)
  1070. cellF.SetValue("部门占比")
  1071. for _, v := range resp.List {
  1072. for k2, v2 := range v.List {
  1073. dataRow := sheel.AddRow()
  1074. dataRow.SetHeight(20)
  1075. cellA := dataRow.AddCell()
  1076. cellA.SetStyle(style)
  1077. cellA.SetValue(v.ChartPermissionName)
  1078. if k2 < len(v.List)-1 {
  1079. cellA.VMerge = 1
  1080. }
  1081. cellB := dataRow.AddCell()
  1082. cellB.SetStyle(style)
  1083. cellB.SetValue(v2.RealName)
  1084. cellC := dataRow.AddCell()
  1085. cellC.SetStyle(style)
  1086. cellC.SetValue(v2.TotalRelatedContract)
  1087. cellD := dataRow.AddCell()
  1088. cellD.SetStyle(style)
  1089. cellD.SetValue(v2.TotalDispatchPoint)
  1090. cellE := dataRow.AddCell()
  1091. cellE.SetStyle(style)
  1092. cellE.SetValue(v2.GroupProportion)
  1093. cellF := dataRow.AddCell()
  1094. cellF.SetStyle(style)
  1095. cellF.SetValue(v2.DepartmentProportion)
  1096. }
  1097. }
  1098. titleRow = sheel.AddRow()
  1099. cellA = titleRow.AddCell()
  1100. cellA.HMerge = 1
  1101. cellA.SetStyle(style)
  1102. cellA.SetValue("部门合计")
  1103. cellB = titleRow.AddCell()
  1104. cellB.SetStyle(style)
  1105. cellB.SetValue("")
  1106. cellC = titleRow.AddCell()
  1107. cellC.SetStyle(style)
  1108. cellC.SetValue(resp.TotalContract)
  1109. cellD = titleRow.AddCell()
  1110. cellD.SetStyle(style)
  1111. cellD.SetValue(resp.TotalMoney)
  1112. cellE = titleRow.AddCell()
  1113. cellE.SetStyle(style)
  1114. cellE.SetValue("-")
  1115. cellF = titleRow.AddCell()
  1116. cellF.SetStyle(style)
  1117. cellF.SetValue("100%")
  1118. err = xlsxFile.Save(downLoadnFilePath)
  1119. if err != nil {
  1120. br.Msg = "保存文件失败"
  1121. br.ErrMsg = "保存文件失败"
  1122. return
  1123. }
  1124. randStr := time.Now().Format(utils.FormatDateTimeUnSpace)
  1125. downloadFileName := "研究员派点统计_" + randStr + ".xlsx"
  1126. this.Ctx.Output.Download(downLoadnFilePath, downloadFileName)
  1127. defer func() {
  1128. os.Remove(downLoadnFilePath)
  1129. }()
  1130. br.Ret = 200
  1131. br.Success = true
  1132. br.Msg = "导出成功"
  1133. }