contract_allocation.go 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  1. package cygx
  2. import (
  3. "errors"
  4. "fmt"
  5. "hongze/hz_crm_api/models"
  6. "hongze/hz_crm_api/models/company"
  7. "hongze/hz_crm_api/models/cygx"
  8. "hongze/hz_crm_api/models/system"
  9. "hongze/hz_crm_api/services/alarm_msg"
  10. "hongze/hz_crm_api/utils"
  11. "strconv"
  12. "strings"
  13. "time"
  14. )
  15. //
  16. //func init() {
  17. // fmt.Println(GetCompanyContractPermissionNameMapBymapCompamy(map[int]string{6991: "1138"}))
  18. //}
  19. // GetCompanyContractPermissionNameMapBymapCompamy 获取合并之后的合同所对应的权限种类名称
  20. func GetCompanyContractPermissionNameMapBymapCompamy(mapCompamy map[int]string) (mapContractResp map[int]string, err error) {
  21. if len(mapCompamy) == 0 {
  22. return
  23. }
  24. mapContracIdCompanyId := make(map[int]int) //建立合同ID与公司ID的map对应关系
  25. var companyContractIds []int
  26. //var companyContractIdGroup string //多个合同ID
  27. for k, v := range mapCompamy {
  28. //companyContractIdGroup += v + ","
  29. sliceContract := strings.Split(v, ",")
  30. for _, compamyContracId := range sliceContract {
  31. companyContractIdInt, _ := strconv.Atoi(compamyContracId)
  32. mapContracIdCompanyId[companyContractIdInt] = k
  33. companyContractIds = append(companyContractIds, companyContractIdInt)
  34. }
  35. }
  36. // 获取单个合同所对应的权限名称
  37. contractPermissionNameMap, e := GetCompanyContractPermissionNameMapById(companyContractIds)
  38. if e != nil {
  39. err = errors.New("GetCompanyContractPermissionNameMapById, Err: " + e.Error())
  40. return
  41. }
  42. mapContractResp = make(map[int]string, 0)
  43. for k, v := range mapCompamy {
  44. sliceContract := strings.Split(v, ",")
  45. for _, compamyContracId := range sliceContract {
  46. companyContractIdInt, _ := strconv.Atoi(compamyContracId)
  47. for k2, v2 := range contractPermissionNameMap {
  48. if companyContractIdInt == k2 {
  49. mapContractResp[k] += v2 + ","
  50. }
  51. }
  52. }
  53. }
  54. for k, v := range mapContractResp {
  55. mapContractResp[k] = strings.TrimRight(v, ",")
  56. }
  57. return
  58. }
  59. // GetCompanyContractPermissionNameMapById 获取合并之后的合同所对应的权限种类名称
  60. func GetCompanyContractPermissionNameMapById(companyContractIds []int) (mapContractResp map[int]string, err error) {
  61. //return
  62. lenArr := len(companyContractIds)
  63. if lenArr == 0 {
  64. return
  65. }
  66. var condition string
  67. var pars []interface{}
  68. condition += " AND company_contract_id IN (" + utils.GetOrmInReplace(lenArr) + ")"
  69. pars = append(pars, companyContractIds)
  70. companyContractList, e := company.GetCompanyContractList(condition, pars)
  71. if e != nil && e.Error() != utils.ErrNoRow() {
  72. err = errors.New("GetCompanyContractList, Err: " + e.Error())
  73. return
  74. }
  75. permissionList, e := models.GetChartPermissionList()
  76. if e != nil && e.Error() != utils.ErrNoRow() {
  77. err = errors.New("GetChartPermissionList, Err: " + e.Error())
  78. return
  79. }
  80. mappermissionName := make(map[int]string)
  81. for _, v := range permissionList {
  82. if v.ProductId == 2 {
  83. mappermissionName[v.ChartPermissionId] = v.Remark
  84. }
  85. }
  86. mapContractIdPerssion := make(map[int]string) //单个合同ID所对应的套餐或者权限名称
  87. for _, v := range companyContractList {
  88. if v.RaiPackageType == 1 {
  89. mapContractIdPerssion[v.CompanyContractId] = "70w套餐,"
  90. }
  91. if v.RaiPackageType == 2 {
  92. mapContractIdPerssion[v.CompanyContractId] = "45w套餐,"
  93. }
  94. }
  95. companyContractPermissionList, e := company.GetCompanyContractPermissionList(condition, pars)
  96. if e != nil && e.Error() != utils.ErrNoRow() {
  97. err = errors.New("GetCompanyContractPermissionList, Err: " + e.Error())
  98. return
  99. }
  100. mapIsUpgrade := make(map[string]bool) //合同ID对应的行业是否有升级
  101. for _, v := range companyContractPermissionList {
  102. //如果开通的不是整个套餐,那么就做单独的子权限处理
  103. if strings.Count(mapContractIdPerssion[(v.CompanyContractId)], "w套餐") == 0 {
  104. if v.ChartPermissionId == utils.CHART_PERMISSION_ID_YANXUAN {
  105. if v.ExpensiveYx == 1 {
  106. mapContractIdPerssion[(v.CompanyContractId)] += utils.CHART_PERMISSION_NAME_MF_YANXUAN + "(5w),"
  107. } else if v.ExpensiveYx == 2 {
  108. mapContractIdPerssion[(v.CompanyContractId)] += utils.CHART_PERMISSION_NAME_MF_YANXUAN + "(10w),"
  109. } else {
  110. mapContractIdPerssion[(v.CompanyContractId)] += utils.CHART_PERMISSION_NAME_MF_YANXUAN + ","
  111. }
  112. } else {
  113. mapContractIdPerssion[(v.CompanyContractId)] += mappermissionName[v.ChartPermissionId] + ","
  114. if v.IsUpgrade == 1 {
  115. //合同ID,权限名称形成唯一的主键索引
  116. perssionName := mappermissionName[v.ChartPermissionId]
  117. perssionName = strings.Replace(perssionName, "(客观)", "", -1)
  118. perssionName = strings.Replace(perssionName, "(主观)", "", -1)
  119. mapIsUpgrade[fmt.Sprint(v.CompanyContractId, "perssionName", perssionName)] = true
  120. }
  121. }
  122. } else {
  123. if v.ChartPermissionId == utils.ZHUAN_JIA_ID {
  124. mapContractIdPerssion[(v.CompanyContractId)] += utils.ZHUAN_JIA_NAME + ","
  125. }
  126. if v.ChartPermissionId == utils.CHART_PERMISSION_ID_YANXUAN {
  127. if v.ExpensiveYx == 1 {
  128. mapContractIdPerssion[(v.CompanyContractId)] += utils.CHART_PERMISSION_NAME_MF_YANXUAN + "(5w),"
  129. } else if v.ExpensiveYx == 2 {
  130. mapContractIdPerssion[(v.CompanyContractId)] += utils.CHART_PERMISSION_NAME_MF_YANXUAN + "(10w),"
  131. } else {
  132. mapContractIdPerssion[(v.CompanyContractId)] += utils.CHART_PERMISSION_NAME_MF_YANXUAN + ","
  133. }
  134. }
  135. if v.ChartPermissionId == utils.YAN_XUAN_KOU_DIAN_BAO_ID {
  136. mapContractIdPerssion[(v.CompanyContractId)] += utils.YAN_XUAN_KOU_DIAN_BAO_NAME + ","
  137. }
  138. }
  139. }
  140. //合并客观权限
  141. for k, v := range mapContractIdPerssion {
  142. perssionName := v
  143. if strings.Count(v, utils.YI_YAO_NAME) == 2 {
  144. perssionName = strings.Replace(perssionName, "医药(主观)", "医药", -1)
  145. perssionName = strings.Replace(perssionName, "医药(客观)", "", -1)
  146. }
  147. if strings.Count(v, utils.XIAO_FEI_NAME) == 2 {
  148. perssionName = strings.Replace(perssionName, "消费(主观)", "消费", -1)
  149. perssionName = strings.Replace(perssionName, "消费(客观)", "", -1)
  150. }
  151. if strings.Count(v, utils.KE_JI_NAME) == 2 {
  152. perssionName = strings.Replace(perssionName, "科技(主观)", "科技", -1)
  153. perssionName = strings.Replace(perssionName, "科技(客观)", "", -1)
  154. }
  155. if strings.Count(v, utils.ZHI_ZAO_NAME) == 2 {
  156. perssionName = strings.Replace(perssionName, "智造(主观)", "智造", -1)
  157. perssionName = strings.Replace(perssionName, "智造(客观)", "", -1)
  158. }
  159. perssionName = strings.TrimRight(perssionName, ",")
  160. mapContractIdPerssion[k] = perssionName
  161. }
  162. mapContractResp = make(map[int]string, 0)
  163. //过滤多余的","
  164. for k, v := range mapContractIdPerssion {
  165. sliceName := strings.Split(v, ",")
  166. var nameArr []string
  167. for _, vName := range sliceName {
  168. if vName == "" {
  169. continue
  170. }
  171. if mapIsUpgrade[fmt.Sprint(k, "perssionName", vName)] {
  172. vName += "(升级)"
  173. }
  174. nameArr = append(nameArr, vName)
  175. }
  176. mapContractResp[k] = strings.Join(nameArr, ",")
  177. }
  178. return
  179. }
  180. // GetCompanyPermissionNameStatusMapByCompanyId 通过公司ID,获取公司当下所有权限的状态信息
  181. func GetCompanyPermissionNameStatusMapByCompanyId(companyIds []int) (mapResp map[string]string, err error) {
  182. //return
  183. lenArr := len(companyIds)
  184. if lenArr == 0 {
  185. return
  186. }
  187. var condition string
  188. var pars []interface{}
  189. condition += " AND company_id IN (" + utils.GetOrmInReplace(lenArr) + ") AND product_id = 2 "
  190. pars = append(pars, companyIds)
  191. //获取所有开通买方研选正式试用的客户列表
  192. listCompanyPermission, e := company.GetCompanyReportPermissionList(condition, pars, 0, 0)
  193. if e != nil && e.Error() != utils.ErrNoRow() {
  194. err = errors.New("GetCompanyReportPermissionList, Err: " + e.Error())
  195. return
  196. }
  197. //获取行业权限ID与名称的映射关系
  198. permissionList, e := models.GetChartPermissionList()
  199. if e != nil && e.Error() != utils.ErrNoRow() {
  200. err = errors.New("GetChartPermissionList, Err: " + e.Error())
  201. return
  202. }
  203. mapResp = make(map[string]string, 0)
  204. mapChartPermission := make(map[int]string)
  205. for _, v := range permissionList {
  206. mapChartPermission[v.ChartPermissionId] = v.PermissionName
  207. }
  208. for _, v := range listCompanyPermission {
  209. if mapChartPermission[v.ChartPermissionId] == "" {
  210. continue
  211. }
  212. //公司ID与权限名称组成映射关系
  213. mapResp[fmt.Sprint(v.CompanyId, mapChartPermission[v.ChartPermissionId])] = v.Status
  214. }
  215. return
  216. }
  217. // 判断合同审核通过时间是否超过九十天
  218. func GetMapIsGrayByCompanyContractIds(companyContractIds []int) (mapResp map[int]bool, err error) {
  219. lenArr := len(companyContractIds)
  220. if lenArr == 0 {
  221. return
  222. }
  223. var condition string
  224. var pars []interface{}
  225. pars = make([]interface{}, 0)
  226. condition = " AND company_contract_id IN (" + utils.GetOrmInReplace(lenArr) + ") GROUP BY company_contract_id ORDER BY create_time DESC "
  227. pars = append(pars, companyContractIds)
  228. companyContractPermissionList, e := company.GetCompanyContractPermissionList(condition, pars)
  229. if e != nil && e.Error() != utils.ErrNoRow() {
  230. err = errors.New("GetCompanyContractPermissionList, Err: " + e.Error())
  231. return
  232. }
  233. //超过九十天就置灰
  234. timeInit := time.Now().AddDate(0, 0, -180)
  235. mapResp = make(map[int]bool, 0)
  236. for _, v := range companyContractPermissionList {
  237. if v.CreateTime.Before(timeInit) {
  238. mapResp[v.CompanyContractId] = true
  239. }
  240. }
  241. return
  242. }
  243. //func init3() {
  244. // var condition string
  245. // var pars []interface{}
  246. // //默认只查询权益 2023-06-01 之后的合同
  247. // condition += ` AND c.product_id = ? AND a.start_date > ? `
  248. // pars = append(pars, 2, "2023-06-01")
  249. //
  250. // //列表页数据
  251. // list, err := cygx.GetCompanyContractListJoinCompany(condition, pars, 0, 1000)
  252. // if err != nil {
  253. // fmt.Println(err)
  254. // return
  255. // }
  256. //
  257. // for _, v := range list {
  258. // fmt.Println(v.CompanyContractId)
  259. // HandleAllocationCompanyContractByYanXuan(v.CompanyContractId)
  260. // }
  261. //
  262. // fmt.Println(len(list))
  263. //}
  264. // 如果合同只有研选的时候,自动处理派点
  265. func HandleAllocationCompanyContractByYanXuan(companyContractId int) (err error) {
  266. defer func() {
  267. if err != nil {
  268. fmt.Println(err)
  269. go alarm_msg.SendAlarmMsg(fmt.Sprint("如果合同只有研选的时候,自动处理派点失败,Err:", err.Error(), "companyContractId", companyContractId), 2)
  270. }
  271. }()
  272. var condition string
  273. var pars []interface{}
  274. pars = make([]interface{}, 0)
  275. condition = " AND company_contract_id = ? "
  276. pars = append(pars, companyContractId)
  277. companyContractPermissionList, e := company.GetCompanyContractPermissionList(condition, pars)
  278. if e != nil && e.Error() != utils.ErrNoRow() {
  279. err = errors.New("GetCompanyContractPermissionList, Err: " + e.Error())
  280. return
  281. }
  282. if e != nil && e.Error() != utils.ErrNoRow() {
  283. err = errors.New("GetCygxAllocationCompanyContractPermissionListById, Err: " + e.Error())
  284. return
  285. }
  286. if len(companyContractPermissionList) > 2 {
  287. return
  288. }
  289. var expensiveYxmoney float64
  290. //for _, v := range companyContractPermissionList {
  291. // if v.ChartPermissionId != utils.CHART_PERMISSION_ID_YANXUAN {
  292. // err = errors.New("没有发现研选权限: ")
  293. // return
  294. // }
  295. // if v.ExpensiveYx == 1 {
  296. // expensiveYxmoney = 5
  297. // } else if v.ExpensiveYx == 2 {
  298. // expensiveYxmoney = 10
  299. // } else {
  300. // expensiveYxmoney = 3
  301. // }
  302. //}
  303. var items []*cygx.CygxAllocationCompanyContract
  304. var itemsPermission []*cygx.CygxAllocationCompanyContractPermission
  305. for _, v := range companyContractPermissionList {
  306. if v.ChartPermissionId != utils.CHART_PERMISSION_ID_YANXUAN && v.ChartPermissionId != utils.YAN_XUAN_KOU_DIAN_BAO_ID {
  307. return
  308. }
  309. if v.ChartPermissionId == utils.CHART_PERMISSION_ID_YANXUAN {
  310. expensiveYxmoney = 3
  311. }
  312. if v.ChartPermissionId == utils.YAN_XUAN_KOU_DIAN_BAO_ID {
  313. expensiveYxmoney = 0.2 * v.Points
  314. }
  315. itemPermission := new(cygx.CygxAllocationCompanyContractPermission)
  316. itemPermission.CompanyContractId = companyContractId
  317. //itemPermission.AdminId = sysUser.AdminId
  318. //itemPermission.AdminName = sysUser.RealName
  319. itemPermission.Proportion = 0
  320. itemPermission.Money = expensiveYxmoney
  321. itemPermission.MoneyAvg = 0
  322. itemPermission.ChartPermissionName = v.PermissionName
  323. itemPermission.CreateTime = time.Now()
  324. itemPermission.ModifyTime = time.Now()
  325. itemsPermission = append(itemsPermission, itemPermission)
  326. item := new(cygx.CygxAllocationCompanyContract)
  327. item.CompanyContractId = companyContractId
  328. //item.AdminId = sysUser.AdminId
  329. //item.AdminName = sysUser.RealName
  330. item.Proportion = 0
  331. item.Money = expensiveYxmoney
  332. item.RealName = v.PermissionName
  333. item.ChartPermissionName = v.PermissionName
  334. item.CreateTime = time.Now()
  335. item.ModifyTime = time.Now()
  336. items = append(items, item)
  337. }
  338. if len(items) == 0 {
  339. return
  340. }
  341. e = cygx.AddAndUpdateCygxAllocationCompanyContract(items, itemsPermission, companyContractId)
  342. if e != nil {
  343. err = errors.New("AddAndUpdateCygxAllocationCompanyContract, Err: " + e.Error())
  344. return
  345. }
  346. return
  347. }
  348. //func init() {
  349. // var condition string
  350. // var pars []interface{}
  351. //
  352. // condition = " AND status = 1 "
  353. //
  354. // list, err := company.GetCompanyContractList(condition, pars)
  355. // if err != nil {
  356. // fmt.Println(err)
  357. // return
  358. // }
  359. // fmt.Println(len(list))
  360. // //return
  361. // for _, v := range list {
  362. // fmt.Println(v.CompanyContractId)
  363. // HandleCompanyContractPackageDifference(v.CompanyContractId)
  364. // }
  365. //
  366. //}
  367. //func init() {
  368. // var condition string
  369. // var pars []interface{}
  370. // condition = " AND product_id = 2 AND package_difference != '' "
  371. // companyContracList, e := company.GetCompanyContracList(condition, pars)
  372. // if e != nil {
  373. // fmt.Println(e)
  374. // return
  375. // }
  376. // for k, v := range companyContracList {
  377. // fmt.Println("k", k, "v.CompanyContractId", v.CompanyContractId)
  378. // HandleCompanyContractPackageDifference(v.CompanyContractId)
  379. // }
  380. //
  381. // fmt.Println("endened")
  382. // //condition = " AND company_contract_id = ? "
  383. // //HandleCompanyContractPackageDifference(3038)
  384. //}
  385. // HandleCompanyContractPackageDifference 更新与上一份合同的金额的对比 '增加套餐','减少套餐','维持套餐'
  386. func HandleCompanyContractPackageDifference(companyContractId int) (err error) {
  387. defer func() {
  388. if err != nil {
  389. fmt.Println(err)
  390. go alarm_msg.SendAlarmMsg(fmt.Sprint("更新与上一份合同的金额的对比 '增加套餐','减少套餐','维持套餐'失败,Err:", err.Error(), "companyContractId", companyContractId), 2)
  391. }
  392. }()
  393. var condition string
  394. var pars []interface{}
  395. condition = " AND company_contract_id = ? "
  396. pars = append(pars, companyContractId)
  397. detail, e := company.GetCompanyContracDetail(condition, pars)
  398. if e != nil {
  399. err = errors.New("GetCompanyContracDetail,detail Err: " + e.Error())
  400. return
  401. }
  402. if detail.ProductId != 2 {
  403. return
  404. }
  405. startYeaar := utils.StrDateToDate(detail.StartDate).Year()
  406. lastYearStartData := fmt.Sprint(startYeaar-1, "-01-01")
  407. lastYearEndData := fmt.Sprint(startYeaar-1, "-12-31")
  408. //fmt.Println(lastYearStartData)
  409. //fmt.Println(lastYearEndData)
  410. //return
  411. //如果不是续约合同就不做对比处理
  412. if detail.ContractType != "续约合同" {
  413. return
  414. }
  415. //获取前一年所有的合同列表
  416. pars = make([]interface{}, 0)
  417. condition = " AND company_id = ? AND company_contract_id < ? AND status = 1 AND product_id = ? AND start_date >= ? AND start_date <= ? "
  418. pars = append(pars, detail.CompanyId, companyContractId, detail.ProductId, lastYearStartData, lastYearEndData)
  419. companyContracList, e := company.GetCompanyContracList(condition, pars)
  420. if e != nil {
  421. err = errors.New("GetCompanyContracList Err: " + e.Error())
  422. return
  423. }
  424. var sumMoney float64
  425. var packageDifference string
  426. for _, v := range companyContracList {
  427. sumMoney += v.Money
  428. }
  429. if detail.Money > sumMoney {
  430. packageDifference = "增加套餐"
  431. } else if detail.Money < sumMoney {
  432. packageDifference = "减少套餐"
  433. } else {
  434. packageDifference = "维持套餐"
  435. }
  436. //fmt.Println(packageDifference)
  437. //return
  438. e = company.UpdateCompanyContractPackageDifference(packageDifference, companyContractId)
  439. if e != nil {
  440. err = errors.New("UpdateCompanyContractPackageDifference, Err: " + e.Error())
  441. return
  442. }
  443. return
  444. }
  445. // HandleCompanyContractTypeByRaiXClass 所有x类试用转正的合同都记为 :打分派点
  446. func HandleCompanyContractTypeByRaiXClass(companyContractId int) (err error) {
  447. defer func() {
  448. if err != nil {
  449. go alarm_msg.SendAlarmMsg(fmt.Sprint(" 所有x类试用转正的合同都记为 :打分派点失败,HandleCompanyContractTypeByRaiXClass Err:", err.Error(), "companyContractId", companyContractId), 2)
  450. }
  451. }()
  452. var condition string
  453. var pars []interface{}
  454. condition = " AND company_contract_id = ? "
  455. pars = append(pars, companyContractId)
  456. detail, e := company.GetCompanyContracDetail(condition, pars)
  457. if e != nil {
  458. err = errors.New("GetCompanyContracDetail,detail Err: " + e.Error())
  459. return
  460. }
  461. companyProduct, e := company.GetCompanyProductByCompanyIdAndProductId(detail.CompanyId, utils.COMPANY_PRODUCT_RAI_ID)
  462. if e != nil {
  463. err = errors.New("GetCompanyProductByCompanyIdAndProductId Err: " + e.Error())
  464. return
  465. }
  466. //权益非永续客户不处理
  467. if companyProduct.Status != utils.COMPANY_STATUS_FOREVER {
  468. return
  469. }
  470. e = company.UpdateCompanyContractType("打分派点", companyContractId)
  471. if e != nil {
  472. err = errors.New("UpdateCompanyContractType, Err: " + e.Error())
  473. return
  474. }
  475. return
  476. }
  477. // HandleCompanyContractPermissionContractType 更新合同权限表中的权限名称,以及对应的行业权限类型(行业新签、行业续约)
  478. func HandleCompanyContractPermissionContractType(companyContractId int) (err error) {
  479. defer func() {
  480. if err != nil {
  481. fmt.Println(err)
  482. go alarm_msg.SendAlarmMsg(fmt.Sprint("更新合同权限表中的权限名称,以及对应的行业权限类型(行业新签、行业续约)失败,HandleCompanyContractPermissionContractType Err:", err.Error(), "companyContractId", companyContractId), 2)
  483. }
  484. }()
  485. var condition string
  486. var pars []interface{}
  487. condition = " AND company_contract_id = ? "
  488. pars = append(pars, companyContractId)
  489. detail, e := company.GetCompanyContracDetail(condition, pars)
  490. if e != nil {
  491. err = errors.New("GetCompanyContracDetail,detail Err: " + e.Error())
  492. return
  493. }
  494. //获取权限ID与名称的map映射
  495. permissionList, e := models.GetChartPermissionList()
  496. if e != nil && e.Error() != utils.ErrNoRow() {
  497. err = errors.New("GetChartPermissionList Err: " + e.Error())
  498. return
  499. }
  500. mapChartPermission := make(map[int]string)
  501. for _, v := range permissionList {
  502. mapChartPermission[v.ChartPermissionId] = v.PermissionName
  503. }
  504. companyId := detail.CompanyId
  505. //如果不是续约合同就不做对比处理
  506. pars = make([]interface{}, 0)
  507. condition = " AND company_contract_id != ? AND company_id = ? "
  508. pars = append(pars, companyContractId, companyId)
  509. //获取历史签约信息
  510. companyContractPermissionListHistory, e := company.GetCompanyContractPermissionList(condition, pars)
  511. if e != nil && e.Error() != utils.ErrNoRow() {
  512. err = errors.New("companyContractPermissionListHistory Err: " + e.Error())
  513. return
  514. }
  515. var mapHistoryPerssionName []string
  516. for _, v := range companyContractPermissionListHistory {
  517. mapHistoryPerssionName = append(mapHistoryPerssionName, mapChartPermission[v.ChartPermissionId])
  518. }
  519. pars = make([]interface{}, 0)
  520. condition = " AND company_contract_id = ? "
  521. pars = append(pars, companyContractId)
  522. //获取当前签约的合同信息
  523. companyContractPermissionLisThis, e := company.GetCompanyContractPermissionList(condition, pars)
  524. if e != nil && e.Error() != utils.ErrNoRow() {
  525. err = errors.New("companyContractPermissionLisThis Err: " + e.Error())
  526. return
  527. }
  528. var items []*company.CompanyContractPermission
  529. for _, v := range companyContractPermissionLisThis {
  530. v.PermissionName = mapChartPermission[v.ChartPermissionId]
  531. if v.PermissionName == "" {
  532. continue
  533. }
  534. //已经存在的属于行业续约、不存在的属于行业新签
  535. if utils.InArrayByStr(mapHistoryPerssionName, v.PermissionName) {
  536. v.ContractType = "行业续约"
  537. } else {
  538. v.ContractType = "行业新签"
  539. }
  540. items = append(items, v)
  541. }
  542. //批量更新合同权限表的权限行业名称,以及签约合同时,当前行业类型(行业续约、行业新签)
  543. e = company.UpdateCompanyContractPermissionMulti(items)
  544. if e != nil {
  545. err = errors.New("UpdateCompanyContractPermissionMulti Err: " + e.Error())
  546. return
  547. }
  548. return
  549. }
  550. func initCRM14_4() {
  551. //func init() {
  552. permissionList, e := models.GetChartPermissionList()
  553. if e != nil && e.Error() != utils.ErrNoRow() {
  554. fmt.Println(permissionList)
  555. return
  556. }
  557. mapChartPermission := make(map[int]string)
  558. for _, v := range permissionList {
  559. mapChartPermission[v.ChartPermissionId] = v.PermissionName
  560. }
  561. var condition string
  562. var pars []interface{}
  563. condition = " AND status = 1 AND product_id = 2 "
  564. companyContractList, e := company.GetCompanyContractList(condition, pars)
  565. if e != nil && e.Error() != utils.ErrNoRow() {
  566. fmt.Println(e)
  567. return
  568. }
  569. fmt.Println(len(companyContractList))
  570. for k, v := range companyContractList {
  571. fmt.Println(k)
  572. HandleCompanyContractPermissionContractType(v.CompanyContractId)
  573. }
  574. //var companyContractIds []int
  575. //for _, v := range companyContractList {
  576. // companyContractIds = append(companyContractIds, v.CompanyContractId)
  577. //}
  578. //condition = " AND company_contract_id IN (" + utils.GetOrmInReplace(len(companyContractIds)) + ") "
  579. //pars = append(pars, companyContractIds)
  580. //
  581. //companyContractPermissionList, e := company.GetCompanyContractPermissionList(condition, pars)
  582. //if e != nil && e.Error() != utils.ErrNoRow() {
  583. // fmt.Println(e)
  584. // return
  585. //}
  586. //
  587. //var items []*company.CompanyContractPermission
  588. //
  589. //for _, v := range companyContractPermissionList {
  590. // v.PermissionName = mapChartPermission[v.ChartPermissionId]
  591. //
  592. // items = append(items, v)
  593. //}
  594. //fmt.Println(len(items))
  595. ////return
  596. //e = company.UpdateCompanyContractPermissionMulti(items)
  597. //if e != nil && e.Error() != utils.ErrNoRow() {
  598. // fmt.Println(e)
  599. // return
  600. //}
  601. }
  602. func initCrm13_2() {
  603. var condition string
  604. var pars []interface{}
  605. condition = " AND chart_permission_id = 31 AND `status` IN ('正式','试用') AND product_id = 2 "
  606. //获取所有开通买方研选正式试用的客户列表
  607. listCompanyPermissionYx, e := company.GetCompanyReportPermissionList(condition, pars, 0, 0)
  608. if e != nil && e.Error() != utils.ErrNoRow() {
  609. fmt.Println(e)
  610. return
  611. }
  612. condition = " AND chart_permission_id = 52 "
  613. listCompanyPermissionKouDian, e := company.GetCompanyReportPermissionList(condition, pars, 0, 0)
  614. if e != nil && e.Error() != utils.ErrNoRow() {
  615. fmt.Println(e)
  616. return
  617. }
  618. mapKoud := make(map[int]bool)
  619. for _, v := range listCompanyPermissionKouDian {
  620. mapKoud[v.CompanyId] = true
  621. }
  622. //var i int
  623. var items []*company.CompanyReportPermission
  624. for k, v := range listCompanyPermissionYx {
  625. if mapKoud[v.CompanyId] {
  626. continue
  627. }
  628. permission := new(company.CompanyReportPermission)
  629. permission.CompanyId = v.CompanyId
  630. permission.ReportPermissionId = v.ReportPermissionId
  631. permission.CreatedTime = time.Now()
  632. permission.LastUpdatedTime = time.Now()
  633. permission.ChartPermissionId = 52
  634. permission.StartDate = v.StartDate
  635. permission.EndDate = v.EndDate
  636. permission.ProductId = 2
  637. permission.ProductName = "权益"
  638. permission.ModifyTime = time.Now()
  639. permission.Status = v.Status
  640. err := company.AddCompanyReportPermission(permission)
  641. if err != nil {
  642. return
  643. }
  644. fmt.Println(k)
  645. items = append(items, permission)
  646. }
  647. fmt.Println(len(items))
  648. }
  649. func initCRM_15_4() {
  650. var conditionAllocation string
  651. var parsAllocation []interface{}
  652. conditionAllocation = " AND company_contract_id IN (SELECT company_contract_id FROM cygx_allocation_company_contract_permission WHERE chart_permission_name LIKE '%研选%') GROUP BY company_contract_id "
  653. allocationCompanyContractPermissionList, err := cygx.GetCygxAllocationCompanyContractPermissionList(conditionAllocation, parsAllocation)
  654. if err != nil {
  655. fmt.Println(err)
  656. return
  657. }
  658. var companyContractIds []int
  659. for _, v := range allocationCompanyContractPermissionList {
  660. companyContractIds = append(companyContractIds, v.CompanyContractId)
  661. }
  662. lenCon := len(companyContractIds)
  663. for _, v := range companyContractIds {
  664. UpdateInitAvg(v)
  665. }
  666. fmt.Println("lenCon", lenCon)
  667. return
  668. }
  669. //func init() {
  670. // UpdateInitAvg(1249)
  671. //}
  672. // 合并研选订阅与研选扣点包,为买方研选
  673. func UpdateInit(companyContractId int) {
  674. fmt.Println(companyContractId)
  675. allocationNames := []string{"董衡", "高亦文", "吴昂迪", "冯柯然", "汪洋"}
  676. fmt.Println(allocationNames)
  677. listPerssion, err := cygx.GetCygxAllocationCompanyContractPermissionListByIdInitYx(companyContractId)
  678. if err != nil {
  679. fmt.Println(err)
  680. return
  681. }
  682. //listUser, err := cygx.GetCygxAllocationCompanyContractListById(companyContractId)
  683. //if err != nil {
  684. // fmt.Println(err)
  685. // return
  686. //}
  687. var totalMoney float64
  688. var items []*cygx.CygxAllocationCompanyContract
  689. var itemsPermission []*cygx.CygxAllocationCompanyContractPermission
  690. //var proportionSum float64 // 校验前端传过来的占比使用
  691. var AdminId int
  692. var AdminName string
  693. for _, v := range listPerssion {
  694. totalMoney += v.Money
  695. AdminId = v.AdminId
  696. AdminName = v.AdminName
  697. }
  698. fmt.Println(totalMoney)
  699. //return
  700. itemPermission := new(cygx.CygxAllocationCompanyContractPermission)
  701. itemPermission.CompanyContractId = companyContractId
  702. itemPermission.AdminId = AdminId
  703. itemPermission.AdminName = AdminName
  704. //itemPermission.Proportion = v.Money / totalMoney * 100
  705. itemPermission.Money = totalMoney
  706. itemPermission.ChartPermissionName = utils.CHART_PERMISSION_NAME_MAI_FANG_YANXUAN
  707. itemPermission.CreateTime = time.Now()
  708. itemPermission.ModifyTime = time.Now()
  709. itemsPermission = append(itemsPermission, itemPermission)
  710. for _, v := range allocationNames {
  711. item := new(cygx.CygxAllocationCompanyContract)
  712. item.CompanyContractId = companyContractId
  713. item.AdminId = AdminId
  714. item.AdminName = AdminName
  715. //item.Proportion =
  716. item.Money = totalMoney / 5
  717. item.RealName = v
  718. item.ChartPermissionName = utils.CHART_PERMISSION_NAME_MAI_FANG_YANXUAN
  719. item.CreateTime = time.Now()
  720. item.ModifyTime = time.Now()
  721. items = append(items, item)
  722. }
  723. fmt.Println(len(items))
  724. fmt.Println(len(itemsPermission))
  725. //return
  726. err = cygx.AddAndUpdateCygxAllocationCompanyContractInit(items, itemsPermission, companyContractId)
  727. fmt.Println(err)
  728. }
  729. func UpdateInitAvg(companyContractId int) {
  730. fmt.Println(companyContractId)
  731. listPerssion, err := cygx.GetCygxAllocationCompanyContractPermissionListByIdInit(companyContractId)
  732. if err != nil {
  733. fmt.Println(err)
  734. return
  735. }
  736. listUser, err := cygx.GetCygxAllocationCompanyContractListById(companyContractId)
  737. if err != nil {
  738. fmt.Println(err)
  739. return
  740. }
  741. var totalMoney float64
  742. var items []*cygx.CygxAllocationCompanyContract
  743. var itemsPermission []*cygx.CygxAllocationCompanyContractPermission
  744. //var proportionSum float64 // 校验前端传过来的占比使用
  745. for _, v := range listPerssion {
  746. totalMoney += v.Money
  747. }
  748. moneyAvg := totalMoney / float64(len(listPerssion))
  749. for _, v := range listPerssion {
  750. itemPermission := new(cygx.CygxAllocationCompanyContractPermission)
  751. itemPermission.CompanyContractId = companyContractId
  752. itemPermission.AdminId = v.AdminId
  753. itemPermission.AdminName = v.AdminName
  754. itemPermission.Proportion = v.Money / totalMoney * 100
  755. itemPermission.Money = v.Money
  756. itemPermission.MoneyAvg = moneyAvg
  757. itemPermission.ChartPermissionName = v.ChartPermissionName
  758. itemPermission.CreateTime = time.Now()
  759. itemPermission.ModifyTime = time.Now()
  760. itemsPermission = append(itemsPermission, itemPermission)
  761. fmt.Println(v.Money, "___", itemPermission.Proportion, "___", v.AllocationCompanyContractId)
  762. err = cygx.Cygx_allocation_company_contract_permissionEdit(itemPermission.Proportion, itemPermission.MoneyAvg, v.AllocationCompanyContractId)
  763. //fmt.Println("AllocationCompanyContractId", v.AllocationCompanyContractId)
  764. if err != nil {
  765. fmt.Println(err)
  766. return
  767. }
  768. }
  769. //return
  770. fmt.Println("研究员修改")
  771. //var userProportionSum float64 // 校验前端传过来的占比使用
  772. for _, v2 := range listUser {
  773. item := new(cygx.CygxAllocationCompanyContract)
  774. item.CompanyContractId = companyContractId
  775. item.AdminId = v2.AdminId
  776. item.AdminName = v2.RealName
  777. item.Proportion = v2.Money / totalMoney * 100
  778. item.Money = v2.Money
  779. item.RealName = v2.RealName
  780. item.ChartPermissionName = v2.ChartPermissionName
  781. item.CreateTime = time.Now()
  782. item.ModifyTime = time.Now()
  783. items = append(items, item)
  784. err = cygx.Cygx_allocation_company_contractEdit(item.Proportion, v2.AllocationCompanyContractId)
  785. fmt.Println("AllocationCompanyContractId", v2.AllocationCompanyContractId)
  786. if err != nil {
  787. fmt.Println(err)
  788. return
  789. }
  790. }
  791. //err = cygx.AddAndUpdateCygxAllocationCompanyContractInit(items, itemsPermission, companyContractId)
  792. //fmt.Println(err)
  793. }
  794. // 权益的合同录分之后自动派点
  795. func UpdateCygxallocationCompanyContractByEnterScore(itemsPermissionEnterScore []*cygx.CygxEnterScorePermission, itemsResearcher []*cygx.CygxEnterScoreResearcher, startDate, endDate string, companyId int, sysUser *system.Admin) {
  796. var err error
  797. defer func() {
  798. if err != nil {
  799. fmt.Println(err)
  800. go alarm_msg.SendAlarmMsg("权益的合同录分之后自动派点失败 UpdateCygxallocationCompanyContractByEnterScore ErrMsg:"+err.Error(), 2)
  801. }
  802. }()
  803. var condition string
  804. var pars []interface{}
  805. condition = " AND status = 1 AND product_id = 2 AND company_id = ? AND start_date <= ? AND end_date <= ? "
  806. pars = append(pars, companyId, startDate, endDate)
  807. companyContractList, e := company.GetCompanyContractList(condition, pars)
  808. if e != nil && e.Error() != utils.ErrNoRow() {
  809. err = errors.New("GetCompanyContractList, Err: " + e.Error())
  810. return
  811. }
  812. if len(companyContractList) == 0 {
  813. err = nil
  814. return
  815. }
  816. var companyContractIds []int
  817. //var companyContractIdMoney []float64 // 合同对应的金额
  818. for _, v := range companyContractList {
  819. companyContractIds = append(companyContractIds, v.CompanyContractId)
  820. }
  821. fmt.Println("companyContractIds", companyContractIds)
  822. var items []*cygx.CygxAllocationCompanyContract
  823. var itemsPermission []*cygx.CygxAllocationCompanyContractPermission
  824. var totalProportionPermission float64 //计算总占比
  825. for _, v := range itemsPermissionEnterScore {
  826. totalProportionPermission += v.Proportion
  827. }
  828. //var totalResearcher float64 //计算研究员的总占比
  829. //for _, v := range itemsResearcher {
  830. // totalResearcher += v.Proportion
  831. //}
  832. for _, vContract := range companyContractList {
  833. for _, v := range itemsPermissionEnterScore {
  834. itemPermission := new(cygx.CygxAllocationCompanyContractPermission)
  835. itemPermission.CompanyContractId = vContract.CompanyContractId
  836. itemPermission.AdminId = sysUser.AdminId
  837. itemPermission.AdminName = sysUser.RealName
  838. itemPermission.Proportion = v.Proportion / totalProportionPermission * 100 // 占比转换
  839. itemPermission.Money = v.Proportion * vContract.Money / 10000 / 100 // 单位转成万
  840. itemPermission.MoneyAvg = vContract.Money / 6 // 目前6个行业
  841. itemPermission.ChartPermissionName = v.ChartPermissionName
  842. itemPermission.CreateTime = time.Now()
  843. itemPermission.ModifyTime = time.Now()
  844. itemsPermission = append(itemsPermission, itemPermission)
  845. }
  846. for _, v := range itemsResearcher {
  847. item := new(cygx.CygxAllocationCompanyContract)
  848. item.CompanyContractId = vContract.CompanyContractId
  849. item.AdminId = sysUser.AdminId
  850. item.AdminName = sysUser.RealName
  851. item.Proportion = v.Proportion / totalProportionPermission * 100 // 占比转换
  852. item.Money = v.Proportion * vContract.Money / 10000 / 100 // 单位转成万
  853. item.RealName = v.RealName
  854. item.ChartPermissionName = v.ChartPermissionName
  855. item.CreateTime = time.Now()
  856. item.ModifyTime = time.Now()
  857. items = append(items, item)
  858. }
  859. }
  860. e = cygx.AddAndUpdateCygxAllocationCompanyContractRai(items, itemsPermission, companyContractIds)
  861. if e != nil {
  862. err = errors.New("AddAndUpdateCygxAllocationCompanyContractRai, Err: " + e.Error())
  863. return
  864. }
  865. return
  866. }
  867. //func init15_4_1() {
  868. // UpdateCygxallocationCompanyContractByEnterScoreInit()
  869. // //"SELECT * FROM company_contract WHERE company_id IN (SELECT company_id FROM company_product WHERE product_id = 2 AND init_status = '永续' ) AND product_id = 2 ;"
  870. //}
  871. func UpdateCygxallocationCompanyContractByEnterScoreInit() {
  872. var condition string
  873. var pars []interface{}
  874. listEnterScore, err := cygx.GetCygxEnterScoreList(condition, pars, 0, 999)
  875. if err != nil {
  876. fmt.Println(err)
  877. return
  878. }
  879. for _, v := range listEnterScore {
  880. fmt.Println("v.EnterScoreId", v.EnterScoreId)
  881. listPermission, err := cygx.GetCygxEnterScorePermissionListById(v.EnterScoreId)
  882. if err != nil {
  883. fmt.Println(err)
  884. return
  885. }
  886. listResearcher, err := cygx.GeCygxEnterScoreResearcherListById(v.EnterScoreId)
  887. if err != nil {
  888. fmt.Println(err)
  889. return
  890. }
  891. sysUser, err := system.GetSysUserById(v.AdminId)
  892. if err != nil {
  893. fmt.Println(err)
  894. return
  895. }
  896. go UpdateCygxallocationCompanyContractByEnterScore(listPermission, listResearcher, v.StartDate, v.EndDate, v.CompanyId, sysUser)
  897. }
  898. fmt.Println("END")
  899. return
  900. }
  901. // 根据公司ID获取近四周之内有决策人互动的客户
  902. func GetCompanyProductIsUserMakerByCompanyIds(companyIds []int) (mapIsUserMakerResp map[int]int) {
  903. var err error
  904. defer func() {
  905. if err != nil {
  906. go alarm_msg.SendAlarmMsg("根据公司ID获取近四周之内有决策人互动的客户失败 GetCompanyProductIsUserMakerByCompanyIds ErrMsg:"+err.Error(), 2)
  907. }
  908. }()
  909. lenArr := len(companyIds)
  910. if lenArr == 0 {
  911. return
  912. }
  913. var condition string
  914. var pars []interface{}
  915. condition += " AND company_id IN (" + utils.GetOrmInReplace(lenArr) + ") AND product_id = 2 AND status IN ('正式','试用','永续') AND company_id > 1 "
  916. pars = append(pars, companyIds)
  917. productList, e := company.GetCompanyProductList(condition, pars)
  918. if e != nil && e.Error() != utils.ErrNoRow() {
  919. err = errors.New("GetCompanyProductList, Err: " + e.Error())
  920. return
  921. }
  922. mapIsUserMakerResp = make(map[int]int)
  923. for _, v := range productList {
  924. mapIsUserMakerResp[v.CompanyId] = v.IsUserMaker
  925. }
  926. return
  927. }