contract_allocation.go 39 KB

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