ai_predict_model_dashboard.go 8.2 KB

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