广告平台(站长使用)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

303 lines
11 KiB

  1. package hdl
  2. import (
  3. "applet/app/e"
  4. "applet/app/lib/validate"
  5. "applet/app/md"
  6. "applet/app/svc"
  7. "applet/app/utils"
  8. db "code.fnuoos.com/zhimeng/model.git/src"
  9. "code.fnuoos.com/zhimeng/model.git/src/super/implement"
  10. "fmt"
  11. "github.com/boombuler/barcode"
  12. "github.com/boombuler/barcode/code128"
  13. "github.com/gin-gonic/gin"
  14. "github.com/jung-kurt/gofpdf"
  15. "image/jpeg"
  16. "net/url"
  17. "os"
  18. "strings"
  19. "time"
  20. )
  21. // SettleCenterMediumList
  22. // @Summary 媒体列表
  23. // @Tags 结算中心------嘉俊
  24. // @Description 结算中心-媒体列表
  25. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  26. // @Accept json
  27. // @Produce json
  28. // @Param args body md.SettleCenterDataReq true "请求参数"
  29. // @Success 200 {object} md.SettleCenterDataRes "具体看返回内容 这是data里面的数据"
  30. // @Failure 400 {object} md.Response "具体错误"
  31. // @Router /api/settleCenter/medium/list [POST]
  32. func SettleCenterMediumList(c *gin.Context) {
  33. var req md.SettleCenterDataReq
  34. err := c.ShouldBindJSON(&req)
  35. if err != nil {
  36. err = validate.HandleValidateErr(err)
  37. err1 := err.(e.E)
  38. e.OutErr(c, err1.Code, err1.Error())
  39. return
  40. }
  41. res := svc.SettleCenterMediumList(c, req)
  42. e.OutSuc(c, res, nil)
  43. return
  44. }
  45. // SettleCenterMediumSave
  46. // @Summary 媒体修改结算方式
  47. // @Tags 结算中心------嘉俊
  48. // @Description 结算中心-媒体修改结算方式
  49. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  50. // @Accept json
  51. // @Produce json
  52. // @Param args body md.SettleCenterDataSaveReq true "请求参数"
  53. // @Success 200 {string} "具体看返回内容 这是data里面的数据"
  54. // @Failure 400 {object} md.Response "具体错误"
  55. // @Router /api/settleCenter/medium/save [POST]
  56. func SettleCenterMediumSave(c *gin.Context) {
  57. var req md.SettleCenterDataSaveReq
  58. err := c.ShouldBindJSON(&req)
  59. if err != nil {
  60. err = validate.HandleValidateErr(err)
  61. err1 := err.(e.E)
  62. e.OutErr(c, err1.Code, err1.Error())
  63. return
  64. }
  65. svc.SettleCenterMediumSave(c, req)
  66. }
  67. // SettleCenterMediumDetail
  68. // @Summary 媒体详情
  69. // @Tags 结算中心------嘉俊
  70. // @Description 结算中心-媒体详情
  71. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  72. // @Accept json
  73. // @Produce json
  74. // @Param args body md.SettleCenterDataDetailReq true "请求参数"
  75. // @Success 200 {object} md.SettleCenterDataDetailRes "具体看返回内容 这是data里面的数据"
  76. // @Failure 400 {object} md.Response "具体错误"
  77. // @Router /api/settleCenter/medium/detail [POST]
  78. func SettleCenterMediumDetail(c *gin.Context) {
  79. var req md.SettleCenterDataDetailReq
  80. err := c.ShouldBindJSON(&req)
  81. if err != nil {
  82. err = validate.HandleValidateErr(err)
  83. err1 := err.(e.E)
  84. e.OutErr(c, err1.Code, err1.Error())
  85. return
  86. }
  87. res := svc.SettleCenterMediumDetail(c, req)
  88. e.OutSuc(c, res, nil)
  89. return
  90. }
  91. func SettleCenterSettleFileDown(c *gin.Context) {
  92. path := "/data/advertisement-pdf"
  93. id := c.Query("id")
  94. engine := db.Db
  95. NewMediumSettlementDb := implement.NewMediumSettlementDb(engine)
  96. data, _ := NewMediumSettlementDb.GetMediumSettlementById(utils.StrToInt(id))
  97. NewMediumListDb := implement.NewMediumListDb(engine)
  98. medium, _ := NewMediumListDb.GetMediumList(data.MediumId)
  99. // 创建一个新的PDF文件
  100. pdf := gofpdf.New("P", "mm", "A4", "")
  101. // 添加一页
  102. pdf.AddPage()
  103. // 设置文本颜色(参数为RGB值)
  104. pdf.SetTextColor(0, 0, 0)
  105. // 换行
  106. pdf.Ln(-1)
  107. // 引入中文字体,需要相应的字体文件
  108. pdf.AddUTF8Font("SourceHanSansCN-Light", "", path+"/_0000000000_2024103011290507244911708.ttf")
  109. // 引入中文字体后设置中文字体和字号
  110. left := 15.0
  111. pdf.Image(path+"/_0000000000_2024103011210157735330925.png", left, 25, 50, 15, false, "PNG", 0, "")
  112. pdf.SetFont("SourceHanSansCN-Light", "", 10)
  113. pdf.Text(130, 40, "杭州激活鸟网络科技有限公司")
  114. //// 生成EAN-13条形码
  115. num := "jihuoniao-Ads-" + time.Now().Format("20060102") + "-" + utils.IntToStr(data.Id)
  116. cs, _ := code128.Encode(num)
  117. // 创建一个要输出数据的文件
  118. file, _ := os.Create(path + "/" + num + ".jpg")
  119. defer file.Close()
  120. // 设置图片像素大小
  121. qrCode, _ := barcode.Scale(cs, 350, 100)
  122. // 将code128的条形码编码为png图片
  123. jpeg.Encode(file, qrCode, nil)
  124. pdf.Image(path+"/"+num+".jpg", left, 50, 170, 30, false, "JPEG", 0, "")
  125. pdf.SetFont("SourceHanSansCN-Light", "", 28)
  126. pdf.Text(left, 95, "激活鸟 · 变现中心结算对账单")
  127. pdf.SetFont("SourceHanSansCN-Light", "", 12)
  128. pdf.Text(left, 105, "结 算 单 号 : "+num)
  129. pdf.Text(left, 115, "制 表 日 期 : "+time.Now().Format("2006/01/02"))
  130. date := ""
  131. if data.StartDate != "0000-00-00" && data.StartDate != "" {
  132. date = strings.ReplaceAll(data.StartDate, "-", ".")
  133. }
  134. if data.EndDate != "0000-00-00" && data.EndDate != "" {
  135. date += " - " + strings.ReplaceAll(data.EndDate, "-", ".")
  136. }
  137. pdf.Text(left, 125, "结 算 日 期 : "+date)
  138. pdf.Text(left, 135, "客 户 名 称 : "+medium.CompanyName)
  139. // 定义表格内容
  140. pdf.SetFont("SourceHanSansCN-Light", "", 12)
  141. table := []string{
  142. "业务类型", "基础收益(元)", "其他调整(元)", "实际结算总计(元)",
  143. "广告合作", utils.Float64ToStr(float64(data.BasicIncome) / 100), utils.Float64ToStr(float64(data.OtherIncome) / 100), utils.Float64ToStr(float64(data.BasicIncome+data.OtherIncome) / 100),
  144. }
  145. // 定义表格列数
  146. col := 4
  147. // 定义单元格位置
  148. cellWidth := 10.0 * 4
  149. cellHeight := 8.0
  150. x := left
  151. y := 140.0
  152. for i := 0; i < len(table); {
  153. pdf.SetXY(x, y)
  154. if i == col {
  155. cellHeight = 13
  156. }
  157. for j := 0; j < col; j++ {
  158. if i < len(table) {
  159. // 循环绘制单元格并输入内容
  160. pdf.CellFormat(cellWidth, cellHeight, table[i], "1", 0, "C", false, 0, "")
  161. }
  162. i++
  163. }
  164. y = y + cellHeight
  165. }
  166. pdf.SetFont("SourceHanSansCN-Light", "", 12)
  167. pdf.Text(100, 170, "实际结算总计(元):")
  168. pdf.Text(160, 170, utils.Float64ToStr(float64(data.BasicIncome+data.OtherIncome)/100))
  169. pdf.Text(210/2-20, 190, "数据确认回执栏")
  170. pdf.Line(left, 193, 180, 194)
  171. pdf.Text(left, 200, "数据确认")
  172. pdf.Text(left, 210, "公司(盖章或手印):")
  173. pdf.Text(left, 220, "授权人代表签字:")
  174. pdf.Text(left, 230, "日期:")
  175. // 保存PDF文件
  176. pdfName := medium.CompanyName + "(" + strings.ReplaceAll(date, " ", "") + ")结算单.pdf"
  177. err := pdf.OutputFileAndClose(path + "/" + pdfName)
  178. if err != nil {
  179. fmt.Println(err)
  180. return
  181. }
  182. c.Header("Content-Type", "application/octet-stream")
  183. c.Header("Content-Disposition", "attachment; filename="+url.QueryEscape(pdfName))
  184. c.Header("Content-Transfer-Encoding", "binary")
  185. c.File(path + "/" + pdfName)
  186. os.Remove(path + "/" + pdfName)
  187. file.Close()
  188. err = os.Remove(path + "/" + num + ".jpg")
  189. fmt.Println(err)
  190. return
  191. }
  192. func AgentSettleCenterSettleFileDown(c *gin.Context) {
  193. path := "/data/advertisement-pdf"
  194. id := c.Query("id")
  195. engine := db.Db
  196. NewMediumSettlementDb := implement.NewAgentSettlementDb(engine)
  197. data, _ := NewMediumSettlementDb.GetAgentSettlementById(utils.StrToInt(id))
  198. NewMediumListDb := implement.NewAgentListDb(engine)
  199. medium, _ := NewMediumListDb.GetAgentList(data.AgentId)
  200. // 创建一个新的PDF文件
  201. pdf := gofpdf.New("P", "mm", "A4", "")
  202. // 添加一页
  203. pdf.AddPage()
  204. // 设置文本颜色(参数为RGB值)
  205. pdf.SetTextColor(0, 0, 0)
  206. // 换行
  207. pdf.Ln(-1)
  208. // 引入中文字体,需要相应的字体文件
  209. pdf.AddUTF8Font("SourceHanSansCN-Light", "", path+"/_0000000000_2024103011290507244911708.ttf")
  210. // 引入中文字体后设置中文字体和字号
  211. left := 15.0
  212. pdf.Image(path+"/_0000000000_2024103011210157735330925.png", left, 25, 50, 15, false, "PNG", 0, "")
  213. pdf.SetFont("SourceHanSansCN-Light", "", 10)
  214. pdf.Text(130, 40, "杭州激活鸟网络科技有限公司")
  215. //// 生成EAN-13条形码
  216. num := "jihuoniao-Ads-agent-" + time.Now().Format("20060102") + "-" + utils.IntToStr(data.Id)
  217. cs, _ := code128.Encode(num)
  218. // 创建一个要输出数据的文件
  219. file, _ := os.Create(path + "/" + num + ".jpg")
  220. defer file.Close()
  221. // 设置图片像素大小
  222. qrCode, _ := barcode.Scale(cs, 350, 100)
  223. // 将code128的条形码编码为png图片
  224. jpeg.Encode(file, qrCode, nil)
  225. pdf.Image(path+"/"+num+".jpg", left, 50, 170, 30, false, "JPEG", 0, "")
  226. pdf.SetFont("SourceHanSansCN-Light", "", 28)
  227. pdf.Text(left, 95, "激活鸟 · 变现中心结算对账单")
  228. pdf.SetFont("SourceHanSansCN-Light", "", 12)
  229. pdf.Text(left, 105, "结 算 单 号 : "+num)
  230. pdf.Text(left, 115, "制 表 日 期 : "+time.Now().Format("2006/01/02"))
  231. date := ""
  232. if data.StartDate != "0000-00-00" && data.StartDate != "" {
  233. date = strings.ReplaceAll(data.StartDate, "-", ".")
  234. }
  235. if data.EndDate != "0000-00-00" && data.EndDate != "" {
  236. date += " - " + strings.ReplaceAll(data.EndDate, "-", ".")
  237. }
  238. pdf.Text(left, 125, "结 算 日 期 : "+date)
  239. pdf.Text(left, 135, "客 户 名 称 : "+medium.CompanyName)
  240. // 定义表格内容
  241. pdf.SetFont("SourceHanSansCN-Light", "", 12)
  242. table := []string{
  243. "业务类型", "基础收益(元)", "其他调整(元)", "实际结算总计(元)",
  244. "广告合作", utils.Float64ToStr(float64(data.BasicIncome) / 100), utils.Float64ToStr(float64(data.OtherIncome) / 100), utils.Float64ToStr(float64(data.BasicIncome+data.OtherIncome) / 100),
  245. }
  246. // 定义表格列数
  247. col := 4
  248. // 定义单元格位置
  249. cellWidth := 10.0 * 4
  250. cellHeight := 8.0
  251. x := left
  252. y := 140.0
  253. for i := 0; i < len(table); {
  254. pdf.SetXY(x, y)
  255. if i == col {
  256. cellHeight = 13
  257. }
  258. for j := 0; j < col; j++ {
  259. if i < len(table) {
  260. // 循环绘制单元格并输入内容
  261. pdf.CellFormat(cellWidth, cellHeight, table[i], "1", 0, "C", false, 0, "")
  262. }
  263. i++
  264. }
  265. y = y + cellHeight
  266. }
  267. pdf.SetFont("SourceHanSansCN-Light", "", 12)
  268. pdf.Text(100, 170, "实际结算总计(元):")
  269. pdf.Text(160, 170, utils.Float64ToStr(float64(data.BasicIncome+data.OtherIncome)/100))
  270. pdf.Text(210/2-20, 190, "数据确认回执栏")
  271. pdf.Line(left, 193, 180, 194)
  272. pdf.Text(left, 200, "数据确认")
  273. pdf.Text(left, 210, "公司(盖章或手印):")
  274. pdf.Text(left, 220, "授权人代表签字:")
  275. pdf.Text(left, 230, "日期:")
  276. // 保存PDF文件
  277. pdfName := medium.CompanyName + "(" + strings.ReplaceAll(date, " ", "") + ")结算单.pdf"
  278. err := pdf.OutputFileAndClose(path + "/" + pdfName)
  279. if err != nil {
  280. fmt.Println(err)
  281. return
  282. }
  283. c.Header("Content-Type", "application/octet-stream")
  284. c.Header("Content-Disposition", "attachment; filename="+url.QueryEscape(pdfName))
  285. c.Header("Content-Transfer-Encoding", "binary")
  286. c.File(path + "/" + pdfName)
  287. os.Remove(path + "/" + pdfName)
  288. file.Close()
  289. err = os.Remove(path + "/" + num + ".jpg")
  290. fmt.Println(err)
  291. return
  292. }