contract.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. peyContractIds := make([]int, 0)
  58. for i := range list {
  59. if list[i].ContractBusinessType == crm.ContractTypePayment {
  60. peyContractIds = append(peyContractIds, list[i].ContractId)
  61. }
  62. }
  63. payCompanyMap := make(map[int]string, 0) // 代付合同实际使用方
  64. payContractCodeMap := make(map[int]string, 0) // 代付合同实际使用方合同编号
  65. if len(peyContractIds) > 0 {
  66. payCond := `a.status IN ('已签回', '已审批') AND b.payment_on_behalf_contract_id IN ?`
  67. payPars := make([]interface{}, 0)
  68. payPars = append(payPars, peyContractIds)
  69. payList, e := crm.GetPayCompanyByContractIds(payCond, payPars)
  70. if e != nil {
  71. resp.FailMsg("获取失败", "获取代付合同信息失败, Err: "+e.Error(), c)
  72. return
  73. }
  74. for i := range payList {
  75. payCompanyMap[payList[i].PaymentOnBehalfContractId] = payList[i].CompanyName
  76. payContractCodeMap[payList[i].PaymentOnBehalfContractId] = payList[i].ContractCode
  77. }
  78. }
  79. respList := make([]*crm.ContractSearchListResp, 0)
  80. for i := range list {
  81. contractType := list[i].ContractType
  82. contractTypeKey := crm.ContractTypeFmsMap[list[i].ContractType]
  83. if list[i].ContractBusinessType == crm.ContractTypePayment {
  84. contractType = crm.ContractTypePayment
  85. contractTypeKey = fms.ContractTypeAgentPay
  86. }
  87. respList = append(respList, &crm.ContractSearchListResp{
  88. ContractId: list[i].ContractId,
  89. ContractCode: list[i].ContractCode,
  90. ProductId: list[i].ProductId,
  91. CompanyName: list[i].CompanyName,
  92. RelateContractCode: payContractCodeMap[list[i].ContractId],
  93. ActualCompanyName: payCompanyMap[list[i].ContractId],
  94. SellerId: list[i].SellerId,
  95. SellerName: list[i].SellerName,
  96. ContractTypeKey: contractTypeKey,
  97. ContractType: contractType,
  98. Price: list[i].Price,
  99. StartDate: utils.TimeTransferString(utils.FormatDate, list[i].StartDate),
  100. EndDate: utils.TimeTransferString(utils.FormatDate, list[i].EndDate),
  101. })
  102. }
  103. page.SetTotal(total)
  104. baseData := new(base.BaseData)
  105. baseData.SetPage(page)
  106. baseData.SetList(respList)
  107. resp.OkData("获取成功", baseData, c)
  108. }
  109. // PermissionList
  110. // @Title 合同品种列表
  111. // @Description 合同品种列表
  112. // @Param ProductId query int false "品种类型: 1-FICC(默认); 2-权益"
  113. // @Success 200 {object} crm.PermissionSetResp
  114. // @router /crm/contract/permission_list [get]
  115. func (rg *ContractController) PermissionList(c *gin.Context) {
  116. var req crm.ContractPermissionListReq
  117. if e := c.BindQuery(&req); e != nil {
  118. err, ok := e.(validator.ValidationErrors)
  119. if !ok {
  120. resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  121. return
  122. }
  123. resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  124. return
  125. }
  126. // 默认FICC
  127. productId := req.ProductId
  128. if productId == 0 {
  129. productId = 1
  130. }
  131. respList := new(crm.PermissionSetResp)
  132. // FICC
  133. if productId == 1 {
  134. // 获取品种分类配置
  135. sysConf := new(system.SysConfig)
  136. confCond := `config_code = ?`
  137. confPars := make([]interface{}, 0)
  138. confPars = append(confPars, system.ConfigKeyCrmPermissionFiccClassify)
  139. confItem, e := sysConf.FetchByCondition(confCond, confPars)
  140. if e != nil {
  141. resp.FailData("获取失败", "Err:"+e.Error(), c)
  142. return
  143. }
  144. if confItem.ConfigValue == "" {
  145. resp.FailData("获取失败", "FICC品种分类配置为空", c)
  146. return
  147. }
  148. classifyArr := strings.Split(confItem.ConfigValue, ",")
  149. if len(classifyArr) == 0 {
  150. resp.FailData("获取失败", "FICC品种分类配置为空", c)
  151. return
  152. }
  153. // 获取FICC权限
  154. ficcCond := `enabled = 1 AND permission_type = 0 AND product_id = ? AND classify_name IN ?`
  155. ficcPars := make([]interface{}, 0)
  156. ficcPars = append(ficcPars, productId, classifyArr)
  157. items, e := crm.GetPermissionSetItemsByCondition(ficcCond, ficcPars)
  158. if e != nil {
  159. resp.FailData("获取失败", "获取FICC权限信息失败, Err: "+e.Error(), c)
  160. return
  161. }
  162. ficcItemMap := make(map[string][]*crm.PermissionSetItem, 0)
  163. for i := range items {
  164. if ficcItemMap[items[i].ClassifyName] == nil {
  165. ficcItemMap[items[i].ClassifyName] = make([]*crm.PermissionSetItem, 0)
  166. }
  167. ficcItemMap[items[i].ClassifyName] = append(ficcItemMap[items[i].ClassifyName], items[i])
  168. }
  169. for i := range classifyArr {
  170. if classifyArr[i] == "市场策略" {
  171. continue
  172. }
  173. checkList := make([]int, 0)
  174. if classifyArr[i] == "宏观经济" {
  175. checkList = append(checkList, 1)
  176. }
  177. p := new(crm.PermissionSetList)
  178. p.ClassifyName = classifyArr[i]
  179. p.Items = ficcItemMap[classifyArr[i]]
  180. p.CheckList = make([]int, 0)
  181. respList.List = append(respList.List, p)
  182. }
  183. }
  184. // 权益
  185. if productId == 2 {
  186. raiCond := `enabled = 1 AND permission_type = 0 AND product_id = ? AND classify_name = ?`
  187. raiPars := make([]interface{}, 0)
  188. raiPars = append(raiPars, productId, crm.CompanyProductRaiName)
  189. items, e := crm.GetPermissionSetItemsByCondition(raiCond, raiPars)
  190. if e != nil {
  191. resp.FailData("获取失败", "获取权益权限信息失败, Err: "+e.Error(), c)
  192. return
  193. }
  194. p := new(crm.PermissionSetList)
  195. p.ClassifyName = crm.CompanyProductRaiName
  196. p.Items = items
  197. p.CheckList = make([]int, 0)
  198. respList.List = append(respList.List, p)
  199. }
  200. resp.OkData("获取成功", respList, c)
  201. }
  202. // ServiceDetail
  203. // @Title 获取合同的套餐及品种权限
  204. // @Description 获取合同的套餐及品种权限
  205. // @Param ContractId query int true "合同ID"
  206. // @Success 200 {object} crm.ContractDetail
  207. // @router /crm/contract/service_detail [get]
  208. func (rg *ContractController) ServiceDetail(c *gin.Context) {
  209. var req crm.ContractServiceDetailReq
  210. if e := c.BindQuery(&req); e != nil {
  211. err, ok := e.(validator.ValidationErrors)
  212. if !ok {
  213. resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  214. return
  215. }
  216. resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  217. return
  218. }
  219. contractDetail, e := crmService.GetContractDetail(req.ContractId)
  220. if e != nil {
  221. global.LOG.Error(e.Error())
  222. resp.FailMsg("获取失败", "获取合同套餐及品种信息失败, Err: "+e.Error(), c)
  223. return
  224. }
  225. // 获取合同服务中的权限ID, 填充contractDetail.Service.ChartPermissionIds字段
  226. _, e = crmService.GetServicePermissionMap(contractDetail.Service)
  227. if e != nil {
  228. resp.FailMsg("获取失败", "获取合同服务中的权限ID失败, Err: "+e.Error(), c)
  229. return
  230. }
  231. resp.OkData("获取成功", contractDetail, c)
  232. }