contract_allocation.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. package cygxService
  2. import (
  3. "fmt"
  4. "errors"
  5. "hongze/hongze_mobile_admin/models/tables/chart_permission"
  6. "hongze/hongze_mobile_admin/models/tables/company_contract_permission"
  7. "hongze/hongze_mobile_admin/models/tables/cygx_allocation_company_contract"
  8. "hongze/hongze_mobile_admin/services/alarm_msg"
  9. "hongze/hongze_mobile_admin/utils"
  10. "time"
  11. "hongze/hongze_mobile_admin/models/tables/company_contract"
  12. )
  13. // 如果合同只有研选的时候,自动处理派点
  14. func HandleAllocationCompanyContractByYanXuan(companyContractId int) (err error) {
  15. defer func() {
  16. if err != nil {
  17. fmt.Println(err)
  18. go alarm_msg.SendAlarmMsg(fmt.Sprint("如果合同只有研选的时候,自动处理派点失败,Err:", err.Error(), "companyContractId", companyContractId), 2)
  19. }
  20. }()
  21. var condition string
  22. var pars []interface{}
  23. pars = make([]interface{}, 0)
  24. condition = " AND company_contract_id = ? "
  25. pars = append(pars, companyContractId)
  26. companyContractPermissionList, e := company_contract_permission.GetCompanyContractPermissionList(condition, pars)
  27. if e != nil && e.Error() != utils.ErrNoRow() {
  28. err = errors.New("GetCompanyContractPermissionList, Err: " + e.Error())
  29. return
  30. }
  31. if e != nil && e.Error() != utils.ErrNoRow() {
  32. err = errors.New("GetCygxAllocationCompanyContractPermissionListById, Err: " + e.Error())
  33. return
  34. }
  35. if len(companyContractPermissionList) > 1 {
  36. return
  37. }
  38. var expensiveYxmoney float64
  39. for _, v := range companyContractPermissionList {
  40. if v.ChartPermissionId != utils.CHART_PERMISSION_ID_YANXUAN {
  41. err = errors.New("没有发现研选权限: ")
  42. return
  43. }
  44. if v.ExpensiveYx == 1 {
  45. expensiveYxmoney = 5
  46. } else if v.ExpensiveYx == 2 {
  47. expensiveYxmoney = 10
  48. } else{
  49. expensiveYxmoney = 3
  50. }
  51. }
  52. var items []*cygx_allocation_company_contract.CygxAllocationCompanyContract
  53. var itemsPermission []*cygx_allocation_company_contract.CygxAllocationCompanyContractPermission
  54. itemPermission := new(cygx_allocation_company_contract.CygxAllocationCompanyContractPermission)
  55. itemPermission.CompanyContractId = companyContractId
  56. //itemPermission.AdminId = sysUser.AdminId
  57. //itemPermission.AdminName = sysUser.RealName
  58. itemPermission.Proportion = 0
  59. itemPermission.Money = expensiveYxmoney
  60. itemPermission.MoneyAvg = 0
  61. itemPermission.ChartPermissionName = utils.CHART_PERMISSION_NAME_MF_YANXUAN
  62. itemPermission.CreateTime = time.Now()
  63. itemPermission.ModifyTime = time.Now()
  64. itemsPermission = append(itemsPermission, itemPermission)
  65. item := new(cygx_allocation_company_contract.CygxAllocationCompanyContract)
  66. item.CompanyContractId = companyContractId
  67. //item.AdminId = sysUser.AdminId
  68. //item.AdminName = sysUser.RealName
  69. item.Proportion = 0
  70. item.Money = expensiveYxmoney
  71. item.RealName = utils.CHART_PERMISSION_NAME_MF_YANXUAN
  72. item.ChartPermissionName = utils.CHART_PERMISSION_NAME_MF_YANXUAN
  73. item.CreateTime = time.Now()
  74. item.ModifyTime = time.Now()
  75. items = append(items, item)
  76. e = cygx_allocation_company_contract.AddAndUpdateCygxAllocationCompanyContract(items, itemsPermission, companyContractId)
  77. if e != nil {
  78. err = errors.New("AddAndUpdateCygxAllocationCompanyContract, Err: " + e.Error())
  79. return
  80. }
  81. return
  82. }
  83. // HandleCompanyContractPackageDifference 更新与上一份合同的金额的对比 '增加套餐','减少套餐','维持套餐'
  84. func HandleCompanyContractPackageDifference(companyContractId int) (err error) {
  85. defer func() {
  86. if err != nil {
  87. fmt.Println(err)
  88. go alarm_msg.SendAlarmMsg(fmt.Sprint("如果合同只有研选的时候,自动处理派点失败,Err:", err.Error(), "companyContractId", companyContractId), 2)
  89. }
  90. }()
  91. var condition string
  92. var pars []interface{}
  93. condition = " AND company_contract_id = ? "
  94. pars = append(pars, companyContractId)
  95. detail, e := company_contract.GetCompanyContracDetail(condition, pars)
  96. if e != nil {
  97. err = errors.New("GetCompanyContracDetail,detail Err: " + e.Error())
  98. return
  99. }
  100. //如果不是续约合同就不做对比处理
  101. if detail.ContractType != "续约合同" {
  102. return
  103. }
  104. //获取前一份合同的信息
  105. pars = make([]interface{}, 0)
  106. condition = " AND company_id = ? AND company_contract_id < ? AND status = 1 AND product_id = ? ORDER BY company_contract_id DESC LIMIT 1 "
  107. pars = append(pars, detail.CompanyId, companyContractId, detail.ProductId)
  108. detailPrevious, e := company_contract.GetCompanyContracDetail(condition, pars)
  109. if e != nil {
  110. err = errors.New("GetCompanyContracDetail,detailPrevious Err: " + e.Error())
  111. return
  112. }
  113. var packageDifference string
  114. if detail.Money > detailPrevious.Money {
  115. packageDifference = "增加套餐"
  116. } else if detail.Money < detailPrevious.Money {
  117. packageDifference = "减少套餐"
  118. } else {
  119. packageDifference = "维持套餐"
  120. }
  121. e = company_contract.UpdateCompanyContractPackageDifference(packageDifference, companyContractId)
  122. fmt.Println(packageDifference)
  123. if e != nil {
  124. err = errors.New("UpdateCompanyContractPackageDifference, Err: " + e.Error())
  125. return
  126. }
  127. return
  128. }
  129. // HandleCompanyContractPermissionContractType 更新合同权限表中的权限名称,以及对应的行业权限类型(行业新签、行业续约)
  130. func HandleCompanyContractPermissionContractType(companyContractId int) (err error) {
  131. defer func() {
  132. if err != nil {
  133. fmt.Println(err)
  134. go alarm_msg.SendAlarmMsg(fmt.Sprint("更新合同权限表中的权限名称,以及对应的行业权限类型(行业新签、行业续约)失败,HandleCompanyContractPermissionContractType Err:", err.Error(), "companyContractId", companyContractId), 2)
  135. }
  136. }()
  137. var condition string
  138. var pars []interface{}
  139. condition = " AND company_contract_id = ? "
  140. pars = append(pars, companyContractId)
  141. detail, e := company_contract.GetCompanyContracDetail(condition, pars)
  142. if e != nil {
  143. err = errors.New("GetCompanyContracDetail,detail Err: " + e.Error())
  144. return
  145. }
  146. //获取权限ID与名称的map映射
  147. permissionList, e := chart_permission.GetChartPermissionList()
  148. if e != nil && e.Error() != utils.ErrNoRow() {
  149. err = errors.New("GetChartPermissionList Err: " + e.Error())
  150. return
  151. }
  152. mapChartPermission := make(map[int]string)
  153. for _, v := range permissionList {
  154. mapChartPermission[v.ChartPermissionId] = v.PermissionName
  155. }
  156. companyId := detail.CompanyId
  157. //如果不是续约合同就不做对比处理
  158. pars = make([]interface{}, 0)
  159. condition = " AND company_contract_id != ? AND company_id = ? "
  160. pars = append(pars, companyContractId, companyId)
  161. //获取历史签约信息
  162. companyContractPermissionListHistory, e := company_contract_permission.GetCompanyContractPermissionList(condition, pars)
  163. if e != nil && e.Error() != utils.ErrNoRow() {
  164. err = errors.New("companyContractPermissionListHistory Err: " + e.Error())
  165. return
  166. }
  167. var mapHistoryPerssionName []string
  168. for _, v := range companyContractPermissionListHistory {
  169. mapHistoryPerssionName = append(mapHistoryPerssionName, mapChartPermission[v.ChartPermissionId])
  170. }
  171. pars = make([]interface{}, 0)
  172. condition = " AND company_contract_id = ? "
  173. pars = append(pars, companyContractId)
  174. //获取当前签约的合同信息
  175. companyContractPermissionLisThis, e := company_contract_permission.GetCompanyContractPermissionList(condition, pars)
  176. if e != nil && e.Error() != utils.ErrNoRow() {
  177. err = errors.New("companyContractPermissionLisThis Err: " + e.Error())
  178. return
  179. }
  180. var items []*company_contract_permission.CompanyContractPermission
  181. for _, v := range companyContractPermissionLisThis {
  182. v.PermissionName = mapChartPermission[v.ChartPermissionId]
  183. if v.PermissionName == "" {
  184. continue
  185. }
  186. //已经存在的属于行业续约、不存在的属于行业新签
  187. if utils.InArrayByStr(mapHistoryPerssionName, v.PermissionName) {
  188. v.ContractType = "行业续约"
  189. } else {
  190. v.ContractType = "行业新签"
  191. }
  192. items = append(items, v)
  193. }
  194. //批量更新合同权限表的权限行业名称,以及签约合同时,当前行业类型(行业续约、行业新签)
  195. e = company_contract_permission.UpdateCompanyContractPermissionMulti(items)
  196. if e != nil {
  197. err = errors.New("UpdateCompanyContractPermissionMulti Err: " + e.Error())
  198. return
  199. }
  200. return
  201. }