industrial_subject.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. package cygx
  2. import (
  3. "github.com/beego/beego/v2/client/orm"
  4. "hongze/hz_crm_api/utils"
  5. "strconv"
  6. "strings"
  7. "time"
  8. )
  9. type IndustrialSubjectAdd struct {
  10. SubjectName string `description:"标的名称"`
  11. IndustrialManagementId int `description:"产业id"`
  12. Source int `description:"来源,1正常添加,2:通过文章添加,3通过活动添加(默认为1)"`
  13. }
  14. type IndustrialSubjectDelete struct {
  15. IndustrialSubjectId int `orm:"column(industrial_subject_id);pk" description:"标的id"`
  16. }
  17. type CygxIndustrialSubject struct {
  18. IndustrialSubjectId int `orm:"column(industrial_subject_id);pk" description:"标的id"`
  19. IndustrialManagementId int `description:"产业id"`
  20. SubjectName string `description:"标的名称"`
  21. CreateTime time.Time `description:"创建时间"`
  22. Source int `description:"来源,1正常添加,2:通过文章添加,3通过活动添加(默认为1)"`
  23. }
  24. type CygxIndustrialSubjectNum struct {
  25. IndustrialSubjectId int `orm:"column(industrial_subject_id);pk" description:"标的id"`
  26. IndustrialManagementId int `description:"产业id"`
  27. SubjectName string `description:"标的名称"`
  28. CreateTime time.Time `description:"创建时间"`
  29. ArtNum int `description:"文章数量"`
  30. IsRelevance bool `description:"是否存在关联文章"`
  31. }
  32. type GetIndustrialSubjectList struct {
  33. List []*CygxIndustrialSubject
  34. }
  35. type GetIndustrialSubjectNumList struct {
  36. List []*CygxIndustrialSubjectNum
  37. }
  38. func GetcygxIndustrialSubject(industrialManagementId int) (items []*CygxIndustrialSubject, err error) {
  39. o := orm.NewOrmUsingDB("hz_cygx")
  40. sql := `SELECT subject_name FROM cygx_industrial_subject WHERE industrial_management_id = ?`
  41. _, err = o.Raw(sql, industrialManagementId).QueryRows(&items)
  42. return
  43. }
  44. func UpdateIndustrialManagementSubjectNames(industrialManagementId int, industryName string) (err error) {
  45. o := orm.NewOrmUsingDB("hz_cygx")
  46. var nameSub string
  47. listSub, err := GetcygxIndustrialSubject(industrialManagementId)
  48. if err != nil {
  49. return
  50. }
  51. for _, v := range listSub {
  52. nameSub += v.SubjectName + ","
  53. }
  54. nameSub += industryName
  55. sql := `UPDATE cygx_industrial_management SET subject_names = ? WHERE industrial_management_id = ?`
  56. _, err = o.Raw(sql, nameSub, industrialManagementId).Exec()
  57. return
  58. }
  59. // 新增
  60. func AddIndustrialSubject(item *CygxIndustrialSubject) (newId int64, err error) {
  61. o := orm.NewOrmUsingDB("hz_cygx")
  62. newId, err = o.Insert(item)
  63. if err != nil {
  64. return
  65. }
  66. return
  67. }
  68. // 批量新增
  69. func AddIndustrialSubjectList(items []*CygxIndustrialSubject) (newIdStr string, err error) {
  70. o := orm.NewOrmUsingDB("hz_cygx")
  71. to, err := o.Begin()
  72. if err != nil {
  73. return
  74. }
  75. defer func() {
  76. if err != nil {
  77. _ = to.Rollback()
  78. } else {
  79. _ = to.Commit()
  80. }
  81. }()
  82. for _, v := range items {
  83. newId, errSub := to.Insert(v)
  84. newIdStr += strconv.Itoa(int(newId)) + ","
  85. if errSub != nil {
  86. return
  87. }
  88. }
  89. newIdStr = strings.Trim(newIdStr, ",")
  90. return
  91. }
  92. // 列表
  93. func GetIndustrialSubjectAll(IndustrialManagementId int) (items []*CygxIndustrialSubjectNum, err error) {
  94. o := orm.NewOrmUsingDB("hz_cygx")
  95. sql := `SELECT *,
  96. ( SELECT COUNT( 1 ) FROM cygx_industrial_article_group_subject AS sub_g WHERE sub_g.industrial_subject_id = sub.industrial_subject_id ) AS art_num
  97. FROM
  98. cygx_industrial_subject AS sub
  99. WHERE
  100. sub.industrial_management_id = ? `
  101. _, err = o.Raw(sql, IndustrialManagementId).QueryRows(&items)
  102. return
  103. }
  104. // 获取数量
  105. func GetIndustrialSubjectCount(condition string, pars []interface{}) (count int, err error) {
  106. sqlCount := ` SELECT COUNT(1) AS count FROM cygx_industrial_subject WHERE 1=1 `
  107. if condition != "" {
  108. sqlCount += condition
  109. }
  110. o := orm.NewOrmUsingDB("hz_cygx")
  111. err = o.Raw(sqlCount, pars).QueryRow(&count)
  112. return
  113. }
  114. // 修改
  115. func EditIndustrialSubject(item *CygxIndustrialSubject) (err error) {
  116. o := orm.NewOrmUsingDB("hz_cygx")
  117. sql := `UPDATE cygx_industrial_subject SET subject_name=? WHERE industrial_subject_id=? `
  118. _, err = o.Raw(sql, item.SubjectName, item.IndustrialSubjectId).Exec()
  119. return
  120. }
  121. // 删除数据
  122. func DeleteIndustrialSubject(industrialSubjectId int) (err error) {
  123. o := orm.NewOrmUsingDB("hz_cygx")
  124. sql := ` DELETE FROM cygx_industrial_subject WHERE industrial_Subject_id = ?`
  125. _, err = o.Raw(sql, industrialSubjectId).Exec()
  126. return
  127. }
  128. type ArtGroupIndustrialSubjectRep struct {
  129. SubjectName string `description:"标的名称"`
  130. }
  131. type ArtGroupIndustrialSubjectRepList struct {
  132. List []*ArtGroupIndustrialSubjectRep
  133. }
  134. // 获取文章关联的标的数量
  135. func GetArtGroupIndustrialSubject(reportId int) (item []*ArtGroupIndustrialSubjectRep, err error) {
  136. sql := ` SELECT sub.subject_name FROM cygx_industrial_article_group_subject AS sub_g
  137. INNER JOIN cygx_industrial_subject AS sub ON sub.industrial_subject_id = sub_g.industrial_subject_id
  138. WHERE
  139. sub_g.cygx_article_id = ?`
  140. o := orm.NewOrmUsingDB("hz_cygx")
  141. _, err = o.Raw(sql, reportId).QueryRows(&item)
  142. return
  143. }
  144. // 获取标的关联的文章数量
  145. func GetIndustrialSubjectGroupArtCount(industrialSubjectId int) (count int, err error) {
  146. sqlCount := ` SELECT COUNT(1) AS count FROM cygx_industrial_article_group_subject WHERE industrial_subject_id=? `
  147. o := orm.NewOrmUsingDB("hz_cygx")
  148. err = o.Raw(sqlCount, industrialSubjectId).QueryRow(&count)
  149. return
  150. }
  151. // 列表
  152. func GetIndustrialSubjectAllByIds(condition string) (items []*CygxIndustrialSubjectNum, err error) {
  153. o := orm.NewOrmUsingDB("hz_cygx")
  154. sql := `SELECT *,
  155. ( SELECT COUNT( 1 ) FROM cygx_industrial_article_group_subject AS sub_g WHERE sub_g.industrial_subject_id = sub.industrial_subject_id ) AS art_num
  156. FROM
  157. cygx_industrial_subject AS sub
  158. WHERE 1=1 ` + condition
  159. _, err = o.Raw(sql).QueryRows(&items)
  160. return
  161. }
  162. func GetIndustrialSubjectDetailById(industrialSubjectId int) (item *CygxIndustrialSubject, err error) {
  163. o := orm.NewOrmUsingDB("hz_cygx")
  164. sql := `SELECT * FROM cygx_industrial_subject WHERE industrial_subject_id = ? `
  165. err = o.Raw(sql, industrialSubjectId).QueryRow(&item)
  166. return
  167. }
  168. func GetIndustrialSubjectDetailByIds(industrialSubjectIds []int) (list []*CygxIndustrialSubject, err error) {
  169. lenArr := len(industrialSubjectIds)
  170. o := orm.NewOrmUsingDB("hz_cygx")
  171. sql := `SELECT * FROM cygx_industrial_subject WHERE industrial_subject_id IN (` + utils.GetOrmInReplace(lenArr) + `) `
  172. _, err = o.Raw(sql, industrialSubjectIds).QueryRows(&list)
  173. return
  174. }
  175. // 通过名称获取相同标的
  176. func GetListIndustrialSubjectDetailByName(IndustrialSubjectName string) (list []*CygxIndustrialSubject, err error) {
  177. o := orm.NewOrmUsingDB("hz_cygx")
  178. sql := `SELECT * FROM cygx_industrial_subject WHERE subject_name = ? `
  179. _, err = o.Raw(sql, IndustrialSubjectName).QueryRows(&list)
  180. return
  181. }
  182. func GetIndustrialSubjectDetailByName(IndustrialSubjectName string) (item *CygxIndustrialSubject, err error) {
  183. o := orm.NewOrmUsingDB("hz_cygx")
  184. sql := `SELECT * FROM cygx_industrial_subject WHERE subject_name = ? `
  185. err = o.Raw(sql, IndustrialSubjectName).QueryRow(&item)
  186. return
  187. }
  188. // 列表
  189. func GetIndustrialSubjectListName(condition string) (items []*ArtGroupIndustrialSubjectRep, err error) {
  190. o := orm.NewOrmUsingDB("hz_cygx")
  191. sql := `SELECT s.*
  192. FROM
  193. cygx_industrial_subject AS s
  194. INNER JOIN cygx_industrial_management AS m ON m.industrial_management_id =s.industrial_management_id
  195. WHERE m.chart_permission_id = 31 `
  196. if condition != "" {
  197. sql += condition
  198. }
  199. _, err = o.Raw(sql).QueryRows(&items)
  200. return
  201. }
  202. type CygxIndustrialSubjectListRep struct {
  203. IndustrialSubjectId int `description:"标的id"`
  204. IndustrialManagementId int `description:"产业id"`
  205. SubjectName string `description:"标的名称"`
  206. IndustryName string `description:"分析师名称"`
  207. }
  208. type CygxIndustrialSubjectList struct {
  209. List []*CygxIndustrialSubjectListRep
  210. }
  211. type IndustrialNameListRep struct {
  212. List []*SubjectNameListRep
  213. }
  214. type SubjectNameListRep struct {
  215. Name string `description:"产业名称"`
  216. List []*SubjectNameRep
  217. }
  218. type SubjectNameRep struct {
  219. Name string `description:"标的名称"`
  220. }
  221. // 列表
  222. func GetIndustrialSubjectListNameByChartId(condition string) (items []*CygxIndustrialSubjectListRep, err error) {
  223. o := orm.NewOrmUsingDB("hz_cygx")
  224. sql := `SELECT s.*,m.industry_name
  225. FROM
  226. cygx_industrial_subject AS s
  227. INNER JOIN cygx_industrial_management AS m ON m.industrial_management_id =s.industrial_management_id
  228. WHERE 1= 1 `
  229. if condition != "" {
  230. sql += condition
  231. }
  232. sql += ` GROUP BY s.subject_name`
  233. _, err = o.Raw(sql).QueryRows(&items)
  234. return
  235. }
  236. // 通过多个标的ID获取产业名称
  237. func GetindustrialSubjectNames(subjectIds string) (names string, err error) {
  238. sql := ` SELECT
  239. GROUP_CONCAT( DISTINCT s.subject_name SEPARATOR '/' ) AS names
  240. FROM
  241. cygx_industrial_subject AS s
  242. WHERE
  243. industrial_subject_id IN ( ` + subjectIds + `) `
  244. o := orm.NewOrmUsingDB("hz_cygx")
  245. err = o.Raw(sql).QueryRow(&names)
  246. return
  247. }
  248. // 列表
  249. func GetIndustrialSubjectListNameByName(condition string) (items []*CygxIndustrialSubject, err error) {
  250. o := orm.NewOrmUsingDB("hz_cygx")
  251. sql := `SELECT * FROM cygx_industrial_subject WHERE 1= 1`
  252. if condition != "" {
  253. sql += condition
  254. }
  255. _, err = o.Raw(sql).QueryRows(&items)
  256. return
  257. }
  258. // 通过标的名称获取标的关联的ID
  259. func GetIndustrialSubjectIdsByName(subjectName string) (industrialSubjectIds string, err error) {
  260. sql := ` SELECT
  261. GROUP_CONCAT( DISTINCT s.industrial_subject_id SEPARATOR ',' ) AS industrial_subject_ids
  262. FROM
  263. cygx_industrial_subject AS s
  264. WHERE
  265. subject_name =? `
  266. o := orm.NewOrmUsingDB("hz_cygx")
  267. err = o.Raw(sql, subjectName).QueryRow(&industrialSubjectIds)
  268. return
  269. }
  270. // 通过标的名称获取标的关联的ID
  271. func GetIndustrialSubjectIdsByNameLike(subjectName string) (industrialSubjectIds string, err error) {
  272. sql := ` SELECT
  273. GROUP_CONCAT( DISTINCT s.industrial_subject_id SEPARATOR ',' ) AS industrial_subject_ids
  274. FROM
  275. cygx_industrial_subject AS s
  276. WHERE
  277. subject_name LIKE '%` + subjectName + `%'`
  278. o := orm.NewOrmUsingDB("hz_cygx")
  279. err = o.Raw(sql).QueryRow(&industrialSubjectIds)
  280. return
  281. }
  282. type IndustrySubjectCountDetail struct {
  283. ArtTotalNum int `description:"文章总数量"`
  284. List []*IndustrySubjectArtTypeCountList
  285. }
  286. type IndustrySubjectArtTypeCountList struct {
  287. MatchTypeName string `description:"匹配类型"`
  288. ArtNum int `description:"文章数量"`
  289. }
  290. // GetIndustrySubjectArtTypeCountList 获取标的关联的报告数量列表-根据类型
  291. func GetIndustrySubjectArtTypeCountList(subjectId int) (list []*IndustrySubjectArtTypeCountList, err error) {
  292. o := orm.NewOrmUsingDB("hz_cygx")
  293. sql := `SELECT
  294. re.match_type_name,
  295. COUNT(*) AS art_num
  296. FROM
  297. cygx_industrial_subject AS sub
  298. INNER JOIN cygx_industrial_article_group_subject AS gsub ON gsub.industrial_subject_id = sub.industrial_subject_id
  299. INNER JOIN cygx_article AS art ON art.article_id = gsub.article_id
  300. INNER JOIN cygx_report_mapping AS re ON re.category_id = art.category_id
  301. WHERE
  302. sub.industrial_subject_id = ? AND re.match_type_name <> '' AND re.report_type = 2 AND art.publish_status = 1
  303. GROUP BY
  304. re.match_type_name `
  305. sql += `UNION ALL
  306. SELECT
  307. "研选报告",
  308. COUNT(*)
  309. FROM
  310. cygx_industrial_subject AS sub
  311. INNER JOIN cygx_industrial_article_group_subject AS gsub ON gsub.industrial_subject_id = sub.industrial_subject_id
  312. INNER JOIN cygx_article AS art ON art.article_id = gsub.article_id
  313. WHERE
  314. art.article_id > 1000000 AND art.article_type_id != 0 AND sub.industrial_subject_id = ? AND art.publish_status = 1`
  315. _, err = o.Raw(sql, subjectId, subjectId).QueryRows(&list)
  316. return
  317. }
  318. // 列表
  319. func GetIndustrySubjectListByIds(IndustrialSubjectIds string) (items []*CygxIndustrialSubject, err error) {
  320. o := orm.NewOrmUsingDB("hz_cygx")
  321. sql := `SELECT * FROM cygx_industrial_subject WHERE industrial_subject_id IN (` + IndustrialSubjectIds + `) `
  322. _, err = o.Raw(sql).QueryRows(&items)
  323. return
  324. }
  325. // 获取标的列表
  326. func GetCygxIndustrialSubjectListCondition(condition string, pars []interface{}) (items []*CygxIndustrialSubject, err error) {
  327. o := orm.NewOrmUsingDB("hz_cygx")
  328. sql := `SELECT *
  329. FROM
  330. cygx_industrial_subject WHERE 1 = 1 ` + condition
  331. _, err = o.Raw(sql, pars).QueryRows(&items)
  332. return
  333. }