allocation_company_contract.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. package cygx
  2. import (
  3. "github.com/beego/beego/v2/client/orm"
  4. "hongze/hz_crm_api/utils"
  5. "time"
  6. )
  7. // 研究员派点
  8. type CygxAllocationCompanyContract struct {
  9. AllocationCompanyContractId int `orm:"column(allocation_company_contract_id);pk"`
  10. CompanyContractId int `description:"合同id"`
  11. CreateTime time.Time `description:"创建时间"`
  12. ModifyTime time.Time `description:"更新时间"`
  13. AdminId int `description:"操作人ID"`
  14. AdminName string `description:"内容"`
  15. Proportion float64 `description:"占比"`
  16. Money float64 `description:"金额"`
  17. RealName string `description:"研究员姓名"`
  18. ChartPermissionName string `description:"行业名称"`
  19. }
  20. // 研究员派点日志
  21. type CygxAllocationCompanyContractLog struct {
  22. AllocationCompanyContractId int `orm:"column(allocation_company_contract_id);pk"`
  23. CompanyContractId int `description:"合同id"`
  24. CreateTime time.Time `description:"创建时间"`
  25. ModifyTime time.Time `description:"更新时间"`
  26. AdminId int `description:"操作人ID"`
  27. AdminName string `description:"内容"`
  28. Proportion float64 `description:"占比"`
  29. Money float64 `description:"金额"`
  30. RealName string `description:"研究员姓名"`
  31. ChartPermissionName string `description:"行业名称"`
  32. }
  33. // 行业派点
  34. type CygxAllocationCompanyContractPermission struct {
  35. AllocationCompanyContractId int `orm:"column(allocation_company_contract_id);pk"`
  36. CompanyContractId int `description:"合同id"`
  37. CreateTime time.Time `description:"创建时间"`
  38. ModifyTime time.Time `description:"更新时间"`
  39. AdminId int `description:"操作人ID"`
  40. AdminName string `description:"内容"`
  41. Proportion float64 `description:"占比"`
  42. Money float64 `description:"金额"`
  43. MoneyAvg float64 `description:"行业所占合同的平均金额"`
  44. ChartPermissionName string `description:"行业名称"`
  45. }
  46. // 行业派点日志
  47. type CygxAllocationCompanyContractPermissionLog struct {
  48. AllocationCompanyContractId int `orm:"column(allocation_company_contract_id);pk"`
  49. CompanyContractId int `description:"合同id"`
  50. CreateTime time.Time `description:"创建时间"`
  51. ModifyTime time.Time `description:"更新时间"`
  52. AdminId int `description:"操作人ID"`
  53. AdminName string `description:"内容"`
  54. Proportion float64 `description:"占比"`
  55. Money float64 `description:"金额"`
  56. MoneyAvg float64 `description:"行业所占合同的平均金额"`
  57. ChartPermissionName string `description:"行业名称"`
  58. }
  59. // 更新派点信息
  60. func AddAndUpdateCygxAllocationCompanyContract(items []*CygxAllocationCompanyContract, itemsPermission []*CygxAllocationCompanyContractPermission, companyContractId int) (err error) {
  61. o := orm.NewOrmUsingDB("hz_cygx")
  62. to, err := o.Begin()
  63. if err != nil {
  64. return
  65. }
  66. defer func() {
  67. if err != nil {
  68. _ = to.Rollback()
  69. } else {
  70. _ = to.Commit()
  71. }
  72. }()
  73. //组合研究员派点日志结构体
  74. var itemsLog []*CygxAllocationCompanyContractLog
  75. for _, v := range items {
  76. item := new(CygxAllocationCompanyContractLog)
  77. item.CompanyContractId = v.CompanyContractId
  78. item.AdminId = v.AdminId
  79. item.CompanyContractId = v.CompanyContractId
  80. item.AdminName = v.AdminName
  81. item.RealName = v.RealName
  82. item.ChartPermissionName = v.ChartPermissionName
  83. item.Proportion = v.Proportion
  84. item.Money = v.Money
  85. item.CreateTime = time.Now()
  86. item.ModifyTime = time.Now()
  87. itemsLog = append(itemsLog, item)
  88. }
  89. //组合行业派点日志结构体
  90. var itemsPermissionLog []*CygxAllocationCompanyContractPermissionLog
  91. for _, v := range itemsPermission {
  92. item := new(CygxAllocationCompanyContractPermissionLog)
  93. item.CompanyContractId = v.CompanyContractId
  94. item.AdminId = v.AdminId
  95. item.CompanyContractId = v.CompanyContractId
  96. item.AdminName = v.AdminName
  97. item.ChartPermissionName = v.ChartPermissionName
  98. item.Proportion = v.Proportion
  99. item.Money = v.Money
  100. item.MoneyAvg = v.MoneyAvg
  101. item.CreateTime = time.Now()
  102. item.ModifyTime = time.Now()
  103. itemsPermissionLog = append(itemsPermissionLog, item)
  104. }
  105. //删除原有的研究员派点信息
  106. sql := `DELETE FROM cygx_allocation_company_contract WHERE company_contract_id = ?`
  107. _, err = to.Raw(sql, companyContractId).Exec()
  108. if err != nil {
  109. return
  110. }
  111. //删除原有的行业派点信息
  112. sql = ` DELETE FROM cygx_allocation_company_contract_permission WHERE company_contract_id = ?`
  113. _, err = to.Raw(sql, companyContractId).Exec()
  114. if err != nil {
  115. return
  116. }
  117. //批量添加研究员派点信息
  118. _, err = to.InsertMulti(len(items), items)
  119. if err != nil {
  120. return
  121. }
  122. _, err = to.InsertMulti(len(itemsLog), itemsLog)
  123. if err != nil {
  124. return
  125. }
  126. //批量添加行业派点信息
  127. _, err = to.InsertMulti(len(itemsPermission), itemsPermission)
  128. if err != nil {
  129. return
  130. }
  131. _, err = to.InsertMulti(len(itemsPermissionLog), itemsPermissionLog)
  132. if err != nil {
  133. return
  134. }
  135. ow := orm.NewOrm()
  136. sqlW := `UPDATE company_contract SET is_allocation=1 WHERE company_contract_id=? `
  137. _, err = ow.Raw(sqlW, companyContractId).Exec()
  138. return
  139. }
  140. // 更新派点信息
  141. func AddAndUpdateCygxAllocationCompanyContractRai(items []*CygxAllocationCompanyContract, itemsPermission []*CygxAllocationCompanyContractPermission, companyContractId []int) (err error) {
  142. o := orm.NewOrmUsingDB("hz_cygx")
  143. to, err := o.Begin()
  144. if err != nil {
  145. return
  146. }
  147. defer func() {
  148. if err != nil {
  149. _ = to.Rollback()
  150. } else {
  151. _ = to.Commit()
  152. }
  153. }()
  154. //组合研究员派点日志结构体
  155. var itemsLog []*CygxAllocationCompanyContractLog
  156. for _, v := range items {
  157. item := new(CygxAllocationCompanyContractLog)
  158. item.CompanyContractId = v.CompanyContractId
  159. item.AdminId = v.AdminId
  160. item.CompanyContractId = v.CompanyContractId
  161. item.AdminName = v.AdminName
  162. item.RealName = v.RealName
  163. item.ChartPermissionName = v.ChartPermissionName
  164. item.Proportion = v.Proportion
  165. item.Money = v.Money
  166. item.CreateTime = time.Now()
  167. item.ModifyTime = time.Now()
  168. itemsLog = append(itemsLog, item)
  169. }
  170. //组合行业派点日志结构体
  171. var itemsPermissionLog []*CygxAllocationCompanyContractPermissionLog
  172. for _, v := range itemsPermission {
  173. item := new(CygxAllocationCompanyContractPermissionLog)
  174. item.CompanyContractId = v.CompanyContractId
  175. item.AdminId = v.AdminId
  176. item.CompanyContractId = v.CompanyContractId
  177. item.AdminName = v.AdminName
  178. item.ChartPermissionName = v.ChartPermissionName
  179. item.Proportion = v.Proportion
  180. item.Money = v.Money
  181. item.MoneyAvg = v.MoneyAvg
  182. item.CreateTime = time.Now()
  183. item.ModifyTime = time.Now()
  184. itemsPermissionLog = append(itemsPermissionLog, item)
  185. }
  186. lenIds := len(companyContractId)
  187. //删除原有的研究员派点信息
  188. sql := `DELETE FROM cygx_allocation_company_contract WHERE company_contract_id IN (` + utils.GetOrmInReplace(lenIds) + `)`
  189. _, err = to.Raw(sql, companyContractId).Exec()
  190. if err != nil {
  191. return
  192. }
  193. //删除原有的行业派点信息
  194. sql = ` DELETE FROM cygx_allocation_company_contract_permission WHERE company_contract_id IN (` + utils.GetOrmInReplace(lenIds) + `)`
  195. _, err = to.Raw(sql, companyContractId).Exec()
  196. if err != nil {
  197. return
  198. }
  199. //批量添加研究员派点信息
  200. _, err = to.InsertMulti(len(items), items)
  201. if err != nil {
  202. return
  203. }
  204. _, err = to.InsertMulti(len(itemsLog), itemsLog)
  205. if err != nil {
  206. return
  207. }
  208. //批量添加行业派点信息
  209. _, err = to.InsertMulti(len(itemsPermission), itemsPermission)
  210. if err != nil {
  211. return
  212. }
  213. _, err = to.InsertMulti(len(itemsPermissionLog), itemsPermissionLog)
  214. if err != nil {
  215. return
  216. }
  217. ow := orm.NewOrm()
  218. sqlW := `UPDATE company_contract SET is_allocation=1 ,allocation_type = 1 WHERE company_contract_id IN (` + utils.GetOrmInReplace(lenIds) + `)`
  219. _, err = ow.Raw(sqlW, companyContractId).Exec()
  220. return
  221. }
  222. type UpdateAllocationCompanyContractReq struct {
  223. CompanyContractId int `description:"合同ID"`
  224. List []*AllocationPermissionListResp
  225. }
  226. type CygxAllocationCompanyContractDetailResp struct {
  227. CompanyContractId int `description:"合同ID"`
  228. Money float64 `description:"金额(单位万)"`
  229. TotalPointsContent string `description:"总点数描述"`
  230. IsGray bool `description:"是否置灰"`
  231. IsXClass bool `description:"是否是X类试用客户"`
  232. AllocationType int `description:"分配方式,0手动分配,1X录分之后自动分配,默认0"`
  233. List []*AllocationPermissionListResp
  234. }
  235. // 行业
  236. type AllocationPermissionListResp struct {
  237. ChartPermissionName string `description:"行业名称"`
  238. ChartPermissionId int `description:"行业id"`
  239. Proportion float64 `description:"占比"`
  240. Money float64 `description:"金额(单位万)"`
  241. MoneyAvg float64 `description:"行业所占合同的平均金额"`
  242. CompanyContractId int `description:"合同ID"`
  243. List []*AllocationRealNameListResp
  244. }
  245. // 行业
  246. type AllocationRealNameListResp struct {
  247. RealName string `description:"研究员姓名"`
  248. Proportion float64 `description:"占比"`
  249. Money float64 `description:"金额(单位万)"`
  250. ChartPermissionId int `description:"行业id"`
  251. }
  252. // 获取数量
  253. func GetCygxAllocationCompanyContractCountByCompanyContractId(companyContractId int) (count int, err error) {
  254. o := orm.NewOrmUsingDB("hz_cygx")
  255. sqlCount := ` SELECT COUNT(1) AS count FROM cygx_allocation_company_contract WHERE company_contract_id = ? `
  256. err = o.Raw(sqlCount, companyContractId).QueryRow(&count)
  257. return
  258. }
  259. // 研究员列表
  260. func GetCygxAllocationCompanyContractListById(companyContractId int) (items []*CygxAllocationCompanyContract, err error) {
  261. o := orm.NewOrmUsingDB("hz_cygx")
  262. sql := `SELECT * FROM cygx_allocation_company_contract WHERE company_contract_id = ? `
  263. _, err = o.Raw(sql, companyContractId).QueryRows(&items)
  264. return
  265. }
  266. // 研究员列表
  267. func GetCygxAllocationCompanyContractList(condition string, pars []interface{}) (items []*CygxAllocationCompanyContract, err error) {
  268. o := orm.NewOrmUsingDB("hz_cygx")
  269. sql := `SELECT * FROM cygx_allocation_company_contract as art WHERE 1= 1 `
  270. if condition != "" {
  271. sql += condition
  272. }
  273. _, err = o.Raw(sql, pars).QueryRows(&items)
  274. return
  275. }
  276. // 行业列表
  277. func GetCygxAllocationCompanyContractPermissionListById(companyContractId int) (items []*AllocationPermissionListResp, err error) {
  278. o := orm.NewOrmUsingDB("hz_cygx")
  279. sql := ` SELECT * FROM cygx_allocation_company_contract_permission WHERE company_contract_id = ? `
  280. _, err = o.Raw(sql, companyContractId).QueryRows(&items)
  281. return
  282. }
  283. // 行业列表
  284. func GetCygxAllocationCompanyContractPermissionList(condition string, pars []interface{}) (items []*AllocationPermissionListResp, err error) {
  285. o := orm.NewOrmUsingDB("hz_cygx")
  286. sql := ` SELECT * FROM cygx_allocation_company_contract_permission as art WHERE 1= 1 `
  287. if condition != "" {
  288. sql += condition
  289. }
  290. _, err = o.Raw(sql, pars).QueryRows(&items)
  291. return
  292. }
  293. type CygxAllocationCompanyContractDetailStatisticsResp struct {
  294. List []*AllocationPermissionStatisticsListResp
  295. TotalContract int `description:"关联合同总计"`
  296. TotalMoney float64 `description:"总派点总计"`
  297. }
  298. // 行业
  299. type AllocationPermissionStatisticsListResp struct {
  300. ChartPermissionName string `description:"行业名称"`
  301. List []*AllocationRealNameStatisticsListResp
  302. }
  303. // 行业
  304. type AllocationRealNameStatisticsListResp struct {
  305. RealName string `description:"研究员姓名"`
  306. TotalRelatedContract float64 `description:"关联合同"`
  307. TotalDispatchPoint string `description:"总派点"`
  308. GroupProportion string `description:"组内占比"`
  309. DepartmentProportion string `description:"部门占比"`
  310. }
  311. // 行业列表
  312. func GetCygxAllocationCompanyContractPermissionListByIdInitYx(companyContractId int) (items []*CygxAllocationCompanyContractPermission, err error) {
  313. o := orm.NewOrmUsingDB("hz_cygx")
  314. sql := ` SELECT * FROM cygx_allocation_company_contract_permission WHERE company_contract_id = ? AND chart_permission_name IN ('研选扣点包','研选订阅','买方研选') `
  315. _, err = o.Raw(sql, companyContractId).QueryRows(&items)
  316. return
  317. }
  318. // 行业列表
  319. func GetCygxAllocationCompanyContractPermissionListByIdInit(companyContractId int) (items []*CygxAllocationCompanyContractPermission, err error) {
  320. o := orm.NewOrmUsingDB("hz_cygx")
  321. sql := ` SELECT * FROM cygx_allocation_company_contract_permission WHERE company_contract_id = ? `
  322. _, err = o.Raw(sql, companyContractId).QueryRows(&items)
  323. return
  324. }
  325. // 更新派点信息
  326. func AddAndUpdateCygxAllocationCompanyContractInit(items []*CygxAllocationCompanyContract, itemsPermission []*CygxAllocationCompanyContractPermission, companyContractId int) (err error) {
  327. o := orm.NewOrmUsingDB("hz_cygx")
  328. to, err := o.Begin()
  329. if err != nil {
  330. return
  331. }
  332. defer func() {
  333. if err != nil {
  334. _ = to.Rollback()
  335. } else {
  336. _ = to.Commit()
  337. }
  338. }()
  339. //删除原有的研究员派点信息
  340. sql := `DELETE FROM cygx_allocation_company_contract WHERE company_contract_id = ? AND chart_permission_name IN ('研选扣点包','研选订阅','买方研选') `
  341. _, err = to.Raw(sql, companyContractId).Exec()
  342. if err != nil {
  343. return
  344. }
  345. //删除原有的行业派点信息
  346. sql = ` DELETE FROM cygx_allocation_company_contract_permission WHERE company_contract_id = ? AND chart_permission_name IN ('研选扣点包','研选订阅','买方研选') `
  347. _, err = to.Raw(sql, companyContractId).Exec()
  348. if err != nil {
  349. return
  350. }
  351. //批量添加研究员派点信息
  352. _, err = to.InsertMulti(len(items), items)
  353. if err != nil {
  354. return
  355. }
  356. //批量添加行业派点信息
  357. _, err = to.InsertMulti(len(itemsPermission), itemsPermission)
  358. if err != nil {
  359. return
  360. }
  361. return
  362. }
  363. // 修改
  364. func Cygx_allocation_company_contractEdit(proportion float64, allocation_company_contract_id int) (err error) {
  365. o := orm.NewOrmUsingDB("hz_cygx")
  366. sql := `UPDATE cygx_allocation_company_contract SET proportion =? WHERE allocation_company_contract_id=? `
  367. _, err = o.Raw(sql, proportion, allocation_company_contract_id).Exec()
  368. return
  369. }
  370. // 修改
  371. func Cygx_allocation_company_contract_permissionEdit(proportion, moneyAvg float64, allocation_company_contract_id int) (err error) {
  372. o := orm.NewOrmUsingDB("hz_cygx")
  373. sql := `UPDATE cygx_allocation_company_contract_permission SET proportion =?,money_avg = ? WHERE allocation_company_contract_id=? `
  374. _, err = o.Raw(sql, proportion, moneyAvg, allocation_company_contract_id).Exec()
  375. return
  376. }