contract.go 9.0 KB

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