package service
|
|
import (
|
"fmt"
|
"github.com/xuri/excelize/v2"
|
"strconv"
|
"time"
|
"wms/constvar"
|
"wms/models"
|
"wms/request"
|
"wms/response"
|
)
|
|
type HistoryFormsService struct{}
|
|
func NewHistoryFormsService() *HistoryFormsService {
|
return &HistoryFormsService{}
|
}
|
|
func (slf *HistoryFormsService) Query(params request.GetInventoryHistory) (result []*response.InventoryHistory, err error) {
|
search, err := slf.BuildSearch(params)
|
search = search.SetPreload(true)
|
if err != nil {
|
return nil, err
|
}
|
|
if params.Page > 0 && params.PageSize > 0 {
|
search = search.SetPage(params.Page, params.PageSize)
|
}
|
result = make([]*response.InventoryHistory, 0, params.PageSize)
|
list, err := search.FindNotTotal()
|
|
for _, v := range list {
|
data := &response.InventoryHistory{
|
Number: v.Number,
|
Date: v.UpdatedAt.Format("2006-01-02"),
|
ProductName: v.ProductName,
|
FromLocation: v.FromLocation,
|
ToLocation: v.ToLocation,
|
Amount: v.Amount,
|
AmountMoreUnits: nil,
|
Unit: v.Unit,
|
ContactedName: v.Operator,
|
BaseOperationType: v.BaseOperationType,
|
Weight: v.Weight,
|
ProductId: v.ProductId,
|
FromLocationId: v.FromLocationId,
|
ToLocationId: v.ToLocationId,
|
OperationId: v.OperationId,
|
OperationTypeName: v.OperationTypeName,
|
}
|
moreUnit := v.Product.MoreUnit
|
if moreUnit != nil && *moreUnit {
|
data.AmountMoreUnits = CreateMoreUnit(v.Amount, v.Product.MoreUnitList)
|
}
|
result = append(result, data)
|
}
|
|
return result, nil
|
}
|
|
func (slf *HistoryFormsService) BuildSearch(params request.GetInventoryHistory) (search *models.MoveHistorySearch, err error) {
|
search = models.NewMoveHistorySearch()
|
var (
|
ids []int
|
locationIds []int
|
)
|
if params.KeyWord != "" {
|
ids, _, err = SearchHistoryReport(params.KeyWord, params.BaseOperationType, params.Page, params.PageSize)
|
if err != nil {
|
return
|
}
|
if len(ids) == 0 {
|
return nil, nil
|
}
|
}
|
|
search.Orm = search.Orm.Model(&models.MoveHistory{}).
|
Select("number, updated_at, product_name, from_location_id, operation_id,to_location_id, amount, " +
|
"unit, operator, base_operation_type, weight, product_id, from_location, to_location, operation_type_name, weight").Order("id desc")
|
if len(ids) > 0 {
|
search.Orm = search.Orm.Where("id in ?", ids)
|
}
|
if params.BaseOperationType != 0 {
|
search.Orm = search.Orm.Where("base_operation_type = ?", params.BaseOperationType)
|
}
|
|
locationSearch := models.NewLocationSearch()
|
if params.WarehouseCode != "" {
|
locationSearch.SetJointName(params.WarehouseCode)
|
}
|
locations, err := locationSearch.FindNotTotal()
|
if err != nil {
|
return nil, err
|
}
|
for _, location := range locations {
|
locationIds = append(locationIds, location.Id)
|
}
|
search.Orm = search.Orm.Where("from_location_id in ? or to_location_id in ?", locationIds, locationIds)
|
|
return search, err
|
}
|
|
func (slf *HistoryFormsService) Count(params request.GetInventoryHistory) (total int64, err error) {
|
search, err := slf.BuildSearch(params)
|
if err != nil {
|
return 0, err
|
}
|
total, err = search.Count()
|
return
|
}
|
|
func (slf *HistoryFormsService) FetchAll(params request.GetInventoryHistory) (list []*response.InventoryHistory, err error) {
|
total, err := slf.Count(params)
|
if err != nil {
|
return nil, err
|
}
|
list = make([]*response.InventoryHistory, 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 *HistoryFormsService) Export(dataList []*response.InventoryHistory, params request.GetInventoryHistory) (filename string, err error) {
|
var fileName string
|
f := excelize.NewFile()
|
|
// 自定义表头
|
headers := []interface{}{"日期", "单号", "产品", "产品编码", "业务类型", "从", "至", map[string][]string{"数量": {"件", "匹", "米", "重量"}}, "单位", "重量"}
|
|
// 设置表头
|
if err := SetExcelHeader(headers, f); err != nil {
|
return "", err
|
}
|
|
style, err := SetHeaderStyle(f)
|
if err != nil {
|
return "", err
|
}
|
|
lastColumn := getColumnAlphabet(13)
|
f.SetCellStyle("Sheet1", "A1", lastColumn+"2", style)
|
// 设置列宽
|
f.SetColWidth("Sheet1", "A", "G", 30)
|
f.SetColWidth("Sheet1", "H", "K", 15)
|
|
for i, v := range dataList {
|
column := strconv.Itoa(i + 3)
|
f.SetCellValue("Sheet1", "A"+column, v.Date)
|
f.SetCellValue("Sheet1", "B"+column, v.Number)
|
f.SetCellValue("Sheet1", "C"+column, v.ProductName)
|
f.SetCellValue("Sheet1", "D"+column, v.ProductId)
|
f.SetCellValue("Sheet1", "E"+column, v.OperationTypeName)
|
f.SetCellValue("Sheet1", "F"+column, v.FromLocation)
|
f.SetCellValue("Sheet1", "G"+column, v.ToLocation)
|
f.SetCellValue("Sheet1", "H"+column, v.Amount)
|
FillMoreUnitToExcel(v.Amount, v.AmountMoreUnits, 7, i+3, f)
|
f.SetCellValue("Sheet1", "L"+column, v.Unit)
|
f.SetCellValue("Sheet1", "M"+column, v.Weight)
|
}
|
|
if params.BaseOperationType == constvar.BaseOperationTypeIncoming {
|
fileName = fmt.Sprintf("入库明细报表%s.xlsx", time.Now().Format("2006-01-02-1504"))
|
} else if params.BaseOperationType == constvar.BaseOperationTypeOutgoing {
|
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
|
}
|