contract.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. package contract
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "github.com/beego/beego/v2/client/orm"
  7. "hongze/hongze_mobile_admin/models/custom"
  8. contractCustom "hongze/hongze_mobile_admin/models/custom/contract"
  9. contractReq "hongze/hongze_mobile_admin/models/request/contract"
  10. contractResp "hongze/hongze_mobile_admin/models/response/contract"
  11. "hongze/hongze_mobile_admin/models/tables/chart_permission"
  12. "hongze/hongze_mobile_admin/models/tables/company_report_permission"
  13. "hongze/hongze_mobile_admin/models/tables/contract"
  14. "hongze/hongze_mobile_admin/models/tables/contract_approval"
  15. "hongze/hongze_mobile_admin/models/tables/contract_approval_record"
  16. "hongze/hongze_mobile_admin/models/tables/contract_operation_record"
  17. "hongze/hongze_mobile_admin/models/tables/contract_relation"
  18. "hongze/hongze_mobile_admin/models/tables/contract_service_detail"
  19. "hongze/hongze_mobile_admin/models/tables/contract_service_template"
  20. "hongze/hongze_mobile_admin/services"
  21. "hongze/hongze_mobile_admin/utils"
  22. "mime/multipart"
  23. "reflect"
  24. "strings"
  25. "time"
  26. )
  27. // InvalidContract 作废合同
  28. func InvalidContract(contractId int, opUser *custom.AdminWx) (err error) {
  29. //查询当前合同信息
  30. nowContract, err := contract.GetContractById(contractId)
  31. if err != nil {
  32. if err.Error() == utils.ErrNoRow() {
  33. err = errors.New(fmt.Sprint("根据合同编号:", contractId, " 找不到初始合同"))
  34. }
  35. return
  36. }
  37. if nowContract.SellerId != opUser.AdminId && opUser.RoleTypeCode != utils.ROLE_TYPE_CODE_COMPLIANCE {
  38. err = errors.New("当前账号无操作权限")
  39. return
  40. }
  41. if nowContract.Status != "已审批" {
  42. err = errors.New("合同状态异常,不允许作废,当前合同状态:" + nowContract.Status)
  43. return
  44. }
  45. //如果删除状态 >0,那么代表已经被删除了
  46. if nowContract.IsDelete > 0 {
  47. err = errors.New("该合同已删除")
  48. return
  49. }
  50. //合同作废
  51. err = contract.InvalidContract(nowContract)
  52. if err != nil {
  53. return
  54. }
  55. //添加操作日志
  56. remark := "作废合同模板"
  57. _ = contract_operation_record.AddContractOperationRecord(nowContract.ContractId, opUser.AdminId, 0, "invalid", opUser.RealName, remark)
  58. //修改业务合同的已支付金额
  59. if nowContract.ContractBusinessType == "代付合同" {
  60. go updateContractPaidPriceByPaymentOnBehalfContractId(nowContract.ContractId, -nowContract.Price)
  61. }
  62. return
  63. }
  64. // GetContractDetail 根据id获取合同详情(包含服务)
  65. func GetContractDetail(contractId int) (contractDetail *contract.ContractDetail, err error) {
  66. contractDetail, err = getContractDetail(contractId)
  67. if err != nil {
  68. if err.Error() == utils.ErrNoRow() {
  69. err = errors.New("找不到该合同")
  70. }
  71. return
  72. }
  73. relationContractDetailList := make([]*contract.ContractDetail, 0)
  74. switch contractDetail.ContractBusinessType {
  75. case "业务合同":
  76. tmpList, tmpErr := contract_relation.GetContractRelationListByRelationContractId(contractDetail.ContractId)
  77. if tmpErr != nil {
  78. err = errors.New("查询业务关联合同异常:" + tmpErr.Error())
  79. return
  80. }
  81. //获取所有关联合同详情
  82. for _, relationContract := range tmpList {
  83. tmpContractDetail, tmpErr := getContractDetail(relationContract.PaymentOnBehalfContractId)
  84. if tmpErr != nil {
  85. err = errors.New("查询业务关联合同详情异常:" + tmpErr.Error())
  86. return
  87. }
  88. relationContractDetailList = append(relationContractDetailList, tmpContractDetail)
  89. }
  90. case "代付合同":
  91. tmpList, tmpErr := contract_relation.GetContractRelationListByPaymentOnBehalfContractId(contractDetail.ContractId)
  92. if tmpErr != nil {
  93. err = errors.New("查询代付关联合同异常:" + tmpErr.Error())
  94. return
  95. }
  96. //获取所有关联合同详情
  97. for _, relationContract := range tmpList {
  98. tmpContractDetail, tmpErr := getContractDetail(relationContract.ContractId)
  99. if tmpErr != nil {
  100. err = errors.New("查询业务关联合同详情异常:" + tmpErr.Error())
  101. return
  102. }
  103. relationContractDetailList = append(relationContractDetailList, tmpContractDetail)
  104. }
  105. }
  106. contractDetail.RelationContractDetailList = relationContractDetailList
  107. return
  108. }
  109. // getContractDetail 根据id获取合同详情(包含服务)
  110. func getContractDetail(contractId int) (contractDetail *contract.ContractDetail, err error) {
  111. contractDetail, err = contract.GetContractDetailById(contractId)
  112. if err != nil {
  113. if err.Error() == utils.ErrNoRow() {
  114. err = errors.New("找不到该合同")
  115. }
  116. return
  117. }
  118. //获取合同服务内容详情
  119. serviceList, err := contractCustom.GetContractServiceAndDetailList(contractId)
  120. if err != nil {
  121. err = errors.New(fmt.Sprint("查找合同服务异常", err))
  122. return
  123. }
  124. // 权益合同主客观合并
  125. if len(serviceList) > 0 && serviceList[0].ProductId == 2 {
  126. mergeList := EquityMergeSubjectAndObject(serviceList)
  127. serviceList = mergeList
  128. }
  129. newLen := len(serviceList)
  130. for i := 0; newLen > i; i++ {
  131. if serviceList[i].HasDetail == "是" {
  132. list, detailErr := contract_service_detail.GetContractServiceDetailListByServiceId(serviceList[i].ContractServiceId)
  133. if detailErr != nil {
  134. err = errors.New(fmt.Sprint("查找合同服务详情异常", detailErr))
  135. return
  136. }
  137. serviceList[i].DetailList = list
  138. }
  139. }
  140. contractDetail.Service = serviceList
  141. return
  142. }
  143. // UploadCheckBackFileByFile 上传签回合同附件
  144. func UploadCheckBackFileByFile(contractId int, ext string, fileMulti multipart.File, opUser *custom.AdminWx) (resourceUrl string, err error) {
  145. //获取合同信息
  146. contractInfo, err := contract.GetContractById(contractId)
  147. if err != nil {
  148. return
  149. }
  150. //合同状态判断
  151. if contractInfo.Status != "已审批" && contractInfo.Status != "已签回" {
  152. err = errors.New("合同状态异常,不允许上传签回合同附件,当前合同状态:" + contractInfo.Status)
  153. return
  154. }
  155. //保存的文件名
  156. fileName := contractInfo.CompanyName + "_" + contractInfo.ContractCode + "(签回)"
  157. //非正式环境下,文件名上面还是加上随机数
  158. if utils.RunMode != "release" {
  159. fileName += "_" + utils.GetRandStringNoSpecialChar(10)
  160. }
  161. fileName += ext
  162. //上传到阿里云
  163. resourceUrl, err = services.UploadToOssAndFileName(fileMulti, fileName)
  164. if err != nil {
  165. err = errors.New("文件保存失败,Err:" + err.Error())
  166. return
  167. }
  168. contractInfo.CheckBackFileUrl = resourceUrl
  169. contractInfo.Status = "已签回"
  170. contractInfo.ModifyTime = time.Now()
  171. contractInfo.CheckBackFileTime = time.Now()
  172. err = contractInfo.Update([]string{"CheckBackFileUrl", "Status", "ModifyTime", "CheckBackFileTime"})
  173. if err != nil {
  174. return
  175. }
  176. //同步更新客户那边提交审批的合同
  177. o := orm.NewOrm()
  178. sql := `update company_contract set img_url = ? where contract_code=? and source="系统合同" `
  179. _, err = o.Raw(sql, contractInfo.CheckBackFileUrl, contractInfo.ContractCode).Exec()
  180. //添加操作日志
  181. remark := "上传签回合同附件"
  182. _ = contract_operation_record.AddContractOperationRecord(contractInfo.ContractId, opUser.AdminId, 0, "upload", opUser.RealName, remark)
  183. return
  184. }
  185. // UploadCheckBackFile 更新上传签回合同附件
  186. func UploadCheckBackFile(contractId int, fileUrl string, opUser *custom.AdminWx) (err error) {
  187. //获取合同信息
  188. contractInfo, err := contract.GetContractById(contractId)
  189. if err != nil {
  190. return
  191. }
  192. //合同状态判断
  193. if contractInfo.Status != "已审批" && contractInfo.Status != "已签回" {
  194. err = errors.New("合同状态异常,不允许上传签回合同附件,当前合同状态:" + contractInfo.Status)
  195. return
  196. }
  197. contractInfo.CheckBackFileUrl = fileUrl
  198. contractInfo.Status = "已签回"
  199. contractInfo.ModifyTime = time.Now()
  200. contractInfo.CheckBackFileTime = time.Now()
  201. err = contractInfo.Update([]string{"CheckBackFileUrl", "Status", "ModifyTime", "CheckBackFileTime"})
  202. if err != nil {
  203. return
  204. }
  205. //同步更新客户那边提交审批的合同
  206. o := orm.NewOrm()
  207. sql := `update company_contract set img_url = ? where contract_code=? and source="系统合同" `
  208. _, err = o.Raw(sql, contractInfo.CheckBackFileUrl, contractInfo.ContractCode).Exec()
  209. //添加操作日志
  210. remark := "上传签回合同附件"
  211. _ = contract_operation_record.AddContractOperationRecord(contractInfo.ContractId, opUser.AdminId, 0, "upload", opUser.RealName, remark)
  212. return
  213. }
  214. // GetOpButton 获取审批流和权限
  215. func GetOpButton(contractInfo *contract.ContractDetail, contractApprovalInfo *contract_approval.ContractApproval, opUser *custom.AdminWx) (contractOpButton contractResp.OpButton, flowNodeListResp [][]contract_approval_record.ContractApprovalRecord, err error) {
  216. //审批流
  217. approvalRecordList, err := contract_approval_record.GetContractApprovalRecordListByContractApprovalId(contractApprovalInfo.ContractApprovalId)
  218. if err != nil {
  219. err = errors.New("获取审批失败,Err:" + err.Error())
  220. return
  221. }
  222. flowNodeMap := make(map[int][]contract_approval_record.ContractApprovalRecord, 0)
  223. keySort := make([]int, 0)
  224. for _, approvalRecord := range approvalRecordList {
  225. //如果当前节点正好是该节点,同时审批单状态是待审批状态,然后当前账号又有权限,该账号也正是审批人,那么允许审批操作
  226. if contractApprovalInfo.CurrNodeId == approvalRecord.NodeId && contractApprovalInfo.Status == "待审批" {
  227. if opUser.AdminId == approvalRecord.ApproveUserId && approvalRecord.NodeType == "check" {
  228. contractOpButton.Approval = true
  229. }
  230. }
  231. if tmpFlowNodeList, ok := flowNodeMap[approvalRecord.NodeId]; ok {
  232. flowNodeMap[approvalRecord.NodeId] = append(tmpFlowNodeList, *approvalRecord)
  233. } else {
  234. tmpFlowNodeList := make([]contract_approval_record.ContractApprovalRecord, 1)
  235. tmpFlowNodeList[0] = *approvalRecord
  236. flowNodeMap[approvalRecord.NodeId] = tmpFlowNodeList
  237. keySort = append(keySort, approvalRecord.NodeId)
  238. }
  239. }
  240. for _, key := range keySort {
  241. flowNodeListResp = append(flowNodeListResp, flowNodeMap[key])
  242. }
  243. //是否具有签回合同权限
  244. uploadStatus := []string{"已审批", "已签回"}
  245. if opUser.RoleTypeCode == utils.ROLE_TYPE_CODE_COMPLIANCE && strings.Contains(strings.Join(uploadStatus, ","), contractInfo.Status) {
  246. contractOpButton.UploadFile = true
  247. }
  248. //作废权限
  249. if (opUser.RoleTypeCode == utils.ROLE_TYPE_CODE_COMPLIANCE || opUser.AdminId == contractInfo.SellerId) && contractInfo.Status == "已审批" {
  250. contractOpButton.Invalid = true
  251. }
  252. return
  253. }
  254. // GetContractDetailByContractId 根据合同获取合同详情
  255. func GetContractDetailByContractId(contractId int, opUser *custom.AdminWx) (contractInfo *contract.ContractDetail, flowNodeListResp [][]contract_approval_record.ContractApprovalRecord, opButton contractResp.OpButton, err error) {
  256. contractInfo, err = GetContractDetail(contractId)
  257. if err != nil {
  258. err = errors.New("获取合同详情失败,ERR:" + err.Error())
  259. return
  260. }
  261. contractInfo.StartDateStr = contractInfo.StartDate.Format(utils.FormatDate)
  262. contractInfo.EndDateStr = contractInfo.EndDate.Format(utils.FormatDate)
  263. contractInfo.ModifyTimeStr = contractInfo.ModifyTime.Format(utils.FormatDateTime)
  264. contractInfo.CreateTimeStr = contractInfo.CreateTime.Format(utils.FormatDateTime)
  265. //合同中包含的产品权限
  266. isNeedPermission := false //是否需要列出来套餐中的详细权限
  267. for _, service := range contractInfo.Service {
  268. if service.ServiceTemplateId == 2 { //小套餐才需要列出来详细的权限
  269. isNeedPermission = true
  270. }
  271. }
  272. if isNeedPermission {
  273. permissionLookList, _ := GetPermissionByContractService(contractInfo.ProductId, contractInfo.Service)
  274. contractInfo.PermissionLookList = permissionLookList
  275. }
  276. //查询最近一次审批单信息
  277. lastApprovalInfo, err := contract_approval.GetLastContractApprovalByContractId(contractInfo.ContractId, "contract")
  278. if err != nil {
  279. if err.Error() != utils.ErrNoRow() {
  280. err = errors.New("获取最近一次审批单信息失败,ERR:" + err.Error())
  281. }
  282. err = nil
  283. return
  284. }
  285. if lastApprovalInfo != nil {
  286. //审批操作权限,上传签回文件权限
  287. tmpContractOpButton, tmpFlowNodeListResp, tmpErr := GetOpButton(contractInfo, lastApprovalInfo, opUser)
  288. if tmpErr != nil {
  289. err = errors.New("获取合同按钮权限、审批流程失败,ERR:" + tmpErr.Error())
  290. return
  291. }
  292. opButton = tmpContractOpButton
  293. flowNodeListResp = tmpFlowNodeListResp
  294. }
  295. return
  296. }
  297. // GetProductCelData 获取品种列数据(因为前端可以随意插入列数据,所以需要根据列名来搜索对应的品种列)
  298. func GetProductCelData(detail contract_service_detail.ContractServiceDetail, tableCel *contractReq.AddContractServiceDetailReq) (err error) {
  299. t := reflect.TypeOf(detail)
  300. v := reflect.ValueOf(detail)
  301. for k := 0; k < t.NumField(); k++ {
  302. //获取结构体的参数名
  303. tmpName := t.Field(k).Name
  304. if strings.Contains(tmpName, "Col") {
  305. //获取结构体该参数名的值
  306. tmpValue := v.Field(k).String()
  307. //如果值不为空的话,那么做下json转换
  308. if tmpValue != "" {
  309. tmpErr := json.Unmarshal([]byte(tmpValue), tableCel)
  310. if tmpErr != nil {
  311. err = tmpErr
  312. continue
  313. }
  314. if tableCel.HeadName == "品种" {
  315. err = nil
  316. return
  317. }
  318. }
  319. }
  320. }
  321. return
  322. }
  323. // GetPermissionByContractService 通过合同服务,获取权限集合列表
  324. func GetPermissionByContractService(productId int, serviceList []*contractCustom.ContractServiceAndDetail) (permissionList []*company_report_permission.PermissionLookList, err error) {
  325. //子权限切片集合
  326. var permissionClassifyArr []*chart_permission.ChartPermission
  327. if productId == 1 {
  328. // 查询一级分类名称
  329. permissionClassifyArr, err = chart_permission.GetPermissionFirstByProductId(productId)
  330. if err != nil {
  331. err = fmt.Errorf("获取权限失败,Err:%s", err.Error())
  332. return
  333. }
  334. } else {
  335. permissionClassifyArr, err = chart_permission.GetPermissionFirstByProductId(productId)
  336. if err != nil {
  337. err = fmt.Errorf("获取权限失败,Err:%s", err.Error())
  338. return
  339. }
  340. }
  341. //获取已经勾选的的权限
  342. checkPermissionIdMap, err := GetServicePermissionMap(serviceList)
  343. if err != nil {
  344. return
  345. }
  346. //遍历获取
  347. for _, v := range permissionClassifyArr {
  348. checkList := make([]int, 0)
  349. plist := new(company_report_permission.PermissionLookList)
  350. items, tmpErr := company_report_permission.GetPermissionLookByParentId(productId, v.ChartPermissionId)
  351. if tmpErr != nil {
  352. err = tmpErr
  353. return
  354. }
  355. for _, n := range items {
  356. if _, ok := checkPermissionIdMap[n.ChartPermissionId]; ok {
  357. checkList = append(checkList, n.ChartPermissionId)
  358. }
  359. }
  360. plist.Items = items
  361. plist.ClassifyName = v.PermissionName
  362. plist.CheckList = checkList
  363. permissionList = append(permissionList, plist)
  364. }
  365. return
  366. }
  367. // GetServicePermissionMap 获取合同服务中的权限id map
  368. func GetServicePermissionMap(serviceList []*contractCustom.ContractServiceAndDetail) (checkPermissionIdMap map[int]int, err error) {
  369. checkPermissionIdMap = make(map[int]int)
  370. //获取已经勾选的的权限
  371. for _, contractService := range serviceList {
  372. //如果是权益,那么研选必要
  373. if contractService.ProductId == 2 {
  374. chartPermissionId := 31 //研 选服务
  375. checkPermissionIdMap[chartPermissionId] = chartPermissionId
  376. }
  377. //看看系统中有没有映射对应的权限,如果有的话,那么直接返回,没有那么可能是特殊服务,需要去遍历
  378. if contractService.ChartPermissionId > 0 {
  379. checkPermissionIdMap[contractService.ChartPermissionId] = contractService.ChartPermissionId
  380. continue
  381. }
  382. switch contractService.ServiceTemplateId {
  383. case 1: //ficc 大套餐
  384. // 查询一级分类名称
  385. allFiccPermissions, e := company_report_permission.GetPermissionLookItemsByProductId(utils.COMPANY_PRODUCT_FICC_ID)
  386. if e != nil {
  387. err = fmt.Errorf("获取权限信息失败,Err:%s", e.Error())
  388. return
  389. }
  390. permissionMap := make(map[int][]*company_report_permission.PermissionLookItem, 0)
  391. for _, v := range allFiccPermissions {
  392. if v.ParentId > 0 {
  393. permissionMap[v.ParentId] = append(permissionMap[v.ParentId], v)
  394. }
  395. }
  396. for _, v := range allFiccPermissions {
  397. if v.ParentId == 0 {
  398. if v.PermissionName == "市场策略" {
  399. continue
  400. }
  401. items, ok := permissionMap[v.ChartPermissionId]
  402. if ok {
  403. for _, n := range items {
  404. checkPermissionIdMap[n.ChartPermissionId] = n.ChartPermissionId
  405. }
  406. }
  407. }
  408. }
  409. /*for _, v := range utils.PermissionFiccClassifyArr {
  410. //大套餐中 市场策略暂时不作为勾选项
  411. if v == "市场策略" {
  412. continue
  413. }
  414. items, tmpErr := company_report_permission.GetPermissionLookItems(1, v)
  415. if tmpErr != nil {
  416. err = tmpErr
  417. return
  418. }
  419. for _, n := range items {
  420. checkPermissionIdMap[n.ChartPermissionId] = n.ChartPermissionId
  421. }
  422. }*/
  423. case 2: //ficc小套餐
  424. permissionValues := []string{
  425. "FICC周报", "商品双周报+线上电话会讨论会<br/>(由弘则的研究员主持线上讨论)", "数据点评",
  426. }
  427. for _, detail := range contractService.DetailList {
  428. var tableCel contractReq.AddContractServiceDetailReq
  429. tmpErr := json.Unmarshal([]byte(detail.Col1), &tableCel)
  430. if tmpErr != nil {
  431. err = tmpErr
  432. return
  433. }
  434. if strings.Contains(strings.Join(permissionValues, ","), tableCel.Value) {
  435. //获取品种列数据
  436. var tableCel2 contractReq.AddContractServiceDetailReq
  437. tmpErr := GetProductCelData(*detail, &tableCel2)
  438. if tmpErr != nil {
  439. err = tmpErr
  440. return
  441. }
  442. for _, serviceId := range tableCel2.ValueId {
  443. checkPermissionIdMap[serviceId] = serviceId
  444. }
  445. }
  446. }
  447. case 4, 5, 6: //权益大套餐
  448. permissionFiccClassifyArr := []string{"权益"}
  449. for _, v := range permissionFiccClassifyArr {
  450. items, tmpErr := company_report_permission.GetPermissionLookItems(2, v)
  451. if tmpErr != nil {
  452. err = tmpErr
  453. return
  454. }
  455. for _, n := range items {
  456. //如果是专家行业,那么 结束当前循环,进入下一循环(产品需求:专家行业不在里面)
  457. if n.ChartPermissionId == 29 {
  458. continue
  459. }
  460. checkPermissionIdMap[n.ChartPermissionId] = n.ChartPermissionId
  461. }
  462. }
  463. }
  464. }
  465. return
  466. }
  467. // CRM8.8-权益合同主客观套餐合并
  468. func EquityMergeSubjectAndObject(serviceList []*contractCustom.ContractServiceAndDetail) (mergeList []*contractCustom.ContractServiceAndDetail) {
  469. serviceLen := len(serviceList)
  470. if serviceLen == 0 {
  471. return serviceList
  472. }
  473. mergeArr := []string{"医药", "消费", "科技", "智造"}
  474. // 获取模板列表
  475. templateList, e := contract_service_template.GetAllContractServiceTemplateList()
  476. if e != nil {
  477. return serviceList
  478. }
  479. parentTempMap := make(map[string]int, 0)
  480. templateMap := make(map[int]*contract_service_template.ContractServiceTemplate, 0)
  481. for _, v := range templateList {
  482. if utils.InArrayByStr(mergeArr, v.Title) {
  483. parentTempMap[v.Title] = v.ServiceTemplateId
  484. }
  485. templateMap[v.ServiceTemplateId] = v
  486. }
  487. // 计算每个行业的子套餐数, 并判断所有套餐所属行业
  488. countIndustryMap := make(map[string]int, 0)
  489. serviceIndustryMap := make(map[int]string, 0)
  490. industryServiceMap := make(map[string]*contractCustom.ContractServiceAndDetail, 0)
  491. childrenServiceMap := make(map[string]*contractCustom.ContractServiceAndDetail, 0)
  492. for i := 0; i < serviceLen; i++ {
  493. temp := templateMap[serviceList[i].ServiceTemplateId]
  494. if temp == nil {
  495. continue
  496. }
  497. // 行业套餐
  498. if utils.InArrayByStr(mergeArr, temp.Title) {
  499. serviceIndustryMap[serviceList[i].ContractServiceId] = temp.Title
  500. industryServiceMap[temp.Title] = serviceList[i]
  501. continue
  502. }
  503. // 主/客观
  504. parentTemp := templateMap[temp.Pid]
  505. if parentTemp != nil && utils.InArrayByStr(mergeArr, parentTemp.Title) {
  506. countIndustryMap[parentTemp.Title] += 1
  507. serviceIndustryMap[serviceList[i].ContractServiceId] = parentTemp.Title
  508. childrenServiceMap[parentTemp.Title] = serviceList[i]
  509. }
  510. }
  511. mergeList = make([]*contractCustom.ContractServiceAndDetail, 0)
  512. // 遍历每一个套餐, 取出所属的行业, 不属于合并的行业则直接加入数组
  513. // 属于合并的行业, 则校验行业子套餐数, 若子套餐数为1, 则取取主/客观套餐, 否则取行业套餐
  514. mergeIndustryMap := make(map[string]int, 0)
  515. for i := 0; i < serviceLen; i++ {
  516. industryName := serviceIndustryMap[serviceList[i].ContractServiceId]
  517. // 不需要合并的行业
  518. if industryName == "" {
  519. fmt.Println("1:", serviceList[i])
  520. mergeList = append(mergeList, serviceList[i])
  521. continue
  522. }
  523. // 未合并则进行合并
  524. if mergeIndustryMap[industryName] > 0 {
  525. continue
  526. }
  527. count := countIndustryMap[industryName]
  528. if count == 1 {
  529. // 取主/客观套餐
  530. mergeList = append(mergeList, childrenServiceMap[industryName])
  531. } else {
  532. // 0或者2都取行业套餐
  533. mergeList = append(mergeList, industryServiceMap[industryName])
  534. }
  535. mergeIndustryMap[industryName] += 1
  536. }
  537. return
  538. }
  539. // CRM8.8-HandleEquityPermissionLookList 权益-权限展示的主客观合并处理
  540. func HandleEquityPermissionLookList(permissionLookList []*company_report_permission.PermissionLookList) []*company_report_permission.PermissionLookList {
  541. if len(permissionLookList) <= 0 {
  542. return permissionLookList
  543. }
  544. checkList := permissionLookList[0].CheckList
  545. //fmt.Println(checkList)
  546. permissionList := permissionLookList[0].Items
  547. permissionLen := len(permissionList)
  548. if len(checkList) <= 0 || permissionLen <= 0 {
  549. return permissionLookList
  550. }
  551. // 若某个行业的主客观权限均被满足,则只留其中一个权限
  552. // 1.取出每个行业对应的其中一个权限
  553. industryName := []string{"医药", "消费", "科技", "智造"}
  554. namePermissionMap := make(map[string]*company_report_permission.PermissionLookItem)
  555. for i := 0; i < permissionLen; i++ {
  556. pName := permissionList[i].PermissionName
  557. if utils.InArrayByStr(industryName, pName) && namePermissionMap[pName] != nil {
  558. namePermissionMap[pName] = permissionList[i]
  559. }
  560. }
  561. // 2.计算哪些行业主客观权限都有
  562. countIndustryMap := make(map[string]int, 0)
  563. for i := 0; i < permissionLen; i++ {
  564. p := permissionList[i]
  565. if utils.InArrayByStr(industryName, p.PermissionName) && utils.InArrayByInt(checkList, p.ChartPermissionId) {
  566. countIndustryMap[p.PermissionName] += 1
  567. }
  568. }
  569. //fmt.Println(countIndustryMap)
  570. // 3.重新组成一个权限列表
  571. mergeIndustryMap := make(map[string]int, 0)
  572. newPermissionList := make([]*company_report_permission.PermissionLookItem, 0)
  573. for i := 0; i < permissionLen; i++ {
  574. item := permissionList[i]
  575. if utils.InArrayByStr(industryName, item.PermissionName) {
  576. // 只有主客观之一, permissionName取remark
  577. if countIndustryMap[item.PermissionName] == 1 {
  578. item.PermissionName = item.Remark
  579. newPermissionList = append(newPermissionList, item)
  580. continue
  581. }
  582. // 主客观都有
  583. if countIndustryMap[item.PermissionName] > 1 && mergeIndustryMap[item.PermissionName] == 0 {
  584. mergeIndustryMap[item.PermissionName] += 1
  585. newPermissionList = append(newPermissionList, item)
  586. }
  587. } else {
  588. newPermissionList = append(newPermissionList, item)
  589. }
  590. }
  591. permissionLookList[0].Items = newPermissionList
  592. return permissionLookList
  593. }