excel_info.go 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  1. package excel
  2. import (
  3. "eta/eta_api/global"
  4. "eta/eta_api/models/data_manage"
  5. "eta/eta_api/utils"
  6. "fmt"
  7. "strconv"
  8. "time"
  9. )
  10. // ExcelInfo excel表格详情表
  11. type ExcelInfo struct {
  12. ExcelInfoId int `orm:"column(excel_info_id);pk" gorm:"primaryKey" `
  13. Source int `description:"表格来源,1:excel插件的表格,2:自定义表格,3:混合表格,4:自定义分析,默认:1"`
  14. ExcelType int `description:"表格类型,1:指标列,2:日期列,默认:1"`
  15. ExcelName string `description:"表格名称"`
  16. UniqueCode string `description:"表格唯一编码"`
  17. ExcelClassifyId int `description:"表格分类id"`
  18. SysUserId int `description:"操作人id"`
  19. SysUserRealName string `description:"操作人真实姓名"`
  20. Content string `description:"表格内容"`
  21. ExcelImage string `description:"表格图片"`
  22. FileUrl string `description:"表格下载地址"`
  23. Sort int `description:"排序字段,数字越小越排前面"`
  24. IsDelete int `description:"是否删除,0:未删除,1:已删除"`
  25. ModifyTime time.Time `description:"最近修改日期"`
  26. CreateTime time.Time `description:"创建日期"`
  27. IsJoinPermission int `description:"是否加入权限管控,0:不加入;1:加入;默认:0"`
  28. ParentId int `description:"表格的父级id"`
  29. BalanceType int `description:"平衡表类型:0 动态表,1静态表"`
  30. UpdateUserId int `description:"更新人id"`
  31. UpdateUserRealName string `description:"更新人真实姓名"`
  32. RelExcelInfoId int `description:"平衡表里静态表关联的动态表excel id"`
  33. VersionName string `description:"静态表版本名称"`
  34. SourcesFrom string `description:"图表来源"`
  35. ExtraConfig string `description:"额外配置:如多空分析、相关性表格参数"`
  36. }
  37. // Update 更新 excel表格基础信息
  38. func (excelInfo *ExcelInfo) Update(cols []string) (err error) {
  39. err = global.DbMap[utils.DbNameIndex].Select(cols).Updates(excelInfo).Error
  40. return
  41. }
  42. type MyExcelInfoList struct {
  43. ExcelInfoId int `orm:"column(excel_info_id);pk" gorm:"primaryKey" `
  44. Source int `description:"表格来源,1:excel插件的表格,2:自定义表格,默认:1"`
  45. ExcelType int `description:"表格类型,1:指标列,2:日期列,默认:1"`
  46. ExcelName string `description:"表格名称"`
  47. UniqueCode string `description:"表格唯一编码"`
  48. ExcelClassifyId int `description:"表格分类id"`
  49. SysUserId int `description:"操作人id"`
  50. SysUserRealName string `description:"操作人真实姓名"`
  51. ExcelImage string `description:"表格图片"`
  52. FileUrl string `description:"表格下载地址"`
  53. Sort int `description:"排序字段,数字越小越排前面"`
  54. ModifyTime time.Time `description:"最近修改日期"`
  55. CreateTime time.Time `description:"创建日期"`
  56. IsJoinPermission int `description:"是否加入权限管控,0:不加入;1:加入;默认:0"`
  57. HaveOperaAuth bool `description:"是否有数据权限"`
  58. UpdateUserId int `description:"更新人id"`
  59. UpdateUserRealName string `description:"更新人真实姓名"`
  60. Button ExcelInfoDetailButton `description:"操作权限" gorm:"-"`
  61. CanEdit bool `description:"是否可编辑"`
  62. Editor string `description:"编辑人"`
  63. }
  64. // AddExcelInfo 新增表格
  65. func AddExcelInfo(excelInfo *ExcelInfo, excelEdbMappingList []*ExcelEdbMapping, childExcel *ExcelInfo) (err error) {
  66. to := global.DbMap[utils.DbNameIndex].Begin()
  67. defer func() {
  68. if err != nil {
  69. _ = to.Rollback()
  70. } else {
  71. _ = to.Commit()
  72. }
  73. }()
  74. // 表格信息入库
  75. err = to.Create(excelInfo).Error
  76. if err != nil {
  77. return
  78. }
  79. //excelInfo.ExcelInfoId = excelInfo.ExcelInfoId
  80. // todo 判断如果是平衡表的父级,则新增一个名叫平衡表的子表, 内容为空
  81. if childExcel != nil {
  82. // 表格信息入库
  83. childExcel.ParentId = excelInfo.ExcelInfoId
  84. err = to.Create(childExcel).Error
  85. if err != nil {
  86. err = fmt.Errorf("新增子表失败:%v", err)
  87. return
  88. }
  89. }
  90. // excel与指标的关联关系
  91. dataNum := len(excelEdbMappingList)
  92. if dataNum > 0 {
  93. for k, v := range excelEdbMappingList {
  94. v.ExcelInfoId = excelInfo.ExcelInfoId
  95. excelEdbMappingList[k] = v
  96. }
  97. err = to.CreateInBatches(excelEdbMappingList, utils.MultiAddNum).Error
  98. }
  99. return
  100. }
  101. // EditExcelInfo 编辑表格
  102. func EditExcelInfo(excelInfo *ExcelInfo, updateExcelInfoParams []string, excelEdbMappingList []*ExcelEdbMapping) (err error) {
  103. to := global.DbMap[utils.DbNameIndex].Begin()
  104. defer func() {
  105. if err != nil {
  106. _ = to.Rollback()
  107. } else {
  108. _ = to.Commit()
  109. }
  110. }()
  111. // ETA表格信息变更
  112. err = to.Select(updateExcelInfoParams).Updates(excelInfo).Error
  113. if err != nil {
  114. return
  115. }
  116. //更新父级分类时,同时更新子集分类
  117. if excelInfo.Source == utils.BALANCE_TABLE && excelInfo.ParentId == 0 {
  118. // 同步更新子表分类
  119. sql := `UPDATE FROM excel_info set excel_classify_id = ? WHERE parent_id=? `
  120. err = to.Exec(sql, excelInfo.ExcelClassifyId, excelInfo.ExcelInfoId).Error
  121. }
  122. // 删除关系表
  123. sql := `DELETE FROM excel_edb_mapping WHERE excel_info_id=? `
  124. err = to.Exec(sql, excelInfo.ExcelInfoId).Error
  125. // excel与指标的关联关系
  126. dataNum := len(excelEdbMappingList)
  127. if dataNum > 0 {
  128. for k, v := range excelEdbMappingList {
  129. v.ExcelInfoId = excelInfo.ExcelInfoId
  130. excelEdbMappingList[k] = v
  131. }
  132. err = to.CreateInBatches(excelEdbMappingList, utils.MultiAddNum).Error
  133. }
  134. return
  135. }
  136. // GetExcelInfoAll 获取所有表格列表,用于分类展示
  137. func GetExcelInfoAll() (items []*ExcelClassifyItems, err error) {
  138. o := global.DbMap[utils.DbNameIndex]
  139. sql := ` SELECT excel_info_id,excel_classify_id,excel_name AS excel_classify_name,
  140. unique_code,sys_user_id,sys_user_real_name,is_join_permission
  141. FROM excel_info where is_delete=0 ORDER BY sort asc,create_time ASC `
  142. err = o.Raw(sql).Find(&items).Error
  143. return
  144. }
  145. // GetNoContentExcelInfoAll 获取不含content的表格列表 用于分类展示
  146. func GetNoContentExcelInfoAll(source, userId int) (items []*ExcelClassifyItems, err error) {
  147. sql := ` SELECT excel_info_id,excel_classify_id,excel_name AS excel_classify_name,
  148. unique_code,sys_user_id,sys_user_real_name,sort,is_join_permission
  149. FROM excel_info where is_delete=0 AND source = ? `
  150. pars := []interface{}{source}
  151. if userId > 0 {
  152. sql += ` AND sys_user_id = ? `
  153. pars = append(pars, userId)
  154. }
  155. sql += ` ORDER BY sort asc,excel_info_id desc `
  156. err = global.DbMap[utils.DbNameIndex].Raw(sql, pars...).Find(&items).Error
  157. return
  158. }
  159. // GetBalanceNoContentExcelInfoAll 获取不含content的平衡表表格列表 用于分类展示
  160. func GetBalanceNoContentExcelInfoAll(source int, excelInfoIds []int, userId int) (items []*ExcelClassifyItems, err error) {
  161. sql := ` SELECT excel_info_id,excel_classify_id,excel_name AS excel_classify_name,
  162. unique_code,sys_user_id,sys_user_real_name,sort,is_join_permission
  163. FROM excel_info where is_delete=0 AND source = ? AND parent_id = 0 AND balance_type=0 `
  164. pars := []interface{}{source}
  165. if userId > 0 {
  166. if len(excelInfoIds) > 0 {
  167. sql += ` AND (excel_info_id in (` + utils.GetOrmInReplace(len(excelInfoIds)) + `) or sys_user_id = ?)`
  168. pars = append(pars, excelInfoIds, userId)
  169. } else {
  170. sql += ` AND sys_user_id = ? `
  171. pars = append(pars, userId)
  172. }
  173. }
  174. sql += ` ORDER BY sort asc,excel_info_id desc `
  175. err = global.DbMap[utils.DbNameIndex].Raw(sql, pars...).Find(&items).Error
  176. return
  177. }
  178. // GetAllExcelInfoBySource 根据来源获取包含content的表格列表
  179. func GetAllExcelInfoBySource(source int) (items []*ExcelInfo, err error) {
  180. o := global.DbMap[utils.DbNameIndex]
  181. sql := ` SELECT * FROM excel_info where is_delete=0 AND source = ? ORDER BY sort asc,create_time desc `
  182. err = o.Raw(sql, source).Find(&items).Error
  183. return
  184. }
  185. // GetExcelInfoById 根据id 获取eta表格详情
  186. func GetExcelInfoById(excelInfoId int) (item *ExcelInfo, err error) {
  187. sql := ` SELECT * FROM excel_info WHERE excel_info_id=? AND is_delete=0 `
  188. err = global.DbMap[utils.DbNameIndex].Raw(sql, excelInfoId).First(&item).Error
  189. return
  190. }
  191. // GetExcelInfoByUnicode 编码获取表格
  192. func GetExcelInfoByUnicode(unicode string) (item *ExcelInfo, err error) {
  193. sql := ` SELECT * FROM excel_info WHERE unique_code = ? AND is_delete = 0 `
  194. err = global.DbMap[utils.DbNameIndex].Raw(sql, unicode).First(&item).Error
  195. return
  196. }
  197. func GetExcelInfoViewById(excelInfoId int) (item *ExcelInfoView, err error) {
  198. o := global.DbMap[utils.DbNameIndex]
  199. sql := ` SELECT * FROM excel_info WHERE excel_info_id=? AND is_delete=0 `
  200. err = o.Raw(sql, excelInfoId).First(&item).Error
  201. return
  202. }
  203. func GetExcelInfoCountByCondition(condition string, pars []interface{}) (count int, err error) {
  204. sql := ` SELECT COUNT(1) AS count FROM excel_info WHERE 1=1 AND is_delete=0 `
  205. if condition != "" {
  206. sql += condition
  207. }
  208. err = global.DbMap[utils.DbNameIndex].Raw(sql, pars...).Scan(&count).Error
  209. return
  210. }
  211. func GetNoContentExcelInfoListByCondition(condition string, pars []interface{}, startSize, pageSize int) (items []*ExcelClassifyItems, err error) {
  212. sql := `SELECT excel_info_id,excel_classify_id,excel_name AS excel_classify_name,
  213. unique_code,sys_user_id,sys_user_real_name,sort,is_join_permission FROM excel_info WHERE 1=1 `
  214. if condition != "" {
  215. sql += condition
  216. }
  217. sql += ` AND is_delete=0 ORDER BY excel_info_id DESC LIMIT ?,? `
  218. pars = append(pars, startSize)
  219. pars = append(pars, pageSize)
  220. err = global.DbMap[utils.DbNameIndex].Raw(sql, pars...).Find(&items).Error
  221. return
  222. }
  223. func GetNoContentExcelInfoListByConditionNoPage(condition string, pars []interface{}) (items []*ExcelInfo, err error) {
  224. sql := `SELECT excel_info_id,excel_classify_id,excel_name,
  225. unique_code,sys_user_id,sys_user_real_name,sort,is_join_permission, parent_id, balance_type, update_user_id,update_user_real_name,rel_excel_info_id,version_name FROM excel_info WHERE is_delete=0 `
  226. if condition != "" {
  227. sql += condition
  228. }
  229. sql += ` ORDER BY excel_info_id DESC `
  230. err = global.DbMap[utils.DbNameIndex].Raw(sql, pars...).Find(&items).Error
  231. return
  232. }
  233. func GetExcelInfoListByCondition(condition string, pars []interface{}) (items []*ExcelInfo, err error) {
  234. sql := ` SELECT * FROM excel_info WHERE 1=1 AND is_delete=0 `
  235. if condition != "" {
  236. sql += condition
  237. }
  238. sql += ` ORDER BY sort asc, excel_info_id asc`
  239. err = global.DbMap[utils.DbNameIndex].Raw(sql, pars...).Find(&items).Error
  240. return
  241. }
  242. // GetNextExcelInfoByCondition 根据条件获取下一个表格
  243. func GetNextExcelInfoByCondition(condition string, pars []interface{}) (item *ExcelInfo, err error) {
  244. sql := ` SELECT * FROM excel_info WHERE 1=1 AND is_delete=0 `
  245. if condition != "" {
  246. sql += condition
  247. }
  248. sql += " ORDER BY sort asc , create_time desc LIMIT 1 "
  249. err = global.DbMap[utils.DbNameIndex].Raw(sql, pars...).First(&item).Error
  250. return
  251. }
  252. // GetNextExcelInfo 根据分类id获取下一个excel表格
  253. func GetNextExcelInfo(classifyId, classifySort, source int) (item *ExcelInfo, err error) {
  254. sql := ` SELECT b.* FROM excel_classify AS a
  255. INNER JOIN excel_info AS b ON a.excel_classify_id=b.excel_classify_id
  256. WHERE (a.sort>? OR (a.sort=? and a.excel_classify_id>?) ) AND a.is_delete=0 AND b.is_delete=0
  257. AND a.source = ? AND b.source = ?
  258. ORDER BY a.sort ASC,b.sort asc,b.create_time desc
  259. LIMIT 1 `
  260. err = global.DbMap[utils.DbNameIndex].Raw(sql, classifySort, classifySort, classifyId, source, source).First(&item).Error
  261. return
  262. }
  263. // EditExcelInfoImage 修改excel表格的图片
  264. func EditExcelInfoImage(excelInfoId int, imageUrl string) (err error) {
  265. o := global.DbMap[utils.DbNameIndex]
  266. sql := ` UPDATE excel_info SET excel_image=?, modify_time = NOW() WHERE excel_info_id = ? AND is_delete=0 `
  267. err = o.Exec(sql, imageUrl, excelInfoId).Error
  268. if err != nil {
  269. fmt.Println("EditExcelInfoImage Err:", err.Error())
  270. return err
  271. }
  272. return
  273. }
  274. // GetExcelInfoByUniqueCode 根据unique_code来获取excel表格详情
  275. func GetExcelInfoByUniqueCode(uniqueCode string) (item *ExcelInfo, err error) {
  276. sql := ` SELECT * FROM excel_info WHERE unique_code=? AND is_delete=0 `
  277. err = global.DbMap[utils.DbNameIndex].Raw(sql, uniqueCode).First(&item).Error
  278. return
  279. }
  280. // GetFirstExcelInfoByClassifyId 获取当前分类下,且排序数相同 的排序第一条的数据
  281. func GetFirstExcelInfoByClassifyId(classifyId int) (item *ExcelInfo, err error) {
  282. sql := ` SELECT * FROM excel_info WHERE excel_classify_id=? AND is_delete=0 order by sort asc,excel_info_id desc limit 1`
  283. err = global.DbMap[utils.DbNameIndex].Raw(sql, classifyId).First(&item).Error
  284. return
  285. }
  286. // UpdateExcelInfoSortByClassifyId 根据表格id更新排序
  287. func UpdateExcelInfoSortByClassifyId(classifyId, nowSort, prevExcelInfoId int, updateSort string, source int) (err error) {
  288. sql := ` update excel_info set sort = ` + updateSort + ` WHERE excel_classify_id=? AND source=? AND is_delete=0 AND ( sort > ? `
  289. // todo 前一个兄弟节点后移
  290. if prevExcelInfoId > 0 {
  291. sql += ` or (excel_info_id < ` + fmt.Sprint(prevExcelInfoId) + ` and sort = ` + fmt.Sprint(nowSort) + `)`
  292. }
  293. sql += `)`
  294. err = global.DbMap[utils.DbNameIndex].Exec(sql, classifyId, source, nowSort).Error
  295. return
  296. }
  297. type ExcelInfoView struct {
  298. ExcelInfoId int `orm:"column(excel_info_id);pk" gorm:"primaryKey" `
  299. ExcelName string `description:"来源名称"`
  300. ExcelClassifyId int `description:"表格分类id"`
  301. ExcelClassifyName string `description:"表格名称"`
  302. SysUserId int
  303. SysUserRealName string
  304. UniqueCode string `description:"表格唯一编码"`
  305. CreateTime time.Time
  306. ModifyTime time.Time
  307. DateType int `description:"日期类型:1:00年至今,2:10年至今,3:15年至今,4:年初至今,5:自定义时间"`
  308. StartDate string `description:"自定义开始日期"`
  309. EndDate string `description:"自定义结束日期"`
  310. IsSetName int `description:"设置名称"`
  311. EdbInfoIds string `description:"指标id"`
  312. ExcelType int `description:"生成样式:1:曲线图,2:季节性图"`
  313. Calendar string `description:"公历/农历"`
  314. SeasonStartDate string `description:"季节性图开始日期"`
  315. SeasonEndDate string `description:"季节性图开始日期"`
  316. ExcelImage string `description:"表格图片"`
  317. Sort int `description:"排序字段,数字越小越排前面"`
  318. IsAdd bool `description:"true:已加入我的图库,false:未加入我的图库"`
  319. MyExcelId int
  320. MyExcelClassifyId string `description:"我的表格分类,多个用逗号隔开"`
  321. ExcelClassify []*ExcelClassifyView
  322. EdbEndDate string `description:"指标最新更新日期"`
  323. LeftMin string `description:"表格左侧最小值"`
  324. LeftMax string `description:"表格左侧最大值"`
  325. RightMin string `description:"表格右侧最小值"`
  326. RightMax string `description:"表格右侧最大值"`
  327. }
  328. // GetExcelInfoByClassifyIdAndName 根据分类id和表格名获取表格信息
  329. func GetExcelInfoByClassifyIdAndName(classifyId int, excelName string) (item *ExcelInfo, err error) {
  330. o := global.DbMap[utils.DbNameIndex]
  331. sql := ` SELECT * FROM excel_info WHERE excel_classify_id = ? and excel_name=? AND is_delete=0 `
  332. err = o.Raw(sql, classifyId, excelName).First(&item).Error
  333. return
  334. }
  335. // GetNoContentExcelListByCondition 获取没有content的excel表格列表数据
  336. func GetNoContentExcelListByCondition(condition string, pars []interface{}, startSize, pageSize int) (item []*MyExcelInfoList, err error) {
  337. sql := ` SELECT excel_info_id,source,excel_type,excel_name,unique_code,excel_classify_id,sys_user_id,sys_user_real_name,excel_image,file_url,sort,create_time,modify_time,is_join_permission,update_user_id,update_user_real_name
  338. FROM excel_info WHERE 1=1 AND is_delete=0 `
  339. if condition != "" {
  340. sql += condition
  341. }
  342. //sql += " ORDER BY sort ASC,chart_info_id DESC LIMIT ?,? "
  343. sql += " ORDER BY create_time DESC LIMIT ?,? "
  344. pars = append(pars, startSize)
  345. pars = append(pars, pageSize)
  346. err = global.DbMap[utils.DbNameIndex].Raw(sql, pars...).Find(&item).Error
  347. return
  348. }
  349. // GetNoContentExcelListByConditionNoPage 获取没有content的excel表格列表数据
  350. func GetNoContentExcelListByConditionNoPage(condition string, pars []interface{}) (item []*MyExcelInfoList, err error) {
  351. sql := ` SELECT excel_info_id,source,excel_type,excel_name,unique_code,excel_classify_id,sys_user_id,sys_user_real_name,excel_image,file_url,sort,create_time,modify_time,is_join_permission,update_user_id,update_user_real_name
  352. FROM excel_info WHERE 1=1 AND is_delete=0 `
  353. if condition != "" {
  354. sql += condition
  355. }
  356. //sql += " ORDER BY sort ASC,chart_info_id DESC LIMIT ?,? "
  357. sql += " ORDER BY create_time DESC"
  358. err = global.DbMap[utils.DbNameIndex].Raw(sql, pars...).Find(&item).Error
  359. return
  360. }
  361. func GetExcelListCountByCondition(condition string, pars []interface{}) (count int, err error) {
  362. sql := ` SELECT COUNT(1) AS count FROM excel_info WHERE 1=1 AND is_delete=0 `
  363. if condition != "" {
  364. sql += condition
  365. }
  366. err = global.DbMap[utils.DbNameIndex].Raw(sql, pars...).Scan(&count).Error
  367. return
  368. }
  369. // GetExcelViewInfoByExcelInfoId 根据excelInfoId 获取ETA表格详情
  370. func GetExcelViewInfoByExcelInfoId(excelInfoId int) (item *MyExcelInfoList, err error) {
  371. o := global.DbMap[utils.DbNameIndex]
  372. sql := ` SELECT excel_info_id,source,excel_type,excel_name,unique_code,excel_classify_id,sys_user_id,sys_user_real_name,excel_image,file_url,sort,create_time,modify_time,is_join_permission FROM excel_info WHERE excel_info_id = ? AND is_delete=0 `
  373. err = o.Raw(sql, excelInfoId).First(&item).Error
  374. return
  375. }
  376. // GetExcelInfoCountByClassifyId 根据分类id获取名下表格数量
  377. func GetExcelInfoCountByClassifyId(classifyId int) (total int64, err error) {
  378. sql := ` SELECT count(1) total FROM excel_info WHERE excel_classify_id = ? AND is_delete=0 `
  379. err = global.DbMap[utils.DbNameIndex].Raw(sql, classifyId).First(&total).Error
  380. return
  381. }
  382. // UpdateExcelInfoClassifyId 更改表格分类
  383. func UpdateExcelInfoClassifyId(classifyId, excelInfoId int) (err error) {
  384. o := global.DbMap[utils.DbNameIndex]
  385. sql := ` update excel_info set excel_classify_id = ? WHERE excel_info_id=? `
  386. err = o.Exec(sql, classifyId, excelInfoId).Error
  387. return
  388. }
  389. // GetNoContentExcelInfoByName 根据名称 获取eta表格详情
  390. func GetNoContentExcelInfoByName(excelName string, source, adminId int) (item *MyExcelInfoList, err error) {
  391. sql := ` SELECT excel_info_id,source,excel_type,excel_name,unique_code,excel_classify_id,sys_user_id,sys_user_real_name,excel_image,file_url,sort,create_time,modify_time,is_join_permission,update_user_id,update_user_real_name
  392. FROM excel_info WHERE excel_name = ? AND source = ? AND is_delete=0 AND sys_user_id = ? `
  393. err = global.DbMap[utils.DbNameIndex].Raw(sql, excelName, source, adminId).First(&item).Error
  394. return
  395. }
  396. // GetNoContentExcelInfoByUniqueCode 根据unique_code来获取excel表格详情
  397. func GetNoContentExcelInfoByUniqueCode(uniqueCode string) (item *MyExcelInfoList, err error) {
  398. sql := ` SELECT excel_info_id,source,excel_type,excel_name,unique_code,excel_classify_id,sys_user_id,sys_user_real_name,excel_image,file_url,sort,create_time,modify_time,is_join_permission,update_user_id,update_user_real_name
  399. FROM excel_info WHERE unique_code=? AND is_delete=0 `
  400. err = global.DbMap[utils.DbNameIndex].Raw(sql, uniqueCode).First(&item).Error
  401. return
  402. }
  403. // GetNoContentExcelInfoByExcelId 根据表格id来获取excel表格详情
  404. func GetNoContentExcelInfoByExcelId(excelInfoId int) (item *MyExcelInfoList, err error) {
  405. sql := ` SELECT excel_info_id,source,excel_type,excel_name,unique_code,excel_classify_id,sys_user_id,sys_user_real_name,excel_image,file_url,sort,create_time,modify_time,is_join_permission,update_user_id,update_user_real_name
  406. FROM excel_info WHERE excel_info_id=? AND is_delete=0 `
  407. err = global.DbMap[utils.DbNameIndex].Raw(sql, excelInfoId).First(&item).Error
  408. return
  409. }
  410. // AddExcelInfoAndSheet 新增excel
  411. func AddExcelInfoAndSheet(excelInfo *ExcelInfo, sheetParamsList []AddExcelSheetParams) (err error) {
  412. to := global.DbMap[utils.DbNameIndex].Begin()
  413. defer func() {
  414. if err != nil {
  415. _ = to.Rollback()
  416. } else {
  417. _ = to.Commit()
  418. }
  419. }()
  420. // 表格信息入库
  421. err = to.Create(excelInfo).Error
  422. if err != nil {
  423. return
  424. }
  425. // sheet信息入库
  426. for _, sheetInfo := range sheetParamsList {
  427. dataNum := len(sheetInfo.DataList)
  428. //sheet信息入库
  429. excelSheetInfo := &ExcelSheet{
  430. ExcelSheetId: 0,
  431. ExcelInfoId: excelInfo.ExcelInfoId,
  432. SheetName: sheetInfo.SheetName,
  433. PageNum: dataNum,
  434. Index: sheetInfo.Index,
  435. Sort: sheetInfo.Sort,
  436. Config: sheetInfo.Config,
  437. CalcChain: sheetInfo.CalcChain,
  438. ModifyTime: time.Now(),
  439. CreateTime: time.Now(),
  440. Frozen: sheetInfo.Frozen,
  441. }
  442. tmpErr := to.Create(excelSheetInfo).Error
  443. if tmpErr != nil {
  444. err = tmpErr
  445. return
  446. }
  447. // data信息入库
  448. if dataNum > 0 {
  449. for k, _ := range sheetInfo.DataList {
  450. sheetInfo.DataList[k].ExcelSheetId = excelSheetInfo.ExcelSheetId
  451. sheetInfo.DataList[k].ExcelInfoId = excelSheetInfo.ExcelInfoId
  452. }
  453. tmpErr = to.CreateInBatches(sheetInfo.DataList, utils.MultiAddNum).Error
  454. if tmpErr != nil {
  455. err = tmpErr
  456. return
  457. }
  458. }
  459. }
  460. return
  461. }
  462. // SaveExcelInfoAndSheet 编辑保存
  463. func SaveExcelInfoAndSheet(excelInfo *ExcelInfo, updateExcelInfoParam []string, sheetParamsList []AddExcelSheetParams) (err error) {
  464. to := global.DbMap[utils.DbNameIndex].Begin()
  465. defer func() {
  466. if err != nil {
  467. _ = to.Rollback()
  468. } else {
  469. _ = to.Commit()
  470. }
  471. }()
  472. // 表格信息入库
  473. err = to.Select(updateExcelInfoParam).Updates(excelInfo).Error
  474. if err != nil {
  475. return
  476. }
  477. // 先删除历史的sheet信息
  478. sql := `DELETE FROM excel_sheet WHERE excel_info_id = ?`
  479. err = to.Exec(sql, excelInfo.ExcelInfoId).Error
  480. if err != nil {
  481. return
  482. }
  483. // 再删除历史sheet中的cell data信息
  484. sql = `DELETE FROM excel_sheet_data WHERE excel_info_id = ?`
  485. err = to.Exec(sql, excelInfo.ExcelInfoId).Error
  486. if err != nil {
  487. return
  488. }
  489. // sheet信息入库
  490. for _, sheetInfo := range sheetParamsList {
  491. dataNum := len(sheetInfo.DataList)
  492. //sheet信息入库
  493. excelSheetInfo := &ExcelSheet{
  494. ExcelSheetId: 0,
  495. ExcelInfoId: excelInfo.ExcelInfoId,
  496. SheetName: sheetInfo.SheetName,
  497. PageNum: dataNum,
  498. Index: sheetInfo.Index,
  499. Sort: sheetInfo.Sort,
  500. Config: sheetInfo.Config,
  501. CalcChain: sheetInfo.CalcChain,
  502. ModifyTime: time.Now(),
  503. CreateTime: time.Now(),
  504. Frozen: sheetInfo.Frozen,
  505. }
  506. tmpErr := to.Create(excelSheetInfo).Error
  507. if tmpErr != nil {
  508. err = tmpErr
  509. return
  510. }
  511. // data信息入库
  512. if dataNum > 0 {
  513. for k, _ := range sheetInfo.DataList {
  514. sheetInfo.DataList[k].ExcelSheetId = excelSheetInfo.ExcelSheetId
  515. sheetInfo.DataList[k].ExcelInfoId = excelSheetInfo.ExcelInfoId
  516. }
  517. tmpErr = to.CreateInBatches(sheetInfo.DataList, utils.MultiAddNum).Error
  518. }
  519. }
  520. return
  521. }
  522. // BatchRefreshExcelReq 批量刷新表格请求
  523. type BatchRefreshExcelReq struct {
  524. ExcelCodes []string `description:"表格编码"`
  525. PrimaryId int `description:"研报/智能研报/PPT主键"`
  526. SubId int `description:"报告章节ID"`
  527. Source string `description:"来源,枚举值:report、english_report、smart_report"`
  528. }
  529. // GetExcelMaxSortByClassifyId 获取当前分类下,且排序数最大的excel
  530. func GetExcelMaxSortByClassifyId(classifyId int, source int) (sort int, err error) {
  531. sql := ` SELECT COALESCE(MAX(sort),0) AS sort FROM excel_info WHERE excel_classify_id=? AND source = ? AND is_delete=0 order by sort desc,excel_info_id desc limit 1`
  532. err = global.DbMap[utils.DbNameIndex].Raw(sql, classifyId, source).Scan(&sort).Error
  533. return
  534. }
  535. // GetNoContentExcelListByExcelInfoIdList
  536. // @Description: 根据ETA表格ID列表获取列表信息
  537. // @param excelInfoIdList []string
  538. // @return items
  539. // @return err
  540. func GetNoContentExcelListByExcelInfoIdList(excelInfoIdList []string) (items []*MyExcelInfoList, err error) {
  541. num := len(excelInfoIdList)
  542. if num <= 0 {
  543. return
  544. }
  545. sql := ` SELECT * FROM excel_info WHERE excel_info_id in (` + utils.GetOrmInReplace(num) + `) order by excel_info_id DESC `
  546. err = global.DbMap[utils.DbNameIndex].Raw(sql, excelInfoIdList).Find(&items).Error
  547. return
  548. }
  549. func GetNoContentExcelListByExcelInfoIdAndParentId(excelInfoIdList []string) (items []*MyExcelInfoList, err error) {
  550. num := len(excelInfoIdList)
  551. if num <= 0 {
  552. return
  553. }
  554. sql := ` SELECT * FROM excel_info WHERE excel_info_id in (` + utils.GetOrmInReplace(num) + `) OR parent_id in (` + utils.GetOrmInReplace(num) + `) order by excel_info_id DESC `
  555. err = global.DbMap[utils.DbNameIndex].Raw(sql, excelInfoIdList, excelInfoIdList).Find(&items).Error
  556. return
  557. }
  558. // GetNoContentExcelListByUserId
  559. // @Description: 根据ETA表格ID列表获取列表信息
  560. // @param userIdList []int
  561. // @return items
  562. // @return err
  563. func GetNoContentExcelListByUserId(userIdList []int) (items []*MyExcelInfoList, err error) {
  564. num := len(userIdList)
  565. if num <= 0 {
  566. return
  567. }
  568. sql := ` SELECT * FROM excel_info WHERE sys_user_id in (` + utils.GetOrmInReplace(num) + `) order by excel_info_id DESC `
  569. err = global.DbMap[utils.DbNameIndex].Raw(sql, userIdList).Find(&items).Error
  570. return
  571. }
  572. // ModifyExcelInfoUserIdByCodeList
  573. // @Description: 根据编码列表修改表格信息
  574. // @author: Roc
  575. // @datetime 2024-03-26 14:16:30
  576. // @param excelIdList []string
  577. // @param userId int
  578. // @param userName string
  579. // @return err error
  580. func ModifyExcelInfoUserIdByCodeList(excelIdList []string, userId int, userName string) (err error) {
  581. num := len(excelIdList)
  582. if num <= 0 {
  583. return
  584. }
  585. sql := `UPDATE excel_info SET sys_user_id=?,sys_user_real_name=? WHERE excel_info_id in (` + utils.GetOrmInReplace(num) + `) `
  586. err = global.DbMap[utils.DbNameIndex].Exec(sql, userId, userName, excelIdList).Error
  587. return
  588. }
  589. // ModifyExcelInfoUserIdByOldUserId
  590. // @Description: 根据旧的用户id修改创建人
  591. // @author: Roc
  592. // @datetime 2024-03-25 19:14:59
  593. // @param oldUserId int
  594. // @param userId int
  595. // @param userName string
  596. // @return err error
  597. func ModifyExcelInfoUserIdByOldUserId(oldUserIdList []int, userId int, userName string) (err error) {
  598. num := len(oldUserIdList)
  599. if num <= 0 {
  600. return
  601. }
  602. sql := `UPDATE excel_info SET sys_user_id=?,sys_user_real_name=? WHERE is_delete=0 AND sys_user_id in (` + utils.GetOrmInReplace(num) + `) `
  603. err = global.DbMap[utils.DbNameIndex].Exec(sql, userId, userName, oldUserIdList).Error
  604. return
  605. }
  606. func GetExcelBaseInfoByExcelInfoIdList(excelInfoIdList []int) (items []*data_manage.ExcelBaseInfo, err error) {
  607. num := len(excelInfoIdList)
  608. if num <= 0 {
  609. return
  610. }
  611. sql := ` SELECT excel_info_id,source,excel_type,excel_name,unique_code,excel_classify_id,sys_user_id,sys_user_real_name FROM excel_info WHERE excel_info_id in (` + utils.GetOrmInReplace(num) + `) order by excel_info_id DESC `
  612. err = global.DbMap[utils.DbNameIndex].Raw(sql, excelInfoIdList).Find(&items).Error
  613. return
  614. }
  615. // ReplaceEdbInExcel 替换表格中的指标
  616. func ReplaceEdbInExcel(oldEdbInfoId, newEdbInfoId int, updateExcelList []*ExcelInfo) (err error) {
  617. var errmsg string
  618. logMsg := `` // 记录替换的日志
  619. replaceTotal := 0
  620. to := global.DbMap[utils.DbNameIndex].Begin()
  621. defer func() {
  622. if err != nil {
  623. _ = to.Rollback()
  624. } else {
  625. _ = to.Commit()
  626. if logMsg != `` {
  627. utils.FileLog.Info(fmt.Sprintf("替换表格中的指标记录,替换总表格数:%d,旧的指标id:%d,新的指标id:%d;%s", replaceTotal, oldEdbInfoId, newEdbInfoId, logMsg))
  628. }
  629. }
  630. if errmsg != "" {
  631. fmt.Println("errmsg:" + errmsg)
  632. }
  633. if err != nil && errmsg != "" {
  634. //go alarm_msg.SendAlarmMsg("替换替换配置中的指标记录失败提醒,errmsg:"+errmsg, 3)
  635. }
  636. }()
  637. // 替换表格中的内容
  638. for _, excelInfo := range updateExcelList {
  639. //更新配置中的指标A
  640. sql := `UPDATE excel_info SET content=?, modify_time=? WHERE excel_info_id=?`
  641. err = to.Exec(sql, excelInfo.Content, time.Now(), excelInfo.ExcelInfoId).Error
  642. if err != nil {
  643. errmsg = "更新表格内容失败:Err:" + err.Error()
  644. return
  645. }
  646. //更新配置中的指标B
  647. // 判断是否已存在绑定,如果存在则删除
  648. sql = `DELETE FROM excel_edb_mapping WHERE excel_info_id=? and edb_info_id=?`
  649. err = to.Exec(sql, excelInfo.ExcelInfoId, newEdbInfoId).Error
  650. if err != nil {
  651. errmsg = "删除指标B关联图表配置信息失败:Err:" + err.Error()
  652. return
  653. }
  654. // 插入新的绑定
  655. sql = `UPDATE excel_edb_mapping SET edb_info_id=?, modify_time=? WHERE excel_info_id=? and edb_info_id=?`
  656. err = to.Exec(sql, newEdbInfoId, time.Now(), excelInfo.ExcelInfoId, oldEdbInfoId).Error
  657. if err != nil {
  658. errmsg = "更新指标B关联图表配置信息失败:Err:" + err.Error()
  659. return
  660. }
  661. logMsg += `涉及到的表格id:` + strconv.Itoa(excelInfo.ExcelInfoId) + ";"
  662. replaceTotal += 1
  663. }
  664. return
  665. }
  666. // GetChildExcelInfoByParentId 根据id 获取eta表格详情
  667. func GetChildExcelInfoByParentId(parentId int) (items []*ExcelInfo, err error) {
  668. sql := ` SELECT excel_info_id,source,excel_type,excel_name,unique_code,excel_classify_id,sys_user_id,sys_user_real_name,excel_image,file_url,sort,create_time,modify_time,is_join_permission, parent_id, balance_type, update_user_id,update_user_real_name FROM excel_info WHERE parent_id=? AND is_delete=0 order by sort asc, excel_info_id asc`
  669. err = global.DbMap[utils.DbNameIndex].Raw(sql, parentId).Find(&items).Error
  670. return
  671. }
  672. // ExcelInfoDetailButton 操作按钮
  673. type ExcelInfoDetailButton struct {
  674. RefreshButton bool `description:"是否可刷新"`
  675. CopyButton bool `description:"是否可另存为"`
  676. DownloadButton bool `description:"是否可下载"`
  677. OpButton bool `description:"是否可编辑"`
  678. DeleteButton bool `description:"是否可删除"`
  679. OpEdbButton bool `description:"是否可生成指标"`
  680. RefreshEdbButton bool `description:"是否可刷新指标"`
  681. OpWorkerButton bool `description:"是否修改协作人"`
  682. }
  683. func UpdateExcelInfoClassifyIdByIds(classifyId int, excelIds []int) (err error) {
  684. if len(excelIds) == 0 {
  685. return
  686. }
  687. sql := fmt.Sprintf(`UPDATE excel_info SET excel_classify_id = ? WHERE excel_info_id IN (%s)`, utils.GetOrmInReplace(len(excelIds)))
  688. err = global.DbMap[utils.DbNameIndex].Exec(sql, classifyId, excelIds).Error
  689. return
  690. }
  691. // SearchExcelInfo 表格搜索
  692. type SearchExcelInfo struct {
  693. ExcelInfo
  694. SearchText string `description:"搜索结果(含高亮)"`
  695. HaveOperaAuth bool `description:"是否有数据权限"`
  696. Button ExcelInfoDetailButton `description:"操作权限"`
  697. CanEdit bool `description:"是否可编辑"`
  698. Editor string `description:"编辑人"`
  699. }
  700. func (m *ExcelInfo) Create() (err error) {
  701. o := global.DbMap[utils.DbNameIndex]
  702. err = o.Create(m).Error
  703. return
  704. }
  705. func (m *ExcelInfo) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
  706. o := global.DbMap[utils.DbNameIndex]
  707. sql := fmt.Sprintf(`SELECT COUNT(1) FROM excel_info WHERE 1=1 %s`, condition)
  708. // err = o.Raw(sql, pars...).QueryRow(&count)
  709. err = o.Raw(sql, pars...).Scan(&count).Error
  710. return
  711. }
  712. // ExcelCommonExtraConfig 表格额外配置
  713. type ExcelCommonExtraConfig struct {
  714. TableFreeze ExcelInfoFreeze `description:"表格冻结"`
  715. }
  716. // ExcelInfoFreeze 表格冻结
  717. type ExcelInfoFreeze struct {
  718. FreezeFirstRow bool `description:"冻结首行"`
  719. FreezeFirstCol bool `description:"冻结首列"`
  720. FreezeStartRow int `description:"冻结开始行"`
  721. FreezeEndRow int `description:"冻结结束行"`
  722. FreezeStartCol int `description:"冻结开始列"`
  723. FreezeEndCol int `description:"冻结结束列"`
  724. }