my_chart.go 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  1. package data_manage
  2. import (
  3. "eta/eta_api/utils"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "github.com/rdlucklib/rdluck_tools/paging"
  7. "time"
  8. )
  9. type ChartListResp struct {
  10. Paging *paging.PagingItem
  11. List []*ChartInfoView
  12. }
  13. func GetChartListByCondition(condition string, pars []interface{}, startSize, pageSize int) (item []*ChartInfoView, err error) {
  14. o := orm.NewOrmUsingDB("data")
  15. sql := ` SELECT * FROM chart_info WHERE 1=1 `
  16. if condition != "" {
  17. sql += condition
  18. }
  19. //sql += " ORDER BY sort ASC,chart_info_id DESC LIMIT ?,? "
  20. sql += " ORDER BY create_time DESC LIMIT ?,? "
  21. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&item)
  22. return
  23. }
  24. func GetChartListCountByCondition(condition string, pars []interface{}) (count int, err error) {
  25. o := orm.NewOrmUsingDB("data")
  26. sql := ` SELECT COUNT(1) AS count FROM chart_info WHERE 1=1 `
  27. if condition != "" {
  28. sql += condition
  29. }
  30. err = o.Raw(sql, pars).QueryRow(&count)
  31. return
  32. }
  33. type MyChartClassify struct {
  34. MyChartClassifyId int `orm:"column(my_chart_classify_id);pk"`
  35. MyChartClassifyName string
  36. Sort int `json:"-" description:"排序字段,值越小,排的越靠前"`
  37. AdminId int `description:"创建人id"`
  38. IsPublic int `description:"是否公共分类"`
  39. IsCompanyPublic int `description:"是否为用户公共分类"`
  40. CreateTime time.Time `json:"-"`
  41. ModifyTime time.Time `json:"-"`
  42. PublicTime time.Time `json:"-" description:"公开(分享)时间"`
  43. CompanyPublicTime time.Time `json:"-" description:"用户可见公开时间"`
  44. }
  45. func AddMyChartClassify(item *MyChartClassify) (err error) {
  46. o := orm.NewOrmUsingDB("data")
  47. _, err = o.Insert(item)
  48. return
  49. }
  50. func GetMyChartClassifyAll(adminId int) (item []*MyChartClassify, err error) {
  51. o := orm.NewOrmUsingDB("data")
  52. sql := ` SELECT * FROM my_chart_classify WHERE 1=1 AND admin_id=? `
  53. sql += " ORDER BY sort asc, my_chart_classify_id asc "
  54. _, err = o.Raw(sql, adminId).QueryRows(&item)
  55. return
  56. }
  57. func GetPublicChartClassifyAllExceptMy(adminId int) (item []*MyChartClassify, err error) {
  58. o := orm.NewOrmUsingDB("data")
  59. sql := ` SELECT * FROM my_chart_classify WHERE 1=1 AND is_public=1 AND admin_id !=?`
  60. sql += " ORDER BY public_time asc"
  61. _, err = o.Raw(sql, adminId).QueryRows(&item)
  62. return
  63. }
  64. type MyChartClassifyResp struct {
  65. List []*MyChartClassify
  66. Language string `description:"指标的展示语言,CN:中文,EN:英文"`
  67. }
  68. // PublicChartClassifyResp 公共分类返回数据结构体
  69. type PublicChartClassifyResp struct {
  70. List []PublicChartClassifyItem
  71. Language string `description:"指标的展示语言,CN:中文,EN:英文"`
  72. }
  73. // PublicChartClassifyItem 公共分类结构体
  74. type PublicChartClassifyItem struct {
  75. MyChartClassifyId int `description:"分类id"`
  76. MyChartClassifyName string `description:"分类名称"`
  77. AdminId int `description:"创建人id"`
  78. RealName string `description:"创建人名称"`
  79. IsPublic int `description:"是否公共分类"`
  80. IsCompanyPublic int `description:"是否为客户可见"`
  81. }
  82. type MyChartClassifyAddReq struct {
  83. MyChartClassifyName string `description:"分类名称"`
  84. }
  85. func GetMyChartClassifyCountByCondition(condition string, pars []interface{}) (count int, err error) {
  86. o := orm.NewOrmUsingDB("data")
  87. sql := ` SELECT COUNT(1) AS count FROM my_chart_classify WHERE 1=1 `
  88. if condition != "" {
  89. sql += condition
  90. }
  91. err = o.Raw(sql, pars).QueryRow(&count)
  92. return
  93. }
  94. func GetMyChartClassifyById(adminId, myChartClassifyId int) (item *MyChartClassify, err error) {
  95. o := orm.NewOrmUsingDB("data")
  96. sql := ` SELECT * FROM my_chart_classify WHERE admin_id=? AND my_chart_classify_id=? `
  97. err = o.Raw(sql, adminId, myChartClassifyId).QueryRow(&item)
  98. return
  99. }
  100. // GetMyChartClassifyMastSort 获取MY ETA 分类中排序最大的值
  101. func GetMyChartClassifyMastSort(adminId int) (sort int, err error) {
  102. o := orm.NewOrmUsingDB("data")
  103. sql := ` SELECT MAX(sort) sort FROM my_chart_classify WHERE admin_id=? `
  104. err = o.Raw(sql, adminId).QueryRow(&sort)
  105. return
  106. }
  107. // GetMyChartClassifyMinSort 获取MY ETA 分类中排序最小的值
  108. func GetMyChartClassifyMinSort(adminId int) (sort int, err error) {
  109. o := orm.NewOrmUsingDB("data")
  110. sql := ` SELECT MIN(sort) sort FROM my_chart_classify WHERE admin_id=? `
  111. err = o.Raw(sql, adminId).QueryRow(&sort)
  112. return
  113. }
  114. type MyChartClassifyEditReq struct {
  115. MyChartClassifyId int `description:"分类ID"`
  116. MyChartClassifyName string `description:"分类名称"`
  117. }
  118. // MyChartClassifyMoveReq 移动分类请求参数
  119. type MyChartClassifyMoveReq struct {
  120. MyChartClassifyId int `description:"分类id"`
  121. PrevClassifyId int `description:"上一个兄弟节点分类id"`
  122. NextClassifyId int `description:"下一个兄弟节点分类id"`
  123. }
  124. func ModifyMyChartClassify(myChartClassifyId int, myChartClassifyName string) (err error) {
  125. o := orm.NewOrmUsingDB("data")
  126. sql := `UPDATE my_chart_classify SET my_chart_classify_name=?,modify_time=NOW() WHERE my_chart_classify_id=?`
  127. _, err = o.Raw(sql, myChartClassifyName, myChartClassifyId).Exec()
  128. return
  129. }
  130. type MyChartClassifyDeleteReq struct {
  131. MyChartClassifyId int `description:"分类ID"`
  132. }
  133. func DeleteMyChartClassify(myChartClassifyId int) (err error) {
  134. o := orm.NewOrmUsingDB("data")
  135. //o.Begin()
  136. //
  137. //defer func() {
  138. // if err != nil {
  139. // o.Rollback()
  140. // } else {
  141. // o.Commit()
  142. // }
  143. //}()
  144. sql := `DELETE FROM my_chart_classify WHERE my_chart_classify_id=?`
  145. _, err = o.Raw(sql, myChartClassifyId).Exec()
  146. if err != nil {
  147. return err
  148. }
  149. mappingList := make([]*MyChartClassifyMapping, 0)
  150. sql = `SELECT * FROM my_chart_classify_mapping WHERE my_chart_classify_id=?`
  151. _, err = o.Raw(sql, myChartClassifyId).QueryRows(&mappingList)
  152. sql = `DELETE FROM my_chart_classify_mapping WHERE my_chart_classify_id=?`
  153. _, err = o.Raw(sql, myChartClassifyId).Exec()
  154. if err != nil {
  155. return err
  156. }
  157. for _, v := range mappingList {
  158. var count int
  159. sql = ` SELECT COUNT(1) AS count FROM my_chart_classify_mapping WHERE my_chart_id=?`
  160. err = o.Raw(sql, v.MyChartId).QueryRow(&count)
  161. if err != nil {
  162. return err
  163. }
  164. if count <= 0 {
  165. sql = `DELETE FROM my_chart WHERE my_chart_id=?`
  166. _, err = o.Raw(sql, v.MyChartId).Exec()
  167. if err != nil {
  168. return err
  169. }
  170. }
  171. }
  172. return
  173. }
  174. type MyChartAddReq struct {
  175. ChartInfoId int `description:"图表id"`
  176. MyChartClassifyId []int `description:"分类id,数组形式"`
  177. }
  178. type MyChartAddResp struct {
  179. MyChartInfoId int `description:"图表id"`
  180. MyChartClassifyId string `description:"分类id,多个用英文逗号隔开"`
  181. }
  182. type MyChart struct {
  183. MyChartId int `orm:"column(my_chart_id);pk"`
  184. ChartInfoId int `description:"图表id"`
  185. Source int `description:"1:ETA图库;2:商品价格"`
  186. AdminId int `description:"用户id"`
  187. CreateTime time.Time `description:"创建时间"`
  188. ModifyTime time.Time `description:"修改时间"`
  189. }
  190. type MyChartView struct {
  191. MyChartId int `orm:"column(my_chart_id);pk"`
  192. ChartInfoId int `description:"图表id"`
  193. AdminId int `description:"用户id"`
  194. CreateTime time.Time `description:"创建时间"`
  195. ModifyTime time.Time `description:"修改时间"`
  196. MyChartClassifyId string
  197. }
  198. func AddMyChart(item *MyChart) (lastId int64, err error) {
  199. o := orm.NewOrmUsingDB("data")
  200. lastId, err = o.Insert(item)
  201. return
  202. }
  203. type MyChartClassifyMapping struct {
  204. MyChartClassifyMappingId int `orm:"column(my_chart_classify_mapping_id);pk"`
  205. AdminId int
  206. MyChartId int
  207. MyChartClassifyId int
  208. CreateTime time.Time
  209. ModifyTime time.Time
  210. Sort float64
  211. }
  212. func AddMyChartClassifyMapping(item *MyChartClassifyMapping) (lastId int64, err error) {
  213. o := orm.NewOrmUsingDB("data")
  214. lastId, err = o.Insert(item)
  215. return
  216. }
  217. func GetMyChartClassifyMapping(adminId, myChartId, myChartClassifyId int) (item *MyChartClassifyMapping, err error) {
  218. o := orm.NewOrmUsingDB("data")
  219. sql := `SELECT * FROM my_chart_classify_mapping WHERE admin_id=? AND my_chart_id=? AND my_chart_classify_id=? `
  220. err = o.Raw(sql, adminId, myChartId, myChartClassifyId).QueryRow(&item)
  221. return
  222. }
  223. type MyChartDeleteReq struct {
  224. MyChartId int `description:"我的图表id"`
  225. MyChartClassifyId int `description:"我的图表分类id"`
  226. }
  227. func DeleteMyChart(myChartId, myChartClassifyId int) (isDelMyChart bool, err error) {
  228. o := orm.NewOrmUsingDB("data")
  229. if myChartClassifyId > 0 {
  230. sql := `DELETE FROM my_chart_classify_mapping WHERE my_chart_id=? AND my_chart_classify_id=? `
  231. _, err = o.Raw(sql, myChartId, myChartClassifyId).Exec()
  232. if err != nil {
  233. return
  234. }
  235. var count int
  236. sql = ` SELECT COUNT(1) AS count FROM my_chart_classify_mapping WHERE my_chart_id=? `
  237. err = o.Raw(sql, myChartId).QueryRow(&count)
  238. if err != nil {
  239. return
  240. }
  241. if count <= 0 {
  242. sql = `DELETE FROM my_chart WHERE my_chart_id=? `
  243. _, err = o.Raw(sql, myChartId).Exec()
  244. if err != nil {
  245. return
  246. }
  247. isDelMyChart = true
  248. }
  249. } else {
  250. sql := `DELETE FROM my_chart WHERE my_chart_id=? `
  251. _, err = o.Raw(sql, myChartId).Exec()
  252. if err != nil {
  253. return
  254. }
  255. isDelMyChart = true
  256. }
  257. return
  258. }
  259. type MyChartList struct {
  260. AdminId int `description:"关联人id"`
  261. MyChartId int `description:"我的图表主键"`
  262. ChartInfoId int
  263. ChartName string `description:"来源名称"`
  264. ChartNameEn string `description:"英文图表名称"`
  265. ChartClassifyId int `description:"图表分类id"`
  266. SysUserId int
  267. SysUserRealName string
  268. UniqueCode string `description:"图表唯一编码"`
  269. CreateTime time.Time
  270. ModifyTime time.Time
  271. DateType int `description:"日期类型:1:00年至今,2:10年至今,3:15年至今,4:年初至今,5:自定义时间"`
  272. StartDate string `description:"自定义开始日期"`
  273. EndDate string `description:"自定义结束日期"`
  274. IsSetName int `description:"设置名称"`
  275. EdbInfoIds string `description:"指标id"`
  276. ChartType int `description:"生成样式:1:曲线图,2:季节性图"`
  277. Calendar string `description:"公历/农历"`
  278. SeasonStartDate string `description:"季节性图开始日期"`
  279. SeasonEndDate string `description:"季节性图开始日期"`
  280. ChartImage string `description:"图表图片"`
  281. Sort int `description:"排序字段,数字越小越排前面"`
  282. IsAdd bool `description:"true:已加入我的图库,false:未加入我的图库"`
  283. MyChartClassifyName string `description:"分类名称,多个用英文逗号分割"`
  284. MyChartClassifyId string `description:"分类id,多个用英文逗号分割"`
  285. EdbEndDate string `description:"指标最新更新日期"`
  286. IsEnChart bool `description:"是否展示英文标识"`
  287. Disabled int `description:"是否禁用,0:启用,1:禁用,默认:0"`
  288. Source int `description:"1:ETA图库;2:商品价格曲线"`
  289. }
  290. type MyChartListResp struct {
  291. Paging *paging.PagingItem
  292. List []*MyChartList
  293. }
  294. func GetMyChartListByCondition(condition string, pars []interface{}, startSize, pageSize int) (item []*MyChartList, err error) {
  295. o := orm.NewOrmUsingDB("data")
  296. field := `a.my_chart_id,c.admin_id,c.sort,c.create_time as c_ctime,b.chart_info_id,b.chart_name,b.chart_name_en,b.chart_classify_id,b.sys_user_id,b.sys_user_real_name,b.unique_code,b.create_time,b.modify_time,b.date_type,b.start_date,b.end_date,b.is_set_name,b.edb_info_ids,b.chart_type,b.calendar,b.season_start_date,b.season_end_date,b.chart_image,b.edb_end_date,b.disabled,b.source `
  297. //MyChartClassifyName string `description:"分类名称,多个用英文逗号分割"`
  298. //MyChartClassifyId string `description:"分类id,多个用英文逗号分割"`
  299. sql := ` SELECT * FROM ( SELECT ` + field + ` FROM my_chart AS a
  300. INNER JOIN chart_info AS b ON a.chart_info_id=b.chart_info_id
  301. INNER JOIN my_chart_classify_mapping AS c ON a.my_chart_id=c.my_chart_id
  302. INNER JOIN my_chart_classify AS d ON c.my_chart_classify_id=d.my_chart_classify_id
  303. WHERE 1=1 `
  304. if condition != "" {
  305. sql += condition
  306. }
  307. sql += " GROUP BY a.chart_info_id) AS f ORDER BY sort ASC,c_ctime desc LIMIT ?,? "
  308. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&item)
  309. return
  310. }
  311. func GetMyChartListCountByCondition(condition string, pars []interface{}) (count int, err error) {
  312. o := orm.NewOrmUsingDB("data")
  313. sql := ` select a.* from my_chart AS a
  314. INNER JOIN chart_info AS b ON a.chart_info_id=b.chart_info_id
  315. INNER JOIN my_chart_classify_mapping AS c ON a.my_chart_id=c.my_chart_id
  316. INNER JOIN my_chart_classify AS d ON c.my_chart_classify_id=d.my_chart_classify_id
  317. WHERE 1=1`
  318. if condition != "" {
  319. sql += condition
  320. }
  321. sql1 := ` SELECT COUNT(1) AS count FROM (` + sql + ` GROUP BY a.chart_info_id ) d`
  322. err = o.Raw(sql1, pars).QueryRow(&count)
  323. return
  324. }
  325. func GetMyChartMaxSort(adminId, myChartClassifyId int) (count int, err error) {
  326. o := orm.NewOrmUsingDB("data")
  327. sql := ` SELECT COUNT(1) AS count FROM my_chart_classify_mapping WHERE 1=1 AND admin_id=? AND my_chart_classify_id=? `
  328. err = o.Raw(sql, adminId, myChartClassifyId).QueryRow(&count)
  329. return
  330. }
  331. type MoveMyChartReq struct {
  332. MyChartId int `description:"移动的,我的图表图表ID"`
  333. PrevMyChartId int `description:"移动到的位置,上一级的,我的图表ID,如果没有上一级,则传0"`
  334. NextMyChartId int `description:"移动到的位置,下一级的,我的图表ID,如果没有下一级,则传0"`
  335. MyChartClassifyId int `description:"当前分类id"`
  336. }
  337. func MyChartMove(sort float64, adminId int, myChartId, myChartClassifyId int) (err error) {
  338. o := orm.NewOrmUsingDB("data")
  339. sql := ` UPDATE my_chart_classify_mapping SET sort = ?,modify_time=NOW() WHERE admin_id=? AND my_chart_id=? AND my_chart_classify_id=? `
  340. _, err = o.Raw(sql, sort, adminId, myChartId, myChartClassifyId).Exec()
  341. return
  342. }
  343. // UpdateMyChartSortByClassifyId 根据分类id更新排序
  344. func UpdateMyChartSortByClassifyId(myChartClassifyId, nowSort int, prevMyChartClassifyMappingId int, updateSort string) (err error) {
  345. o := orm.NewOrmUsingDB("data")
  346. sql := ` update my_chart_classify_mapping set sort = ` + updateSort + ` WHERE my_chart_classify_id=? AND `
  347. if prevMyChartClassifyMappingId > 0 {
  348. sql += ` ( sort > ? or ( my_chart_classify_mapping_id < ? and sort=? )) `
  349. }
  350. _, err = o.Raw(sql, myChartClassifyId, nowSort, prevMyChartClassifyMappingId, nowSort).Exec()
  351. return
  352. }
  353. func GetMyChartMinSortByAdminId(adminId int) (item *MyChart, err error) {
  354. o := orm.NewOrmUsingDB("data")
  355. sql := ` SELECT * FROM my_chart WHERE admin_id=? ORDER BY sort ASC LIMIT 1`
  356. err = o.Raw(sql, adminId).QueryRow(&item)
  357. return
  358. }
  359. type ChartRecommendListResp struct {
  360. List []*ChartInfoView
  361. }
  362. func GetChartClassify(chartClassifyId int) (chart_classify_id string, err error) {
  363. o := orm.NewOrmUsingDB("data")
  364. sql := `SELECT GROUP_CONCAT(t.chart_classify_id) AS chart_classify_id FROM (
  365. SELECT a.chart_classify_id FROM chart_classify AS a
  366. WHERE a.chart_classify_id=?
  367. UNION ALL
  368. SELECT a.chart_classify_id FROM chart_classify AS a
  369. WHERE a.parent_id=? UNION ALL
  370. SELECT
  371. chart_classify_id
  372. FROM
  373. chart_classify
  374. WHERE
  375. parent_id IN ( SELECT chart_classify_id FROM chart_classify WHERE parent_id = ? )
  376. )AS t`
  377. err = o.Raw(sql, chartClassifyId, chartClassifyId, chartClassifyId).QueryRow(&chart_classify_id)
  378. return
  379. }
  380. func GetRecommendChartListByCondition(condition string, pars []interface{}, startSize, pageSize int) (item []*ChartInfoView, err error) {
  381. o := orm.NewOrmUsingDB("data")
  382. sql := ` SELECT b.* FROM my_chart AS a
  383. INNER JOIN chart_info AS b ON a.chart_info_id=b.chart_info_id `
  384. if condition != "" {
  385. sql += condition
  386. }
  387. sql += " ORDER BY a.create_time DESC LIMIT ?,? "
  388. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&item)
  389. return
  390. }
  391. func GetMyChartListByAdminId(adminId int) (item []*MyChartView, err error) {
  392. o := orm.NewOrmUsingDB("data")
  393. //sql := ` SELECT * FROM my_chart WHERE 1=1 AND admin_id=? `
  394. sql := ` SELECT a.*,GROUP_CONCAT(c.my_chart_classify_id SEPARATOR ',') AS my_chart_classify_id FROM my_chart AS a
  395. LEFT JOIN my_chart_classify_mapping AS b ON a.my_chart_id=b.my_chart_id AND a.admin_id=b.admin_id
  396. LEFT JOIN my_chart_classify AS c ON b.my_chart_classify_id=c.my_chart_classify_id AND b.admin_id=c.admin_id
  397. WHERE 1=1 AND a.admin_id=?
  398. GROUP BY a.chart_info_id `
  399. _, err = o.Raw(sql, adminId).QueryRows(&item)
  400. return
  401. }
  402. func GetMyChartSearchByCondition(condition string, pars []interface{}) (item []*MyChartList, err error) {
  403. o := orm.NewOrmUsingDB("data")
  404. sql := ` SELECT a.my_chart_id,c.sort,b.*,GROUP_CONCAT(DISTINCT d.my_chart_classify_name) AS my_chart_classify_name,GROUP_CONCAT(DISTINCT d.my_chart_classify_id) AS my_chart_classify_id
  405. FROM my_chart AS a
  406. INNER JOIN chart_info AS b ON a.chart_info_id=b.chart_info_id
  407. INNER JOIN my_chart_classify_mapping AS c ON a.my_chart_id=c.my_chart_id
  408. INNER JOIN my_chart_classify AS d ON c.my_chart_classify_id=d.my_chart_classify_id
  409. WHERE 1=1 `
  410. if condition != "" {
  411. sql += condition
  412. }
  413. sql += " GROUP BY a.chart_info_id ORDER BY c.sort DESC "
  414. _, err = o.Raw(sql, pars).QueryRows(&item)
  415. return
  416. }
  417. type MyChartSearchListResp struct {
  418. List []*MyChartList
  419. Paging *paging.PagingItem
  420. }
  421. type MyChartEditReq struct {
  422. MyChartId int `description:"我的图表主键"`
  423. CurrentMyChartClassifyId int `description:"当前分类ID"`
  424. MyChartClassifyId []int `description:"分类id,数组形式"`
  425. }
  426. func GetMyChartClassifyMappingCount(adminId, myChartId, myChartClassifyId int) (count int, err error) {
  427. o := orm.NewOrmUsingDB("data")
  428. sql := ` SELECT COUNT(1) AS count
  429. FROM my_chart_classify_mapping
  430. WHERE 1=1 AND admin_id=? AND my_chart_id=? AND my_chart_classify_id=? `
  431. err = o.Raw(sql, adminId, myChartId, myChartClassifyId).QueryRow(&count)
  432. return
  433. }
  434. func DeleteMyChartClassifyMapping(adminId, myChartId, myChartClassifyId int) (err error) {
  435. o := orm.NewOrmUsingDB("data")
  436. sql := ` DELETE FROM my_chart_classify_mapping
  437. WHERE admin_id=? AND my_chart_id=? AND my_chart_classify_id=? `
  438. _, err = o.Raw(sql, adminId, myChartId, myChartClassifyId).Exec()
  439. return
  440. }
  441. func GetMyChartSort(adminId, myChartClassifyId, sort int) (item *MyChartClassifyMapping, err error) {
  442. o := orm.NewOrmUsingDB("data")
  443. sql := ` SELECT * FROM my_chart_classify_mapping WHERE 1=1 AND admin_id=? AND my_chart_classify_id=? `
  444. if sort == 1 {
  445. sql += ` ORDER BY sort DESC,my_chart_classify_mapping_id ASC LIMIT 1 `
  446. } else {
  447. sql += ` ORDER BY sort ASC,my_chart_classify_mapping_id DESC LIMIT 1 `
  448. }
  449. err = o.Raw(sql, adminId, myChartClassifyId).QueryRow(&item)
  450. return
  451. }
  452. func GetMyChartByCondition(condition string, pars []interface{}) (item []*MyChartView, err error) {
  453. o := orm.NewOrmUsingDB("data")
  454. //sql := ` SELECT * FROM my_chart WHERE 1=1 `
  455. sql := `SELECT a.*,GROUP_CONCAT(c.my_chart_classify_id SEPARATOR ',') AS my_chart_classify_id FROM my_chart AS a
  456. LEFT JOIN my_chart_classify_mapping AS b ON a.my_chart_id=b.my_chart_id AND a.admin_id=b.admin_id
  457. LEFT JOIN my_chart_classify AS c ON b.my_chart_classify_id=c.my_chart_classify_id AND b.admin_id=c.admin_id
  458. WHERE 1=1
  459. `
  460. if condition != "" {
  461. sql += condition
  462. }
  463. sql += ` GROUP BY a.chart_info_id `
  464. _, err = o.Raw(sql, pars).QueryRows(&item)
  465. return
  466. }
  467. func GetMyChartDetailByCondition(condition string, pars []interface{}) (item *MyChartView, err error) {
  468. o := orm.NewOrmUsingDB("data")
  469. sql := ` SELECT * FROM my_chart WHERE 1=1 `
  470. if condition != "" {
  471. sql += condition
  472. }
  473. err = o.Raw(sql, pars).QueryRow(&item)
  474. return
  475. }
  476. func MoveMyChartClassify(adminId, myChartId int, myChartClassifyId []int) (err error) {
  477. o := orm.NewOrmUsingDB("data")
  478. to, err := o.Begin()
  479. if err != nil {
  480. return
  481. }
  482. defer func() {
  483. if err != nil {
  484. _ = to.Rollback()
  485. } else {
  486. _ = to.Commit()
  487. }
  488. }()
  489. classifyIdMap := make(map[int]int)
  490. for _, v := range myChartClassifyId {
  491. classifyIdMap[v] = v
  492. }
  493. mappingList := make([]*MyChartClassifyMapping, 0)
  494. sql := `SELECT * FROM my_chart_classify_mapping WHERE admin_id=? AND my_chart_id=?`
  495. _, err = to.Raw(sql, adminId, myChartId).QueryRows(&mappingList)
  496. if err != nil {
  497. return
  498. }
  499. for _, v := range mappingList {
  500. if _, ok := classifyIdMap[v.MyChartClassifyId]; !ok {
  501. sql := ` DELETE FROM my_chart_classify_mapping WHERE my_chart_classify_mapping_id=? `
  502. _, err = to.Raw(sql, v.MyChartClassifyMappingId).Exec()
  503. if err != nil {
  504. return
  505. }
  506. }
  507. }
  508. //sql := `DELETE FROM my_chart_classify_mapping WHERE admin_id=? AND my_chart_id=? `
  509. //_, err = to.Raw(sql, adminId, myChartId).Exec()
  510. //if err != nil {
  511. // return err
  512. //}
  513. //step := float64(65536)
  514. for _, v := range myChartClassifyId {
  515. var count int
  516. sql := ` SELECT COUNT(1) AS count FROM my_chart_classify_mapping WHERE my_chart_id=? AND my_chart_classify_id=? `
  517. err = to.Raw(sql, myChartId, v).QueryRow(&count)
  518. if err != nil {
  519. return
  520. }
  521. if count <= 0 {
  522. //total, err := GetMyChartMaxSort(adminId, v)
  523. if err != nil {
  524. return err
  525. }
  526. //sort := (float64(total) + 1) * step
  527. sort := 0
  528. sql = ` INSERT INTO my_chart_classify_mapping (
  529. admin_id,
  530. my_chart_id,
  531. my_chart_classify_id,
  532. create_time,
  533. modify_time,
  534. sort )VALUES (?,?,?,NOW(),NOW(),?) `
  535. _, err = to.Raw(sql, adminId, myChartId, v, sort).Exec()
  536. if err != nil {
  537. return err
  538. }
  539. }
  540. }
  541. return
  542. }
  543. // Update 更新分类基础信息
  544. func (myChartClassify *MyChartClassify) Update(cols []string) (err error) {
  545. o := orm.NewOrmUsingDB("data")
  546. _, err = o.Update(myChartClassify, cols...)
  547. return
  548. }
  549. // GetFirstMyChartClassifyByAdminId 获取当前账号下,排序第一条的分类数据
  550. func GetFirstMyChartClassifyByAdminId(adminId int) (item *MyChartClassify, err error) {
  551. o := orm.NewOrmUsingDB("data")
  552. sql := ` SELECT * FROM my_chart_classify WHERE admin_id=? order by sort asc,my_chart_classify_id asc limit 1`
  553. err = o.Raw(sql, adminId).QueryRow(&item)
  554. return
  555. }
  556. // UpdateMyChartClassifySortByClassifyId 根据分类id更新排序
  557. func UpdateMyChartClassifySortByClassifyId(adminId, classifyId, nowSort int, updateSort string) (err error) {
  558. o := orm.NewOrmUsingDB("data")
  559. sql := ` update my_chart_classify set sort = ` + updateSort + ` WHERE admin_id = ? and sort > ? `
  560. if classifyId > 0 {
  561. sql += ` or ( my_chart_classify_id > ` + fmt.Sprint(classifyId) + ` and sort = ` + fmt.Sprint(nowSort) + `)`
  562. }
  563. _, err = o.Raw(sql, adminId, nowSort).Exec()
  564. return
  565. }
  566. // ModifyChartClassifyPublicReq 修改我的图库分类是否可见
  567. type ModifyChartClassifyPublicReq struct {
  568. MyChartClassifyId int `description:"分类id"`
  569. IsPublic int `description:"是否所有人可见,0:仅自己可见,1:所有人可见"`
  570. }
  571. // CopyChartClassifyReq 复制我的图库分类
  572. type CopyChartClassifyReq struct {
  573. MyChartClassifyId int `description:"分类id"`
  574. }
  575. // GetPublicMyChartClassifyById 根据分类ID获取公开分类
  576. func GetPublicMyChartClassifyById(myChartClassifyId int) (item *MyChartClassify, err error) {
  577. o := orm.NewOrmUsingDB("data")
  578. sql := ` SELECT * FROM my_chart_classify WHERE is_public=1 AND my_chart_classify_id=? `
  579. err = o.Raw(sql, myChartClassifyId).QueryRow(&item)
  580. return
  581. }
  582. // CopyPublicMyChartClassifyById 根据分类ID获取公开分类
  583. func CopyPublicMyChartClassifyById(myChartClassifyId, adminId int, newMyChartClassifyName string) (newMyChartClassify *MyChartClassify, err error) {
  584. o := orm.NewOrmUsingDB("data")
  585. to, err := o.Begin()
  586. if err != nil {
  587. return
  588. }
  589. defer func() {
  590. if err != nil {
  591. _ = to.Rollback()
  592. } else {
  593. _ = to.Commit()
  594. }
  595. }()
  596. //获取分类里面所有的图表信息
  597. fromMappingList := make([]*MyChartClassifyMapping, 0)
  598. sql := `SELECT * FROM my_chart_classify_mapping WHERE my_chart_classify_id=?`
  599. _, err = to.Raw(sql, myChartClassifyId).QueryRows(&fromMappingList)
  600. if err != nil {
  601. return
  602. }
  603. fromMyChartSortMap := make(map[int]float64)
  604. for _, fromMapping := range fromMappingList {
  605. fromMyChartSortMap[fromMapping.MyChartId] = fromMapping.Sort
  606. }
  607. var minSort int
  608. sql = ` SELECT MIN(sort) sort FROM my_chart_classify WHERE admin_id=? `
  609. err = o.Raw(sql, adminId).QueryRow(&minSort)
  610. if err != nil {
  611. return
  612. }
  613. // 我的图表
  614. fromMyChartList := make([]*MyChart, 0)
  615. sql = `SELECT a.* FROM my_chart a join my_chart_classify_mapping b on a.my_chart_id=b.my_chart_id
  616. WHERE b.my_chart_classify_id=?`
  617. _, err = to.Raw(sql, myChartClassifyId).QueryRows(&fromMyChartList)
  618. if err != nil {
  619. return
  620. }
  621. //新增我的图库分类
  622. newMyChartClassify = &MyChartClassify{
  623. MyChartClassifyName: newMyChartClassifyName,
  624. AdminId: adminId,
  625. CreateTime: time.Now(),
  626. ModifyTime: time.Now(),
  627. Sort: minSort - 1,
  628. }
  629. newMyChartClassifyId, err := to.Insert(newMyChartClassify)
  630. if err != nil {
  631. return
  632. }
  633. newMappingList := make([]*MyChartClassifyMapping, 0)
  634. //将图表加入我的图库
  635. for _, v := range fromMyChartList {
  636. tmpMyChart := &MyChart{
  637. ChartInfoId: v.ChartInfoId,
  638. AdminId: adminId,
  639. Source: v.Source,
  640. CreateTime: time.Now(),
  641. ModifyTime: time.Now(),
  642. }
  643. tmpMyChartId, tmpErr := to.Insert(tmpMyChart)
  644. if tmpErr != nil {
  645. err = tmpErr
  646. }
  647. tmpMyChart.MyChartId = int(tmpMyChartId)
  648. // 往自己的分类里面新增图表关系
  649. //原始图表的排序
  650. tmpSort, ok := fromMyChartSortMap[v.MyChartId]
  651. if !ok {
  652. tmpSort = 0
  653. }
  654. tmp := &MyChartClassifyMapping{
  655. AdminId: adminId,
  656. MyChartId: int(tmpMyChartId),
  657. MyChartClassifyId: int(newMyChartClassifyId),
  658. CreateTime: time.Now(),
  659. ModifyTime: time.Now(),
  660. Sort: tmpSort,
  661. }
  662. newMappingList = append(newMappingList, tmp)
  663. }
  664. if len(newMappingList) > 0 {
  665. _, err = to.InsertMulti(len(newMappingList), newMappingList)
  666. }
  667. return
  668. }
  669. // ModifyChartClassifyCompanyPublicReq 修改公共图库分类是否用户可见
  670. type ModifyChartClassifyCompanyPublicReq struct {
  671. MyChartClassifyId int `description:"分类id"`
  672. IsCompanyPublic int `description:"是否用户可见 0-否 1-是"`
  673. }
  674. // GetCompanyPublicClassifyList 获取用户可见的分类列表
  675. func GetCompanyPublicClassifyList(condition string, pars []interface{}) (item []*MyChartClassify, err error) {
  676. o := orm.NewOrmUsingDB("data")
  677. sql := ` SELECT * FROM my_chart_classify WHERE 1 = 1 AND is_public = 1 AND is_company_public = 1 `
  678. sql += condition
  679. sql += " ORDER BY public_time asc"
  680. _, err = o.Raw(sql, pars).QueryRows(&item)
  681. return
  682. }
  683. // GetMyChartClassifyList 获取我的图表关联的分类列表数据
  684. func GetMyChartClassifyList(myChartClassifyId int) (mappingList []*MyChartClassifyMapping, err error) {
  685. o := orm.NewOrmUsingDB("data")
  686. //o.Begin()
  687. //
  688. //defer func() {
  689. // if err != nil {
  690. // o.Rollback()
  691. // } else {
  692. // o.Commit()
  693. // }
  694. //}()
  695. sql := `SELECT * FROM my_chart_classify_mapping WHERE my_chart_id=?`
  696. _, err = o.Raw(sql, myChartClassifyId).QueryRows(&mappingList)
  697. return
  698. }
  699. // AddMyChartToMyChartClassify 添加我的图表至我的分类
  700. func AddMyChartToMyChartClassify(adminId, myChartId int, myChartClassifyId []int) (err error) {
  701. o := orm.NewOrmUsingDB("data")
  702. //o.Begin()
  703. //defer func() {
  704. // if err != nil {
  705. // o.Rollback()
  706. // } else {
  707. // o.Commit()
  708. // }
  709. //}()
  710. //需要添加的分类id 集合map
  711. classifyIdMap := make(map[int]int)
  712. for _, v := range myChartClassifyId {
  713. classifyIdMap[v] = v
  714. }
  715. //查询已存在系统中的图表分类集合
  716. mappingList := make([]*MyChartClassifyMapping, 0)
  717. sql := `SELECT * FROM my_chart_classify_mapping WHERE admin_id=? AND my_chart_id=?`
  718. _, err = o.Raw(sql, adminId, myChartId).QueryRows(&mappingList)
  719. if err != nil {
  720. return
  721. }
  722. //遍历已存在系统中的图表分类集合,如果和需要添加的分类重复了,那么移除需要添加分类集合map里面的数据
  723. for _, v := range mappingList {
  724. if _, ok := classifyIdMap[v.MyChartClassifyId]; ok {
  725. delete(classifyIdMap, v.MyChartClassifyId)
  726. }
  727. }
  728. step := float64(65536)
  729. for _, v := range myChartClassifyId {
  730. var count int
  731. sql := ` SELECT COUNT(1) AS count FROM my_chart_classify_mapping WHERE my_chart_id=? AND my_chart_classify_id=? `
  732. err = o.Raw(sql, myChartId, v).QueryRow(&count)
  733. if err != nil {
  734. return
  735. }
  736. if count <= 0 {
  737. total, err := GetMyChartMaxSort(adminId, v)
  738. if err != nil {
  739. return err
  740. }
  741. sort := (float64(total) + 1) * step
  742. sql = ` INSERT INTO my_chart_classify_mapping (
  743. admin_id,
  744. my_chart_id,
  745. my_chart_classify_id,
  746. create_time,
  747. modify_time,
  748. sort )VALUES (?,?,?,NOW(),NOW(),?) `
  749. _, err = o.Raw(sql, adminId, myChartId, v, sort).Exec()
  750. if err != nil {
  751. return err
  752. }
  753. }
  754. }
  755. return
  756. }
  757. // GetMyChartListGroupByCharyInfoIdAndAdminIdByCondition 获取MY ETA 图表数据,根据图表id和操作员id
  758. func GetMyChartListGroupByCharyInfoIdAndAdminIdByCondition(condition string, pars []interface{}) (item []*MyChartList, err error) {
  759. o := orm.NewOrmUsingDB("data")
  760. sql := ` SELECT a.admin_id,a.my_chart_id,c.sort,b.*,GROUP_CONCAT(DISTINCT d.my_chart_classify_name) AS my_chart_classify_name,GROUP_CONCAT(DISTINCT d.my_chart_classify_id) AS my_chart_classify_id
  761. FROM my_chart AS a
  762. INNER JOIN chart_info AS b ON a.chart_info_id=b.chart_info_id
  763. INNER JOIN my_chart_classify_mapping AS c ON a.my_chart_id=c.my_chart_id
  764. INNER JOIN my_chart_classify AS d ON c.my_chart_classify_id=d.my_chart_classify_id
  765. WHERE 1=1 `
  766. if condition != "" {
  767. sql += condition
  768. }
  769. sql += " GROUP BY a.chart_info_id,a.admin_id ORDER BY c.sort DESC "
  770. _, err = o.Raw(sql, pars).QueryRows(&item)
  771. return
  772. }
  773. func GetRelationChartListByCondition(condition string, pars []interface{}, startSize, pageSize int) (item []*ChartInfoView, err error) {
  774. o := orm.NewOrmUsingDB("data")
  775. sql := ` SELECT a.* FROM chart_info AS a
  776. JOIN chart_edb_mapping AS b on a.chart_info_id = b.chart_info_id WHERE 1=1 `
  777. if condition != "" {
  778. sql += condition
  779. }
  780. //sql += " ORDER BY sort ASC,chart_info_id DESC LIMIT ?,? "
  781. sql += " ORDER BY a.create_time DESC LIMIT ?,? "
  782. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&item)
  783. return
  784. }
  785. func GetRelationChartListCountByCondition(condition string, pars []interface{}) (count int, err error) {
  786. o := orm.NewOrmUsingDB("data")
  787. sql := ` SELECT COUNT(1) AS count FROM chart_info AS a
  788. JOIN chart_edb_mapping AS b on a.chart_info_id = b.chart_info_id WHERE 1=1 `
  789. if condition != "" {
  790. sql += condition
  791. }
  792. err = o.Raw(sql, pars).QueryRow(&count)
  793. return
  794. }
  795. // GetChartInfoByIdList 根据图表id集合 获取 图表列表
  796. func GetChartInfoByIdList(chartInfoIdList []int) (items []*ChartInfo, err error) {
  797. num := len(chartInfoIdList)
  798. if num <= 0 {
  799. return
  800. }
  801. o := orm.NewOrmUsingDB("data")
  802. sql := ` SELECT * FROM chart_info WHERE chart_info_id in (` + utils.GetOrmInReplace(num) + `) `
  803. _, err = o.Raw(sql, chartInfoIdList).QueryRows(&items)
  804. return
  805. }