custom_analysis.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. package excel
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta_gn/eta_api/models/data_manage/excel"
  6. "eta_gn/eta_api/models/data_manage/excel/request"
  7. "eta_gn/eta_api/models/system"
  8. "eta_gn/eta_api/utils"
  9. "strconv"
  10. "time"
  11. )
  12. var cellSplitNum = 10000 // 基础分割单元格数
  13. // AddCustomAnalysisTable 添加自定义分析表格
  14. func AddCustomAnalysisTable(excelName, content, excelImage string, excelClassifyId int, sysUser *system.Admin) (excelInfo *excel.ExcelInfo, err error, errMsg string, isSendEmail bool) {
  15. isSendEmail = true
  16. contentByte := []byte(content)
  17. var luckySheetList []LuckySheet
  18. err = json.Unmarshal(contentByte, &luckySheetList)
  19. if err != nil {
  20. return
  21. }
  22. // sheet内容为空
  23. if len(luckySheetList) <= 0 {
  24. errMsg = "sheet内容为空"
  25. err = errors.New(errMsg)
  26. isSendEmail = false
  27. return
  28. }
  29. excelClassify, err := excel.GetExcelClassifyById(excelClassifyId)
  30. if err != nil {
  31. if utils.IsErrNoRow(err) {
  32. errMsg = "分类不存在"
  33. err = errors.New(errMsg)
  34. isSendEmail = false
  35. return
  36. }
  37. errMsg = "获取分类信息失败"
  38. err = errors.New("获取分类信息失败,Err:" + err.Error())
  39. return
  40. }
  41. if excelClassify == nil {
  42. errMsg = "分类不存在"
  43. err = errors.New(errMsg)
  44. isSendEmail = false
  45. return
  46. }
  47. if excelClassify.Source != utils.CUSTOM_ANALYSIS_TABLE {
  48. errMsg = "当前分类不是自定义分析分类"
  49. err = errors.New("当前分类不是自定义分析分类")
  50. isSendEmail = false
  51. return
  52. }
  53. // 校验是否同名文件
  54. {
  55. var condition string
  56. var pars []interface{}
  57. condition += " AND excel_classify_id=? "
  58. pars = append(pars, excelClassifyId)
  59. condition += " AND excel_name=? "
  60. pars = append(pars, excelName)
  61. // 获取分类下是否存在该表格名称
  62. count, tmpErr := excel.GetExcelInfoCountByCondition(condition, pars)
  63. if tmpErr != nil {
  64. errMsg = "判断表格名称是否存在失败"
  65. err = errors.New("判断表格名称是否存在失败,Err:" + tmpErr.Error())
  66. return
  67. }
  68. if count > 0 {
  69. errMsg = "表格名称已存在,请重新填写表格名称"
  70. err = errors.New(errMsg)
  71. isSendEmail = false
  72. return
  73. }
  74. }
  75. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  76. // 表格
  77. excelInfo = &excel.ExcelInfo{
  78. //ExcelInfoId: 0,
  79. ExcelName: excelName,
  80. Source: utils.CUSTOM_ANALYSIS_TABLE,
  81. //ExcelType: req.ExcelType,
  82. UniqueCode: utils.MD5(utils.EXCEL_DATA_PREFIX + "_" + timestamp),
  83. ExcelClassifyId: excelClassifyId,
  84. SysUserId: sysUser.AdminId,
  85. SysUserRealName: sysUser.RealName,
  86. Content: ``,
  87. ExcelImage: excelImage,
  88. Sort: 0,
  89. IsDelete: 0,
  90. ModifyTime: time.Now(),
  91. CreateTime: time.Now(),
  92. }
  93. addSheetList := make([]excel.AddExcelSheetParams, 0)
  94. // sheet处理
  95. sheetNameMap := make(map[string]string)
  96. for k, sheetInfo := range luckySheetList {
  97. sheetName := utils.TrimLRStr(sheetInfo.Name)
  98. _, ok := sheetNameMap[sheetName]
  99. if ok {
  100. errMsg = "excel表中存在同名sheet"
  101. err = errors.New(errMsg)
  102. isSendEmail = false
  103. return
  104. }
  105. sheetConf, tmpErr := json.Marshal(sheetInfo.Config)
  106. if tmpErr != nil {
  107. err = tmpErr
  108. return
  109. }
  110. // 计算公式
  111. sheetCalcChain, tmpErr := json.Marshal(sheetInfo.CalcChain)
  112. if tmpErr != nil {
  113. err = tmpErr
  114. return
  115. }
  116. addSheetItem := excel.AddExcelSheetParams{
  117. ExcelSheetId: 0,
  118. ExcelInfoId: 0,
  119. SheetName: sheetName,
  120. Index: sheetInfo.Index,
  121. Sort: k,
  122. Config: string(sheetConf),
  123. CalcChain: string(sheetCalcChain),
  124. }
  125. lenCellData := len(sheetInfo.CellData)
  126. splitLen := lenCellData / cellSplitNum
  127. residue := lenCellData % cellSplitNum
  128. if residue > 0 {
  129. splitLen += 1
  130. }
  131. sheetDataList := make([]*excel.ExcelSheetData, 0)
  132. for i := 0; i < splitLen; i++ {
  133. startRow := i * cellSplitNum
  134. endRow := (i + 1) * cellSplitNum
  135. if i == splitLen-1 && residue > 0 {
  136. endRow = lenCellData
  137. }
  138. tmpData := sheetInfo.CellData[startRow:endRow]
  139. tmpDataByte, tmpErr := json.Marshal(tmpData)
  140. if tmpErr != nil {
  141. errMsg = "保存失败"
  142. err = errors.New("保存失败:" + tmpErr.Error())
  143. return
  144. }
  145. sheetDataList = append(sheetDataList, &excel.ExcelSheetData{
  146. ExcelDataId: 0,
  147. ExcelInfoId: 0,
  148. ExcelSheetId: 0,
  149. Sort: i + 1,
  150. Data: string(tmpDataByte),
  151. ModifyTime: time.Now(),
  152. CreateTime: time.Now(),
  153. })
  154. }
  155. addSheetItem.DataList = sheetDataList
  156. addSheetList = append(addSheetList, addSheetItem)
  157. }
  158. err = excel.AddExcelInfoAndSheet(excelInfo, addSheetList)
  159. return
  160. }
  161. // SaveCustomAnalysisTable 编辑自定义分析表格
  162. func SaveCustomAnalysisTable(excelInfo *excel.ExcelInfo, excelName, content, excelImage string, excelClassifyId int, sheetOpList []request.SheetOp) (err error, errMsg string, isSendEmail bool) {
  163. isSendEmail = true
  164. contentByte := []byte(content)
  165. var luckySheetList []LuckySheet
  166. err = json.Unmarshal(contentByte, &luckySheetList)
  167. if err != nil {
  168. return
  169. }
  170. // sheet内容为空
  171. if len(luckySheetList) <= 0 {
  172. errMsg = "sheet内容为空"
  173. err = errors.New(errMsg)
  174. isSendEmail = false
  175. return
  176. }
  177. // sheet内容为空
  178. //if len(sheetOpList) <= 0 {
  179. // errMsg = "sheet操作为空"
  180. // err = errors.New(errMsg)
  181. // isSendEmail = false
  182. // return
  183. //}
  184. excelClassify, err := excel.GetExcelClassifyById(excelClassifyId)
  185. if err != nil {
  186. if utils.IsErrNoRow(err) {
  187. errMsg = "分类不存在"
  188. err = errors.New(errMsg)
  189. isSendEmail = false
  190. return
  191. }
  192. errMsg = "获取分类信息失败"
  193. err = errors.New("获取分类信息失败,Err:" + err.Error())
  194. return
  195. }
  196. if excelClassify == nil {
  197. errMsg = "分类不存在"
  198. err = errors.New(errMsg)
  199. isSendEmail = false
  200. return
  201. }
  202. if excelClassify.Source != utils.CUSTOM_ANALYSIS_TABLE {
  203. errMsg = "当前分类不是自定义分析分类"
  204. err = errors.New("当前分类不是自定义分析分类")
  205. isSendEmail = false
  206. return
  207. }
  208. // 校验是否同名文件
  209. {
  210. var condition string
  211. var pars []interface{}
  212. condition += " AND excel_classify_id=? AND excel_info_id !=? "
  213. pars = append(pars, excelClassifyId, excelInfo.ExcelInfoId)
  214. condition += " AND excel_name=? "
  215. pars = append(pars, excelName)
  216. // 获取分类下是否存在该表格名称
  217. count, tmpErr := excel.GetExcelInfoCountByCondition(condition, pars)
  218. if tmpErr != nil {
  219. errMsg = "判断表格名称是否存在失败"
  220. err = errors.New("判断表格名称是否存在失败,Err:" + tmpErr.Error())
  221. return
  222. }
  223. if count > 0 {
  224. errMsg = "表格名称已存在,请重新填写表格名称"
  225. err = errors.New(errMsg)
  226. isSendEmail = false
  227. return
  228. }
  229. }
  230. // 表格
  231. excelInfo.ExcelName = excelName
  232. // 如果分类不传入的话,那么分类不变更
  233. if excelClassifyId <= 0 {
  234. excelInfo.ExcelClassifyId = excelClassifyId
  235. }
  236. // 如果缩略图不传入的话,那么缩略图不变更
  237. if excelImage != `` {
  238. excelInfo.ExcelImage = excelImage
  239. }
  240. excelInfo.ModifyTime = time.Now()
  241. updateExcelInfoParam := []string{"ExcelName", "ExcelClassifyId", "ExcelImage", "ModifyTime"}
  242. addSheetList := make([]excel.AddExcelSheetParams, 0)
  243. // sheet处理
  244. sheetNameMap := make(map[string]string)
  245. for k, sheetInfo := range luckySheetList {
  246. sheetName := utils.TrimLRStr(sheetInfo.Name)
  247. _, ok := sheetNameMap[sheetName]
  248. if ok {
  249. errMsg = "excel表中存在同名sheet"
  250. err = errors.New(errMsg)
  251. isSendEmail = false
  252. return
  253. }
  254. sheetConf, tmpErr := json.Marshal(sheetInfo.Config)
  255. if tmpErr != nil {
  256. err = tmpErr
  257. return
  258. }
  259. // 计算公式
  260. sheetCalcChain, tmpErr := json.Marshal(sheetInfo.CalcChain)
  261. if tmpErr != nil {
  262. err = tmpErr
  263. return
  264. }
  265. addSheetItem := excel.AddExcelSheetParams{
  266. ExcelSheetId: 0,
  267. ExcelInfoId: excelInfo.ExcelInfoId,
  268. SheetName: sheetName,
  269. Index: sheetInfo.Index,
  270. Sort: k,
  271. Config: string(sheetConf),
  272. CalcChain: string(sheetCalcChain),
  273. }
  274. lenCellData := len(sheetInfo.CellData)
  275. splitLen := lenCellData / cellSplitNum
  276. residue := lenCellData % cellSplitNum
  277. if residue > 0 {
  278. splitLen += 1
  279. }
  280. sheetDataList := make([]*excel.ExcelSheetData, 0)
  281. for i := 0; i < splitLen; i++ {
  282. startRow := i * cellSplitNum
  283. endRow := (i + 1) * cellSplitNum
  284. if i == splitLen-1 && residue > 0 {
  285. endRow = lenCellData
  286. }
  287. tmpData := sheetInfo.CellData[startRow:endRow]
  288. tmpDataByte, tmpErr := json.Marshal(tmpData)
  289. if tmpErr != nil {
  290. errMsg = "保存失败"
  291. err = errors.New("保存失败:" + tmpErr.Error())
  292. return
  293. }
  294. sheetDataList = append(sheetDataList, &excel.ExcelSheetData{
  295. ExcelDataId: 0,
  296. ExcelInfoId: excelInfo.ExcelInfoId,
  297. ExcelSheetId: 0,
  298. Sort: i + 1,
  299. Data: string(tmpDataByte),
  300. ModifyTime: time.Now(),
  301. CreateTime: time.Now(),
  302. })
  303. }
  304. addSheetItem.DataList = sheetDataList
  305. addSheetList = append(addSheetList, addSheetItem)
  306. }
  307. err = excel.SaveExcelInfoAndSheet(excelInfo, updateExcelInfoParam, addSheetList)
  308. return
  309. }
  310. type LuckySheet struct {
  311. Name string `json:"name"`
  312. //Config struct {
  313. // Columnlen struct {
  314. // Num15 int `json:"15"`
  315. // Num16 int `json:"16"`
  316. // Num20 int `json:"20"`
  317. // Num34 int `json:"34"`
  318. // Num35 int `json:"35"`
  319. // } `json:"columnlen"`
  320. //} `json:"config"`
  321. Config interface{}
  322. Index string `json:"index"`
  323. Order int `json:"order"`
  324. ZoomRatio float64 `json:"zoomRatio"`
  325. ShowGridLines string `json:"showGridLines"`
  326. DefaultColWidth int `json:"defaultColWidth"`
  327. DefaultRowHeight int `json:"defaultRowHeight"`
  328. CellData []LuckySheetCellData `json:"celldata"`
  329. CalcChain []interface{} `json:"calcChain"`
  330. //DataVerification struct {
  331. //} `json:"dataVerification"`
  332. //Hyperlink struct {
  333. //} `json:"hyperlink"`
  334. //Hide int `json:"hide"`
  335. }
  336. // LuckySheetCellData 单元格数据
  337. type LuckySheetCellData struct {
  338. R int `json:"r"`
  339. C int `json:"c"`
  340. V interface{} `json:"v,omitempty"`
  341. }