contract.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. package crm
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "github.com/go-playground/validator/v10"
  5. "hongze/fms_api/controller/resp"
  6. "hongze/fms_api/global"
  7. "hongze/fms_api/models/base"
  8. "hongze/fms_api/models/crm"
  9. "hongze/fms_api/models/fms"
  10. "hongze/fms_api/models/system"
  11. crmService "hongze/fms_api/services/crm"
  12. "hongze/fms_api/utils"
  13. "strings"
  14. )
  15. // ContractController CRM系统合同
  16. type ContractController struct{}
  17. // SearchList
  18. // @Title 搜索合同
  19. // @Description 搜索合同
  20. // @Param Keyword query string false "关键词"
  21. // @Param ProductId query int false "合同类型: 0-全部; 1-FICC; 2-权益"
  22. // @Success 200 {object} crm.ContractSearchListResp
  23. // @router /crm/contract/search_list [get]
  24. func (rg *ContractController) SearchList(c *gin.Context) {
  25. var req crm.ContractSearchListReq
  26. if e := c.BindQuery(&req); e != nil {
  27. err, ok := e.(validator.ValidationErrors)
  28. if !ok {
  29. resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  30. return
  31. }
  32. resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  33. return
  34. }
  35. cond := `status IN ('已审批', '已签回') AND contract_type IN ('新签合同', '续约合同', '补充协议')`
  36. pars := make([]interface{}, 0)
  37. if req.Keyword != "" {
  38. kw := "%" + req.Keyword + "%"
  39. cond += ` AND (company_name LIKE ? OR contract_code LIKE ?)`
  40. pars = append(pars, kw, kw)
  41. }
  42. if req.ProductId > 0 {
  43. cond += ` AND product_id = ?`
  44. pars = append(pars, req.ProductId)
  45. }
  46. page := new(base.Page)
  47. page.SetPageSize(req.PageSize)
  48. page.SetCurrent(req.Current)
  49. page.AddOrderItem(base.OrderItem{Column: "create_time", Asc: false})
  50. ob := new(crm.Contract)
  51. total, list, e := ob.PageList(page, cond, pars)
  52. if e != nil {
  53. resp.FailMsg("获取失败", "获取合同列表失败, Err: "+e.Error(), c)
  54. return
  55. }
  56. // 获取代付合同实际使用方
  57. businessContractIds := make([]int, 0)
  58. payContractIds := make([]int, 0)
  59. for i := range list {
  60. if list[i].ContractBusinessType == crm.ContractTypePayment {
  61. payContractIds = append(payContractIds, list[i].ContractId)
  62. } else {
  63. businessContractIds = append(businessContractIds, list[i].ContractId)
  64. }
  65. }
  66. payCompanyMap := make(map[int]string, 0) // 代付合同实际使用方
  67. payContractCodeMap := make(map[int]string, 0) // 代付合同实际使用方合同编号
  68. if len(payContractIds) > 0 {
  69. payCond := `a.status IN ('已签回', '已审批') AND b.payment_on_behalf_contract_id IN ?`
  70. payPars := make([]interface{}, 0)
  71. payPars = append(payPars, payContractIds)
  72. payList, e := crm.GetPayCompanyByContractIds(payCond, payPars)
  73. if e != nil {
  74. resp.FailMsg("获取失败", "获取代付合同实际使用方信息失败, Err: "+e.Error(), c)
  75. return
  76. }
  77. for i := range payList {
  78. payCompanyMap[payList[i].PaymentOnBehalfContractId] = payList[i].CompanyName
  79. payContractCodeMap[payList[i].PaymentOnBehalfContractId] = payList[i].ContractCode
  80. }
  81. }
  82. // 获取业务合同的所有代付方信息
  83. businessPayMap := make(map[int]string, 0)
  84. if len(businessContractIds) > 0 {
  85. businessCond := `b.status IN ('已签回', '已审批') AND a.contract_id IN ?`
  86. businessPars := make([]interface{}, 0)
  87. businessPars = append(businessPars, businessContractIds)
  88. businessPayCompanies, e := crm.GetContractActualPayCompaniesByContractIds(businessCond, businessPars)
  89. if e != nil {
  90. resp.FailMsg("获取失败", "获取业务合同代付方信息失败, Err: "+e.Error(), c)
  91. return
  92. }
  93. for i := range businessPayCompanies {
  94. businessPayMap[businessPayCompanies[i].ContractId] = businessPayCompanies[i].ActualPayCompanies
  95. }
  96. }
  97. respList := make([]*crm.ContractSearchListResp, 0)
  98. for i := range list {
  99. contractType := list[i].ContractType
  100. contractTypeKey := crm.ContractTypeFmsMap[list[i].ContractType]
  101. if list[i].ContractBusinessType == crm.ContractTypePayment {
  102. contractType = crm.ContractTypePayment
  103. contractTypeKey = fms.ContractTypeAgentPay
  104. }
  105. respList = append(respList, &crm.ContractSearchListResp{
  106. ContractId: list[i].ContractId,
  107. ContractCode: list[i].ContractCode,
  108. ProductId: list[i].ProductId,
  109. CompanyName: list[i].CompanyName,
  110. RelateContractCode: payContractCodeMap[list[i].ContractId],
  111. ActualCompanyName: payCompanyMap[list[i].ContractId],
  112. SellerId: list[i].SellerId,
  113. SellerName: list[i].SellerName,
  114. ContractTypeKey: contractTypeKey,
  115. ContractType: contractType,
  116. Price: list[i].Price,
  117. StartDate: utils.TimeTransferString(utils.FormatDate, list[i].StartDate),
  118. EndDate: utils.TimeTransferString(utils.FormatDate, list[i].EndDate),
  119. ActualPayCompanies: businessPayMap[list[i].ContractId],
  120. })
  121. }
  122. page.SetTotal(total)
  123. baseData := new(base.BaseData)
  124. baseData.SetPage(page)
  125. baseData.SetList(respList)
  126. resp.OkData("获取成功", baseData, c)
  127. }
  128. // PermissionList
  129. // @Title 合同品种列表
  130. // @Description 合同品种列表
  131. // @Param ProductId query int false "品种类型: 1-FICC(默认); 2-权益"
  132. // @Success 200 {object} crm.PermissionSetResp
  133. // @router /crm/contract/permission_list [get]
  134. func (rg *ContractController) PermissionList(c *gin.Context) {
  135. var req crm.ContractPermissionListReq
  136. if e := c.BindQuery(&req); e != nil {
  137. err, ok := e.(validator.ValidationErrors)
  138. if !ok {
  139. resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  140. return
  141. }
  142. resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  143. return
  144. }
  145. // 默认FICC
  146. productId := req.ProductId
  147. if productId == 0 {
  148. productId = 1
  149. }
  150. respList := new(crm.PermissionSetResp)
  151. // FICC
  152. if productId == 1 {
  153. // 获取品种分类配置
  154. sysConf := new(system.SysConfig)
  155. confCond := `config_code = ?`
  156. confPars := make([]interface{}, 0)
  157. confPars = append(confPars, system.ConfigKeyCrmPermissionFiccClassify)
  158. confItem, e := sysConf.FetchByCondition(confCond, confPars)
  159. if e != nil {
  160. resp.FailData("获取失败", "Err:"+e.Error(), c)
  161. return
  162. }
  163. if confItem.ConfigValue == "" {
  164. resp.FailData("获取失败", "FICC品种分类配置为空", c)
  165. return
  166. }
  167. classifyArr := strings.Split(confItem.ConfigValue, ",")
  168. if len(classifyArr) == 0 {
  169. resp.FailData("获取失败", "FICC品种分类配置为空", c)
  170. return
  171. }
  172. // 获取FICC权限
  173. ficcCond := `enabled = 1 AND permission_type = 0 AND product_id = ? AND classify_name IN ?`
  174. ficcPars := make([]interface{}, 0)
  175. ficcPars = append(ficcPars, productId, classifyArr)
  176. items, e := crm.GetPermissionSetItemsByCondition(ficcCond, ficcPars)
  177. if e != nil {
  178. resp.FailData("获取失败", "获取FICC权限信息失败, Err: "+e.Error(), c)
  179. return
  180. }
  181. ficcItemMap := make(map[string][]*crm.PermissionSetItem, 0)
  182. for i := range items {
  183. if ficcItemMap[items[i].ClassifyName] == nil {
  184. ficcItemMap[items[i].ClassifyName] = make([]*crm.PermissionSetItem, 0)
  185. }
  186. ficcItemMap[items[i].ClassifyName] = append(ficcItemMap[items[i].ClassifyName], items[i])
  187. }
  188. for i := range classifyArr {
  189. if classifyArr[i] == "市场策略" {
  190. continue
  191. }
  192. checkList := make([]int, 0)
  193. if classifyArr[i] == "宏观经济" {
  194. checkList = append(checkList, 1)
  195. }
  196. p := new(crm.PermissionSetList)
  197. p.ClassifyName = classifyArr[i]
  198. p.Items = ficcItemMap[classifyArr[i]]
  199. p.CheckList = make([]int, 0)
  200. respList.List = append(respList.List, p)
  201. }
  202. }
  203. // 权益
  204. if productId == 2 {
  205. raiCond := `enabled = 1 AND permission_type = 0 AND product_id = ? AND classify_name = ?`
  206. raiPars := make([]interface{}, 0)
  207. raiPars = append(raiPars, productId, crm.CompanyProductRaiName)
  208. items, e := crm.GetPermissionSetItemsByCondition(raiCond, raiPars)
  209. if e != nil {
  210. resp.FailData("获取失败", "获取权益权限信息失败, Err: "+e.Error(), c)
  211. return
  212. }
  213. p := new(crm.PermissionSetList)
  214. p.ClassifyName = crm.CompanyProductRaiName
  215. p.Items = items
  216. p.CheckList = make([]int, 0)
  217. respList.List = append(respList.List, p)
  218. }
  219. resp.OkData("获取成功", respList, c)
  220. }
  221. // ServiceDetail
  222. // @Title 获取合同的套餐及品种权限
  223. // @Description 获取合同的套餐及品种权限
  224. // @Param ContractId query int true "合同ID"
  225. // @Success 200 {object} crm.ContractDetail
  226. // @router /crm/contract/service_detail [get]
  227. func (rg *ContractController) ServiceDetail(c *gin.Context) {
  228. var req crm.ContractServiceDetailReq
  229. if e := c.BindQuery(&req); e != nil {
  230. err, ok := e.(validator.ValidationErrors)
  231. if !ok {
  232. resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  233. return
  234. }
  235. resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  236. return
  237. }
  238. contractDetail, e := crmService.GetContractDetail(req.ContractId)
  239. if e != nil {
  240. resp.FailMsg("获取失败", "获取合同套餐及品种信息失败, Err: "+e.Error(), c)
  241. return
  242. }
  243. // 获取合同服务中的权限ID, 填充contractDetail.Service.ChartPermissionIds字段
  244. _, e = crmService.GetServicePermissionMap(contractDetail.Service)
  245. if e != nil {
  246. resp.FailMsg("获取失败", "获取合同服务中的权限ID失败, Err: "+e.Error(), c)
  247. return
  248. }
  249. resp.OkData("获取成功", contractDetail, c)
  250. }