excel_info.go 31 KB

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