contract_allocation.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. if detail.ProductId != 2 {
  108. return
  109. }
  110. startYeaar := utils.StrDateToDate(detail.StartDate).Year()
  111. lastYearStartData := fmt.Sprint(startYeaar-1, "-01-01")
  112. lastYearEndData := fmt.Sprint(startYeaar-1, "-12-31")
  113. //如果不是续约合同就不做对比处理
  114. if detail.ContractType != "续约合同" {
  115. return
  116. }
  117. //获取前一年所有的合同列表
  118. pars = make([]interface{}, 0)
  119. condition = " AND company_id = ? AND company_contract_id < ? AND status = 1 AND product_id = ? AND start_date >= ? AND start_date <= ? "
  120. pars = append(pars, detail.CompanyId, companyContractId, detail.ProductId, lastYearStartData, lastYearEndData)
  121. companyContracList, e := company_contract.GetCompanyContracList(condition, pars)
  122. if e != nil {
  123. err = errors.New("GetCompanyContracList Err: " + e.Error())
  124. return
  125. }
  126. var sumMoney float64
  127. var packageDifference string
  128. for _, v := range companyContracList {
  129. sumMoney += v.Money
  130. }
  131. if detail.Money > sumMoney {
  132. packageDifference = "增加套餐"
  133. } else if detail.Money < sumMoney {
  134. packageDifference = "减少套餐"
  135. } else {
  136. packageDifference = "维持套餐"
  137. }
  138. e = company_contract.UpdateCompanyContractPackageDifference(packageDifference, companyContractId)
  139. fmt.Println(packageDifference)
  140. if e != nil {
  141. err = errors.New("UpdateCompanyContractPackageDifference, Err: " + e.Error())
  142. return
  143. }
  144. return
  145. }
  146. // HandleCompanyContractPermissionContractType 更新合同权限表中的权限名称,以及对应的行业权限类型(行业新签、行业续约)
  147. func HandleCompanyContractPermissionContractType(companyContractId int) (err error) {
  148. defer func() {
  149. if err != nil {
  150. fmt.Println(err)
  151. go alarm_msg.SendAlarmMsg(fmt.Sprint("更新合同权限表中的权限名称,以及对应的行业权限类型(行业新签、行业续约)失败,HandleCompanyContractPermissionContractType Err:", err.Error(), "companyContractId", companyContractId), 2)
  152. }
  153. }()
  154. var condition string
  155. var pars []interface{}
  156. condition = " AND company_contract_id = ? "
  157. pars = append(pars, companyContractId)
  158. detail, e := company_contract.GetCompanyContracDetail(condition, pars)
  159. if e != nil {
  160. err = errors.New("GetCompanyContracDetail,detail Err: " + e.Error())
  161. return
  162. }
  163. //获取权限ID与名称的map映射
  164. permissionList, e := chart_permission.GetChartPermissionList()
  165. if e != nil && e.Error() != utils.ErrNoRow() {
  166. err = errors.New("GetChartPermissionList Err: " + e.Error())
  167. return
  168. }
  169. mapChartPermission := make(map[int]string)
  170. for _, v := range permissionList {
  171. mapChartPermission[v.ChartPermissionId] = v.PermissionName
  172. }
  173. companyId := detail.CompanyId
  174. //如果不是续约合同就不做对比处理
  175. pars = make([]interface{}, 0)
  176. condition = " AND company_contract_id != ? AND company_id = ? "
  177. pars = append(pars, companyContractId, companyId)
  178. //获取历史签约信息
  179. companyContractPermissionListHistory, e := company_contract_permission.GetCompanyContractPermissionList(condition, pars)
  180. if e != nil && e.Error() != utils.ErrNoRow() {
  181. err = errors.New("companyContractPermissionListHistory Err: " + e.Error())
  182. return
  183. }
  184. var mapHistoryPerssionName []string
  185. for _, v := range companyContractPermissionListHistory {
  186. mapHistoryPerssionName = append(mapHistoryPerssionName, mapChartPermission[v.ChartPermissionId])
  187. }
  188. pars = make([]interface{}, 0)
  189. condition = " AND company_contract_id = ? "
  190. pars = append(pars, companyContractId)
  191. //获取当前签约的合同信息
  192. companyContractPermissionLisThis, e := company_contract_permission.GetCompanyContractPermissionList(condition, pars)
  193. if e != nil && e.Error() != utils.ErrNoRow() {
  194. err = errors.New("companyContractPermissionLisThis Err: " + e.Error())
  195. return
  196. }
  197. var items []*company_contract_permission.CompanyContractPermission
  198. for _, v := range companyContractPermissionLisThis {
  199. v.PermissionName = mapChartPermission[v.ChartPermissionId]
  200. if v.PermissionName == "" {
  201. continue
  202. }
  203. //已经存在的属于行业续约、不存在的属于行业新签
  204. if utils.InArrayByStr(mapHistoryPerssionName, v.PermissionName) {
  205. v.ContractType = "行业续约"
  206. } else {
  207. v.ContractType = "行业新签"
  208. }
  209. items = append(items, v)
  210. }
  211. //批量更新合同权限表的权限行业名称,以及签约合同时,当前行业类型(行业续约、行业新签)
  212. e = company_contract_permission.UpdateCompanyContractPermissionMulti(items)
  213. if e != nil {
  214. err = errors.New("UpdateCompanyContractPermissionMulti Err: " + e.Error())
  215. return
  216. }
  217. return
  218. }