custom_analysis.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. package excel
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta/eta_mobile/models/data_manage/excel"
  6. "eta/eta_mobile/models/data_manage/excel/request"
  7. "eta/eta_mobile/models/system"
  8. "eta/eta_mobile/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 err.Error() == utils.ErrNoRow() {
  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 err.Error() == utils.ErrNoRow() {
  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. //// 查找当前excel的sheet列表
  231. //currSheetList, err := excel.GetAllSheetList(excelInfo.ExcelInfoId)
  232. //if err != nil {
  233. // errMsg = "保存失败"
  234. // err = errors.New("查找当前excel的sheet列表失败,Err:" + err.Error())
  235. // return
  236. //}
  237. //currSheetMap := make(map[string]string)
  238. //for _, currSheet := range currSheetList {
  239. // currSheetMap[currSheet.SheetName] = currSheet.SheetName
  240. //}
  241. //
  242. //for k, sheetOp := range sheetOpList {
  243. // sheetName := utils.TrimLRStr(sheetOp.SheetName)
  244. // switch sheetOp.OpType {
  245. // case "add":
  246. // // 新增
  247. // _, ok := currSheetMap[sheetName]
  248. // if ok {
  249. // errMsg = "存在同名sheet:" + sheetName
  250. // err = errors.New(errMsg)
  251. // isSendEmail = false
  252. // return
  253. // }
  254. // case "replace":
  255. // // 替换
  256. // case "append":
  257. // // 追加
  258. // default:
  259. // errMsg = fmt.Sprint("第", k+1, "个sheet,错误的操作类型")
  260. // err = errors.New(errMsg + "op:" + sheetOp.OpType)
  261. // isSendEmail = false
  262. // return
  263. // }
  264. //}
  265. // 表格
  266. excelInfo.ExcelName = excelName
  267. // 如果分类不传入的话,那么分类不变更
  268. if excelClassifyId <= 0 {
  269. excelInfo.ExcelClassifyId = excelClassifyId
  270. }
  271. // 如果缩略图不传入的话,那么缩略图不变更
  272. if excelImage != `` {
  273. excelInfo.ExcelImage = excelImage
  274. }
  275. excelInfo.ModifyTime = time.Now()
  276. updateExcelInfoParam := []string{"ExcelName", "ExcelClassifyId", "ExcelImage", "ModifyTime"}
  277. addSheetList := make([]excel.AddExcelSheetParams, 0)
  278. // sheet处理
  279. sheetNameMap := make(map[string]string)
  280. for k, sheetInfo := range luckySheetList {
  281. sheetName := utils.TrimLRStr(sheetInfo.Name)
  282. _, ok := sheetNameMap[sheetName]
  283. if ok {
  284. errMsg = "excel表中存在同名sheet"
  285. err = errors.New(errMsg)
  286. isSendEmail = false
  287. return
  288. }
  289. sheetConf, tmpErr := json.Marshal(sheetInfo.Config)
  290. if tmpErr != nil {
  291. err = tmpErr
  292. return
  293. }
  294. // 计算公式
  295. sheetCalcChain, tmpErr := json.Marshal(sheetInfo.CalcChain)
  296. if tmpErr != nil {
  297. err = tmpErr
  298. return
  299. }
  300. addSheetItem := excel.AddExcelSheetParams{
  301. ExcelSheetId: 0,
  302. ExcelInfoId: excelInfo.ExcelInfoId,
  303. SheetName: sheetName,
  304. Index: sheetInfo.Index,
  305. Sort: k,
  306. Config: string(sheetConf),
  307. CalcChain: string(sheetCalcChain),
  308. }
  309. lenCellData := len(sheetInfo.CellData)
  310. splitLen := lenCellData / cellSplitNum
  311. residue := lenCellData % cellSplitNum
  312. if residue > 0 {
  313. splitLen += 1
  314. }
  315. sheetDataList := make([]*excel.ExcelSheetData, 0)
  316. for i := 0; i < splitLen; i++ {
  317. startRow := i * cellSplitNum
  318. endRow := (i + 1) * cellSplitNum
  319. if i == splitLen-1 && residue > 0 {
  320. endRow = lenCellData
  321. }
  322. tmpData := sheetInfo.CellData[startRow:endRow]
  323. tmpDataByte, tmpErr := json.Marshal(tmpData)
  324. if tmpErr != nil {
  325. errMsg = "保存失败"
  326. err = errors.New("保存失败:" + tmpErr.Error())
  327. return
  328. }
  329. sheetDataList = append(sheetDataList, &excel.ExcelSheetData{
  330. ExcelDataId: 0,
  331. ExcelInfoId: excelInfo.ExcelInfoId,
  332. ExcelSheetId: 0,
  333. Sort: i + 1,
  334. Data: string(tmpDataByte),
  335. ModifyTime: time.Now(),
  336. CreateTime: time.Now(),
  337. })
  338. }
  339. addSheetItem.DataList = sheetDataList
  340. addSheetList = append(addSheetList, addSheetItem)
  341. }
  342. err = excel.SaveExcelInfoAndSheet(excelInfo, updateExcelInfoParam, addSheetList)
  343. return
  344. }
  345. type LuckySheet struct {
  346. Name string `json:"name"`
  347. //Config struct {
  348. // Columnlen struct {
  349. // Num15 int `json:"15"`
  350. // Num16 int `json:"16"`
  351. // Num20 int `json:"20"`
  352. // Num34 int `json:"34"`
  353. // Num35 int `json:"35"`
  354. // } `json:"columnlen"`
  355. //} `json:"config"`
  356. Config interface{}
  357. Index string `json:"index"`
  358. Order int `json:"order"`
  359. ZoomRatio float64 `json:"zoomRatio"`
  360. ShowGridLines string `json:"showGridLines"`
  361. DefaultColWidth int `json:"defaultColWidth"`
  362. DefaultRowHeight int `json:"defaultRowHeight"`
  363. CellData []LuckySheetCellData `json:"celldata"`
  364. CalcChain []interface{} `json:"calcChain"`
  365. //DataVerification struct {
  366. //} `json:"dataVerification"`
  367. //Hyperlink struct {
  368. //} `json:"hyperlink"`
  369. //Hide int `json:"hide"`
  370. }
  371. // LuckySheetCellData 单元格数据
  372. type LuckySheetCellData struct {
  373. R int `json:"r"`
  374. C int `json:"c"`
  375. V interface{} `json:"v,omitempty"`
  376. }