excel_info.go 31 KB

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