bill_export_excel.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. package accounting
  2. import (
  3. "context"
  4. "gd_management/apis"
  5. "gd_management/errors"
  6. "encoding/json"
  7. "fmt"
  8. "os"
  9. "strconv"
  10. "strings"
  11. "time"
  12. "gd_management/common.in/config"
  13. "gd_management/common.in/utils"
  14. "git.getensh.com/common/gopkgsv2/id"
  15. "github.com/aliyun/aliyun-oss-go-sdk/oss"
  16. "github.com/astaxie/beego/orm"
  17. "github.com/tealeg/xlsx"
  18. "go.uber.org/zap"
  19. )
  20. const defaultLine = 10
  21. func AddSpaceRow(row *xlsx.Row, num int) {
  22. if num == 0 {
  23. num = defaultLine
  24. }
  25. allTitleStyle := xlsx.NewStyle()
  26. allFill := *xlsx.NewFill("solid", "FFFFFF", "FFFFFF")
  27. allTitleStyle.Fill = allFill
  28. for i := 0; i < num; i++ {
  29. row.AddCell().SetStyle(allTitleStyle)
  30. }
  31. }
  32. func BillExportExcel(ctx context.Context, req *apis.BillExportExcelReq, reply *apis.BillExportExcelReply) error {
  33. o := orm.NewOrm()
  34. if req.MerchantId == 0 || req.Month == "" || req.BillId == 0 {
  35. return errors.ArgsError
  36. }
  37. //sql := "select t1.id as bill_detail_id,t1.month,t5.data_api_name,t1.merchant_data_api_id,t2.alias,t1.total_count,t1.valid_count,t1.charge_count,t1.amount,t2.unit_price,t1.remark from t_gd_bill_detail as t1 left join t_gd_merchant_data_api t2 on t1.merchant_data_api_id=t2.id left join t_gd_data_api_query_type as t4 on t4.id=t2.query_type_id left join t_gd_data_api as t5 on t5.id=t4.data_api_id where t1.month=? and t2.merchant_id=?"
  38. sql := "select t1.id as bill_detail_id,t1.month,t5.data_api_name,t1.merchant_data_api_id,t1.alias,t1.total_count,t1.valid_count,t1.charge_count,t1.amount,t1.unit_price,t1.remark from t_gd_bill_detail as t1 left join t_gd_merchant_data_api t2 on t1.merchant_data_api_id=t2.id left join t_gd_data_api_query_type as t4 on t4.id=t2.query_type_id left join t_gd_data_api as t5 on t5.id=t4.data_api_id where t1.month=? and t1.merchant_id=?"
  39. _, err := o.Raw(sql, req.Month, req.MerchantId).QueryRows(&reply.List)
  40. if err != nil {
  41. l.Error("func",
  42. zap.String("call", "MerchantConsumeList"),
  43. zap.String("args", utils.MarshalJsonString(req)),
  44. zap.String("error", err.Error()))
  45. return errors.DataBaseError
  46. }
  47. name := ""
  48. sql1 := "select merchant_name from t_gd_merchants where id=?"
  49. err = o.Raw(sql1, req.MerchantId).QueryRow(&name)
  50. if err != nil {
  51. l.Error("func",
  52. zap.String("call", sql1),
  53. zap.String("args", utils.MarshalJsonString(req)),
  54. zap.String("error", err.Error()))
  55. return errors.DataBaseError
  56. }
  57. // 1 生成excel
  58. // 生成一个新的文件
  59. id, _ := id.GetUniqueID()
  60. fileName := fmt.Sprintf("field-download-%d-%s.xlsx", id, name+"对账单("+req.Month+")")
  61. file := xlsx.NewFile()
  62. allFill := *xlsx.NewFill("solid", "FFFFFF", "FFFFFF")
  63. allTitleStyle := xlsx.NewStyle()
  64. allTitleStyle.Alignment = xlsx.Alignment{
  65. Horizontal: "center",
  66. Vertical: "center",
  67. }
  68. allTitleFont := *xlsx.NewFont(14, "微软雅黑")
  69. allTitleFont.Bold = true
  70. allTitleStyle.Font = allTitleFont
  71. allTitleStyle.Fill = allFill
  72. //allTitleStyle.Fill.BgColor = "rgb(255, 255, 255)"
  73. // 添加sheet页
  74. sheet, _ := file.AddSheet(name + "对账单(" + req.Month + ")")
  75. sheet.SetColWidth(1, 1, 10)
  76. sheet.SetColWidth(2, 2, 25)
  77. sheet.SetColWidth(3, 5, 15)
  78. sheet.SetColWidth(6, 6, 17)
  79. sheet.SetColWidth(7, 7, 18)
  80. // 插入标题
  81. allTitleRow := sheet.AddRow()
  82. allTitle := allTitleRow.AddCell()
  83. for i := 0; i < 6; i++ {
  84. allTitleRow.AddCell().SetStyle(allTitleStyle)
  85. }
  86. allTitle.HMerge = 6
  87. allTitleRow2 := sheet.AddRow()
  88. for i := 0; i < 6; i++ {
  89. allTitleRow2.AddCell().SetStyle(allTitleStyle)
  90. }
  91. allTitle.VMerge = 1
  92. allTitle.Value = name + "对账单(" + req.Month + ")"
  93. allTitle.SetStyle(allTitleStyle)
  94. allTitleRow.SetHeight(30)
  95. AddSpaceRow(allTitleRow, 0)
  96. AddSpaceRow(allTitleRow2, defaultLine+1)
  97. // 插入表头
  98. titleRow := sheet.AddRow()
  99. titleRow.SetHeight(20)
  100. titleStyle := xlsx.NewStyle()
  101. titleStyle.Alignment = xlsx.Alignment{
  102. Horizontal: "center",
  103. Vertical: "center",
  104. }
  105. titleFont := *xlsx.NewFont(12, "微软雅黑")
  106. titleStyle.Font = titleFont
  107. titleStyle.ApplyFill = true
  108. titleFill := *xlsx.NewFill("solid", "BDD7EE", "DEDEDE")
  109. titleStyle.Fill = titleFill
  110. titleStyle.ApplyBorder = true
  111. titleBorder := *xlsx.NewBorder("thin", "thin", "thin", "thin")
  112. titleStyle.Border = titleBorder
  113. cell1 := titleRow.AddCell()
  114. cell1.Value = "统计时间"
  115. cell1.SetStyle(titleStyle)
  116. cell2 := titleRow.AddCell()
  117. cell2.Value = "产品"
  118. cell2.SetStyle(titleStyle)
  119. cell3 := titleRow.AddCell()
  120. cell3.Value = "总查次"
  121. cell3.SetStyle(titleStyle)
  122. cell4 := titleRow.AddCell()
  123. cell4.Value = "有效查次"
  124. cell4.SetStyle(titleStyle)
  125. cell5 := titleRow.AddCell()
  126. cell5.Value = "实际计费查次"
  127. cell5.SetStyle(titleStyle)
  128. cell6 := titleRow.AddCell()
  129. cell6.Value = "单价"
  130. cell6.SetStyle(titleStyle)
  131. cell7 := titleRow.AddCell()
  132. cell7.Value = "消费金额"
  133. cell7.SetStyle(titleStyle)
  134. AddSpaceRow(titleRow, 0)
  135. for index, _ := range reply.List {
  136. if reply.List[index].Remark != "" {
  137. tmp := []apis.BillDetailRemarkInfo{}
  138. err = json.Unmarshal([]byte(reply.List[index].Remark), &tmp)
  139. if err == nil {
  140. for index1, _ := range tmp {
  141. tmp[index1].Alias = reply.List[index].Alias
  142. tmp[index1].DataApiName = reply.List[index].DataApiName
  143. }
  144. reply.RemarkList = append(reply.RemarkList, tmp...)
  145. }
  146. }
  147. }
  148. remark := ""
  149. err = o.Raw("select remark,amount,balance,bill_create_time from t_gd_bill where id=?", req.BillId).QueryRow(&remark, &reply.Amount, &reply.Balance, &reply.BillCreateTime)
  150. if err != nil {
  151. l.Error("func",
  152. zap.String("call", "BillDetailList"),
  153. zap.String("args", utils.MarshalJsonString(req)),
  154. zap.String("error", err.Error()))
  155. return errors.DataBaseError
  156. }
  157. if remark != "" {
  158. json.Unmarshal([]byte(remark), &reply.BillRemarkList)
  159. }
  160. apiStyle := xlsx.NewStyle()
  161. apiStyle.Alignment = xlsx.Alignment{
  162. Horizontal: "left",
  163. Vertical: "center",
  164. }
  165. apiFont := *xlsx.NewFont(10, "微软雅黑")
  166. apiStyle.Font = apiFont
  167. apiStyle.ApplyBorder = true
  168. apiStyle.Border = titleBorder
  169. monthStyle := xlsx.NewStyle()
  170. monthStyle.Alignment = xlsx.Alignment{
  171. Horizontal: "center",
  172. Vertical: "center",
  173. }
  174. monthStyle.Font = apiFont
  175. monthStyle.ApplyBorder = true
  176. monthStyle.Border = titleBorder
  177. numStyle := xlsx.NewStyle()
  178. numStyle.Alignment = xlsx.Alignment{
  179. Horizontal: "right",
  180. Vertical: "center",
  181. }
  182. numStyle.Font = apiFont
  183. numStyle.ApplyBorder = true
  184. numStyle.Border = titleBorder
  185. num1Style := xlsx.NewStyle()
  186. num1Style.Alignment = xlsx.Alignment{
  187. Horizontal: "right",
  188. Vertical: "center",
  189. }
  190. num1Style.Font = apiFont
  191. num1Style.ApplyBorder = true
  192. num1Style.Border = titleBorder
  193. for _, v := range reply.List {
  194. dataRow := sheet.AddRow()
  195. dataRow.SetHeight(20)
  196. month := dataRow.AddCell()
  197. month.Value = v.Month
  198. month.SetStyle(monthStyle)
  199. dataApiName := dataRow.AddCell()
  200. if len(v.Alias) > 0 {
  201. dataApiName.Value = v.Alias
  202. } else {
  203. dataApiName.Value = v.DataApiName
  204. }
  205. dataApiName.SetStyle(apiStyle)
  206. totalCount := dataRow.AddCell()
  207. totalCount.SetInt(v.TotalCount)
  208. totalCount.SetStyle(num1Style)
  209. validCount := dataRow.AddCell()
  210. validCount.SetInt(v.ValidCount)
  211. validCount.SetStyle(num1Style)
  212. chargeCount := dataRow.AddCell()
  213. chargeCount.SetInt(v.ChargeCount)
  214. chargeCount.SetStyle(num1Style)
  215. unitPrice := dataRow.AddCell()
  216. unitPrice.Value = fmt.Sprintf("%.2f", v.UnitPrice)
  217. unitPrice.SetStyle(numStyle)
  218. unitPrice.NumFmt = `_("$"* #,##0.00_);_("$"* \(#,##0.00\);_("$"* "-"??_);_(@_)`
  219. amount := dataRow.AddCell()
  220. amount.Value = fmt.Sprintf("%.2f", v.Amount)
  221. amount.SetStyle(numStyle)
  222. amount.NumFmt = `_("$"* #,##0.00_);_("$"* \(#,##0.00\);_("$"* "-"??_);_(@_)`
  223. AddSpaceRow(dataRow, 0)
  224. }
  225. r1 := sheet.AddRow()
  226. AddSpaceRow(r1, defaultLine+7)
  227. num3Style := xlsx.NewStyle()
  228. num3Style.Alignment = xlsx.Alignment{
  229. Horizontal: "right",
  230. Vertical: "center",
  231. }
  232. num3Style.Font = apiFont
  233. num3Style.ApplyBorder = true
  234. num3Style.Fill = allFill
  235. infoStyle := xlsx.NewStyle()
  236. infoStyle.Alignment = xlsx.Alignment{
  237. Horizontal: "center",
  238. Vertical: "center",
  239. }
  240. infoStyle.Font = apiFont
  241. infoStyle.Fill = allFill
  242. infoStyle.ApplyBorder = true
  243. //infoStyle.Border = titleBorder
  244. info := sheet.AddRow()
  245. for i := 0; i < 5; i++ {
  246. tmpStyle := xlsx.NewStyle()
  247. tmpStyle.Fill = allFill
  248. info.AddCell().SetStyle(tmpStyle)
  249. }
  250. sumAmountName := info.AddCell()
  251. sumAmountName.Value = "合计"
  252. sumAmountName.SetStyle(infoStyle)
  253. sumAmount := info.AddCell()
  254. sumAmount.Value = fmt.Sprintf("%.2f", reply.Amount)
  255. sumAmount.NumFmt = `_("$"* #,##0.00_);_("$"* \(#,##0.00\);_("$"* "-"??_);_(@_)`
  256. sumAmount.SetStyle(num3Style)
  257. AddSpaceRow(info, 0)
  258. info2 := sheet.AddRow()
  259. for i := 0; i < 5; i++ {
  260. tmpStyle := xlsx.NewStyle()
  261. tmpStyle.Fill = allFill
  262. info2.AddCell().SetStyle(tmpStyle)
  263. }
  264. balanceName := info2.AddCell()
  265. balanceName.Value = "账户余额"
  266. balanceName.SetStyle(infoStyle)
  267. balance := info2.AddCell()
  268. balance.Value = fmt.Sprintf("%.2f", reply.Balance)
  269. balance.NumFmt = `_("$"* #,##0.00_);_("$"* \(#,##0.00\);_("$"* "-"??_);_(@_)`
  270. balance.SetStyle(num3Style)
  271. AddSpaceRow(info2, 0)
  272. if (len(reply.RemarkList) + len(reply.BillRemarkList)) > 0 {
  273. r2 := sheet.AddRow()
  274. AddSpaceRow(r2, defaultLine+7)
  275. /*r2Title := r2.AddCell()
  276. r2Title.HMerge = 6
  277. r2Title.Value = ""
  278. r2Title.SetStyle(allTitleStyle)
  279. AddSpaceRow(r2, 0)*/
  280. r3 := sheet.AddRow()
  281. /*AddSpaceRow(r3, 6)
  282. r3Title := r3.AddCell()
  283. r3Title.HMerge = 6
  284. r3Title.Value = ""
  285. r3Title.SetStyle(allTitleStyle)*/
  286. AddSpaceRow(r3, defaultLine+7)
  287. // 插入表头
  288. remarkRow := sheet.AddRow()
  289. remarkRow.SetHeight(15)
  290. cell10 := remarkRow.AddCell()
  291. for i := 0; i < 6; i++ {
  292. remarkRow.AddCell().SetStyle(titleStyle)
  293. }
  294. cell10.HMerge = 6
  295. cell10.Value = "备注"
  296. cell10.SetStyle(titleStyle)
  297. AddSpaceRow(remarkRow, 0)
  298. remarkStyle := xlsx.NewStyle()
  299. remarkStyle.Alignment = xlsx.Alignment{
  300. Horizontal: "left",
  301. Vertical: "center",
  302. }
  303. remarkFont := *xlsx.NewFont(10, "arial")
  304. remarkStyle.Font = remarkFont
  305. remarkStyle.Fill = allFill
  306. remarkStyle.ApplyBorder = true
  307. remarkStyle.Border = titleBorder
  308. timeStyle := xlsx.NewStyle()
  309. timeStyle.Alignment = xlsx.Alignment{
  310. Horizontal: "center",
  311. Vertical: "center",
  312. }
  313. timeFont := *xlsx.NewFont(10, "arial")
  314. timeStyle.Font = timeFont
  315. timeStyle.Fill = allFill
  316. timeStyle.ApplyBorder = true
  317. timeStyle.Border = titleBorder
  318. for i := 0; i < len(reply.RemarkList); i++ {
  319. dataRow := sheet.AddRow()
  320. dataRow.SetHeight(15)
  321. remark1 := dataRow.AddCell()
  322. for i := 0; i < 5; i++ {
  323. dataRow.AddCell().SetStyle(remarkStyle)
  324. }
  325. remark1.HMerge = 5
  326. if reply.RemarkList[i].Alias != "" {
  327. if reply.RemarkList[i].After < reply.RemarkList[i].Befor {
  328. remark1.Value = reply.RemarkList[i].Alias + "产品核减" + strconv.Itoa(reply.RemarkList[i].Befor-reply.RemarkList[i].After) + "次(核定前" + strconv.Itoa(reply.RemarkList[i].Befor) + "次,核定后" + strconv.Itoa(reply.RemarkList[i].After) + "次)"
  329. } else {
  330. remark1.Value = reply.RemarkList[i].Alias + "产品增加" + strconv.Itoa(reply.RemarkList[i].After-reply.RemarkList[i].Befor) + "次(核定前" + strconv.Itoa(reply.RemarkList[i].Befor) + "次,核定后" + strconv.Itoa(reply.RemarkList[i].After) + "次)"
  331. }
  332. } else {
  333. if reply.RemarkList[i].After < reply.RemarkList[i].Befor {
  334. remark1.Value = reply.RemarkList[i].DataApiName + "产品核减" + strconv.Itoa(reply.RemarkList[i].Befor-reply.RemarkList[i].After) + "次(核定前" + strconv.Itoa(reply.RemarkList[i].Befor) + "次,核定后" + strconv.Itoa(reply.RemarkList[i].After) + "次)"
  335. } else {
  336. remark1.Value = reply.RemarkList[i].DataApiName + "产品增加" + strconv.Itoa(reply.RemarkList[i].After-reply.RemarkList[i].Befor) + "次(核定前" + strconv.Itoa(reply.RemarkList[i].Befor) + "次,核定后" + strconv.Itoa(reply.RemarkList[i].After) + "次)"
  337. }
  338. }
  339. remark1.SetStyle(remarkStyle)
  340. timeT := dataRow.AddCell()
  341. timeT.Value = reply.RemarkList[i].Time
  342. timeT.SetStyle(timeStyle)
  343. AddSpaceRow(dataRow, 0)
  344. }
  345. for i := 0; i < len(reply.BillRemarkList); i++ {
  346. dataRow := sheet.AddRow()
  347. billRemark := dataRow.AddCell()
  348. for i := 0; i < 5; i++ {
  349. dataRow.AddCell().SetStyle(remarkStyle)
  350. }
  351. billRemark.HMerge = 5
  352. billRemark.Value = reply.BillRemarkList[i]
  353. billRemark.SetStyle(remarkStyle)
  354. blank := dataRow.AddCell()
  355. blank.SetStyle(remarkStyle)
  356. AddSpaceRow(dataRow, 0)
  357. }
  358. }
  359. spaceRow := sheet.AddRow()
  360. AddSpaceRow(spaceRow, defaultLine+7)
  361. endStyle := xlsx.NewStyle()
  362. endStyle.Alignment = xlsx.Alignment{
  363. Horizontal: "right",
  364. Vertical: "center",
  365. }
  366. endStyle.Font = allTitleFont
  367. endStyle.Fill = allFill
  368. r4 := sheet.AddRow()
  369. for i := 0; i < 5; i++ {
  370. tmpStyle := xlsx.NewStyle()
  371. tmpStyle.Fill = allFill
  372. r4.AddCell().SetStyle(tmpStyle)
  373. }
  374. r4Title := r4.AddCell()
  375. r4Title.HMerge = 1
  376. r4Title.Value = "成都深云智能科技有限公司"
  377. r4Title.SetStyle(endStyle)
  378. AddSpaceRow(r4, defaultLine+1)
  379. r5 := sheet.AddRow()
  380. for i := 0; i < 5; i++ {
  381. tmpStyle := xlsx.NewStyle()
  382. tmpStyle.Fill = allFill
  383. r5.AddCell().SetStyle(tmpStyle)
  384. }
  385. r5Title := r5.AddCell()
  386. r5Title.HMerge = 1
  387. s, _ := time.Parse("2006-01-02", reply.BillCreateTime)
  388. r5Title.Value = strconv.Itoa(s.Year()) + "年" + strconv.Itoa(int(s.Month())) + "月" + strconv.Itoa(s.Day()) + "日"
  389. r5Title.SetStyle(endStyle)
  390. AddSpaceRow(r5, defaultLine+1)
  391. for i := 0; i < 20; i++ {
  392. space := sheet.AddRow()
  393. AddSpaceRow(space, defaultLine+7)
  394. }
  395. err = file.Save(fileName)
  396. if err != nil {
  397. return err
  398. }
  399. // 3 upload to oss ,得到文件地址
  400. reply.Url, err = UploadOss(fileName, fileName)
  401. // 删除本地文件
  402. go os.Remove(fileName)
  403. return nil
  404. }
  405. func ossGetUrl() string {
  406. endpoint := strings.Replace(config.Conf.Oss.EndPoint, "https://", "", -1)
  407. return "https://" + config.Conf.Oss.Bucket + "." + endpoint
  408. }
  409. // UploadOss 上传本地文件到oss
  410. func UploadOss(source, file string) (string, error) {
  411. client, err := oss.New(config.Conf.Oss.EndPoint, config.Conf.Oss.AccessKeyId, config.Conf.Oss.AccessKeySecret)
  412. if err != nil {
  413. by, _ := json.Marshal(config.Conf.Oss)
  414. l.Error("oss",
  415. zap.String("call", "New"),
  416. zap.String("params", string(by)),
  417. zap.String("error", err.Error()))
  418. return "", err
  419. }
  420. bucket, err := client.Bucket(config.Conf.Oss.Bucket)
  421. if err != nil {
  422. l.Error("oss",
  423. zap.String("call", "Bucket"),
  424. zap.String("params", config.Conf.Oss.Bucket),
  425. zap.String("error", err.Error()))
  426. return "", err
  427. }
  428. err = bucket.PutObjectFromFile(file, source)
  429. if err != nil {
  430. l.Error("oss",
  431. zap.String("call", "PutObjectFromFile"),
  432. zap.String("params", source),
  433. zap.String("error", err.Error()))
  434. return "", err
  435. }
  436. filePath := ossGetUrl() + "/" + file
  437. return filePath, nil
  438. }