ai_predict_model_dashboard.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. package data_manage
  2. import (
  3. "eta/eta_api/utils"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "strings"
  7. "time"
  8. )
  9. // AiPredictModelDashboard AI预测模型-BI看板
  10. type AiPredictModelDashboard struct {
  11. AiPredictModelDashboardId int `orm:"column(ai_predict_model_dashboard_id);pk"`
  12. AiPredictModelIndexId int `description:"标的ID"`
  13. DashboardName string `description:"看板名称"`
  14. Sort int `description:"排序"`
  15. SysUserId int `description:"创建人ID"`
  16. SysUserRealName string `description:"创建人姓名"`
  17. CreateTime time.Time `description:"创建时间"`
  18. ModifyTime time.Time `description:"修改时间"`
  19. }
  20. func (m *AiPredictModelDashboard) TableName() string {
  21. return "ai_predict_model_dashboard"
  22. }
  23. type AiPredictModelDashboardCols struct {
  24. PrimaryId string
  25. AiPredictModelIndexId string
  26. DashboardName string
  27. Sort string
  28. SysUserId string
  29. SysUserRealName string
  30. CreateTime string
  31. ModifyTime string
  32. }
  33. func (m *AiPredictModelDashboard) Cols() AiPredictModelDashboardCols {
  34. return AiPredictModelDashboardCols{
  35. PrimaryId: "ai_predict_model_dashboard_id",
  36. AiPredictModelIndexId: "ai_predict_model_index_id",
  37. DashboardName: "dashboard_name",
  38. Sort: "sort",
  39. SysUserId: "sys_user_id",
  40. SysUserRealName: "sys_user_real_name",
  41. CreateTime: "create_time",
  42. ModifyTime: "modify_time",
  43. }
  44. }
  45. func (m *AiPredictModelDashboard) Create() (err error) {
  46. o := orm.NewOrmUsingDB("data")
  47. id, err := o.Insert(m)
  48. if err != nil {
  49. return
  50. }
  51. m.AiPredictModelDashboardId = int(id)
  52. return
  53. }
  54. func (m *AiPredictModelDashboard) CreateMulti(items []*AiPredictModelDashboard) (err error) {
  55. if len(items) == 0 {
  56. return
  57. }
  58. o := orm.NewOrmUsingDB("data")
  59. _, err = o.InsertMulti(len(items), items)
  60. return
  61. }
  62. func (m *AiPredictModelDashboard) Update(cols []string) (err error) {
  63. o := orm.NewOrmUsingDB("data")
  64. _, err = o.Update(m, cols...)
  65. return
  66. }
  67. func (m *AiPredictModelDashboard) Remove() (err error) {
  68. o := orm.NewOrmUsingDB("data")
  69. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.Cols().PrimaryId)
  70. _, err = o.Raw(sql, m.AiPredictModelDashboardId).Exec()
  71. return
  72. }
  73. func (m *AiPredictModelDashboard) MultiRemove(ids []int) (err error) {
  74. if len(ids) == 0 {
  75. return
  76. }
  77. o := orm.NewOrmUsingDB("data")
  78. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s IN (%s)`, m.TableName(), m.Cols().PrimaryId, utils.GetOrmInReplace(len(ids)))
  79. _, err = o.Raw(sql, ids).Exec()
  80. return
  81. }
  82. func (m *AiPredictModelDashboard) RemoveByCondition(condition string, pars []interface{}) (err error) {
  83. if condition == "" {
  84. return
  85. }
  86. o := orm.NewOrmUsingDB("data")
  87. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s`, m.TableName(), condition)
  88. _, err = o.Raw(sql, pars).Exec()
  89. return
  90. }
  91. func (m *AiPredictModelDashboard) GetItemById(id int) (item *AiPredictModelDashboard, err error) {
  92. o := orm.NewOrmUsingDB("data")
  93. sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.Cols().PrimaryId)
  94. err = o.Raw(sql, id).QueryRow(&item)
  95. return
  96. }
  97. func (m *AiPredictModelDashboard) GetItemByCondition(condition string, pars []interface{}, orderRule string) (item *AiPredictModelDashboard, err error) {
  98. o := orm.NewOrmUsingDB("data")
  99. order := ``
  100. if orderRule != "" {
  101. order = ` ORDER BY ` + orderRule
  102. }
  103. sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s %s LIMIT 1`, m.TableName(), condition, order)
  104. err = o.Raw(sql, pars).QueryRow(&item)
  105. return
  106. }
  107. func (m *AiPredictModelDashboard) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
  108. o := orm.NewOrmUsingDB("data")
  109. sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
  110. err = o.Raw(sql, pars).QueryRow(&count)
  111. return
  112. }
  113. func (m *AiPredictModelDashboard) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*AiPredictModelDashboard, err error) {
  114. o := orm.NewOrmUsingDB("data")
  115. fields := strings.Join(fieldArr, ",")
  116. if len(fieldArr) == 0 {
  117. fields = `*`
  118. }
  119. order := fmt.Sprintf(`ORDER BY %s DESC`, m.Cols().CreateTime)
  120. if orderRule != "" {
  121. order = ` ORDER BY ` + orderRule
  122. }
  123. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
  124. _, err = o.Raw(sql, pars).QueryRows(&items)
  125. return
  126. }
  127. func (m *AiPredictModelDashboard) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*AiPredictModelDashboard, err error) {
  128. o := orm.NewOrmUsingDB("data")
  129. fields := strings.Join(fieldArr, ",")
  130. if len(fieldArr) == 0 {
  131. fields = `*`
  132. }
  133. order := fmt.Sprintf(`ORDER BY %s DESC`, m.Cols().CreateTime)
  134. if orderRule != "" {
  135. order = ` ORDER BY ` + orderRule
  136. }
  137. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s LIMIT ?,?`, fields, m.TableName(), condition, order)
  138. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  139. return
  140. }
  141. // AiPredictModelDashboardItem AI预测模型-BI看板
  142. type AiPredictModelDashboardItem struct {
  143. DashboardId int `description:"看板ID"`
  144. IndexId int `description:"标的ID"`
  145. DashboardName string `description:"看板名称"`
  146. Sort int `description:"排序"`
  147. SysUserId int `description:"创建人ID"`
  148. SysUserRealName string `description:"创建人姓名"`
  149. CreateTime string `description:"创建时间"`
  150. ModifyTime string `description:"修改时间"`
  151. }
  152. func (m *AiPredictModelDashboard) Format2Item() (item *AiPredictModelDashboardItem) {
  153. item = new(AiPredictModelDashboardItem)
  154. item.DashboardId = m.AiPredictModelDashboardId
  155. item.IndexId = m.AiPredictModelIndexId
  156. item.DashboardName = m.DashboardName
  157. item.Sort = m.Sort
  158. item.SysUserId = m.SysUserId
  159. item.SysUserRealName = m.SysUserRealName
  160. item.CreateTime = utils.TimeTransferString(utils.FormatDateTime, m.CreateTime)
  161. item.ModifyTime = utils.TimeTransferString(utils.FormatDateTime, m.ModifyTime)
  162. return
  163. }
  164. // AiPredictModelDashboardDetailResp BI看板详情响应
  165. type AiPredictModelDashboardDetailResp struct {
  166. *AiPredictModelDashboardItem
  167. CreateUserId int `description:"标的创建人ID"`
  168. CreateUserRealName string `description:"标的创建人姓名"`
  169. List []*AiPredictModelDashboardDetailItem
  170. }
  171. // AiPredictModelDashboardSaveReq BI看板保存请求
  172. type AiPredictModelDashboardSaveReq struct {
  173. IndexId int `description:"标的ID"`
  174. DashboardName string `description:"看板名称"`
  175. List []*AiPredictModelDashboardDetailReq
  176. }
  177. type AiPredictModelDashboardDetailReq struct {
  178. Type int
  179. UniqueCode string
  180. Sort int
  181. }
  182. // SaveIndexDashboard 保存标的看板
  183. func (m *AiPredictModelDashboard) SaveIndexDashboard(dashboardItem *AiPredictModelDashboard, dashboardDetails []*AiPredictModelDashboardDetail, isUpdate bool, updateCols []string) (err error) {
  184. if dashboardItem == nil {
  185. return
  186. }
  187. o := orm.NewOrmUsingDB("data")
  188. tx, e := o.Begin()
  189. if e != nil {
  190. err = fmt.Errorf("trans begin err: %v", e)
  191. return
  192. }
  193. defer func() {
  194. if err != nil {
  195. _ = tx.Rollback()
  196. return
  197. }
  198. _ = tx.Commit()
  199. }()
  200. // 新增/更新看板
  201. var dashboardId int
  202. if !isUpdate {
  203. newId, e := tx.Insert(dashboardItem)
  204. if e != nil {
  205. err = fmt.Errorf("insert dashboard err: %v", e)
  206. return
  207. }
  208. dashboardId = int(newId)
  209. } else {
  210. _, e = tx.Update(dashboardItem, updateCols...)
  211. if e != nil {
  212. err = fmt.Errorf("update dashboard err: %v", e)
  213. return
  214. }
  215. dashboardId = dashboardItem.AiPredictModelDashboardId
  216. }
  217. // 清空详情并新增
  218. if isUpdate {
  219. sql := `DELETE FROM ai_predict_model_dashboard_detail WHERE ai_predict_model_dashboard_id = ?`
  220. _, e = tx.Raw(sql, dashboardItem.AiPredictModelDashboardId).Exec()
  221. if e != nil {
  222. err = fmt.Errorf("clear dashboard detail err: %v", e)
  223. return
  224. }
  225. }
  226. if len(dashboardDetails) > 0 {
  227. for _, v := range dashboardDetails {
  228. v.AiPredictModelDashboardId = dashboardId
  229. }
  230. _, e = tx.InsertMulti(utils.MultiAddNum, dashboardDetails)
  231. if e != nil {
  232. err = fmt.Errorf("insert dashboard detail err: %v", e)
  233. return
  234. }
  235. }
  236. return
  237. }