excel_info.go 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  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. childId, e := o.Insert(childExcel)
  88. if e != nil {
  89. err = fmt.Errorf("新增子表失败:%v", e)
  90. return
  91. }
  92. childExcel.ExcelInfoId = int(childId)
  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. }
  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. }
  535. sheetId, tmpErr := o.Insert(excelSheetInfo)
  536. if tmpErr != nil {
  537. err = tmpErr
  538. return
  539. }
  540. excelSheetInfo.ExcelSheetId = int(sheetId)
  541. // data信息入库
  542. if dataNum > 0 {
  543. for k, _ := range sheetInfo.DataList {
  544. sheetInfo.DataList[k].ExcelSheetId = excelSheetInfo.ExcelSheetId
  545. sheetInfo.DataList[k].ExcelInfoId = excelSheetInfo.ExcelInfoId
  546. }
  547. _, tmpErr = o.InsertMulti(dataNum, sheetInfo.DataList)
  548. if tmpErr != nil {
  549. err = tmpErr
  550. return
  551. }
  552. }
  553. }
  554. return
  555. }
  556. // BatchRefreshExcelReq 批量刷新表格请求
  557. type BatchRefreshExcelReq struct {
  558. ExcelCodes []string `description:"表格编码"`
  559. PrimaryId int `description:"研报/智能研报/PPT主键"`
  560. SubId int `description:"报告章节ID"`
  561. Source string `description:"来源,枚举值:report、english_report、smart_report"`
  562. }
  563. // GetExcelMaxSortByClassifyId 获取当前分类下,且排序数最大的excel
  564. func GetExcelMaxSortByClassifyId(classifyId int, source int) (sort int, err error) {
  565. o := orm.NewOrmUsingDB("data")
  566. 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`
  567. err = o.Raw(sql, classifyId, source).QueryRow(&sort)
  568. return
  569. }
  570. // GetNoContentExcelListByExcelInfoIdList
  571. // @Description: 根据ETA表格ID列表获取列表信息
  572. // @param excelInfoIdList []string
  573. // @return items
  574. // @return err
  575. func GetNoContentExcelListByExcelInfoIdList(excelInfoIdList []string) (items []*MyExcelInfoList, err error) {
  576. num := len(excelInfoIdList)
  577. if num <= 0 {
  578. return
  579. }
  580. o := orm.NewOrmUsingDB("data")
  581. sql := ` SELECT * FROM excel_info WHERE excel_info_id in (` + utils.GetOrmInReplace(num) + `) order by excel_info_id DESC `
  582. _, err = o.Raw(sql, excelInfoIdList).QueryRows(&items)
  583. return
  584. }
  585. func GetNoContentExcelListByExcelInfoIdAndParentId(excelInfoIdList []string) (items []*MyExcelInfoList, err error) {
  586. num := len(excelInfoIdList)
  587. if num <= 0 {
  588. return
  589. }
  590. o := orm.NewOrmUsingDB("data")
  591. 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 `
  592. _, err = o.Raw(sql, excelInfoIdList, excelInfoIdList).QueryRows(&items)
  593. return
  594. }
  595. // GetNoContentExcelListByUserId
  596. // @Description: 根据ETA表格ID列表获取列表信息
  597. // @param userIdList []int
  598. // @return items
  599. // @return err
  600. func GetNoContentExcelListByUserId(userIdList []int) (items []*MyExcelInfoList, err error) {
  601. num := len(userIdList)
  602. if num <= 0 {
  603. return
  604. }
  605. o := orm.NewOrmUsingDB("data")
  606. sql := ` SELECT * FROM excel_info WHERE sys_user_id in (` + utils.GetOrmInReplace(num) + `) order by excel_info_id DESC `
  607. _, err = o.Raw(sql, userIdList).QueryRows(&items)
  608. return
  609. }
  610. // ModifyExcelInfoUserIdByCodeList
  611. // @Description: 根据编码列表修改表格信息
  612. // @author: Roc
  613. // @datetime 2024-03-26 14:16:30
  614. // @param excelIdList []string
  615. // @param userId int
  616. // @param userName string
  617. // @return err error
  618. func ModifyExcelInfoUserIdByCodeList(excelIdList []string, userId int, userName string) (err error) {
  619. num := len(excelIdList)
  620. if num <= 0 {
  621. return
  622. }
  623. o := orm.NewOrmUsingDB("data")
  624. sql := `UPDATE excel_info SET sys_user_id=?,sys_user_real_name=? WHERE excel_info_id in (` + utils.GetOrmInReplace(num) + `) `
  625. _, err = o.Raw(sql, userId, userName, excelIdList).Exec()
  626. return
  627. }
  628. // ModifyExcelInfoUserIdByOldUserId
  629. // @Description: 根据旧的用户id修改创建人
  630. // @author: Roc
  631. // @datetime 2024-03-25 19:14:59
  632. // @param oldUserId int
  633. // @param userId int
  634. // @param userName string
  635. // @return err error
  636. func ModifyExcelInfoUserIdByOldUserId(oldUserIdList []int, userId int, userName string) (err error) {
  637. num := len(oldUserIdList)
  638. if num <= 0 {
  639. return
  640. }
  641. o := orm.NewOrmUsingDB("data")
  642. sql := `UPDATE excel_info SET sys_user_id=?,sys_user_real_name=? WHERE is_delete=0 AND sys_user_id in (` + utils.GetOrmInReplace(num) + `) `
  643. _, err = o.Raw(sql, userId, userName, oldUserIdList).Exec()
  644. return
  645. }
  646. func GetExcelBaseInfoByExcelInfoIdList(excelInfoIdList []int) (items []*data_manage.ExcelBaseInfo, err error) {
  647. num := len(excelInfoIdList)
  648. if num <= 0 {
  649. return
  650. }
  651. o := orm.NewOrmUsingDB("data")
  652. 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 `
  653. _, err = o.Raw(sql, excelInfoIdList).QueryRows(&items)
  654. return
  655. }
  656. // ReplaceEdbInExcel 替换表格中的指标
  657. func ReplaceEdbInExcel(oldEdbInfoId, newEdbInfoId int, updateExcelList []*ExcelInfo) (err error) {
  658. var errmsg string
  659. logMsg := `` // 记录替换的日志
  660. replaceTotal := 0
  661. o := orm.NewOrmUsingDB("data")
  662. to, err := o.Begin()
  663. if err != nil {
  664. return
  665. }
  666. defer func() {
  667. if err != nil {
  668. _ = to.Rollback()
  669. } else {
  670. _ = to.Commit()
  671. if logMsg != `` {
  672. utils.FileLog.Info(fmt.Sprintf("替换表格中的指标记录,替换总表格数:%d,旧的指标id:%d,新的指标id:%d;%s", replaceTotal, oldEdbInfoId, newEdbInfoId, logMsg))
  673. }
  674. }
  675. if errmsg != "" {
  676. fmt.Println("errmsg:" + errmsg)
  677. }
  678. if err != nil && errmsg != "" {
  679. //go alarm_msg.SendAlarmMsg("替换替换配置中的指标记录失败提醒,errmsg:"+errmsg, 3)
  680. }
  681. }()
  682. // 替换表格中的内容
  683. for _, excelInfo := range updateExcelList {
  684. //更新配置中的指标A
  685. sql := `UPDATE excel_info SET content=?, modify_time=? WHERE excel_info_id=?`
  686. _, err = to.Raw(sql, excelInfo.Content, time.Now(), excelInfo.ExcelInfoId).Exec()
  687. if err != nil {
  688. errmsg = "更新表格内容失败:Err:" + err.Error()
  689. return
  690. }
  691. //更新配置中的指标B
  692. // 判断是否已存在绑定,如果存在则删除
  693. sql = `DELETE FROM excel_edb_mapping WHERE excel_info_id=? and edb_info_id=?`
  694. _, err = to.Raw(sql, excelInfo.ExcelInfoId, newEdbInfoId).Exec()
  695. if err != nil {
  696. errmsg = "删除指标B关联图表配置信息失败:Err:" + err.Error()
  697. return
  698. }
  699. // 插入新的绑定
  700. sql = `UPDATE excel_edb_mapping SET edb_info_id=?, modify_time=? WHERE excel_info_id=? and edb_info_id=?`
  701. _, err = to.Raw(sql, newEdbInfoId, time.Now(), excelInfo.ExcelInfoId, oldEdbInfoId).Exec()
  702. if err != nil {
  703. errmsg = "更新指标B关联图表配置信息失败:Err:" + err.Error()
  704. return
  705. }
  706. logMsg += `涉及到的表格id:` + strconv.Itoa(excelInfo.ExcelInfoId) + ";"
  707. replaceTotal += 1
  708. }
  709. return
  710. }
  711. // GetChildExcelInfoByParentId 根据id 获取eta表格详情
  712. func GetChildExcelInfoByParentId(parentId int) (items []*ExcelInfo, err error) {
  713. o := orm.NewOrmUsingDB("data")
  714. 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`
  715. _, err = o.Raw(sql, parentId).QueryRows(&items)
  716. return
  717. }
  718. // ExcelInfoDetailButton 操作按钮
  719. type ExcelInfoDetailButton struct {
  720. RefreshButton bool `description:"是否可刷新"`
  721. CopyButton bool `description:"是否可另存为"`
  722. DownloadButton bool `description:"是否可下载"`
  723. OpButton bool `description:"是否可编辑"`
  724. DeleteButton bool `description:"是否可删除"`
  725. OpEdbButton bool `description:"是否可生成指标"`
  726. RefreshEdbButton bool `description:"是否可刷新指标"`
  727. OpWorkerButton bool `description:"是否修改协作人"`
  728. }
  729. func UpdateExcelInfoClassifyIdByIds(classifyId int, excelIds []int) (err error) {
  730. if len(excelIds) == 0 {
  731. return
  732. }
  733. o := orm.NewOrmUsingDB("data")
  734. sql := fmt.Sprintf(`UPDATE excel_info SET excel_classify_id = ? WHERE excel_info_id IN (%s)`, utils.GetOrmInReplace(len(excelIds)))
  735. _, err = o.Raw(sql, classifyId, excelIds).Exec()
  736. return
  737. }
  738. // SearchExcelInfo 表格搜索
  739. type SearchExcelInfo struct {
  740. ExcelInfo
  741. SearchText string `description:"搜索结果(含高亮)"`
  742. HaveOperaAuth bool `description:"是否有数据权限"`
  743. Button ExcelInfoDetailButton `description:"操作权限"`
  744. CanEdit bool `description:"是否可编辑"`
  745. Editor string `description:"编辑人"`
  746. }