contract_allocation.go 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985
  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. // HandleCompanyContractPackageDifference 更新与上一份合同的金额的对比 '增加套餐','减少套餐','维持套餐'
  368. func HandleCompanyContractPackageDifference(companyContractId int) (err error) {
  369. defer func() {
  370. if err != nil {
  371. fmt.Println(err)
  372. go alarm_msg.SendAlarmMsg(fmt.Sprint("更新与上一份合同的金额的对比 '增加套餐','减少套餐','维持套餐'失败,Err:", err.Error(), "companyContractId", companyContractId), 2)
  373. }
  374. }()
  375. var condition string
  376. var pars []interface{}
  377. condition = " AND company_contract_id = ? "
  378. pars = append(pars, companyContractId)
  379. detail, e := company.GetCompanyContracDetail(condition, pars)
  380. if e != nil {
  381. err = errors.New("GetCompanyContracDetail,detail Err: " + e.Error())
  382. return
  383. }
  384. //如果不是续约合同就不做对比处理
  385. if detail.ContractType != "续约合同" {
  386. return
  387. }
  388. //获取前一份合同的信息
  389. pars = make([]interface{}, 0)
  390. condition = " AND company_id = ? AND company_contract_id < ? AND status = 1 AND product_id = ? ORDER BY company_contract_id DESC LIMIT 1 "
  391. pars = append(pars, detail.CompanyId, companyContractId, detail.ProductId)
  392. detailPrevious, e := company.GetCompanyContracDetail(condition, pars)
  393. if e != nil {
  394. err = errors.New("GetCompanyContracDetail,detailPrevious Err: " + e.Error())
  395. return
  396. }
  397. var packageDifference string
  398. if detail.Money > detailPrevious.Money {
  399. packageDifference = "增加套餐"
  400. } else if detail.Money < detailPrevious.Money {
  401. packageDifference = "减少套餐"
  402. } else {
  403. packageDifference = "维持套餐"
  404. }
  405. e = company.UpdateCompanyContractPackageDifference(packageDifference, companyContractId)
  406. if e != nil {
  407. err = errors.New("UpdateCompanyContractPackageDifference, Err: " + e.Error())
  408. return
  409. }
  410. return
  411. }
  412. // HandleCompanyContractTypeByRaiXClass 所有x类试用转正的合同都记为 :打分派点
  413. func HandleCompanyContractTypeByRaiXClass(companyContractId int) (err error) {
  414. defer func() {
  415. if err != nil {
  416. go alarm_msg.SendAlarmMsg(fmt.Sprint(" 所有x类试用转正的合同都记为 :打分派点失败,HandleCompanyContractTypeByRaiXClass Err:", err.Error(), "companyContractId", companyContractId), 2)
  417. }
  418. }()
  419. var condition string
  420. var pars []interface{}
  421. condition = " AND company_contract_id = ? "
  422. pars = append(pars, companyContractId)
  423. detail, e := company.GetCompanyContracDetail(condition, pars)
  424. if e != nil {
  425. err = errors.New("GetCompanyContracDetail,detail Err: " + e.Error())
  426. return
  427. }
  428. companyProduct, e := company.GetCompanyProductByCompanyIdAndProductId(detail.CompanyId, utils.COMPANY_PRODUCT_RAI_ID)
  429. if e != nil {
  430. err = errors.New("GetCompanyProductByCompanyIdAndProductId Err: " + e.Error())
  431. return
  432. }
  433. //权益非永续客户不处理
  434. if companyProduct.Status != utils.COMPANY_STATUS_FOREVER {
  435. return
  436. }
  437. e = company.UpdateCompanyContractType("打分派点", companyContractId)
  438. if e != nil {
  439. err = errors.New("UpdateCompanyContractType, Err: " + e.Error())
  440. return
  441. }
  442. return
  443. }
  444. // HandleCompanyContractPermissionContractType 更新合同权限表中的权限名称,以及对应的行业权限类型(行业新签、行业续约)
  445. func HandleCompanyContractPermissionContractType(companyContractId int) (err error) {
  446. defer func() {
  447. if err != nil {
  448. fmt.Println(err)
  449. go alarm_msg.SendAlarmMsg(fmt.Sprint("更新合同权限表中的权限名称,以及对应的行业权限类型(行业新签、行业续约)失败,HandleCompanyContractPermissionContractType 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. //获取权限ID与名称的map映射
  462. permissionList, e := models.GetChartPermissionList()
  463. if e != nil && e.Error() != utils.ErrNoRow() {
  464. err = errors.New("GetChartPermissionList Err: " + e.Error())
  465. return
  466. }
  467. mapChartPermission := make(map[int]string)
  468. for _, v := range permissionList {
  469. mapChartPermission[v.ChartPermissionId] = v.PermissionName
  470. }
  471. companyId := detail.CompanyId
  472. //如果不是续约合同就不做对比处理
  473. pars = make([]interface{}, 0)
  474. condition = " AND company_contract_id != ? AND company_id = ? "
  475. pars = append(pars, companyContractId, companyId)
  476. //获取历史签约信息
  477. companyContractPermissionListHistory, e := company.GetCompanyContractPermissionList(condition, pars)
  478. if e != nil && e.Error() != utils.ErrNoRow() {
  479. err = errors.New("companyContractPermissionListHistory Err: " + e.Error())
  480. return
  481. }
  482. var mapHistoryPerssionName []string
  483. for _, v := range companyContractPermissionListHistory {
  484. mapHistoryPerssionName = append(mapHistoryPerssionName, mapChartPermission[v.ChartPermissionId])
  485. }
  486. pars = make([]interface{}, 0)
  487. condition = " AND company_contract_id = ? "
  488. pars = append(pars, companyContractId)
  489. //获取当前签约的合同信息
  490. companyContractPermissionLisThis, e := company.GetCompanyContractPermissionList(condition, pars)
  491. if e != nil && e.Error() != utils.ErrNoRow() {
  492. err = errors.New("companyContractPermissionLisThis Err: " + e.Error())
  493. return
  494. }
  495. var items []*company.CompanyContractPermission
  496. for _, v := range companyContractPermissionLisThis {
  497. v.PermissionName = mapChartPermission[v.ChartPermissionId]
  498. if v.PermissionName == "" {
  499. continue
  500. }
  501. //已经存在的属于行业续约、不存在的属于行业新签
  502. if utils.InArrayByStr(mapHistoryPerssionName, v.PermissionName) {
  503. v.ContractType = "行业续约"
  504. } else {
  505. v.ContractType = "行业新签"
  506. }
  507. items = append(items, v)
  508. }
  509. //批量更新合同权限表的权限行业名称,以及签约合同时,当前行业类型(行业续约、行业新签)
  510. e = company.UpdateCompanyContractPermissionMulti(items)
  511. if e != nil {
  512. err = errors.New("UpdateCompanyContractPermissionMulti Err: " + e.Error())
  513. return
  514. }
  515. return
  516. }
  517. func initCRM14_4() {
  518. //func init() {
  519. permissionList, e := models.GetChartPermissionList()
  520. if e != nil && e.Error() != utils.ErrNoRow() {
  521. fmt.Println(permissionList)
  522. return
  523. }
  524. mapChartPermission := make(map[int]string)
  525. for _, v := range permissionList {
  526. mapChartPermission[v.ChartPermissionId] = v.PermissionName
  527. }
  528. var condition string
  529. var pars []interface{}
  530. condition = " AND status = 1 AND product_id = 2 "
  531. companyContractList, e := company.GetCompanyContractList(condition, pars)
  532. if e != nil && e.Error() != utils.ErrNoRow() {
  533. fmt.Println(e)
  534. return
  535. }
  536. fmt.Println(len(companyContractList))
  537. for k, v := range companyContractList {
  538. fmt.Println(k)
  539. HandleCompanyContractPermissionContractType(v.CompanyContractId)
  540. }
  541. //var companyContractIds []int
  542. //for _, v := range companyContractList {
  543. // companyContractIds = append(companyContractIds, v.CompanyContractId)
  544. //}
  545. //condition = " AND company_contract_id IN (" + utils.GetOrmInReplace(len(companyContractIds)) + ") "
  546. //pars = append(pars, companyContractIds)
  547. //
  548. //companyContractPermissionList, e := company.GetCompanyContractPermissionList(condition, pars)
  549. //if e != nil && e.Error() != utils.ErrNoRow() {
  550. // fmt.Println(e)
  551. // return
  552. //}
  553. //
  554. //var items []*company.CompanyContractPermission
  555. //
  556. //for _, v := range companyContractPermissionList {
  557. // v.PermissionName = mapChartPermission[v.ChartPermissionId]
  558. //
  559. // items = append(items, v)
  560. //}
  561. //fmt.Println(len(items))
  562. ////return
  563. //e = company.UpdateCompanyContractPermissionMulti(items)
  564. //if e != nil && e.Error() != utils.ErrNoRow() {
  565. // fmt.Println(e)
  566. // return
  567. //}
  568. }
  569. func initCrm13_2() {
  570. var condition string
  571. var pars []interface{}
  572. condition = " AND chart_permission_id = 31 AND `status` IN ('正式','试用') AND product_id = 2 "
  573. //获取所有开通买方研选正式试用的客户列表
  574. listCompanyPermissionYx, e := company.GetCompanyReportPermissionList(condition, pars, 0, 0)
  575. if e != nil && e.Error() != utils.ErrNoRow() {
  576. fmt.Println(e)
  577. return
  578. }
  579. condition = " AND chart_permission_id = 52 "
  580. listCompanyPermissionKouDian, e := company.GetCompanyReportPermissionList(condition, pars, 0, 0)
  581. if e != nil && e.Error() != utils.ErrNoRow() {
  582. fmt.Println(e)
  583. return
  584. }
  585. mapKoud := make(map[int]bool)
  586. for _, v := range listCompanyPermissionKouDian {
  587. mapKoud[v.CompanyId] = true
  588. }
  589. //var i int
  590. var items []*company.CompanyReportPermission
  591. for k, v := range listCompanyPermissionYx {
  592. if mapKoud[v.CompanyId] {
  593. continue
  594. }
  595. permission := new(company.CompanyReportPermission)
  596. permission.CompanyId = v.CompanyId
  597. permission.ReportPermissionId = v.ReportPermissionId
  598. permission.CreatedTime = time.Now()
  599. permission.LastUpdatedTime = time.Now()
  600. permission.ChartPermissionId = 52
  601. permission.StartDate = v.StartDate
  602. permission.EndDate = v.EndDate
  603. permission.ProductId = 2
  604. permission.ProductName = "权益"
  605. permission.ModifyTime = time.Now()
  606. permission.Status = v.Status
  607. err := company.AddCompanyReportPermission(permission)
  608. if err != nil {
  609. return
  610. }
  611. fmt.Println(k)
  612. items = append(items, permission)
  613. }
  614. fmt.Println(len(items))
  615. }
  616. func initCRM_15_4() {
  617. var conditionAllocation string
  618. var parsAllocation []interface{}
  619. 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 "
  620. allocationCompanyContractPermissionList, err := cygx.GetCygxAllocationCompanyContractPermissionList(conditionAllocation, parsAllocation)
  621. if err != nil {
  622. fmt.Println(err)
  623. return
  624. }
  625. var companyContractIds []int
  626. for _, v := range allocationCompanyContractPermissionList {
  627. companyContractIds = append(companyContractIds, v.CompanyContractId)
  628. }
  629. lenCon := len(companyContractIds)
  630. for _, v := range companyContractIds {
  631. UpdateInitAvg(v)
  632. }
  633. fmt.Println("lenCon", lenCon)
  634. return
  635. }
  636. //func init() {
  637. // UpdateInitAvg(1249)
  638. //}
  639. // 合并研选订阅与研选扣点包,为买方研选
  640. func UpdateInit(companyContractId int) {
  641. fmt.Println(companyContractId)
  642. allocationNames := []string{"董衡", "高亦文", "吴昂迪", "冯柯然", "汪洋"}
  643. fmt.Println(allocationNames)
  644. listPerssion, err := cygx.GetCygxAllocationCompanyContractPermissionListByIdInitYx(companyContractId)
  645. if err != nil {
  646. fmt.Println(err)
  647. return
  648. }
  649. //listUser, err := cygx.GetCygxAllocationCompanyContractListById(companyContractId)
  650. //if err != nil {
  651. // fmt.Println(err)
  652. // return
  653. //}
  654. var totalMoney float64
  655. var items []*cygx.CygxAllocationCompanyContract
  656. var itemsPermission []*cygx.CygxAllocationCompanyContractPermission
  657. //var proportionSum float64 // 校验前端传过来的占比使用
  658. var AdminId int
  659. var AdminName string
  660. for _, v := range listPerssion {
  661. totalMoney += v.Money
  662. AdminId = v.AdminId
  663. AdminName = v.AdminName
  664. }
  665. fmt.Println(totalMoney)
  666. //return
  667. itemPermission := new(cygx.CygxAllocationCompanyContractPermission)
  668. itemPermission.CompanyContractId = companyContractId
  669. itemPermission.AdminId = AdminId
  670. itemPermission.AdminName = AdminName
  671. //itemPermission.Proportion = v.Money / totalMoney * 100
  672. itemPermission.Money = totalMoney
  673. itemPermission.ChartPermissionName = utils.CHART_PERMISSION_NAME_MAI_FANG_YANXUAN
  674. itemPermission.CreateTime = time.Now()
  675. itemPermission.ModifyTime = time.Now()
  676. itemsPermission = append(itemsPermission, itemPermission)
  677. for _, v := range allocationNames {
  678. item := new(cygx.CygxAllocationCompanyContract)
  679. item.CompanyContractId = companyContractId
  680. item.AdminId = AdminId
  681. item.AdminName = AdminName
  682. //item.Proportion =
  683. item.Money = totalMoney / 5
  684. item.RealName = v
  685. item.ChartPermissionName = utils.CHART_PERMISSION_NAME_MAI_FANG_YANXUAN
  686. item.CreateTime = time.Now()
  687. item.ModifyTime = time.Now()
  688. items = append(items, item)
  689. }
  690. fmt.Println(len(items))
  691. fmt.Println(len(itemsPermission))
  692. //return
  693. err = cygx.AddAndUpdateCygxAllocationCompanyContractInit(items, itemsPermission, companyContractId)
  694. fmt.Println(err)
  695. }
  696. func UpdateInitAvg(companyContractId int) {
  697. fmt.Println(companyContractId)
  698. listPerssion, err := cygx.GetCygxAllocationCompanyContractPermissionListByIdInit(companyContractId)
  699. if err != nil {
  700. fmt.Println(err)
  701. return
  702. }
  703. listUser, err := cygx.GetCygxAllocationCompanyContractListById(companyContractId)
  704. if err != nil {
  705. fmt.Println(err)
  706. return
  707. }
  708. var totalMoney float64
  709. var items []*cygx.CygxAllocationCompanyContract
  710. var itemsPermission []*cygx.CygxAllocationCompanyContractPermission
  711. //var proportionSum float64 // 校验前端传过来的占比使用
  712. for _, v := range listPerssion {
  713. totalMoney += v.Money
  714. }
  715. moneyAvg := totalMoney / float64(len(listPerssion))
  716. for _, v := range listPerssion {
  717. itemPermission := new(cygx.CygxAllocationCompanyContractPermission)
  718. itemPermission.CompanyContractId = companyContractId
  719. itemPermission.AdminId = v.AdminId
  720. itemPermission.AdminName = v.AdminName
  721. itemPermission.Proportion = v.Money / totalMoney * 100
  722. itemPermission.Money = v.Money
  723. itemPermission.MoneyAvg = moneyAvg
  724. itemPermission.ChartPermissionName = v.ChartPermissionName
  725. itemPermission.CreateTime = time.Now()
  726. itemPermission.ModifyTime = time.Now()
  727. itemsPermission = append(itemsPermission, itemPermission)
  728. fmt.Println(v.Money, "___", itemPermission.Proportion, "___", v.AllocationCompanyContractId)
  729. err = cygx.Cygx_allocation_company_contract_permissionEdit(itemPermission.Proportion, itemPermission.MoneyAvg, v.AllocationCompanyContractId)
  730. //fmt.Println("AllocationCompanyContractId", v.AllocationCompanyContractId)
  731. if err != nil {
  732. fmt.Println(err)
  733. return
  734. }
  735. }
  736. //return
  737. fmt.Println("研究员修改")
  738. //var userProportionSum float64 // 校验前端传过来的占比使用
  739. for _, v2 := range listUser {
  740. item := new(cygx.CygxAllocationCompanyContract)
  741. item.CompanyContractId = companyContractId
  742. item.AdminId = v2.AdminId
  743. item.AdminName = v2.RealName
  744. item.Proportion = v2.Money / totalMoney * 100
  745. item.Money = v2.Money
  746. item.RealName = v2.RealName
  747. item.ChartPermissionName = v2.ChartPermissionName
  748. item.CreateTime = time.Now()
  749. item.ModifyTime = time.Now()
  750. items = append(items, item)
  751. err = cygx.Cygx_allocation_company_contractEdit(item.Proportion, v2.AllocationCompanyContractId)
  752. fmt.Println("AllocationCompanyContractId", v2.AllocationCompanyContractId)
  753. if err != nil {
  754. fmt.Println(err)
  755. return
  756. }
  757. }
  758. //err = cygx.AddAndUpdateCygxAllocationCompanyContractInit(items, itemsPermission, companyContractId)
  759. //fmt.Println(err)
  760. }
  761. // 权益的合同录分之后自动派点
  762. func UpdateCygxallocationCompanyContractByEnterScore(itemsPermissionEnterScore []*cygx.CygxEnterScorePermission, itemsResearcher []*cygx.CygxEnterScoreResearcher, startDate, endDate string, companyId int, sysUser *system.Admin) {
  763. var err error
  764. defer func() {
  765. if err != nil {
  766. fmt.Println(err)
  767. go alarm_msg.SendAlarmMsg("权益的合同录分之后自动派点失败 UpdateCygxallocationCompanyContractByEnterScore ErrMsg:"+err.Error(), 2)
  768. }
  769. }()
  770. var condition string
  771. var pars []interface{}
  772. condition = " AND status = 1 AND product_id = 2 AND company_id = ? AND start_date <= ? AND end_date <= ? "
  773. pars = append(pars, companyId, startDate, endDate)
  774. companyContractList, e := company.GetCompanyContractList(condition, pars)
  775. if e != nil && e.Error() != utils.ErrNoRow() {
  776. err = errors.New("GetCompanyContractList, Err: " + e.Error())
  777. return
  778. }
  779. if len(companyContractList) == 0 {
  780. err = nil
  781. return
  782. }
  783. var companyContractIds []int
  784. //var companyContractIdMoney []float64 // 合同对应的金额
  785. for _, v := range companyContractList {
  786. companyContractIds = append(companyContractIds, v.CompanyContractId)
  787. }
  788. fmt.Println("companyContractIds", companyContractIds)
  789. var items []*cygx.CygxAllocationCompanyContract
  790. var itemsPermission []*cygx.CygxAllocationCompanyContractPermission
  791. var totalProportionPermission float64 //计算总占比
  792. for _, v := range itemsPermissionEnterScore {
  793. totalProportionPermission += v.Proportion
  794. }
  795. //var totalResearcher float64 //计算研究员的总占比
  796. //for _, v := range itemsResearcher {
  797. // totalResearcher += v.Proportion
  798. //}
  799. for _, vContract := range companyContractList {
  800. for _, v := range itemsPermissionEnterScore {
  801. itemPermission := new(cygx.CygxAllocationCompanyContractPermission)
  802. itemPermission.CompanyContractId = vContract.CompanyContractId
  803. itemPermission.AdminId = sysUser.AdminId
  804. itemPermission.AdminName = sysUser.RealName
  805. itemPermission.Proportion = v.Proportion / totalProportionPermission * 100 // 占比转换
  806. itemPermission.Money = v.Proportion * vContract.Money / 10000 / 100 // 单位转成万
  807. itemPermission.MoneyAvg = vContract.Money / 6 // 目前6个行业
  808. itemPermission.ChartPermissionName = v.ChartPermissionName
  809. itemPermission.CreateTime = time.Now()
  810. itemPermission.ModifyTime = time.Now()
  811. itemsPermission = append(itemsPermission, itemPermission)
  812. }
  813. for _, v := range itemsResearcher {
  814. item := new(cygx.CygxAllocationCompanyContract)
  815. item.CompanyContractId = vContract.CompanyContractId
  816. item.AdminId = sysUser.AdminId
  817. item.AdminName = sysUser.RealName
  818. item.Proportion = v.Proportion / totalProportionPermission * 100 // 占比转换
  819. item.Money = v.Proportion * vContract.Money / 10000 / 100 // 单位转成万
  820. item.RealName = v.RealName
  821. item.ChartPermissionName = v.ChartPermissionName
  822. item.CreateTime = time.Now()
  823. item.ModifyTime = time.Now()
  824. items = append(items, item)
  825. }
  826. }
  827. e = cygx.AddAndUpdateCygxAllocationCompanyContractRai(items, itemsPermission, companyContractIds)
  828. if e != nil {
  829. err = errors.New("AddAndUpdateCygxAllocationCompanyContractRai, Err: " + e.Error())
  830. return
  831. }
  832. return
  833. }
  834. //func init15_4_1() {
  835. // UpdateCygxallocationCompanyContractByEnterScoreInit()
  836. // //"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 ;"
  837. //}
  838. func UpdateCygxallocationCompanyContractByEnterScoreInit() {
  839. var condition string
  840. var pars []interface{}
  841. listEnterScore, err := cygx.GetCygxEnterScoreList(condition, pars, 0, 999)
  842. if err != nil {
  843. fmt.Println(err)
  844. return
  845. }
  846. for _, v := range listEnterScore {
  847. fmt.Println("v.EnterScoreId", v.EnterScoreId)
  848. listPermission, err := cygx.GetCygxEnterScorePermissionListById(v.EnterScoreId)
  849. if err != nil {
  850. fmt.Println(err)
  851. return
  852. }
  853. listResearcher, err := cygx.GeCygxEnterScoreResearcherListById(v.EnterScoreId)
  854. if err != nil {
  855. fmt.Println(err)
  856. return
  857. }
  858. sysUser, err := system.GetSysUserById(v.AdminId)
  859. if err != nil {
  860. fmt.Println(err)
  861. return
  862. }
  863. go UpdateCygxallocationCompanyContractByEnterScore(listPermission, listResearcher, v.StartDate, v.EndDate, v.CompanyId, sysUser)
  864. }
  865. fmt.Println("END")
  866. return
  867. }
  868. // 根据公司ID获取近四周之内有决策人互动的客户
  869. func GetCompanyProductIsUserMakerByCompanyIds(companyIds []int) (mapIsUserMakerResp map[int]int) {
  870. var err error
  871. defer func() {
  872. if err != nil {
  873. go alarm_msg.SendAlarmMsg("根据公司ID获取近四周之内有决策人互动的客户失败 GetCompanyProductIsUserMakerByCompanyIds ErrMsg:"+err.Error(), 2)
  874. }
  875. }()
  876. lenArr := len(companyIds)
  877. if lenArr == 0 {
  878. return
  879. }
  880. var condition string
  881. var pars []interface{}
  882. condition += " AND company_id IN (" + utils.GetOrmInReplace(lenArr) + ") AND product_id = 2 AND status IN ('正式','试用','永续') AND company_id > 1 "
  883. pars = append(pars, companyIds)
  884. productList, e := company.GetCompanyProductList(condition, pars)
  885. if e != nil && e.Error() != utils.ErrNoRow() {
  886. err = errors.New("GetCompanyProductList, Err: " + e.Error())
  887. return
  888. }
  889. mapIsUserMakerResp = make(map[int]int)
  890. for _, v := range productList {
  891. mapIsUserMakerResp[v.CompanyId] = v.IsUserMaker
  892. }
  893. return
  894. }