1、库存列表增加excel导出功能;

2、零售明细接口优化,展示所有支付方式;
3、系统用户接口优化,门店id改成复选;
This commit is contained in:
chenlin 2024-11-19 18:38:17 +08:00
parent 17d4131b1d
commit 7e7a48e008
4 changed files with 818 additions and 455 deletions

View File

@ -12,6 +12,8 @@ import (
"go-admin/tools"
"go-admin/tools/app"
"net/http"
"strconv"
"strings"
)
// GetSysUserList
@ -60,8 +62,23 @@ func GetSysUserList(c *gin.Context) {
}
data.RoleId, _ = tools.StringToInt(strRoleId)
nStoreId, _ := tools.StringToInt(strStoreId)
data.StoreId = uint32(nStoreId)
//nStoreId, _ := tools.StringToInt(strStoreId)
//data.StoreId = uint32(nStoreId)
var ids []int
if strStoreId != "" {
// 将字符串按逗号分隔成字符串数组
idsStrArray := strings.Split(strStoreId, ",")
// 将每个字符串转换为整数,并添加到 ids 数组
for _, idStr := range idsStrArray {
id, err := strconv.Atoi(idStr)
if err != nil {
tools.HasError(err, "门店id传参有误", -1)
}
ids = append(ids, id)
}
}
postId := c.Request.FormValue("postId")
data.PostId, _ = tools.StringToInt(postId)
@ -70,7 +87,7 @@ func GetSysUserList(c *gin.Context) {
data.DeptId, _ = tools.StringToInt(deptId)
data.DataScope = tools.GetUserIdStr(c)
result, count, exportUrl, err := data.GetPage(pageSize, pageIndex, export)
result, count, exportUrl, err := data.GetPage(pageSize, pageIndex, export, ids)
tools.HasError(err, "", -1)
var resp models.SysUserListResp
resp.List = result

View File

@ -1626,7 +1626,7 @@ func checkRoleMenu(c *gin.Context, menuName string) (bool, error) {
return exist, nil
}
// InventoryDetailListExport 导出库存商品列表
// InventoryDetailListExport 导出库存详情
func InventoryDetailListExport(list []ErpStockCommodity, c *gin.Context) (string, error) {
file := excelize.NewFile()
streamWriter, err := file.NewStreamWriter("Sheet1")
@ -1793,7 +1793,7 @@ type ErpStockListReq struct {
StoreId uint32 `json:"store_id"` // 门店编号
PageIndex int `json:"pageIndex"` // 页码
PageSize int `json:"pageSize"` // 页面条数
//IsExport uint32 `json:"is_export"` // 1-导出
IsExport uint32 `json:"is_export"` // 1-导出
}
type ErpStockListResp struct {
@ -2092,22 +2092,31 @@ func (m *ErpStockListReq) stockIsEmptyList(c *gin.Context) (*ErpStockListResp, e
SortStockCommodities(stockList)
// Paginate results
startIndex := page * m.PageSize
endIndex := startIndex + m.PageSize
if endIndex > len(stockList) {
endIndex = len(stockList)
if m.IsExport == 1 {
filePath, err := InventoryListExport(stockList, m.StockType)
if err != nil {
return nil, err
}
resp = &ErpStockListResp{}
resp.ExportUrl = filePath
} else {
// Paginate results
startIndex := page * m.PageSize
endIndex := startIndex + m.PageSize
if endIndex > len(stockList) {
endIndex = len(stockList)
}
// Slice the users based on pagination
pagedList := stockList[startIndex:endIndex]
//跟之前保持一致
resp.Total = len(stockList)
resp.PageIndex = page + 1
resp.PageSize = m.PageSize
resp.List = pagedList
}
// Slice the users based on pagination
pagedList := stockList[startIndex:endIndex]
//跟之前保持一致
resp.Total = len(stockList)
resp.PageIndex = page + 1
resp.PageSize = m.PageSize
resp.List = pagedList
return resp, nil
}
@ -2282,22 +2291,31 @@ func (m *ErpStockListReq) stockNoEmptyList(c *gin.Context) (*ErpStockListResp, e
SortStockCommodities(stockList)
// Paginate results
startIndex := page * m.PageSize
endIndex := startIndex + m.PageSize
if endIndex > len(stockList) {
endIndex = len(stockList)
if m.IsExport == 1 {
filePath, err := InventoryListExport(stockList, m.StockType)
if err != nil {
return nil, err
}
resp = &ErpStockListResp{}
resp.ExportUrl = filePath
} else {
// Paginate results
startIndex := page * m.PageSize
endIndex := startIndex + m.PageSize
if endIndex > len(stockList) {
endIndex = len(stockList)
}
// Slice the users based on pagination
pagedList := stockList[startIndex:endIndex]
//跟之前保持一致
resp.Total = len(stockList)
resp.PageIndex = page + 1
resp.PageSize = m.PageSize
resp.List = pagedList
}
// Slice the users based on pagination
pagedList := stockList[startIndex:endIndex]
//跟之前保持一致
resp.Total = len(stockList)
resp.PageIndex = page + 1
resp.PageSize = m.PageSize
resp.List = pagedList
return resp, nil
}
@ -2438,7 +2456,12 @@ func (m *ErpStockListReq) allCommodityList(c *gin.Context) (*ErpStockListResp, e
return resp, err
}
err = qs.Offset(page * m.PageSize).Limit(m.PageSize).Find(&commodities).Error
if m.IsExport == 1 {
err = qs.Find(&commodities).Error
} else {
err = qs.Offset(page * m.PageSize).Limit(m.PageSize).Find(&commodities).Error
}
if err != nil && err != RecordNotFound {
logger.Error("commodityList err", logger.Field("err", err))
return resp, err
@ -2469,15 +2492,124 @@ func (m *ErpStockListReq) allCommodityList(c *gin.Context) (*ErpStockListResp, e
SortStockCommodities(stockList)
//跟之前保持一致
resp.Total = int(count)
resp.PageIndex = page + 1
resp.PageSize = m.PageSize
resp.List = stockList
if m.IsExport == 1 {
filePath, err := InventoryListExport(stockList, m.StockType)
if err != nil {
return nil, err
}
resp = &ErpStockListResp{}
resp.ExportUrl = filePath
} else {
//跟之前保持一致
resp.Total = int(count)
resp.PageIndex = page + 1
resp.PageSize = m.PageSize
resp.List = stockList
}
return resp, nil
}
// InventoryListExport 导出库存列表
func InventoryListExport(list []ErpStock, nType uint32) (string, error) {
file := excelize.NewFile()
fSheet := "Sheet1"
var fileName string
switch nType {
case 2: // 有库存
fileName = time.Now().Format(TimeFormat) + "库存列表(有库存)" + ".xlsx"
case 3: // 无库存
fileName = time.Now().Format(TimeFormat) + "库存列表(无库存)" + ".xlsx"
default: // 全部
fileName = time.Now().Format(TimeFormat) + "库存列表(全部)" + ".xlsx"
}
url := config.ExportConfig.Url
fmt.Println("url fileName:", url+fileName)
// 设置标题行
title1 := []interface{}{"基础资料", "", "", "", "", "", "库存情况", ""}
title2 := []interface{}{"商品编号", "商品名称", "商品分类", "是否串码", "指导零售价", "最低零售价", "有效库存数量", "调出中数量"}
for i, _ := range title1 {
cell, _ := excelize.CoordinatesToCellName(1+i, 1)
err := file.SetCellValue(fSheet, cell, title1[i])
if err != nil {
logger.Errorf("file set value err:", err)
}
}
for i, _ := range title2 {
cell, _ := excelize.CoordinatesToCellName(1+i, 2)
err := file.SetCellValue(fSheet, cell, title2[i])
if err != nil {
logger.Errorf("file set value err:", err)
}
}
nExcelStartRow := 0
for rowId := 0; rowId < len(list); rowId++ {
isIMEIType := "是"
if list[rowId].IMEIType == 1 {
isIMEIType = "否"
}
row := []interface{}{
list[rowId].CommoditySerialNumber,
list[rowId].ErpCommodityName,
list[rowId].ErpCategoryName,
isIMEIType,
list[rowId].RetailPrice,
list[rowId].MinRetailPrice,
list[rowId].Count,
list[rowId].DispatchCount,
}
for j, _ := range row {
cell, _ := excelize.CoordinatesToCellName(1+j, nExcelStartRow+3)
err := file.SetCellValue(fSheet, cell, row[j])
if err != nil {
logger.Error("file set value err:", logger.Field("err", err))
}
}
nExcelStartRow++
}
// 设置所有单元格的样式: 居中、加边框
style, _ := file.NewStyle(`{"alignment":{"horizontal":"center","vertical":"center"},
"border":[{"type":"left","color":"000000","style":1},
{"type":"top","color":"000000","style":1},
{"type":"right","color":"000000","style":1},
{"type":"bottom","color":"000000","style":1}]}`)
// 合并单元格
_ = file.MergeCell(fSheet, "A1", "F1")
_ = file.MergeCell(fSheet, "G1", "H1")
//设置单元格高度
file.SetRowHeight("Sheet1", 1, 20)
// 设置单元格大小
file.SetColWidth("Sheet1", "A", "A", 13)
file.SetColWidth("Sheet1", "B", "B", 40)
file.SetColWidth("Sheet1", "C", "C", 13)
file.SetColWidth("Sheet1", "E", "E", 13)
file.SetColWidth("Sheet1", "F", "F", 13)
file.SetColWidth("Sheet1", "G", "G", 13)
file.SetColWidth("Sheet1", "H", "H", 13)
endRow := fmt.Sprintf("H"+"%d", nExcelStartRow+2)
// 应用样式到整个表格
_ = file.SetCellStyle("Sheet1", "A1", endRow, style)
fmt.Println("save fileName:", config.ExportConfig.Path+fileName)
if err := file.SaveAs(config.ExportConfig.Path + fileName); err != nil {
fmt.Println(err)
}
return url + fileName, nil
}
func ErpStockCommodityListSetAge(commodities []ErpStockCommodity) {
nowTime := time.Now()
for i, _ := range commodities {

File diff suppressed because it is too large Load Diff

View File

@ -270,7 +270,7 @@ type SysUserListResp struct {
List []SysUserPage `json:"list"` // 采购报表信息
}
func (e *SysUser) GetPage(pageSize int, pageIndex int, exportFlag int) ([]SysUserPage, int, string, error) {
func (e *SysUser) GetPage(pageSize int, pageIndex int, exportFlag int, storeList []int) ([]SysUserPage, int, string, error) {
if e.ShopperCode != "" {
var shopperCode ShopperPromotionCode
err := orm.Eloquent.Table("shopper_promotion_code").Where("code = ?", e.ShopperCode).Find(&shopperCode).Error
@ -311,8 +311,26 @@ func (e *SysUser) GetPage(pageSize int, pageIndex int, exportFlag int) ([]SysUse
table = table.Where("sys_user.nick_name = ?", e.NickName)
}
if e.StoreId != 0 {
table = table.Where("JSON_CONTAINS(store_data, ?)", fmt.Sprintf(`{"storeId":%d}`, e.StoreId))
//if e.StoreId != 0 {
// table = table.Where("JSON_CONTAINS(store_data, ?)", fmt.Sprintf(`{"storeId":%d}`, e.StoreId))
//}
// 假设 e.StoreId 是 []int 类型
if len(storeList) > 0 {
// 在 SQL 查询中使用 JSON_CONTAINS
// 如果需要匹配多个 storeId可以使用 OR 连接条件
var conditions []string
for _, storeId := range storeList {
condition := fmt.Sprintf("JSON_CONTAINS(store_data, '{\"storeId\": %d}')", storeId)
conditions = append(conditions, condition)
}
// 使用 OR 连接多个查询条件
// 最终的查询条件形如JSON_CONTAINS(store_data, '{"storeId": 13}') OR JSON_CONTAINS(store_data, '{"storeId": 19}')
queryCondition := strings.Join(conditions, " OR ")
// 将构造好的条件传递给 WHERE 子句
table = table.Where(queryCondition)
}
if e.DeptId != 0 {