| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754 |
- package controllers
- import (
- "ERP_storage/Nats/NatsServer"
- "ERP_storage/conf"
- "ERP_storage/logs"
- "ERP_storage/models/Account"
- "ERP_storage/models/Purchase"
- "fmt"
- "math"
- "net/url"
- "os"
- "time"
- "github.com/beego/beego/v2/adapter/orm"
- beego "github.com/beego/beego/v2/server/web"
- "github.com/xuri/excelize/v2"
- userlibs "gogs.baozhida.cn/zoie/ERP_libs/User"
- "gogs.baozhida.cn/zoie/ERP_libs/lib"
- )
- type PurchaseController struct {
- beego.Controller
- User userlibs.User
- }
- func (c *PurchaseController) Prepare() {
- c.User = *Account.User_r
- }
- // 管理员
- func (c *PurchaseController) Purchase_List() {
- // 分页参数 初始化
- page, _ := c.GetInt("page")
- if page < 1 {
- page = 1
- }
- page_z, _ := c.GetInt("page_z")
- if page_z < 1 {
- page_z = conf.Page_size
- }
- T_uuid := c.GetString("T_uuid")
- T_dept := c.GetString("T_dept")
- T_start_date := c.GetString("T_start_date") // 开始时间
- T_end_date := c.GetString("T_end_date") // 结束时间
- T_state, _ := c.GetInt("T_state")
- userList, _ := NatsServer.Read_User_List_All()
- Account.Read_User_All_Map(userList)
- PurchaseDao := Purchase.NewPurchase(orm.NewOrm())
- R_List, R_cnt := PurchaseDao.Read_Purchase_List(T_dept, T_uuid, "", T_state, true, T_start_date, T_end_date, page, page_z)
- var r_jsons lib.R_JSONS
- r_jsons.Num = R_cnt
- r_jsons.Data = R_List
- r_jsons.Page = page
- r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
- c.ServeJSON()
- return
- }
- func (c *PurchaseController) Purchase_Audit_List() {
- // 分页参数 初始化
- page, _ := c.GetInt("page")
- if page < 1 {
- page = 1
- }
- page_z, _ := c.GetInt("page_z")
- if page_z < 1 {
- page_z = conf.Page_size
- }
- userList, _ := NatsServer.Read_User_List_All()
- Account.Read_User_All_Map(userList)
- PurchaseDao := Purchase.NewPurchase(orm.NewOrm())
- R_List, R_cnt := PurchaseDao.Read_Purchase_List("", "", c.User.T_uuid, Purchase.WaitAudit, false, "", "", page, page_z)
- var r_jsons lib.R_JSONS
- r_jsons.Num = R_cnt
- r_jsons.Data = R_List
- r_jsons.Page = page
- r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
- c.ServeJSON()
- return
- }
- // 用户采购列表
- func (c *PurchaseController) Purchase_User_List() {
- // 分页参数 初始化
- page, _ := c.GetInt("page")
- if page < 1 {
- page = 1
- }
- page_z, _ := c.GetInt("page_z")
- if page_z < 1 {
- page_z = conf.Page_size
- }
- T_start_date := c.GetString("T_start_date") // 开始时间
- T_end_date := c.GetString("T_end_date") // 结束时间
- T_state, _ := c.GetInt("T_state")
- userList, _ := NatsServer.Read_User_List_All()
- Account.Read_User_All_Map(userList)
- PurchaseDao := Purchase.NewPurchase(orm.NewOrm())
- R_List, R_cnt := PurchaseDao.Read_Purchase_List("", c.User.T_uuid, "", T_state, false, T_start_date, T_end_date, page, page_z)
- var r_jsons lib.R_JSONS
- r_jsons.Num = R_cnt
- r_jsons.Data = R_List
- r_jsons.Page = page
- r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
- c.ServeJSON()
- return
- }
- func (c *PurchaseController) Purchase_Get() {
- // 查询
- T_id, _ := c.GetInt("T_id")
- o := orm.NewOrm()
- PurchaseDao := Purchase.NewPurchase(o)
- purchase, err := PurchaseDao.Read_Purchase_ById(T_id)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
- c.ServeJSON()
- return
- }
- userList, _ := NatsServer.Read_User_List_All()
- Account.Read_User_All_Map(userList)
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Purchase.PurchaseToPurchase_Detail(purchase)}
- c.ServeJSON()
- return
- }
- func (c *PurchaseController) Purchase_Add() {
- T_uuid := c.GetString("T_uuid")
- T_date := c.GetString("T_date")
- T_remark := c.GetString("T_remark")
- T_detail := c.GetString("T_detail")
- T_approver := c.GetString("T_approver")
- T_dept := c.GetString("T_dept")
- T_product_name := c.GetString("T_product_name")
- T_total_price, _ := c.GetFloat("T_total_price")
- T_product_quantity, _ := c.GetInt("T_product_quantity")
- var_ := Purchase.Purchase{
- T_dept: T_dept,
- T_uuid: T_uuid,
- T_submit: c.User.T_uuid,
- T_date: T_date,
- T_remark: T_remark,
- T_detail: T_detail,
- T_approver: T_approver,
- T_State: Purchase.WaitAudit,
- T_product_name: T_product_name,
- T_total_price: float32(T_total_price),
- T_product_quantity: T_product_quantity,
- }
- o := orm.NewOrm()
- PurchaseDao := Purchase.NewPurchase(o)
- id, err := PurchaseDao.Add_Purchase(var_)
- if err != nil {
- //o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 203, Msg: "添加失败"}
- c.ServeJSON()
- return
- }
- NatsServer.AddNews(T_approver, fmt.Sprintf("【采购申请】您有一条%s的采购申请待审批", c.User.T_name), conf.PurchaseAuditUrl)
- NatsServer.AddUserLogs(c.User.T_uuid, "采购", "添加", var_)
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: id}
- c.ServeJSON()
- return
- }
- func (c *PurchaseController) Purchase_Approval() {
- T_id, _ := c.GetInt("T_id")
- T_State, _ := c.GetInt("T_State")
- o := orm.NewOrm()
- PurchaseDao := Purchase.NewPurchase(o)
- purchase, err := PurchaseDao.Read_Purchase_ById(T_id)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
- c.ServeJSON()
- return
- }
- if purchase.T_State == T_State {
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_id}
- c.ServeJSON()
- return
- }
- // 1 待采购 2 已采购
- purchase.T_State = T_State
- _, err = PurchaseDao.Update_Purchase(purchase, "T_State")
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 203, Msg: "添加失败"}
- c.ServeJSON()
- return
- }
- if T_State == Purchase.PurchaseCompleted {
- NatsServer.AddNews(purchase.T_uuid, "【采购审核】您提交的采购申请已采购", conf.MyPurchaseNewsUrl)
- }
- NatsServer.AddUserLogs(c.User.T_uuid, "采购申请", "采购", purchase)
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_id}
- c.ServeJSON()
- return
- }
- func (c *PurchaseController) Purchase_Edit() {
- T_id, _ := c.GetInt("T_id")
- T_uuid := c.GetString("T_uuid")
- T_date := c.GetString("T_date")
- T_remark := c.GetString("T_remark")
- T_State, _ := c.GetInt("T_State")
- T_detail := c.GetString("T_detail")
- T_approver := c.GetString("T_approver")
- T_dept := c.GetString("T_dept")
- T_product_name := c.GetString("T_product_name")
- T_total_price, _ := c.GetFloat("T_total_price")
- T_product_quantity, _ := c.GetInt("T_product_quantity")
- o := orm.NewOrm()
- PurchaseDao := Purchase.NewPurchase(o)
- purchase, err := PurchaseDao.Read_Purchase_ById(T_id)
- if err != nil {
- //o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
- c.ServeJSON()
- return
- }
- clos := []string{}
- if len(T_uuid) > 0 {
- purchase.T_uuid = T_uuid
- clos = append(clos, "T_uuid")
- }
- if len(T_date) > 0 {
- purchase.T_date = T_date
- clos = append(clos, "T_date")
- }
- if len(T_dept) > 0 {
- purchase.T_dept = T_dept
- clos = append(clos, "T_dept")
- }
- if len(T_remark) > 0 {
- purchase.T_remark = T_remark
- clos = append(clos, "T_remark")
- }
- if len(T_detail) > 0 {
- purchase.T_detail = T_detail
- clos = append(clos, "T_detail")
- }
- if len(T_approver) > 0 {
- purchase.T_approver = T_approver
- clos = append(clos, "T_approver")
- }
- if T_State > 0 {
- if T_State == Purchase.NotPass {
- T_State = Purchase.WaitAudit
- }
- purchase.T_State = T_State
- clos = append(clos, "T_State")
- }
- if len(T_product_name) > 0 {
- purchase.T_product_name = T_product_name
- clos = append(clos, "T_product_name")
- }
- if T_total_price > 0 {
- purchase.T_total_price = float32(T_total_price)
- clos = append(clos, "T_total_price")
- }
- if T_product_quantity > 0 {
- purchase.T_product_quantity = T_product_quantity
- clos = append(clos, "T_product_quantity")
- }
- _, err = PurchaseDao.Update_Purchase(purchase, clos...)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改失败"}
- c.ServeJSON()
- return
- }
- NatsServer.AddUserLogs(c.User.T_uuid, "采购", "修改", purchase)
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: purchase.Id}
- c.ServeJSON()
- return
- }
- func (c *PurchaseController) Purchase_Del() {
- T_id, _ := c.GetInt("T_id")
- o := orm.NewOrm()
- PurchaseDao := Purchase.NewPurchase(o)
- purchase, err := PurchaseDao.Read_Purchase_ById(T_id)
- if err != nil {
- //o.Rollback()
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
- c.ServeJSON()
- return
- }
- if purchase.T_State == 2 {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "已采购,禁止删除!"}
- c.ServeJSON()
- return
- }
- _, err = PurchaseDao.Delete_Purchase(purchase)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
- c.ServeJSON()
- return
- }
- NatsServer.AddUserLogs(c.User.T_uuid, "采购", "删除", purchase.Id)
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: purchase.Id}
- c.ServeJSON()
- return
- }
- // 下载采购申请
- func (c *PurchaseController) Purchase_Excel() {
- T_id, _ := c.GetInt("T_id")
- o := orm.NewOrm()
- PurchaseDao := Purchase.NewPurchase(o)
- purchase, err := PurchaseDao.Read_Purchase_ById(T_id)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
- c.ServeJSON()
- return
- }
- userList, _ := NatsServer.Read_User_List_All()
- Account.Read_User_All_Map(userList)
- detail := Purchase.PurchaseToPurchase_Detail(purchase)
- f := excelize.NewFile() //设置单元格值
- // 这里设置表头ÒÒ
- Style1, _ := f.NewStyle(
- &excelize.Style{
- Font: &excelize.Font{Size: 16, Family: "宋体"},
- Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
- Border: []excelize.Border{
- {Type: "left", Color: "000000", Style: 1},
- {Type: "top", Color: "000000", Style: 1},
- {Type: "bottom", Color: "000000", Style: 1},
- {Type: "right", Color: "000000", Style: 1},
- },
- })
- Style2, _ := f.NewStyle(
- &excelize.Style{
- Font: &excelize.Font{Size: 15, Family: "宋体"},
- Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center", WrapText: true},
- Border: []excelize.Border{
- {Type: "left", Color: "000000", Style: 1},
- {Type: "top", Color: "000000", Style: 1},
- {Type: "bottom", Color: "000000", Style: 1},
- {Type: "right", Color: "000000", Style: 1},
- },
- })
- Style3, _ := f.NewStyle(
- &excelize.Style{
- Font: &excelize.Font{Size: 13, Family: "宋体"},
- Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
- Border: []excelize.Border{
- {Type: "left", Color: "000000", Style: 1},
- {Type: "top", Color: "000000", Style: 1},
- {Type: "bottom", Color: "000000", Style: 1},
- {Type: "right", Color: "000000", Style: 1},
- },
- })
- Style4, _ := f.NewStyle(
- &excelize.Style{
- Font: &excelize.Font{Size: 13, Family: "宋体"},
- Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
- Border: []excelize.Border{
- {Type: "left", Color: "000000", Style: 1},
- {Type: "top", Color: "000000", Style: 1},
- {Type: "bottom", Color: "000000", Style: 1},
- {Type: "right", Color: "000000", Style: 1},
- },
- })
- // 定义列配置(可随时增加、删除或调整字段位置)
- type ExcelColumn struct {
- Header string // 表头名称
- Width float64 // 列宽
- DataFunc func(detail Purchase.PurchaseDetail_R, index int) interface{}
- }
- columns := []ExcelColumn{
- {"序号", 6, func(detail Purchase.PurchaseDetail_R, index int) interface{} { return index + 1 }},
- {"名称", 18, func(detail Purchase.PurchaseDetail_R, index int) interface{} { return detail.T_name }},
- {"位号", 15, func(detail Purchase.PurchaseDetail_R, index int) interface{} { return detail.T_bit_number }},
- {"封装", 15, func(detail Purchase.PurchaseDetail_R, index int) interface{} { return detail.T_packaging }},
- {"型号", 15, func(detail Purchase.PurchaseDetail_R, index int) interface{} { return detail.T_model }},
- {"规格", 20, func(detail Purchase.PurchaseDetail_R, index int) interface{} { return detail.T_spec }},
- {"数量", 6, func(detail Purchase.PurchaseDetail_R, index int) interface{} { return detail.T_quantity }},
- {"需求", 12, func(detail Purchase.PurchaseDetail_R, index int) interface{} { return detail.T_demand }},
- {"采购单价", 10, func(detail Purchase.PurchaseDetail_R, index int) interface{} { return detail.T_unit_price }},
- {"采购金额", 15, func(detail Purchase.PurchaseDetail_R, index int) interface{} {
- return fmt.Sprintf("%.2f", detail.T_unit_price*float32(detail.T_quantity))
- }},
- {"参考网址", 30, func(detail Purchase.PurchaseDetail_R, index int) interface{} { return detail.T_reference_site }},
- {"备注", 20, func(detail Purchase.PurchaseDetail_R, index int) interface{} { return detail.T_remark }},
- }
- // 计算最后一列的列名
- lastCol := string(rune('A' + len(columns) - 1))
- // 计算中间列(用于分隔左右两部分信息)
- midCol := string(rune('A' + len(columns)/2))
- // 设置头部信息
- f.MergeCell("Sheet1", "A1", lastCol+"1")
- f.SetCellValue("Sheet1", "A1", fmt.Sprintf("%d宝智达冷链采购申请", time.Now().Year()))
- f.SetCellStyle("Sheet1", "A1", lastCol+"1", Style1)
- f.MergeCell("Sheet1", "A2", midCol+"2")
- date, _ := lib.DateStrToTime(detail.T_date)
- f.SetCellValue("Sheet1", "A2", fmt.Sprintf("申请时间:%s", date.Format("2006年01月02日")))
- f.MergeCell("Sheet1", string(rune('A'+len(columns)/2+1))+"2", lastCol+"2")
- f.SetCellValue("Sheet1", string(rune('A'+len(columns)/2+1))+"2", fmt.Sprintf("申请人:%-10s负责人审批:", detail.T_uuid_name))
- f.MergeCell("Sheet1", "A3", midCol+"3")
- f.SetCellValue("Sheet1", "A3", "执行时间:")
- f.MergeCell("Sheet1", string(rune('A'+len(columns)/2+1))+"3", lastCol+"3")
- f.SetCellValue("Sheet1", string(rune('A'+len(columns)/2+1))+"3", fmt.Sprintf("执行部门:%-15s执行人:", ""))
- f.MergeCell("Sheet1", "A4", midCol+"4")
- f.SetCellValue("Sheet1", "A4", fmt.Sprintf("审批人:%s", detail.T_approver_name))
- f.MergeCell("Sheet1", string(rune('A'+len(columns)/2+1))+"4", lastCol+"4")
- approver_date := ""
- if len(detail.T_approver_date) > 0 {
- date2, _ := lib.DateStrToTime(detail.T_approver_date)
- approver_date = date2.Format("2006年01月02日")
- }
- f.SetCellValue("Sheet1", string(rune('A'+len(columns)/2+1))+"4", fmt.Sprintf("审批时间:%s", approver_date))
- f.SetCellStyle("Sheet1", "A2", lastCol+"4", Style2)
- f.SetRowHeight("Sheet1", 1, 40)
- height := 30.0
- f.SetRowHeight("Sheet1", 2, height)
- f.SetRowHeight("Sheet1", 3, height)
- f.SetRowHeight("Sheet1", 4, height)
- f.SetRowHeight("Sheet1", 5, height)
- // 设置"申请采购明细"标题行
- f.MergeCell("Sheet1", "A5", lastCol+"5")
- f.SetCellValue("Sheet1", "A5", "申请采购明细")
- f.SetCellStyle("Sheet1", "A5", lastCol+"5", Style1)
- // 设置表头样式
- f.SetCellStyle("Sheet1", "A6", lastCol+"6", Style3)
- f.SetRowHeight("Sheet1", 2, 25)
- // 循环设置表头和列宽
- for i, col := range columns {
- colName := string(rune('A' + i))
- f.SetCellValue("Sheet1", colName+"6", col.Header)
- f.SetColWidth("Sheet1", colName, colName, col.Width)
- }
- // 填充数据
- line := 6
- // 用于记录需要设置超链接的单元格
- type hyperlinkCell struct {
- cellRef string
- url string
- displayText string
- }
- var hyperlinkCells []hyperlinkCell
- for i, v := range detail.T_Detail {
- line++
- // 循环填充每列数据
- for j, col := range columns {
- colName := string(rune('A' + j))
- cellRef := fmt.Sprintf("%s%d", colName, line)
- // 特殊处理"参考网址"列
- if col.Header == "参考网址" {
- // 确定显示的文本
- displayText := v.T_reference_site_title
- if len(displayText) == 0 {
- displayText = "参考网址"
- }
- // 设置单元格值
- f.SetCellValue("Sheet1", cellRef, displayText)
- // 如果有网址,记录下来,稍后统一设置超链接
- if len(v.T_reference_site) > 0 {
- hyperlinkCells = append(hyperlinkCells, hyperlinkCell{
- cellRef: cellRef,
- url: v.T_reference_site,
- displayText: displayText,
- })
- }
- } else {
- f.SetCellValue("Sheet1", cellRef, col.DataFunc(v, i))
- }
- }
- }
- // 合计行
- line++
- // 合并"序号"到"备注"列(最后一列之前的所有列)
- f.MergeCell("Sheet1", fmt.Sprintf("A%d", line), fmt.Sprintf("F%d", line))
- f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), "合计")
- // 在最后一列设置求和公式
- f.SetCellFormula("Sheet1", fmt.Sprintf("%s%d", "J", line), fmt.Sprintf("SUM(%s7:%s%d)", "J", "J", line-1))
- // 设置数据区域样式
- f.SetCellStyle("Sheet1", "A6", fmt.Sprintf("%s%d", lastCol, line), Style4)
- // 在统一样式之后,设置超链接和超链接样式
- linkStyle, _ := f.NewStyle(&excelize.Style{
- Font: &excelize.Font{
- Color: "0563C1",
- Underline: "single",
- Size: 13,
- Family: "宋体",
- },
- Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
- Border: []excelize.Border{
- {Type: "left", Color: "000000", Style: 1},
- {Type: "top", Color: "000000", Style: 1},
- {Type: "bottom", Color: "000000", Style: 1},
- {Type: "right", Color: "000000", Style: 1},
- },
- })
- for _, cell := range hyperlinkCells {
- f.SetCellHyperLink("Sheet1", cell.cellRef, cell.url, "External")
- f.SetCellStyle("Sheet1", cell.cellRef, cell.cellRef, linkStyle)
- }
- timeStr := time.Now().Format("20060102150405")
- fileName := fmt.Sprintf("%d宝智达冷链采购申请%v.xlsx", time.Now().Year(), timeStr)
- filePath := "ofile/" + fileName
- // 保存文件
- if err = f.SaveAs(filePath); err != nil {
- logs.Error("保存文件失败:", err)
- }
- defer func() {
- os.Remove(filePath)
- }()
- c.Ctx.Output.Header("Content-Type", "application/vnd.ms-excel;charset=utf8")
- c.Ctx.Output.Header("Content-Disposition", "attachment; filename="+url.PathEscape(fileName))
- c.Ctx.Output.Header("Content-Transfer-Encoding", "binary")
- c.Ctx.Output.Download(filePath, fileName)
- }
- // 下载采购统计
- func (c *PurchaseController) Purchase_Stat_Excel() {
- T_uuid := c.GetString("T_uuid")
- T_dept := c.GetString("T_dept")
- T_start_date := c.GetString("T_start_date") // 开始时间
- T_end_date := c.GetString("T_end_date") // 结束时间
- T_state, _ := c.GetInt("T_state")
- userList, _ := NatsServer.Read_User_List_All()
- Account.Read_User_All_Map(userList)
- PurchaseDao := Purchase.NewPurchase(orm.NewOrm())
- R_List, _ := PurchaseDao.Read_Purchase_Stat(T_dept, T_uuid, "", T_state, true, T_start_date, T_end_date, 0, 9999)
- f := excelize.NewFile() //设置单元格值
- // 这里设置表头ÒÒ
- Style1, _ := f.NewStyle(
- &excelize.Style{
- Font: &excelize.Font{Size: 20, Family: "宋体"},
- Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
- })
- Style2, _ := f.NewStyle(
- &excelize.Style{
- Font: &excelize.Font{Size: 15, Family: "宋体"},
- Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
- Border: []excelize.Border{
- {Type: "left", Color: "000000", Style: 1},
- {Type: "top", Color: "000000", Style: 1},
- {Type: "bottom", Color: "000000", Style: 1},
- {Type: "right", Color: "000000", Style: 1},
- },
- Fill: excelize.Fill{Type: "pattern", Color: []string{"D9D9D9"}, Pattern: 1},
- })
- Style3, _ := f.NewStyle(
- &excelize.Style{
- Font: &excelize.Font{Size: 15, Family: "宋体"},
- Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
- Border: []excelize.Border{
- {Type: "left", Color: "000000", Style: 1},
- {Type: "top", Color: "000000", Style: 1},
- {Type: "bottom", Color: "000000", Style: 1},
- {Type: "right", Color: "000000", Style: 1},
- },
- })
- f.MergeCell("Sheet1", "A1", "M1")
- f.SetCellValue("Sheet1", "A1", "资金一览表")
- f.SetCellStyle("Sheet1", "A1", "M1", Style1)
- // 写入表头
- headers := []string{"序号", "采购物品", "规格", "单价", "数量", "实际金额", "合计金额", "用途", "采购申请人", "采购申请时间", "是否已有发票", "报销上报时间", "备注"}
- for col, header := range headers {
- cell := fmt.Sprintf("%c%d", rune('A'+col), 2)
- f.SetCellValue("Sheet1", cell, header)
- }
- // 这里设置表头
- f.SetCellStyle("Sheet1", "A2", "M2", Style2)
- f.SetRowHeight("Sheet1", 2, 25)
- colsWidth := []float64{6, 12, 10, 10, 6, 30, 12, 15, 15, 15, 15, 15, 15}
- for col, width := range colsWidth {
- cell := fmt.Sprintf("%c", rune('A'+col))
- f.SetColWidth("Sheet1", cell, cell, width)
- }
- line := 2
- for i, purchase := range R_List {
- // 写入数据
- startRow := line + 1
- for _, v := range purchase.T_Detail {
- line++
- f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), i+1)
- f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), v.T_name)
- f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), v.T_model)
- f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), v.T_unit_price)
- f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), v.T_quantity)
- f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), v.T_amount)
- if len(purchase.T_Detail) == 1 {
- f.SetCellValue("Sheet1", fmt.Sprintf("G%d", line), v.T_amount)
- f.SetCellValue("Sheet1", fmt.Sprintf("I%d", line), purchase.T_uuid_name)
- f.SetCellValue("Sheet1", fmt.Sprintf("J%d", line), purchase.T_date)
- f.SetCellValue("Sheet1", fmt.Sprintf("M%d", line), purchase.T_remark)
- }
- }
- if len(purchase.T_Detail) > 1 {
- f.MergeCell("Sheet1", fmt.Sprintf("G%d", startRow), fmt.Sprintf("G%d", line))
- f.SetCellFormula("Sheet1", fmt.Sprintf("G%d", startRow), fmt.Sprintf("SUM(F%d:F%d)", startRow, line))
- f.MergeCell("Sheet1", fmt.Sprintf("I%d", startRow), fmt.Sprintf("I%d", line))
- f.SetCellValue("Sheet1", fmt.Sprintf("I%d", startRow), purchase.T_uuid_name)
- f.MergeCell("Sheet1", fmt.Sprintf("J%d", startRow), fmt.Sprintf("J%d", line))
- f.SetCellValue("Sheet1", fmt.Sprintf("J%d", startRow), purchase.T_date)
- f.MergeCell("Sheet1", fmt.Sprintf("M%d", startRow), fmt.Sprintf("M%d", line))
- f.SetCellValue("Sheet1", fmt.Sprintf("M%d", startRow), purchase.T_remark)
- }
- }
- f.SetCellStyle("Sheet1", "A3", fmt.Sprintf("M%d", line), Style3)
- timeStr := time.Now().Format("20060102150405")
- fileName := fmt.Sprintf("%d资金一览表%v.xlsx", time.Now().Year(), timeStr)
- filePath := "ofile/" + fileName
- // 保存文件
- if err := f.SaveAs(filePath); err != nil {
- logs.Error("保存文件失败:", err)
- }
- defer func() {
- os.Remove(filePath)
- }()
- c.Ctx.Output.Header("Content-Type", "application/vnd.ms-excel;charset=utf8")
- c.Ctx.Output.Header("Content-Disposition", "attachment; filename="+url.PathEscape(fileName))
- c.Ctx.Output.Header("Content-Transfer-Encoding", "binary")
- c.Ctx.Output.Download(filePath, fileName)
- }
- func (c *PurchaseController) User_List() {
- var r_jsons lib.R_JSONS
- // 分页参数 初始化
- page, _ := c.GetInt("page")
- if page < 1 {
- page = 1
- }
- page_z, _ := c.GetInt("page_z")
- if page_z < 1 {
- page_z = conf.Page_size
- }
- T_name := c.GetString("T_name")
- PurchaseDao := Purchase.NewPurchase(orm.NewOrm())
- uuidList := PurchaseDao.Read_T_uuid_List()
- if len(uuidList) == 0 {
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
- c.ServeJSON()
- return
- }
- R_List, R_cnt, err := NatsServer.Read_User_List_T_uuid(T_name, uuidList, page, page_z)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败"}
- c.ServeJSON()
- return
- }
- var U_List []userlibs.User
- for _, user := range R_List {
- U_List = append(U_List, user)
- }
- r_jsons.Num = R_cnt
- r_jsons.Data = U_List
- r_jsons.Page = page
- r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
- c.ServeJSON()
- return
- }
|