contract.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. package crm
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "hongze/fms_api/models/crm"
  7. "hongze/fms_api/models/fms"
  8. "hongze/fms_api/utils"
  9. "reflect"
  10. "strings"
  11. )
  12. // GetContractDetail 根据id获取合同详情(包含服务)
  13. func GetContractDetail(contractId int) (contractDetail *crm.ContractDetail, err error) {
  14. contractDetail, err = getContractDetail(contractId)
  15. if err != nil {
  16. if err == utils.ErrNoRow {
  17. err = errors.New("找不到该合同")
  18. }
  19. return
  20. }
  21. relationContractDetailList := make([]*crm.ContractDetail, 0)
  22. switch contractDetail.ContractBusinessType {
  23. case "业务合同":
  24. tmpList, tmpErr := crm.GetContractRelationListByRelationContractId(contractDetail.ContractId)
  25. if tmpErr != nil {
  26. err = errors.New("查询业务关联合同异常:" + tmpErr.Error())
  27. return
  28. }
  29. // 获取所有关联合同详情
  30. for _, relationContract := range tmpList {
  31. tmpContractDetail, tmpErr := getContractDetail(relationContract.PaymentOnBehalfContractId)
  32. if tmpErr != nil {
  33. err = errors.New("查询业务关联合同详情异常:" + tmpErr.Error())
  34. return
  35. }
  36. relationContractDetailList = append(relationContractDetailList, tmpContractDetail)
  37. }
  38. case "代付合同":
  39. tmpList, tmpErr := crm.GetContractRelationListByPaymentOnBehalfContractId(contractDetail.ContractId)
  40. if tmpErr != nil {
  41. err = errors.New("查询代付关联合同异常:" + tmpErr.Error())
  42. return
  43. }
  44. // 获取所有关联合同详情
  45. for _, relationContract := range tmpList {
  46. tmpContractDetail, tmpErr := getContractDetail(relationContract.ContractId)
  47. if tmpErr != nil {
  48. err = errors.New("查询业务关联合同详情异常:" + tmpErr.Error())
  49. return
  50. }
  51. relationContractDetailList = append(relationContractDetailList, tmpContractDetail)
  52. }
  53. }
  54. contractDetail.RelationContractDetailList = relationContractDetailList
  55. return
  56. }
  57. // getContractDetail 根据id获取合同详情(包含服务)
  58. func getContractDetail(contractId int) (contractDetail *crm.ContractDetail, err error) {
  59. // 获取合同基础信息详情
  60. contractDetail, err = crm.GetContractDetailById(contractId)
  61. if err != nil {
  62. if err == utils.ErrNoRow {
  63. err = errors.New("找不到该合同")
  64. }
  65. return
  66. }
  67. // 获取合同服务内容详情
  68. serviceList, err := getContractServiceAndDetailList(contractId)
  69. if err != nil {
  70. err = errors.New(fmt.Sprint("查找合同服务异常", err))
  71. return
  72. }
  73. contractDetail.Service = serviceList
  74. return
  75. }
  76. // getContractServiceAndDetailList 根据合同Id获取服务详情列表
  77. func getContractServiceAndDetailList(contractId int) (serviceList []*crm.ContractServiceAndDetailRespItem, err error) {
  78. serviceList, err = crm.GetContractServiceAndDetailList(contractId)
  79. if err != nil {
  80. err = errors.New(fmt.Sprint("查找合同服务异常", err))
  81. return
  82. }
  83. serviceTempCond := ``
  84. serviceTempPars := make([]interface{}, 0)
  85. serviceTempOB := new(fms.ContractServiceTemplate)
  86. serviceTempList, e := serviceTempOB.List(serviceTempCond, serviceTempPars)
  87. if e != nil {
  88. return
  89. }
  90. templateMap := make(map[string]*fms.ContractServiceTemplate, 0)
  91. templateIdMap := make(map[int]string, 0)
  92. for _, v := range serviceTempList {
  93. templateIdMap[v.ServiceTemplateId] = v.Title
  94. }
  95. for _, v := range serviceTempList {
  96. if v.Title == "主观" || v.Title == "客观" {
  97. pTitle := templateIdMap[v.Pid]
  98. templateMap[pTitle+"("+v.Title+")"] = v
  99. } else {
  100. templateMap[v.Title] = v
  101. }
  102. }
  103. fmsTemplateIdMap := make(map[int]string, 0)
  104. newLen := len(serviceList)
  105. for i := 0; newLen > i; i++ {
  106. if serviceList[i].HasDetail == "是" {
  107. list, detailErr := crm.GetContractServiceDetailListByServiceId(serviceList[i].ContractServiceId)
  108. if detailErr != nil {
  109. err = errors.New(fmt.Sprint("查找合同服务详情异常", detailErr))
  110. return
  111. }
  112. for aaa := range list {
  113. fmt.Println("list[aaa].Col1", list[aaa].Col1)
  114. }
  115. serviceList[i].DetailList = list
  116. }
  117. fmsTemplateIdMap[serviceList[i].ServiceTemplateId] = serviceList[i].Title
  118. }
  119. for _, v := range serviceList {
  120. if v.Title == "主观" || v.Title == "客观" {
  121. v.FMSServiceTemplateId = templateMap[v.Title].ServiceTemplateId
  122. v.FMSTitle = templateIdMap[v.FMSServiceTemplateId]
  123. v.FMSServiceTemplatePId = templateMap[v.Title].Pid
  124. v.FMSPTitle = templateIdMap[v.Pid]
  125. } else {
  126. if templateMap[v.Title] != nil {
  127. v.FMSServiceTemplateId = templateMap[v.Title].ServiceTemplateId
  128. v.FMSServiceTemplatePId = templateMap[v.Title].Pid
  129. }
  130. v.FMSTitle = templateIdMap[v.FMSServiceTemplateId]
  131. v.FMSPTitle = templateIdMap[v.Pid]
  132. }
  133. }
  134. return
  135. }
  136. // GetServicePermissionMap 获取合同服务中的权限id map
  137. func GetServicePermissionMap(serviceList []*crm.ContractServiceAndDetailRespItem) (checkPermissionIdMap map[int]int, err error) {
  138. checkPermissionIdMap = make(map[int]int)
  139. // 获取已经勾选的的权限
  140. for _, contractService := range serviceList {
  141. // 看看系统中有没有映射对应的权限,如果有的话,那么直接返回,没有那么可能是特殊服务,需要去遍历
  142. if contractService.ChartPermissionId > 0 {
  143. checkPermissionIdMap[contractService.ChartPermissionId] = contractService.ChartPermissionId
  144. continue
  145. }
  146. switch contractService.ServiceTemplateId {
  147. case 1: //ficc 大套餐
  148. permissionList, tmpErr := crm.GetSecondPermissionLookItemsByProductId(1)
  149. if tmpErr != nil {
  150. err = errors.New("查询FICC品种出错" + tmpErr.Error())
  151. return
  152. }
  153. for _, v := range permissionList {
  154. //大套餐中 市场策略暂时不作为勾选项
  155. if v.ClassifyName == "市场策略" {
  156. continue
  157. }
  158. checkPermissionIdMap[v.ChartPermissionId] = v.ChartPermissionId
  159. }
  160. case 2: //ficc小套餐
  161. permissionValues := []string{
  162. "FICC周报", "商品双周报+线上电话会讨论会<br/>(由弘则的研究员主持线上讨论)", "数据点评",
  163. }
  164. chartPermissionIds := make([]int, 0)
  165. for _, detail := range contractService.DetailList {
  166. var tableCel AddContractServiceDetailReq
  167. fmt.Println("detail.Col1", detail.Col1)
  168. tmpErr := json.Unmarshal([]byte(detail.Col1), &tableCel)
  169. if tmpErr != nil {
  170. fmt.Println(111)
  171. err = tmpErr
  172. return
  173. }
  174. if strings.Contains(strings.Join(permissionValues, ","), tableCel.Value) {
  175. //获取品种列数据
  176. var tableCel2 AddContractServiceDetailReq
  177. tmpErr := GetProductCelData(*detail, &tableCel2)
  178. if tmpErr != nil {
  179. err = tmpErr
  180. return
  181. }
  182. for _, serviceId := range tableCel2.ValueId {
  183. checkPermissionIdMap[serviceId] = serviceId
  184. }
  185. }
  186. }
  187. for k := range checkPermissionIdMap {
  188. chartPermissionIds = append(chartPermissionIds, checkPermissionIdMap[k])
  189. }
  190. contractService.ChartPermissionIds = chartPermissionIds
  191. case 4, 5, 6: //权益大套餐
  192. permissionFiccClassifyArr := []string{"权益"}
  193. for _, v := range permissionFiccClassifyArr {
  194. items, tmpErr := crm.GetPermissionLookItems(2, v)
  195. if tmpErr != nil {
  196. err = tmpErr
  197. return
  198. }
  199. for _, n := range items {
  200. //如果是专家行业,那么 结束当前循环,进入下一循环(产品需求:专家行业不在里面)
  201. if n.ChartPermissionId == 29 || n.ChartPermissionId == 31 {
  202. continue
  203. }
  204. checkPermissionIdMap[n.ChartPermissionId] = n.ChartPermissionId
  205. }
  206. }
  207. }
  208. }
  209. return
  210. }
  211. type AddContractServiceDetailReq struct {
  212. CanEdit bool `json:"CanEdit" description:"是否可编辑"`
  213. Type string `json:"Type" description:"类型"`
  214. Value string `json:"Value" description:"值"`
  215. ValueId []int `json:"ValueId" description:"类型"`
  216. HeadName string `json:"HeadName" description:"表头名称"`
  217. Tag string `json:"Tag" description:"表头名称"`
  218. RowDisable bool `json:"RowDisable" description:"该行是否禁用操作"`
  219. RowName string `json:"RowName" description:"该行关联名称"`
  220. Name string `json:"Name" description:"行名称"`
  221. }
  222. // GetProductCelData 获取品种列数据(因为前端可以随意插入列数据,所以需要根据列名来搜索对应的品种列)
  223. func GetProductCelData(detail crm.ContractServiceDetail, tableCel *AddContractServiceDetailReq) (err error) {
  224. t := reflect.TypeOf(detail)
  225. v := reflect.ValueOf(detail)
  226. for k := 0; k < t.NumField(); k++ {
  227. //获取结构体的参数名
  228. tmpName := t.Field(k).Name
  229. if strings.Contains(tmpName, "Col") {
  230. //获取结构体该参数名的值
  231. tmpValue := v.Field(k).String()
  232. //如果值不为空的话,那么做下json转换
  233. if tmpValue != "" {
  234. tmpErr := json.Unmarshal([]byte(tmpValue), tableCel)
  235. if tmpErr != nil {
  236. fmt.Println(222)
  237. err = tmpErr
  238. continue
  239. }
  240. if tableCel.HeadName == "品种" {
  241. err = nil
  242. return
  243. }
  244. }
  245. }
  246. }
  247. return
  248. }