excel_info.go 32 KB

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