excel_info.go 27 KB

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