contract_allocation.go 8.2 KB

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