lz_data.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. package data_manage
  2. import (
  3. "fmt"
  4. "github.com/tealeg/xlsx"
  5. "hongze/hz_eta_api/models"
  6. "hongze/hz_eta_api/models/data_manage"
  7. "hongze/hz_eta_api/models/data_source"
  8. "hongze/hz_eta_api/utils"
  9. "os"
  10. "path/filepath"
  11. "strconv"
  12. "strings"
  13. "time"
  14. )
  15. // @Title 隆众数据分类
  16. // @Description 隆众数据分类接口
  17. // @Success 200 {object} data_manage.LzClassify
  18. // @router /lz/classify [get]
  19. func (this *EdbInfoController) LzClassify() {
  20. br := new(models.BaseResponse).Init()
  21. defer func() {
  22. this.Data["json"] = br
  23. this.ServeJSON()
  24. }()
  25. sysUser := this.SysUser
  26. if sysUser == nil {
  27. br.Msg = "请登录"
  28. br.ErrMsg = "请登录,SysUser Is Empty"
  29. br.Ret = 408
  30. return
  31. }
  32. classifyList, err := data_manage.GetLzSurveyClassify()
  33. if err != nil {
  34. br.Msg = "获取分类失败"
  35. br.ErrMsg = "获取分类失败,Err:" + err.Error()
  36. return
  37. }
  38. br.Ret = 200
  39. br.Success = true
  40. br.Msg = "获取成功"
  41. br.Data = classifyList
  42. }
  43. // @Title 隆众数据频度
  44. // @Description 隆众数据频度接口
  45. // @Param BreedId query int true "分类id"
  46. // @Success 200 {object} data_manage.LzFrequency
  47. // @router /lz/frequency [get]
  48. func (this *EdbInfoController) LzFrequency() {
  49. br := new(models.BaseResponse).Init()
  50. defer func() {
  51. this.Data["json"] = br
  52. this.ServeJSON()
  53. }()
  54. sysUser := this.SysUser
  55. if sysUser == nil {
  56. br.Msg = "请登录"
  57. br.ErrMsg = "请登录,SysUser Is Empty"
  58. br.Ret = 408
  59. return
  60. }
  61. breedId, _ := this.GetInt("BreedId")
  62. if breedId <= 0 {
  63. br.Msg = "分类id错误"
  64. br.ErrMsg = "分类id错误:breedId" + strconv.Itoa(breedId)
  65. return
  66. }
  67. frequencyList, err := data_manage.GetLzFrequencyByClassifyId(breedId)
  68. if err != nil {
  69. br.Msg = "获取频度失败"
  70. br.ErrMsg = "获取频度失败,Err:" + err.Error()
  71. return
  72. }
  73. br.Ret = 200
  74. br.Success = true
  75. br.Msg = "获取成功"
  76. br.Data = frequencyList
  77. }
  78. // @Title 获取隆众数据
  79. // @Description 获取隆众数据接口
  80. // @Param BreedId query int true "分类id"
  81. // @Param Frequency query int true "频度"
  82. // @Success 200 {object} data_manage.LzFrequency
  83. // @router /lz/data [get]
  84. func (this *EdbInfoController) LzData() {
  85. br := new(models.BaseResponse).Init()
  86. defer func() {
  87. this.Data["json"] = br
  88. this.ServeJSON()
  89. }()
  90. sysUser := this.SysUser
  91. if sysUser == nil {
  92. br.Msg = "请登录"
  93. br.ErrMsg = "请登录,SysUser Is Empty"
  94. br.Ret = 408
  95. return
  96. }
  97. breedId, _ := this.GetInt("BreedId")
  98. if breedId <= 0 {
  99. br.Msg = "分类id错误"
  100. br.ErrMsg = "分类id错误:breedId" + strconv.Itoa(breedId)
  101. return
  102. }
  103. frequency, _ := this.GetInt("Frequency")
  104. if frequency <= 0 {
  105. br.Msg = "参数错误"
  106. br.ErrMsg = "频度错误:frequency" + strconv.Itoa(frequency)
  107. return
  108. }
  109. //获取指标
  110. fmt.Println("breedId:", breedId)
  111. fmt.Println("frequency:", frequency)
  112. productList, err := data_manage.GetLongzhongSurveyProduct(breedId, frequency)
  113. if err != nil {
  114. br.Msg = "获取数据失败"
  115. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  116. return
  117. }
  118. resultList := make([]*data_manage.LzProductList, 0)
  119. for _, v := range productList {
  120. fmt.Println(v.SurveyProductId)
  121. product := new(data_manage.LzProductList)
  122. product.SurveyProductId = v.SurveyProductId
  123. product.BreedName = v.BreedName
  124. product.QuotaName = v.SampleName + v.BreedName + v.Custom + v.QuotaName
  125. product.UnitName = v.UnitName
  126. product.SampleType = v.SampleType
  127. product.SampleName = v.SampleName
  128. product.Device = v.Device
  129. product.Frequency = v.Frequency
  130. product.Custom = v.Custom
  131. product.StartDate = v.StartDate
  132. product.EndDate = v.EndDate
  133. product.ModifyTime = v.ModifyTime.Format(utils.FormatDateTime)
  134. product.LzCode = v.LzCode
  135. dataList, err := data_manage.GetLongzhongSurveyProductData(v.SurveyProductId)
  136. if err != nil {
  137. br.Msg = "获取数据失败"
  138. br.ErrMsg = "获取指标数据失败,Err:" + err.Error()
  139. return
  140. }
  141. if dataList == nil {
  142. dataList = make([]*data_manage.LzProductData, 0)
  143. }
  144. product.DataList = dataList
  145. resultList = append(resultList, product)
  146. }
  147. br.Ret = 200
  148. br.Success = true
  149. br.Msg = "获取成功"
  150. br.Data = resultList
  151. }
  152. // LzSearchList
  153. // @Title 隆众模糊搜索
  154. // @Description 隆众模糊搜索
  155. // @Param Keyword query string ture "关键字搜索"
  156. // @Success 200 {object} models.BaseResponse
  157. // @router /lz/search_list [get]
  158. func (this *EdbInfoController) LzSearchList() {
  159. br := new(models.BaseResponse).Init()
  160. defer func() {
  161. this.Data["json"] = br
  162. this.ServeJSON()
  163. }()
  164. sysUser := this.SysUser
  165. if sysUser == nil {
  166. br.Msg = "请重新登录"
  167. return
  168. }
  169. //关键字
  170. keyword := this.GetString("Keyword")
  171. list, err := data_source.GetLzItemList(keyword)
  172. if err != nil {
  173. br.ErrMsg = "获取失败,Err:" + err.Error()
  174. br.Msg = "获取失败"
  175. return
  176. }
  177. resultList := make([]*data_manage.LzProductList, 0)
  178. for _, v := range list {
  179. product := new(data_manage.LzProductList)
  180. product.SurveyProductId = v.SurveyProductId
  181. product.BreedName = v.BreedName
  182. product.QuotaName = v.SampleName + v.BreedName + v.Custom + v.QuotaName
  183. product.UnitName = v.UnitName
  184. product.SampleType = v.SampleType
  185. product.SampleName = v.SampleName
  186. product.Device = v.Device
  187. product.Frequency = v.Frequency
  188. product.Custom = v.Custom
  189. product.StartDate = v.StartDate
  190. product.EndDate = v.EndDate
  191. product.ModifyTime = v.ModifyTime.Format(utils.FormatDateTime)
  192. product.LzCode = v.LzCode
  193. dataList, err := data_manage.GetLongzhongSurveyProductData(v.SurveyProductId)
  194. if err != nil {
  195. br.Msg = "获取数据失败"
  196. br.ErrMsg = "获取指标数据失败,Err:" + err.Error()
  197. return
  198. }
  199. if dataList == nil {
  200. dataList = make([]*data_manage.LzProductData, 0)
  201. }
  202. product.DataList = dataList
  203. resultList = append(resultList, product)
  204. }
  205. br.Ret = 200
  206. br.Success = true
  207. br.Msg = "获取成功"
  208. br.Data = resultList
  209. }
  210. // ExportLzList
  211. // @Title 导出隆众数据
  212. // @Description 导出数据列表
  213. // @Param QuotaName query string false "名称关键词"
  214. // @Param LzCode query string false "指标唯一编码"
  215. // @Param BreedName query string true "分类"
  216. // @Param Frequency query string true "频度"
  217. // @Param UnitName query string false "单位"
  218. // @Param ModifyTime query string false "更新时间"
  219. // @Success 200 导出成功
  220. // @router /export/lzList [get]
  221. func (this *EdbInfoController) ExportLzList() {
  222. br := new(models.BaseResponse).Init()
  223. defer func() {
  224. this.Data["json"] = br
  225. this.ServeJSON()
  226. }()
  227. sysUser := this.SysUser
  228. if sysUser == nil {
  229. br.Msg = "请重新登录"
  230. return
  231. }
  232. breedName := this.GetString("BreedName") //分类
  233. //frequency := this.GetString("Frequency")
  234. secNameList := make([]*models.EdbdataExportList, 0)
  235. dir, _ := os.Executable()
  236. exPath := filepath.Dir(dir)
  237. downLoadnFilePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  238. xlsxFile := xlsx.NewFile()
  239. frequencies, err := models.GetLzFrequency(breedName)
  240. if err != nil {
  241. fmt.Println("GetLzFrequency err:", err.Error())
  242. utils.FileLog.Info("GetLzFrequency err:" + err.Error())
  243. return
  244. }
  245. if breedName != "" {
  246. for _, frequency := range frequencies {
  247. //获取指标
  248. secNameList, err := models.GetLzSurveyProductByNameAndFrequency(breedName, *frequency)
  249. if err != nil {
  250. fmt.Println("获取数据失败,Err:" + err.Error())
  251. return
  252. }
  253. if len(secNameList) <= 0 {
  254. fmt.Println("secNameList长度为0")
  255. return
  256. }
  257. var frequencyName string
  258. if *frequency == 1 {
  259. frequencyName = "日度"
  260. } else if *frequency == 2 {
  261. frequencyName = "周度"
  262. } else if *frequency == 3 {
  263. frequencyName = "月度"
  264. } else if *frequency == 4 {
  265. frequencyName = "季度"
  266. } else if *frequency == 5 {
  267. frequencyName = "年度"
  268. } else if *frequency == 99 {
  269. frequencyName = "无固定频率"
  270. }
  271. singalDataMax, err := data_source.GetLongzhongSurveyDataMaxCountByFrequency(breedName, *frequency)
  272. if err != nil {
  273. fmt.Println("获取指标最大数据量失败", err.Error())
  274. return
  275. }
  276. if singalDataMax <= 0 {
  277. continue
  278. }
  279. sheetNew, err := xlsxFile.AddSheet(frequencyName)
  280. if err != nil {
  281. fmt.Println("新增Sheet失败", err.Error())
  282. return
  283. }
  284. //sheetNew.SetColWidth()
  285. //获取指标数据
  286. windRow := sheetNew.AddRow()
  287. secNameRow := sheetNew.AddRow()
  288. indexCodeRow := sheetNew.AddRow()
  289. frequencyRow := sheetNew.AddRow()
  290. unitRow := sheetNew.AddRow()
  291. lastModifyDateRow := sheetNew.AddRow()
  292. //获取分类下指标最大数据量
  293. dataMax, err := data_source.GetLongzhongSurveyDataMaxCount(breedName)
  294. if err != nil {
  295. fmt.Println("获取指标最大数据量失败", err.Error())
  296. return
  297. }
  298. setRowIndex := 6
  299. for k, sv := range secNameList {
  300. //获取数据
  301. dataList, err := data_source.GetLongzhongSurveyDataById(sv.SurveyProductId)
  302. if err != nil {
  303. br.Msg = "获取数据失败"
  304. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  305. return
  306. }
  307. if len(dataList) > 0 {
  308. windRow.AddCell().SetValue("隆众")
  309. secNameRow.AddCell().SetValue("指标名称")
  310. indexCodeRow.AddCell().SetValue("指标ID")
  311. frequencyRow.AddCell().SetValue("频率")
  312. unitRow.AddCell().SetValue("单位")
  313. lastModifyDateRow.AddCell().SetValue("更新时间")
  314. secNameRow.AddCell().SetValue(sv.SampleName + sv.BreedName + sv.Custom + sv.QuotaName)
  315. indexCodeRow.AddCell().SetValue(sv.LzCode)
  316. if sv.Frequency == 1 {
  317. frequencyRow.AddCell().SetValue("日度")
  318. } else if sv.Frequency == 2 {
  319. frequencyRow.AddCell().SetValue("周度")
  320. } else if sv.Frequency == 3 {
  321. frequencyRow.AddCell().SetValue("月度")
  322. } else if sv.Frequency == 4 {
  323. frequencyRow.AddCell().SetValue("季度")
  324. } else if sv.Frequency == 5 {
  325. frequencyRow.AddCell().SetValue("年度")
  326. } else if sv.Frequency == 99 {
  327. frequencyRow.AddCell().SetValue("无固定频率")
  328. }
  329. unitRow.AddCell().SetValue(sv.UnitName)
  330. lastModifyDateRow.AddCell().SetValue(sv.EndDate)
  331. windRow.AddCell()
  332. windRow.AddCell()
  333. secNameRow.AddCell()
  334. indexCodeRow.AddCell()
  335. frequencyRow.AddCell()
  336. unitRow.AddCell()
  337. lastModifyDateRow.AddCell()
  338. min := k * 3
  339. sheetNew.SetColWidth(min, min, 15)
  340. if len(dataList) <= 0 {
  341. for n := 0; n < dataMax; n++ {
  342. rowIndex := setRowIndex + n
  343. row := sheetNew.Row(rowIndex)
  344. row.AddCell()
  345. row.AddCell()
  346. row.AddCell()
  347. }
  348. } else {
  349. endRowIndex := 0
  350. for rk, dv := range dataList {
  351. rowIndex := setRowIndex + rk
  352. row := sheetNew.Row(rowIndex)
  353. displayDate, _ := time.Parse(utils.FormatDate, dv.DataTime)
  354. displayDateCell := row.AddCell()
  355. style := new(xlsx.Style)
  356. style.ApplyAlignment = true
  357. style.Alignment.WrapText = true
  358. displayDateCell.SetStyle(style)
  359. displayDateCell.SetDate(displayDate)
  360. row.AddCell().SetFloat(dv.InputValue)
  361. row.AddCell()
  362. endRowIndex = rowIndex
  363. }
  364. if len(dataList) < dataMax {
  365. dataLen := dataMax - len(dataList)
  366. for n := 0; n < dataLen; n++ {
  367. rowIndex := (endRowIndex + 1) + n
  368. row := sheetNew.Row(rowIndex)
  369. row.AddCell()
  370. row.AddCell()
  371. row.AddCell()
  372. }
  373. }
  374. }
  375. }
  376. }
  377. }
  378. } else {
  379. name := this.GetString("QuotaName")
  380. lzCode := this.GetString("LzCode") //指标唯一编码
  381. unit := this.GetString("UnitName")
  382. modifyTime := this.GetString("ModifyTime")
  383. frequency, _ := this.GetInt("Frequency")
  384. sheet, err := xlsxFile.AddSheet("指标")
  385. if err != nil {
  386. br.Msg = "新增Sheet失败"
  387. br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
  388. return
  389. }
  390. //获取指标数据
  391. windRow := sheet.AddRow()
  392. windRow.AddCell().SetValue("隆众")
  393. rowSecName := sheet.AddRow()
  394. celSecName := rowSecName.AddCell()
  395. celSecName.SetValue("指标名称")
  396. cellSenName := rowSecName.AddCell()
  397. cellSenName.SetValue(name)
  398. indexCodeRow := sheet.AddRow()
  399. indexCodeRow.AddCell().SetValue("指标ID")
  400. indexCodeRow.AddCell().SetValue(lzCode)
  401. rowFrequency := sheet.AddRow()
  402. celFrequency := rowFrequency.AddCell()
  403. celFrequency.SetValue("频率")
  404. if frequency == 1 {
  405. rowFrequency.AddCell().SetValue("日度")
  406. } else if frequency == 2 {
  407. rowFrequency.AddCell().SetValue("周度")
  408. } else if frequency == 3 {
  409. rowFrequency.AddCell().SetValue("月度")
  410. } else if frequency == 4 {
  411. rowFrequency.AddCell().SetValue("季度")
  412. } else if frequency == 5 {
  413. rowFrequency.AddCell().SetValue("年度")
  414. } else if frequency == 99 {
  415. rowFrequency.AddCell().SetValue("无固定频率")
  416. }
  417. rowUnit := sheet.AddRow()
  418. celUnit := rowUnit.AddCell()
  419. celUnit.SetValue("单位")
  420. cellUnit := rowUnit.AddCell()
  421. cellUnit.SetValue(unit)
  422. rowModifyDate := sheet.AddRow()
  423. rowModifyCell := rowModifyDate.AddCell()
  424. rowModifyCell.SetValue("更新时间")
  425. rowModifyCell = rowModifyDate.AddCell()
  426. rowModifyCell.SetValue(modifyTime)
  427. surveyProductId, err := data_source.GetLzProductIdByCode(lzCode)
  428. //获取数据
  429. dataList, err := data_source.GetLongzhongSurveyDataById(surveyProductId)
  430. if err != nil {
  431. br.Msg = "获取数据失败"
  432. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  433. return
  434. }
  435. fmt.Println("len(dataList):", len(dataList))
  436. dataMax, err := data_source.GetLzSurveyDataMaxCountByProductId(surveyProductId)
  437. fmt.Println("dataMax:", dataMax)
  438. if len(dataList) <= 0 {
  439. for n := 0; n < dataMax; n++ {
  440. rowIndex := 6 + n
  441. row := sheet.Row(rowIndex)
  442. row.AddCell()
  443. row.AddCell()
  444. row.AddCell()
  445. }
  446. } else {
  447. endRowIndex := 0
  448. for rk, dv := range dataList {
  449. rowIndex := 6 + rk
  450. row := sheet.Row(rowIndex)
  451. displayDate, _ := time.Parse(utils.FormatDate, dv.DataTime)
  452. displayDateCell := row.AddCell()
  453. style := new(xlsx.Style)
  454. style.ApplyAlignment = true
  455. style.Alignment.WrapText = true
  456. displayDateCell.SetStyle(style)
  457. displayDateCell.SetDate(displayDate)
  458. row.AddCell().SetFloat(dv.InputValue)
  459. row.AddCell()
  460. endRowIndex = rowIndex
  461. }
  462. if len(dataList) < dataMax {
  463. dataLen := dataMax - len(dataList)
  464. for n := 0; n < dataLen; n++ {
  465. rowIndex := (endRowIndex + 1) + n
  466. row := sheet.Row(rowIndex)
  467. row.AddCell()
  468. row.AddCell()
  469. row.AddCell()
  470. }
  471. }
  472. }
  473. }
  474. err = xlsxFile.Save(downLoadnFilePath)
  475. if err != nil {
  476. //有指标无数据时先导出一遍空表
  477. sheet, err := xlsxFile.AddSheet("无数据")
  478. if err != nil {
  479. br.Msg = "新增Sheet失败"
  480. br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
  481. return
  482. }
  483. rowSecName := sheet.AddRow()
  484. celSecName := rowSecName.AddCell()
  485. celSecName.SetValue("")
  486. err = xlsxFile.Save(downLoadnFilePath)
  487. if err != nil {
  488. br.Msg = "保存文件失败"
  489. br.ErrMsg = "保存文件失败"
  490. return
  491. }
  492. }
  493. fileName := `隆众数据`
  494. if len(secNameList) > 0 {
  495. fileName = secNameList[0].ClassifyName
  496. }
  497. fileName += time.Now().Format("06.01.02") + `.xlsx` //文件名称
  498. this.Ctx.Output.Download(downLoadnFilePath, fileName)
  499. defer func() {
  500. os.Remove(downLoadnFilePath)
  501. }()
  502. br.Ret = 200
  503. br.Success = true
  504. br.Msg = "success"
  505. }
  506. // ExportLzQuotasData
  507. // @Title 导出隆众多个指标数据
  508. // @Description 导出隆众多个指标数据
  509. // @Param BreedIds query string false "品种ID集合"
  510. // @Success 200 导出成功
  511. // @router /export/lzQuotasData [get]
  512. func (this *EdbInfoController) ExportLzQuotasData() {
  513. br := new(models.BaseResponse).Init()
  514. defer func() {
  515. this.Data["json"] = br
  516. this.ServeJSON()
  517. }()
  518. sysUser := this.SysUser
  519. if sysUser == nil {
  520. br.Msg = "请重新登录"
  521. return
  522. }
  523. breedIdsStr := this.GetString("BreedIds")
  524. breedIds := strings.Split(breedIdsStr, ",")
  525. if len(breedIds) <= 0 {
  526. br.Msg = "请选择品种"
  527. br.ErrMsg = "请选择品种"
  528. return
  529. }
  530. // 获取多个品种的指标信息
  531. quotaList, err := models.GetExportLzSurveyProductByBreedIds(breedIdsStr)
  532. if err != nil {
  533. br.Msg = "导出失败"
  534. br.ErrMsg = "获取指标信息失败, Err: " + err.Error()
  535. return
  536. }
  537. // 获取多个品种的指标最大数据量
  538. quotaMaxCountList, err := data_source.GetExportLzSurveyDataMaxCount(breedIdsStr)
  539. if err != nil {
  540. br.Msg = "导出失败"
  541. br.ErrMsg = "获取指标最大数据量失败, Err: " + err.Error()
  542. return
  543. }
  544. quotaMaxCountMatch := make(map[string]int)
  545. for i := 0; i < len(quotaMaxCountList); i++ {
  546. quotaMaxCountMatch[strconv.Itoa(quotaMaxCountList[i].BreedId)] = quotaMaxCountList[i].Count
  547. }
  548. // 获取多个品种对应的指标数据
  549. productIdDataMatch := make(map[int][]*data_source.LongzhongSurveyData)
  550. for i := 0; i < len(quotaList); i++ {
  551. var iniArr []*data_source.LongzhongSurveyData
  552. productIdDataMatch[quotaList[i].SurveyProductId] = iniArr
  553. }
  554. quotaDataList, err := data_source.GetExportLzSurveyDataByBreedIds(breedIdsStr)
  555. for k, _ := range productIdDataMatch {
  556. var tmpArr []*data_source.LongzhongSurveyData
  557. for _, qd := range quotaDataList {
  558. if k == qd.SurveyProductId {
  559. tmpArr = append(tmpArr, qd)
  560. }
  561. }
  562. productIdDataMatch[k] = tmpArr
  563. }
  564. dir, _ := os.Executable()
  565. exPath := filepath.Dir(dir)
  566. downloadFilePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  567. excelFile := xlsx.NewFile()
  568. frequencyMatch := map[int64]string{
  569. 1: "日度",
  570. 2: "周度",
  571. 3: "月度",
  572. 4: "季度",
  573. 5: "年度",
  574. 99: "无固定频率",
  575. }
  576. for i := 0; i < len(breedIds); i++ {
  577. breedId := breedIds[i]
  578. breedName := ""
  579. var breedQuotas []*models.LongzhongSurveyProduct
  580. for _, quota := range quotaList {
  581. if quota.BreedId == breedId {
  582. breedName = quota.BreedName
  583. breedQuotas = append(breedQuotas, quota)
  584. }
  585. }
  586. if len(breedQuotas) <= 0 {
  587. continue
  588. }
  589. sheetNew, err := excelFile.AddSheet(breedName)
  590. if err != nil {
  591. br.Msg = "导出失败"
  592. br.ErrMsg = "新增sheet失败, Err: " + err.Error()
  593. return
  594. }
  595. windRow := sheetNew.AddRow()
  596. secNameRow := sheetNew.AddRow()
  597. indexCodeRow := sheetNew.AddRow()
  598. frequencyRow := sheetNew.AddRow()
  599. unitRow := sheetNew.AddRow()
  600. lastModifyDateRow := sheetNew.AddRow()
  601. dataMax := quotaMaxCountMatch[breedId] // 最大数据量
  602. setRowIndex := 6
  603. for k, sv := range breedQuotas {
  604. // 指标信息
  605. windRow.AddCell().SetValue("隆众")
  606. secNameRow.AddCell().SetValue("指标名称")
  607. indexCodeRow.AddCell().SetValue("指标ID")
  608. frequencyRow.AddCell().SetValue("频率")
  609. unitRow.AddCell().SetValue("单位")
  610. lastModifyDateRow.AddCell().SetValue("更新时间")
  611. secNameRow.AddCell().SetValue(sv.SampleName + sv.BreedName + sv.Custom + sv.QuotaName)
  612. indexCodeRow.AddCell().SetValue(sv.LzCode)
  613. frequencyRow.AddCell().SetValue(frequencyMatch[sv.Frequency])
  614. unitRow.AddCell().SetValue(sv.UnitName)
  615. lastModifyDateRow.AddCell().SetValue(sv.EndDate)
  616. windRow.AddCell()
  617. windRow.AddCell()
  618. secNameRow.AddCell()
  619. indexCodeRow.AddCell()
  620. frequencyRow.AddCell()
  621. unitRow.AddCell()
  622. lastModifyDateRow.AddCell()
  623. min := k * 3
  624. sheetNew.SetColWidth(min, min, 15)
  625. // 指标数据
  626. quotaData := productIdDataMatch[sv.SurveyProductId]
  627. if len(quotaData) <= 0 {
  628. for n := 0; n < dataMax; n++ {
  629. rowIndex := setRowIndex + n
  630. row := sheetNew.Row(rowIndex)
  631. row.AddCell()
  632. row.AddCell()
  633. row.AddCell()
  634. }
  635. } else {
  636. endRowIndex := 0
  637. for rk, dv := range quotaData {
  638. rowIndex := setRowIndex + rk
  639. row := sheetNew.Row(rowIndex)
  640. displayDate, _ := time.Parse(utils.FormatDate, dv.DataTime)
  641. displayDateCell := row.AddCell()
  642. style := new(xlsx.Style)
  643. style.ApplyAlignment = true
  644. style.Alignment.WrapText = true
  645. displayDateCell.SetStyle(style)
  646. displayDateCell.SetDate(displayDate)
  647. row.AddCell().SetFloat(dv.InputValue)
  648. row.AddCell()
  649. endRowIndex = rowIndex
  650. }
  651. if len(quotaData) < dataMax {
  652. dataLen := dataMax - len(quotaData)
  653. for n := 0; n < dataLen; n++ {
  654. rowIndex := (endRowIndex + 1) + n
  655. row := sheetNew.Row(rowIndex)
  656. row.AddCell()
  657. row.AddCell()
  658. row.AddCell()
  659. }
  660. }
  661. }
  662. }
  663. }
  664. err = excelFile.Save(downloadFilePath)
  665. if err != nil {
  666. br.Msg = "导出失败"
  667. br.ErrMsg = "保存文件失败"
  668. return
  669. }
  670. fileName := `隆众数据`
  671. fileName += time.Now().Format("06.01.02") + `.xlsx` //文件名称
  672. this.Ctx.Output.Download(downloadFilePath, fileName)
  673. defer func() {
  674. os.Remove(downloadFilePath)
  675. }()
  676. br.Ret = 200
  677. br.Success = true
  678. br.Msg = "success"
  679. }