package service import ( "fmt" "github.com/xuri/excelize/v2" "strconv" "time" "wms/models" "wms/request" "wms/response" ) type LocationFormsService struct{} func NewLocationFormsService() *LocationFormsService { return &LocationFormsService{} } func (slf *LocationFormsService) Query(params request.GetLocationForms) (result []*response.LocationForms, err error) { search, err := slf.BuildSearch(params) search = search.SetPage(params.Page, params.PageSize) amounts, err := search.Find() if err != nil { return } for _, amount := range amounts { resp := new(response.LocationForms) resp.Amount = amount.Amount resp.LocationId = amount.LocationId resp.LocationName = amount.Location.JointName resp.ProduceId = amount.Product.ID resp.ProductName = amount.Product.Name resp.ProductTypeName = amount.ProductCategory.Name resp.Unit = amount.Product.Unit resp.Value = resp.Amount.Mul(amount.Product.Cost) moreUnit := amount.Product.MoreUnit if moreUnit != nil && *moreUnit { resp.AmountMoreUnits = CreateMoreUnit(resp.Amount, amount.Product.MoreUnitList) } result = append(result, resp) } return } func (slf *LocationFormsService) BuildSearch(params request.GetLocationForms) (search *models.LocationProductAmountSearch, err error) { ids := params.LocationIds if params.LocationId != 0 { ids = append(ids, params.LocationId) } if len(ids) == 0 { //查询位置 locations, err := models.NewLocationSearch().SetJointName(params.WareHouseCode).SetType(3).FindAll() if err != nil { return nil, err } for _, location := range locations { ids = append(ids, location.Id) } } search = models.NewLocationProductAmountSearch().SetPreload(true).SetKeyword(params.KeyWord).SetProductId(params.ProductId).SetLocationIds(ids) return search, nil } func (slf *LocationFormsService) Count(params request.GetLocationForms) (total int64, err error) { search, err := slf.BuildSearch(params) if err != nil { return 0, err } return search.Count() } func (slf *LocationFormsService) FetchAll(params request.GetLocationForms) (list []*response.LocationForms, err error) { total, err := slf.Count(params) if err != nil { return nil, err } list = make([]*response.LocationForms, 0, total) params.PageSize = 500 page := 1 for { params.Page = page data, err := slf.Query(params) if err != nil { return nil, err } if len(data) == 0 { break } list = append(list, data...) page++ } return } func (slf *LocationFormsService) Export(dataList []*response.LocationForms) (filename string, err error) { var fileName string f := excelize.NewFile() // 自定义表头 headers := []interface{}{"位置", "产品", "产品类别", map[string][]string{"在库数量": {"件", "匹", "米", "重量"}}, "价值"} // 设置表头 var i int for _, h := range headers { if v, ok := h.(string); ok { f.SetCellValue("Sheet1", getColumnAlphabet(i)+"1", v) i++ } else if childHeaders, ok := h.(map[string][]string); ok { for title, list := range childHeaders { f.SetCellValue("Sheet1", getColumnAlphabet(i)+"1", title) err = f.MergeCell("Sheet1", getColumnAlphabet(i)+"1", getColumnAlphabet(i-1+len(list))+"1") // 合并单元格 for _, t := range list { f.SetCellValue("Sheet1", getColumnAlphabet(i)+"2", t) i++ } } } } // 设置表头样式 style := &excelize.Style{ Border: nil, Fill: excelize.Fill{ Type: "pattern", Pattern: 1, Shading: 0, }, Font: &excelize.Font{ Bold: true, }, Alignment: &excelize.Alignment{ Horizontal: "center", }, } titleStyle, err := f.NewStyle(style) if err != nil { return } lastColumn := getColumnAlphabet(i) f.SetCellStyle("Sheet1", "A1", lastColumn+"2", titleStyle) // 设置列宽 f.SetColWidth("Sheet1", "A", "C", 30) f.SetColWidth("Sheet1", "D", lastColumn, 15) for i, v := range dataList { column := strconv.Itoa(i + 3) f.SetCellValue("Sheet1", "A"+column, v.LocationName) f.SetCellValue("Sheet1", "B"+column, v.ProductName) f.SetCellValue("Sheet1", "C"+column, v.ProductTypeName) f.SetCellValue("Sheet1", "D"+column, v.Amount) FillMoreUnitToExcel(v.Amount, v.AmountMoreUnits, 4, i+2, f) f.SetCellValue("Sheet1", "H"+column, v.Value) } fileName = fmt.Sprintf("位置报表%s.xlsx", time.Now().Format("2006-01-02-1504")) if err := f.SaveAs(fileName); err != nil { return fileName, err } return fileName, nil }