excel_info.go 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  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. }
  467. sheetId, tmpErr := o.Insert(excelSheetInfo)
  468. if tmpErr != nil {
  469. err = tmpErr
  470. return
  471. }
  472. excelSheetInfo.ExcelSheetId = int(sheetId)
  473. // data信息入库
  474. if dataNum > 0 {
  475. for k, _ := range sheetInfo.DataList {
  476. sheetInfo.DataList[k].ExcelSheetId = excelSheetInfo.ExcelSheetId
  477. sheetInfo.DataList[k].ExcelInfoId = excelSheetInfo.ExcelInfoId
  478. }
  479. _, tmpErr = o.InsertMulti(dataNum, sheetInfo.DataList)
  480. if tmpErr != nil {
  481. err = tmpErr
  482. return
  483. }
  484. }
  485. }
  486. return
  487. }
  488. // SaveExcelInfoAndSheet 编辑保存
  489. func SaveExcelInfoAndSheet(excelInfo *ExcelInfo, updateExcelInfoParam []string, sheetParamsList []AddExcelSheetParams) (err error) {
  490. o, err := orm.NewOrmUsingDB("data").Begin()
  491. if err != nil {
  492. return
  493. }
  494. defer func() {
  495. if err != nil {
  496. _ = o.Rollback()
  497. } else {
  498. _ = o.Commit()
  499. }
  500. }()
  501. // 表格信息入库
  502. _, err = o.Update(excelInfo, updateExcelInfoParam...)
  503. if err != nil {
  504. return
  505. }
  506. // 先删除历史的sheet信息
  507. sql := `DELETE FROM excel_sheet WHERE excel_info_id = ?`
  508. _, err = o.Raw(sql, excelInfo.ExcelInfoId).Exec()
  509. if err != nil {
  510. return
  511. }
  512. // 再删除历史sheet中的cell data信息
  513. sql = `DELETE FROM excel_sheet_data WHERE excel_info_id = ?`
  514. _, err = o.Raw(sql, excelInfo.ExcelInfoId).Exec()
  515. if err != nil {
  516. return
  517. }
  518. // sheet信息入库
  519. for _, sheetInfo := range sheetParamsList {
  520. dataNum := len(sheetInfo.DataList)
  521. //sheet信息入库
  522. excelSheetInfo := &ExcelSheet{
  523. ExcelSheetId: 0,
  524. ExcelInfoId: excelInfo.ExcelInfoId,
  525. SheetName: sheetInfo.SheetName,
  526. PageNum: dataNum,
  527. Index: sheetInfo.Index,
  528. Sort: sheetInfo.Sort,
  529. Config: sheetInfo.Config,
  530. CalcChain: sheetInfo.CalcChain,
  531. ModifyTime: time.Now(),
  532. CreateTime: time.Now(),
  533. }
  534. sheetId, tmpErr := o.Insert(excelSheetInfo)
  535. if tmpErr != nil {
  536. err = tmpErr
  537. return
  538. }
  539. excelSheetInfo.ExcelSheetId = int(sheetId)
  540. // data信息入库
  541. if dataNum > 0 {
  542. for k, _ := range sheetInfo.DataList {
  543. sheetInfo.DataList[k].ExcelSheetId = excelSheetInfo.ExcelSheetId
  544. sheetInfo.DataList[k].ExcelInfoId = excelSheetInfo.ExcelInfoId
  545. }
  546. _, tmpErr = o.InsertMulti(dataNum, sheetInfo.DataList)
  547. if tmpErr != nil {
  548. err = tmpErr
  549. return
  550. }
  551. }
  552. }
  553. return
  554. }
  555. // BatchRefreshExcelReq 批量刷新表格请求
  556. type BatchRefreshExcelReq struct {
  557. ExcelCodes []string `description:"表格编码"`
  558. PrimaryId int `description:"研报/智能研报/PPT主键"`
  559. SubId int `description:"报告章节ID"`
  560. Source string `description:"来源,枚举值:report、english_report、smart_report"`
  561. }
  562. // GetExcelMaxSortByClassifyId 获取当前分类下,且排序数最大的excel
  563. func GetExcelMaxSortByClassifyId(classifyId int, source int) (sort int, err error) {
  564. o := orm.NewOrmUsingDB("data")
  565. 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`
  566. err = o.Raw(sql, classifyId, source).QueryRow(&sort)
  567. return
  568. }
  569. // GetNoContentExcelListByExcelInfoIdList
  570. // @Description: 根据ETA表格ID列表获取列表信息
  571. // @param excelInfoIdList []string
  572. // @return items
  573. // @return err
  574. func GetNoContentExcelListByExcelInfoIdList(excelInfoIdList []string) (items []*MyExcelInfoList, err error) {
  575. num := len(excelInfoIdList)
  576. if num <= 0 {
  577. return
  578. }
  579. o := orm.NewOrmUsingDB("data")
  580. sql := ` SELECT * FROM excel_info WHERE excel_info_id in (` + utils.GetOrmInReplace(num) + `) order by excel_info_id DESC `
  581. _, err = o.Raw(sql, excelInfoIdList).QueryRows(&items)
  582. return
  583. }
  584. func GetNoContentExcelListByExcelInfoIdAndParentId(excelInfoIdList []string) (items []*MyExcelInfoList, err error) {
  585. num := len(excelInfoIdList)
  586. if num <= 0 {
  587. return
  588. }
  589. o := orm.NewOrmUsingDB("data")
  590. 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 `
  591. _, err = o.Raw(sql, excelInfoIdList, excelInfoIdList).QueryRows(&items)
  592. return
  593. }
  594. // GetNoContentExcelListByUserId
  595. // @Description: 根据ETA表格ID列表获取列表信息
  596. // @param userIdList []int
  597. // @return items
  598. // @return err
  599. func GetNoContentExcelListByUserId(userIdList []int) (items []*MyExcelInfoList, err error) {
  600. num := len(userIdList)
  601. if num <= 0 {
  602. return
  603. }
  604. o := orm.NewOrmUsingDB("data")
  605. sql := ` SELECT * FROM excel_info WHERE sys_user_id in (` + utils.GetOrmInReplace(num) + `) order by excel_info_id DESC `
  606. _, err = o.Raw(sql, userIdList).QueryRows(&items)
  607. return
  608. }
  609. // ModifyExcelInfoUserIdByCodeList
  610. // @Description: 根据编码列表修改表格信息
  611. // @author: Roc
  612. // @datetime 2024-03-26 14:16:30
  613. // @param excelIdList []string
  614. // @param userId int
  615. // @param userName string
  616. // @return err error
  617. func ModifyExcelInfoUserIdByCodeList(excelIdList []string, userId int, userName string) (err error) {
  618. num := len(excelIdList)
  619. if num <= 0 {
  620. return
  621. }
  622. o := orm.NewOrmUsingDB("data")
  623. sql := `UPDATE excel_info SET sys_user_id=?,sys_user_real_name=? WHERE excel_info_id in (` + utils.GetOrmInReplace(num) + `) `
  624. _, err = o.Raw(sql, userId, userName, excelIdList).Exec()
  625. return
  626. }
  627. // ModifyExcelInfoUserIdByOldUserId
  628. // @Description: 根据旧的用户id修改创建人
  629. // @author: Roc
  630. // @datetime 2024-03-25 19:14:59
  631. // @param oldUserId int
  632. // @param userId int
  633. // @param userName string
  634. // @return err error
  635. func ModifyExcelInfoUserIdByOldUserId(oldUserIdList []int, userId int, userName string) (err error) {
  636. num := len(oldUserIdList)
  637. if num <= 0 {
  638. return
  639. }
  640. o := orm.NewOrmUsingDB("data")
  641. sql := `UPDATE excel_info SET sys_user_id=?,sys_user_real_name=? WHERE is_delete=0 AND sys_user_id in (` + utils.GetOrmInReplace(num) + `) `
  642. _, err = o.Raw(sql, userId, userName, oldUserIdList).Exec()
  643. return
  644. }
  645. func GetExcelBaseInfoByExcelInfoIdList(excelInfoIdList []int) (items []*data_manage.ExcelBaseInfo, err error) {
  646. num := len(excelInfoIdList)
  647. if num <= 0 {
  648. return
  649. }
  650. o := orm.NewOrmUsingDB("data")
  651. 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 `
  652. _, err = o.Raw(sql, excelInfoIdList).QueryRows(&items)
  653. return
  654. }
  655. // ReplaceEdbInExcel 替换表格中的指标
  656. func ReplaceEdbInExcel(oldEdbInfoId, newEdbInfoId int, updateExcelList []*ExcelInfo) (err error) {
  657. var errmsg string
  658. logMsg := `` // 记录替换的日志
  659. replaceTotal := 0
  660. o := orm.NewOrmUsingDB("data")
  661. to, err := o.Begin()
  662. if err != nil {
  663. return
  664. }
  665. defer func() {
  666. if err != nil {
  667. _ = to.Rollback()
  668. } else {
  669. _ = to.Commit()
  670. if logMsg != `` {
  671. utils.FileLog.Info(fmt.Sprintf("替换表格中的指标记录,替换总表格数:%d,旧的指标id:%d,新的指标id:%d;%s", replaceTotal, oldEdbInfoId, newEdbInfoId, logMsg))
  672. }
  673. }
  674. if errmsg != "" {
  675. fmt.Println("errmsg:" + errmsg)
  676. }
  677. if err != nil && errmsg != "" {
  678. //go alarm_msg.SendAlarmMsg("替换替换配置中的指标记录失败提醒,errmsg:"+errmsg, 3)
  679. }
  680. }()
  681. // 替换表格中的内容
  682. for _, excelInfo := range updateExcelList {
  683. //更新配置中的指标A
  684. sql := `UPDATE excel_info SET content=?, modify_time=? WHERE excel_info_id=?`
  685. _, err = to.Raw(sql, excelInfo.Content, time.Now(), excelInfo.ExcelInfoId).Exec()
  686. if err != nil {
  687. errmsg = "更新表格内容失败:Err:" + err.Error()
  688. return
  689. }
  690. //更新配置中的指标B
  691. // 判断是否已存在绑定,如果存在则删除
  692. sql = `DELETE FROM excel_edb_mapping WHERE excel_info_id=? and edb_info_id=?`
  693. _, err = to.Raw(sql, excelInfo.ExcelInfoId, newEdbInfoId).Exec()
  694. if err != nil {
  695. errmsg = "删除指标B关联图表配置信息失败:Err:" + err.Error()
  696. return
  697. }
  698. // 插入新的绑定
  699. sql = `UPDATE excel_edb_mapping SET edb_info_id=?, modify_time=? WHERE excel_info_id=? and edb_info_id=?`
  700. _, err = to.Raw(sql, newEdbInfoId, time.Now(), excelInfo.ExcelInfoId, oldEdbInfoId).Exec()
  701. if err != nil {
  702. errmsg = "更新指标B关联图表配置信息失败:Err:" + err.Error()
  703. return
  704. }
  705. logMsg += `涉及到的表格id:` + strconv.Itoa(excelInfo.ExcelInfoId) + ";"
  706. replaceTotal += 1
  707. }
  708. return
  709. }
  710. // GetChildExcelInfoByParentId 根据id 获取eta表格详情
  711. func GetChildExcelInfoByParentId(parentId int) (items []*ExcelInfo, err error) {
  712. o := orm.NewOrmUsingDB("data")
  713. 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`
  714. _, err = o.Raw(sql, parentId).QueryRows(&items)
  715. return
  716. }
  717. // ExcelInfoDetailButton 操作按钮
  718. type ExcelInfoDetailButton struct {
  719. RefreshButton bool `description:"是否可刷新"`
  720. CopyButton bool `description:"是否可另存为"`
  721. DownloadButton bool `description:"是否可下载"`
  722. OpButton bool `description:"是否可编辑"`
  723. DeleteButton bool `description:"是否可删除"`
  724. OpEdbButton bool `description:"是否可生成指标"`
  725. RefreshEdbButton bool `description:"是否可刷新指标"`
  726. OpWorkerButton bool `description:"是否修改协作人"`
  727. }
  728. func UpdateExcelInfoClassifyIdByIds(classifyId int, excelIds []int) (err error) {
  729. if len(excelIds) == 0 {
  730. return
  731. }
  732. o := orm.NewOrmUsingDB("data")
  733. sql := fmt.Sprintf(`UPDATE excel_info SET excel_classify_id = ? WHERE excel_info_id IN (%s)`, utils.GetOrmInReplace(len(excelIds)))
  734. _, err = o.Raw(sql, classifyId, excelIds).Exec()
  735. return
  736. }