Purchase.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  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. "math"
  10. "net/url"
  11. "os"
  12. "time"
  13. "github.com/beego/beego/v2/adapter/orm"
  14. beego "github.com/beego/beego/v2/server/web"
  15. "github.com/xuri/excelize/v2"
  16. userlibs "gogs.baozhida.cn/zoie/ERP_libs/User"
  17. "gogs.baozhida.cn/zoie/ERP_libs/lib"
  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. T_product_name := c.GetString("T_product_name")
  130. T_total_price, _ := c.GetFloat("T_total_price")
  131. T_product_quantity, _ := c.GetInt("T_product_quantity")
  132. var_ := Purchase.Purchase{
  133. T_dept: T_dept,
  134. T_uuid: T_uuid,
  135. T_submit: c.User.T_uuid,
  136. T_date: T_date,
  137. T_remark: T_remark,
  138. T_detail: T_detail,
  139. T_approver: T_approver,
  140. T_State: Purchase.WaitAudit,
  141. T_product_name: T_product_name,
  142. T_total_price: float32(T_total_price),
  143. T_product_quantity: T_product_quantity,
  144. }
  145. o := orm.NewOrm()
  146. PurchaseDao := Purchase.NewPurchase(o)
  147. id, err := PurchaseDao.Add_Purchase(var_)
  148. if err != nil {
  149. //o.Rollback()
  150. c.Data["json"] = lib.JSONS{Code: 203, Msg: "添加失败"}
  151. c.ServeJSON()
  152. return
  153. }
  154. NatsServer.AddNews(T_approver, fmt.Sprintf("【采购申请】您有一条%s的采购申请待审批", c.User.T_name), conf.PurchaseAuditUrl)
  155. NatsServer.AddUserLogs(c.User.T_uuid, "采购", "添加", var_)
  156. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: id}
  157. c.ServeJSON()
  158. return
  159. }
  160. func (c *PurchaseController) Purchase_Approval() {
  161. T_id, _ := c.GetInt("T_id")
  162. T_State, _ := c.GetInt("T_State")
  163. o := orm.NewOrm()
  164. PurchaseDao := Purchase.NewPurchase(o)
  165. purchase, err := PurchaseDao.Read_Purchase_ById(T_id)
  166. if err != nil {
  167. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
  168. c.ServeJSON()
  169. return
  170. }
  171. if purchase.T_State == T_State {
  172. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_id}
  173. c.ServeJSON()
  174. return
  175. }
  176. // 1 待采购 2 已采购
  177. purchase.T_State = T_State
  178. _, err = PurchaseDao.Update_Purchase(purchase, "T_State")
  179. if err != nil {
  180. c.Data["json"] = lib.JSONS{Code: 203, Msg: "添加失败"}
  181. c.ServeJSON()
  182. return
  183. }
  184. if T_State == Purchase.PurchaseCompleted {
  185. NatsServer.AddNews(purchase.T_uuid, "【采购审核】您提交的采购申请已采购", conf.MyPurchaseNewsUrl)
  186. }
  187. NatsServer.AddUserLogs(c.User.T_uuid, "采购申请", "采购", purchase)
  188. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_id}
  189. c.ServeJSON()
  190. return
  191. }
  192. func (c *PurchaseController) Purchase_Edit() {
  193. T_id, _ := c.GetInt("T_id")
  194. T_uuid := c.GetString("T_uuid")
  195. T_date := c.GetString("T_date")
  196. T_remark := c.GetString("T_remark")
  197. T_State, _ := c.GetInt("T_State")
  198. T_detail := c.GetString("T_detail")
  199. T_approver := c.GetString("T_approver")
  200. T_dept := c.GetString("T_dept")
  201. T_product_name := c.GetString("T_product_name")
  202. T_total_price, _ := c.GetFloat("T_total_price")
  203. T_product_quantity, _ := c.GetInt("T_product_quantity")
  204. o := orm.NewOrm()
  205. PurchaseDao := Purchase.NewPurchase(o)
  206. purchase, err := PurchaseDao.Read_Purchase_ById(T_id)
  207. if err != nil {
  208. //o.Rollback()
  209. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
  210. c.ServeJSON()
  211. return
  212. }
  213. clos := []string{}
  214. if len(T_uuid) > 0 {
  215. purchase.T_uuid = T_uuid
  216. clos = append(clos, "T_uuid")
  217. }
  218. if len(T_date) > 0 {
  219. purchase.T_date = T_date
  220. clos = append(clos, "T_date")
  221. }
  222. if len(T_dept) > 0 {
  223. purchase.T_dept = T_dept
  224. clos = append(clos, "T_dept")
  225. }
  226. if len(T_remark) > 0 {
  227. purchase.T_remark = T_remark
  228. clos = append(clos, "T_remark")
  229. }
  230. if len(T_detail) > 0 {
  231. purchase.T_detail = T_detail
  232. clos = append(clos, "T_detail")
  233. }
  234. if len(T_approver) > 0 {
  235. purchase.T_approver = T_approver
  236. clos = append(clos, "T_approver")
  237. }
  238. if T_State > 0 {
  239. if T_State == Purchase.NotPass {
  240. T_State = Purchase.WaitAudit
  241. }
  242. purchase.T_State = T_State
  243. clos = append(clos, "T_State")
  244. }
  245. if len(T_product_name) > 0 {
  246. purchase.T_product_name = T_product_name
  247. clos = append(clos, "T_product_name")
  248. }
  249. if T_total_price > 0 {
  250. purchase.T_total_price = float32(T_total_price)
  251. clos = append(clos, "T_total_price")
  252. }
  253. if T_product_quantity > 0 {
  254. purchase.T_product_quantity = T_product_quantity
  255. clos = append(clos, "T_product_quantity")
  256. }
  257. _, err = PurchaseDao.Update_Purchase(purchase, clos...)
  258. if err != nil {
  259. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改失败"}
  260. c.ServeJSON()
  261. return
  262. }
  263. NatsServer.AddUserLogs(c.User.T_uuid, "采购", "修改", purchase)
  264. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: purchase.Id}
  265. c.ServeJSON()
  266. return
  267. }
  268. func (c *PurchaseController) Purchase_Del() {
  269. T_id, _ := c.GetInt("T_id")
  270. o := orm.NewOrm()
  271. PurchaseDao := Purchase.NewPurchase(o)
  272. purchase, err := PurchaseDao.Read_Purchase_ById(T_id)
  273. if err != nil {
  274. //o.Rollback()
  275. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
  276. c.ServeJSON()
  277. return
  278. }
  279. if purchase.T_State == 2 {
  280. c.Data["json"] = lib.JSONS{Code: 202, Msg: "已采购,禁止删除!"}
  281. c.ServeJSON()
  282. return
  283. }
  284. _, err = PurchaseDao.Delete_Purchase(purchase)
  285. if err != nil {
  286. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  287. c.ServeJSON()
  288. return
  289. }
  290. NatsServer.AddUserLogs(c.User.T_uuid, "采购", "删除", purchase.Id)
  291. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: purchase.Id}
  292. c.ServeJSON()
  293. return
  294. }
  295. // 下载采购申请
  296. func (c *PurchaseController) Purchase_Excel() {
  297. T_id, _ := c.GetInt("T_id")
  298. o := orm.NewOrm()
  299. PurchaseDao := Purchase.NewPurchase(o)
  300. purchase, err := PurchaseDao.Read_Purchase_ById(T_id)
  301. if err != nil {
  302. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  303. c.ServeJSON()
  304. return
  305. }
  306. userList, _ := NatsServer.Read_User_List_All()
  307. Account.Read_User_All_Map(userList)
  308. detail := Purchase.PurchaseToPurchase_Detail(purchase)
  309. f := excelize.NewFile() //设置单元格值
  310. // 这里设置表头ÒÒ
  311. Style1, _ := f.NewStyle(
  312. &excelize.Style{
  313. Font: &excelize.Font{Size: 16, Family: "宋体"},
  314. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  315. Border: []excelize.Border{
  316. {Type: "left", Color: "000000", Style: 1},
  317. {Type: "top", Color: "000000", Style: 1},
  318. {Type: "bottom", Color: "000000", Style: 1},
  319. {Type: "right", Color: "000000", Style: 1},
  320. },
  321. })
  322. Style2, _ := f.NewStyle(
  323. &excelize.Style{
  324. Font: &excelize.Font{Size: 15, Family: "宋体"},
  325. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center", WrapText: true},
  326. Border: []excelize.Border{
  327. {Type: "left", Color: "000000", Style: 1},
  328. {Type: "top", Color: "000000", Style: 1},
  329. {Type: "bottom", Color: "000000", Style: 1},
  330. {Type: "right", Color: "000000", Style: 1},
  331. },
  332. })
  333. Style3, _ := f.NewStyle(
  334. &excelize.Style{
  335. Font: &excelize.Font{Size: 13, Family: "宋体"},
  336. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  337. Border: []excelize.Border{
  338. {Type: "left", Color: "000000", Style: 1},
  339. {Type: "top", Color: "000000", Style: 1},
  340. {Type: "bottom", Color: "000000", Style: 1},
  341. {Type: "right", Color: "000000", Style: 1},
  342. },
  343. })
  344. Style4, _ := f.NewStyle(
  345. &excelize.Style{
  346. Font: &excelize.Font{Size: 13, Family: "宋体"},
  347. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  348. Border: []excelize.Border{
  349. {Type: "left", Color: "000000", Style: 1},
  350. {Type: "top", Color: "000000", Style: 1},
  351. {Type: "bottom", Color: "000000", Style: 1},
  352. {Type: "right", Color: "000000", Style: 1},
  353. },
  354. })
  355. // 定义列配置(可随时增加、删除或调整字段位置)
  356. type ExcelColumn struct {
  357. Header string // 表头名称
  358. Width float64 // 列宽
  359. DataFunc func(detail Purchase.PurchaseDetail_R, index int) interface{}
  360. }
  361. columns := []ExcelColumn{
  362. {"序号", 6, func(detail Purchase.PurchaseDetail_R, index int) interface{} { return index + 1 }},
  363. {"名称", 18, func(detail Purchase.PurchaseDetail_R, index int) interface{} { return detail.T_name }},
  364. {"位号", 15, func(detail Purchase.PurchaseDetail_R, index int) interface{} { return detail.T_bit_number }},
  365. {"封装", 15, func(detail Purchase.PurchaseDetail_R, index int) interface{} { return detail.T_packaging }},
  366. {"型号", 15, func(detail Purchase.PurchaseDetail_R, index int) interface{} { return detail.T_model }},
  367. {"规格", 20, func(detail Purchase.PurchaseDetail_R, index int) interface{} { return detail.T_spec }},
  368. {"数量", 6, func(detail Purchase.PurchaseDetail_R, index int) interface{} { return detail.T_quantity }},
  369. {"需求", 12, func(detail Purchase.PurchaseDetail_R, index int) interface{} { return detail.T_demand }},
  370. {"采购单价", 10, func(detail Purchase.PurchaseDetail_R, index int) interface{} { return detail.T_unit_price }},
  371. {"采购金额", 15, func(detail Purchase.PurchaseDetail_R, index int) interface{} {
  372. return fmt.Sprintf("%.2f", detail.T_unit_price*float32(detail.T_quantity))
  373. }},
  374. {"参考网址", 30, func(detail Purchase.PurchaseDetail_R, index int) interface{} { return detail.T_reference_site }},
  375. {"备注", 20, func(detail Purchase.PurchaseDetail_R, index int) interface{} { return detail.T_remark }},
  376. }
  377. // 计算最后一列的列名
  378. lastCol := string(rune('A' + len(columns) - 1))
  379. // 计算中间列(用于分隔左右两部分信息)
  380. midCol := string(rune('A' + len(columns)/2))
  381. // 设置头部信息
  382. f.MergeCell("Sheet1", "A1", lastCol+"1")
  383. f.SetCellValue("Sheet1", "A1", fmt.Sprintf("%d宝智达冷链采购申请", time.Now().Year()))
  384. f.SetCellStyle("Sheet1", "A1", lastCol+"1", Style1)
  385. f.MergeCell("Sheet1", "A2", midCol+"2")
  386. date, _ := lib.DateStrToTime(detail.T_date)
  387. f.SetCellValue("Sheet1", "A2", fmt.Sprintf("申请时间:%s", date.Format("2006年01月02日")))
  388. f.MergeCell("Sheet1", string(rune('A'+len(columns)/2+1))+"2", lastCol+"2")
  389. f.SetCellValue("Sheet1", string(rune('A'+len(columns)/2+1))+"2", fmt.Sprintf("申请人:%-10s负责人审批:", detail.T_uuid_name))
  390. f.MergeCell("Sheet1", "A3", midCol+"3")
  391. f.SetCellValue("Sheet1", "A3", "执行时间:")
  392. f.MergeCell("Sheet1", string(rune('A'+len(columns)/2+1))+"3", lastCol+"3")
  393. f.SetCellValue("Sheet1", string(rune('A'+len(columns)/2+1))+"3", fmt.Sprintf("执行部门:%-15s执行人:", ""))
  394. f.MergeCell("Sheet1", "A4", midCol+"4")
  395. f.SetCellValue("Sheet1", "A4", fmt.Sprintf("审批人:%s", detail.T_approver_name))
  396. f.MergeCell("Sheet1", string(rune('A'+len(columns)/2+1))+"4", lastCol+"4")
  397. approver_date := ""
  398. if len(detail.T_approver_date) > 0 {
  399. date2, _ := lib.DateStrToTime(detail.T_approver_date)
  400. approver_date = date2.Format("2006年01月02日")
  401. }
  402. f.SetCellValue("Sheet1", string(rune('A'+len(columns)/2+1))+"4", fmt.Sprintf("审批时间:%s", approver_date))
  403. f.SetCellStyle("Sheet1", "A2", lastCol+"4", Style2)
  404. f.SetRowHeight("Sheet1", 1, 40)
  405. height := 30.0
  406. f.SetRowHeight("Sheet1", 2, height)
  407. f.SetRowHeight("Sheet1", 3, height)
  408. f.SetRowHeight("Sheet1", 4, height)
  409. f.SetRowHeight("Sheet1", 5, height)
  410. // 设置"申请采购明细"标题行
  411. f.MergeCell("Sheet1", "A5", lastCol+"5")
  412. f.SetCellValue("Sheet1", "A5", "申请采购明细")
  413. f.SetCellStyle("Sheet1", "A5", lastCol+"5", Style1)
  414. // 设置表头样式
  415. f.SetCellStyle("Sheet1", "A6", lastCol+"6", Style3)
  416. f.SetRowHeight("Sheet1", 2, 25)
  417. // 循环设置表头和列宽
  418. for i, col := range columns {
  419. colName := string(rune('A' + i))
  420. f.SetCellValue("Sheet1", colName+"6", col.Header)
  421. f.SetColWidth("Sheet1", colName, colName, col.Width)
  422. }
  423. // 填充数据
  424. line := 6
  425. // 用于记录需要设置超链接的单元格
  426. type hyperlinkCell struct {
  427. cellRef string
  428. url string
  429. displayText string
  430. }
  431. var hyperlinkCells []hyperlinkCell
  432. for i, v := range detail.T_Detail {
  433. line++
  434. // 循环填充每列数据
  435. for j, col := range columns {
  436. colName := string(rune('A' + j))
  437. cellRef := fmt.Sprintf("%s%d", colName, line)
  438. // 特殊处理"参考网址"列
  439. if col.Header == "参考网址" {
  440. // 确定显示的文本
  441. displayText := v.T_reference_site_title
  442. if len(displayText) == 0 {
  443. displayText = "参考网址"
  444. }
  445. // 设置单元格值
  446. f.SetCellValue("Sheet1", cellRef, displayText)
  447. // 如果有网址,记录下来,稍后统一设置超链接
  448. if len(v.T_reference_site) > 0 {
  449. hyperlinkCells = append(hyperlinkCells, hyperlinkCell{
  450. cellRef: cellRef,
  451. url: v.T_reference_site,
  452. displayText: displayText,
  453. })
  454. }
  455. } else {
  456. f.SetCellValue("Sheet1", cellRef, col.DataFunc(v, i))
  457. }
  458. }
  459. }
  460. // 合计行
  461. line++
  462. // 合并"序号"到"备注"列(最后一列之前的所有列)
  463. f.MergeCell("Sheet1", fmt.Sprintf("A%d", line), fmt.Sprintf("F%d", line))
  464. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), "合计")
  465. // 在最后一列设置求和公式
  466. f.SetCellFormula("Sheet1", fmt.Sprintf("%s%d", "J", line), fmt.Sprintf("SUM(%s7:%s%d)", "J", "J", line-1))
  467. // 设置数据区域样式
  468. f.SetCellStyle("Sheet1", "A6", fmt.Sprintf("%s%d", lastCol, line), Style4)
  469. // 在统一样式之后,设置超链接和超链接样式
  470. linkStyle, _ := f.NewStyle(&excelize.Style{
  471. Font: &excelize.Font{
  472. Color: "0563C1",
  473. Underline: "single",
  474. Size: 13,
  475. Family: "宋体",
  476. },
  477. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  478. Border: []excelize.Border{
  479. {Type: "left", Color: "000000", Style: 1},
  480. {Type: "top", Color: "000000", Style: 1},
  481. {Type: "bottom", Color: "000000", Style: 1},
  482. {Type: "right", Color: "000000", Style: 1},
  483. },
  484. })
  485. for _, cell := range hyperlinkCells {
  486. f.SetCellHyperLink("Sheet1", cell.cellRef, cell.url, "External")
  487. f.SetCellStyle("Sheet1", cell.cellRef, cell.cellRef, linkStyle)
  488. }
  489. timeStr := time.Now().Format("20060102150405")
  490. fileName := fmt.Sprintf("%d宝智达冷链采购申请%v.xlsx", time.Now().Year(), timeStr)
  491. filePath := "ofile/" + fileName
  492. // 保存文件
  493. if err = f.SaveAs(filePath); err != nil {
  494. logs.Error("保存文件失败:", err)
  495. }
  496. defer func() {
  497. os.Remove(filePath)
  498. }()
  499. c.Ctx.Output.Header("Content-Type", "application/vnd.ms-excel;charset=utf8")
  500. c.Ctx.Output.Header("Content-Disposition", "attachment; filename="+url.PathEscape(fileName))
  501. c.Ctx.Output.Header("Content-Transfer-Encoding", "binary")
  502. c.Ctx.Output.Download(filePath, fileName)
  503. }
  504. // 下载采购统计
  505. func (c *PurchaseController) Purchase_Stat_Excel() {
  506. T_uuid := c.GetString("T_uuid")
  507. T_dept := c.GetString("T_dept")
  508. T_start_date := c.GetString("T_start_date") // 开始时间
  509. T_end_date := c.GetString("T_end_date") // 结束时间
  510. T_state, _ := c.GetInt("T_state")
  511. userList, _ := NatsServer.Read_User_List_All()
  512. Account.Read_User_All_Map(userList)
  513. PurchaseDao := Purchase.NewPurchase(orm.NewOrm())
  514. R_List, _ := PurchaseDao.Read_Purchase_Stat(T_dept, T_uuid, "", T_state, true, T_start_date, T_end_date, 0, 9999)
  515. f := excelize.NewFile() //设置单元格值
  516. // 这里设置表头ÒÒ
  517. Style1, _ := f.NewStyle(
  518. &excelize.Style{
  519. Font: &excelize.Font{Size: 20, Family: "宋体"},
  520. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  521. })
  522. Style2, _ := f.NewStyle(
  523. &excelize.Style{
  524. Font: &excelize.Font{Size: 15, Family: "宋体"},
  525. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  526. Border: []excelize.Border{
  527. {Type: "left", Color: "000000", Style: 1},
  528. {Type: "top", Color: "000000", Style: 1},
  529. {Type: "bottom", Color: "000000", Style: 1},
  530. {Type: "right", Color: "000000", Style: 1},
  531. },
  532. Fill: excelize.Fill{Type: "pattern", Color: []string{"D9D9D9"}, Pattern: 1},
  533. })
  534. Style3, _ := f.NewStyle(
  535. &excelize.Style{
  536. Font: &excelize.Font{Size: 15, Family: "宋体"},
  537. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  538. Border: []excelize.Border{
  539. {Type: "left", Color: "000000", Style: 1},
  540. {Type: "top", Color: "000000", Style: 1},
  541. {Type: "bottom", Color: "000000", Style: 1},
  542. {Type: "right", Color: "000000", Style: 1},
  543. },
  544. })
  545. f.MergeCell("Sheet1", "A1", "M1")
  546. f.SetCellValue("Sheet1", "A1", "资金一览表")
  547. f.SetCellStyle("Sheet1", "A1", "M1", Style1)
  548. // 写入表头
  549. headers := []string{"序号", "采购物品", "规格", "单价", "数量", "实际金额", "合计金额", "用途", "采购申请人", "采购申请时间", "是否已有发票", "报销上报时间", "备注"}
  550. for col, header := range headers {
  551. cell := fmt.Sprintf("%c%d", rune('A'+col), 2)
  552. f.SetCellValue("Sheet1", cell, header)
  553. }
  554. // 这里设置表头
  555. f.SetCellStyle("Sheet1", "A2", "M2", Style2)
  556. f.SetRowHeight("Sheet1", 2, 25)
  557. colsWidth := []float64{6, 12, 10, 10, 6, 30, 12, 15, 15, 15, 15, 15, 15}
  558. for col, width := range colsWidth {
  559. cell := fmt.Sprintf("%c", rune('A'+col))
  560. f.SetColWidth("Sheet1", cell, cell, width)
  561. }
  562. line := 2
  563. for i, purchase := range R_List {
  564. // 写入数据
  565. startRow := line + 1
  566. for _, v := range purchase.T_Detail {
  567. line++
  568. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), i+1)
  569. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), v.T_name)
  570. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), v.T_model)
  571. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), v.T_unit_price)
  572. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), v.T_quantity)
  573. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), v.T_amount)
  574. if len(purchase.T_Detail) == 1 {
  575. f.SetCellValue("Sheet1", fmt.Sprintf("G%d", line), v.T_amount)
  576. f.SetCellValue("Sheet1", fmt.Sprintf("I%d", line), purchase.T_uuid_name)
  577. f.SetCellValue("Sheet1", fmt.Sprintf("J%d", line), purchase.T_date)
  578. f.SetCellValue("Sheet1", fmt.Sprintf("M%d", line), purchase.T_remark)
  579. }
  580. }
  581. if len(purchase.T_Detail) > 1 {
  582. f.MergeCell("Sheet1", fmt.Sprintf("G%d", startRow), fmt.Sprintf("G%d", line))
  583. f.SetCellFormula("Sheet1", fmt.Sprintf("G%d", startRow), fmt.Sprintf("SUM(F%d:F%d)", startRow, line))
  584. f.MergeCell("Sheet1", fmt.Sprintf("I%d", startRow), fmt.Sprintf("I%d", line))
  585. f.SetCellValue("Sheet1", fmt.Sprintf("I%d", startRow), purchase.T_uuid_name)
  586. f.MergeCell("Sheet1", fmt.Sprintf("J%d", startRow), fmt.Sprintf("J%d", line))
  587. f.SetCellValue("Sheet1", fmt.Sprintf("J%d", startRow), purchase.T_date)
  588. f.MergeCell("Sheet1", fmt.Sprintf("M%d", startRow), fmt.Sprintf("M%d", line))
  589. f.SetCellValue("Sheet1", fmt.Sprintf("M%d", startRow), purchase.T_remark)
  590. }
  591. }
  592. f.SetCellStyle("Sheet1", "A3", fmt.Sprintf("M%d", line), Style3)
  593. timeStr := time.Now().Format("20060102150405")
  594. fileName := fmt.Sprintf("%d资金一览表%v.xlsx", time.Now().Year(), timeStr)
  595. filePath := "ofile/" + fileName
  596. // 保存文件
  597. if err := f.SaveAs(filePath); err != nil {
  598. logs.Error("保存文件失败:", err)
  599. }
  600. defer func() {
  601. os.Remove(filePath)
  602. }()
  603. c.Ctx.Output.Header("Content-Type", "application/vnd.ms-excel;charset=utf8")
  604. c.Ctx.Output.Header("Content-Disposition", "attachment; filename="+url.PathEscape(fileName))
  605. c.Ctx.Output.Header("Content-Transfer-Encoding", "binary")
  606. c.Ctx.Output.Download(filePath, fileName)
  607. }
  608. func (c *PurchaseController) User_List() {
  609. var r_jsons lib.R_JSONS
  610. // 分页参数 初始化
  611. page, _ := c.GetInt("page")
  612. if page < 1 {
  613. page = 1
  614. }
  615. page_z, _ := c.GetInt("page_z")
  616. if page_z < 1 {
  617. page_z = conf.Page_size
  618. }
  619. T_name := c.GetString("T_name")
  620. PurchaseDao := Purchase.NewPurchase(orm.NewOrm())
  621. uuidList := PurchaseDao.Read_T_uuid_List()
  622. if len(uuidList) == 0 {
  623. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  624. c.ServeJSON()
  625. return
  626. }
  627. R_List, R_cnt, err := NatsServer.Read_User_List_T_uuid(T_name, uuidList, page, page_z)
  628. if err != nil {
  629. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败"}
  630. c.ServeJSON()
  631. return
  632. }
  633. var U_List []userlibs.User
  634. for _, user := range R_List {
  635. U_List = append(U_List, user)
  636. }
  637. r_jsons.Num = R_cnt
  638. r_jsons.Data = U_List
  639. r_jsons.Page = page
  640. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  641. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  642. c.ServeJSON()
  643. return
  644. }