Purchase.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. package controllers
  2. import (
  3. "ERP_storage/Nats/NatsServer"
  4. "ERP_storage/conf"
  5. "ERP_storage/logs"
  6. "ERP_storage/models/Account"
  7. "ERP_storage/models/Purchase"
  8. "fmt"
  9. "github.com/beego/beego/v2/adapter/orm"
  10. beego "github.com/beego/beego/v2/server/web"
  11. "github.com/xuri/excelize/v2"
  12. userlibs "gogs.baozhida.cn/zoie/ERP_libs/User"
  13. "gogs.baozhida.cn/zoie/ERP_libs/lib"
  14. "math"
  15. "net/url"
  16. "os"
  17. "time"
  18. )
  19. type PurchaseController struct {
  20. beego.Controller
  21. User userlibs.User
  22. }
  23. func (c *PurchaseController) Prepare() {
  24. c.User = *Account.User_r
  25. }
  26. // 管理员
  27. func (c *PurchaseController) Purchase_List() {
  28. // 分页参数 初始化
  29. page, _ := c.GetInt("page")
  30. if page < 1 {
  31. page = 1
  32. }
  33. page_z, _ := c.GetInt("page_z")
  34. if page_z < 1 {
  35. page_z = conf.Page_size
  36. }
  37. T_uuid := c.GetString("T_uuid")
  38. T_dept := c.GetString("T_dept")
  39. T_start_date := c.GetString("T_start_date") // 开始时间
  40. T_end_date := c.GetString("T_end_date") // 结束时间
  41. T_state, _ := c.GetInt("T_state")
  42. userList, _ := NatsServer.Read_User_List_All()
  43. Account.Read_User_All_Map(userList)
  44. PurchaseDao := Purchase.NewPurchase(orm.NewOrm())
  45. R_List, R_cnt := PurchaseDao.Read_Purchase_List(T_dept, T_uuid, "", T_state, true, T_start_date, T_end_date, page, page_z)
  46. var r_jsons lib.R_JSONS
  47. r_jsons.Num = R_cnt
  48. r_jsons.Data = R_List
  49. r_jsons.Page = page
  50. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  51. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  52. c.ServeJSON()
  53. return
  54. }
  55. func (c *PurchaseController) Purchase_Audit_List() {
  56. // 分页参数 初始化
  57. page, _ := c.GetInt("page")
  58. if page < 1 {
  59. page = 1
  60. }
  61. page_z, _ := c.GetInt("page_z")
  62. if page_z < 1 {
  63. page_z = conf.Page_size
  64. }
  65. userList, _ := NatsServer.Read_User_List_All()
  66. Account.Read_User_All_Map(userList)
  67. PurchaseDao := Purchase.NewPurchase(orm.NewOrm())
  68. R_List, R_cnt := PurchaseDao.Read_Purchase_List("", "", c.User.T_uuid, Purchase.WaitAudit, false, "", "", page, page_z)
  69. var r_jsons lib.R_JSONS
  70. r_jsons.Num = R_cnt
  71. r_jsons.Data = R_List
  72. r_jsons.Page = page
  73. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  74. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  75. c.ServeJSON()
  76. return
  77. }
  78. // 用户采购列表
  79. func (c *PurchaseController) Purchase_User_List() {
  80. // 分页参数 初始化
  81. page, _ := c.GetInt("page")
  82. if page < 1 {
  83. page = 1
  84. }
  85. page_z, _ := c.GetInt("page_z")
  86. if page_z < 1 {
  87. page_z = conf.Page_size
  88. }
  89. T_start_date := c.GetString("T_start_date") // 开始时间
  90. T_end_date := c.GetString("T_end_date") // 结束时间
  91. T_state, _ := c.GetInt("T_state")
  92. userList, _ := NatsServer.Read_User_List_All()
  93. Account.Read_User_All_Map(userList)
  94. PurchaseDao := Purchase.NewPurchase(orm.NewOrm())
  95. R_List, R_cnt := PurchaseDao.Read_Purchase_List("", c.User.T_uuid, "", T_state, false, T_start_date, T_end_date, page, page_z)
  96. var r_jsons lib.R_JSONS
  97. r_jsons.Num = R_cnt
  98. r_jsons.Data = R_List
  99. r_jsons.Page = page
  100. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  101. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  102. c.ServeJSON()
  103. return
  104. }
  105. func (c *PurchaseController) Purchase_Get() {
  106. // 查询
  107. T_id, _ := c.GetInt("T_id")
  108. o := orm.NewOrm()
  109. PurchaseDao := Purchase.NewPurchase(o)
  110. purchase, err := PurchaseDao.Read_Purchase_ById(T_id)
  111. if err != nil {
  112. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  113. c.ServeJSON()
  114. return
  115. }
  116. userList, _ := NatsServer.Read_User_List_All()
  117. Account.Read_User_All_Map(userList)
  118. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Purchase.PurchaseToPurchase_Detail(purchase)}
  119. c.ServeJSON()
  120. return
  121. }
  122. func (c *PurchaseController) Purchase_Add() {
  123. T_uuid := c.GetString("T_uuid")
  124. T_date := c.GetString("T_date")
  125. T_remark := c.GetString("T_remark")
  126. T_detail := c.GetString("T_detail")
  127. T_approver := c.GetString("T_approver")
  128. T_dept := c.GetString("T_dept")
  129. var_ := Purchase.Purchase{
  130. T_dept: T_dept,
  131. T_uuid: T_uuid,
  132. T_submit: c.User.T_uuid,
  133. T_date: T_date,
  134. T_remark: T_remark,
  135. T_detail: T_detail,
  136. T_approver: T_approver,
  137. T_State: Purchase.WaitAudit,
  138. }
  139. o := orm.NewOrm()
  140. PurchaseDao := Purchase.NewPurchase(o)
  141. id, err := PurchaseDao.Add_Purchase(var_)
  142. if err != nil {
  143. //o.Rollback()
  144. c.Data["json"] = lib.JSONS{Code: 203, Msg: "添加失败"}
  145. c.ServeJSON()
  146. return
  147. }
  148. NatsServer.AddNews(T_approver, fmt.Sprintf("【采购申请】您有一条%s的采购申请待审批", c.User.T_name), conf.PurchaseAuditUrl)
  149. NatsServer.AddUserLogs(c.User.T_uuid, "采购", "添加", var_)
  150. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: id}
  151. c.ServeJSON()
  152. return
  153. }
  154. func (c *PurchaseController) Purchase_Approval() {
  155. T_id, _ := c.GetInt("T_id")
  156. T_State, _ := c.GetInt("T_State")
  157. o := orm.NewOrm()
  158. PurchaseDao := Purchase.NewPurchase(o)
  159. purchase, err := PurchaseDao.Read_Purchase_ById(T_id)
  160. if err != nil {
  161. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
  162. c.ServeJSON()
  163. return
  164. }
  165. if purchase.T_State == T_State {
  166. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_id}
  167. c.ServeJSON()
  168. return
  169. }
  170. // 1 待采购 2 已采购
  171. purchase.T_State = T_State
  172. _, err = PurchaseDao.Update_Purchase(purchase, "T_State")
  173. if err != nil {
  174. c.Data["json"] = lib.JSONS{Code: 203, Msg: "添加失败"}
  175. c.ServeJSON()
  176. return
  177. }
  178. if T_State == Purchase.PurchaseCompleted {
  179. NatsServer.AddNews(purchase.T_uuid, "【采购审核】您提交的采购申请已采购", conf.MyPurchaseNewsUrl)
  180. }
  181. NatsServer.AddUserLogs(c.User.T_uuid, "采购申请", "采购", purchase)
  182. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_id}
  183. c.ServeJSON()
  184. return
  185. }
  186. func (c *PurchaseController) Purchase_Edit() {
  187. T_id, _ := c.GetInt("T_id")
  188. T_uuid := c.GetString("T_uuid")
  189. T_date := c.GetString("T_date")
  190. T_remark := c.GetString("T_remark")
  191. T_State, _ := c.GetInt("T_State")
  192. T_detail := c.GetString("T_detail")
  193. T_approver := c.GetString("T_approver")
  194. T_dept := c.GetString("T_dept")
  195. o := orm.NewOrm()
  196. PurchaseDao := Purchase.NewPurchase(o)
  197. purchase, err := PurchaseDao.Read_Purchase_ById(T_id)
  198. if err != nil {
  199. //o.Rollback()
  200. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
  201. c.ServeJSON()
  202. return
  203. }
  204. clos := []string{}
  205. if len(T_uuid) > 0 {
  206. purchase.T_uuid = T_uuid
  207. clos = append(clos, "T_uuid")
  208. }
  209. if len(T_date) > 0 {
  210. purchase.T_date = T_date
  211. clos = append(clos, "T_date")
  212. }
  213. if len(T_dept) > 0 {
  214. purchase.T_dept = T_dept
  215. clos = append(clos, "T_dept")
  216. }
  217. if len(T_remark) > 0 {
  218. purchase.T_remark = T_remark
  219. clos = append(clos, "T_remark")
  220. }
  221. if len(T_detail) > 0 {
  222. purchase.T_detail = T_detail
  223. clos = append(clos, "T_detail")
  224. }
  225. if len(T_approver) > 0 {
  226. purchase.T_approver = T_approver
  227. clos = append(clos, "T_approver")
  228. }
  229. if T_State > 0 {
  230. if T_State == Purchase.NotPass {
  231. T_State = Purchase.WaitAudit
  232. }
  233. purchase.T_State = T_State
  234. clos = append(clos, "T_State")
  235. }
  236. _, err = PurchaseDao.Update_Purchase(purchase, clos...)
  237. if err != nil {
  238. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改失败"}
  239. c.ServeJSON()
  240. return
  241. }
  242. NatsServer.AddUserLogs(c.User.T_uuid, "采购", "修改", purchase)
  243. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: purchase.Id}
  244. c.ServeJSON()
  245. return
  246. }
  247. func (c *PurchaseController) Purchase_Del() {
  248. T_id, _ := c.GetInt("T_id")
  249. o := orm.NewOrm()
  250. PurchaseDao := Purchase.NewPurchase(o)
  251. purchase, err := PurchaseDao.Read_Purchase_ById(T_id)
  252. if err != nil {
  253. //o.Rollback()
  254. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
  255. c.ServeJSON()
  256. return
  257. }
  258. if purchase.T_State == 2 {
  259. c.Data["json"] = lib.JSONS{Code: 202, Msg: "已采购,禁止删除!"}
  260. c.ServeJSON()
  261. return
  262. }
  263. _, err = PurchaseDao.Delete_Purchase(purchase)
  264. if err != nil {
  265. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  266. c.ServeJSON()
  267. return
  268. }
  269. NatsServer.AddUserLogs(c.User.T_uuid, "采购", "删除", purchase.Id)
  270. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: purchase.Id}
  271. c.ServeJSON()
  272. return
  273. }
  274. // 下载采购申请
  275. func (c *PurchaseController) Purchase_Excel() {
  276. T_id, _ := c.GetInt("T_id")
  277. o := orm.NewOrm()
  278. PurchaseDao := Purchase.NewPurchase(o)
  279. purchase, err := PurchaseDao.Read_Purchase_ById(T_id)
  280. if err != nil {
  281. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  282. c.ServeJSON()
  283. return
  284. }
  285. userList, _ := NatsServer.Read_User_List_All()
  286. Account.Read_User_All_Map(userList)
  287. detail := Purchase.PurchaseToPurchase_Detail(purchase)
  288. f := excelize.NewFile() //设置单元格值
  289. // 这里设置表头ÒÒ
  290. Style1, _ := f.NewStyle(
  291. &excelize.Style{
  292. Font: &excelize.Font{Size: 16, Family: "宋体"},
  293. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  294. Border: []excelize.Border{
  295. {Type: "left", Color: "000000", Style: 1},
  296. {Type: "top", Color: "000000", Style: 1},
  297. {Type: "bottom", Color: "000000", Style: 1},
  298. {Type: "right", Color: "000000", Style: 1},
  299. },
  300. })
  301. Style2, _ := f.NewStyle(
  302. &excelize.Style{
  303. Font: &excelize.Font{Size: 15, Family: "宋体"},
  304. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center", WrapText: true},
  305. Border: []excelize.Border{
  306. {Type: "left", Color: "000000", Style: 1},
  307. {Type: "top", Color: "000000", Style: 1},
  308. {Type: "bottom", Color: "000000", Style: 1},
  309. {Type: "right", Color: "000000", Style: 1},
  310. },
  311. })
  312. Style3, _ := f.NewStyle(
  313. &excelize.Style{
  314. Font: &excelize.Font{Size: 13, Family: "宋体"},
  315. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  316. Border: []excelize.Border{
  317. {Type: "left", Color: "000000", Style: 1},
  318. {Type: "top", Color: "000000", Style: 1},
  319. {Type: "bottom", Color: "000000", Style: 1},
  320. {Type: "right", Color: "000000", Style: 1},
  321. },
  322. })
  323. Style4, _ := f.NewStyle(
  324. &excelize.Style{
  325. Font: &excelize.Font{Size: 13, Family: "宋体"},
  326. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  327. Border: []excelize.Border{
  328. {Type: "left", Color: "000000", Style: 1},
  329. {Type: "top", Color: "000000", Style: 1},
  330. {Type: "bottom", Color: "000000", Style: 1},
  331. {Type: "right", Color: "000000", Style: 1},
  332. },
  333. })
  334. f.MergeCell("Sheet1", "A1", "I1")
  335. f.SetCellValue("Sheet1", "A1", fmt.Sprintf("%d宝智达冷链采购申请", time.Now().Year()))
  336. f.SetCellStyle("Sheet1", "A1", "I1", Style1)
  337. f.MergeCell("Sheet1", "A2", "E2")
  338. date, _ := lib.DateStrToTime(detail.T_date)
  339. f.SetCellValue("Sheet1", "A2", fmt.Sprintf("申请时间:%s", date.Format("2006年01月02日")))
  340. f.MergeCell("Sheet1", "F2", "I2")
  341. f.SetCellValue("Sheet1", "F2", fmt.Sprintf("申请人:%-10s负责人审批:", detail.T_uuid_name))
  342. f.MergeCell("Sheet1", "A3", "E3")
  343. f.SetCellValue("Sheet1", "A3", "执行时间:")
  344. f.MergeCell("Sheet1", "F3", "I3")
  345. f.SetCellValue("Sheet1", "F3", fmt.Sprintf("执行部门:%-15s执行人:", ""))
  346. f.MergeCell("Sheet1", "A4", "E4")
  347. f.SetCellValue("Sheet1", "A4", fmt.Sprintf("审批人:%s", detail.T_approver_name))
  348. f.MergeCell("Sheet1", "F4", "I4")
  349. approver_date := ""
  350. if len(detail.T_approver_date) > 0 {
  351. date2, _ := lib.DateStrToTime(detail.T_approver_date)
  352. approver_date = date2.Format("2006年01月02日")
  353. }
  354. f.SetCellValue("Sheet1", "F4", fmt.Sprintf("审批时间:%s", approver_date))
  355. f.SetCellStyle("Sheet1", "A2", "I4", Style2)
  356. f.SetRowHeight("Sheet1", 1, 40)
  357. height := 30.0
  358. f.SetRowHeight("Sheet1", 2, height)
  359. f.SetRowHeight("Sheet1", 3, height)
  360. f.SetRowHeight("Sheet1", 4, height)
  361. f.SetRowHeight("Sheet1", 5, height)
  362. f.MergeCell("Sheet1", "A5", "I5")
  363. f.SetCellValue("Sheet1", "A5", "申请采购明细")
  364. f.SetCellStyle("Sheet1", "A5", "I5", Style1)
  365. f.SetCellValue("Sheet1", "A6", "序号")
  366. f.SetCellValue("Sheet1", "B6", "名称")
  367. f.SetCellValue("Sheet1", "C6", "型号")
  368. f.SetCellValue("Sheet1", "D6", "规格")
  369. f.SetCellValue("Sheet1", "E6", "数量")
  370. f.SetCellValue("Sheet1", "F6", "参考网址")
  371. f.SetCellValue("Sheet1", "G6", "需求")
  372. f.SetCellValue("Sheet1", "H6", "备注")
  373. f.SetCellValue("Sheet1", "I6", "采购金额")
  374. // 这里设置表头
  375. f.SetCellStyle("Sheet1", "A6", "H6", Style3)
  376. f.SetRowHeight("Sheet1", 2, 25)
  377. // 设置列宽
  378. f.SetColWidth("Sheet1", "A", "A", 6)
  379. f.SetColWidth("Sheet1", "B", "B", 12)
  380. f.SetColWidth("Sheet1", "C", "C", 10)
  381. f.SetColWidth("Sheet1", "D", "D", 10)
  382. f.SetColWidth("Sheet1", "E", "E", 6)
  383. f.SetColWidth("Sheet1", "F", "F", 30)
  384. f.SetColWidth("Sheet1", "G", "G", 12)
  385. f.SetColWidth("Sheet1", "H", "H", 15)
  386. f.SetColWidth("Sheet1", "I", "I", 15)
  387. line := 6
  388. for i, v := range detail.T_Detail {
  389. line++
  390. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), i+1)
  391. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), v.T_name)
  392. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), v.T_model)
  393. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), v.T_spec)
  394. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), v.T_quantity)
  395. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), v.T_reference_site)
  396. f.SetCellValue("Sheet1", fmt.Sprintf("G%d", line), v.T_demand)
  397. f.SetCellValue("Sheet1", fmt.Sprintf("H%d", line), v.T_remark)
  398. f.SetCellValue("Sheet1", fmt.Sprintf("I%d", line), fmt.Sprintf("%.2f", v.T_unit_price*float32(v.T_quantity)))
  399. }
  400. line++
  401. f.MergeCell("Sheet1", fmt.Sprintf("A%d", line), fmt.Sprintf("H%d", line))
  402. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), "合计")
  403. f.SetCellFormula("Sheet1", fmt.Sprintf("I%d", line), fmt.Sprintf("SUM(I7:I%d)", line-1))
  404. f.SetCellStyle("Sheet1", "A6", fmt.Sprintf("I%d", line), Style4)
  405. timeStr := time.Now().Format("20060102150405")
  406. fileName := fmt.Sprintf("%d宝智达冷链采购申请%v.xlsx", time.Now().Year(), timeStr)
  407. filePath := "ofile/" + fileName
  408. // 保存文件
  409. if err = f.SaveAs(filePath); err != nil {
  410. logs.Error("保存文件失败:", err)
  411. }
  412. defer func() {
  413. os.Remove(filePath)
  414. }()
  415. c.Ctx.Output.Header("Content-Type", "application/vnd.ms-excel;charset=utf8")
  416. c.Ctx.Output.Header("Content-Disposition", "attachment; filename="+url.PathEscape(fileName))
  417. c.Ctx.Output.Header("Content-Transfer-Encoding", "binary")
  418. c.Ctx.Output.Download(filePath, fileName)
  419. }
  420. // 下载采购统计
  421. func (c *PurchaseController) Purchase_Stat_Excel() {
  422. T_uuid := c.GetString("T_uuid")
  423. T_dept := c.GetString("T_dept")
  424. T_start_date := c.GetString("T_start_date") // 开始时间
  425. T_end_date := c.GetString("T_end_date") // 结束时间
  426. T_state, _ := c.GetInt("T_state")
  427. userList, _ := NatsServer.Read_User_List_All()
  428. Account.Read_User_All_Map(userList)
  429. PurchaseDao := Purchase.NewPurchase(orm.NewOrm())
  430. R_List, _ := PurchaseDao.Read_Purchase_Stat(T_dept, T_uuid, "", T_state, true, T_start_date, T_end_date, 0, 9999)
  431. f := excelize.NewFile() //设置单元格值
  432. // 这里设置表头ÒÒ
  433. Style1, _ := f.NewStyle(
  434. &excelize.Style{
  435. Font: &excelize.Font{Size: 20, Family: "宋体"},
  436. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  437. })
  438. Style2, _ := f.NewStyle(
  439. &excelize.Style{
  440. Font: &excelize.Font{Size: 15, Family: "宋体"},
  441. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  442. Border: []excelize.Border{
  443. {Type: "left", Color: "000000", Style: 1},
  444. {Type: "top", Color: "000000", Style: 1},
  445. {Type: "bottom", Color: "000000", Style: 1},
  446. {Type: "right", Color: "000000", Style: 1},
  447. },
  448. Fill: excelize.Fill{Type: "pattern", Color: []string{"D9D9D9"}, Pattern: 1},
  449. })
  450. Style3, _ := f.NewStyle(
  451. &excelize.Style{
  452. Font: &excelize.Font{Size: 15, Family: "宋体"},
  453. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  454. Border: []excelize.Border{
  455. {Type: "left", Color: "000000", Style: 1},
  456. {Type: "top", Color: "000000", Style: 1},
  457. {Type: "bottom", Color: "000000", Style: 1},
  458. {Type: "right", Color: "000000", Style: 1},
  459. },
  460. })
  461. f.MergeCell("Sheet1", "A1", "M1")
  462. f.SetCellValue("Sheet1", "A1", "资金一览表")
  463. f.SetCellStyle("Sheet1", "A1", "M1", Style1)
  464. // 写入表头
  465. headers := []string{"序号", "采购物品", "规格", "单价", "数量", "实际金额", "合计金额", "用途", "采购申请人", "采购申请时间", "是否已有发票", "报销上报时间", "备注"}
  466. for col, header := range headers {
  467. cell := fmt.Sprintf("%c%d", rune('A'+col), 2)
  468. f.SetCellValue("Sheet1", cell, header)
  469. }
  470. // 这里设置表头
  471. f.SetCellStyle("Sheet1", "A2", "M2", Style2)
  472. f.SetRowHeight("Sheet1", 2, 25)
  473. colsWidth := []float64{6, 12, 10, 10, 6, 30, 12, 15, 15, 15, 15, 15, 15}
  474. for col, width := range colsWidth {
  475. cell := fmt.Sprintf("%c", rune('A'+col))
  476. f.SetColWidth("Sheet1", cell, cell, width)
  477. }
  478. line := 2
  479. for i, purchase := range R_List {
  480. // 写入数据
  481. startRow := line + 1
  482. for _, v := range purchase.T_Detail {
  483. line++
  484. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), i+1)
  485. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), v.T_name)
  486. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), v.T_model)
  487. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), v.T_unit_price)
  488. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), v.T_quantity)
  489. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), v.T_amount)
  490. if len(purchase.T_Detail) == 1 {
  491. f.SetCellValue("Sheet1", fmt.Sprintf("G%d", line), v.T_amount)
  492. f.SetCellValue("Sheet1", fmt.Sprintf("I%d", line), purchase.T_uuid_name)
  493. f.SetCellValue("Sheet1", fmt.Sprintf("J%d", line), purchase.T_date)
  494. f.SetCellValue("Sheet1", fmt.Sprintf("M%d", line), purchase.T_remark)
  495. }
  496. }
  497. if len(purchase.T_Detail) > 1 {
  498. f.MergeCell("Sheet1", fmt.Sprintf("G%d", startRow), fmt.Sprintf("G%d", line))
  499. f.SetCellFormula("Sheet1", fmt.Sprintf("G%d", startRow), fmt.Sprintf("SUM(F%d:F%d)", startRow, line))
  500. f.MergeCell("Sheet1", fmt.Sprintf("I%d", startRow), fmt.Sprintf("I%d", line))
  501. f.SetCellValue("Sheet1", fmt.Sprintf("I%d", startRow), purchase.T_uuid_name)
  502. f.MergeCell("Sheet1", fmt.Sprintf("J%d", startRow), fmt.Sprintf("J%d", line))
  503. f.SetCellValue("Sheet1", fmt.Sprintf("J%d", startRow), purchase.T_date)
  504. f.MergeCell("Sheet1", fmt.Sprintf("M%d", startRow), fmt.Sprintf("M%d", line))
  505. f.SetCellValue("Sheet1", fmt.Sprintf("M%d", startRow), purchase.T_remark)
  506. }
  507. }
  508. f.SetCellStyle("Sheet1", "A3", fmt.Sprintf("M%d", line), Style3)
  509. timeStr := time.Now().Format("20060102150405")
  510. fileName := fmt.Sprintf("%d资金一览表%v.xlsx", time.Now().Year(), timeStr)
  511. filePath := "ofile/" + fileName
  512. // 保存文件
  513. if err := f.SaveAs(filePath); err != nil {
  514. logs.Error("保存文件失败:", err)
  515. }
  516. defer func() {
  517. os.Remove(filePath)
  518. }()
  519. c.Ctx.Output.Header("Content-Type", "application/vnd.ms-excel;charset=utf8")
  520. c.Ctx.Output.Header("Content-Disposition", "attachment; filename="+url.PathEscape(fileName))
  521. c.Ctx.Output.Header("Content-Transfer-Encoding", "binary")
  522. c.Ctx.Output.Download(filePath, fileName)
  523. }
  524. func (c *PurchaseController) User_List() {
  525. var r_jsons lib.R_JSONS
  526. // 分页参数 初始化
  527. page, _ := c.GetInt("page")
  528. if page < 1 {
  529. page = 1
  530. }
  531. page_z, _ := c.GetInt("page_z")
  532. if page_z < 1 {
  533. page_z = conf.Page_size
  534. }
  535. T_name := c.GetString("T_name")
  536. PurchaseDao := Purchase.NewPurchase(orm.NewOrm())
  537. uuidList := PurchaseDao.Read_T_uuid_List()
  538. if len(uuidList) == 0 {
  539. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  540. c.ServeJSON()
  541. return
  542. }
  543. R_List, R_cnt, err := NatsServer.Read_User_List_T_uuid(T_name, uuidList, page, page_z)
  544. if err != nil {
  545. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败"}
  546. c.ServeJSON()
  547. return
  548. }
  549. var U_List []userlibs.User
  550. for _, user := range R_List {
  551. U_List = append(U_List, user)
  552. }
  553. r_jsons.Num = R_cnt
  554. r_jsons.Data = U_List
  555. r_jsons.Page = page
  556. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  557. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  558. c.ServeJSON()
  559. return
  560. }